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