blob: 803bfece3c3c51e62f6ab18ac1f759c1f38e3cb3 [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>
Chong Zhang181e6952019-10-09 13:23:39 -0700127static 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,
136 MediaResourceParcel resource) {
137 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) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700168 for (size_t i = 0; i < infos.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100169 if (hasResourceType(type, subType, infos[i].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) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700177 ssize_t index = map.indexOfKey(pid);
178 if (index < 0) {
179 // new pid
180 ResourceInfos infosForPid;
181 map.add(pid, infosForPid);
182 }
183
184 return map.editValueFor(pid);
185}
186
Brian Lindahl64ee9452022-01-14 13:31:16 +0100187static ResourceInfo& getResourceInfoForEdit(uid_t uid, int64_t clientId,
Girish9128e242022-11-23 20:52:29 +0000188 const std::string& name,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100189 const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700190 ssize_t index = infos.indexOfKey(clientId);
191
192 if (index < 0) {
193 ResourceInfo info;
194 info.uid = uid;
195 info.clientId = clientId;
Girish1f002cf2023-02-17 00:36:29 +0000196 if (name.empty()) {
197 info.name = "<unknown client>";
198 } else {
199 info.name = name;
200 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700201 info.client = client;
Chong Zhang97d367b2020-09-16 12:53:14 -0700202 info.cookie = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700203 info.pendingRemoval = false;
Chong Zhangfb092d32019-08-12 09:45:44 -0700204
205 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700206 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700207
208 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700209}
210
Chong Zhang181e6952019-10-09 13:23:39 -0700211static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900212 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700213 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900214 if (binder != NULL) {
215 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
216 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100217 switch (resources[i].subType) {
218 case MediaResource::SubType::kAudioCodec:
219 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
220 break;
221 case MediaResource::SubType::kVideoCodec:
222 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
223 break;
224 case MediaResource::SubType::kImageCodec:
225 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
226 break;
227 case MediaResource::SubType::kUnspecifiedSubType:
228 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700229 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900230 }
231 }
232}
233
Brian Lindahl64ee9452022-01-14 13:31:16 +0100234binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700235 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700236
dcashman014e91e2015-09-11 09:33:01 -0700237 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
238 result.format("Permission Denial: "
239 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800240 AIBinder_getCallingPid(),
241 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700242 write(fd, result.string(), result.size());
243 return PERMISSION_DENIED;
244 }
245
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700246 PidResourceInfosMap mapCopy;
247 bool supportsMultipleSecureCodecs;
248 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800249 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700250 String8 serviceLog;
251 {
252 Mutex::Autolock lock(mLock);
253 mapCopy = mMap; // Shadow copy, real copy will happen on write.
254 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
255 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
256 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800257 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700258 }
259
260 const size_t SIZE = 256;
261 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700262 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
263 result.append(buffer);
264 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700265 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700266 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700267 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
268 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700269 result.append(buffer);
270
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700271 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700272 for (size_t i = 0; i < mapCopy.size(); ++i) {
Girish9128e242022-11-23 20:52:29 +0000273 int pid = mapCopy.keyAt(i);
274 snprintf(buffer, SIZE, " Pid: %d\n", pid);
275 result.append(buffer);
276 int priority = 0;
277 if (getPriority_l(pid, &priority)) {
278 snprintf(buffer, SIZE, " Priority: %d\n", priority);
279 } else {
280 snprintf(buffer, SIZE, " Priority: <unknown>\n");
281 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700282 result.append(buffer);
283
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700284 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700285 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700286 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700287 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
288 result.append(buffer);
289
Girish1f002cf2023-02-17 00:36:29 +0000290 std::string clientName = infos[j].name;
Chong Zhang181e6952019-10-09 13:23:39 -0700291 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700292 result.append(buffer);
293
Chong Zhangfb092d32019-08-12 09:45:44 -0700294 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700295 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700296 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700297 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700298 result.append(buffer);
299 }
300 }
301 }
Henry Fang32762922020-01-28 18:40:39 -0800302 result.append(" Process Pid override:\n");
303 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
304 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
305 it->first, it->second);
306 result.append(buffer);
307 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700308 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700309 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700310
311 write(fd, result.string(), result.size());
312 return OK;
313}
314
Brian Lindahl64ee9452022-01-14 13:31:16 +0100315struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800316 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700317
Chong Zhangdd726802019-08-21 17:24:13 -0700318 virtual void noteStartVideo(int uid) override {
319 BatteryNotifier::getInstance().noteStartVideo(uid);
320 }
321 virtual void noteStopVideo(int uid) override {
322 BatteryNotifier::getInstance().noteStopVideo(uid);
323 }
324 virtual void noteResetVideo() override {
325 BatteryNotifier::getInstance().noteResetVideo();
326 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800327 virtual bool requestCpusetBoost(bool enable) override {
328 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700329 }
330
331protected:
332 virtual ~SystemCallbackImpl() {}
333
334private:
335 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800336 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700337};
338
339ResourceManagerService::ResourceManagerService()
340 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
341
Brian Lindahl64ee9452022-01-14 13:31:16 +0100342ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700343 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700344 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700345 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700346 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700347 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700348 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800349 mCpuBoostCount(0),
350 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700351 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000352 // Create ResourceManagerMetrics that handles all the metrics.
353 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700354}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700355
Chong Zhangfdd512a2019-11-22 11:03:14 -0800356//static
357void ResourceManagerService::instantiate() {
358 std::shared_ptr<ResourceManagerService> service =
359 ::ndk::SharedRefBase::make<ResourceManagerService>();
360 binder_status_t status =
Charles Chene55549f2023-03-15 01:21:22 +0000361 AServiceManager_addServiceWithFlags(
362 service->asBinder().get(), getServiceName(),
363 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800364 if (status != STATUS_OK) {
365 return;
366 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700367
368 std::shared_ptr<ResourceObserverService> observerService =
369 ResourceObserverService::instantiate();
370
371 if (observerService != nullptr) {
372 service->setObserverService(observerService);
373 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800374 // TODO: mediaserver main() is already starting the thread pool,
375 // move this to mediaserver main() when other services in mediaserver
376 // are converted to ndk-platform aidl.
377 //ABinderProcess_startThreadPool();
378}
379
Ronghua Wu231c3d12015-03-11 15:10:32 -0700380ResourceManagerService::~ResourceManagerService() {}
381
Chong Zhanga9d45c72020-09-09 12:41:17 -0700382void ResourceManagerService::setObserverService(
383 const std::shared_ptr<ResourceObserverService>& observerService) {
384 mObserverService = observerService;
385}
386
Chong Zhang181e6952019-10-09 13:23:39 -0700387Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700388 String8 log = String8::format("config(%s)", getString(policies).string());
389 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700390
391 Mutex::Autolock lock(mLock);
392 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700393 const std::string &type = policies[i].type;
394 const std::string &value = policies[i].value;
395 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700396 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700397 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700398 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700399 }
400 }
Chong Zhang181e6952019-10-09 13:23:39 -0700401 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700402}
403
Brian Lindahl64ee9452022-01-14 13:31:16 +0100404void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource,
405 const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700406 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700407 if (resource.type == MediaResource::Type::kCpuBoost
408 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700409 // Request it on every new instance of kCpuBoost, as the media.codec
410 // could have died, if we only do it the first time subsequent instances
411 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800412 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700413 ALOGW("couldn't request cpuset boost");
414 }
415 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700416 } else if (resource.type == MediaResource::Type::kBattery
417 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700418 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700419 }
420}
421
Brian Lindahl64ee9452022-01-14 13:31:16 +0100422void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource,
423 const ResourceInfo& clientInfo) {
Chong Zhang181e6952019-10-09 13:23:39 -0700424 if (resource.type == MediaResource::Type::kCpuBoost
425 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700426 && mCpuBoostCount > 0) {
427 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800428 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700429 }
Chong Zhang181e6952019-10-09 13:23:39 -0700430 } else if (resource.type == MediaResource::Type::kBattery
431 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700432 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700433 }
434}
435
Brian Lindahl64ee9452022-01-14 13:31:16 +0100436void ResourceManagerService::mergeResources(MediaResourceParcel& r1,
437 const MediaResourceParcel& r2) {
Chong Zhang181e6952019-10-09 13:23:39 -0700438 // The resource entry on record is maintained to be in [0,INT64_MAX].
439 // Clamp if merging in the new resource value causes it to go out of bound.
440 // Note that the new resource value could be negative, eg.DrmSession, the
441 // value goes lower when the session is used more often. During reclaim
442 // the session with the highest value (lowest usage) would be closed.
443 if (r2.value < INT64_MAX - r1.value) {
444 r1.value += r2.value;
445 if (r1.value < 0) {
446 r1.value = 0;
447 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700448 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700449 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700450 }
451}
452
Girish9128e242022-11-23 20:52:29 +0000453Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800454 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700455 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000456 int32_t pid = clientInfo.pid;
457 int32_t uid = clientInfo.uid;
458 int64_t clientId = clientInfo.id;
459 const std::string& name = clientInfo.name;
460 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
461 pid, uid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700462 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700463
464 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100465 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800466 pid_t callingPid = IPCThreadState::self()->getCallingPid();
467 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100468 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
469 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800470 pid = callingPid;
471 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800472 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700473 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish9128e242022-11-23 20:52:29 +0000474 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700475 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700476
477 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700478 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700479 const auto resType = std::tuple(res.type, res.subType, res.id);
480
481 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
482 ALOGW("Ignoring request to remove negative value of non-drm resource");
483 continue;
484 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700485 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700486 if (res.value <= 0) {
487 // We can't init a new entry with negative value, although it's allowed
488 // to merge in negative values after the initial add.
489 ALOGW("Ignoring request to add new resource entry with value <= 0");
490 continue;
491 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700492 onFirstAdded(res, info);
493 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700494 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700495 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700496 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700497 // Add it to the list of added resources for observers.
498 auto it = resourceAdded.find(resType);
499 if (it == resourceAdded.end()) {
500 resourceAdded[resType] = res;
501 } else {
502 mergeResources(it->second, res);
503 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700504 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700505 if (info.cookie == 0 && client != nullptr) {
Arun Johnsond1f59732022-03-25 17:10:29 +0000506 info.cookie = addCookieAndLink_l(client,
Girish1f002cf2023-02-17 00:36:29 +0000507 new DeathNotifier(ref<ResourceManagerService>(), clientInfo));
Wonsik Kim3e378962017-01-05 17:00:02 +0900508 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700509 if (mObserverService != nullptr && !resourceAdded.empty()) {
510 mObserverService->onResourceAdded(uid, pid, resourceAdded);
511 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900512 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000513
Chong Zhang181e6952019-10-09 13:23:39 -0700514 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700515}
516
Girish9128e242022-11-23 20:52:29 +0000517Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700518 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000519 int32_t pid = clientInfo.pid;
520 int32_t uid = clientInfo.uid;
521 int64_t clientId = clientInfo.id;
522 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
523 pid, uid, (long long) clientId, getString(resources).string());
Chong Zhangfb092d32019-08-12 09:45:44 -0700524 mServiceLog->add(log);
525
526 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100527 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800528 pid_t callingPid = IPCThreadState::self()->getCallingPid();
529 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
530 pid, callingPid);
531 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700532 }
533 ssize_t index = mMap.indexOfKey(pid);
534 if (index < 0) {
535 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700536 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700537 }
538 ResourceInfos &infos = mMap.editValueAt(index);
539
540 index = infos.indexOfKey(clientId);
541 if (index < 0) {
542 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700543 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700544 }
545
546 ResourceInfo &info = infos.editValueAt(index);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700547 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700548 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700549 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700550 const auto resType = std::tuple(res.type, res.subType, res.id);
551
552 if (res.value < 0) {
553 ALOGW("Ignoring request to remove negative value of resource");
554 continue;
555 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700556 // ignore if we don't have it
557 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700558 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700559 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700560 if (resource.value > res.value) {
561 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700562 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700563 onLastRemoved(res, info);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700564 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800565 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700566 }
567
568 // Add it to the list of removed resources for observers.
569 auto it = resourceRemoved.find(resType);
570 if (it == resourceRemoved.end()) {
571 resourceRemoved[resType] = actualRemoved;
572 } else {
573 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700574 }
575 }
576 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700577 if (mObserverService != nullptr && !resourceRemoved.empty()) {
578 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
579 }
Chong Zhang181e6952019-10-09 13:23:39 -0700580 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700581}
582
Girish9128e242022-11-23 20:52:29 +0000583Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
584 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700585 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900586}
587
Girish9128e242022-11-23 20:52:29 +0000588Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
589 int32_t pid = clientInfo.pid;
590 int32_t uid = clientInfo.uid;
591 int64_t clientId = clientInfo.id;
592 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
593 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700594 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700595
596 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100597 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800598 pid_t callingPid = IPCThreadState::self()->getCallingPid();
599 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
600 pid, callingPid);
601 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800602 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700603 ssize_t index = mMap.indexOfKey(pid);
604 if (index < 0) {
605 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700606 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700607 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700608 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700609
610 index = infos.indexOfKey(clientId);
611 if (index < 0) {
612 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700613 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700614 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700615
616 const ResourceInfo &info = infos[index];
617 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
618 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700619 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700620
Girish1f002cf2023-02-17 00:36:29 +0000621 // Since this client has been removed, update the metrics collector.
622 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000623
Arun Johnsond1f59732022-03-25 17:10:29 +0000624 removeCookieAndUnlink_l(info.client, info.cookie);
Chong Zhangfb092d32019-08-12 09:45:44 -0700625
Chong Zhanga9d45c72020-09-09 12:41:17 -0700626 if (mObserverService != nullptr && !info.resources.empty()) {
627 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
628 }
629
Chong Zhangfb092d32019-08-12 09:45:44 -0700630 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700631 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700632}
633
Girish9128e242022-11-23 20:52:29 +0000634void ResourceManagerService::getClientForResource_l(int callingPid,
635 const MediaResourceParcel *res,
636 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800637 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700638 if (res == NULL) {
639 return;
640 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800641 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000642 if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700643 clients->push_back(client);
644 }
645}
646
Girish9128e242022-11-23 20:52:29 +0000647Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100648 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000649 int32_t callingPid = clientInfo.pid;
650 std::string clientName = clientInfo.name;
651 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
652 callingPid, clientInfo.uid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700653 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700654 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700655
Chong Zhangfdd512a2019-11-22 11:03:14 -0800656 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Girish9128e242022-11-23 20:52:29 +0000657 PidUidVector idVector;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700658 {
659 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100660 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800661 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
662 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
663 callingPid, actualCallingPid);
664 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800665 }
Chong Zhang181e6952019-10-09 13:23:39 -0700666 const MediaResourceParcel *secureCodec = NULL;
667 const MediaResourceParcel *nonSecureCodec = NULL;
668 const MediaResourceParcel *graphicMemory = NULL;
669 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700670 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100671 switch (resources[i].type) {
672 case MediaResource::Type::kSecureCodec:
673 secureCodec = &resources[i];
674 break;
675 case MediaResource::Type::kNonSecureCodec:
676 nonSecureCodec = &resources[i];
677 break;
678 case MediaResource::Type::kGraphicMemory:
679 graphicMemory = &resources[i];
680 break;
681 case MediaResource::Type::kDrmSession:
682 drmSession = &resources[i];
683 break;
684 default:
685 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700686 }
687 }
688
689 // first pass to handle secure/non-secure codec conflict
690 if (secureCodec != NULL) {
691 if (!mSupportsMultipleSecureCodecs) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100692 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
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 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100698 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000699 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700700 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700701 }
702 }
703 }
704 if (nonSecureCodec != NULL) {
705 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100706 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000707 nonSecureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700708 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700709 }
710 }
711 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700712 if (drmSession != NULL) {
Girish9128e242022-11-23 20:52:29 +0000713 getClientForResource_l(callingPid, drmSession, &idVector, &clients);
Robert Shihc3af31b2019-09-20 21:45:01 -0700714 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700715 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700716 }
717 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700718
719 if (clients.size() == 0) {
720 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish9128e242022-11-23 20:52:29 +0000721 getClientForResource_l(callingPid, graphicMemory, &idVector, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700722 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700723
724 if (clients.size() == 0) {
725 // if we are here, run the third pass to free one codec with the same type.
Girish9128e242022-11-23 20:52:29 +0000726 getClientForResource_l(callingPid, secureCodec, &idVector, &clients);
727 getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700728 }
729
730 if (clients.size() == 0) {
731 // if we are here, run the fourth pass to free one codec with the different type.
732 if (secureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700733 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000734 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700735 }
736 if (nonSecureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700737 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000738 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700739 }
740 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700741 }
742
Brian Lindahl64ee9452022-01-14 13:31:16 +0100743 *_aidl_return = reclaimUnconditionallyFrom(clients);
Girish9128e242022-11-23 20:52:29 +0000744
745 // Log Reclaim Pushed Atom to statsd
746 pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return);
747
Wonsik Kim271429d2020-10-01 10:12:56 -0700748 return Status::ok();
749}
750
Girish9128e242022-11-23 20:52:29 +0000751void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
752 const Vector<std::shared_ptr<IResourceManagerClient>>& clients,
753 const PidUidVector& idVector, bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000754 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000755 int requesterPriority = -1;
756 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000757 std::vector<int> priorities;
758 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000759
Girish1f002cf2023-02-17 00:36:29 +0000760 for (PidUidVector::const_reference id : idVector) {
Girish9128e242022-11-23 20:52:29 +0000761 int targetPriority = -1;
762 getPriority_l(id.first, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000763 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000764 }
Girish1f002cf2023-02-17 00:36:29 +0000765 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients,
766 idVector, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000767}
768
Brian Lindahl64ee9452022-01-14 13:31:16 +0100769bool ResourceManagerService::reclaimUnconditionallyFrom(
Wonsik Kim271429d2020-10-01 10:12:56 -0700770 const Vector<std::shared_ptr<IResourceManagerClient>> &clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700771 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700772 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700773 }
774
Chong Zhangfdd512a2019-11-22 11:03:14 -0800775 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700776 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700777 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700778 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700779 bool success;
780 Status status = clients[i]->reclaimResource(&success);
781 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700782 failedClient = clients[i];
783 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700784 }
785 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700786
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700787 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700788 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700789 }
790
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200791 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700792 {
793 Mutex::Autolock lock(mLock);
794 bool found = false;
795 for (size_t i = 0; i < mMap.size(); ++i) {
796 ResourceInfos &infos = mMap.editValueAt(i);
797 for (size_t j = 0; j < infos.size();) {
798 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700799 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700800 found = true;
801 } else {
802 ++j;
803 }
804 }
805 if (found) {
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200806 failedClientPid = mMap.keyAt(i);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700807 break;
808 }
809 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200810 if (found) {
811 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
812 } else {
813 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700814 }
815 }
816
Wonsik Kim271429d2020-10-01 10:12:56 -0700817 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700818}
819
Brian Lindahl64ee9452022-01-14 13:31:16 +0100820Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800821 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
822 originalPid, newPid);
823 mServiceLog->add(log);
824
825 // allow if this is called from the same process or the process has
826 // permission.
827 if ((AIBinder_getCallingPid() != getpid()) &&
828 (checkCallingPermission(String16(
829 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
830 ALOGE(
831 "Permission Denial: can't access overridePid method from pid=%d, "
832 "self pid=%d\n",
833 AIBinder_getCallingPid(), getpid());
834 return Status::fromServiceSpecificError(PERMISSION_DENIED);
835 }
836
837 {
838 Mutex::Autolock lock(mLock);
839 mOverridePidMap.erase(originalPid);
840 if (newPid != -1) {
841 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000842 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800843 }
844 }
845
846 return Status::ok();
847}
848
Chong Zhang97d367b2020-09-16 12:53:14 -0700849Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100850 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700851 int oomScore) {
852 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
853 pid, procState, oomScore);
854 mServiceLog->add(log);
855
856 // Only allow the override if the caller already can access process state and oom scores.
857 int callingPid = AIBinder_getCallingPid();
858 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
859 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
860 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
861 return Status::fromServiceSpecificError(PERMISSION_DENIED);
862 }
863
864 if (client == nullptr) {
865 return Status::fromServiceSpecificError(BAD_VALUE);
866 }
867
868 Mutex::Autolock lock(mLock);
869 removeProcessInfoOverride_l(pid);
870
871 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
872 // Override value is rejected by ProcessInfo.
873 return Status::fromServiceSpecificError(BAD_VALUE);
874 }
875
Girish1f002cf2023-02-17 00:36:29 +0000876 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
877 .uid = 0,
878 .id = 0,
879 .name = "<unknown client>"};
Arun Johnsond1f59732022-03-25 17:10:29 +0000880 uintptr_t cookie = addCookieAndLink_l(client,
Girish1f002cf2023-02-17 00:36:29 +0000881 new OverrideProcessInfoDeathNotifier(ref<ResourceManagerService>(), clientInfo));
Chong Zhang97d367b2020-09-16 12:53:14 -0700882
883 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{cookie, client});
884
885 return Status::ok();
886}
887
Arun Johnsond1f59732022-03-25 17:10:29 +0000888uintptr_t ResourceManagerService::addCookieAndLink_l(
889 const std::shared_ptr<IResourceManagerClient>& client, const sp<DeathNotifier>& notifier) {
890 if (client == nullptr) {
891 return 0;
892 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700893 std::scoped_lock lock{sCookieLock};
894
895 uintptr_t cookie;
896 // Need to skip cookie 0 (if it wraps around). ResourceInfo has cookie initialized to 0
897 // indicating the death notifier is not created yet.
898 while ((cookie = ++sCookieCounter) == 0);
Arun Johnsond1f59732022-03-25 17:10:29 +0000899 AIBinder_linkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700900 sCookieToDeathNotifierMap.emplace(cookie, notifier);
901
902 return cookie;
903}
904
Arun Johnsond1f59732022-03-25 17:10:29 +0000905void ResourceManagerService::removeCookieAndUnlink_l(
906 const std::shared_ptr<IResourceManagerClient>& client, uintptr_t cookie) {
Chong Zhang97d367b2020-09-16 12:53:14 -0700907 std::scoped_lock lock{sCookieLock};
Arun Johnsond1f59732022-03-25 17:10:29 +0000908 if (client != nullptr) {
909 AIBinder_unlinkToDeath(client->asBinder().get(), mDeathRecipient.get(), (void*)cookie);
910 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700911 sCookieToDeathNotifierMap.erase(cookie);
912}
913
914void ResourceManagerService::removeProcessInfoOverride(int pid) {
915 Mutex::Autolock lock(mLock);
916
917 removeProcessInfoOverride_l(pid);
918}
919
920void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
921 auto it = mProcessInfoOverrideMap.find(pid);
922 if (it == mProcessInfoOverrideMap.end()) {
923 return;
924 }
925
926 mProcessInfo->removeProcessInfoOverride(pid);
927
Arun Johnsond1f59732022-03-25 17:10:29 +0000928 removeCookieAndUnlink_l(it->second.client, it->second.cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700929
930 mProcessInfoOverrideMap.erase(pid);
931}
932
Girish9128e242022-11-23 20:52:29 +0000933Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
934 int32_t pid = clientInfo.pid;
935 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700936 String8 log = String8::format(
937 "markClientForPendingRemoval(pid %d, clientId %lld)",
938 pid, (long long) clientId);
939 mServiceLog->add(log);
940
941 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100942 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800943 pid_t callingPid = IPCThreadState::self()->getCallingPid();
944 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
945 pid, callingPid);
946 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700947 }
948 ssize_t index = mMap.indexOfKey(pid);
949 if (index < 0) {
950 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
951 pid, (long long)clientId);
952 return Status::ok();
953 }
954 ResourceInfos &infos = mMap.editValueAt(index);
955
956 index = infos.indexOfKey(clientId);
957 if (index < 0) {
958 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
959 return Status::ok();
960 }
961
962 ResourceInfo &info = infos.editValueAt(index);
963 info.pendingRemoval = true;
964 return Status::ok();
965}
966
Wonsik Kim271429d2020-10-01 10:12:56 -0700967Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
968 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
969 mServiceLog->add(log);
970
971 Vector<std::shared_ptr<IResourceManagerClient>> clients;
972 {
973 Mutex::Autolock lock(mLock);
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100974 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800975 pid_t callingPid = IPCThreadState::self()->getCallingPid();
976 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
977 pid, callingPid);
978 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700979 }
980
981 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
982 MediaResource::Type::kNonSecureCodec,
983 MediaResource::Type::kGraphicMemory,
984 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100985 switch (type) {
986 // Codec resources are segregated by audio, video and image domains.
987 case MediaResource::Type::kSecureCodec:
988 case MediaResource::Type::kNonSecureCodec:
989 for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec,
990 MediaResource::SubType::kVideoCodec,
991 MediaResource::SubType::kImageCodec}) {
992 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000993 uid_t uid = 0;
994 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100995 clients.add(client);
996 continue;
997 }
998 }
999 break;
1000 // Non-codec resources are shared by audio, video and image codecs (no subtype).
1001 default:
1002 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +00001003 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001004 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +00001005 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001006 clients.add(client);
1007 }
1008 break;
Wonsik Kim271429d2020-10-01 10:12:56 -07001009 }
1010 }
1011 }
1012
1013 if (!clients.empty()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001014 reclaimUnconditionallyFrom(clients);
Wonsik Kim271429d2020-10-01 10:12:56 -07001015 }
1016 return Status::ok();
1017}
1018
Henry Fang32762922020-01-28 18:40:39 -08001019bool ResourceManagerService::getPriority_l(int pid, int* priority) {
1020 int newPid = pid;
1021
1022 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
1023 newPid = mOverridePidMap[pid];
1024 ALOGD("getPriority_l: use override pid %d instead original pid %d",
1025 newPid, pid);
1026 }
1027
1028 return mProcessInfo->getPriority(newPid, priority);
1029}
1030
Brian Lindahl64ee9452022-01-14 13:31:16 +01001031bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001032 MediaResource::SubType subType,
1033 PidUidVector* idVector,
1034 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Chong Zhangfdd512a2019-11-22 11:03:14 -08001035 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Girish9128e242022-11-23 20:52:29 +00001036 PidUidVector tempIdList;
1037
Ronghua Wu231c3d12015-03-11 15:10:32 -07001038 for (size_t i = 0; i < mMap.size(); ++i) {
1039 ResourceInfos &infos = mMap.editValueAt(i);
1040 for (size_t j = 0; j < infos.size(); ++j) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001041 if (hasResourceType(type, subType, infos[j].resources)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
1043 // some higher/equal priority process owns the resource,
1044 // this request can't be fulfilled.
1045 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -08001046 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001047 return false;
1048 }
1049 temp.push_back(infos[j].client);
Girish9128e242022-11-23 20:52:29 +00001050 tempIdList.emplace_back(mMap.keyAt(i), infos[j].uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001051 }
1052 }
1053 }
1054 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -08001055 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001056 return true;
1057 }
1058 clients->appendVector(temp);
Girish9128e242022-11-23 20:52:29 +00001059 idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001060 return true;
1061}
1062
Brian Lindahl64ee9452022-01-14 13:31:16 +01001063bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid,
Girish9128e242022-11-23 20:52:29 +00001064 MediaResource::Type type,
1065 MediaResource::SubType subType,
1066 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -08001067 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001068 int lowestPriorityPid;
1069 int lowestPriority;
1070 int callingPriority;
Girish9128e242022-11-23 20:52:29 +00001071 uid_t uid = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001072
1073 // Before looking into other processes, check if we have clients marked for
1074 // pending removal in the same process.
Girish9128e242022-11-23 20:52:29 +00001075 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) {
1076 idVector->emplace_back(callingPid, uid);
Wonsik Kimd20e9362020-04-28 10:42:57 -07001077 return true;
1078 }
Henry Fang32762922020-01-28 18:40:39 -08001079 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001080 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
1081 callingPid);
1082 return false;
1083 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001084 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001085 return false;
1086 }
1087 if (lowestPriority <= callingPriority) {
1088 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1089 lowestPriority, callingPriority);
1090 return false;
1091 }
1092
Girish9128e242022-11-23 20:52:29 +00001093 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001094 return false;
1095 }
Girish9128e242022-11-23 20:52:29 +00001096
1097 idVector->emplace_back(lowestPriorityPid, uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001098 return true;
1099}
1100
Brian Lindahl64ee9452022-01-14 13:31:16 +01001101bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1102 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001103 int pid = -1;
1104 int priority = -1;
1105 for (size_t i = 0; i < mMap.size(); ++i) {
1106 if (mMap.valueAt(i).size() == 0) {
1107 // no client on this process.
1108 continue;
1109 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001110 if (!hasResourceType(type, subType, mMap.valueAt(i))) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001111 // doesn't have the requested resource type
1112 continue;
1113 }
1114 int tempPid = mMap.keyAt(i);
1115 int tempPriority;
Henry Fang32762922020-01-28 18:40:39 -08001116 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001117 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1118 // TODO: remove this pid from mMap?
1119 continue;
1120 }
1121 if (pid == -1 || tempPriority > priority) {
1122 // initial the value
1123 pid = tempPid;
1124 priority = tempPriority;
1125 }
1126 }
1127 if (pid != -1) {
1128 *lowestPriorityPid = pid;
1129 *lowestPriority = priority;
1130 }
1131 return (pid != -1);
1132}
1133
1134bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1135 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001136 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001137 return false;
1138 }
1139
1140 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001141 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001142 return false;
1143 }
1144
1145 return (callingPidPriority < priority);
1146}
1147
Brian Lindahl64ee9452022-01-14 13:31:16 +01001148bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001149 MediaResource::SubType subType, uid_t& uid,
1150 std::shared_ptr<IResourceManagerClient> *client) {
1151 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001152}
1153
1154bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001155 MediaResource::SubType subType, uid_t& uid,
1156 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001157 bool pendingRemovalOnly) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001158 ssize_t index = mMap.indexOfKey(pid);
1159 if (index < 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001160 ALOGE_IF(!pendingRemovalOnly,
1161 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001162 return false;
1163 }
1164
Chong Zhangfdd512a2019-11-22 11:03:14 -08001165 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001166 uint64_t largestValue = 0;
1167 const ResourceInfos &infos = mMap.valueAt(index);
1168 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -07001169 const ResourceList &resources = infos[i].resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001170 if (pendingRemovalOnly && !infos[i].pendingRemoval) {
1171 continue;
1172 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001173 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001174 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001175 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001176 if (resource.value > largestValue) {
1177 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001178 clientTemp = infos[i].client;
Girish9128e242022-11-23 20:52:29 +00001179 uid = infos[i].uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001180 }
1181 }
1182 }
1183 }
1184
1185 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001186 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001187 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1188 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001189 return false;
1190 }
1191
1192 *client = clientTemp;
1193 return true;
1194}
1195
Girish1f002cf2023-02-17 00:36:29 +00001196Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1197 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1198 return Status::ok();
1199}
1200
1201Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1202 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1203 return Status::ok();
1204}
1205
1206Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1207 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1208 return Status::ok();
1209}
1210
1211long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1212 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1213}
1214
1215long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1216 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1217}
1218
Ronghua Wu231c3d12015-03-11 15:10:32 -07001219} // namespace android