blob: f12a5d688b28d6ee9f6c9ebcc3ea3d9b4353ab85 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
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 Zhangfdd512a2019-11-22 11:03:14 -080022#include <android/binder_manager.h>
23#include <android/binder_process.h>
Chong Zhangc7303e82020-12-02 16:38:52 -080024#include <binder/IPCThreadState.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070026#include <cutils/sched_policy.h>
Chong Zhang181e6952019-10-09 13:23:39 -070027#include <media/MediaResourcePolicy.h>
Girish1f002cf2023-02-17 00:36:29 +000028#include <media/stagefright/foundation/ABase.h>
Chong Zhangee33d642019-08-08 14:26:43 -070029#include <mediautils/BatteryNotifier.h>
Girish1f002cf2023-02-17 00:36:29 +000030#include <mediautils/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070031#include <mediautils/SchedulingPolicyService.h>
Girish1484e5d2023-11-20 06:00:44 +000032#include <com_android_media_codec_flags.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070033
Girish1f002cf2023-02-17 00:36:29 +000034#include "ResourceManagerMetrics.h"
Girish1484e5d2023-11-20 06:00:44 +000035#include "ResourceManagerServiceNew.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070036#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070037#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070038
Girish1484e5d2023-11-20 06:00:44 +000039namespace CodecFeatureFlags = com::android::media::codec::flags;
40
Ronghua Wu231c3d12015-03-11 15:10:32 -070041namespace android {
42
Girish6a6044d2023-11-22 21:23:14 +000043void 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");
Girishd11a03a2023-11-30 21:17:51 +000077 resourceLog.append(resources.toString());
Dongwon Kangfe508d32015-12-15 14:22:05 +090078 }
79 }
Girish6a6044d2023-11-22 21:23:14 +000080
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 Kangfe508d32015-12-15 14:22:05 +090087}
88
Brian Lindahl64ee9452022-01-14 13:31:16 +010089binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070090 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -070091
dcashman014e91e2015-09-11 09:33:01 -070092 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
93 result.format("Permission Denial: "
94 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -080095 AIBinder_getCallingPid(),
96 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +000097 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -070098 return PERMISSION_DENIED;
99 }
100
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700101 bool supportsMultipleSecureCodecs;
102 bool supportsSecureWithNonSecureCodec;
103 String8 serviceLog;
104 {
Girish434b4d82023-07-11 23:24:54 +0000105 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700106 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
107 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
108 serviceLog = mServiceLog->toString(" " /* linePrefix */);
109 }
110
Girishdc187552024-04-02 21:51:46 +0000111 // Get all the resource (and overload pid) log.
Girish6a6044d2023-11-22 21:23:14 +0000112 std::string resourceLog;
113 getResourceDump(resourceLog);
114
Girishdc187552024-04-02 21:51:46 +0000115 // Get all the metrics log.
116 std::string metricsLog;
117 {
118 std::scoped_lock lock{mLock};
119 metricsLog = mResourceManagerMetrics->dump();
120 }
121
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700122 const size_t SIZE = 256;
123 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700124 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
125 result.append(buffer);
126 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700127 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700128 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700129 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
130 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700131 result.append(buffer);
132
Girishdc187552024-04-02 21:51:46 +0000133 // Add resource log.
Girish6a6044d2023-11-22 21:23:14 +0000134 result.append(resourceLog.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700135
Girishdc187552024-04-02 21:51:46 +0000136 // Add service log.
Ronghua Wu022ed722015-05-11 15:15:09 -0700137 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700138 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700139
Girishdc187552024-04-02 21:51:46 +0000140 // Add metrics log.
141 result.append(metricsLog.c_str());
142
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000143 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700144 return OK;
145}
146
Brian Lindahl64ee9452022-01-14 13:31:16 +0100147struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800148 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700149
Chong Zhangdd726802019-08-21 17:24:13 -0700150 virtual void noteStartVideo(int uid) override {
151 BatteryNotifier::getInstance().noteStartVideo(uid);
152 }
153 virtual void noteStopVideo(int uid) override {
154 BatteryNotifier::getInstance().noteStopVideo(uid);
155 }
156 virtual void noteResetVideo() override {
157 BatteryNotifier::getInstance().noteResetVideo();
158 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800159 virtual bool requestCpusetBoost(bool enable) override {
160 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700161 }
162
163protected:
164 virtual ~SystemCallbackImpl() {}
165
166private:
167 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800168 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700169};
170
171ResourceManagerService::ResourceManagerService()
172 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
173
Brian Lindahl64ee9452022-01-14 13:31:16 +0100174ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700175 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700176 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700177 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700178 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700179 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700180 mSupportsSecureWithNonSecureCodec(true),
Girishab17b0f2023-11-20 06:00:44 +0000181 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700182 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000183 // Create ResourceManagerMetrics that handles all the metrics.
184 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700185}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700186
Chong Zhangfdd512a2019-11-22 11:03:14 -0800187//static
188void ResourceManagerService::instantiate() {
Girishab17b0f2023-11-20 06:00:44 +0000189 std::shared_ptr<ResourceManagerService> service = Create();
Chong Zhangfdd512a2019-11-22 11:03:14 -0800190 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000191 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000192 service->asBinder().get(), getServiceName(),
193 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800194 if (status != STATUS_OK) {
195 return;
196 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700197
198 std::shared_ptr<ResourceObserverService> observerService =
199 ResourceObserverService::instantiate();
200
201 if (observerService != nullptr) {
202 service->setObserverService(observerService);
203 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800204 // TODO: mediaserver main() is already starting the thread pool,
205 // move this to mediaserver main() when other services in mediaserver
206 // are converted to ndk-platform aidl.
207 //ABinderProcess_startThreadPool();
208}
209
Girishab17b0f2023-11-20 06:00:44 +0000210std::shared_ptr<ResourceManagerService> ResourceManagerService::Create() {
211 return Create(new ProcessInfo(), new SystemCallbackImpl());
212}
213
214std::shared_ptr<ResourceManagerService> ResourceManagerService::Create(
215 const sp<ProcessInfoInterface>& processInfo,
216 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000217 std::shared_ptr<ResourceManagerService> service = nullptr;
Girish1484e5d2023-11-20 06:00:44 +0000218 // If codec importance feature is on, create the refactored implementation.
219 if (CodecFeatureFlags::codec_importance()) {
Girish6a6044d2023-11-22 21:23:14 +0000220 service = ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo,
221 systemResource);
222 } else {
223 service = ::ndk::SharedRefBase::make<ResourceManagerService>(processInfo,
224 systemResource);
Girish1484e5d2023-11-20 06:00:44 +0000225 }
Girish6a6044d2023-11-22 21:23:14 +0000226
227 if (service != nullptr) {
228 service->init();
229 }
230
231 return service;
Girishab17b0f2023-11-20 06:00:44 +0000232}
233
Girish1484e5d2023-11-20 06:00:44 +0000234// TEST only function.
235std::shared_ptr<ResourceManagerService> ResourceManagerService::CreateNew(
236 const sp<ProcessInfoInterface>& processInfo,
237 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000238 std::shared_ptr<ResourceManagerService> service =
239 ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo, systemResource);
240 service->init();
241 return service;
Girish1484e5d2023-11-20 06:00:44 +0000242}
243
Girish6a6044d2023-11-22 21:23:14 +0000244void ResourceManagerService::init() {}
245
Ronghua Wu231c3d12015-03-11 15:10:32 -0700246ResourceManagerService::~ResourceManagerService() {}
247
Chong Zhanga9d45c72020-09-09 12:41:17 -0700248void ResourceManagerService::setObserverService(
249 const std::shared_ptr<ResourceObserverService>& observerService) {
250 mObserverService = observerService;
251}
252
Chong Zhang181e6952019-10-09 13:23:39 -0700253Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000254 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700255 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700256
Girish434b4d82023-07-11 23:24:54 +0000257 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700258 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700259 const std::string &type = policies[i].type;
260 const std::string &value = policies[i].value;
261 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700262 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700263 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700264 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700265 }
266 }
Chong Zhang181e6952019-10-09 13:23:39 -0700267 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700268}
269
Girishab17b0f2023-11-20 06:00:44 +0000270void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700271 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700272 if (resource.type == MediaResource::Type::kCpuBoost
273 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700274 // Request it on every new instance of kCpuBoost, as the media.codec
275 // could have died, if we only do it the first time subsequent instances
276 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800277 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700278 ALOGW("couldn't request cpuset boost");
279 }
280 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700281 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000282 && (resource.subType == MediaResource::SubType::kHwVideoCodec
283 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000284 mSystemCB->noteStartVideo(uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700285 }
286}
287
Girishab17b0f2023-11-20 06:00:44 +0000288void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhang181e6952019-10-09 13:23:39 -0700289 if (resource.type == MediaResource::Type::kCpuBoost
290 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700291 && mCpuBoostCount > 0) {
292 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800293 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700294 }
Chong Zhang181e6952019-10-09 13:23:39 -0700295 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000296 && (resource.subType == MediaResource::SubType::kHwVideoCodec
297 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000298 mSystemCB->noteStopVideo(uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700299 }
300}
301
Girish9128e242022-11-23 20:52:29 +0000302Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800303 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700304 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000305 int32_t pid = clientInfo.pid;
306 int32_t uid = clientInfo.uid;
307 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000308 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000309 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700310 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700311
Girish434b4d82023-07-11 23:24:54 +0000312 std::scoped_lock lock{mLock};
guochuang6df21c52024-08-30 11:24:43 +0800313 ClientInfoParcel updatedClientInfo = clientInfo;
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100314 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800315 pid_t callingPid = IPCThreadState::self()->getCallingPid();
316 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100317 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
318 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800319 pid = callingPid;
320 uid = callingUid;
guochuang6df21c52024-08-30 11:24:43 +0800321 updatedClientInfo.pid = callingPid;
322 updatedClientInfo.uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800323 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700324 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000325 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700326 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700327
328 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700329 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700330
331 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
332 ALOGW("Ignoring request to remove negative value of non-drm resource");
333 continue;
334 }
Girishd11a03a2023-11-30 21:17:51 +0000335 bool isNewEntry = false;
336 if (!info.resources.add(res, &isNewEntry)) {
337 continue;
338 }
339 if (isNewEntry) {
Girishab17b0f2023-11-20 06:00:44 +0000340 onFirstAdded(res, info.uid);
Chong Zhang79d2b282018-04-17 14:14:31 -0700341 }
Girishd11a03a2023-11-30 21:17:51 +0000342
Chong Zhanga9d45c72020-09-09 12:41:17 -0700343 // Add it to the list of added resources for observers.
Girishd11a03a2023-11-30 21:17:51 +0000344 resourceAdded.add(res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700345 }
Girishf1d166c2023-07-20 22:35:29 +0000346 if (info.deathNotifier == nullptr && client != nullptr) {
Girishab17b0f2023-11-20 06:00:44 +0000347 info.deathNotifier = DeathNotifier::Create(
guochuang6df21c52024-08-30 11:24:43 +0800348 client, ref<ResourceManagerService>(), updatedClientInfo);
Wonsik Kim3e378962017-01-05 17:00:02 +0900349 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700350 if (mObserverService != nullptr && !resourceAdded.empty()) {
351 mObserverService->onResourceAdded(uid, pid, resourceAdded);
352 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900353 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000354
Chong Zhang181e6952019-10-09 13:23:39 -0700355 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700356}
357
Girish9128e242022-11-23 20:52:29 +0000358Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700359 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000360 int32_t pid = clientInfo.pid;
361 int32_t uid = clientInfo.uid;
362 int64_t clientId = clientInfo.id;
363 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000364 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700365 mServiceLog->add(log);
366
Girish434b4d82023-07-11 23:24:54 +0000367 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100368 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800369 pid_t callingPid = IPCThreadState::self()->getCallingPid();
370 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
371 pid, callingPid);
372 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700373 }
Girish434b4d82023-07-11 23:24:54 +0000374 PidResourceInfosMap::iterator found = mMap.find(pid);
375 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700376 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700377 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700378 }
Girish434b4d82023-07-11 23:24:54 +0000379 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700380
Girish434b4d82023-07-11 23:24:54 +0000381 ResourceInfos::iterator foundClient = infos.find(clientId);
382 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700383 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700384 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700385 }
386
Girish434b4d82023-07-11 23:24:54 +0000387 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700388 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700389 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700390 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700391
392 if (res.value < 0) {
393 ALOGW("Ignoring request to remove negative value of resource");
394 continue;
395 }
Girishd11a03a2023-11-30 21:17:51 +0000396
397 long removedEntryValue = -1;
398 if (info.resources.remove(res, &removedEntryValue)) {
Chong Zhanga9d45c72020-09-09 12:41:17 -0700399 MediaResourceParcel actualRemoved = res;
Girishd11a03a2023-11-30 21:17:51 +0000400 if (removedEntryValue != -1) {
Girishab17b0f2023-11-20 06:00:44 +0000401 onLastRemoved(res, info.uid);
Girishd11a03a2023-11-30 21:17:51 +0000402 actualRemoved.value = removedEntryValue;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700403 }
404
405 // Add it to the list of removed resources for observers.
Girishd11a03a2023-11-30 21:17:51 +0000406 resourceRemoved.add(actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700407 }
408 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700409 if (mObserverService != nullptr && !resourceRemoved.empty()) {
410 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
411 }
Chong Zhang181e6952019-10-09 13:23:39 -0700412 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700413}
414
Girish9128e242022-11-23 20:52:29 +0000415Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
416 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700417 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900418}
419
Girish9128e242022-11-23 20:52:29 +0000420Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
421 int32_t pid = clientInfo.pid;
422 int32_t uid = clientInfo.uid;
423 int64_t clientId = clientInfo.id;
424 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
425 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700426 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700427
Girish434b4d82023-07-11 23:24:54 +0000428 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100429 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800430 pid_t callingPid = IPCThreadState::self()->getCallingPid();
431 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
432 pid, callingPid);
433 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800434 }
Girish434b4d82023-07-11 23:24:54 +0000435 PidResourceInfosMap::iterator found = mMap.find(pid);
436 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700437 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700438 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700439 }
Girish434b4d82023-07-11 23:24:54 +0000440 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700441
Girish434b4d82023-07-11 23:24:54 +0000442 ResourceInfos::iterator foundClient = infos.find(clientId);
443 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700444 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700445 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700446 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700447
Girish434b4d82023-07-11 23:24:54 +0000448 const ResourceInfo& info = foundClient->second;
Girishd11a03a2023-11-30 21:17:51 +0000449 for (const MediaResourceParcel& res : info.resources.getResources()) {
450 onLastRemoved(res, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700451 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700452
Girish1f002cf2023-02-17 00:36:29 +0000453 // Since this client has been removed, update the metrics collector.
454 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000455
Chong Zhanga9d45c72020-09-09 12:41:17 -0700456 if (mObserverService != nullptr && !info.resources.empty()) {
457 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
458 }
459
Girish434b4d82023-07-11 23:24:54 +0000460 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700461 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700462}
463
Girish56fda312023-10-12 21:32:35 +0000464void ResourceManagerService::getClientForResource_l(
465 const ResourceRequestInfo& resourceRequestInfo,
466 std::vector<ClientInfo>& clientsInfo) {
Girishaff0ef22023-10-13 00:22:39 +0000467 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000468 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700469 if (res == NULL) {
470 return;
471 }
Girishaff0ef22023-10-13 00:22:39 +0000472
473 // Before looking into other processes, check if we have clients marked for
474 // pending removal in the same process.
Girishab17b0f2023-11-20 06:00:44 +0000475 ClientInfo clientInfo;
476 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, clientInfo)) {
477 clientsInfo.emplace_back(clientInfo);
Girishaff0ef22023-10-13 00:22:39 +0000478 return;
479 }
480
481 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000482 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
483 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700484 }
485}
486
Girish0ac5c212023-11-23 09:14:03 +0000487bool ResourceManagerService::getTargetClients(
Girish88a83502023-11-23 11:23:07 +0000488 const ClientInfoParcel& clientInfo,
Girish0ac5c212023-11-23 09:14:03 +0000489 const std::vector<MediaResourceParcel>& resources,
490 std::vector<ClientInfo>& targetClients) {
Girish88a83502023-11-23 11:23:07 +0000491 int32_t callingPid = clientInfo.pid;
Girish1c682402024-01-31 21:03:51 +0000492 int64_t clientId = clientInfo.id;
Girish0ac5c212023-11-23 09:14:03 +0000493 std::scoped_lock lock{mLock};
494 if (!mProcessInfo->isPidTrusted(callingPid)) {
495 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
496 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
497 callingPid, actualCallingPid);
498 callingPid = actualCallingPid;
499 }
500 const MediaResourceParcel *secureCodec = NULL;
501 const MediaResourceParcel *nonSecureCodec = NULL;
502 const MediaResourceParcel *graphicMemory = NULL;
503 const MediaResourceParcel *drmSession = NULL;
504 for (size_t i = 0; i < resources.size(); ++i) {
505 switch (resources[i].type) {
506 case MediaResource::Type::kSecureCodec:
507 secureCodec = &resources[i];
508 break;
509 case MediaResource::Type::kNonSecureCodec:
510 nonSecureCodec = &resources[i];
511 break;
512 case MediaResource::Type::kGraphicMemory:
513 graphicMemory = &resources[i];
514 break;
515 case MediaResource::Type::kDrmSession:
516 drmSession = &resources[i];
517 break;
518 default:
519 break;
520 }
521 }
522
523 // first pass to handle secure/non-secure codec conflict
524 if (secureCodec != NULL) {
525 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
526 .subType = secureCodec->subType};
Girish1c682402024-01-31 21:03:51 +0000527 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &mediaResource};
Girish0ac5c212023-11-23 09:14:03 +0000528 if (!mSupportsMultipleSecureCodecs) {
529 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
530 return false;
531 }
532 }
533 if (!mSupportsSecureWithNonSecureCodec) {
534 mediaResource.type = MediaResource::Type::kNonSecureCodec;
535 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
536 return false;
537 }
538 }
539 }
540 if (nonSecureCodec != NULL) {
541 if (!mSupportsSecureWithNonSecureCodec) {
542 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
543 .subType = nonSecureCodec->subType};
Girish1c682402024-01-31 21:03:51 +0000544 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &mediaResource};
Girish0ac5c212023-11-23 09:14:03 +0000545 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
546 return false;
547 }
548 }
549 }
550
551 if (drmSession != NULL) {
Girish1c682402024-01-31 21:03:51 +0000552 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, drmSession};
Girish0ac5c212023-11-23 09:14:03 +0000553 getClientForResource_l(resourceRequestInfo, targetClients);
554 if (targetClients.size() == 0) {
555 return false;
556 }
557 }
558
559 if (targetClients.size() == 0 && graphicMemory != nullptr) {
560 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish1c682402024-01-31 21:03:51 +0000561 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, graphicMemory};
Girish0ac5c212023-11-23 09:14:03 +0000562 getClientForResource_l(resourceRequestInfo, targetClients);
563 }
564
565 if (targetClients.size() == 0) {
566 // if we are here, run the third pass to free one codec with the same type.
567 if (secureCodec != nullptr) {
Girish1c682402024-01-31 21:03:51 +0000568 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, secureCodec};
Girish0ac5c212023-11-23 09:14:03 +0000569 getClientForResource_l(resourceRequestInfo, targetClients);
570 }
571 if (nonSecureCodec != nullptr) {
Girish1c682402024-01-31 21:03:51 +0000572 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, nonSecureCodec};
Girish0ac5c212023-11-23 09:14:03 +0000573 getClientForResource_l(resourceRequestInfo, targetClients);
574 }
575 }
576
577 if (targetClients.size() == 0) {
578 // if we are here, run the fourth pass to free one codec with the different type.
579 if (secureCodec != nullptr) {
580 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish1c682402024-01-31 21:03:51 +0000581 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &temp};
Girish0ac5c212023-11-23 09:14:03 +0000582 getClientForResource_l(resourceRequestInfo, targetClients);
583 }
584 if (nonSecureCodec != nullptr) {
585 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish1c682402024-01-31 21:03:51 +0000586 ResourceRequestInfo resourceRequestInfo{callingPid, clientId, &temp};
Girish0ac5c212023-11-23 09:14:03 +0000587 getClientForResource_l(resourceRequestInfo, targetClients);
588 }
589 }
590
591 return !targetClients.empty();
592}
593
Girish9128e242022-11-23 20:52:29 +0000594Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100595 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000596 std::string clientName = clientInfo.name;
597 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Girish88a83502023-11-23 11:23:07 +0000598 clientInfo.pid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700599 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700600 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700601
Girish0ac5c212023-11-23 09:14:03 +0000602 // Check if there are any resources to be reclaimed before processing.
603 if (resources.empty()) {
Girishe7b338f2024-02-08 22:03:51 +0000604 // Invalid reclaim request. So no need to log.
Girish0ac5c212023-11-23 09:14:03 +0000605 return Status::ok();
606 }
607
Girish56fda312023-10-12 21:32:35 +0000608 std::vector<ClientInfo> targetClients;
Girishe7b338f2024-02-08 22:03:51 +0000609 if (getTargetClients(clientInfo, resources, targetClients)) {
610 // Reclaim all the target clients.
611 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
612 } else {
613 // No clients to reclaim from.
Girish0ac5c212023-11-23 09:14:03 +0000614 ALOGI("%s: There aren't any clients to reclaim from", __func__);
Girishe7b338f2024-02-08 22:03:51 +0000615 // We need to log this failed reclaim as "no clients to reclaim from".
616 targetClients.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700617 }
618
Girish9128e242022-11-23 20:52:29 +0000619 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000620 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000621
Wonsik Kim271429d2020-10-01 10:12:56 -0700622 return Status::ok();
623}
624
Girish9128e242022-11-23 20:52:29 +0000625void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000626 const std::vector<ClientInfo>& targetClients,
627 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000628 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000629 int requesterPriority = -1;
630 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000631 std::vector<int> priorities;
632 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000633
Girish56fda312023-10-12 21:32:35 +0000634 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000635 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000636 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000637 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000638 }
Girish56fda312023-10-12 21:32:35 +0000639 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000640}
641
Girishe8305272023-12-18 19:17:58 +0000642std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient_l(
Girishab17b0f2023-11-20 06:00:44 +0000643 int pid, const int64_t& clientId) const {
644 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
645 if (found == mMap.end()) {
646 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
647 return nullptr;
648 }
649
650 const ResourceInfos& infos = found->second;
651 ResourceInfos::const_iterator foundClient = infos.find(clientId);
652 if (foundClient == infos.end()) {
653 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
654 return nullptr;
655 }
656
657 return foundClient->second.client;
658}
659
Girishe8305272023-12-18 19:17:58 +0000660bool ResourceManagerService::removeClient_l(int pid, const int64_t& clientId) {
Girishab17b0f2023-11-20 06:00:44 +0000661 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
662 if (found == mMap.end()) {
663 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
664 return false;
665 }
666
667 ResourceInfos& infos = found->second;
668 ResourceInfos::iterator foundClient = infos.find(clientId);
669 if (foundClient == infos.end()) {
670 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
671 return false;
672 }
673
674 infos.erase(foundClient);
675 return true;
676}
677
Brian Lindahl64ee9452022-01-14 13:31:16 +0100678bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000679 const std::vector<ClientInfo>& targetClients) {
680 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700681 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700682 }
683
Girishab17b0f2023-11-20 06:00:44 +0000684 int64_t failedClientId = -1;
685 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000686 for (const ClientInfo& targetClient : targetClients) {
Girishe8305272023-12-18 19:17:58 +0000687 std::shared_ptr<IResourceManagerClient> client = nullptr;
688 {
689 std::scoped_lock lock{mLock};
690 client = getClient_l(targetClient.mPid, targetClient.mClientId);
691 }
Girishab17b0f2023-11-20 06:00:44 +0000692 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000693 // skip already released clients.
694 continue;
695 }
Girishab17b0f2023-11-20 06:00:44 +0000696 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700697 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700698 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000699 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700700 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000701 failedClientId = targetClient.mClientId;
702 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700703 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700704 }
705 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700706
Girishab17b0f2023-11-20 06:00:44 +0000707 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700708 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700709 }
710
Ronghua Wu67e7f542015-03-13 10:47:08 -0700711 {
Girish434b4d82023-07-11 23:24:54 +0000712 std::scoped_lock lock{mLock};
Girishe8305272023-12-18 19:17:58 +0000713 bool found = removeClient_l(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200714 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 Wu67e7f542015-03-13 10:47:08 -0700718 }
719 }
720
Wonsik Kim271429d2020-10-01 10:12:56 -0700721 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700722}
723
Girish6a6044d2023-11-22 21:23:14 +0000724bool 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 Lindahl64ee9452022-01-14 13:31:16 +0100734Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800735 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 {
Girish434b4d82023-07-11 23:24:54 +0000752 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000753 if (overridePid_l(originalPid, newPid)) {
Girish1f002cf2023-02-17 00:36:29 +0000754 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800755 }
756 }
757
758 return Status::ok();
759}
760
Girish6a6044d2023-11-22 21:23:14 +0000761bool 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 Zhang97d367b2020-09-16 12:53:14 -0700784Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100785 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700786 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
Girish434b4d82023-07-11 23:24:54 +0000803 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000804 if (!overrideProcessInfo_l(client, pid, procState, oomScore)) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700805 // Override value is rejected by ProcessInfo.
806 return Status::fromServiceSpecificError(BAD_VALUE);
807 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700808 return Status::ok();
Girish6a6044d2023-11-22 21:23:14 +0000809
Chong Zhang97d367b2020-09-16 12:53:14 -0700810}
811
Chong Zhang97d367b2020-09-16 12:53:14 -0700812void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000813 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700814
815 removeProcessInfoOverride_l(pid);
816}
817
818void 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 Zhang97d367b2020-09-16 12:53:14 -0700825 mProcessInfoOverrideMap.erase(pid);
826}
827
Girish9128e242022-11-23 20:52:29 +0000828Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
829 int32_t pid = clientInfo.pid;
830 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700831 String8 log = String8::format(
832 "markClientForPendingRemoval(pid %d, clientId %lld)",
833 pid, (long long) clientId);
834 mServiceLog->add(log);
835
Girish434b4d82023-07-11 23:24:54 +0000836 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100837 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800838 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 Kimd20e9362020-04-28 10:42:57 -0700842 }
Girish434b4d82023-07-11 23:24:54 +0000843 PidResourceInfosMap::iterator found = mMap.find(pid);
844 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700845 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
846 pid, (long long)clientId);
847 return Status::ok();
848 }
Girish434b4d82023-07-11 23:24:54 +0000849 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700850
Girish434b4d82023-07-11 23:24:54 +0000851 ResourceInfos::iterator foundClient = infos.find(clientId);
852 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700853 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
854 return Status::ok();
855 }
856
Girish434b4d82023-07-11 23:24:54 +0000857 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700858 info.pendingRemoval = true;
859 return Status::ok();
860}
861
Wonsik Kim271429d2020-10-01 10:12:56 -0700862Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
863 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
864 mServiceLog->add(log);
865
Girish56fda312023-10-12 21:32:35 +0000866 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700867 {
Girish434b4d82023-07-11 23:24:54 +0000868 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100869 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800870 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 Kim271429d2020-10-01 10:12:56 -0700874 }
875
876 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
877 MediaResource::Type::kNonSecureCodec,
878 MediaResource::Type::kGraphicMemory,
879 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100880 switch (type) {
881 // Codec resources are segregated by audio, video and image domains.
882 case MediaResource::Type::kSecureCodec:
883 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000884 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}) {
Girishab17b0f2023-11-20 06:00:44 +0000890 ClientInfo clientInfo;
891 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
892 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100893 continue;
894 }
895 }
896 break;
897 // Non-codec resources are shared by audio, video and image codecs (no subtype).
898 default:
Girishab17b0f2023-11-20 06:00:44 +0000899 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100900 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000901 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
902 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100903 }
904 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700905 }
906 }
907 }
908
Girish56fda312023-10-12 21:32:35 +0000909 if (!targetClients.empty()) {
910 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700911 }
912 return Status::ok();
913}
914
Girish6a6044d2023-11-22 21:23:14 +0000915bool ResourceManagerService::getPriority_l(int pid, int* priority) const {
Henry Fang32762922020-01-28 18:40:39 -0800916 int newPid = pid;
917
Girish6a6044d2023-11-22 21:23:14 +0000918 std::map<int, int>::const_iterator found = mOverridePidMap.find(pid);
919 if (found != mOverridePidMap.end()) {
920 newPid = found->second;
Henry Fang32762922020-01-28 18:40:39 -0800921 ALOGD("getPriority_l: use override pid %d instead original pid %d",
922 newPid, pid);
923 }
924
925 return mProcessInfo->getPriority(newPid, priority);
926}
927
Girish56fda312023-10-12 21:32:35 +0000928bool 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;
Girish9128e242022-11-23 20:52:29 +0000933
Girish434b4d82023-07-11 23:24:54 +0000934 for (auto& [pid, infos] : mMap) {
935 for (const auto& [id, info] : infos) {
Girish1c682402024-01-31 21:03:51 +0000936 if (pid == resourceRequestInfo.mCallingPid && id == resourceRequestInfo.mClientId) {
937 ALOGI("%s: Skip the client[%jd] for which the resource request is made",
938 __func__, id);
939 continue;
940 }
Girish434b4d82023-07-11 23:24:54 +0000941 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000942 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700943 // some higher/equal priority process owns the resource,
944 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000945 ALOGE("%s: can't reclaim resource %s from pid %d",
946 __func__, asString(type), pid);
947 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700948 return false;
949 }
Girishab17b0f2023-11-20 06:00:44 +0000950 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700951 }
952 }
953 }
Girish56fda312023-10-12 21:32:35 +0000954 if (clientsInfo.size() == 0) {
955 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700956 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700957 return true;
958}
959
Girishaff0ef22023-10-13 00:22:39 +0000960// Process priority (oom score) based reclaim:
961// - Find a process with lowest priority (than that of calling process).
962// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000963bool ResourceManagerService::getLowestPriorityBiggestClient_l(
964 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000965 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000966 int callingPid = resourceRequestInfo.mCallingPid;
967 MediaResource::Type type = resourceRequestInfo.mResource->type;
968 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700969 int lowestPriorityPid;
970 int lowestPriority;
971 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700972
Henry Fang32762922020-01-28 18:40:39 -0800973 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000974 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700975 return false;
976 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100977 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700978 return false;
979 }
980 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000981 ALOGE("%s: lowest priority %d vs caller priority %d",
982 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700983 return false;
984 }
985
Girishab17b0f2023-11-20 06:00:44 +0000986 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700987 return false;
988 }
Girish9128e242022-11-23 20:52:29 +0000989
Girishaff0ef22023-10-13 00:22:39 +0000990 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
991 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700992 return true;
993}
994
Brian Lindahl64ee9452022-01-14 13:31:16 +0100995bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
996 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700997 int pid = -1;
998 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000999 for (auto& [tempPid, infos] : mMap) {
1000 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001001 // no client on this process.
1002 continue;
1003 }
Girish434b4d82023-07-11 23:24:54 +00001004 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001005 // doesn't have the requested resource type
1006 continue;
1007 }
Girish434b4d82023-07-11 23:24:54 +00001008 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001009 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001010 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1011 // TODO: remove this pid from mMap?
1012 continue;
1013 }
1014 if (pid == -1 || tempPriority > priority) {
1015 // initial the value
1016 pid = tempPid;
1017 priority = tempPriority;
1018 }
1019 }
1020 if (pid != -1) {
1021 *lowestPriorityPid = pid;
1022 *lowestPriority = priority;
1023 }
1024 return (pid != -1);
1025}
1026
1027bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1028 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001029 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001030 return false;
1031 }
1032
1033 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001034 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001035 return false;
1036 }
1037
1038 return (callingPidPriority < priority);
1039}
1040
Brian Lindahl64ee9452022-01-14 13:31:16 +01001041bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001042 MediaResource::SubType subType, ClientInfo& clientInfo) {
1043 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001044}
1045
1046bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001047 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001048 PidResourceInfosMap::iterator found = mMap.find(pid);
1049 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001050 ALOGE_IF(!pendingRemovalOnly,
1051 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001052 return false;
1053 }
1054
Girishab17b0f2023-11-20 06:00:44 +00001055 uid_t uid = -1;
1056 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001057 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001058 const ResourceInfos& infos = found->second;
1059 for (const auto& [id, info] : infos) {
1060 const ResourceList& resources = info.resources;
1061 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001062 continue;
1063 }
Girishd11a03a2023-11-30 21:17:51 +00001064 for (const MediaResourceParcel& resource : resources.getResources()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001065 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001066 if (resource.value > largestValue) {
1067 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001068 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001069 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001070 }
1071 }
1072 }
1073 }
1074
Girishab17b0f2023-11-20 06:00:44 +00001075 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001076 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001077 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1078 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001079 return false;
1080 }
1081
Girishab17b0f2023-11-20 06:00:44 +00001082 clientInfo.mPid = pid;
1083 clientInfo.mUid = uid;
1084 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001085 return true;
1086}
1087
Girish1f002cf2023-02-17 00:36:29 +00001088Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1089 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1090 return Status::ok();
1091}
1092
1093Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1094 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1095 return Status::ok();
1096}
1097
1098Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1099 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1100 return Status::ok();
1101}
1102
Girishde8eb592023-04-13 18:49:17 +00001103Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1104 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1105 return Status::ok();
1106}
1107
Girish1f002cf2023-02-17 00:36:29 +00001108long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1109 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1110}
1111
1112long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1113 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1114}
1115
Girish6a6044d2023-11-22 21:23:14 +00001116void ResourceManagerService::notifyClientReleased(const ClientInfoParcel& clientInfo) {
1117 mResourceManagerMetrics->notifyClientReleased(clientInfo);
1118}
1119
Ronghua Wu231c3d12015-03-11 15:10:32 -07001120} // namespace android