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 = |
| 366 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 367 | if (status != STATUS_OK) { |
| 368 | return; |
| 369 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 370 | |
| 371 | std::shared_ptr<ResourceObserverService> observerService = |
| 372 | ResourceObserverService::instantiate(); |
| 373 | |
| 374 | if (observerService != nullptr) { |
| 375 | service->setObserverService(observerService); |
| 376 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 377 | // TODO: mediaserver main() is already starting the thread pool, |
| 378 | // move this to mediaserver main() when other services in mediaserver |
| 379 | // are converted to ndk-platform aidl. |
| 380 | //ABinderProcess_startThreadPool(); |
| 381 | } |
| 382 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 383 | ResourceManagerService::~ResourceManagerService() {} |
| 384 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 385 | void ResourceManagerService::setObserverService( |
| 386 | const std::shared_ptr<ResourceObserverService>& observerService) { |
| 387 | mObserverService = observerService; |
| 388 | } |
| 389 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 390 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 391 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 392 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 393 | |
| 394 | Mutex::Autolock lock(mLock); |
| 395 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 396 | const std::string &type = policies[i].type; |
| 397 | const std::string &value = policies[i].value; |
| 398 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 399 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 400 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 401 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 404 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 407 | void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, |
| 408 | const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 409 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 410 | if (resource.type == MediaResource::Type::kCpuBoost |
| 411 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 412 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 413 | // could have died, if we only do it the first time subsequent instances |
| 414 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 415 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 416 | ALOGW("couldn't request cpuset boost"); |
| 417 | } |
| 418 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 419 | } else if (resource.type == MediaResource::Type::kBattery |
| 420 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 421 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 425 | void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, |
| 426 | const ResourceInfo& clientInfo) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 427 | if (resource.type == MediaResource::Type::kCpuBoost |
| 428 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 429 | && mCpuBoostCount > 0) { |
| 430 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 431 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 432 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 433 | } else if (resource.type == MediaResource::Type::kBattery |
| 434 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 435 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 439 | void ResourceManagerService::mergeResources(MediaResourceParcel& r1, |
| 440 | const MediaResourceParcel& r2) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 441 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 442 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 443 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 444 | // value goes lower when the session is used more often. During reclaim |
| 445 | // the session with the highest value (lowest usage) would be closed. |
| 446 | if (r2.value < INT64_MAX - r1.value) { |
| 447 | r1.value += r2.value; |
| 448 | if (r1.value < 0) { |
| 449 | r1.value = 0; |
| 450 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 451 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 452 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 456 | Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 457 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 458 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 459 | int32_t pid = clientInfo.pid; |
| 460 | int32_t uid = clientInfo.uid; |
| 461 | int64_t clientId = clientInfo.id; |
| 462 | const std::string& name = clientInfo.name; |
| 463 | String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)", |
| 464 | pid, uid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 465 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 466 | |
| 467 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 468 | if (!mProcessInfo->isPidUidTrusted(pid, uid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 469 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 470 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 471 | ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d", |
| 472 | __FUNCTION__, pid, uid, callingPid, callingUid); |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 473 | pid = callingPid; |
| 474 | uid = callingUid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 475 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 476 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 477 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 478 | ResourceList resourceAdded; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 479 | |
| 480 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 481 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 482 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 483 | |
| 484 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 485 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 486 | continue; |
| 487 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 488 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 489 | if (res.value <= 0) { |
| 490 | // We can't init a new entry with negative value, although it's allowed |
| 491 | // to merge in negative values after the initial add. |
| 492 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 493 | continue; |
| 494 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 495 | onFirstAdded(res, info); |
| 496 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 497 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 498 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 499 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 500 | // Add it to the list of added resources for observers. |
| 501 | auto it = resourceAdded.find(resType); |
| 502 | if (it == resourceAdded.end()) { |
| 503 | resourceAdded[resType] = res; |
| 504 | } else { |
| 505 | mergeResources(it->second, res); |
| 506 | } |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 507 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 508 | if (info.cookie == 0 && client != nullptr) { |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 509 | info.cookie = addCookieAndLink_l(client, |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 510 | new DeathNotifier(ref<ResourceManagerService>(), pid, clientId)); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 511 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 512 | if (mObserverService != nullptr && !resourceAdded.empty()) { |
| 513 | mObserverService->onResourceAdded(uid, pid, resourceAdded); |
| 514 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 515 | notifyResourceGranted(pid, resources); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 516 | |
| 517 | // Increase the instance count of the resource associated with this client. |
| 518 | increaseResourceInstanceCount(clientId, name); |
| 519 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 520 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 523 | void ResourceManagerService::increaseResourceInstanceCount(int64_t clientId, |
| 524 | const std::string& name) { |
| 525 | // Check whether this client has been looked into already. |
| 526 | if (mClientIdSet.find(clientId) == mClientIdSet.end()) { |
| 527 | mClientIdSet.insert(clientId); |
| 528 | // Update the resource instance count. |
| 529 | auto found = mConcurrentResourceCountMap.find(name); |
| 530 | if (found == mConcurrentResourceCountMap.end()) { |
| 531 | mConcurrentResourceCountMap[name] = 1; |
| 532 | } else { |
| 533 | found->second++; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void ResourceManagerService::decreaseResourceInstanceCount(int64_t clientId, |
| 539 | const std::string& name) { |
| 540 | // Since this client has been removed, remove it from mClientIdSet |
| 541 | mClientIdSet.erase(clientId); |
| 542 | // Update the resource instance count also. |
| 543 | auto found = mConcurrentResourceCountMap.find(name); |
| 544 | if (found != mConcurrentResourceCountMap.end()) { |
| 545 | if (found->second == 1) { |
| 546 | mConcurrentResourceCountMap.erase(found); |
| 547 | } else { |
| 548 | found->second--; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 554 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 555 | int32_t pid = clientInfo.pid; |
| 556 | int32_t uid = clientInfo.uid; |
| 557 | int64_t clientId = clientInfo.id; |
| 558 | String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)", |
| 559 | pid, uid, (long long) clientId, getString(resources).string()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 560 | mServiceLog->add(log); |
| 561 | |
| 562 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 563 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 564 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 565 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 566 | pid, callingPid); |
| 567 | pid = callingPid; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 568 | } |
| 569 | ssize_t index = mMap.indexOfKey(pid); |
| 570 | if (index < 0) { |
| 571 | 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] | 572 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 573 | } |
| 574 | ResourceInfos &infos = mMap.editValueAt(index); |
| 575 | |
| 576 | index = infos.indexOfKey(clientId); |
| 577 | if (index < 0) { |
| 578 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 579 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | ResourceInfo &info = infos.editValueAt(index); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 583 | ResourceList resourceRemoved; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 584 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 585 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 586 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 587 | |
| 588 | if (res.value < 0) { |
| 589 | ALOGW("Ignoring request to remove negative value of resource"); |
| 590 | continue; |
| 591 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 592 | // ignore if we don't have it |
| 593 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 594 | MediaResourceParcel &resource = info.resources[resType]; |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 595 | MediaResourceParcel actualRemoved = res; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 596 | if (resource.value > res.value) { |
| 597 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 598 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 599 | onLastRemoved(res, info); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 600 | actualRemoved.value = resource.value; |
Chong Zhang | 102b3e3 | 2020-11-02 12:21:50 -0800 | [diff] [blame] | 601 | info.resources.erase(resType); |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // Add it to the list of removed resources for observers. |
| 605 | auto it = resourceRemoved.find(resType); |
| 606 | if (it == resourceRemoved.end()) { |
| 607 | resourceRemoved[resType] = actualRemoved; |
| 608 | } else { |
| 609 | mergeResources(it->second, actualRemoved); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 613 | if (mObserverService != nullptr && !resourceRemoved.empty()) { |
| 614 | mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved); |
| 615 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 616 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 619 | Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) { |
| 620 | removeResource(clientInfo, true /*checkValid*/); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 621 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 622 | } |
| 623 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 624 | Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) { |
| 625 | int32_t pid = clientInfo.pid; |
| 626 | int32_t uid = clientInfo.uid; |
| 627 | int64_t clientId = clientInfo.id; |
| 628 | String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)", |
| 629 | pid, uid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 630 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 631 | |
| 632 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 633 | if (checkValid && !mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 634 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 635 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 636 | pid, callingPid); |
| 637 | pid = callingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 638 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 639 | ssize_t index = mMap.indexOfKey(pid); |
| 640 | if (index < 0) { |
| 641 | 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] | 642 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 643 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 644 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 645 | |
| 646 | index = infos.indexOfKey(clientId); |
| 647 | if (index < 0) { |
| 648 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 649 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 650 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 651 | |
| 652 | const ResourceInfo &info = infos[index]; |
| 653 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 654 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 655 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 656 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 657 | // Since this client has been removed, decrease the corresponding |
| 658 | // resources instance count. |
| 659 | decreaseResourceInstanceCount(clientId, info.name); |
| 660 | |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 661 | removeCookieAndUnlink_l(info.client, info.cookie); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 662 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 663 | if (mObserverService != nullptr && !info.resources.empty()) { |
| 664 | mObserverService->onResourceRemoved(info.uid, pid, info.resources); |
| 665 | } |
| 666 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 667 | infos.removeItemsAt(index); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 668 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 671 | void ResourceManagerService::getClientForResource_l(int callingPid, |
| 672 | const MediaResourceParcel *res, |
| 673 | PidUidVector* idVector, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 674 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 675 | if (res == NULL) { |
| 676 | return; |
| 677 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 678 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 679 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 680 | clients->push_back(client); |
| 681 | } |
| 682 | } |
| 683 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 684 | Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo, |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 685 | const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 686 | int32_t callingPid = clientInfo.pid; |
| 687 | std::string clientName = clientInfo.name; |
| 688 | String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)", |
| 689 | callingPid, clientInfo.uid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 690 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 691 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 692 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 693 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 694 | PidUidVector idVector; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 695 | { |
| 696 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 697 | if (!mProcessInfo->isPidTrusted(callingPid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 698 | pid_t actualCallingPid = IPCThreadState::self()->getCallingPid(); |
| 699 | ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__, |
| 700 | callingPid, actualCallingPid); |
| 701 | callingPid = actualCallingPid; |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 702 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 703 | const MediaResourceParcel *secureCodec = NULL; |
| 704 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 705 | const MediaResourceParcel *graphicMemory = NULL; |
| 706 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 707 | for (size_t i = 0; i < resources.size(); ++i) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 708 | switch (resources[i].type) { |
| 709 | case MediaResource::Type::kSecureCodec: |
| 710 | secureCodec = &resources[i]; |
| 711 | break; |
| 712 | case MediaResource::Type::kNonSecureCodec: |
| 713 | nonSecureCodec = &resources[i]; |
| 714 | break; |
| 715 | case MediaResource::Type::kGraphicMemory: |
| 716 | graphicMemory = &resources[i]; |
| 717 | break; |
| 718 | case MediaResource::Type::kDrmSession: |
| 719 | drmSession = &resources[i]; |
| 720 | break; |
| 721 | default: |
| 722 | break; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | // first pass to handle secure/non-secure codec conflict |
| 727 | if (secureCodec != NULL) { |
| 728 | if (!mSupportsMultipleSecureCodecs) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 729 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 730 | secureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 731 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | if (!mSupportsSecureWithNonSecureCodec) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 735 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 736 | secureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 737 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
| 741 | if (nonSecureCodec != NULL) { |
| 742 | if (!mSupportsSecureWithNonSecureCodec) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 743 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 744 | nonSecureCodec->subType, &idVector, &clients)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 745 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 749 | if (drmSession != NULL) { |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 750 | getClientForResource_l(callingPid, drmSession, &idVector, &clients); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 751 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 752 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 755 | |
| 756 | if (clients.size() == 0) { |
| 757 | // 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] | 758 | getClientForResource_l(callingPid, graphicMemory, &idVector, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 759 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 760 | |
| 761 | if (clients.size() == 0) { |
| 762 | // 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] | 763 | getClientForResource_l(callingPid, secureCodec, &idVector, &clients); |
| 764 | getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if (clients.size() == 0) { |
| 768 | // if we are here, run the fourth pass to free one codec with the different type. |
| 769 | if (secureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 770 | MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 771 | getClientForResource_l(callingPid, &temp, &idVector, &clients); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 772 | } |
| 773 | if (nonSecureCodec != NULL) { |
Wonsik Kim | b363901 | 2022-03-16 13:57:41 -0700 | [diff] [blame] | 774 | MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 775 | getClientForResource_l(callingPid, &temp, &idVector, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 780 | *_aidl_return = reclaimUnconditionallyFrom(clients); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 781 | |
| 782 | // Log Reclaim Pushed Atom to statsd |
| 783 | pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return); |
| 784 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 785 | return Status::ok(); |
| 786 | } |
| 787 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 788 | void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo, |
| 789 | const Vector<std::shared_ptr<IResourceManagerClient>>& clients, |
| 790 | const PidUidVector& idVector, bool reclaimed) { |
| 791 | // Construct the metrics for codec reclaim as a pushed atom. |
| 792 | // 1. Information about the requester. |
| 793 | // - UID and the priority (oom score) |
| 794 | int32_t callingPid = clientInfo.pid; |
| 795 | int32_t requesterUid = clientInfo.uid; |
| 796 | std::string clientName = clientInfo.name; |
| 797 | int requesterPriority = -1; |
| 798 | getPriority_l(callingPid, &requesterPriority); |
| 799 | |
| 800 | // 2. Information about the codec. |
| 801 | // - Name of the codec requested |
| 802 | // - Number of concurrent codecs running. |
| 803 | int32_t noOfConcurrentCodecs = 0; |
| 804 | auto found = mConcurrentResourceCountMap.find(clientName); |
| 805 | if (found != mConcurrentResourceCountMap.end()) { |
| 806 | noOfConcurrentCodecs = found->second; |
| 807 | } |
| 808 | |
| 809 | // 3. Information about the Reclaim: |
| 810 | // - Status of reclaim request |
| 811 | // - How many codecs are reclaimed |
| 812 | // - For each codecs reclaimed, information of the process that it belonged to: |
| 813 | // - UID and the Priority (oom score) |
| 814 | int32_t reclaimStatus = MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_SUCCESS; |
| 815 | if (!reclaimed) { |
| 816 | if (clients.size() == 0) { |
| 817 | // No clients to reclaim from |
| 818 | reclaimStatus = |
| 819 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_NO_CLIENTS; |
| 820 | } else { |
| 821 | // Couldn't reclaim resources from the clients |
| 822 | reclaimStatus = |
| 823 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED__RECLAIM_STATUS__RECLAIM_FAILED_RECLAIM_RESOURCES; |
| 824 | } |
| 825 | } |
| 826 | int32_t noOfCodecsReclaimed = clients.size(); |
| 827 | int32_t targetIndex = 1; |
| 828 | for (const auto& id : idVector) { |
| 829 | int32_t targetUid = id.second; |
| 830 | int targetPriority = -1; |
| 831 | getPriority_l(id.first, &targetPriority); |
| 832 | // Post the pushed atom |
| 833 | int result = stats_write( |
| 834 | MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED, |
| 835 | requesterUid, |
| 836 | requesterPriority, |
| 837 | clientName.c_str(), |
| 838 | noOfConcurrentCodecs, |
| 839 | reclaimStatus, |
| 840 | noOfCodecsReclaimed, |
| 841 | targetIndex, |
| 842 | targetUid, |
| 843 | targetPriority); |
| 844 | ALOGI("%s: Pushed MEDIA_CODEC_RECLAIM_REQUEST_COMPLETED atom: " |
| 845 | "Requester[pid(%d): uid(%d): priority(%d)] " |
| 846 | "Codec: [%s] " |
| 847 | "No of concurrent codecs: %d " |
| 848 | "Reclaim Status: %d " |
| 849 | "No of codecs reclaimed: %d " |
| 850 | "Target[%d][pid(%d): uid(%d): priority(%d)] " |
| 851 | "Atom Size: %d", |
| 852 | __func__, callingPid, requesterUid, requesterPriority, |
| 853 | clientName.c_str(), noOfConcurrentCodecs, |
| 854 | reclaimStatus, noOfCodecsReclaimed, |
| 855 | targetIndex, id.first, targetUid, targetPriority, result); |
| 856 | targetIndex++; |
| 857 | } |
| 858 | } |
| 859 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 860 | bool ResourceManagerService::reclaimUnconditionallyFrom( |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 861 | const Vector<std::shared_ptr<IResourceManagerClient>> &clients) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 862 | if (clients.size() == 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 863 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 866 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 867 | for (size_t i = 0; i < clients.size(); ++i) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 868 | String8 log = String8::format("reclaimResource from client %p", clients[i].get()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 869 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 870 | bool success; |
| 871 | Status status = clients[i]->reclaimResource(&success); |
| 872 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 873 | failedClient = clients[i]; |
| 874 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 877 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 878 | if (failedClient == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 879 | return true; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Brian Lindahl | 9f626cf | 2022-04-25 13:29:59 +0200 | [diff] [blame] | 882 | int failedClientPid = -1; |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 883 | { |
| 884 | Mutex::Autolock lock(mLock); |
| 885 | bool found = false; |
| 886 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 887 | ResourceInfos &infos = mMap.editValueAt(i); |
| 888 | for (size_t j = 0; j < infos.size();) { |
| 889 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 890 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 891 | found = true; |
| 892 | } else { |
| 893 | ++j; |
| 894 | } |
| 895 | } |
| 896 | if (found) { |
Brian Lindahl | 9f626cf | 2022-04-25 13:29:59 +0200 | [diff] [blame] | 897 | failedClientPid = mMap.keyAt(i); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 898 | break; |
| 899 | } |
| 900 | } |
Brian Lindahl | 9f626cf | 2022-04-25 13:29:59 +0200 | [diff] [blame] | 901 | if (found) { |
| 902 | ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid); |
| 903 | } else { |
| 904 | ALOGW("Failed to reclaim resources from unlocateable client"); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 908 | return false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 911 | Status ResourceManagerService::overridePid(int originalPid, int newPid) { |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 912 | String8 log = String8::format("overridePid(originalPid %d, newPid %d)", |
| 913 | originalPid, newPid); |
| 914 | mServiceLog->add(log); |
| 915 | |
| 916 | // allow if this is called from the same process or the process has |
| 917 | // permission. |
| 918 | if ((AIBinder_getCallingPid() != getpid()) && |
| 919 | (checkCallingPermission(String16( |
| 920 | "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) { |
| 921 | ALOGE( |
| 922 | "Permission Denial: can't access overridePid method from pid=%d, " |
| 923 | "self pid=%d\n", |
| 924 | AIBinder_getCallingPid(), getpid()); |
| 925 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 926 | } |
| 927 | |
| 928 | { |
| 929 | Mutex::Autolock lock(mLock); |
| 930 | mOverridePidMap.erase(originalPid); |
| 931 | if (newPid != -1) { |
| 932 | mOverridePidMap.emplace(originalPid, newPid); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | return Status::ok(); |
| 937 | } |
| 938 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 939 | Status ResourceManagerService::overrideProcessInfo( |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 940 | const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState, |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 941 | int oomScore) { |
| 942 | String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)", |
| 943 | pid, procState, oomScore); |
| 944 | mServiceLog->add(log); |
| 945 | |
| 946 | // Only allow the override if the caller already can access process state and oom scores. |
| 947 | int callingPid = AIBinder_getCallingPid(); |
| 948 | if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16( |
| 949 | "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) { |
| 950 | ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid); |
| 951 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 952 | } |
| 953 | |
| 954 | if (client == nullptr) { |
| 955 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 956 | } |
| 957 | |
| 958 | Mutex::Autolock lock(mLock); |
| 959 | removeProcessInfoOverride_l(pid); |
| 960 | |
| 961 | if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) { |
| 962 | // Override value is rejected by ProcessInfo. |
| 963 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 964 | } |
| 965 | |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 966 | uintptr_t cookie = addCookieAndLink_l(client, |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 967 | new OverrideProcessInfoDeathNotifier(ref<ResourceManagerService>(), pid)); |
| 968 | |
| 969 | mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{cookie, client}); |
| 970 | |
| 971 | return Status::ok(); |
| 972 | } |
| 973 | |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 974 | uintptr_t ResourceManagerService::addCookieAndLink_l( |
| 975 | const std::shared_ptr<IResourceManagerClient>& client, const sp<DeathNotifier>& notifier) { |
| 976 | if (client == nullptr) { |
| 977 | return 0; |
| 978 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 979 | std::scoped_lock lock{sCookieLock}; |
| 980 | |
| 981 | uintptr_t cookie; |
| 982 | // Need to skip cookie 0 (if it wraps around). ResourceInfo has cookie initialized to 0 |
| 983 | // indicating the death notifier is not created yet. |
| 984 | while ((cookie = ++sCookieCounter) == 0); |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 985 | AIBinder_linkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 986 | sCookieToDeathNotifierMap.emplace(cookie, notifier); |
| 987 | |
| 988 | return cookie; |
| 989 | } |
| 990 | |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 991 | void ResourceManagerService::removeCookieAndUnlink_l( |
| 992 | const std::shared_ptr<IResourceManagerClient>& client, uintptr_t cookie) { |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 993 | std::scoped_lock lock{sCookieLock}; |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 994 | if (client != nullptr) { |
| 995 | AIBinder_unlinkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie); |
| 996 | } |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 997 | sCookieToDeathNotifierMap.erase(cookie); |
| 998 | } |
| 999 | |
| 1000 | void ResourceManagerService::removeProcessInfoOverride(int pid) { |
| 1001 | Mutex::Autolock lock(mLock); |
| 1002 | |
| 1003 | removeProcessInfoOverride_l(pid); |
| 1004 | } |
| 1005 | |
| 1006 | void ResourceManagerService::removeProcessInfoOverride_l(int pid) { |
| 1007 | auto it = mProcessInfoOverrideMap.find(pid); |
| 1008 | if (it == mProcessInfoOverrideMap.end()) { |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | mProcessInfo->removeProcessInfoOverride(pid); |
| 1013 | |
Arun Johnson | d1f5973 | 2022-03-25 17:10:29 +0000 | [diff] [blame] | 1014 | removeCookieAndUnlink_l(it->second.client, it->second.cookie); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 1015 | |
| 1016 | mProcessInfoOverrideMap.erase(pid); |
| 1017 | } |
| 1018 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1019 | Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) { |
| 1020 | int32_t pid = clientInfo.pid; |
| 1021 | int64_t clientId = clientInfo.id; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1022 | String8 log = String8::format( |
| 1023 | "markClientForPendingRemoval(pid %d, clientId %lld)", |
| 1024 | pid, (long long) clientId); |
| 1025 | mServiceLog->add(log); |
| 1026 | |
| 1027 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 1028 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 1029 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1030 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 1031 | pid, callingPid); |
| 1032 | pid = callingPid; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1033 | } |
| 1034 | ssize_t index = mMap.indexOfKey(pid); |
| 1035 | if (index < 0) { |
| 1036 | ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld", |
| 1037 | pid, (long long)clientId); |
| 1038 | return Status::ok(); |
| 1039 | } |
| 1040 | ResourceInfos &infos = mMap.editValueAt(index); |
| 1041 | |
| 1042 | index = infos.indexOfKey(clientId); |
| 1043 | if (index < 0) { |
| 1044 | ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId); |
| 1045 | return Status::ok(); |
| 1046 | } |
| 1047 | |
| 1048 | ResourceInfo &info = infos.editValueAt(index); |
| 1049 | info.pendingRemoval = true; |
| 1050 | return Status::ok(); |
| 1051 | } |
| 1052 | |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1053 | Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) { |
| 1054 | String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid); |
| 1055 | mServiceLog->add(log); |
| 1056 | |
| 1057 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
| 1058 | { |
| 1059 | Mutex::Autolock lock(mLock); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 1060 | if (!mProcessInfo->isPidTrusted(pid)) { |
Chong Zhang | c7303e8 | 2020-12-02 16:38:52 -0800 | [diff] [blame] | 1061 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1062 | ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__, |
| 1063 | pid, callingPid); |
| 1064 | pid = callingPid; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | for (MediaResource::Type type : {MediaResource::Type::kSecureCodec, |
| 1068 | MediaResource::Type::kNonSecureCodec, |
| 1069 | MediaResource::Type::kGraphicMemory, |
| 1070 | MediaResource::Type::kDrmSession}) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1071 | switch (type) { |
| 1072 | // Codec resources are segregated by audio, video and image domains. |
| 1073 | case MediaResource::Type::kSecureCodec: |
| 1074 | case MediaResource::Type::kNonSecureCodec: |
| 1075 | for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec, |
| 1076 | MediaResource::SubType::kVideoCodec, |
| 1077 | MediaResource::SubType::kImageCodec}) { |
| 1078 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1079 | uid_t uid = 0; |
| 1080 | if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1081 | clients.add(client); |
| 1082 | continue; |
| 1083 | } |
| 1084 | } |
| 1085 | break; |
| 1086 | // Non-codec resources are shared by audio, video and image codecs (no subtype). |
| 1087 | default: |
| 1088 | std::shared_ptr<IResourceManagerClient> client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1089 | uid_t uid = 0; |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1090 | if (getBiggestClientPendingRemoval_l(pid, type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1091 | MediaResource::SubType::kUnspecifiedSubType, uid, &client)) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1092 | clients.add(client); |
| 1093 | } |
| 1094 | break; |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | if (!clients.empty()) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1100 | reclaimUnconditionallyFrom(clients); |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1101 | } |
| 1102 | return Status::ok(); |
| 1103 | } |
| 1104 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1105 | bool ResourceManagerService::getPriority_l(int pid, int* priority) { |
| 1106 | int newPid = pid; |
| 1107 | |
| 1108 | if (mOverridePidMap.find(pid) != mOverridePidMap.end()) { |
| 1109 | newPid = mOverridePidMap[pid]; |
| 1110 | ALOGD("getPriority_l: use override pid %d instead original pid %d", |
| 1111 | newPid, pid); |
| 1112 | } |
| 1113 | |
| 1114 | return mProcessInfo->getPriority(newPid, priority); |
| 1115 | } |
| 1116 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1117 | bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1118 | MediaResource::SubType subType, |
| 1119 | PidUidVector* idVector, |
| 1120 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1121 | Vector<std::shared_ptr<IResourceManagerClient>> temp; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1122 | PidUidVector tempIdList; |
| 1123 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1124 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 1125 | ResourceInfos &infos = mMap.editValueAt(i); |
| 1126 | for (size_t j = 0; j < infos.size(); ++j) { |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1127 | if (hasResourceType(type, subType, infos[j].resources)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1128 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 1129 | // some higher/equal priority process owns the resource, |
| 1130 | // this request can't be fulfilled. |
| 1131 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 1132 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1133 | return false; |
| 1134 | } |
| 1135 | temp.push_back(infos[j].client); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1136 | tempIdList.emplace_back(mMap.keyAt(i), infos[j].uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 1141 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1142 | return true; |
| 1143 | } |
| 1144 | clients->appendVector(temp); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1145 | idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1146 | return true; |
| 1147 | } |
| 1148 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1149 | bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1150 | MediaResource::Type type, |
| 1151 | MediaResource::SubType subType, |
| 1152 | PidUidVector* idVector, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1153 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1154 | int lowestPriorityPid; |
| 1155 | int lowestPriority; |
| 1156 | int callingPriority; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1157 | uid_t uid = 0; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1158 | |
| 1159 | // Before looking into other processes, check if we have clients marked for |
| 1160 | // pending removal in the same process. |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1161 | if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) { |
| 1162 | idVector->emplace_back(callingPid, uid); |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1165 | if (!getPriority_l(callingPid, &callingPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1166 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 1167 | callingPid); |
| 1168 | return false; |
| 1169 | } |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1170 | if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | if (lowestPriority <= callingPriority) { |
| 1174 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 1175 | lowestPriority, callingPriority); |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1179 | if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1182 | |
| 1183 | idVector->emplace_back(lowestPriorityPid, uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1184 | return true; |
| 1185 | } |
| 1186 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1187 | bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type, |
| 1188 | MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1189 | int pid = -1; |
| 1190 | int priority = -1; |
| 1191 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 1192 | if (mMap.valueAt(i).size() == 0) { |
| 1193 | // no client on this process. |
| 1194 | continue; |
| 1195 | } |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1196 | if (!hasResourceType(type, subType, mMap.valueAt(i))) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1197 | // doesn't have the requested resource type |
| 1198 | continue; |
| 1199 | } |
| 1200 | int tempPid = mMap.keyAt(i); |
| 1201 | int tempPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1202 | if (!getPriority_l(tempPid, &tempPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1203 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 1204 | // TODO: remove this pid from mMap? |
| 1205 | continue; |
| 1206 | } |
| 1207 | if (pid == -1 || tempPriority > priority) { |
| 1208 | // initial the value |
| 1209 | pid = tempPid; |
| 1210 | priority = tempPriority; |
| 1211 | } |
| 1212 | } |
| 1213 | if (pid != -1) { |
| 1214 | *lowestPriorityPid = pid; |
| 1215 | *lowestPriority = priority; |
| 1216 | } |
| 1217 | return (pid != -1); |
| 1218 | } |
| 1219 | |
| 1220 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 1221 | int callingPidPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1222 | if (!getPriority_l(callingPid, &callingPidPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1223 | return false; |
| 1224 | } |
| 1225 | |
| 1226 | int priority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 1227 | if (!getPriority_l(pid, &priority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | return (callingPidPriority < priority); |
| 1232 | } |
| 1233 | |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1234 | bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1235 | MediaResource::SubType subType, uid_t& uid, |
| 1236 | std::shared_ptr<IResourceManagerClient> *client) { |
| 1237 | return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */); |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type, |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1241 | MediaResource::SubType subType, uid_t& uid, |
| 1242 | std::shared_ptr<IResourceManagerClient> *client, |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1243 | bool pendingRemovalOnly) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1244 | ssize_t index = mMap.indexOfKey(pid); |
| 1245 | if (index < 0) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1246 | ALOGE_IF(!pendingRemovalOnly, |
| 1247 | "getBiggestClient_l: can't find resource info for pid %d", pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
| 1250 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 1251 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1252 | uint64_t largestValue = 0; |
| 1253 | const ResourceInfos &infos = mMap.valueAt(index); |
| 1254 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 1255 | const ResourceList &resources = infos[i].resources; |
Wonsik Kim | d20e936 | 2020-04-28 10:42:57 -0700 | [diff] [blame] | 1256 | if (pendingRemovalOnly && !infos[i].pendingRemoval) { |
| 1257 | continue; |
| 1258 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 1259 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 1260 | const MediaResourceParcel &resource = it->second; |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1261 | if (hasResourceType(type, subType, resource)) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 1262 | if (resource.value > largestValue) { |
| 1263 | largestValue = resource.value; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1264 | clientTemp = infos[i].client; |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 1265 | uid = infos[i].uid; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | if (clientTemp == NULL) { |
Wonsik Kim | 271429d | 2020-10-01 10:12:56 -0700 | [diff] [blame] | 1272 | ALOGE_IF(!pendingRemovalOnly, |
Brian Lindahl | 64ee945 | 2022-01-14 13:31:16 +0100 | [diff] [blame] | 1273 | "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d", |
| 1274 | asString(type), asString(subType), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | *client = clientTemp; |
| 1279 | return true; |
| 1280 | } |
| 1281 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1282 | } // namespace android |