Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 18 | #ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H |
| 19 | #define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 20 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 21 | #include <map> |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 22 | #include <mutex> |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 23 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 24 | #include <aidl/android/media/BnResourceManagerService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 26 | #include <media/MediaResource.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <utils/Errors.h> |
| 28 | #include <utils/KeyedVector.h> |
| 29 | #include <utils/String8.h> |
| 30 | #include <utils/threads.h> |
| 31 | #include <utils/Vector.h> |
| 32 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 35 | class DeathNotifier; |
| 36 | class ResourceManagerService; |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 37 | class ResourceObserverService; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 38 | class ServiceLog; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 39 | struct ProcessInfoInterface; |
| 40 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 41 | using Status = ::ndk::ScopedAStatus; |
| 42 | using ::aidl::android::media::IResourceManagerClient; |
| 43 | using ::aidl::android::media::BnResourceManagerService; |
| 44 | using ::aidl::android::media::MediaResourceParcel; |
| 45 | using ::aidl::android::media::MediaResourcePolicyParcel; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 46 | |
| 47 | typedef std::map<std::tuple< |
Jooyung Han | 3d564ff | 2020-02-22 00:46:06 +0900 | [diff] [blame] | 48 | MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 49 | MediaResourceParcel> ResourceList; |
| 50 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 51 | struct ResourceInfo { |
| 52 | int64_t clientId; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 53 | uid_t uid; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 54 | std::shared_ptr<IResourceManagerClient> client; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 55 | uintptr_t cookie{0}; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 56 | ResourceList resources; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 57 | bool pendingRemoval{false}; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 60 | // TODO: convert these to std::map |
| 61 | typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 62 | typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap; |
| 63 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 64 | class ResourceManagerService : public BnResourceManagerService { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 65 | public: |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 66 | struct SystemCallbackInterface : public RefBase { |
| 67 | virtual void noteStartVideo(int uid) = 0; |
| 68 | virtual void noteStopVideo(int uid) = 0; |
| 69 | virtual void noteResetVideo() = 0; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 70 | virtual bool requestCpusetBoost(bool enable) = 0; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 73 | static char const *getServiceName() { return "media.resource_manager"; } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 74 | static void instantiate(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 75 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 76 | virtual inline binder_status_t dump( |
| 77 | int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 78 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 79 | ResourceManagerService(); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 80 | explicit ResourceManagerService( |
| 81 | const sp<ProcessInfoInterface> &processInfo, |
| 82 | const sp<SystemCallbackInterface> &systemResource); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 83 | virtual ~ResourceManagerService(); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 84 | void setObserverService( |
| 85 | const std::shared_ptr<ResourceObserverService>& observerService); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 86 | |
| 87 | // IResourceManagerService interface |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 88 | Status config(const std::vector<MediaResourcePolicyParcel>& policies) override; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 89 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 90 | Status addResource( |
| 91 | int32_t pid, |
| 92 | int32_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 93 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 94 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 95 | const std::vector<MediaResourceParcel>& resources) override; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 96 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 97 | Status removeResource( |
| 98 | int32_t pid, |
| 99 | int64_t clientId, |
| 100 | const std::vector<MediaResourceParcel>& resources) override; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 101 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 102 | Status removeClient(int32_t pid, int64_t clientId) override; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 103 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 104 | // Tries to reclaim resource from processes with lower priority than the calling process |
| 105 | // according to the requested resources. |
| 106 | // Returns true if any resource has been reclaimed, otherwise returns false. |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 107 | Status reclaimResource( |
| 108 | int32_t callingPid, |
| 109 | const std::vector<MediaResourceParcel>& resources, |
| 110 | bool* _aidl_return) override; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 111 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 112 | Status overridePid( |
| 113 | int originalPid, |
| 114 | int newPid) override; |
| 115 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 116 | Status overrideProcessInfo( |
| 117 | const std::shared_ptr<IResourceManagerClient>& client, |
| 118 | int pid, |
| 119 | int procState, |
| 120 | int oomScore) override; |
| 121 | |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 122 | Status markClientForPendingRemoval(int32_t pid, int64_t clientId) override; |
| 123 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 124 | Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override; |
| 125 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 126 | Status removeResource(int pid, int64_t clientId, bool checkValid); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 127 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 128 | private: |
| 129 | friend class ResourceManagerServiceTest; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 130 | friend class DeathNotifier; |
| 131 | friend class OverrideProcessInfoDeathNotifier; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 132 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 133 | // Reclaims resources from |clients|. Returns true if reclaim succeeded |
| 134 | // for all clients. |
| 135 | bool reclaimInternal( |
| 136 | const Vector<std::shared_ptr<IResourceManagerClient>> &clients); |
| 137 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 138 | // Gets the list of all the clients who own the specified resource type. |
| 139 | // Returns false if any client belongs to a process with higher priority than the |
| 140 | // calling process. The clients will remain unchanged if returns false. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 141 | bool getAllClients_l(int callingPid, MediaResource::Type type, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 142 | Vector<std::shared_ptr<IResourceManagerClient>> *clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 143 | |
| 144 | // Gets the client who owns specified resource type from lowest possible priority process. |
| 145 | // Returns false if the calling process priority is not higher than the lowest process |
| 146 | // priority. The client will remain unchanged if returns false. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 147 | bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 148 | std::shared_ptr<IResourceManagerClient> *client); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 149 | |
| 150 | // Gets lowest priority process that has the specified resource type. |
| 151 | // Returns false if failed. The output parameters will remain unchanged if failed. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 152 | bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 153 | |
| 154 | // Gets the client who owns biggest piece of specified resource type from pid. |
| 155 | // Returns false if failed. The client will remain unchanged if failed. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 156 | bool getBiggestClient_l(int pid, MediaResource::Type type, |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 157 | std::shared_ptr<IResourceManagerClient> *client, |
| 158 | bool pendingRemovalOnly = false); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 159 | |
| 160 | bool isCallingPriorityHigher_l(int callingPid, int pid); |
| 161 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 162 | // A helper function basically calls getLowestPriorityBiggestClient_l and add |
| 163 | // the result client to the given Vector. |
| 164 | void getClientForResource_l(int callingPid, const MediaResourceParcel *res, |
| 165 | Vector<std::shared_ptr<IResourceManagerClient>> *clients); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 166 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 167 | void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo); |
| 168 | void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 169 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 170 | // Merge r2 into r1 |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 171 | void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 172 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 173 | // Get priority from process's pid |
| 174 | bool getPriority_l(int pid, int* priority); |
| 175 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 176 | void removeProcessInfoOverride(int pid); |
| 177 | |
| 178 | void removeProcessInfoOverride_l(int pid); |
| 179 | uintptr_t addCookieAndLink_l(::ndk::SpAIBinder binder, const sp<DeathNotifier>& notifier); |
| 180 | void removeCookieAndUnlink_l(::ndk::SpAIBinder binder, uintptr_t cookie); |
| 181 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 182 | mutable Mutex mLock; |
| 183 | sp<ProcessInfoInterface> mProcessInfo; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 184 | sp<SystemCallbackInterface> mSystemCB; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 185 | sp<ServiceLog> mServiceLog; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 186 | PidResourceInfosMap mMap; |
| 187 | bool mSupportsMultipleSecureCodecs; |
| 188 | bool mSupportsSecureWithNonSecureCodec; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 189 | int32_t mCpuBoostCount; |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 190 | ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 191 | struct ProcessInfoOverride { |
| 192 | uintptr_t cookie; |
| 193 | std::shared_ptr<IResourceManagerClient> client; |
| 194 | }; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 195 | std::map<int, int> mOverridePidMap; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 196 | std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap; |
| 197 | static std::mutex sCookieLock; |
| 198 | static uintptr_t sCookieCounter GUARDED_BY(sCookieLock); |
| 199 | static std::map<uintptr_t, sp<DeathNotifier> > sCookieToDeathNotifierMap |
| 200 | GUARDED_BY(sCookieLock); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 201 | std::shared_ptr<ResourceObserverService> mObserverService; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | // ---------------------------------------------------------------------------- |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 205 | } // namespace android |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 206 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 207 | #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H |