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