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