blob: 3e0ac84ec9c5c97c3e8612bd72b60dc4c5c4724a [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");
77 for (auto it = resources.begin(); it != resources.end(); it++) {
78 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
79 resourceLog.append(buffer);
Dongwon Kang69c23dd2016-03-22 15:22:45 -070080 }
Dongwon Kangfe508d32015-12-15 14:22:05 +090081 }
82 }
Girish6a6044d2023-11-22 21:23:14 +000083
84 resourceLog.append(" Process Pid override:\n");
85 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
86 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
87 it->first, it->second);
88 resourceLog.append(buffer);
89 }
Dongwon Kangfe508d32015-12-15 14:22:05 +090090}
91
Brian Lindahl64ee9452022-01-14 13:31:16 +010092binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070093 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -070094
dcashman014e91e2015-09-11 09:33:01 -070095 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
96 result.format("Permission Denial: "
97 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -080098 AIBinder_getCallingPid(),
99 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000100 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -0700101 return PERMISSION_DENIED;
102 }
103
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700104 bool supportsMultipleSecureCodecs;
105 bool supportsSecureWithNonSecureCodec;
106 String8 serviceLog;
107 {
Girish434b4d82023-07-11 23:24:54 +0000108 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700109 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
110 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
111 serviceLog = mServiceLog->toString(" " /* linePrefix */);
112 }
113
Girish6a6044d2023-11-22 21:23:14 +0000114 // Get all the resource (and overload pid) logs
115 std::string resourceLog;
116 getResourceDump(resourceLog);
117
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700118 const size_t SIZE = 256;
119 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700120 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
121 result.append(buffer);
122 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700123 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700124 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700125 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
126 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700127 result.append(buffer);
128
Girish6a6044d2023-11-22 21:23:14 +0000129 result.append(resourceLog.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700130
Ronghua Wu022ed722015-05-11 15:15:09 -0700131 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700132 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700133
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000134 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700135 return OK;
136}
137
Brian Lindahl64ee9452022-01-14 13:31:16 +0100138struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800139 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700140
Chong Zhangdd726802019-08-21 17:24:13 -0700141 virtual void noteStartVideo(int uid) override {
142 BatteryNotifier::getInstance().noteStartVideo(uid);
143 }
144 virtual void noteStopVideo(int uid) override {
145 BatteryNotifier::getInstance().noteStopVideo(uid);
146 }
147 virtual void noteResetVideo() override {
148 BatteryNotifier::getInstance().noteResetVideo();
149 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800150 virtual bool requestCpusetBoost(bool enable) override {
151 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700152 }
153
154protected:
155 virtual ~SystemCallbackImpl() {}
156
157private:
158 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800159 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700160};
161
162ResourceManagerService::ResourceManagerService()
163 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
164
Brian Lindahl64ee9452022-01-14 13:31:16 +0100165ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700166 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700167 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700168 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700169 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700170 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700171 mSupportsSecureWithNonSecureCodec(true),
Girishab17b0f2023-11-20 06:00:44 +0000172 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700173 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000174 // Create ResourceManagerMetrics that handles all the metrics.
175 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700176}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700177
Chong Zhangfdd512a2019-11-22 11:03:14 -0800178//static
179void ResourceManagerService::instantiate() {
Girishab17b0f2023-11-20 06:00:44 +0000180 std::shared_ptr<ResourceManagerService> service = Create();
Chong Zhangfdd512a2019-11-22 11:03:14 -0800181 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000182 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000183 service->asBinder().get(), getServiceName(),
184 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800185 if (status != STATUS_OK) {
186 return;
187 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700188
189 std::shared_ptr<ResourceObserverService> observerService =
190 ResourceObserverService::instantiate();
191
192 if (observerService != nullptr) {
193 service->setObserverService(observerService);
194 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800195 // TODO: mediaserver main() is already starting the thread pool,
196 // move this to mediaserver main() when other services in mediaserver
197 // are converted to ndk-platform aidl.
198 //ABinderProcess_startThreadPool();
199}
200
Girishab17b0f2023-11-20 06:00:44 +0000201std::shared_ptr<ResourceManagerService> ResourceManagerService::Create() {
202 return Create(new ProcessInfo(), new SystemCallbackImpl());
203}
204
205std::shared_ptr<ResourceManagerService> ResourceManagerService::Create(
206 const sp<ProcessInfoInterface>& processInfo,
207 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000208 std::shared_ptr<ResourceManagerService> service = nullptr;
Girish1484e5d2023-11-20 06:00:44 +0000209 // If codec importance feature is on, create the refactored implementation.
210 if (CodecFeatureFlags::codec_importance()) {
Girish6a6044d2023-11-22 21:23:14 +0000211 service = ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo,
212 systemResource);
213 } else {
214 service = ::ndk::SharedRefBase::make<ResourceManagerService>(processInfo,
215 systemResource);
Girish1484e5d2023-11-20 06:00:44 +0000216 }
Girish6a6044d2023-11-22 21:23:14 +0000217
218 if (service != nullptr) {
219 service->init();
220 }
221
222 return service;
Girishab17b0f2023-11-20 06:00:44 +0000223}
224
Girish1484e5d2023-11-20 06:00:44 +0000225// TEST only function.
226std::shared_ptr<ResourceManagerService> ResourceManagerService::CreateNew(
227 const sp<ProcessInfoInterface>& processInfo,
228 const sp<SystemCallbackInterface>& systemResource) {
Girish6a6044d2023-11-22 21:23:14 +0000229 std::shared_ptr<ResourceManagerService> service =
230 ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo, systemResource);
231 service->init();
232 return service;
Girish1484e5d2023-11-20 06:00:44 +0000233}
234
Girish6a6044d2023-11-22 21:23:14 +0000235void ResourceManagerService::init() {}
236
Ronghua Wu231c3d12015-03-11 15:10:32 -0700237ResourceManagerService::~ResourceManagerService() {}
238
Chong Zhanga9d45c72020-09-09 12:41:17 -0700239void ResourceManagerService::setObserverService(
240 const std::shared_ptr<ResourceObserverService>& observerService) {
241 mObserverService = observerService;
242}
243
Chong Zhang181e6952019-10-09 13:23:39 -0700244Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000245 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700246 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700247
Girish434b4d82023-07-11 23:24:54 +0000248 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700249 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700250 const std::string &type = policies[i].type;
251 const std::string &value = policies[i].value;
252 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700253 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700254 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700255 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700256 }
257 }
Chong Zhang181e6952019-10-09 13:23:39 -0700258 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700259}
260
Girishab17b0f2023-11-20 06:00:44 +0000261void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700262 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700263 if (resource.type == MediaResource::Type::kCpuBoost
264 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700265 // Request it on every new instance of kCpuBoost, as the media.codec
266 // could have died, if we only do it the first time subsequent instances
267 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800268 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700269 ALOGW("couldn't request cpuset boost");
270 }
271 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700272 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000273 && (resource.subType == MediaResource::SubType::kHwVideoCodec
274 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000275 mSystemCB->noteStartVideo(uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700276 }
277}
278
Girishab17b0f2023-11-20 06:00:44 +0000279void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhang181e6952019-10-09 13:23:39 -0700280 if (resource.type == MediaResource::Type::kCpuBoost
281 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700282 && mCpuBoostCount > 0) {
283 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800284 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700285 }
Chong Zhang181e6952019-10-09 13:23:39 -0700286 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000287 && (resource.subType == MediaResource::SubType::kHwVideoCodec
288 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000289 mSystemCB->noteStopVideo(uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700290 }
291}
292
Girish9128e242022-11-23 20:52:29 +0000293Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800294 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700295 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000296 int32_t pid = clientInfo.pid;
297 int32_t uid = clientInfo.uid;
298 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000299 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000300 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700301 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700302
Girish434b4d82023-07-11 23:24:54 +0000303 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100304 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800305 pid_t callingPid = IPCThreadState::self()->getCallingPid();
306 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100307 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
308 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800309 pid = callingPid;
310 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800311 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700312 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000313 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700314 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700315
316 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700317 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700318 const auto resType = std::tuple(res.type, res.subType, res.id);
319
320 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
321 ALOGW("Ignoring request to remove negative value of non-drm resource");
322 continue;
323 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700324 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700325 if (res.value <= 0) {
326 // We can't init a new entry with negative value, although it's allowed
327 // to merge in negative values after the initial add.
328 ALOGW("Ignoring request to add new resource entry with value <= 0");
329 continue;
330 }
Girishab17b0f2023-11-20 06:00:44 +0000331 onFirstAdded(res, info.uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700332 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700333 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700334 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700335 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700336 // Add it to the list of added resources for observers.
337 auto it = resourceAdded.find(resType);
338 if (it == resourceAdded.end()) {
339 resourceAdded[resType] = res;
340 } else {
341 mergeResources(it->second, res);
342 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700343 }
Girishf1d166c2023-07-20 22:35:29 +0000344 if (info.deathNotifier == nullptr && client != nullptr) {
Girishab17b0f2023-11-20 06:00:44 +0000345 info.deathNotifier = DeathNotifier::Create(
346 client, ref<ResourceManagerService>(), clientInfo);
Wonsik Kim3e378962017-01-05 17:00:02 +0900347 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700348 if (mObserverService != nullptr && !resourceAdded.empty()) {
349 mObserverService->onResourceAdded(uid, pid, resourceAdded);
350 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900351 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000352
Chong Zhang181e6952019-10-09 13:23:39 -0700353 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700354}
355
Girish9128e242022-11-23 20:52:29 +0000356Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700357 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000358 int32_t pid = clientInfo.pid;
359 int32_t uid = clientInfo.uid;
360 int64_t clientId = clientInfo.id;
361 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000362 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700363 mServiceLog->add(log);
364
Girish434b4d82023-07-11 23:24:54 +0000365 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100366 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800367 pid_t callingPid = IPCThreadState::self()->getCallingPid();
368 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
369 pid, callingPid);
370 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700371 }
Girish434b4d82023-07-11 23:24:54 +0000372 PidResourceInfosMap::iterator found = mMap.find(pid);
373 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700374 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700375 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700376 }
Girish434b4d82023-07-11 23:24:54 +0000377 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700378
Girish434b4d82023-07-11 23:24:54 +0000379 ResourceInfos::iterator foundClient = infos.find(clientId);
380 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700381 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700382 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700383 }
384
Girish434b4d82023-07-11 23:24:54 +0000385 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700386 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700387 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700388 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700389 const auto resType = std::tuple(res.type, res.subType, res.id);
390
391 if (res.value < 0) {
392 ALOGW("Ignoring request to remove negative value of resource");
393 continue;
394 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700395 // ignore if we don't have it
396 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700397 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700398 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700399 if (resource.value > res.value) {
400 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700401 } else {
Girishab17b0f2023-11-20 06:00:44 +0000402 onLastRemoved(res, info.uid);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700403 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800404 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700405 }
406
407 // Add it to the list of removed resources for observers.
408 auto it = resourceRemoved.find(resType);
409 if (it == resourceRemoved.end()) {
410 resourceRemoved[resType] = actualRemoved;
411 } else {
412 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700413 }
414 }
415 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700416 if (mObserverService != nullptr && !resourceRemoved.empty()) {
417 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
418 }
Chong Zhang181e6952019-10-09 13:23:39 -0700419 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700420}
421
Girish9128e242022-11-23 20:52:29 +0000422Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
423 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700424 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900425}
426
Girish9128e242022-11-23 20:52:29 +0000427Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
428 int32_t pid = clientInfo.pid;
429 int32_t uid = clientInfo.uid;
430 int64_t clientId = clientInfo.id;
431 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
432 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700433 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700434
Girish434b4d82023-07-11 23:24:54 +0000435 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100436 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800437 pid_t callingPid = IPCThreadState::self()->getCallingPid();
438 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
439 pid, callingPid);
440 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800441 }
Girish434b4d82023-07-11 23:24:54 +0000442 PidResourceInfosMap::iterator found = mMap.find(pid);
443 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700444 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700445 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700446 }
Girish434b4d82023-07-11 23:24:54 +0000447 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700448
Girish434b4d82023-07-11 23:24:54 +0000449 ResourceInfos::iterator foundClient = infos.find(clientId);
450 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700451 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700452 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700453 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700454
Girish434b4d82023-07-11 23:24:54 +0000455 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700456 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
Girishab17b0f2023-11-20 06:00:44 +0000457 onLastRemoved(it->second, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700458 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700459
Girish1f002cf2023-02-17 00:36:29 +0000460 // Since this client has been removed, update the metrics collector.
461 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000462
Chong Zhanga9d45c72020-09-09 12:41:17 -0700463 if (mObserverService != nullptr && !info.resources.empty()) {
464 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
465 }
466
Girish434b4d82023-07-11 23:24:54 +0000467 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700468 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700469}
470
Girish56fda312023-10-12 21:32:35 +0000471void ResourceManagerService::getClientForResource_l(
472 const ResourceRequestInfo& resourceRequestInfo,
473 std::vector<ClientInfo>& clientsInfo) {
Girishaff0ef22023-10-13 00:22:39 +0000474 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000475 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700476 if (res == NULL) {
477 return;
478 }
Girishaff0ef22023-10-13 00:22:39 +0000479
480 // Before looking into other processes, check if we have clients marked for
481 // pending removal in the same process.
Girishab17b0f2023-11-20 06:00:44 +0000482 ClientInfo clientInfo;
483 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, clientInfo)) {
484 clientsInfo.emplace_back(clientInfo);
Girishaff0ef22023-10-13 00:22:39 +0000485 return;
486 }
487
488 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000489 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
490 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700491 }
492}
493
Girish9128e242022-11-23 20:52:29 +0000494Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100495 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000496 int32_t callingPid = clientInfo.pid;
497 std::string clientName = clientInfo.name;
498 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000499 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700500 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700501 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700502
Girish56fda312023-10-12 21:32:35 +0000503 std::vector<ClientInfo> targetClients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700504 {
Girish434b4d82023-07-11 23:24:54 +0000505 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100506 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800507 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
508 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
509 callingPid, actualCallingPid);
510 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800511 }
Chong Zhang181e6952019-10-09 13:23:39 -0700512 const MediaResourceParcel *secureCodec = NULL;
513 const MediaResourceParcel *nonSecureCodec = NULL;
514 const MediaResourceParcel *graphicMemory = NULL;
515 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700516 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100517 switch (resources[i].type) {
518 case MediaResource::Type::kSecureCodec:
519 secureCodec = &resources[i];
520 break;
521 case MediaResource::Type::kNonSecureCodec:
522 nonSecureCodec = &resources[i];
523 break;
524 case MediaResource::Type::kGraphicMemory:
525 graphicMemory = &resources[i];
526 break;
527 case MediaResource::Type::kDrmSession:
528 drmSession = &resources[i];
529 break;
530 default:
531 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700532 }
533 }
534
535 // first pass to handle secure/non-secure codec conflict
536 if (secureCodec != NULL) {
Girish56fda312023-10-12 21:32:35 +0000537 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
538 .subType = secureCodec->subType};
539 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
Ronghua Wu05d89f12015-07-07 16:47:42 -0700540 if (!mSupportsMultipleSecureCodecs) {
Girish56fda312023-10-12 21:32:35 +0000541 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700542 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700543 }
544 }
545 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000546 mediaResource.type = MediaResource::Type::kNonSecureCodec;
547 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700548 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700549 }
550 }
551 }
552 if (nonSecureCodec != NULL) {
553 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000554 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
555 .subType = nonSecureCodec->subType};
556 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
557 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700558 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700559 }
560 }
561 }
Girish56fda312023-10-12 21:32:35 +0000562
Robert Shihc3af31b2019-09-20 21:45:01 -0700563 if (drmSession != NULL) {
Girish56fda312023-10-12 21:32:35 +0000564 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
565 getClientForResource_l(resourceRequestInfo, targetClients);
566 if (targetClients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700567 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700568 }
569 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700570
Girish56fda312023-10-12 21:32:35 +0000571 if (targetClients.size() == 0 && graphicMemory != nullptr) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700572 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish56fda312023-10-12 21:32:35 +0000573 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
574 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700575 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700576
Girish56fda312023-10-12 21:32:35 +0000577 if (targetClients.size() == 0) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700578 // if we are here, run the third pass to free one codec with the same type.
Girish56fda312023-10-12 21:32:35 +0000579 if (secureCodec != nullptr) {
580 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
581 getClientForResource_l(resourceRequestInfo, targetClients);
582 }
583 if (nonSecureCodec != nullptr) {
584 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
585 getClientForResource_l(resourceRequestInfo, targetClients);
586 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700587 }
588
Girish56fda312023-10-12 21:32:35 +0000589 if (targetClients.size() == 0) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700590 // if we are here, run the fourth pass to free one codec with the different type.
Girish56fda312023-10-12 21:32:35 +0000591 if (secureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700592 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000593 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
594 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700595 }
Girish56fda312023-10-12 21:32:35 +0000596 if (nonSecureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700597 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000598 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
599 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700600 }
601 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700602 }
603
Girish56fda312023-10-12 21:32:35 +0000604 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000605
606 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000607 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000608
Wonsik Kim271429d2020-10-01 10:12:56 -0700609 return Status::ok();
610}
611
Girish9128e242022-11-23 20:52:29 +0000612void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000613 const std::vector<ClientInfo>& targetClients,
614 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000615 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000616 int requesterPriority = -1;
617 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000618 std::vector<int> priorities;
619 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000620
Girish56fda312023-10-12 21:32:35 +0000621 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000622 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000623 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000624 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000625 }
Girish56fda312023-10-12 21:32:35 +0000626 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000627}
628
Girishab17b0f2023-11-20 06:00:44 +0000629std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient(
630 int pid, const int64_t& clientId) const {
631 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
632 if (found == mMap.end()) {
633 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
634 return nullptr;
635 }
636
637 const ResourceInfos& infos = found->second;
638 ResourceInfos::const_iterator foundClient = infos.find(clientId);
639 if (foundClient == infos.end()) {
640 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
641 return nullptr;
642 }
643
644 return foundClient->second.client;
645}
646
647bool ResourceManagerService::removeClient(int pid, const int64_t& clientId) {
648 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
649 if (found == mMap.end()) {
650 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
651 return false;
652 }
653
654 ResourceInfos& infos = found->second;
655 ResourceInfos::iterator foundClient = infos.find(clientId);
656 if (foundClient == infos.end()) {
657 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
658 return false;
659 }
660
661 infos.erase(foundClient);
662 return true;
663}
664
Brian Lindahl64ee9452022-01-14 13:31:16 +0100665bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000666 const std::vector<ClientInfo>& targetClients) {
667 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700668 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700669 }
670
Girishab17b0f2023-11-20 06:00:44 +0000671 int64_t failedClientId = -1;
672 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000673 for (const ClientInfo& targetClient : targetClients) {
Girishab17b0f2023-11-20 06:00:44 +0000674 std::shared_ptr<IResourceManagerClient> client = getClient(
675 targetClient.mPid, targetClient.mClientId);
676 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000677 // skip already released clients.
678 continue;
679 }
Girishab17b0f2023-11-20 06:00:44 +0000680 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700681 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700682 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000683 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700684 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000685 failedClientId = targetClient.mClientId;
686 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700687 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700688 }
689 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700690
Girishab17b0f2023-11-20 06:00:44 +0000691 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700692 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700693 }
694
Ronghua Wu67e7f542015-03-13 10:47:08 -0700695 {
Girish434b4d82023-07-11 23:24:54 +0000696 std::scoped_lock lock{mLock};
Girishab17b0f2023-11-20 06:00:44 +0000697 bool found = removeClient(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200698 if (found) {
699 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
700 } else {
701 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700702 }
703 }
704
Wonsik Kim271429d2020-10-01 10:12:56 -0700705 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700706}
707
Girish6a6044d2023-11-22 21:23:14 +0000708bool ResourceManagerService::overridePid_l(int32_t originalPid, int32_t newPid) {
709 mOverridePidMap.erase(originalPid);
710 if (newPid != -1) {
711 mOverridePidMap.emplace(originalPid, newPid);
712 return true;
713 }
714
715 return false;
716}
717
Brian Lindahl64ee9452022-01-14 13:31:16 +0100718Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800719 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
720 originalPid, newPid);
721 mServiceLog->add(log);
722
723 // allow if this is called from the same process or the process has
724 // permission.
725 if ((AIBinder_getCallingPid() != getpid()) &&
726 (checkCallingPermission(String16(
727 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
728 ALOGE(
729 "Permission Denial: can't access overridePid method from pid=%d, "
730 "self pid=%d\n",
731 AIBinder_getCallingPid(), getpid());
732 return Status::fromServiceSpecificError(PERMISSION_DENIED);
733 }
734
735 {
Girish434b4d82023-07-11 23:24:54 +0000736 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000737 if (overridePid_l(originalPid, newPid)) {
Girish1f002cf2023-02-17 00:36:29 +0000738 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800739 }
740 }
741
742 return Status::ok();
743}
744
Girish6a6044d2023-11-22 21:23:14 +0000745bool ResourceManagerService::overrideProcessInfo_l(
746 const std::shared_ptr<IResourceManagerClient>& client,
747 int pid,
748 int procState,
749 int oomScore) {
750 removeProcessInfoOverride_l(pid);
751
752 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
753 // Override value is rejected by ProcessInfo.
754 return false;
755 }
756
757 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
758 .uid = 0,
759 .id = 0,
760 .name = "<unknown client>"};
761 auto deathNotifier = DeathNotifier::Create(
762 client, ref<ResourceManagerService>(), clientInfo, true);
763
764 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
765 return true;
766}
767
Chong Zhang97d367b2020-09-16 12:53:14 -0700768Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100769 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700770 int oomScore) {
771 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
772 pid, procState, oomScore);
773 mServiceLog->add(log);
774
775 // Only allow the override if the caller already can access process state and oom scores.
776 int callingPid = AIBinder_getCallingPid();
777 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
778 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
779 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
780 return Status::fromServiceSpecificError(PERMISSION_DENIED);
781 }
782
783 if (client == nullptr) {
784 return Status::fromServiceSpecificError(BAD_VALUE);
785 }
786
Girish434b4d82023-07-11 23:24:54 +0000787 std::scoped_lock lock{mLock};
Girish6a6044d2023-11-22 21:23:14 +0000788 if (!overrideProcessInfo_l(client, pid, procState, oomScore)) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700789 // Override value is rejected by ProcessInfo.
790 return Status::fromServiceSpecificError(BAD_VALUE);
791 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700792 return Status::ok();
Girish6a6044d2023-11-22 21:23:14 +0000793
Chong Zhang97d367b2020-09-16 12:53:14 -0700794}
795
Chong Zhang97d367b2020-09-16 12:53:14 -0700796void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000797 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700798
799 removeProcessInfoOverride_l(pid);
800}
801
802void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
803 auto it = mProcessInfoOverrideMap.find(pid);
804 if (it == mProcessInfoOverrideMap.end()) {
805 return;
806 }
807
808 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700809 mProcessInfoOverrideMap.erase(pid);
810}
811
Girish9128e242022-11-23 20:52:29 +0000812Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
813 int32_t pid = clientInfo.pid;
814 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700815 String8 log = String8::format(
816 "markClientForPendingRemoval(pid %d, clientId %lld)",
817 pid, (long long) clientId);
818 mServiceLog->add(log);
819
Girish434b4d82023-07-11 23:24:54 +0000820 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100821 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800822 pid_t callingPid = IPCThreadState::self()->getCallingPid();
823 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
824 pid, callingPid);
825 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700826 }
Girish434b4d82023-07-11 23:24:54 +0000827 PidResourceInfosMap::iterator found = mMap.find(pid);
828 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700829 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
830 pid, (long long)clientId);
831 return Status::ok();
832 }
Girish434b4d82023-07-11 23:24:54 +0000833 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700834
Girish434b4d82023-07-11 23:24:54 +0000835 ResourceInfos::iterator foundClient = infos.find(clientId);
836 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700837 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
838 return Status::ok();
839 }
840
Girish434b4d82023-07-11 23:24:54 +0000841 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700842 info.pendingRemoval = true;
843 return Status::ok();
844}
845
Wonsik Kim271429d2020-10-01 10:12:56 -0700846Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
847 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
848 mServiceLog->add(log);
849
Girish56fda312023-10-12 21:32:35 +0000850 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700851 {
Girish434b4d82023-07-11 23:24:54 +0000852 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100853 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800854 pid_t callingPid = IPCThreadState::self()->getCallingPid();
855 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
856 pid, callingPid);
857 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700858 }
859
860 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
861 MediaResource::Type::kNonSecureCodec,
862 MediaResource::Type::kGraphicMemory,
863 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100864 switch (type) {
865 // Codec resources are segregated by audio, video and image domains.
866 case MediaResource::Type::kSecureCodec:
867 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000868 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
869 MediaResource::SubType::kSwAudioCodec,
870 MediaResource::SubType::kHwVideoCodec,
871 MediaResource::SubType::kSwVideoCodec,
872 MediaResource::SubType::kHwImageCodec,
873 MediaResource::SubType::kSwImageCodec}) {
Girishab17b0f2023-11-20 06:00:44 +0000874 ClientInfo clientInfo;
875 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
876 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100877 continue;
878 }
879 }
880 break;
881 // Non-codec resources are shared by audio, video and image codecs (no subtype).
882 default:
Girishab17b0f2023-11-20 06:00:44 +0000883 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100884 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000885 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
886 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100887 }
888 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700889 }
890 }
891 }
892
Girish56fda312023-10-12 21:32:35 +0000893 if (!targetClients.empty()) {
894 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700895 }
896 return Status::ok();
897}
898
Girish6a6044d2023-11-22 21:23:14 +0000899bool ResourceManagerService::getPriority_l(int pid, int* priority) const {
Henry Fang32762922020-01-28 18:40:39 -0800900 int newPid = pid;
901
Girish6a6044d2023-11-22 21:23:14 +0000902 std::map<int, int>::const_iterator found = mOverridePidMap.find(pid);
903 if (found != mOverridePidMap.end()) {
904 newPid = found->second;
Henry Fang32762922020-01-28 18:40:39 -0800905 ALOGD("getPriority_l: use override pid %d instead original pid %d",
906 newPid, pid);
907 }
908
909 return mProcessInfo->getPriority(newPid, priority);
910}
911
Girish56fda312023-10-12 21:32:35 +0000912bool ResourceManagerService::getAllClients_l(
913 const ResourceRequestInfo& resourceRequestInfo,
914 std::vector<ClientInfo>& clientsInfo) {
915 MediaResource::Type type = resourceRequestInfo.mResource->type;
916 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000917
Girish434b4d82023-07-11 23:24:54 +0000918 for (auto& [pid, infos] : mMap) {
919 for (const auto& [id, info] : infos) {
920 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000921 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700922 // some higher/equal priority process owns the resource,
923 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000924 ALOGE("%s: can't reclaim resource %s from pid %d",
925 __func__, asString(type), pid);
926 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700927 return false;
928 }
Girishab17b0f2023-11-20 06:00:44 +0000929 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700930 }
931 }
932 }
Girish56fda312023-10-12 21:32:35 +0000933 if (clientsInfo.size() == 0) {
934 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700935 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700936 return true;
937}
938
Girishaff0ef22023-10-13 00:22:39 +0000939// Process priority (oom score) based reclaim:
940// - Find a process with lowest priority (than that of calling process).
941// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000942bool ResourceManagerService::getLowestPriorityBiggestClient_l(
943 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000944 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000945 int callingPid = resourceRequestInfo.mCallingPid;
946 MediaResource::Type type = resourceRequestInfo.mResource->type;
947 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700948 int lowestPriorityPid;
949 int lowestPriority;
950 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700951
Henry Fang32762922020-01-28 18:40:39 -0800952 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000953 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700954 return false;
955 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100956 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700957 return false;
958 }
959 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000960 ALOGE("%s: lowest priority %d vs caller priority %d",
961 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700962 return false;
963 }
964
Girishab17b0f2023-11-20 06:00:44 +0000965 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700966 return false;
967 }
Girish9128e242022-11-23 20:52:29 +0000968
Girishaff0ef22023-10-13 00:22:39 +0000969 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
970 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700971 return true;
972}
973
Brian Lindahl64ee9452022-01-14 13:31:16 +0100974bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
975 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700976 int pid = -1;
977 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000978 for (auto& [tempPid, infos] : mMap) {
979 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700980 // no client on this process.
981 continue;
982 }
Girish434b4d82023-07-11 23:24:54 +0000983 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700984 // doesn't have the requested resource type
985 continue;
986 }
Girish434b4d82023-07-11 23:24:54 +0000987 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -0800988 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700989 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
990 // TODO: remove this pid from mMap?
991 continue;
992 }
993 if (pid == -1 || tempPriority > priority) {
994 // initial the value
995 pid = tempPid;
996 priority = tempPriority;
997 }
998 }
999 if (pid != -1) {
1000 *lowestPriorityPid = pid;
1001 *lowestPriority = priority;
1002 }
1003 return (pid != -1);
1004}
1005
1006bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1007 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001008 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001009 return false;
1010 }
1011
1012 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001013 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001014 return false;
1015 }
1016
1017 return (callingPidPriority < priority);
1018}
1019
Brian Lindahl64ee9452022-01-14 13:31:16 +01001020bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001021 MediaResource::SubType subType, ClientInfo& clientInfo) {
1022 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001023}
1024
1025bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001026 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001027 PidResourceInfosMap::iterator found = mMap.find(pid);
1028 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001029 ALOGE_IF(!pendingRemovalOnly,
1030 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001031 return false;
1032 }
1033
Girishab17b0f2023-11-20 06:00:44 +00001034 uid_t uid = -1;
1035 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001036 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001037 const ResourceInfos& infos = found->second;
1038 for (const auto& [id, info] : infos) {
1039 const ResourceList& resources = info.resources;
1040 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001041 continue;
1042 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001043 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001044 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001045 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001046 if (resource.value > largestValue) {
1047 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001048 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001049 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001050 }
1051 }
1052 }
1053 }
1054
Girishab17b0f2023-11-20 06:00:44 +00001055 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001056 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001057 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1058 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001059 return false;
1060 }
1061
Girishab17b0f2023-11-20 06:00:44 +00001062 clientInfo.mPid = pid;
1063 clientInfo.mUid = uid;
1064 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001065 return true;
1066}
1067
Girish1f002cf2023-02-17 00:36:29 +00001068Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1069 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1070 return Status::ok();
1071}
1072
1073Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1074 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1075 return Status::ok();
1076}
1077
1078Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1079 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1080 return Status::ok();
1081}
1082
Girishde8eb592023-04-13 18:49:17 +00001083Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1084 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1085 return Status::ok();
1086}
1087
Girish1f002cf2023-02-17 00:36:29 +00001088long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1089 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1090}
1091
1092long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1093 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1094}
1095
Girish6a6044d2023-11-22 21:23:14 +00001096void ResourceManagerService::notifyClientReleased(const ClientInfoParcel& clientInfo) {
1097 mResourceManagerMetrics->notifyClientReleased(clientInfo);
1098}
1099
Ronghua Wu231c3d12015-03-11 15:10:32 -07001100} // namespace android