blob: d303ab416065010d88c3b2f6ec65dbfce08026f2 [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
Girish56fda312023-10-12 21:32:35 +0000588void ResourceManagerService::getClientForResource_l(
589 const ResourceRequestInfo& resourceRequestInfo,
590 std::vector<ClientInfo>& clientsInfo) {
591 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700592 if (res == NULL) {
593 return;
594 }
Girish56fda312023-10-12 21:32:35 +0000595 ClientInfo clientInfo;
596 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
597 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700598 }
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
Girish56fda312023-10-12 21:32:35 +0000610 std::vector<ClientInfo> targetClients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700611 {
Girish434b4d82023-07-11 23:24:54 +0000612 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100613 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800614 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
615 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
616 callingPid, actualCallingPid);
617 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800618 }
Chong Zhang181e6952019-10-09 13:23:39 -0700619 const MediaResourceParcel *secureCodec = NULL;
620 const MediaResourceParcel *nonSecureCodec = NULL;
621 const MediaResourceParcel *graphicMemory = NULL;
622 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700623 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100624 switch (resources[i].type) {
625 case MediaResource::Type::kSecureCodec:
626 secureCodec = &resources[i];
627 break;
628 case MediaResource::Type::kNonSecureCodec:
629 nonSecureCodec = &resources[i];
630 break;
631 case MediaResource::Type::kGraphicMemory:
632 graphicMemory = &resources[i];
633 break;
634 case MediaResource::Type::kDrmSession:
635 drmSession = &resources[i];
636 break;
637 default:
638 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700639 }
640 }
641
642 // first pass to handle secure/non-secure codec conflict
643 if (secureCodec != NULL) {
Girish56fda312023-10-12 21:32:35 +0000644 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
645 .subType = secureCodec->subType};
646 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
Ronghua Wu05d89f12015-07-07 16:47:42 -0700647 if (!mSupportsMultipleSecureCodecs) {
Girish56fda312023-10-12 21:32:35 +0000648 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700649 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700650 }
651 }
652 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000653 mediaResource.type = MediaResource::Type::kNonSecureCodec;
654 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700655 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700656 }
657 }
658 }
659 if (nonSecureCodec != NULL) {
660 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000661 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
662 .subType = nonSecureCodec->subType};
663 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
664 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700665 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700666 }
667 }
668 }
Girish56fda312023-10-12 21:32:35 +0000669
Robert Shihc3af31b2019-09-20 21:45:01 -0700670 if (drmSession != NULL) {
Girish56fda312023-10-12 21:32:35 +0000671 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
672 getClientForResource_l(resourceRequestInfo, targetClients);
673 if (targetClients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700674 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700675 }
676 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700677
Girish56fda312023-10-12 21:32:35 +0000678 if (targetClients.size() == 0 && graphicMemory != nullptr) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700679 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish56fda312023-10-12 21:32:35 +0000680 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
681 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700682 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700683
Girish56fda312023-10-12 21:32:35 +0000684 if (targetClients.size() == 0) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700685 // if we are here, run the third pass to free one codec with the same type.
Girish56fda312023-10-12 21:32:35 +0000686 if (secureCodec != nullptr) {
687 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
688 getClientForResource_l(resourceRequestInfo, targetClients);
689 }
690 if (nonSecureCodec != nullptr) {
691 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
692 getClientForResource_l(resourceRequestInfo, targetClients);
693 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700694 }
695
Girish56fda312023-10-12 21:32:35 +0000696 if (targetClients.size() == 0) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700697 // if we are here, run the fourth pass to free one codec with the different type.
Girish56fda312023-10-12 21:32:35 +0000698 if (secureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700699 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000700 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
701 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700702 }
Girish56fda312023-10-12 21:32:35 +0000703 if (nonSecureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700704 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000705 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
706 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700707 }
708 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700709 }
710
Girish56fda312023-10-12 21:32:35 +0000711 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000712
713 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000714 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000715
Wonsik Kim271429d2020-10-01 10:12:56 -0700716 return Status::ok();
717}
718
Girish9128e242022-11-23 20:52:29 +0000719void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000720 const std::vector<ClientInfo>& targetClients,
721 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000722 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000723 int requesterPriority = -1;
724 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000725 std::vector<int> priorities;
726 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000727
Girish56fda312023-10-12 21:32:35 +0000728 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000729 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000730 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000731 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000732 }
Girish56fda312023-10-12 21:32:35 +0000733 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000734}
735
Brian Lindahl64ee9452022-01-14 13:31:16 +0100736bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000737 const std::vector<ClientInfo>& targetClients) {
738 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700739 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700740 }
741
Chong Zhangfdd512a2019-11-22 11:03:14 -0800742 std::shared_ptr<IResourceManagerClient> failedClient;
Girish56fda312023-10-12 21:32:35 +0000743 for (const ClientInfo& targetClient : targetClients) {
744 if (targetClient.mClient == nullptr) {
745 // skip already released clients.
746 continue;
747 }
748 String8 log = String8::format("reclaimResource from client %p", targetClient.mClient.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700749 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700750 bool success;
Girish56fda312023-10-12 21:32:35 +0000751 Status status = targetClient.mClient->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700752 if (!status.isOk() || !success) {
Girish56fda312023-10-12 21:32:35 +0000753 failedClient = targetClient.mClient;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700754 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700755 }
756 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700757
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700758 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700759 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700760 }
761
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200762 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700763 {
Girish434b4d82023-07-11 23:24:54 +0000764 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700765 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000766 for (auto& [pid, infos] : mMap) {
767 for (const auto& [id, info] : infos) {
768 if (info.client == failedClient) {
769 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700770 found = true;
Girish434b4d82023-07-11 23:24:54 +0000771 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700772 }
773 }
774 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000775 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700776 break;
777 }
778 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200779 if (found) {
780 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
781 } else {
782 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700783 }
784 }
785
Wonsik Kim271429d2020-10-01 10:12:56 -0700786 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700787}
788
Brian Lindahl64ee9452022-01-14 13:31:16 +0100789Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800790 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
791 originalPid, newPid);
792 mServiceLog->add(log);
793
794 // allow if this is called from the same process or the process has
795 // permission.
796 if ((AIBinder_getCallingPid() != getpid()) &&
797 (checkCallingPermission(String16(
798 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
799 ALOGE(
800 "Permission Denial: can't access overridePid method from pid=%d, "
801 "self pid=%d\n",
802 AIBinder_getCallingPid(), getpid());
803 return Status::fromServiceSpecificError(PERMISSION_DENIED);
804 }
805
806 {
Girish434b4d82023-07-11 23:24:54 +0000807 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800808 mOverridePidMap.erase(originalPid);
809 if (newPid != -1) {
810 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000811 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800812 }
813 }
814
815 return Status::ok();
816}
817
Chong Zhang97d367b2020-09-16 12:53:14 -0700818Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100819 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700820 int oomScore) {
821 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
822 pid, procState, oomScore);
823 mServiceLog->add(log);
824
825 // Only allow the override if the caller already can access process state and oom scores.
826 int callingPid = AIBinder_getCallingPid();
827 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
828 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
829 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
830 return Status::fromServiceSpecificError(PERMISSION_DENIED);
831 }
832
833 if (client == nullptr) {
834 return Status::fromServiceSpecificError(BAD_VALUE);
835 }
836
Girish434b4d82023-07-11 23:24:54 +0000837 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700838 removeProcessInfoOverride_l(pid);
839
840 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
841 // Override value is rejected by ProcessInfo.
842 return Status::fromServiceSpecificError(BAD_VALUE);
843 }
844
Girish1f002cf2023-02-17 00:36:29 +0000845 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
846 .uid = 0,
847 .id = 0,
848 .name = "<unknown client>"};
Girishf1d166c2023-07-20 22:35:29 +0000849 auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
850 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Chong Zhang97d367b2020-09-16 12:53:14 -0700851
Girishf1d166c2023-07-20 22:35:29 +0000852 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700853
854 return Status::ok();
855}
856
Chong Zhang97d367b2020-09-16 12:53:14 -0700857void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000858 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700859
860 removeProcessInfoOverride_l(pid);
861}
862
863void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
864 auto it = mProcessInfoOverrideMap.find(pid);
865 if (it == mProcessInfoOverrideMap.end()) {
866 return;
867 }
868
869 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700870 mProcessInfoOverrideMap.erase(pid);
871}
872
Girish9128e242022-11-23 20:52:29 +0000873Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
874 int32_t pid = clientInfo.pid;
875 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700876 String8 log = String8::format(
877 "markClientForPendingRemoval(pid %d, clientId %lld)",
878 pid, (long long) clientId);
879 mServiceLog->add(log);
880
Girish434b4d82023-07-11 23:24:54 +0000881 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100882 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800883 pid_t callingPid = IPCThreadState::self()->getCallingPid();
884 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
885 pid, callingPid);
886 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700887 }
Girish434b4d82023-07-11 23:24:54 +0000888 PidResourceInfosMap::iterator found = mMap.find(pid);
889 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700890 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
891 pid, (long long)clientId);
892 return Status::ok();
893 }
Girish434b4d82023-07-11 23:24:54 +0000894 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700895
Girish434b4d82023-07-11 23:24:54 +0000896 ResourceInfos::iterator foundClient = infos.find(clientId);
897 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700898 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
899 return Status::ok();
900 }
901
Girish434b4d82023-07-11 23:24:54 +0000902 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700903 info.pendingRemoval = true;
904 return Status::ok();
905}
906
Wonsik Kim271429d2020-10-01 10:12:56 -0700907Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
908 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
909 mServiceLog->add(log);
910
Girish56fda312023-10-12 21:32:35 +0000911 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700912 {
Girish434b4d82023-07-11 23:24:54 +0000913 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100914 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800915 pid_t callingPid = IPCThreadState::self()->getCallingPid();
916 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
917 pid, callingPid);
918 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700919 }
920
921 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
922 MediaResource::Type::kNonSecureCodec,
923 MediaResource::Type::kGraphicMemory,
924 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100925 switch (type) {
926 // Codec resources are segregated by audio, video and image domains.
927 case MediaResource::Type::kSecureCodec:
928 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000929 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
930 MediaResource::SubType::kSwAudioCodec,
931 MediaResource::SubType::kHwVideoCodec,
932 MediaResource::SubType::kSwVideoCodec,
933 MediaResource::SubType::kHwImageCodec,
934 MediaResource::SubType::kSwImageCodec}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100935 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000936 uid_t uid = 0;
937 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish56fda312023-10-12 21:32:35 +0000938 targetClients.emplace_back(pid, uid, client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100939 continue;
940 }
941 }
942 break;
943 // Non-codec resources are shared by audio, video and image codecs (no subtype).
944 default:
945 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000946 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100947 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +0000948 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish56fda312023-10-12 21:32:35 +0000949 targetClients.emplace_back(pid, uid, client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100950 }
951 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700952 }
953 }
954 }
955
Girish56fda312023-10-12 21:32:35 +0000956 if (!targetClients.empty()) {
957 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700958 }
959 return Status::ok();
960}
961
Henry Fang32762922020-01-28 18:40:39 -0800962bool ResourceManagerService::getPriority_l(int pid, int* priority) {
963 int newPid = pid;
964
965 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
966 newPid = mOverridePidMap[pid];
967 ALOGD("getPriority_l: use override pid %d instead original pid %d",
968 newPid, pid);
969 }
970
971 return mProcessInfo->getPriority(newPid, priority);
972}
973
Girish56fda312023-10-12 21:32:35 +0000974bool ResourceManagerService::getAllClients_l(
975 const ResourceRequestInfo& resourceRequestInfo,
976 std::vector<ClientInfo>& clientsInfo) {
977 MediaResource::Type type = resourceRequestInfo.mResource->type;
978 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000979
Girish434b4d82023-07-11 23:24:54 +0000980 for (auto& [pid, infos] : mMap) {
981 for (const auto& [id, info] : infos) {
982 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000983 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700984 // some higher/equal priority process owns the resource,
985 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000986 ALOGE("%s: can't reclaim resource %s from pid %d",
987 __func__, asString(type), pid);
988 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700989 return false;
990 }
Girish56fda312023-10-12 21:32:35 +0000991 clientsInfo.emplace_back(pid, info.uid, info.client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700992 }
993 }
994 }
Girish56fda312023-10-12 21:32:35 +0000995 if (clientsInfo.size() == 0) {
996 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700997 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700998 return true;
999}
1000
Girish56fda312023-10-12 21:32:35 +00001001bool ResourceManagerService::getLowestPriorityBiggestClient_l(
1002 const ResourceRequestInfo& resourceRequestInfo,
1003 ClientInfo& clientsInfo) {
1004 int callingPid = resourceRequestInfo.mCallingPid;
1005 MediaResource::Type type = resourceRequestInfo.mResource->type;
1006 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001007 int lowestPriorityPid;
1008 int lowestPriority;
1009 int callingPriority;
Girish9128e242022-11-23 20:52:29 +00001010 uid_t uid = 0;
Girish56fda312023-10-12 21:32:35 +00001011 std::shared_ptr<IResourceManagerClient> client;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001012
1013 // Before looking into other processes, check if we have clients marked for
1014 // pending removal in the same process.
Girish56fda312023-10-12 21:32:35 +00001015 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, &client)) {
1016 clientsInfo.mPid = callingPid;
1017 clientsInfo.mUid = uid;
1018 clientsInfo.mClient = client;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001019 return true;
1020 }
Henry Fang32762922020-01-28 18:40:39 -08001021 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001022 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
1023 callingPid);
1024 return false;
1025 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001026 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001027 return false;
1028 }
1029 if (lowestPriority <= callingPriority) {
1030 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1031 lowestPriority, callingPriority);
1032 return false;
1033 }
1034
Girish56fda312023-10-12 21:32:35 +00001035 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, &client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001036 return false;
1037 }
Girish9128e242022-11-23 20:52:29 +00001038
Girish56fda312023-10-12 21:32:35 +00001039 clientsInfo.mPid = lowestPriorityPid;
1040 clientsInfo.mUid = uid;
1041 clientsInfo.mClient = client;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 return true;
1043}
1044
Brian Lindahl64ee9452022-01-14 13:31:16 +01001045bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1046 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001047 int pid = -1;
1048 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001049 for (auto& [tempPid, infos] : mMap) {
1050 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001051 // no client on this process.
1052 continue;
1053 }
Girish434b4d82023-07-11 23:24:54 +00001054 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001055 // doesn't have the requested resource type
1056 continue;
1057 }
Girish434b4d82023-07-11 23:24:54 +00001058 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001059 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001060 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1061 // TODO: remove this pid from mMap?
1062 continue;
1063 }
1064 if (pid == -1 || tempPriority > priority) {
1065 // initial the value
1066 pid = tempPid;
1067 priority = tempPriority;
1068 }
1069 }
1070 if (pid != -1) {
1071 *lowestPriorityPid = pid;
1072 *lowestPriority = priority;
1073 }
1074 return (pid != -1);
1075}
1076
1077bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1078 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001079 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001080 return false;
1081 }
1082
1083 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001084 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001085 return false;
1086 }
1087
1088 return (callingPidPriority < priority);
1089}
1090
Brian Lindahl64ee9452022-01-14 13:31:16 +01001091bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001092 MediaResource::SubType subType, uid_t& uid,
1093 std::shared_ptr<IResourceManagerClient> *client) {
1094 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001095}
1096
1097bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001098 MediaResource::SubType subType, uid_t& uid,
1099 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001100 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001101 PidResourceInfosMap::iterator found = mMap.find(pid);
1102 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001103 ALOGE_IF(!pendingRemovalOnly,
1104 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001105 return false;
1106 }
1107
Chong Zhangfdd512a2019-11-22 11:03:14 -08001108 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001109 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001110 const ResourceInfos& infos = found->second;
1111 for (const auto& [id, info] : infos) {
1112 const ResourceList& resources = info.resources;
1113 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001114 continue;
1115 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001116 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001117 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001118 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001119 if (resource.value > largestValue) {
1120 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001121 clientTemp = info.client;
1122 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001123 }
1124 }
1125 }
1126 }
1127
1128 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001129 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001130 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1131 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001132 return false;
1133 }
1134
1135 *client = clientTemp;
1136 return true;
1137}
1138
Girish1f002cf2023-02-17 00:36:29 +00001139Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1140 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1141 return Status::ok();
1142}
1143
1144Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1145 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1146 return Status::ok();
1147}
1148
1149Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1150 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1151 return Status::ok();
1152}
1153
Girishde8eb592023-04-13 18:49:17 +00001154Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1155 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1156 return Status::ok();
1157}
1158
Girish1f002cf2023-02-17 00:36:29 +00001159long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1160 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1161}
1162
1163long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1164 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1165}
1166
Ronghua Wu231c3d12015-03-11 15:10:32 -07001167} // namespace android