blob: b2971c4e7a6977979d0b1d3251e76b6c7ce06e59 [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>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <dirent.h>
Chong Zhang181e6952019-10-09 13:23:39 -070028#include <media/MediaResourcePolicy.h>
Girish1f002cf2023-02-17 00:36:29 +000029#include <media/stagefright/foundation/ABase.h>
Chong Zhangee33d642019-08-08 14:26:43 -070030#include <mediautils/BatteryNotifier.h>
Girish1f002cf2023-02-17 00:36:29 +000031#include <mediautils/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070032#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070033#include <string.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <unistd.h>
38
Steven Moreland0a0ff0b2021-04-02 00:06:01 +000039#include "IMediaResourceMonitor.h"
Girish1f002cf2023-02-17 00:36:29 +000040#include "ResourceManagerMetrics.h"
Ronghua Wu231c3d12015-03-11 15:10:32 -070041#include "ResourceManagerService.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070042#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070043#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070044
Ronghua Wu231c3d12015-03-11 15:10:32 -070045namespace android {
46
Chong Zhang97d367b2020-09-16 12:53:14 -070047//static
48std::mutex ResourceManagerService::sCookieLock;
49//static
50uintptr_t ResourceManagerService::sCookieCounter = 0;
51//static
52std::map<uintptr_t, sp<DeathNotifier> > ResourceManagerService::sCookieToDeathNotifierMap;
53
54class DeathNotifier : public RefBase {
55public:
Girish1f002cf2023-02-17 00:36:29 +000056 DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
57 const ClientInfoParcel& clientInfo);
Chong Zhang97d367b2020-09-16 12:53:14 -070058
59 virtual ~DeathNotifier() {}
60
61 // Implement death recipient
62 static void BinderDiedCallback(void* cookie);
63 virtual void binderDied();
64
65protected:
66 std::weak_ptr<ResourceManagerService> mService;
Girish1f002cf2023-02-17 00:36:29 +000067 const ClientInfoParcel mClientInfo;
Chong Zhang97d367b2020-09-16 12:53:14 -070068};
69
Chong Zhangfdd512a2019-11-22 11:03:14 -080070DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
Girish1f002cf2023-02-17 00:36:29 +000071 const ClientInfoParcel& clientInfo)
72 : mService(service), mClientInfo(clientInfo) {}
Wonsik Kim3e378962017-01-05 17:00:02 +090073
Chong Zhangfdd512a2019-11-22 11:03:14 -080074//static
75void DeathNotifier::BinderDiedCallback(void* cookie) {
Chong Zhang97d367b2020-09-16 12:53:14 -070076 sp<DeathNotifier> notifier;
77 {
78 std::scoped_lock lock{ResourceManagerService::sCookieLock};
79 auto it = ResourceManagerService::sCookieToDeathNotifierMap.find(
80 reinterpret_cast<uintptr_t>(cookie));
81 if (it == ResourceManagerService::sCookieToDeathNotifierMap.end()) {
82 return;
83 }
84 notifier = it->second;
85 }
86 if (notifier.get() != nullptr) {
87 notifier->binderDied();
88 }
Chong Zhangfdd512a2019-11-22 11:03:14 -080089}
Wonsik Kim3e378962017-01-05 17:00:02 +090090
Chong Zhangfdd512a2019-11-22 11:03:14 -080091void DeathNotifier::binderDied() {
92 // Don't check for pid validity since we know it's already dead.
93 std::shared_ptr<ResourceManagerService> service = mService.lock();
94 if (service == nullptr) {
95 ALOGW("ResourceManagerService is dead as well.");
96 return;
Wonsik Kim3e378962017-01-05 17:00:02 +090097 }
Henry Fang32762922020-01-28 18:40:39 -080098
Girish1f002cf2023-02-17 00:36:29 +000099 service->overridePid(mClientInfo.pid, -1);
Henry Fangb35141c2020-06-29 13:11:39 -0700100 // thiz is freed in the call below, so it must be last call referring thiz
Girish1f002cf2023-02-17 00:36:29 +0000101 service->removeResource(mClientInfo, false /*checkValid*/);
Chong Zhang97d367b2020-09-16 12:53:14 -0700102}
Henry Fangb35141c2020-06-29 13:11:39 -0700103
Chong Zhang97d367b2020-09-16 12:53:14 -0700104class OverrideProcessInfoDeathNotifier : public DeathNotifier {
105public:
106 OverrideProcessInfoDeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
Girish1f002cf2023-02-17 00:36:29 +0000107 const ClientInfoParcel& clientInfo)
108 : DeathNotifier(service, clientInfo) {}
Chong Zhang97d367b2020-09-16 12:53:14 -0700109
110 virtual ~OverrideProcessInfoDeathNotifier() {}
111
112 virtual void binderDied();
113};
114
115void OverrideProcessInfoDeathNotifier::binderDied() {
116 // Don't check for pid validity since we know it's already dead.
117 std::shared_ptr<ResourceManagerService> service = mService.lock();
118 if (service == nullptr) {
119 ALOGW("ResourceManagerService is dead as well.");
120 return;
121 }
122
Girish1f002cf2023-02-17 00:36:29 +0000123 service->removeProcessInfoOverride(mClientInfo.pid);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800124}
Wonsik Kim3e378962017-01-05 17:00:02 +0900125
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126template <typename T>
Girish434b4d82023-07-11 23:24:54 +0000127static String8 getString(const std::vector<T>& items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700128 String8 itemsStr;
129 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700130 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -0700131 }
132 return itemsStr;
133}
134
Brian Lindahl64ee9452022-01-14 13:31:16 +0100135static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
Girish429f8592022-10-13 04:27:00 +0000136 const MediaResourceParcel& resource) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100137 if (type != resource.type) {
138 return false;
139 }
140 switch (type) {
141 // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
142 // compare the subtypes as well.
143 case MediaResource::Type::kSecureCodec:
144 case MediaResource::Type::kNonSecureCodec:
145 if (resource.subType == subType) {
146 return true;
147 }
148 break;
149 // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
150 default:
151 return true;
152 }
153 return false;
154}
155
156static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
157 const ResourceList& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700158 for (auto it = resources.begin(); it != resources.end(); it++) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100159 if (hasResourceType(type, subType, it->second)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700160 return true;
161 }
162 }
163 return false;
164}
165
Brian Lindahl64ee9452022-01-14 13:31:16 +0100166static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
167 const ResourceInfos& infos) {
Girish434b4d82023-07-11 23:24:54 +0000168 for (const auto& [id, info] : infos) {
169 if (hasResourceType(type, subType, info.resources)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700170 return true;
171 }
172 }
173 return false;
174}
175
Brian Lindahl64ee9452022-01-14 13:31:16 +0100176static ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
Girish434b4d82023-07-11 23:24:54 +0000177 PidResourceInfosMap::iterator found = map.find(pid);
178 if (found == map.end()) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700179 // new pid
180 ResourceInfos infosForPid;
Girish434b4d82023-07-11 23:24:54 +0000181 auto [it, inserted] = map.emplace(pid, infosForPid);
182 found = it;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700183 }
184
Girish434b4d82023-07-11 23:24:54 +0000185 return found->second;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700186}
187
Brian Lindahl64ee9452022-01-14 13:31:16 +0100188static ResourceInfo& getResourceInfoForEdit(uid_t uid, int64_t clientId,
Girish9128e242022-11-23 20:52:29 +0000189 const std::string& name,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100190 const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
Girish434b4d82023-07-11 23:24:54 +0000191 ResourceInfos::iterator found = infos.find(clientId);
Chong Zhangfb092d32019-08-12 09:45:44 -0700192
Girish434b4d82023-07-11 23:24:54 +0000193 if (found == infos.end()) {
194 ResourceInfo info{.uid = uid,
195 .clientId = clientId,
196 .name = name.empty()? "<unknown client>" : name,
197 .client = client,
198 .cookie = 0,
199 .pendingRemoval = false};
200 auto [it, inserted] = infos.emplace(clientId, info);
201 found = it;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700202 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700203
Girish434b4d82023-07-11 23:24:54 +0000204 return found->second;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700205}
206
Girish434b4d82023-07-11 23:24:54 +0000207static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900208 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700209 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900210 if (binder != NULL) {
211 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
212 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100213 switch (resources[i].subType) {
214 case MediaResource::SubType::kAudioCodec:
215 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
216 break;
217 case MediaResource::SubType::kVideoCodec:
218 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
219 break;
220 case MediaResource::SubType::kImageCodec:
221 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
222 break;
223 case MediaResource::SubType::kUnspecifiedSubType:
224 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700225 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900226 }
227 }
228}
229
Brian Lindahl64ee9452022-01-14 13:31:16 +0100230binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700231 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700232
dcashman014e91e2015-09-11 09:33:01 -0700233 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
234 result.format("Permission Denial: "
235 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800236 AIBinder_getCallingPid(),
237 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700238 write(fd, result.string(), result.size());
239 return PERMISSION_DENIED;
240 }
241
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700242 PidResourceInfosMap mapCopy;
243 bool supportsMultipleSecureCodecs;
244 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800245 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700246 String8 serviceLog;
247 {
Girish434b4d82023-07-11 23:24:54 +0000248 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700249 mapCopy = mMap; // Shadow copy, real copy will happen on write.
250 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
251 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
252 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800253 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700254 }
255
256 const size_t SIZE = 256;
257 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700258 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
259 result.append(buffer);
260 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700261 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700262 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700263 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
264 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700265 result.append(buffer);
266
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700267 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000268 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000269 snprintf(buffer, SIZE, " Pid: %d\n", pid);
270 result.append(buffer);
271 int priority = 0;
272 if (getPriority_l(pid, &priority)) {
273 snprintf(buffer, SIZE, " Priority: %d\n", priority);
274 } else {
275 snprintf(buffer, SIZE, " Priority: <unknown>\n");
276 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700277 result.append(buffer);
278
Girish434b4d82023-07-11 23:24:54 +0000279 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700280 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000281 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700282 result.append(buffer);
283
Girish434b4d82023-07-11 23:24:54 +0000284 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700285 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700286 result.append(buffer);
287
Girish434b4d82023-07-11 23:24:54 +0000288 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700289 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700290 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700291 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700292 result.append(buffer);
293 }
294 }
295 }
Henry Fang32762922020-01-28 18:40:39 -0800296 result.append(" Process Pid override:\n");
297 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
298 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
299 it->first, it->second);
300 result.append(buffer);
301 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700302 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700303 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700304
305 write(fd, result.string(), result.size());
306 return OK;
307}
308
Brian Lindahl64ee9452022-01-14 13:31:16 +0100309struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800310 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700311
Chong Zhangdd726802019-08-21 17:24:13 -0700312 virtual void noteStartVideo(int uid) override {
313 BatteryNotifier::getInstance().noteStartVideo(uid);
314 }
315 virtual void noteStopVideo(int uid) override {
316 BatteryNotifier::getInstance().noteStopVideo(uid);
317 }
318 virtual void noteResetVideo() override {
319 BatteryNotifier::getInstance().noteResetVideo();
320 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800321 virtual bool requestCpusetBoost(bool enable) override {
322 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700323 }
324
325protected:
326 virtual ~SystemCallbackImpl() {}
327
328private:
329 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800330 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700331};
332
333ResourceManagerService::ResourceManagerService()
334 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
335
Brian Lindahl64ee9452022-01-14 13:31:16 +0100336ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700337 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700338 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700339 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700340 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700341 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700342 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800343 mCpuBoostCount(0),
344 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700345 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000346 // Create ResourceManagerMetrics that handles all the metrics.
347 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700348}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700349
Chong Zhangfdd512a2019-11-22 11:03:14 -0800350//static
351void ResourceManagerService::instantiate() {
352 std::shared_ptr<ResourceManagerService> service =
353 ::ndk::SharedRefBase::make<ResourceManagerService>();
354 binder_status_t status =
Charles Chene55549f2023-03-15 01:21:22 +0000355 AServiceManager_addServiceWithFlags(
356 service->asBinder().get(), getServiceName(),
357 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800358 if (status != STATUS_OK) {
359 return;
360 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700361
362 std::shared_ptr<ResourceObserverService> observerService =
363 ResourceObserverService::instantiate();
364
365 if (observerService != nullptr) {
366 service->setObserverService(observerService);
367 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800368 // TODO: mediaserver main() is already starting the thread pool,
369 // move this to mediaserver main() when other services in mediaserver
370 // are converted to ndk-platform aidl.
371 //ABinderProcess_startThreadPool();
372}
373
Ronghua Wu231c3d12015-03-11 15:10:32 -0700374ResourceManagerService::~ResourceManagerService() {}
375
Chong Zhanga9d45c72020-09-09 12:41:17 -0700376void ResourceManagerService::setObserverService(
377 const std::shared_ptr<ResourceObserverService>& observerService) {
378 mObserverService = observerService;
379}
380
Chong Zhang181e6952019-10-09 13:23:39 -0700381Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700382 String8 log = String8::format("config(%s)", getString(policies).string());
383 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700384
Girish434b4d82023-07-11 23:24:54 +0000385 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700386 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700387 const std::string &type = policies[i].type;
388 const std::string &value = policies[i].value;
389 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700390 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700391 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700392 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700393 }
394 }
Chong Zhang181e6952019-10-09 13:23:39 -0700395 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700396}
397
Brian Lindahl64ee9452022-01-14 13:31:16 +0100398void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource,
399 const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700400 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700401 if (resource.type == MediaResource::Type::kCpuBoost
402 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700403 // Request it on every new instance of kCpuBoost, as the media.codec
404 // could have died, if we only do it the first time subsequent instances
405 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800406 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700407 ALOGW("couldn't request cpuset boost");
408 }
409 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700410 } else if (resource.type == MediaResource::Type::kBattery
411 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700412 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700413 }
414}
415
Brian Lindahl64ee9452022-01-14 13:31:16 +0100416void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource,
417 const ResourceInfo& clientInfo) {
Chong Zhang181e6952019-10-09 13:23:39 -0700418 if (resource.type == MediaResource::Type::kCpuBoost
419 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700420 && mCpuBoostCount > 0) {
421 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800422 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700423 }
Chong Zhang181e6952019-10-09 13:23:39 -0700424 } else if (resource.type == MediaResource::Type::kBattery
425 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700426 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700427 }
428}
429
Brian Lindahl64ee9452022-01-14 13:31:16 +0100430void ResourceManagerService::mergeResources(MediaResourceParcel& r1,
431 const MediaResourceParcel& r2) {
Chong Zhang181e6952019-10-09 13:23:39 -0700432 // The resource entry on record is maintained to be in [0,INT64_MAX].
433 // Clamp if merging in the new resource value causes it to go out of bound.
434 // Note that the new resource value could be negative, eg.DrmSession, the
435 // value goes lower when the session is used more often. During reclaim
436 // the session with the highest value (lowest usage) would be closed.
437 if (r2.value < INT64_MAX - r1.value) {
438 r1.value += r2.value;
439 if (r1.value < 0) {
440 r1.value = 0;
441 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700442 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700443 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700444 }
445}
446
Girish9128e242022-11-23 20:52:29 +0000447Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800448 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700449 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000450 int32_t pid = clientInfo.pid;
451 int32_t uid = clientInfo.uid;
452 int64_t clientId = clientInfo.id;
453 const std::string& name = clientInfo.name;
454 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
455 pid, uid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700456 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700457
Girish434b4d82023-07-11 23:24:54 +0000458 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100459 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800460 pid_t callingPid = IPCThreadState::self()->getCallingPid();
461 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100462 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
463 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800464 pid = callingPid;
465 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800466 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700467 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish9128e242022-11-23 20:52:29 +0000468 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700469 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700470
471 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700472 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700473 const auto resType = std::tuple(res.type, res.subType, res.id);
474
475 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
476 ALOGW("Ignoring request to remove negative value of non-drm resource");
477 continue;
478 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700479 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700480 if (res.value <= 0) {
481 // We can't init a new entry with negative value, although it's allowed
482 // to merge in negative values after the initial add.
483 ALOGW("Ignoring request to add new resource entry with value <= 0");
484 continue;
485 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700486 onFirstAdded(res, info);
487 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700488 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700489 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700490 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700491 // Add it to the list of added resources for observers.
492 auto it = resourceAdded.find(resType);
493 if (it == resourceAdded.end()) {
494 resourceAdded[resType] = res;
495 } else {
496 mergeResources(it->second, res);
497 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700498 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700499 if (info.cookie == 0 && client != nullptr) {
Arun Johnsond1f59732022-03-25 17:10:29 +0000500 info.cookie = addCookieAndLink_l(client,
Girish1f002cf2023-02-17 00:36:29 +0000501 new DeathNotifier(ref<ResourceManagerService>(), clientInfo));
Wonsik Kim3e378962017-01-05 17:00:02 +0900502 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700503 if (mObserverService != nullptr && !resourceAdded.empty()) {
504 mObserverService->onResourceAdded(uid, pid, resourceAdded);
505 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900506 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000507
Chong Zhang181e6952019-10-09 13:23:39 -0700508 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700509}
510
Girish9128e242022-11-23 20:52:29 +0000511Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700512 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000513 int32_t pid = clientInfo.pid;
514 int32_t uid = clientInfo.uid;
515 int64_t clientId = clientInfo.id;
516 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
517 pid, uid, (long long) clientId, getString(resources).string());
Chong Zhangfb092d32019-08-12 09:45:44 -0700518 mServiceLog->add(log);
519
Girish434b4d82023-07-11 23:24:54 +0000520 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100521 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800522 pid_t callingPid = IPCThreadState::self()->getCallingPid();
523 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
524 pid, callingPid);
525 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700526 }
Girish434b4d82023-07-11 23:24:54 +0000527 PidResourceInfosMap::iterator found = mMap.find(pid);
528 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700529 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700530 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700531 }
Girish434b4d82023-07-11 23:24:54 +0000532 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700533
Girish434b4d82023-07-11 23:24:54 +0000534 ResourceInfos::iterator foundClient = infos.find(clientId);
535 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700536 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700537 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700538 }
539
Girish434b4d82023-07-11 23:24:54 +0000540 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700541 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700542 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700543 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700544 const auto resType = std::tuple(res.type, res.subType, res.id);
545
546 if (res.value < 0) {
547 ALOGW("Ignoring request to remove negative value of resource");
548 continue;
549 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700550 // ignore if we don't have it
551 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700552 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700553 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700554 if (resource.value > res.value) {
555 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700556 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700557 onLastRemoved(res, info);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700558 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800559 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700560 }
561
562 // Add it to the list of removed resources for observers.
563 auto it = resourceRemoved.find(resType);
564 if (it == resourceRemoved.end()) {
565 resourceRemoved[resType] = actualRemoved;
566 } else {
567 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700568 }
569 }
570 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700571 if (mObserverService != nullptr && !resourceRemoved.empty()) {
572 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
573 }
Chong Zhang181e6952019-10-09 13:23:39 -0700574 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700575}
576
Girish9128e242022-11-23 20:52:29 +0000577Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
578 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700579 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900580}
581
Girish9128e242022-11-23 20:52:29 +0000582Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
583 int32_t pid = clientInfo.pid;
584 int32_t uid = clientInfo.uid;
585 int64_t clientId = clientInfo.id;
586 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
587 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700588 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700589
Girish434b4d82023-07-11 23:24:54 +0000590 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100591 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800592 pid_t callingPid = IPCThreadState::self()->getCallingPid();
593 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
594 pid, callingPid);
595 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800596 }
Girish434b4d82023-07-11 23:24:54 +0000597 PidResourceInfosMap::iterator found = mMap.find(pid);
598 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700599 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700600 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700601 }
Girish434b4d82023-07-11 23:24:54 +0000602 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700603
Girish434b4d82023-07-11 23:24:54 +0000604 ResourceInfos::iterator foundClient = infos.find(clientId);
605 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700606 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700607 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700608 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700609
Girish434b4d82023-07-11 23:24:54 +0000610 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700611 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
612 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700613 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700614
Girish1f002cf2023-02-17 00:36:29 +0000615 // Since this client has been removed, update the metrics collector.
616 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000617
Arun Johnsond1f59732022-03-25 17:10:29 +0000618 removeCookieAndUnlink_l(info.client, info.cookie);
Chong Zhangfb092d32019-08-12 09:45:44 -0700619
Chong Zhanga9d45c72020-09-09 12:41:17 -0700620 if (mObserverService != nullptr && !info.resources.empty()) {
621 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
622 }
623
Girish434b4d82023-07-11 23:24:54 +0000624 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700625 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700626}
627
Girish9128e242022-11-23 20:52:29 +0000628void ResourceManagerService::getClientForResource_l(int callingPid,
629 const MediaResourceParcel *res,
630 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000631 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700632 if (res == NULL) {
633 return;
634 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800635 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000636 if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700637 clients->push_back(client);
638 }
639}
640
Girish9128e242022-11-23 20:52:29 +0000641Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100642 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000643 int32_t callingPid = clientInfo.pid;
644 std::string clientName = clientInfo.name;
645 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
646 callingPid, clientInfo.uid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700647 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700648 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700649
Girish434b4d82023-07-11 23:24:54 +0000650 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Girish9128e242022-11-23 20:52:29 +0000651 PidUidVector idVector;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700652 {
Girish434b4d82023-07-11 23:24:54 +0000653 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100654 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800655 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
656 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
657 callingPid, actualCallingPid);
658 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800659 }
Chong Zhang181e6952019-10-09 13:23:39 -0700660 const MediaResourceParcel *secureCodec = NULL;
661 const MediaResourceParcel *nonSecureCodec = NULL;
662 const MediaResourceParcel *graphicMemory = NULL;
663 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700664 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100665 switch (resources[i].type) {
666 case MediaResource::Type::kSecureCodec:
667 secureCodec = &resources[i];
668 break;
669 case MediaResource::Type::kNonSecureCodec:
670 nonSecureCodec = &resources[i];
671 break;
672 case MediaResource::Type::kGraphicMemory:
673 graphicMemory = &resources[i];
674 break;
675 case MediaResource::Type::kDrmSession:
676 drmSession = &resources[i];
677 break;
678 default:
679 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700680 }
681 }
682
683 // first pass to handle secure/non-secure codec conflict
684 if (secureCodec != NULL) {
685 if (!mSupportsMultipleSecureCodecs) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100686 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000687 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700688 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700689 }
690 }
691 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100692 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000693 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700694 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700695 }
696 }
697 }
698 if (nonSecureCodec != NULL) {
699 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100700 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000701 nonSecureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700702 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700703 }
704 }
705 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700706 if (drmSession != NULL) {
Girish9128e242022-11-23 20:52:29 +0000707 getClientForResource_l(callingPid, drmSession, &idVector, &clients);
Robert Shihc3af31b2019-09-20 21:45:01 -0700708 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700709 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700710 }
711 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700712
713 if (clients.size() == 0) {
714 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish9128e242022-11-23 20:52:29 +0000715 getClientForResource_l(callingPid, graphicMemory, &idVector, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700716 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700717
718 if (clients.size() == 0) {
719 // if we are here, run the third pass to free one codec with the same type.
Girish9128e242022-11-23 20:52:29 +0000720 getClientForResource_l(callingPid, secureCodec, &idVector, &clients);
721 getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700722 }
723
724 if (clients.size() == 0) {
725 // if we are here, run the fourth pass to free one codec with the different type.
726 if (secureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700727 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000728 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700729 }
730 if (nonSecureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700731 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000732 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700733 }
734 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700735 }
736
Brian Lindahl64ee9452022-01-14 13:31:16 +0100737 *_aidl_return = reclaimUnconditionallyFrom(clients);
Girish9128e242022-11-23 20:52:29 +0000738
739 // Log Reclaim Pushed Atom to statsd
740 pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return);
741
Wonsik Kim271429d2020-10-01 10:12:56 -0700742 return Status::ok();
743}
744
Girish9128e242022-11-23 20:52:29 +0000745void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish434b4d82023-07-11 23:24:54 +0000746 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish9128e242022-11-23 20:52:29 +0000747 const PidUidVector& idVector, bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000748 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000749 int requesterPriority = -1;
750 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000751 std::vector<int> priorities;
752 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000753
Girish1f002cf2023-02-17 00:36:29 +0000754 for (PidUidVector::const_reference id : idVector) {
Girish9128e242022-11-23 20:52:29 +0000755 int targetPriority = -1;
756 getPriority_l(id.first, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000757 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000758 }
Girish1f002cf2023-02-17 00:36:29 +0000759 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients,
760 idVector, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000761}
762
Brian Lindahl64ee9452022-01-14 13:31:16 +0100763bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish434b4d82023-07-11 23:24:54 +0000764 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700765 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700766 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700767 }
768
Chong Zhangfdd512a2019-11-22 11:03:14 -0800769 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700770 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700771 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700772 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700773 bool success;
774 Status status = clients[i]->reclaimResource(&success);
775 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700776 failedClient = clients[i];
777 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700778 }
779 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700780
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700781 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700782 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700783 }
784
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200785 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700786 {
Girish434b4d82023-07-11 23:24:54 +0000787 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700788 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000789 for (auto& [pid, infos] : mMap) {
790 for (const auto& [id, info] : infos) {
791 if (info.client == failedClient) {
792 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700793 found = true;
Girish434b4d82023-07-11 23:24:54 +0000794 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700795 }
796 }
797 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000798 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700799 break;
800 }
801 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200802 if (found) {
803 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
804 } else {
805 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700806 }
807 }
808
Wonsik Kim271429d2020-10-01 10:12:56 -0700809 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700810}
811
Brian Lindahl64ee9452022-01-14 13:31:16 +0100812Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800813 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
814 originalPid, newPid);
815 mServiceLog->add(log);
816
817 // allow if this is called from the same process or the process has
818 // permission.
819 if ((AIBinder_getCallingPid() != getpid()) &&
820 (checkCallingPermission(String16(
821 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
822 ALOGE(
823 "Permission Denial: can't access overridePid method from pid=%d, "
824 "self pid=%d\n",
825 AIBinder_getCallingPid(), getpid());
826 return Status::fromServiceSpecificError(PERMISSION_DENIED);
827 }
828
829 {
Girish434b4d82023-07-11 23:24:54 +0000830 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800831 mOverridePidMap.erase(originalPid);
832 if (newPid != -1) {
833 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000834 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800835 }
836 }
837
838 return Status::ok();
839}
840
Chong Zhang97d367b2020-09-16 12:53:14 -0700841Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100842 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700843 int oomScore) {
844 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
845 pid, procState, oomScore);
846 mServiceLog->add(log);
847
848 // Only allow the override if the caller already can access process state and oom scores.
849 int callingPid = AIBinder_getCallingPid();
850 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
851 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
852 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
853 return Status::fromServiceSpecificError(PERMISSION_DENIED);
854 }
855
856 if (client == nullptr) {
857 return Status::fromServiceSpecificError(BAD_VALUE);
858 }
859
Girish434b4d82023-07-11 23:24:54 +0000860 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700861 removeProcessInfoOverride_l(pid);
862
863 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
864 // Override value is rejected by ProcessInfo.
865 return Status::fromServiceSpecificError(BAD_VALUE);
866 }
867
Girish1f002cf2023-02-17 00:36:29 +0000868 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
869 .uid = 0,
870 .id = 0,
871 .name = "<unknown client>"};
Arun Johnsond1f59732022-03-25 17:10:29 +0000872 uintptr_t cookie = addCookieAndLink_l(client,
Girish1f002cf2023-02-17 00:36:29 +0000873 new OverrideProcessInfoDeathNotifier(ref<ResourceManagerService>(), clientInfo));
Chong Zhang97d367b2020-09-16 12:53:14 -0700874
875 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{cookie, client});
876
877 return Status::ok();
878}
879
Arun Johnsond1f59732022-03-25 17:10:29 +0000880uintptr_t ResourceManagerService::addCookieAndLink_l(
881 const std::shared_ptr<IResourceManagerClient>& client, const sp<DeathNotifier>& notifier) {
882 if (client == nullptr) {
883 return 0;
884 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700885 std::scoped_lock lock{sCookieLock};
886
887 uintptr_t cookie;
888 // Need to skip cookie 0 (if it wraps around). ResourceInfo has cookie initialized to 0
889 // indicating the death notifier is not created yet.
890 while ((cookie = ++sCookieCounter) == 0);
Arun Johnsond1f59732022-03-25 17:10:29 +0000891 AIBinder_linkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700892 sCookieToDeathNotifierMap.emplace(cookie, notifier);
893
894 return cookie;
895}
896
Arun Johnsond1f59732022-03-25 17:10:29 +0000897void ResourceManagerService::removeCookieAndUnlink_l(
898 const std::shared_ptr<IResourceManagerClient>& client, uintptr_t cookie) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700899 std::scoped_lock lock{sCookieLock};
Arun Johnsond1f59732022-03-25 17:10:29 +0000900 if (client != nullptr) {
901 AIBinder_unlinkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie);
902 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700903 sCookieToDeathNotifierMap.erase(cookie);
904}
905
906void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000907 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700908
909 removeProcessInfoOverride_l(pid);
910}
911
912void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
913 auto it = mProcessInfoOverrideMap.find(pid);
914 if (it == mProcessInfoOverrideMap.end()) {
915 return;
916 }
917
918 mProcessInfo->removeProcessInfoOverride(pid);
919
Arun Johnsond1f59732022-03-25 17:10:29 +0000920 removeCookieAndUnlink_l(it->second.client, it->second.cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700921
922 mProcessInfoOverrideMap.erase(pid);
923}
924
Girish9128e242022-11-23 20:52:29 +0000925Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
926 int32_t pid = clientInfo.pid;
927 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700928 String8 log = String8::format(
929 "markClientForPendingRemoval(pid %d, clientId %lld)",
930 pid, (long long) clientId);
931 mServiceLog->add(log);
932
Girish434b4d82023-07-11 23:24:54 +0000933 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100934 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800935 pid_t callingPid = IPCThreadState::self()->getCallingPid();
936 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
937 pid, callingPid);
938 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700939 }
Girish434b4d82023-07-11 23:24:54 +0000940 PidResourceInfosMap::iterator found = mMap.find(pid);
941 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700942 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
943 pid, (long long)clientId);
944 return Status::ok();
945 }
Girish434b4d82023-07-11 23:24:54 +0000946 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700947
Girish434b4d82023-07-11 23:24:54 +0000948 ResourceInfos::iterator foundClient = infos.find(clientId);
949 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700950 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
951 return Status::ok();
952 }
953
Girish434b4d82023-07-11 23:24:54 +0000954 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700955 info.pendingRemoval = true;
956 return Status::ok();
957}
958
Wonsik Kim271429d2020-10-01 10:12:56 -0700959Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
960 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
961 mServiceLog->add(log);
962
Girish434b4d82023-07-11 23:24:54 +0000963 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700964 {
Girish434b4d82023-07-11 23:24:54 +0000965 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100966 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800967 pid_t callingPid = IPCThreadState::self()->getCallingPid();
968 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
969 pid, callingPid);
970 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700971 }
972
973 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
974 MediaResource::Type::kNonSecureCodec,
975 MediaResource::Type::kGraphicMemory,
976 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100977 switch (type) {
978 // Codec resources are segregated by audio, video and image domains.
979 case MediaResource::Type::kSecureCodec:
980 case MediaResource::Type::kNonSecureCodec:
981 for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec,
982 MediaResource::SubType::kVideoCodec,
983 MediaResource::SubType::kImageCodec}) {
984 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000985 uid_t uid = 0;
986 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000987 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100988 continue;
989 }
990 }
991 break;
992 // Non-codec resources are shared by audio, video and image codecs (no subtype).
993 default:
994 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000995 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100996 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +0000997 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000998 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100999 }
1000 break;
Wonsik Kim271429d2020-10-01 10:12:56 -07001001 }
1002 }
1003 }
1004
1005 if (!clients.empty()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001006 reclaimUnconditionallyFrom(clients);
Wonsik Kim271429d2020-10-01 10:12:56 -07001007 }
1008 return Status::ok();
1009}
1010
Henry Fang32762922020-01-28 18:40:39 -08001011bool ResourceManagerService::getPriority_l(int pid, int* priority) {
1012 int newPid = pid;
1013
1014 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
1015 newPid = mOverridePidMap[pid];
1016 ALOGD("getPriority_l: use override pid %d instead original pid %d",
1017 newPid, pid);
1018 }
1019
1020 return mProcessInfo->getPriority(newPid, priority);
1021}
1022
Brian Lindahl64ee9452022-01-14 13:31:16 +01001023bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001024 MediaResource::SubType subType,
1025 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +00001026 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
1027 std::vector<std::shared_ptr<IResourceManagerClient>> temp;
Girish9128e242022-11-23 20:52:29 +00001028 PidUidVector tempIdList;
1029
Girish434b4d82023-07-11 23:24:54 +00001030 for (auto& [pid, infos] : mMap) {
1031 for (const auto& [id, info] : infos) {
1032 if (hasResourceType(type, subType, info.resources)) {
1033 if (!isCallingPriorityHigher_l(callingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001034 // some higher/equal priority process owns the resource,
1035 // this request can't be fulfilled.
1036 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Girish434b4d82023-07-11 23:24:54 +00001037 asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001038 return false;
1039 }
Girish434b4d82023-07-11 23:24:54 +00001040 temp.push_back(info.client);
1041 tempIdList.emplace_back(pid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 }
1043 }
1044 }
1045 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -08001046 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001047 return true;
1048 }
Girish434b4d82023-07-11 23:24:54 +00001049
1050 clients->insert(std::end(*clients), std::begin(temp), std::end(temp));
Girish9128e242022-11-23 20:52:29 +00001051 idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001052 return true;
1053}
1054
Brian Lindahl64ee9452022-01-14 13:31:16 +01001055bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid,
Girish9128e242022-11-23 20:52:29 +00001056 MediaResource::Type type,
1057 MediaResource::SubType subType,
1058 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -08001059 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001060 int lowestPriorityPid;
1061 int lowestPriority;
1062 int callingPriority;
Girish9128e242022-11-23 20:52:29 +00001063 uid_t uid = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001064
1065 // Before looking into other processes, check if we have clients marked for
1066 // pending removal in the same process.
Girish9128e242022-11-23 20:52:29 +00001067 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) {
1068 idVector->emplace_back(callingPid, uid);
Wonsik Kimd20e9362020-04-28 10:42:57 -07001069 return true;
1070 }
Henry Fang32762922020-01-28 18:40:39 -08001071 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001072 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
1073 callingPid);
1074 return false;
1075 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001076 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001077 return false;
1078 }
1079 if (lowestPriority <= callingPriority) {
1080 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1081 lowestPriority, callingPriority);
1082 return false;
1083 }
1084
Girish9128e242022-11-23 20:52:29 +00001085 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001086 return false;
1087 }
Girish9128e242022-11-23 20:52:29 +00001088
1089 idVector->emplace_back(lowestPriorityPid, uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001090 return true;
1091}
1092
Brian Lindahl64ee9452022-01-14 13:31:16 +01001093bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1094 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001095 int pid = -1;
1096 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001097 for (auto& [tempPid, infos] : mMap) {
1098 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001099 // no client on this process.
1100 continue;
1101 }
Girish434b4d82023-07-11 23:24:54 +00001102 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001103 // doesn't have the requested resource type
1104 continue;
1105 }
Girish434b4d82023-07-11 23:24:54 +00001106 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001107 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001108 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1109 // TODO: remove this pid from mMap?
1110 continue;
1111 }
1112 if (pid == -1 || tempPriority > priority) {
1113 // initial the value
1114 pid = tempPid;
1115 priority = tempPriority;
1116 }
1117 }
1118 if (pid != -1) {
1119 *lowestPriorityPid = pid;
1120 *lowestPriority = priority;
1121 }
1122 return (pid != -1);
1123}
1124
1125bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1126 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001127 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001128 return false;
1129 }
1130
1131 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001132 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001133 return false;
1134 }
1135
1136 return (callingPidPriority < priority);
1137}
1138
Brian Lindahl64ee9452022-01-14 13:31:16 +01001139bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001140 MediaResource::SubType subType, uid_t& uid,
1141 std::shared_ptr<IResourceManagerClient> *client) {
1142 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001143}
1144
1145bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001146 MediaResource::SubType subType, uid_t& uid,
1147 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001148 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001149 PidResourceInfosMap::iterator found = mMap.find(pid);
1150 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001151 ALOGE_IF(!pendingRemovalOnly,
1152 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001153 return false;
1154 }
1155
Chong Zhangfdd512a2019-11-22 11:03:14 -08001156 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001157 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001158 const ResourceInfos& infos = found->second;
1159 for (const auto& [id, info] : infos) {
1160 const ResourceList& resources = info.resources;
1161 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001162 continue;
1163 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001164 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001165 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001166 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001167 if (resource.value > largestValue) {
1168 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001169 clientTemp = info.client;
1170 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001171 }
1172 }
1173 }
1174 }
1175
1176 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001177 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001178 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1179 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001180 return false;
1181 }
1182
1183 *client = clientTemp;
1184 return true;
1185}
1186
Girish1f002cf2023-02-17 00:36:29 +00001187Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1188 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1189 return Status::ok();
1190}
1191
1192Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1193 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1194 return Status::ok();
1195}
1196
1197Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1198 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1199 return Status::ok();
1200}
1201
Girishde8eb592023-04-13 18:49:17 +00001202Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1203 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1204 return Status::ok();
1205}
1206
Girish1f002cf2023-02-17 00:36:29 +00001207long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1208 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1209}
1210
1211long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1212 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1213}
1214
Ronghua Wu231c3d12015-03-11 15:10:32 -07001215} // namespace android