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 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ResourceManagerService" |
| 20 | #include <utils/Log.h> |
| 21 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 28 | #include <media/MediaResourcePolicy.h> |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ABase.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 30 | #include <mediautils/BatteryNotifier.h> |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 31 | #include <mediautils/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 32 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <unistd.h> |
| 38 | |
Steven Moreland | 0a0ff0b | 2021-04-02 00:06:01 +0000 | [diff] [blame] | 39 | #include "IMediaResourceMonitor.h" |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 40 | #include "ResourceManagerMetrics.h" |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 41 | #include "ResourceManagerService.h" |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 42 | #include "ResourceObserverService.h" |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 43 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 44 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 47 | class DeathNotifier : public std::enable_shared_from_this<DeathNotifier> { |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 48 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 49 | // BinderDiedContext defines the cookie that is passed as DeathRecipient. |
| 50 | // Since this can maintain more context than a raw pointer, we can |
| 51 | // validate the scope of DeathNotifier, before deferencing it upon the binder death. |
| 52 | struct BinderDiedContext { |
| 53 | std::weak_ptr<DeathNotifier> mDeathNotifier; |
| 54 | }; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 55 | public: |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 56 | DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client, |
| 57 | const std::shared_ptr<ResourceManagerService>& service, |
| 58 | const ClientInfoParcel& clientInfo, |
| 59 | AIBinder_DeathRecipient* recipient); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 60 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 61 | virtual ~DeathNotifier() { |
| 62 | unlink(); |
| 63 | } |
| 64 | |
| 65 | void unlink() { |
| 66 | if (mClient != nullptr) { |
| 67 | // Register for the callbacks by linking to death notification. |
| 68 | AIBinder_unlinkToDeath(mClient->asBinder().get(), mRecipient, mCookie); |
| 69 | mClient = nullptr; |
| 70 | } |
| 71 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 72 | |
| 73 | // Implement death recipient |
| 74 | static void BinderDiedCallback(void* cookie); |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 75 | static void BinderUnlinkedCallback(void* cookie); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 76 | virtual void binderDied(); |
| 77 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 78 | private: |
| 79 | void link() { |
| 80 | // Create the context that is passed as cookie to the binder death notification. |
| 81 | // The context gets deleted at BinderUnlinkedCallback. |
| 82 | mCookie = new BinderDiedContext{.mDeathNotifier = weak_from_this()}; |
| 83 | // Register for the callbacks by linking to death notification. |
| 84 | AIBinder_linkToDeath(mClient->asBinder().get(), mRecipient, mCookie); |
| 85 | } |
| 86 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 87 | protected: |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 88 | std::shared_ptr<IResourceManagerClient> mClient; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 89 | std::weak_ptr<ResourceManagerService> mService; |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 90 | const ClientInfoParcel mClientInfo; |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 91 | AIBinder_DeathRecipient* mRecipient; |
| 92 | BinderDiedContext* mCookie; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 95 | DeathNotifier::DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client, |
| 96 | const std::shared_ptr<ResourceManagerService>& service, |
| 97 | const ClientInfoParcel& clientInfo, |
| 98 | AIBinder_DeathRecipient* recipient) |
| 99 | : mClient(client), mService(service), mClientInfo(clientInfo), |
| 100 | mRecipient(recipient), mCookie(nullptr) { |
| 101 | link(); |
| 102 | } |
| 103 | |
| 104 | //static |
| 105 | void DeathNotifier::BinderUnlinkedCallback(void* cookie) { |
| 106 | BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie); |
| 107 | // Since we don't need the context anymore, we are deleting it now. |
| 108 | delete context; |
| 109 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 110 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 111 | //static |
| 112 | void DeathNotifier::BinderDiedCallback(void* cookie) { |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 113 | BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie); |
| 114 | |
| 115 | // Validate the context and check if the DeathNotifier object is still in scope. |
| 116 | if (context != nullptr) { |
| 117 | std::shared_ptr<DeathNotifier> thiz = context->mDeathNotifier.lock(); |
| 118 | if (thiz != nullptr) { |
| 119 | thiz->binderDied(); |
| 120 | } else { |
| 121 | ALOGI("DeathNotifier is out of scope already"); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 122 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 123 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 124 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 125 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 126 | void DeathNotifier::binderDied() { |
| 127 | // Don't check for pid validity since we know it's already dead. |
| 128 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 129 | if (service == nullptr) { |
| 130 | ALOGW("ResourceManagerService is dead as well."); |
| 131 | return; |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 132 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 133 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 134 | service->overridePid(mClientInfo.pid, -1); |
Henry Fang | b35141c | 2020-06-29 13:11:39 -0700 | [diff] [blame] | 135 | // thiz is freed in the call below, so it must be last call referring thiz |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 136 | service->removeResource(mClientInfo, false /*checkValid*/); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 137 | } |
Henry Fang | b35141c | 2020-06-29 13:11:39 -0700 | [diff] [blame] | 138 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 139 | class OverrideProcessInfoDeathNotifier : public DeathNotifier { |
| 140 | public: |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 141 | OverrideProcessInfoDeathNotifier(const std::shared_ptr<IResourceManagerClient>& client, |
| 142 | const std::shared_ptr<ResourceManagerService>& service, |
| 143 | const ClientInfoParcel& clientInfo, |
| 144 | AIBinder_DeathRecipient* recipient) |
| 145 | : DeathNotifier(client, service, clientInfo, recipient) {} |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 146 | |
| 147 | virtual ~OverrideProcessInfoDeathNotifier() {} |
| 148 | |
| 149 | virtual void binderDied(); |
| 150 | }; |
| 151 | |
| 152 | void OverrideProcessInfoDeathNotifier::binderDied() { |
| 153 | // Don't check for pid validity since we know it's already dead. |
| 154 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 155 | if (service == nullptr) { |
| 156 | ALOGW("ResourceManagerService is dead as well."); |
| 157 | return; |
| 158 | } |
| 159 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 160 | service->removeProcessInfoOverride(mClientInfo.pid); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 161 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 162 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 163 | template <typename T> |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 164 | static String8 getString(const std::vector<T>& items) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 165 | String8 itemsStr; |
| 166 | for (size_t i = 0; i < items.size(); ++i) { |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 167 | itemsStr.appendFormat("%s ", toString(items[i]).c_str()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 168 | } |
| 169 | return itemsStr; |
| 170 | } |
| 171 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 172 | static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
Girish | 429f859 | 2022-10-13 04:27:00 +0000 | [diff] [blame] | 173 | const MediaResourceParcel& resource) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 174 | if (type != resource.type) { |
| 175 | return false; |
| 176 | } |
| 177 | switch (type) { |
| 178 | // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so |
| 179 | // compare the subtypes as well. |
| 180 | case MediaResource::Type::kSecureCodec: |
| 181 | case MediaResource::Type::kNonSecureCodec: |
| 182 | if (resource.subType == subType) { |
| 183 | return true; |
| 184 | } |
| 185 | break; |
| 186 | // Non-codec resources are not segregated by the subtype (e.g. video vs. audio). |
| 187 | default: |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
| 194 | const ResourceList& resources) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 195 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 196 | if (hasResourceType(type, subType, it->second)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 203 | static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
| 204 | const ResourceInfos& infos) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 205 | for (const auto& [id, info] : infos) { |
| 206 | if (hasResourceType(type, subType, info.resources)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 213 | static ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 214 | PidResourceInfosMap::iterator found = map.find(pid); |
| 215 | if (found == map.end()) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 216 | // new pid |
| 217 | ResourceInfos infosForPid; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 218 | auto [it, inserted] = map.emplace(pid, infosForPid); |
| 219 | found = it; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 222 | return found->second; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 225 | static ResourceInfo& getResourceInfoForEdit(uid_t uid, int64_t clientId, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 226 | const std::string& name, |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 227 | const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 228 | ResourceInfos::iterator found = infos.find(clientId); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 229 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 230 | if (found == infos.end()) { |
| 231 | ResourceInfo info{.uid = uid, |
| 232 | .clientId = clientId, |
| 233 | .name = name.empty()? "<unknown client>" : name, |
| 234 | .client = client, |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 235 | .deathNotifier = nullptr, |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 236 | .pendingRemoval = false}; |
| 237 | auto [it, inserted] = infos.emplace(clientId, info); |
| 238 | found = it; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 239 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 240 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 241 | return found->second; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 244 | static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) { |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 245 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 246 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 247 | if (binder != NULL) { |
| 248 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 249 | for (size_t i = 0; i < resources.size(); ++i) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 250 | switch (resources[i].subType) { |
| 251 | case MediaResource::SubType::kAudioCodec: |
| 252 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
| 253 | break; |
| 254 | case MediaResource::SubType::kVideoCodec: |
| 255 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 256 | break; |
| 257 | case MediaResource::SubType::kImageCodec: |
| 258 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC); |
| 259 | break; |
| 260 | case MediaResource::SubType::kUnspecifiedSubType: |
| 261 | break; |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 262 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 267 | binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 268 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 269 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 270 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 271 | result.format("Permission Denial: " |
| 272 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 273 | AIBinder_getCallingPid(), |
| 274 | AIBinder_getCallingUid()); |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 275 | write(fd, result.c_str(), result.size()); |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 276 | return PERMISSION_DENIED; |
| 277 | } |
| 278 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 279 | PidResourceInfosMap mapCopy; |
| 280 | bool supportsMultipleSecureCodecs; |
| 281 | bool supportsSecureWithNonSecureCodec; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 282 | std::map<int, int> overridePidMapCopy; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 283 | String8 serviceLog; |
| 284 | { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 285 | std::scoped_lock lock{mLock}; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 286 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 287 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 288 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 289 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 290 | overridePidMapCopy = mOverridePidMap; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | const size_t SIZE = 256; |
| 294 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 295 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 296 | result.append(buffer); |
| 297 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 298 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 299 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 300 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 301 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 302 | result.append(buffer); |
| 303 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 304 | result.append(" Processes:\n"); |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 305 | for (const auto& [pid, infos] : mapCopy) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 306 | snprintf(buffer, SIZE, " Pid: %d\n", pid); |
| 307 | result.append(buffer); |
| 308 | int priority = 0; |
| 309 | if (getPriority_l(pid, &priority)) { |
| 310 | snprintf(buffer, SIZE, " Priority: %d\n", priority); |
| 311 | } else { |
| 312 | snprintf(buffer, SIZE, " Priority: <unknown>\n"); |
| 313 | } |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 314 | result.append(buffer); |
| 315 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 316 | for (const auto& [infoKey, info] : infos) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 317 | result.append(" Client:\n"); |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 318 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 319 | result.append(buffer); |
| 320 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 321 | std::string clientName = info.name; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 322 | snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 323 | result.append(buffer); |
| 324 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 325 | const ResourceList& resources = info.resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 326 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 327 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 328 | snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 329 | result.append(buffer); |
| 330 | } |
| 331 | } |
| 332 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 333 | result.append(" Process Pid override:\n"); |
| 334 | for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) { |
| 335 | snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n", |
| 336 | it->first, it->second); |
| 337 | result.append(buffer); |
| 338 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 339 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 340 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 341 | |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 342 | write(fd, result.c_str(), result.size()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 343 | return OK; |
| 344 | } |
| 345 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 346 | struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 347 | SystemCallbackImpl() : mClientToken(new BBinder()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 348 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 349 | virtual void noteStartVideo(int uid) override { |
| 350 | BatteryNotifier::getInstance().noteStartVideo(uid); |
| 351 | } |
| 352 | virtual void noteStopVideo(int uid) override { |
| 353 | BatteryNotifier::getInstance().noteStopVideo(uid); |
| 354 | } |
| 355 | virtual void noteResetVideo() override { |
| 356 | BatteryNotifier::getInstance().noteResetVideo(); |
| 357 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 358 | virtual bool requestCpusetBoost(bool enable) override { |
| 359 | return android::requestCpusetBoost(enable, mClientToken); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | protected: |
| 363 | virtual ~SystemCallbackImpl() {} |
| 364 | |
| 365 | private: |
| 366 | DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 367 | sp<IBinder> mClientToken; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | ResourceManagerService::ResourceManagerService() |
| 371 | : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {} |
| 372 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 373 | ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo, |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 374 | const sp<SystemCallbackInterface> &systemResource) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 375 | : mProcessInfo(processInfo), |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 376 | mSystemCB(systemResource), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 377 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 378 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 379 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 380 | mCpuBoostCount(0), |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 381 | mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient( |
| 382 | AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback))) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 383 | mSystemCB->noteResetVideo(); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 384 | // Create ResourceManagerMetrics that handles all the metrics. |
| 385 | mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 386 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 387 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 388 | //static |
| 389 | void ResourceManagerService::instantiate() { |
| 390 | std::shared_ptr<ResourceManagerService> service = |
| 391 | ::ndk::SharedRefBase::make<ResourceManagerService>(); |
| 392 | binder_status_t status = |
Charles Chen | 5b097ef | 2023-03-15 01:21:22 +0000 | [diff] [blame] | 393 | AServiceManager_addServiceWithFlags( |
Charles Chen | e55549f | 2023-03-15 01:21:22 +0000 | [diff] [blame] | 394 | service->asBinder().get(), getServiceName(), |
| 395 | AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 396 | if (status != STATUS_OK) { |
| 397 | return; |
| 398 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 399 | |
| 400 | std::shared_ptr<ResourceObserverService> observerService = |
| 401 | ResourceObserverService::instantiate(); |
| 402 | |
| 403 | if (observerService != nullptr) { |
| 404 | service->setObserverService(observerService); |
| 405 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 406 | // TODO: mediaserver main() is already starting the thread pool, |
| 407 | // move this to mediaserver main() when other services in mediaserver |
| 408 | // are converted to ndk-platform aidl. |
| 409 | //ABinderProcess_startThreadPool(); |
| 410 | } |
| 411 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 412 | ResourceManagerService::~ResourceManagerService() {} |
| 413 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 414 | void ResourceManagerService::setObserverService( |
| 415 | const std::shared_ptr<ResourceObserverService>& observerService) { |
| 416 | mObserverService = observerService; |
| 417 | } |
| 418 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 419 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 420 | String8 log = String8::format("config(%s)", getString(policies).c_str()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 421 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 422 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 423 | std::scoped_lock lock{mLock}; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 424 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 425 | const std::string &type = policies[i].type; |
| 426 | const std::string &value = policies[i].value; |
| 427 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 428 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 429 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 430 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 433 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 436 | void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, |
| 437 | const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 438 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 439 | if (resource.type == MediaResource::Type::kCpuBoost |
| 440 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 441 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 442 | // could have died, if we only do it the first time subsequent instances |
| 443 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 444 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 445 | ALOGW("couldn't request cpuset boost"); |
| 446 | } |
| 447 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 448 | } else if (resource.type == MediaResource::Type::kBattery |
| 449 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 450 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 454 | void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, |
| 455 | const ResourceInfo& clientInfo) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 456 | if (resource.type == MediaResource::Type::kCpuBoost |
| 457 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 458 | && mCpuBoostCount > 0) { |
| 459 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 460 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 461 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 462 | } else if (resource.type == MediaResource::Type::kBattery |
| 463 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 464 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 468 | void ResourceManagerService::mergeResources(MediaResourceParcel& r1, |
| 469 | const MediaResourceParcel& r2) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 470 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 471 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 472 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 473 | // value goes lower when the session is used more often. During reclaim |
| 474 | // the session with the highest value (lowest usage) would be closed. |
| 475 | if (r2.value < INT64_MAX - r1.value) { |
| 476 | r1.value += r2.value; |
| 477 | if (r1.value < 0) { |
| 478 | r1.value = 0; |
| 479 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 480 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 481 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 485 | Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 486 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 487 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 488 | int32_t pid = clientInfo.pid; |
| 489 | int32_t uid = clientInfo.uid; |
| 490 | int64_t clientId = clientInfo.id; |
| 491 | const std::string& name = clientInfo.name; |
| 492 | String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)", |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 493 | pid, uid, (long long) clientId, getString(resources).c_str()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 494 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 495 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 496 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 497 | if (!mProcessInfo->isPidUidTrusted(pid, uid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 498 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 499 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 500 | ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d", |
| 501 | __FUNCTION__, pid, uid, callingPid, callingUid); |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 502 | pid = callingPid; |
| 503 | uid = callingUid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 504 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 505 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 506 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 507 | ResourceList resourceAdded; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 508 | |
| 509 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 510 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 511 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 512 | |
| 513 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 514 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 515 | continue; |
| 516 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 517 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 518 | if (res.value <= 0) { |
| 519 | // We can't init a new entry with negative value, although it's allowed |
| 520 | // to merge in negative values after the initial add. |
| 521 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 522 | continue; |
| 523 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 524 | onFirstAdded(res, info); |
| 525 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 526 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 527 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 528 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 529 | // Add it to the list of added resources for observers. |
| 530 | auto it = resourceAdded.find(resType); |
| 531 | if (it == resourceAdded.end()) { |
| 532 | resourceAdded[resType] = res; |
| 533 | } else { |
| 534 | mergeResources(it->second, res); |
| 535 | } |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 536 | } |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 537 | if (info.deathNotifier == nullptr && client != nullptr) { |
| 538 | info.deathNotifier = std::make_shared<DeathNotifier>( |
| 539 | client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get()); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 540 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 541 | if (mObserverService != nullptr && !resourceAdded.empty()) { |
| 542 | mObserverService->onResourceAdded(uid, pid, resourceAdded); |
| 543 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 544 | notifyResourceGranted(pid, resources); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 545 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 546 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 549 | Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 550 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 551 | int32_t pid = clientInfo.pid; |
| 552 | int32_t uid = clientInfo.uid; |
| 553 | int64_t clientId = clientInfo.id; |
| 554 | String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)", |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 555 | pid, uid, (long long) clientId, getString(resources).c_str()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 556 | mServiceLog->add(log); |
| 557 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 558 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 559 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 560 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 561 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 562 | pid, callingPid); |
| 563 | pid = callingPid; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 564 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 565 | PidResourceInfosMap::iterator found = mMap.find(pid); |
| 566 | if (found == mMap.end()) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 567 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 568 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 569 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 570 | ResourceInfos& infos = found->second; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 571 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 572 | ResourceInfos::iterator foundClient = infos.find(clientId); |
| 573 | if (foundClient == infos.end()) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 574 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 575 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 578 | ResourceInfo& info = foundClient->second; |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 579 | ResourceList resourceRemoved; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 580 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 581 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 582 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 583 | |
| 584 | if (res.value < 0) { |
| 585 | ALOGW("Ignoring request to remove negative value of resource"); |
| 586 | continue; |
| 587 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 588 | // ignore if we don't have it |
| 589 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 590 | MediaResourceParcel &resource = info.resources[resType]; |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 591 | MediaResourceParcel actualRemoved = res; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 592 | if (resource.value > res.value) { |
| 593 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 594 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 595 | onLastRemoved(res, info); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 596 | actualRemoved.value = resource.value; |
Chong Zhang | 102b3e3 | 2020-11-02 12:21:50 -0800 | [diff] [blame] | 597 | info.resources.erase(resType); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Add it to the list of removed resources for observers. |
| 601 | auto it = resourceRemoved.find(resType); |
| 602 | if (it == resourceRemoved.end()) { |
| 603 | resourceRemoved[resType] = actualRemoved; |
| 604 | } else { |
| 605 | mergeResources(it->second, actualRemoved); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 609 | if (mObserverService != nullptr && !resourceRemoved.empty()) { |
| 610 | mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved); |
| 611 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 612 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 615 | Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) { |
| 616 | removeResource(clientInfo, true /*checkValid*/); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 617 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 618 | } |
| 619 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 620 | Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) { |
| 621 | int32_t pid = clientInfo.pid; |
| 622 | int32_t uid = clientInfo.uid; |
| 623 | int64_t clientId = clientInfo.id; |
| 624 | String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)", |
| 625 | pid, uid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 626 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 627 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 628 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 629 | if (checkValid && !mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 630 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 631 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 632 | pid, callingPid); |
| 633 | pid = callingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 634 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 635 | PidResourceInfosMap::iterator found = mMap.find(pid); |
| 636 | if (found == mMap.end()) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 637 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 638 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 639 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 640 | ResourceInfos& infos = found->second; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 641 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 642 | ResourceInfos::iterator foundClient = infos.find(clientId); |
| 643 | if (foundClient == infos.end()) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 644 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 645 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 646 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 647 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 648 | const ResourceInfo& info = foundClient->second; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 649 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 650 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 651 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 652 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 653 | // Since this client has been removed, update the metrics collector. |
| 654 | mResourceManagerMetrics->notifyClientReleased(clientInfo); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 655 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 656 | if (mObserverService != nullptr && !info.resources.empty()) { |
| 657 | mObserverService->onResourceRemoved(info.uid, pid, info.resources); |
| 658 | } |
| 659 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 660 | infos.erase(foundClient); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 661 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 664 | void ResourceManagerService::getClientForResource_l(int callingPid, |
| 665 | const MediaResourceParcel *res, |
| 666 | PidUidVector* idVector, |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 667 | std::vector<std::shared_ptr<IResourceManagerClient>>* clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 668 | if (res == NULL) { |
| 669 | return; |
| 670 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 671 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 672 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 673 | clients->push_back(client); |
| 674 | } |
| 675 | } |
| 676 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 677 | Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo, |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 678 | const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 679 | int32_t callingPid = clientInfo.pid; |
| 680 | std::string clientName = clientInfo.name; |
| 681 | String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)", |
Tomasz Wasilczyk | 09977ff | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 682 | callingPid, clientInfo.uid, getString(resources).c_str()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 683 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 684 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 685 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 686 | std::vector<std::shared_ptr<IResourceManagerClient>> clients; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 687 | PidUidVector idVector; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 688 | { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 689 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 690 | if (!mProcessInfo->isPidTrusted(callingPid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 691 | pid_t actualCallingPid = IPCThreadState::self()->getCallingPid(); |
| 692 | ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__, |
| 693 | callingPid, actualCallingPid); |
| 694 | callingPid = actualCallingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 695 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 696 | const MediaResourceParcel *secureCodec = NULL; |
| 697 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 698 | const MediaResourceParcel *graphicMemory = NULL; |
| 699 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 700 | for (size_t i = 0; i < resources.size(); ++i) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 701 | switch (resources[i].type) { |
| 702 | case MediaResource::Type::kSecureCodec: |
| 703 | secureCodec = &resources[i]; |
| 704 | break; |
| 705 | case MediaResource::Type::kNonSecureCodec: |
| 706 | nonSecureCodec = &resources[i]; |
| 707 | break; |
| 708 | case MediaResource::Type::kGraphicMemory: |
| 709 | graphicMemory = &resources[i]; |
| 710 | break; |
| 711 | case MediaResource::Type::kDrmSession: |
| 712 | drmSession = &resources[i]; |
| 713 | break; |
| 714 | default: |
| 715 | break; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | // first pass to handle secure/non-secure codec conflict |
| 720 | if (secureCodec != NULL) { |
| 721 | if (!mSupportsMultipleSecureCodecs) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 722 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 723 | secureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 724 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | if (!mSupportsSecureWithNonSecureCodec) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 728 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 729 | secureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 730 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | if (nonSecureCodec != NULL) { |
| 735 | if (!mSupportsSecureWithNonSecureCodec) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 736 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 737 | nonSecureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 738 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 742 | if (drmSession != NULL) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 743 | getClientForResource_l(callingPid, drmSession, &idVector, &clients); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 744 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 745 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 748 | |
| 749 | if (clients.size() == 0) { |
| 750 | // if no secure/non-secure codec conflict, run second pass to handle other resources. |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 751 | getClientForResource_l(callingPid, graphicMemory, &idVector, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 752 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 753 | |
| 754 | if (clients.size() == 0) { |
| 755 | // if we are here, run the third pass to free one codec with the same type. |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 756 | getClientForResource_l(callingPid, secureCodec, &idVector, &clients); |
| 757 | getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | if (clients.size() == 0) { |
| 761 | // if we are here, run the fourth pass to free one codec with the different type. |
| 762 | if (secureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 763 | MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 764 | getClientForResource_l(callingPid, &temp, &idVector, &clients); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 765 | } |
| 766 | if (nonSecureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 767 | MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 768 | getClientForResource_l(callingPid, &temp, &idVector, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 773 | *_aidl_return = reclaimUnconditionallyFrom(clients); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 774 | |
| 775 | // Log Reclaim Pushed Atom to statsd |
| 776 | pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return); |
| 777 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 778 | return Status::ok(); |
| 779 | } |
| 780 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 781 | void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo, |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 782 | const std::vector<std::shared_ptr<IResourceManagerClient>>& clients, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 783 | const PidUidVector& idVector, bool reclaimed) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 784 | int32_t callingPid = clientInfo.pid; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 785 | int requesterPriority = -1; |
| 786 | getPriority_l(callingPid, &requesterPriority); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 787 | std::vector<int> priorities; |
| 788 | priorities.push_back(requesterPriority); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 789 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 790 | for (PidUidVector::const_reference id : idVector) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 791 | int targetPriority = -1; |
| 792 | getPriority_l(id.first, &targetPriority); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 793 | priorities.push_back(targetPriority); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 794 | } |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 795 | mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients, |
| 796 | idVector, reclaimed); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 799 | bool ResourceManagerService::reclaimUnconditionallyFrom( |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 800 | const std::vector<std::shared_ptr<IResourceManagerClient>>& clients) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 801 | if (clients.size() == 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 802 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 805 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 806 | for (size_t i = 0; i < clients.size(); ++i) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 807 | String8 log = String8::format("reclaimResource from client %p", clients[i].get()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 808 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 809 | bool success; |
| 810 | Status status = clients[i]->reclaimResource(&success); |
| 811 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 812 | failedClient = clients[i]; |
| 813 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 816 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 817 | if (failedClient == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 818 | return true; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Brian Lindahl | 9f626cf | 2022-04-25 13:29:59 +0200 | [diff] [blame] | 821 | int failedClientPid = -1; |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 822 | { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 823 | std::scoped_lock lock{mLock}; |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 824 | bool found = false; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 825 | for (auto& [pid, infos] : mMap) { |
| 826 | for (const auto& [id, info] : infos) { |
| 827 | if (info.client == failedClient) { |
| 828 | infos.erase(id); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 829 | found = true; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 830 | break; |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | if (found) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 834 | failedClientPid = pid; |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | } |
Brian Lindahl | 9f626cf | 2022-04-25 13:29:59 +0200 | [diff] [blame] | 838 | if (found) { |
| 839 | ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid); |
| 840 | } else { |
| 841 | ALOGW("Failed to reclaim resources from unlocateable client"); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 845 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 848 | Status ResourceManagerService::overridePid(int originalPid, int newPid) { |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 849 | String8 log = String8::format("overridePid(originalPid %d, newPid %d)", |
| 850 | originalPid, newPid); |
| 851 | mServiceLog->add(log); |
| 852 | |
| 853 | // allow if this is called from the same process or the process has |
| 854 | // permission. |
| 855 | if ((AIBinder_getCallingPid() != getpid()) && |
| 856 | (checkCallingPermission(String16( |
| 857 | "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) { |
| 858 | ALOGE( |
| 859 | "Permission Denial: can't access overridePid method from pid=%d, " |
| 860 | "self pid=%d\n", |
| 861 | AIBinder_getCallingPid(), getpid()); |
| 862 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 863 | } |
| 864 | |
| 865 | { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 866 | std::scoped_lock lock{mLock}; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 867 | mOverridePidMap.erase(originalPid); |
| 868 | if (newPid != -1) { |
| 869 | mOverridePidMap.emplace(originalPid, newPid); |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 870 | mResourceManagerMetrics->addPid(newPid); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
| 874 | return Status::ok(); |
| 875 | } |
| 876 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 877 | Status ResourceManagerService::overrideProcessInfo( |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 878 | const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState, |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 879 | int oomScore) { |
| 880 | String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)", |
| 881 | pid, procState, oomScore); |
| 882 | mServiceLog->add(log); |
| 883 | |
| 884 | // Only allow the override if the caller already can access process state and oom scores. |
| 885 | int callingPid = AIBinder_getCallingPid(); |
| 886 | if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16( |
| 887 | "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) { |
| 888 | ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid); |
| 889 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 890 | } |
| 891 | |
| 892 | if (client == nullptr) { |
| 893 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 894 | } |
| 895 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 896 | std::scoped_lock lock{mLock}; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 897 | removeProcessInfoOverride_l(pid); |
| 898 | |
| 899 | if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) { |
| 900 | // Override value is rejected by ProcessInfo. |
| 901 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 902 | } |
| 903 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 904 | ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid), |
| 905 | .uid = 0, |
| 906 | .id = 0, |
| 907 | .name = "<unknown client>"}; |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 908 | auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>( |
| 909 | client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get()); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 910 | |
Girish | f1d166c | 2023-07-20 22:35:29 +0000 | [diff] [blame] | 911 | mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client}); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 912 | |
| 913 | return Status::ok(); |
| 914 | } |
| 915 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 916 | void ResourceManagerService::removeProcessInfoOverride(int pid) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 917 | std::scoped_lock lock{mLock}; |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 918 | |
| 919 | removeProcessInfoOverride_l(pid); |
| 920 | } |
| 921 | |
| 922 | void ResourceManagerService::removeProcessInfoOverride_l(int pid) { |
| 923 | auto it = mProcessInfoOverrideMap.find(pid); |
| 924 | if (it == mProcessInfoOverrideMap.end()) { |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | mProcessInfo->removeProcessInfoOverride(pid); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 929 | mProcessInfoOverrideMap.erase(pid); |
| 930 | } |
| 931 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 932 | Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) { |
| 933 | int32_t pid = clientInfo.pid; |
| 934 | int64_t clientId = clientInfo.id; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 935 | String8 log = String8::format( |
| 936 | "markClientForPendingRemoval(pid %d, clientId %lld)", |
| 937 | pid, (long long) clientId); |
| 938 | mServiceLog->add(log); |
| 939 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 940 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 941 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 942 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 943 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 944 | pid, callingPid); |
| 945 | pid = callingPid; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 946 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 947 | PidResourceInfosMap::iterator found = mMap.find(pid); |
| 948 | if (found == mMap.end()) { |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 949 | ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld", |
| 950 | pid, (long long)clientId); |
| 951 | return Status::ok(); |
| 952 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 953 | ResourceInfos& infos = found->second; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 954 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 955 | ResourceInfos::iterator foundClient = infos.find(clientId); |
| 956 | if (foundClient == infos.end()) { |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 957 | ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId); |
| 958 | return Status::ok(); |
| 959 | } |
| 960 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 961 | ResourceInfo& info = foundClient->second; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 962 | info.pendingRemoval = true; |
| 963 | return Status::ok(); |
| 964 | } |
| 965 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 966 | Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) { |
| 967 | String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid); |
| 968 | mServiceLog->add(log); |
| 969 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 970 | std::vector<std::shared_ptr<IResourceManagerClient>> clients; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 971 | { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 972 | std::scoped_lock lock{mLock}; |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 973 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 974 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 975 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 976 | pid, callingPid); |
| 977 | pid = callingPid; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | for (MediaResource::Type type : {MediaResource::Type::kSecureCodec, |
| 981 | MediaResource::Type::kNonSecureCodec, |
| 982 | MediaResource::Type::kGraphicMemory, |
| 983 | MediaResource::Type::kDrmSession}) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 984 | switch (type) { |
| 985 | // Codec resources are segregated by audio, video and image domains. |
| 986 | case MediaResource::Type::kSecureCodec: |
| 987 | case MediaResource::Type::kNonSecureCodec: |
| 988 | for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec, |
| 989 | MediaResource::SubType::kVideoCodec, |
| 990 | MediaResource::SubType::kImageCodec}) { |
| 991 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 992 | uid_t uid = 0; |
| 993 | if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 994 | clients.push_back(client); |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 995 | continue; |
| 996 | } |
| 997 | } |
| 998 | break; |
| 999 | // Non-codec resources are shared by audio, video and image codecs (no subtype). |
| 1000 | default: |
| 1001 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1002 | uid_t uid = 0; |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1003 | if (getBiggestClientPendingRemoval_l(pid, type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1004 | MediaResource::SubType::kUnspecifiedSubType, uid, &client)) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1005 | clients.push_back(client); |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1006 | } |
| 1007 | break; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | if (!clients.empty()) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1013 | reclaimUnconditionallyFrom(clients); |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1014 | } |
| 1015 | return Status::ok(); |
| 1016 | } |
| 1017 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1018 | bool ResourceManagerService::getPriority_l(int pid, int* priority) { |
| 1019 | int newPid = pid; |
| 1020 | |
| 1021 | if (mOverridePidMap.find(pid) != mOverridePidMap.end()) { |
| 1022 | newPid = mOverridePidMap[pid]; |
| 1023 | ALOGD("getPriority_l: use override pid %d instead original pid %d", |
| 1024 | newPid, pid); |
| 1025 | } |
| 1026 | |
| 1027 | return mProcessInfo->getPriority(newPid, priority); |
| 1028 | } |
| 1029 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1030 | bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1031 | MediaResource::SubType subType, |
| 1032 | PidUidVector* idVector, |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1033 | std::vector<std::shared_ptr<IResourceManagerClient>>* clients) { |
| 1034 | std::vector<std::shared_ptr<IResourceManagerClient>> temp; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1035 | PidUidVector tempIdList; |
| 1036 | |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1037 | for (auto& [pid, infos] : mMap) { |
| 1038 | for (const auto& [id, info] : infos) { |
| 1039 | if (hasResourceType(type, subType, info.resources)) { |
| 1040 | if (!isCallingPriorityHigher_l(callingPid, pid)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1041 | // some higher/equal priority process owns the resource, |
| 1042 | // this request can't be fulfilled. |
| 1043 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1044 | asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1047 | temp.push_back(info.client); |
| 1048 | tempIdList.emplace_back(pid, info.uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 1053 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1056 | |
| 1057 | clients->insert(std::end(*clients), std::begin(temp), std::end(temp)); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1058 | idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1059 | return true; |
| 1060 | } |
| 1061 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1062 | bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1063 | MediaResource::Type type, |
| 1064 | MediaResource::SubType subType, |
| 1065 | PidUidVector* idVector, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1066 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1067 | int lowestPriorityPid; |
| 1068 | int lowestPriority; |
| 1069 | int callingPriority; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1070 | uid_t uid = 0; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1071 | |
| 1072 | // Before looking into other processes, check if we have clients marked for |
| 1073 | // pending removal in the same process. |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1074 | if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) { |
| 1075 | idVector->emplace_back(callingPid, uid); |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1078 | if (!getPriority_l(callingPid, &callingPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1079 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 1080 | callingPid); |
| 1081 | return false; |
| 1082 | } |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1083 | if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
| 1086 | if (lowestPriority <= callingPriority) { |
| 1087 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 1088 | lowestPriority, callingPriority); |
| 1089 | return false; |
| 1090 | } |
| 1091 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1092 | if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1093 | return false; |
| 1094 | } |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1095 | |
| 1096 | idVector->emplace_back(lowestPriorityPid, uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1097 | return true; |
| 1098 | } |
| 1099 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1100 | bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type, |
| 1101 | MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1102 | int pid = -1; |
| 1103 | int priority = -1; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1104 | for (auto& [tempPid, infos] : mMap) { |
| 1105 | if (infos.size() == 0) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1106 | // no client on this process. |
| 1107 | continue; |
| 1108 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1109 | if (!hasResourceType(type, subType, infos)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1110 | // doesn't have the requested resource type |
| 1111 | continue; |
| 1112 | } |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1113 | int tempPriority = -1; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1114 | if (!getPriority_l(tempPid, &tempPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1115 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 1116 | // TODO: remove this pid from mMap? |
| 1117 | continue; |
| 1118 | } |
| 1119 | if (pid == -1 || tempPriority > priority) { |
| 1120 | // initial the value |
| 1121 | pid = tempPid; |
| 1122 | priority = tempPriority; |
| 1123 | } |
| 1124 | } |
| 1125 | if (pid != -1) { |
| 1126 | *lowestPriorityPid = pid; |
| 1127 | *lowestPriority = priority; |
| 1128 | } |
| 1129 | return (pid != -1); |
| 1130 | } |
| 1131 | |
| 1132 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 1133 | int callingPidPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1134 | if (!getPriority_l(callingPid, &callingPidPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | int priority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1139 | if (!getPriority_l(pid, &priority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | return (callingPidPriority < priority); |
| 1144 | } |
| 1145 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1146 | bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1147 | MediaResource::SubType subType, uid_t& uid, |
| 1148 | std::shared_ptr<IResourceManagerClient> *client) { |
| 1149 | return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */); |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1153 | MediaResource::SubType subType, uid_t& uid, |
| 1154 | std::shared_ptr<IResourceManagerClient> *client, |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1155 | bool pendingRemovalOnly) { |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1156 | PidResourceInfosMap::iterator found = mMap.find(pid); |
| 1157 | if (found == mMap.end()) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1158 | ALOGE_IF(!pendingRemovalOnly, |
| 1159 | "getBiggestClient_l: can't find resource info for pid %d", pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1163 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1164 | uint64_t largestValue = 0; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1165 | const ResourceInfos& infos = found->second; |
| 1166 | for (const auto& [id, info] : infos) { |
| 1167 | const ResourceList& resources = info.resources; |
| 1168 | if (pendingRemovalOnly && !info.pendingRemoval) { |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1169 | continue; |
| 1170 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 1171 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 1172 | const MediaResourceParcel &resource = it->second; |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1173 | if (hasResourceType(type, subType, resource)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 1174 | if (resource.value > largestValue) { |
| 1175 | largestValue = resource.value; |
Girish | 434b4d8 | 2023-07-11 23:24:54 +0000 | [diff] [blame] | 1176 | clientTemp = info.client; |
| 1177 | uid = info.uid; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | if (clientTemp == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1184 | ALOGE_IF(!pendingRemovalOnly, |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1185 | "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d", |
| 1186 | asString(type), asString(subType), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | *client = clientTemp; |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 1194 | Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) { |
| 1195 | mResourceManagerMetrics->notifyClientCreated(clientInfo); |
| 1196 | return Status::ok(); |
| 1197 | } |
| 1198 | |
| 1199 | Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) { |
| 1200 | mResourceManagerMetrics->notifyClientStarted(clientConfig); |
| 1201 | return Status::ok(); |
| 1202 | } |
| 1203 | |
| 1204 | Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) { |
| 1205 | mResourceManagerMetrics->notifyClientStopped(clientConfig); |
| 1206 | return Status::ok(); |
| 1207 | } |
| 1208 | |
Girish | de8eb59 | 2023-04-13 18:49:17 +0000 | [diff] [blame] | 1209 | Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) { |
| 1210 | mResourceManagerMetrics->notifyClientConfigChanged(clientConfig); |
| 1211 | return Status::ok(); |
| 1212 | } |
| 1213 | |
Girish | 1f002cf | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 1214 | long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const { |
| 1215 | return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid); |
| 1216 | } |
| 1217 | |
| 1218 | long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const { |
| 1219 | return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid); |
| 1220 | } |
| 1221 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1222 | } // namespace android |