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