blob: 6c02b2364ef9737673eed93d849422a723ae3862 [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"
Girish27365ed2023-10-11 20:20:55 +000042#include "ResourceManagerServiceUtils.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070043#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070044#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070045
Ronghua Wu231c3d12015-03-11 15:10:32 -070046namespace android {
47
Girishf1d166c2023-07-20 22:35:29 +000048class DeathNotifier : public std::enable_shared_from_this<DeathNotifier> {
Chong Zhang97d367b2020-09-16 12:53:14 -070049
Girishf1d166c2023-07-20 22:35:29 +000050 // BinderDiedContext defines the cookie that is passed as DeathRecipient.
51 // Since this can maintain more context than a raw pointer, we can
52 // validate the scope of DeathNotifier, before deferencing it upon the binder death.
53 struct BinderDiedContext {
54 std::weak_ptr<DeathNotifier> mDeathNotifier;
55 };
Chong Zhang97d367b2020-09-16 12:53:14 -070056public:
Girishf1d166c2023-07-20 22:35:29 +000057 DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
58 const std::shared_ptr<ResourceManagerService>& service,
59 const ClientInfoParcel& clientInfo,
60 AIBinder_DeathRecipient* recipient);
Chong Zhang97d367b2020-09-16 12:53:14 -070061
Girishf1d166c2023-07-20 22:35:29 +000062 virtual ~DeathNotifier() {
63 unlink();
64 }
65
66 void unlink() {
67 if (mClient != nullptr) {
68 // Register for the callbacks by linking to death notification.
69 AIBinder_unlinkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
70 mClient = nullptr;
71 }
72 }
Chong Zhang97d367b2020-09-16 12:53:14 -070073
74 // Implement death recipient
75 static void BinderDiedCallback(void* cookie);
Girishf1d166c2023-07-20 22:35:29 +000076 static void BinderUnlinkedCallback(void* cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -070077 virtual void binderDied();
78
Girishf1d166c2023-07-20 22:35:29 +000079private:
80 void link() {
81 // Create the context that is passed as cookie to the binder death notification.
82 // The context gets deleted at BinderUnlinkedCallback.
83 mCookie = new BinderDiedContext{.mDeathNotifier = weak_from_this()};
84 // Register for the callbacks by linking to death notification.
85 AIBinder_linkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
86 }
87
Chong Zhang97d367b2020-09-16 12:53:14 -070088protected:
Girishf1d166c2023-07-20 22:35:29 +000089 std::shared_ptr<IResourceManagerClient> mClient;
Chong Zhang97d367b2020-09-16 12:53:14 -070090 std::weak_ptr<ResourceManagerService> mService;
Girish1f002cf2023-02-17 00:36:29 +000091 const ClientInfoParcel mClientInfo;
Girishf1d166c2023-07-20 22:35:29 +000092 AIBinder_DeathRecipient* mRecipient;
93 BinderDiedContext* mCookie;
Chong Zhang97d367b2020-09-16 12:53:14 -070094};
95
Girishf1d166c2023-07-20 22:35:29 +000096DeathNotifier::DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
97 const std::shared_ptr<ResourceManagerService>& service,
98 const ClientInfoParcel& clientInfo,
99 AIBinder_DeathRecipient* recipient)
100 : mClient(client), mService(service), mClientInfo(clientInfo),
101 mRecipient(recipient), mCookie(nullptr) {
102 link();
103}
104
105//static
106void DeathNotifier::BinderUnlinkedCallback(void* cookie) {
107 BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie);
108 // Since we don't need the context anymore, we are deleting it now.
109 delete context;
110}
Wonsik Kim3e378962017-01-05 17:00:02 +0900111
Chong Zhangfdd512a2019-11-22 11:03:14 -0800112//static
113void DeathNotifier::BinderDiedCallback(void* cookie) {
Girishf1d166c2023-07-20 22:35:29 +0000114 BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie);
115
116 // Validate the context and check if the DeathNotifier object is still in scope.
117 if (context != nullptr) {
118 std::shared_ptr<DeathNotifier> thiz = context->mDeathNotifier.lock();
119 if (thiz != nullptr) {
120 thiz->binderDied();
121 } else {
122 ALOGI("DeathNotifier is out of scope already");
Chong Zhang97d367b2020-09-16 12:53:14 -0700123 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700124 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800125}
Wonsik Kim3e378962017-01-05 17:00:02 +0900126
Chong Zhangfdd512a2019-11-22 11:03:14 -0800127void DeathNotifier::binderDied() {
128 // Don't check for pid validity since we know it's already dead.
129 std::shared_ptr<ResourceManagerService> service = mService.lock();
130 if (service == nullptr) {
131 ALOGW("ResourceManagerService is dead as well.");
132 return;
Wonsik Kim3e378962017-01-05 17:00:02 +0900133 }
Henry Fang32762922020-01-28 18:40:39 -0800134
Girish1f002cf2023-02-17 00:36:29 +0000135 service->overridePid(mClientInfo.pid, -1);
Henry Fangb35141c2020-06-29 13:11:39 -0700136 // thiz is freed in the call below, so it must be last call referring thiz
Girish1f002cf2023-02-17 00:36:29 +0000137 service->removeResource(mClientInfo, false /*checkValid*/);
Chong Zhang97d367b2020-09-16 12:53:14 -0700138}
Henry Fangb35141c2020-06-29 13:11:39 -0700139
Chong Zhang97d367b2020-09-16 12:53:14 -0700140class OverrideProcessInfoDeathNotifier : public DeathNotifier {
141public:
Girishf1d166c2023-07-20 22:35:29 +0000142 OverrideProcessInfoDeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
143 const std::shared_ptr<ResourceManagerService>& service,
144 const ClientInfoParcel& clientInfo,
145 AIBinder_DeathRecipient* recipient)
146 : DeathNotifier(client, service, clientInfo, recipient) {}
Chong Zhang97d367b2020-09-16 12:53:14 -0700147
148 virtual ~OverrideProcessInfoDeathNotifier() {}
149
150 virtual void binderDied();
151};
152
153void OverrideProcessInfoDeathNotifier::binderDied() {
154 // Don't check for pid validity since we know it's already dead.
155 std::shared_ptr<ResourceManagerService> service = mService.lock();
156 if (service == nullptr) {
157 ALOGW("ResourceManagerService is dead as well.");
158 return;
159 }
160
Girish1f002cf2023-02-17 00:36:29 +0000161 service->removeProcessInfoOverride(mClientInfo.pid);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800162}
Wonsik Kim3e378962017-01-05 17:00:02 +0900163
Girish434b4d82023-07-11 23:24:54 +0000164static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900165 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700166 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900167 if (binder != NULL) {
168 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
169 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100170 switch (resources[i].subType) {
Girisha5a2d672023-09-20 18:40:20 +0000171 case MediaResource::SubType::kHwAudioCodec:
172 case MediaResource::SubType::kSwAudioCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +0100173 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
174 break;
Girisha5a2d672023-09-20 18:40:20 +0000175 case MediaResource::SubType::kHwVideoCodec:
176 case MediaResource::SubType::kSwVideoCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +0100177 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
178 break;
Girisha5a2d672023-09-20 18:40:20 +0000179 case MediaResource::SubType::kHwImageCodec:
180 case MediaResource::SubType::kSwImageCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +0100181 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
182 break;
183 case MediaResource::SubType::kUnspecifiedSubType:
184 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700185 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900186 }
187 }
188}
189
Brian Lindahl64ee9452022-01-14 13:31:16 +0100190binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700191 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700192
dcashman014e91e2015-09-11 09:33:01 -0700193 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
194 result.format("Permission Denial: "
195 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800196 AIBinder_getCallingPid(),
197 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000198 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -0700199 return PERMISSION_DENIED;
200 }
201
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700202 PidResourceInfosMap mapCopy;
203 bool supportsMultipleSecureCodecs;
204 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800205 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700206 String8 serviceLog;
207 {
Girish434b4d82023-07-11 23:24:54 +0000208 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700209 mapCopy = mMap; // Shadow copy, real copy will happen on write.
210 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
211 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
212 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800213 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700214 }
215
216 const size_t SIZE = 256;
217 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700218 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
219 result.append(buffer);
220 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700221 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700222 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700223 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
224 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700225 result.append(buffer);
226
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700227 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000228 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000229 snprintf(buffer, SIZE, " Pid: %d\n", pid);
230 result.append(buffer);
231 int priority = 0;
232 if (getPriority_l(pid, &priority)) {
233 snprintf(buffer, SIZE, " Priority: %d\n", priority);
234 } else {
235 snprintf(buffer, SIZE, " Priority: <unknown>\n");
236 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700237 result.append(buffer);
238
Girish434b4d82023-07-11 23:24:54 +0000239 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700240 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000241 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700242 result.append(buffer);
243
Girish434b4d82023-07-11 23:24:54 +0000244 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700245 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700246 result.append(buffer);
247
Girish434b4d82023-07-11 23:24:54 +0000248 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700249 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700250 for (auto it = resources.begin(); it != resources.end(); it++) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000251 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700252 result.append(buffer);
253 }
254 }
255 }
Henry Fang32762922020-01-28 18:40:39 -0800256 result.append(" Process Pid override:\n");
257 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
258 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
259 it->first, it->second);
260 result.append(buffer);
261 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700262 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700263 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700264
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000265 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700266 return OK;
267}
268
Brian Lindahl64ee9452022-01-14 13:31:16 +0100269struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800270 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700271
Chong Zhangdd726802019-08-21 17:24:13 -0700272 virtual void noteStartVideo(int uid) override {
273 BatteryNotifier::getInstance().noteStartVideo(uid);
274 }
275 virtual void noteStopVideo(int uid) override {
276 BatteryNotifier::getInstance().noteStopVideo(uid);
277 }
278 virtual void noteResetVideo() override {
279 BatteryNotifier::getInstance().noteResetVideo();
280 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800281 virtual bool requestCpusetBoost(bool enable) override {
282 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700283 }
284
285protected:
286 virtual ~SystemCallbackImpl() {}
287
288private:
289 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800290 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700291};
292
293ResourceManagerService::ResourceManagerService()
294 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
295
Brian Lindahl64ee9452022-01-14 13:31:16 +0100296ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700297 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700299 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700300 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700301 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700302 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800303 mCpuBoostCount(0),
Girishf1d166c2023-07-20 22:35:29 +0000304 mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient(
305 AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback))) {
Chong Zhangdd726802019-08-21 17:24:13 -0700306 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000307 // Create ResourceManagerMetrics that handles all the metrics.
308 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700309}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700310
Chong Zhangfdd512a2019-11-22 11:03:14 -0800311//static
312void ResourceManagerService::instantiate() {
313 std::shared_ptr<ResourceManagerService> service =
314 ::ndk::SharedRefBase::make<ResourceManagerService>();
315 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000316 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000317 service->asBinder().get(), getServiceName(),
318 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800319 if (status != STATUS_OK) {
320 return;
321 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700322
323 std::shared_ptr<ResourceObserverService> observerService =
324 ResourceObserverService::instantiate();
325
326 if (observerService != nullptr) {
327 service->setObserverService(observerService);
328 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800329 // TODO: mediaserver main() is already starting the thread pool,
330 // move this to mediaserver main() when other services in mediaserver
331 // are converted to ndk-platform aidl.
332 //ABinderProcess_startThreadPool();
333}
334
Ronghua Wu231c3d12015-03-11 15:10:32 -0700335ResourceManagerService::~ResourceManagerService() {}
336
Chong Zhanga9d45c72020-09-09 12:41:17 -0700337void ResourceManagerService::setObserverService(
338 const std::shared_ptr<ResourceObserverService>& observerService) {
339 mObserverService = observerService;
340}
341
Chong Zhang181e6952019-10-09 13:23:39 -0700342Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000343 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700344 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700345
Girish434b4d82023-07-11 23:24:54 +0000346 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700347 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700348 const std::string &type = policies[i].type;
349 const std::string &value = policies[i].value;
350 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700351 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700352 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700353 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700354 }
355 }
Chong Zhang181e6952019-10-09 13:23:39 -0700356 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700357}
358
Brian Lindahl64ee9452022-01-14 13:31:16 +0100359void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource,
360 const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700361 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700362 if (resource.type == MediaResource::Type::kCpuBoost
363 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700364 // Request it on every new instance of kCpuBoost, as the media.codec
365 // could have died, if we only do it the first time subsequent instances
366 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800367 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700368 ALOGW("couldn't request cpuset boost");
369 }
370 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700371 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000372 && (resource.subType == MediaResource::SubType::kHwVideoCodec
373 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700374 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700375 }
376}
377
Brian Lindahl64ee9452022-01-14 13:31:16 +0100378void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource,
379 const ResourceInfo& clientInfo) {
Chong Zhang181e6952019-10-09 13:23:39 -0700380 if (resource.type == MediaResource::Type::kCpuBoost
381 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700382 && mCpuBoostCount > 0) {
383 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800384 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700385 }
Chong Zhang181e6952019-10-09 13:23:39 -0700386 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000387 && (resource.subType == MediaResource::SubType::kHwVideoCodec
388 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700389 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700390 }
391}
392
Brian Lindahl64ee9452022-01-14 13:31:16 +0100393void ResourceManagerService::mergeResources(MediaResourceParcel& r1,
394 const MediaResourceParcel& r2) {
Chong Zhang181e6952019-10-09 13:23:39 -0700395 // The resource entry on record is maintained to be in [0,INT64_MAX].
396 // Clamp if merging in the new resource value causes it to go out of bound.
397 // Note that the new resource value could be negative, eg.DrmSession, the
398 // value goes lower when the session is used more often. During reclaim
399 // the session with the highest value (lowest usage) would be closed.
400 if (r2.value < INT64_MAX - r1.value) {
401 r1.value += r2.value;
402 if (r1.value < 0) {
403 r1.value = 0;
404 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700405 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700406 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700407 }
408}
409
Girish9128e242022-11-23 20:52:29 +0000410Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800411 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700412 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000413 int32_t pid = clientInfo.pid;
414 int32_t uid = clientInfo.uid;
415 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000416 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000417 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700418 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700419
Girish434b4d82023-07-11 23:24:54 +0000420 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100421 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800422 pid_t callingPid = IPCThreadState::self()->getCallingPid();
423 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100424 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
425 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800426 pid = callingPid;
427 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800428 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700429 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000430 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700431 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700432
433 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700434 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700435 const auto resType = std::tuple(res.type, res.subType, res.id);
436
437 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
438 ALOGW("Ignoring request to remove negative value of non-drm resource");
439 continue;
440 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700441 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700442 if (res.value <= 0) {
443 // We can't init a new entry with negative value, although it's allowed
444 // to merge in negative values after the initial add.
445 ALOGW("Ignoring request to add new resource entry with value <= 0");
446 continue;
447 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700448 onFirstAdded(res, info);
449 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700450 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700451 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700452 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700453 // Add it to the list of added resources for observers.
454 auto it = resourceAdded.find(resType);
455 if (it == resourceAdded.end()) {
456 resourceAdded[resType] = res;
457 } else {
458 mergeResources(it->second, res);
459 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700460 }
Girishf1d166c2023-07-20 22:35:29 +0000461 if (info.deathNotifier == nullptr && client != nullptr) {
462 info.deathNotifier = std::make_shared<DeathNotifier>(
463 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900464 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700465 if (mObserverService != nullptr && !resourceAdded.empty()) {
466 mObserverService->onResourceAdded(uid, pid, resourceAdded);
467 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900468 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000469
Chong Zhang181e6952019-10-09 13:23:39 -0700470 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700471}
472
Girish9128e242022-11-23 20:52:29 +0000473Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700474 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000475 int32_t pid = clientInfo.pid;
476 int32_t uid = clientInfo.uid;
477 int64_t clientId = clientInfo.id;
478 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000479 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700480 mServiceLog->add(log);
481
Girish434b4d82023-07-11 23:24:54 +0000482 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100483 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800484 pid_t callingPid = IPCThreadState::self()->getCallingPid();
485 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
486 pid, callingPid);
487 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700488 }
Girish434b4d82023-07-11 23:24:54 +0000489 PidResourceInfosMap::iterator found = mMap.find(pid);
490 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700491 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700492 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700493 }
Girish434b4d82023-07-11 23:24:54 +0000494 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700495
Girish434b4d82023-07-11 23:24:54 +0000496 ResourceInfos::iterator foundClient = infos.find(clientId);
497 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700498 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700499 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700500 }
501
Girish434b4d82023-07-11 23:24:54 +0000502 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700503 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700504 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700505 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700506 const auto resType = std::tuple(res.type, res.subType, res.id);
507
508 if (res.value < 0) {
509 ALOGW("Ignoring request to remove negative value of resource");
510 continue;
511 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700512 // ignore if we don't have it
513 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700514 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700515 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700516 if (resource.value > res.value) {
517 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700518 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700519 onLastRemoved(res, info);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700520 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800521 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700522 }
523
524 // Add it to the list of removed resources for observers.
525 auto it = resourceRemoved.find(resType);
526 if (it == resourceRemoved.end()) {
527 resourceRemoved[resType] = actualRemoved;
528 } else {
529 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700530 }
531 }
532 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700533 if (mObserverService != nullptr && !resourceRemoved.empty()) {
534 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
535 }
Chong Zhang181e6952019-10-09 13:23:39 -0700536 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700537}
538
Girish9128e242022-11-23 20:52:29 +0000539Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
540 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700541 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900542}
543
Girish9128e242022-11-23 20:52:29 +0000544Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
545 int32_t pid = clientInfo.pid;
546 int32_t uid = clientInfo.uid;
547 int64_t clientId = clientInfo.id;
548 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
549 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700550 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700551
Girish434b4d82023-07-11 23:24:54 +0000552 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100553 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800554 pid_t callingPid = IPCThreadState::self()->getCallingPid();
555 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
556 pid, callingPid);
557 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800558 }
Girish434b4d82023-07-11 23:24:54 +0000559 PidResourceInfosMap::iterator found = mMap.find(pid);
560 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700561 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700562 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700563 }
Girish434b4d82023-07-11 23:24:54 +0000564 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700565
Girish434b4d82023-07-11 23:24:54 +0000566 ResourceInfos::iterator foundClient = infos.find(clientId);
567 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700568 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700569 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700570 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700571
Girish434b4d82023-07-11 23:24:54 +0000572 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700573 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
574 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700575 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700576
Girish1f002cf2023-02-17 00:36:29 +0000577 // Since this client has been removed, update the metrics collector.
578 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000579
Chong Zhanga9d45c72020-09-09 12:41:17 -0700580 if (mObserverService != nullptr && !info.resources.empty()) {
581 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
582 }
583
Girish434b4d82023-07-11 23:24:54 +0000584 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700585 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700586}
587
Girish9128e242022-11-23 20:52:29 +0000588void ResourceManagerService::getClientForResource_l(int callingPid,
589 const MediaResourceParcel *res,
590 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000591 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700592 if (res == NULL) {
593 return;
594 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800595 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000596 if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700597 clients->push_back(client);
598 }
599}
600
Girish9128e242022-11-23 20:52:29 +0000601Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100602 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000603 int32_t callingPid = clientInfo.pid;
604 std::string clientName = clientInfo.name;
605 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000606 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700607 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700608 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700609
Girish434b4d82023-07-11 23:24:54 +0000610 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Girish9128e242022-11-23 20:52:29 +0000611 PidUidVector idVector;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700612 {
Girish434b4d82023-07-11 23:24:54 +0000613 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100614 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800615 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
616 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
617 callingPid, actualCallingPid);
618 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800619 }
Chong Zhang181e6952019-10-09 13:23:39 -0700620 const MediaResourceParcel *secureCodec = NULL;
621 const MediaResourceParcel *nonSecureCodec = NULL;
622 const MediaResourceParcel *graphicMemory = NULL;
623 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700624 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100625 switch (resources[i].type) {
626 case MediaResource::Type::kSecureCodec:
627 secureCodec = &resources[i];
628 break;
629 case MediaResource::Type::kNonSecureCodec:
630 nonSecureCodec = &resources[i];
631 break;
632 case MediaResource::Type::kGraphicMemory:
633 graphicMemory = &resources[i];
634 break;
635 case MediaResource::Type::kDrmSession:
636 drmSession = &resources[i];
637 break;
638 default:
639 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700640 }
641 }
642
643 // first pass to handle secure/non-secure codec conflict
644 if (secureCodec != NULL) {
645 if (!mSupportsMultipleSecureCodecs) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100646 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000647 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700648 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700649 }
650 }
651 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100652 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000653 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700654 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700655 }
656 }
657 }
658 if (nonSecureCodec != NULL) {
659 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100660 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000661 nonSecureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700662 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700663 }
664 }
665 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700666 if (drmSession != NULL) {
Girish9128e242022-11-23 20:52:29 +0000667 getClientForResource_l(callingPid, drmSession, &idVector, &clients);
Robert Shihc3af31b2019-09-20 21:45:01 -0700668 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700669 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700670 }
671 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700672
673 if (clients.size() == 0) {
674 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish9128e242022-11-23 20:52:29 +0000675 getClientForResource_l(callingPid, graphicMemory, &idVector, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700676 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700677
678 if (clients.size() == 0) {
679 // if we are here, run the third pass to free one codec with the same type.
Girish9128e242022-11-23 20:52:29 +0000680 getClientForResource_l(callingPid, secureCodec, &idVector, &clients);
681 getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700682 }
683
684 if (clients.size() == 0) {
685 // if we are here, run the fourth pass to free one codec with the different type.
686 if (secureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700687 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000688 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700689 }
690 if (nonSecureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700691 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000692 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700693 }
694 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700695 }
696
Brian Lindahl64ee9452022-01-14 13:31:16 +0100697 *_aidl_return = reclaimUnconditionallyFrom(clients);
Girish9128e242022-11-23 20:52:29 +0000698
699 // Log Reclaim Pushed Atom to statsd
700 pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return);
701
Wonsik Kim271429d2020-10-01 10:12:56 -0700702 return Status::ok();
703}
704
Girish9128e242022-11-23 20:52:29 +0000705void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish434b4d82023-07-11 23:24:54 +0000706 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish9128e242022-11-23 20:52:29 +0000707 const PidUidVector& idVector, bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000708 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000709 int requesterPriority = -1;
710 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000711 std::vector<int> priorities;
712 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000713
Girish1f002cf2023-02-17 00:36:29 +0000714 for (PidUidVector::const_reference id : idVector) {
Girish9128e242022-11-23 20:52:29 +0000715 int targetPriority = -1;
716 getPriority_l(id.first, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000717 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000718 }
Girish1f002cf2023-02-17 00:36:29 +0000719 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients,
720 idVector, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000721}
722
Brian Lindahl64ee9452022-01-14 13:31:16 +0100723bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish434b4d82023-07-11 23:24:54 +0000724 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700725 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700726 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700727 }
728
Chong Zhangfdd512a2019-11-22 11:03:14 -0800729 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700730 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700731 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700732 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700733 bool success;
734 Status status = clients[i]->reclaimResource(&success);
735 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700736 failedClient = clients[i];
737 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700738 }
739 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700740
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700741 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700742 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700743 }
744
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200745 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700746 {
Girish434b4d82023-07-11 23:24:54 +0000747 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700748 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000749 for (auto& [pid, infos] : mMap) {
750 for (const auto& [id, info] : infos) {
751 if (info.client == failedClient) {
752 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700753 found = true;
Girish434b4d82023-07-11 23:24:54 +0000754 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700755 }
756 }
757 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000758 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700759 break;
760 }
761 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200762 if (found) {
763 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
764 } else {
765 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700766 }
767 }
768
Wonsik Kim271429d2020-10-01 10:12:56 -0700769 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700770}
771
Brian Lindahl64ee9452022-01-14 13:31:16 +0100772Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800773 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
774 originalPid, newPid);
775 mServiceLog->add(log);
776
777 // allow if this is called from the same process or the process has
778 // permission.
779 if ((AIBinder_getCallingPid() != getpid()) &&
780 (checkCallingPermission(String16(
781 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
782 ALOGE(
783 "Permission Denial: can't access overridePid method from pid=%d, "
784 "self pid=%d\n",
785 AIBinder_getCallingPid(), getpid());
786 return Status::fromServiceSpecificError(PERMISSION_DENIED);
787 }
788
789 {
Girish434b4d82023-07-11 23:24:54 +0000790 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800791 mOverridePidMap.erase(originalPid);
792 if (newPid != -1) {
793 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000794 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800795 }
796 }
797
798 return Status::ok();
799}
800
Chong Zhang97d367b2020-09-16 12:53:14 -0700801Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100802 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700803 int oomScore) {
804 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
805 pid, procState, oomScore);
806 mServiceLog->add(log);
807
808 // Only allow the override if the caller already can access process state and oom scores.
809 int callingPid = AIBinder_getCallingPid();
810 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
811 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
812 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
813 return Status::fromServiceSpecificError(PERMISSION_DENIED);
814 }
815
816 if (client == nullptr) {
817 return Status::fromServiceSpecificError(BAD_VALUE);
818 }
819
Girish434b4d82023-07-11 23:24:54 +0000820 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700821 removeProcessInfoOverride_l(pid);
822
823 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
824 // Override value is rejected by ProcessInfo.
825 return Status::fromServiceSpecificError(BAD_VALUE);
826 }
827
Girish1f002cf2023-02-17 00:36:29 +0000828 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
829 .uid = 0,
830 .id = 0,
831 .name = "<unknown client>"};
Girishf1d166c2023-07-20 22:35:29 +0000832 auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
833 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Chong Zhang97d367b2020-09-16 12:53:14 -0700834
Girishf1d166c2023-07-20 22:35:29 +0000835 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700836
837 return Status::ok();
838}
839
Chong Zhang97d367b2020-09-16 12:53:14 -0700840void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000841 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700842
843 removeProcessInfoOverride_l(pid);
844}
845
846void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
847 auto it = mProcessInfoOverrideMap.find(pid);
848 if (it == mProcessInfoOverrideMap.end()) {
849 return;
850 }
851
852 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700853 mProcessInfoOverrideMap.erase(pid);
854}
855
Girish9128e242022-11-23 20:52:29 +0000856Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
857 int32_t pid = clientInfo.pid;
858 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700859 String8 log = String8::format(
860 "markClientForPendingRemoval(pid %d, clientId %lld)",
861 pid, (long long) clientId);
862 mServiceLog->add(log);
863
Girish434b4d82023-07-11 23:24:54 +0000864 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100865 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800866 pid_t callingPid = IPCThreadState::self()->getCallingPid();
867 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
868 pid, callingPid);
869 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700870 }
Girish434b4d82023-07-11 23:24:54 +0000871 PidResourceInfosMap::iterator found = mMap.find(pid);
872 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700873 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
874 pid, (long long)clientId);
875 return Status::ok();
876 }
Girish434b4d82023-07-11 23:24:54 +0000877 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700878
Girish434b4d82023-07-11 23:24:54 +0000879 ResourceInfos::iterator foundClient = infos.find(clientId);
880 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700881 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
882 return Status::ok();
883 }
884
Girish434b4d82023-07-11 23:24:54 +0000885 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700886 info.pendingRemoval = true;
887 return Status::ok();
888}
889
Wonsik Kim271429d2020-10-01 10:12:56 -0700890Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
891 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
892 mServiceLog->add(log);
893
Girish434b4d82023-07-11 23:24:54 +0000894 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700895 {
Girish434b4d82023-07-11 23:24:54 +0000896 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100897 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800898 pid_t callingPid = IPCThreadState::self()->getCallingPid();
899 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
900 pid, callingPid);
901 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700902 }
903
904 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
905 MediaResource::Type::kNonSecureCodec,
906 MediaResource::Type::kGraphicMemory,
907 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100908 switch (type) {
909 // Codec resources are segregated by audio, video and image domains.
910 case MediaResource::Type::kSecureCodec:
911 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000912 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
913 MediaResource::SubType::kSwAudioCodec,
914 MediaResource::SubType::kHwVideoCodec,
915 MediaResource::SubType::kSwVideoCodec,
916 MediaResource::SubType::kHwImageCodec,
917 MediaResource::SubType::kSwImageCodec}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100918 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000919 uid_t uid = 0;
920 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000921 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100922 continue;
923 }
924 }
925 break;
926 // Non-codec resources are shared by audio, video and image codecs (no subtype).
927 default:
928 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000929 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100930 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +0000931 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000932 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100933 }
934 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700935 }
936 }
937 }
938
939 if (!clients.empty()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100940 reclaimUnconditionallyFrom(clients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700941 }
942 return Status::ok();
943}
944
Henry Fang32762922020-01-28 18:40:39 -0800945bool ResourceManagerService::getPriority_l(int pid, int* priority) {
946 int newPid = pid;
947
948 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
949 newPid = mOverridePidMap[pid];
950 ALOGD("getPriority_l: use override pid %d instead original pid %d",
951 newPid, pid);
952 }
953
954 return mProcessInfo->getPriority(newPid, priority);
955}
956
Brian Lindahl64ee9452022-01-14 13:31:16 +0100957bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +0000958 MediaResource::SubType subType,
959 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000960 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
961 std::vector<std::shared_ptr<IResourceManagerClient>> temp;
Girish9128e242022-11-23 20:52:29 +0000962 PidUidVector tempIdList;
963
Girish434b4d82023-07-11 23:24:54 +0000964 for (auto& [pid, infos] : mMap) {
965 for (const auto& [id, info] : infos) {
966 if (hasResourceType(type, subType, info.resources)) {
967 if (!isCallingPriorityHigher_l(callingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700968 // some higher/equal priority process owns the resource,
969 // this request can't be fulfilled.
970 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Girish434b4d82023-07-11 23:24:54 +0000971 asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700972 return false;
973 }
Girish434b4d82023-07-11 23:24:54 +0000974 temp.push_back(info.client);
975 tempIdList.emplace_back(pid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700976 }
977 }
978 }
979 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800980 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700981 return true;
982 }
Girish434b4d82023-07-11 23:24:54 +0000983
984 clients->insert(std::end(*clients), std::begin(temp), std::end(temp));
Girish9128e242022-11-23 20:52:29 +0000985 idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700986 return true;
987}
988
Brian Lindahl64ee9452022-01-14 13:31:16 +0100989bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid,
Girish9128e242022-11-23 20:52:29 +0000990 MediaResource::Type type,
991 MediaResource::SubType subType,
992 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800993 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700994 int lowestPriorityPid;
995 int lowestPriority;
996 int callingPriority;
Girish9128e242022-11-23 20:52:29 +0000997 uid_t uid = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700998
999 // Before looking into other processes, check if we have clients marked for
1000 // pending removal in the same process.
Girish9128e242022-11-23 20:52:29 +00001001 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) {
1002 idVector->emplace_back(callingPid, uid);
Wonsik Kimd20e9362020-04-28 10:42:57 -07001003 return true;
1004 }
Henry Fang32762922020-01-28 18:40:39 -08001005 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001006 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
1007 callingPid);
1008 return false;
1009 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001010 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001011 return false;
1012 }
1013 if (lowestPriority <= callingPriority) {
1014 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1015 lowestPriority, callingPriority);
1016 return false;
1017 }
1018
Girish9128e242022-11-23 20:52:29 +00001019 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001020 return false;
1021 }
Girish9128e242022-11-23 20:52:29 +00001022
1023 idVector->emplace_back(lowestPriorityPid, uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001024 return true;
1025}
1026
Brian Lindahl64ee9452022-01-14 13:31:16 +01001027bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1028 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001029 int pid = -1;
1030 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001031 for (auto& [tempPid, infos] : mMap) {
1032 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001033 // no client on this process.
1034 continue;
1035 }
Girish434b4d82023-07-11 23:24:54 +00001036 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001037 // doesn't have the requested resource type
1038 continue;
1039 }
Girish434b4d82023-07-11 23:24:54 +00001040 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001041 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1043 // TODO: remove this pid from mMap?
1044 continue;
1045 }
1046 if (pid == -1 || tempPriority > priority) {
1047 // initial the value
1048 pid = tempPid;
1049 priority = tempPriority;
1050 }
1051 }
1052 if (pid != -1) {
1053 *lowestPriorityPid = pid;
1054 *lowestPriority = priority;
1055 }
1056 return (pid != -1);
1057}
1058
1059bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1060 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001061 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001062 return false;
1063 }
1064
1065 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001066 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001067 return false;
1068 }
1069
1070 return (callingPidPriority < priority);
1071}
1072
Brian Lindahl64ee9452022-01-14 13:31:16 +01001073bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001074 MediaResource::SubType subType, uid_t& uid,
1075 std::shared_ptr<IResourceManagerClient> *client) {
1076 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001077}
1078
1079bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001080 MediaResource::SubType subType, uid_t& uid,
1081 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001082 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001083 PidResourceInfosMap::iterator found = mMap.find(pid);
1084 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001085 ALOGE_IF(!pendingRemovalOnly,
1086 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001087 return false;
1088 }
1089
Chong Zhangfdd512a2019-11-22 11:03:14 -08001090 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001091 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001092 const ResourceInfos& infos = found->second;
1093 for (const auto& [id, info] : infos) {
1094 const ResourceList& resources = info.resources;
1095 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001096 continue;
1097 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001098 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001099 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001100 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001101 if (resource.value > largestValue) {
1102 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001103 clientTemp = info.client;
1104 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001105 }
1106 }
1107 }
1108 }
1109
1110 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001111 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001112 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1113 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001114 return false;
1115 }
1116
1117 *client = clientTemp;
1118 return true;
1119}
1120
Girish1f002cf2023-02-17 00:36:29 +00001121Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1122 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1123 return Status::ok();
1124}
1125
1126Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1127 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1128 return Status::ok();
1129}
1130
1131Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1132 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1133 return Status::ok();
1134}
1135
Girishde8eb592023-04-13 18:49:17 +00001136Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1137 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1138 return Status::ok();
1139}
1140
Girish1f002cf2023-02-17 00:36:29 +00001141long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1142 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1143}
1144
1145long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1146 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1147}
1148
Ronghua Wu231c3d12015-03-11 15:10:32 -07001149} // namespace android