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