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