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