blob: 9552e25a5f958be202d524c82cf19620ecddda47 [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) {
171 case MediaResource::SubType::kAudioCodec:
172 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
173 break;
174 case MediaResource::SubType::kVideoCodec:
175 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
176 break;
177 case MediaResource::SubType::kImageCodec:
178 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
179 break;
180 case MediaResource::SubType::kUnspecifiedSubType:
181 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700182 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900183 }
184 }
185}
186
Brian Lindahl64ee9452022-01-14 13:31:16 +0100187binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700188 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700189
dcashman014e91e2015-09-11 09:33:01 -0700190 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
191 result.format("Permission Denial: "
192 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800193 AIBinder_getCallingPid(),
194 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000195 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -0700196 return PERMISSION_DENIED;
197 }
198
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700199 PidResourceInfosMap mapCopy;
200 bool supportsMultipleSecureCodecs;
201 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800202 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700203 String8 serviceLog;
204 {
Girish434b4d82023-07-11 23:24:54 +0000205 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700206 mapCopy = mMap; // Shadow copy, real copy will happen on write.
207 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
208 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
209 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800210 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700211 }
212
213 const size_t SIZE = 256;
214 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700215 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
216 result.append(buffer);
217 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700218 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700219 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700220 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
221 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700222 result.append(buffer);
223
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700224 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000225 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000226 snprintf(buffer, SIZE, " Pid: %d\n", pid);
227 result.append(buffer);
228 int priority = 0;
229 if (getPriority_l(pid, &priority)) {
230 snprintf(buffer, SIZE, " Priority: %d\n", priority);
231 } else {
232 snprintf(buffer, SIZE, " Priority: <unknown>\n");
233 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700234 result.append(buffer);
235
Girish434b4d82023-07-11 23:24:54 +0000236 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700237 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000238 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700239 result.append(buffer);
240
Girish434b4d82023-07-11 23:24:54 +0000241 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700242 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700243 result.append(buffer);
244
Girish434b4d82023-07-11 23:24:54 +0000245 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700246 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700247 for (auto it = resources.begin(); it != resources.end(); it++) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000248 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700249 result.append(buffer);
250 }
251 }
252 }
Henry Fang32762922020-01-28 18:40:39 -0800253 result.append(" Process Pid override:\n");
254 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
255 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
256 it->first, it->second);
257 result.append(buffer);
258 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700259 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700260 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700261
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000262 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700263 return OK;
264}
265
Brian Lindahl64ee9452022-01-14 13:31:16 +0100266struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800267 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700268
Chong Zhangdd726802019-08-21 17:24:13 -0700269 virtual void noteStartVideo(int uid) override {
270 BatteryNotifier::getInstance().noteStartVideo(uid);
271 }
272 virtual void noteStopVideo(int uid) override {
273 BatteryNotifier::getInstance().noteStopVideo(uid);
274 }
275 virtual void noteResetVideo() override {
276 BatteryNotifier::getInstance().noteResetVideo();
277 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800278 virtual bool requestCpusetBoost(bool enable) override {
279 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700280 }
281
282protected:
283 virtual ~SystemCallbackImpl() {}
284
285private:
286 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800287 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700288};
289
290ResourceManagerService::ResourceManagerService()
291 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
292
Brian Lindahl64ee9452022-01-14 13:31:16 +0100293ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700294 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700295 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700296 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700297 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700299 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800300 mCpuBoostCount(0),
Girishf1d166c2023-07-20 22:35:29 +0000301 mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient(
302 AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback))) {
Chong Zhangdd726802019-08-21 17:24:13 -0700303 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000304 // Create ResourceManagerMetrics that handles all the metrics.
305 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700306}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700307
Chong Zhangfdd512a2019-11-22 11:03:14 -0800308//static
309void ResourceManagerService::instantiate() {
310 std::shared_ptr<ResourceManagerService> service =
311 ::ndk::SharedRefBase::make<ResourceManagerService>();
312 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000313 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000314 service->asBinder().get(), getServiceName(),
315 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800316 if (status != STATUS_OK) {
317 return;
318 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700319
320 std::shared_ptr<ResourceObserverService> observerService =
321 ResourceObserverService::instantiate();
322
323 if (observerService != nullptr) {
324 service->setObserverService(observerService);
325 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800326 // TODO: mediaserver main() is already starting the thread pool,
327 // move this to mediaserver main() when other services in mediaserver
328 // are converted to ndk-platform aidl.
329 //ABinderProcess_startThreadPool();
330}
331
Ronghua Wu231c3d12015-03-11 15:10:32 -0700332ResourceManagerService::~ResourceManagerService() {}
333
Chong Zhanga9d45c72020-09-09 12:41:17 -0700334void ResourceManagerService::setObserverService(
335 const std::shared_ptr<ResourceObserverService>& observerService) {
336 mObserverService = observerService;
337}
338
Chong Zhang181e6952019-10-09 13:23:39 -0700339Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000340 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700341 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700342
Girish434b4d82023-07-11 23:24:54 +0000343 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700344 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700345 const std::string &type = policies[i].type;
346 const std::string &value = policies[i].value;
347 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700348 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700349 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700350 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700351 }
352 }
Chong Zhang181e6952019-10-09 13:23:39 -0700353 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700354}
355
Brian Lindahl64ee9452022-01-14 13:31:16 +0100356void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource,
357 const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700358 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700359 if (resource.type == MediaResource::Type::kCpuBoost
360 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700361 // Request it on every new instance of kCpuBoost, as the media.codec
362 // could have died, if we only do it the first time subsequent instances
363 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800364 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700365 ALOGW("couldn't request cpuset boost");
366 }
367 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700368 } else if (resource.type == MediaResource::Type::kBattery
369 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700370 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700371 }
372}
373
Brian Lindahl64ee9452022-01-14 13:31:16 +0100374void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource,
375 const ResourceInfo& clientInfo) {
Chong Zhang181e6952019-10-09 13:23:39 -0700376 if (resource.type == MediaResource::Type::kCpuBoost
377 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700378 && mCpuBoostCount > 0) {
379 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800380 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700381 }
Chong Zhang181e6952019-10-09 13:23:39 -0700382 } else if (resource.type == MediaResource::Type::kBattery
383 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700384 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700385 }
386}
387
Brian Lindahl64ee9452022-01-14 13:31:16 +0100388void ResourceManagerService::mergeResources(MediaResourceParcel& r1,
389 const MediaResourceParcel& r2) {
Chong Zhang181e6952019-10-09 13:23:39 -0700390 // The resource entry on record is maintained to be in [0,INT64_MAX].
391 // Clamp if merging in the new resource value causes it to go out of bound.
392 // Note that the new resource value could be negative, eg.DrmSession, the
393 // value goes lower when the session is used more often. During reclaim
394 // the session with the highest value (lowest usage) would be closed.
395 if (r2.value < INT64_MAX - r1.value) {
396 r1.value += r2.value;
397 if (r1.value < 0) {
398 r1.value = 0;
399 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700400 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700401 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700402 }
403}
404
Girish9128e242022-11-23 20:52:29 +0000405Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800406 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700407 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000408 int32_t pid = clientInfo.pid;
409 int32_t uid = clientInfo.uid;
410 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000411 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000412 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700413 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700414
Girish434b4d82023-07-11 23:24:54 +0000415 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100416 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800417 pid_t callingPid = IPCThreadState::self()->getCallingPid();
418 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100419 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
420 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800421 pid = callingPid;
422 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800423 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700424 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000425 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700426 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700427
428 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700429 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700430 const auto resType = std::tuple(res.type, res.subType, res.id);
431
432 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
433 ALOGW("Ignoring request to remove negative value of non-drm resource");
434 continue;
435 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700436 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700437 if (res.value <= 0) {
438 // We can't init a new entry with negative value, although it's allowed
439 // to merge in negative values after the initial add.
440 ALOGW("Ignoring request to add new resource entry with value <= 0");
441 continue;
442 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700443 onFirstAdded(res, info);
444 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700445 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700446 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700447 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700448 // Add it to the list of added resources for observers.
449 auto it = resourceAdded.find(resType);
450 if (it == resourceAdded.end()) {
451 resourceAdded[resType] = res;
452 } else {
453 mergeResources(it->second, res);
454 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700455 }
Girishf1d166c2023-07-20 22:35:29 +0000456 if (info.deathNotifier == nullptr && client != nullptr) {
457 info.deathNotifier = std::make_shared<DeathNotifier>(
458 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900459 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700460 if (mObserverService != nullptr && !resourceAdded.empty()) {
461 mObserverService->onResourceAdded(uid, pid, resourceAdded);
462 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900463 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000464
Chong Zhang181e6952019-10-09 13:23:39 -0700465 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700466}
467
Girish9128e242022-11-23 20:52:29 +0000468Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700469 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000470 int32_t pid = clientInfo.pid;
471 int32_t uid = clientInfo.uid;
472 int64_t clientId = clientInfo.id;
473 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000474 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700475 mServiceLog->add(log);
476
Girish434b4d82023-07-11 23:24:54 +0000477 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100478 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800479 pid_t callingPid = IPCThreadState::self()->getCallingPid();
480 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
481 pid, callingPid);
482 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700483 }
Girish434b4d82023-07-11 23:24:54 +0000484 PidResourceInfosMap::iterator found = mMap.find(pid);
485 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700486 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700487 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700488 }
Girish434b4d82023-07-11 23:24:54 +0000489 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700490
Girish434b4d82023-07-11 23:24:54 +0000491 ResourceInfos::iterator foundClient = infos.find(clientId);
492 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700493 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700494 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700495 }
496
Girish434b4d82023-07-11 23:24:54 +0000497 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700498 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700499 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700500 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700501 const auto resType = std::tuple(res.type, res.subType, res.id);
502
503 if (res.value < 0) {
504 ALOGW("Ignoring request to remove negative value of resource");
505 continue;
506 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700507 // ignore if we don't have it
508 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700509 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700510 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700511 if (resource.value > res.value) {
512 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700513 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700514 onLastRemoved(res, info);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700515 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800516 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700517 }
518
519 // Add it to the list of removed resources for observers.
520 auto it = resourceRemoved.find(resType);
521 if (it == resourceRemoved.end()) {
522 resourceRemoved[resType] = actualRemoved;
523 } else {
524 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700525 }
526 }
527 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700528 if (mObserverService != nullptr && !resourceRemoved.empty()) {
529 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
530 }
Chong Zhang181e6952019-10-09 13:23:39 -0700531 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700532}
533
Girish9128e242022-11-23 20:52:29 +0000534Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
535 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700536 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900537}
538
Girish9128e242022-11-23 20:52:29 +0000539Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
540 int32_t pid = clientInfo.pid;
541 int32_t uid = clientInfo.uid;
542 int64_t clientId = clientInfo.id;
543 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
544 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700545 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700546
Girish434b4d82023-07-11 23:24:54 +0000547 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100548 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800549 pid_t callingPid = IPCThreadState::self()->getCallingPid();
550 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
551 pid, callingPid);
552 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800553 }
Girish434b4d82023-07-11 23:24:54 +0000554 PidResourceInfosMap::iterator found = mMap.find(pid);
555 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700556 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700557 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700558 }
Girish434b4d82023-07-11 23:24:54 +0000559 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700560
Girish434b4d82023-07-11 23:24:54 +0000561 ResourceInfos::iterator foundClient = infos.find(clientId);
562 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700563 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700564 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700565 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700566
Girish434b4d82023-07-11 23:24:54 +0000567 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700568 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
569 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700570 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700571
Girish1f002cf2023-02-17 00:36:29 +0000572 // Since this client has been removed, update the metrics collector.
573 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000574
Chong Zhanga9d45c72020-09-09 12:41:17 -0700575 if (mObserverService != nullptr && !info.resources.empty()) {
576 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
577 }
578
Girish434b4d82023-07-11 23:24:54 +0000579 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700580 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700581}
582
Girish9128e242022-11-23 20:52:29 +0000583void ResourceManagerService::getClientForResource_l(int callingPid,
584 const MediaResourceParcel *res,
585 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000586 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700587 if (res == NULL) {
588 return;
589 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800590 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000591 if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700592 clients->push_back(client);
593 }
594}
595
Girish9128e242022-11-23 20:52:29 +0000596Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100597 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000598 int32_t callingPid = clientInfo.pid;
599 std::string clientName = clientInfo.name;
600 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000601 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700602 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700603 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700604
Girish434b4d82023-07-11 23:24:54 +0000605 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Girish9128e242022-11-23 20:52:29 +0000606 PidUidVector idVector;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700607 {
Girish434b4d82023-07-11 23:24:54 +0000608 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100609 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800610 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
611 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
612 callingPid, actualCallingPid);
613 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800614 }
Chong Zhang181e6952019-10-09 13:23:39 -0700615 const MediaResourceParcel *secureCodec = NULL;
616 const MediaResourceParcel *nonSecureCodec = NULL;
617 const MediaResourceParcel *graphicMemory = NULL;
618 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700619 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100620 switch (resources[i].type) {
621 case MediaResource::Type::kSecureCodec:
622 secureCodec = &resources[i];
623 break;
624 case MediaResource::Type::kNonSecureCodec:
625 nonSecureCodec = &resources[i];
626 break;
627 case MediaResource::Type::kGraphicMemory:
628 graphicMemory = &resources[i];
629 break;
630 case MediaResource::Type::kDrmSession:
631 drmSession = &resources[i];
632 break;
633 default:
634 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700635 }
636 }
637
638 // first pass to handle secure/non-secure codec conflict
639 if (secureCodec != NULL) {
640 if (!mSupportsMultipleSecureCodecs) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100641 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000642 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700643 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700644 }
645 }
646 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100647 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000648 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700649 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700650 }
651 }
652 }
653 if (nonSecureCodec != NULL) {
654 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100655 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000656 nonSecureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700657 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700658 }
659 }
660 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700661 if (drmSession != NULL) {
Girish9128e242022-11-23 20:52:29 +0000662 getClientForResource_l(callingPid, drmSession, &idVector, &clients);
Robert Shihc3af31b2019-09-20 21:45:01 -0700663 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700664 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700665 }
666 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700667
668 if (clients.size() == 0) {
669 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish9128e242022-11-23 20:52:29 +0000670 getClientForResource_l(callingPid, graphicMemory, &idVector, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700671 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700672
673 if (clients.size() == 0) {
674 // if we are here, run the third pass to free one codec with the same type.
Girish9128e242022-11-23 20:52:29 +0000675 getClientForResource_l(callingPid, secureCodec, &idVector, &clients);
676 getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700677 }
678
679 if (clients.size() == 0) {
680 // if we are here, run the fourth pass to free one codec with the different type.
681 if (secureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700682 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000683 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700684 }
685 if (nonSecureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700686 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000687 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700688 }
689 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700690 }
691
Brian Lindahl64ee9452022-01-14 13:31:16 +0100692 *_aidl_return = reclaimUnconditionallyFrom(clients);
Girish9128e242022-11-23 20:52:29 +0000693
694 // Log Reclaim Pushed Atom to statsd
695 pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return);
696
Wonsik Kim271429d2020-10-01 10:12:56 -0700697 return Status::ok();
698}
699
Girish9128e242022-11-23 20:52:29 +0000700void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish434b4d82023-07-11 23:24:54 +0000701 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish9128e242022-11-23 20:52:29 +0000702 const PidUidVector& idVector, bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000703 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000704 int requesterPriority = -1;
705 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000706 std::vector<int> priorities;
707 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000708
Girish1f002cf2023-02-17 00:36:29 +0000709 for (PidUidVector::const_reference id : idVector) {
Girish9128e242022-11-23 20:52:29 +0000710 int targetPriority = -1;
711 getPriority_l(id.first, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000712 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000713 }
Girish1f002cf2023-02-17 00:36:29 +0000714 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients,
715 idVector, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000716}
717
Brian Lindahl64ee9452022-01-14 13:31:16 +0100718bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish434b4d82023-07-11 23:24:54 +0000719 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700720 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700721 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700722 }
723
Chong Zhangfdd512a2019-11-22 11:03:14 -0800724 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700725 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700726 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700727 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700728 bool success;
729 Status status = clients[i]->reclaimResource(&success);
730 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700731 failedClient = clients[i];
732 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700733 }
734 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700735
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700736 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700737 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700738 }
739
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200740 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700741 {
Girish434b4d82023-07-11 23:24:54 +0000742 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700743 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000744 for (auto& [pid, infos] : mMap) {
745 for (const auto& [id, info] : infos) {
746 if (info.client == failedClient) {
747 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700748 found = true;
Girish434b4d82023-07-11 23:24:54 +0000749 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700750 }
751 }
752 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000753 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700754 break;
755 }
756 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200757 if (found) {
758 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
759 } else {
760 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700761 }
762 }
763
Wonsik Kim271429d2020-10-01 10:12:56 -0700764 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700765}
766
Brian Lindahl64ee9452022-01-14 13:31:16 +0100767Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800768 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
769 originalPid, newPid);
770 mServiceLog->add(log);
771
772 // allow if this is called from the same process or the process has
773 // permission.
774 if ((AIBinder_getCallingPid() != getpid()) &&
775 (checkCallingPermission(String16(
776 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
777 ALOGE(
778 "Permission Denial: can't access overridePid method from pid=%d, "
779 "self pid=%d\n",
780 AIBinder_getCallingPid(), getpid());
781 return Status::fromServiceSpecificError(PERMISSION_DENIED);
782 }
783
784 {
Girish434b4d82023-07-11 23:24:54 +0000785 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800786 mOverridePidMap.erase(originalPid);
787 if (newPid != -1) {
788 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000789 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800790 }
791 }
792
793 return Status::ok();
794}
795
Chong Zhang97d367b2020-09-16 12:53:14 -0700796Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100797 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700798 int oomScore) {
799 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
800 pid, procState, oomScore);
801 mServiceLog->add(log);
802
803 // Only allow the override if the caller already can access process state and oom scores.
804 int callingPid = AIBinder_getCallingPid();
805 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
806 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
807 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
808 return Status::fromServiceSpecificError(PERMISSION_DENIED);
809 }
810
811 if (client == nullptr) {
812 return Status::fromServiceSpecificError(BAD_VALUE);
813 }
814
Girish434b4d82023-07-11 23:24:54 +0000815 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700816 removeProcessInfoOverride_l(pid);
817
818 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
819 // Override value is rejected by ProcessInfo.
820 return Status::fromServiceSpecificError(BAD_VALUE);
821 }
822
Girish1f002cf2023-02-17 00:36:29 +0000823 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
824 .uid = 0,
825 .id = 0,
826 .name = "<unknown client>"};
Girishf1d166c2023-07-20 22:35:29 +0000827 auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
828 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Chong Zhang97d367b2020-09-16 12:53:14 -0700829
Girishf1d166c2023-07-20 22:35:29 +0000830 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700831
832 return Status::ok();
833}
834
Chong Zhang97d367b2020-09-16 12:53:14 -0700835void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000836 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700837
838 removeProcessInfoOverride_l(pid);
839}
840
841void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
842 auto it = mProcessInfoOverrideMap.find(pid);
843 if (it == mProcessInfoOverrideMap.end()) {
844 return;
845 }
846
847 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700848 mProcessInfoOverrideMap.erase(pid);
849}
850
Girish9128e242022-11-23 20:52:29 +0000851Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
852 int32_t pid = clientInfo.pid;
853 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700854 String8 log = String8::format(
855 "markClientForPendingRemoval(pid %d, clientId %lld)",
856 pid, (long long) clientId);
857 mServiceLog->add(log);
858
Girish434b4d82023-07-11 23:24:54 +0000859 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100860 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800861 pid_t callingPid = IPCThreadState::self()->getCallingPid();
862 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
863 pid, callingPid);
864 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700865 }
Girish434b4d82023-07-11 23:24:54 +0000866 PidResourceInfosMap::iterator found = mMap.find(pid);
867 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700868 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
869 pid, (long long)clientId);
870 return Status::ok();
871 }
Girish434b4d82023-07-11 23:24:54 +0000872 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700873
Girish434b4d82023-07-11 23:24:54 +0000874 ResourceInfos::iterator foundClient = infos.find(clientId);
875 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700876 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
877 return Status::ok();
878 }
879
Girish434b4d82023-07-11 23:24:54 +0000880 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700881 info.pendingRemoval = true;
882 return Status::ok();
883}
884
Wonsik Kim271429d2020-10-01 10:12:56 -0700885Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
886 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
887 mServiceLog->add(log);
888
Girish434b4d82023-07-11 23:24:54 +0000889 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700890 {
Girish434b4d82023-07-11 23:24:54 +0000891 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100892 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800893 pid_t callingPid = IPCThreadState::self()->getCallingPid();
894 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
895 pid, callingPid);
896 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700897 }
898
899 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
900 MediaResource::Type::kNonSecureCodec,
901 MediaResource::Type::kGraphicMemory,
902 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100903 switch (type) {
904 // Codec resources are segregated by audio, video and image domains.
905 case MediaResource::Type::kSecureCodec:
906 case MediaResource::Type::kNonSecureCodec:
907 for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec,
908 MediaResource::SubType::kVideoCodec,
909 MediaResource::SubType::kImageCodec}) {
910 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000911 uid_t uid = 0;
912 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000913 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100914 continue;
915 }
916 }
917 break;
918 // Non-codec resources are shared by audio, video and image codecs (no subtype).
919 default:
920 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000921 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100922 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +0000923 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000924 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100925 }
926 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700927 }
928 }
929 }
930
931 if (!clients.empty()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100932 reclaimUnconditionallyFrom(clients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700933 }
934 return Status::ok();
935}
936
Henry Fang32762922020-01-28 18:40:39 -0800937bool ResourceManagerService::getPriority_l(int pid, int* priority) {
938 int newPid = pid;
939
940 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
941 newPid = mOverridePidMap[pid];
942 ALOGD("getPriority_l: use override pid %d instead original pid %d",
943 newPid, pid);
944 }
945
946 return mProcessInfo->getPriority(newPid, priority);
947}
948
Brian Lindahl64ee9452022-01-14 13:31:16 +0100949bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +0000950 MediaResource::SubType subType,
951 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000952 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
953 std::vector<std::shared_ptr<IResourceManagerClient>> temp;
Girish9128e242022-11-23 20:52:29 +0000954 PidUidVector tempIdList;
955
Girish434b4d82023-07-11 23:24:54 +0000956 for (auto& [pid, infos] : mMap) {
957 for (const auto& [id, info] : infos) {
958 if (hasResourceType(type, subType, info.resources)) {
959 if (!isCallingPriorityHigher_l(callingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700960 // some higher/equal priority process owns the resource,
961 // this request can't be fulfilled.
962 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Girish434b4d82023-07-11 23:24:54 +0000963 asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700964 return false;
965 }
Girish434b4d82023-07-11 23:24:54 +0000966 temp.push_back(info.client);
967 tempIdList.emplace_back(pid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700968 }
969 }
970 }
971 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800972 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700973 return true;
974 }
Girish434b4d82023-07-11 23:24:54 +0000975
976 clients->insert(std::end(*clients), std::begin(temp), std::end(temp));
Girish9128e242022-11-23 20:52:29 +0000977 idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700978 return true;
979}
980
Brian Lindahl64ee9452022-01-14 13:31:16 +0100981bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid,
Girish9128e242022-11-23 20:52:29 +0000982 MediaResource::Type type,
983 MediaResource::SubType subType,
984 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800985 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700986 int lowestPriorityPid;
987 int lowestPriority;
988 int callingPriority;
Girish9128e242022-11-23 20:52:29 +0000989 uid_t uid = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700990
991 // Before looking into other processes, check if we have clients marked for
992 // pending removal in the same process.
Girish9128e242022-11-23 20:52:29 +0000993 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) {
994 idVector->emplace_back(callingPid, uid);
Wonsik Kimd20e9362020-04-28 10:42:57 -0700995 return true;
996 }
Henry Fang32762922020-01-28 18:40:39 -0800997 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700998 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
999 callingPid);
1000 return false;
1001 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001002 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001003 return false;
1004 }
1005 if (lowestPriority <= callingPriority) {
1006 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1007 lowestPriority, callingPriority);
1008 return false;
1009 }
1010
Girish9128e242022-11-23 20:52:29 +00001011 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001012 return false;
1013 }
Girish9128e242022-11-23 20:52:29 +00001014
1015 idVector->emplace_back(lowestPriorityPid, uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001016 return true;
1017}
1018
Brian Lindahl64ee9452022-01-14 13:31:16 +01001019bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1020 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001021 int pid = -1;
1022 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001023 for (auto& [tempPid, infos] : mMap) {
1024 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001025 // no client on this process.
1026 continue;
1027 }
Girish434b4d82023-07-11 23:24:54 +00001028 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001029 // doesn't have the requested resource type
1030 continue;
1031 }
Girish434b4d82023-07-11 23:24:54 +00001032 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001033 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001034 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1035 // TODO: remove this pid from mMap?
1036 continue;
1037 }
1038 if (pid == -1 || tempPriority > priority) {
1039 // initial the value
1040 pid = tempPid;
1041 priority = tempPriority;
1042 }
1043 }
1044 if (pid != -1) {
1045 *lowestPriorityPid = pid;
1046 *lowestPriority = priority;
1047 }
1048 return (pid != -1);
1049}
1050
1051bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1052 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001053 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001054 return false;
1055 }
1056
1057 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001058 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001059 return false;
1060 }
1061
1062 return (callingPidPriority < priority);
1063}
1064
Brian Lindahl64ee9452022-01-14 13:31:16 +01001065bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001066 MediaResource::SubType subType, uid_t& uid,
1067 std::shared_ptr<IResourceManagerClient> *client) {
1068 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001069}
1070
1071bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001072 MediaResource::SubType subType, uid_t& uid,
1073 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001074 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001075 PidResourceInfosMap::iterator found = mMap.find(pid);
1076 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001077 ALOGE_IF(!pendingRemovalOnly,
1078 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001079 return false;
1080 }
1081
Chong Zhangfdd512a2019-11-22 11:03:14 -08001082 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001083 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001084 const ResourceInfos& infos = found->second;
1085 for (const auto& [id, info] : infos) {
1086 const ResourceList& resources = info.resources;
1087 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001088 continue;
1089 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001090 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001091 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001092 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001093 if (resource.value > largestValue) {
1094 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001095 clientTemp = info.client;
1096 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001097 }
1098 }
1099 }
1100 }
1101
1102 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001103 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001104 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1105 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001106 return false;
1107 }
1108
1109 *client = clientTemp;
1110 return true;
1111}
1112
Girish1f002cf2023-02-17 00:36:29 +00001113Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1114 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1115 return Status::ok();
1116}
1117
1118Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1119 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1120 return Status::ok();
1121}
1122
1123Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1124 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1125 return Status::ok();
1126}
1127
Girishde8eb592023-04-13 18:49:17 +00001128Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1129 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1130 return Status::ok();
1131}
1132
Girish1f002cf2023-02-17 00:36:29 +00001133long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1134 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1135}
1136
1137long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1138 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1139}
1140
Ronghua Wu231c3d12015-03-11 15:10:32 -07001141} // namespace android