blob: c5eb537a003f4a7f7c366f85a0074e37243ac200 [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) {
Girishaff0ef22023-10-13 00:22:39 +0000591 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000592 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700593 if (res == NULL) {
594 return;
595 }
Girishaff0ef22023-10-13 00:22:39 +0000596
597 // Before looking into other processes, check if we have clients marked for
598 // pending removal in the same process.
599 uid_t uid = 0;
600 std::shared_ptr<IResourceManagerClient> client;
601 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, uid, &client)) {
602 clientsInfo.emplace_back(callingPid, uid, client);
603 return;
604 }
605
606 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000607 ClientInfo clientInfo;
608 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
609 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700610 }
611}
612
Girish9128e242022-11-23 20:52:29 +0000613Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100614 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000615 int32_t callingPid = clientInfo.pid;
616 std::string clientName = clientInfo.name;
617 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000618 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700619 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700620 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700621
Girish56fda312023-10-12 21:32:35 +0000622 std::vector<ClientInfo> targetClients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700623 {
Girish434b4d82023-07-11 23:24:54 +0000624 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100625 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800626 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
627 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
628 callingPid, actualCallingPid);
629 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800630 }
Chong Zhang181e6952019-10-09 13:23:39 -0700631 const MediaResourceParcel *secureCodec = NULL;
632 const MediaResourceParcel *nonSecureCodec = NULL;
633 const MediaResourceParcel *graphicMemory = NULL;
634 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700635 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100636 switch (resources[i].type) {
637 case MediaResource::Type::kSecureCodec:
638 secureCodec = &resources[i];
639 break;
640 case MediaResource::Type::kNonSecureCodec:
641 nonSecureCodec = &resources[i];
642 break;
643 case MediaResource::Type::kGraphicMemory:
644 graphicMemory = &resources[i];
645 break;
646 case MediaResource::Type::kDrmSession:
647 drmSession = &resources[i];
648 break;
649 default:
650 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700651 }
652 }
653
654 // first pass to handle secure/non-secure codec conflict
655 if (secureCodec != NULL) {
Girish56fda312023-10-12 21:32:35 +0000656 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
657 .subType = secureCodec->subType};
658 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
Ronghua Wu05d89f12015-07-07 16:47:42 -0700659 if (!mSupportsMultipleSecureCodecs) {
Girish56fda312023-10-12 21:32:35 +0000660 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700661 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700662 }
663 }
664 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000665 mediaResource.type = MediaResource::Type::kNonSecureCodec;
666 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700667 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700668 }
669 }
670 }
671 if (nonSecureCodec != NULL) {
672 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000673 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
674 .subType = nonSecureCodec->subType};
675 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
676 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700677 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700678 }
679 }
680 }
Girish56fda312023-10-12 21:32:35 +0000681
Robert Shihc3af31b2019-09-20 21:45:01 -0700682 if (drmSession != NULL) {
Girish56fda312023-10-12 21:32:35 +0000683 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
684 getClientForResource_l(resourceRequestInfo, targetClients);
685 if (targetClients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700686 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700687 }
688 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700689
Girish56fda312023-10-12 21:32:35 +0000690 if (targetClients.size() == 0 && graphicMemory != nullptr) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700691 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish56fda312023-10-12 21:32:35 +0000692 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
693 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700694 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700695
Girish56fda312023-10-12 21:32:35 +0000696 if (targetClients.size() == 0) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700697 // if we are here, run the third pass to free one codec with the same type.
Girish56fda312023-10-12 21:32:35 +0000698 if (secureCodec != nullptr) {
699 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
700 getClientForResource_l(resourceRequestInfo, targetClients);
701 }
702 if (nonSecureCodec != nullptr) {
703 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
704 getClientForResource_l(resourceRequestInfo, targetClients);
705 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700706 }
707
Girish56fda312023-10-12 21:32:35 +0000708 if (targetClients.size() == 0) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700709 // if we are here, run the fourth pass to free one codec with the different type.
Girish56fda312023-10-12 21:32:35 +0000710 if (secureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700711 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000712 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
713 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700714 }
Girish56fda312023-10-12 21:32:35 +0000715 if (nonSecureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700716 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000717 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
718 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700719 }
720 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700721 }
722
Girish56fda312023-10-12 21:32:35 +0000723 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000724
725 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000726 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000727
Wonsik Kim271429d2020-10-01 10:12:56 -0700728 return Status::ok();
729}
730
Girish9128e242022-11-23 20:52:29 +0000731void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000732 const std::vector<ClientInfo>& targetClients,
733 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000734 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000735 int requesterPriority = -1;
736 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000737 std::vector<int> priorities;
738 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000739
Girish56fda312023-10-12 21:32:35 +0000740 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000741 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000742 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000743 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000744 }
Girish56fda312023-10-12 21:32:35 +0000745 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000746}
747
Brian Lindahl64ee9452022-01-14 13:31:16 +0100748bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000749 const std::vector<ClientInfo>& targetClients) {
750 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700751 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700752 }
753
Chong Zhangfdd512a2019-11-22 11:03:14 -0800754 std::shared_ptr<IResourceManagerClient> failedClient;
Girish56fda312023-10-12 21:32:35 +0000755 for (const ClientInfo& targetClient : targetClients) {
756 if (targetClient.mClient == nullptr) {
757 // skip already released clients.
758 continue;
759 }
760 String8 log = String8::format("reclaimResource from client %p", targetClient.mClient.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700761 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700762 bool success;
Girish56fda312023-10-12 21:32:35 +0000763 Status status = targetClient.mClient->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700764 if (!status.isOk() || !success) {
Girish56fda312023-10-12 21:32:35 +0000765 failedClient = targetClient.mClient;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700766 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700767 }
768 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700769
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700770 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700771 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700772 }
773
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200774 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700775 {
Girish434b4d82023-07-11 23:24:54 +0000776 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700777 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000778 for (auto& [pid, infos] : mMap) {
779 for (const auto& [id, info] : infos) {
780 if (info.client == failedClient) {
781 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700782 found = true;
Girish434b4d82023-07-11 23:24:54 +0000783 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700784 }
785 }
786 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000787 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700788 break;
789 }
790 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200791 if (found) {
792 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
793 } else {
794 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700795 }
796 }
797
Wonsik Kim271429d2020-10-01 10:12:56 -0700798 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700799}
800
Brian Lindahl64ee9452022-01-14 13:31:16 +0100801Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800802 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
803 originalPid, newPid);
804 mServiceLog->add(log);
805
806 // allow if this is called from the same process or the process has
807 // permission.
808 if ((AIBinder_getCallingPid() != getpid()) &&
809 (checkCallingPermission(String16(
810 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
811 ALOGE(
812 "Permission Denial: can't access overridePid method from pid=%d, "
813 "self pid=%d\n",
814 AIBinder_getCallingPid(), getpid());
815 return Status::fromServiceSpecificError(PERMISSION_DENIED);
816 }
817
818 {
Girish434b4d82023-07-11 23:24:54 +0000819 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800820 mOverridePidMap.erase(originalPid);
821 if (newPid != -1) {
822 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000823 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800824 }
825 }
826
827 return Status::ok();
828}
829
Chong Zhang97d367b2020-09-16 12:53:14 -0700830Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100831 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700832 int oomScore) {
833 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
834 pid, procState, oomScore);
835 mServiceLog->add(log);
836
837 // Only allow the override if the caller already can access process state and oom scores.
838 int callingPid = AIBinder_getCallingPid();
839 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
840 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
841 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
842 return Status::fromServiceSpecificError(PERMISSION_DENIED);
843 }
844
845 if (client == nullptr) {
846 return Status::fromServiceSpecificError(BAD_VALUE);
847 }
848
Girish434b4d82023-07-11 23:24:54 +0000849 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700850 removeProcessInfoOverride_l(pid);
851
852 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
853 // Override value is rejected by ProcessInfo.
854 return Status::fromServiceSpecificError(BAD_VALUE);
855 }
856
Girish1f002cf2023-02-17 00:36:29 +0000857 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
858 .uid = 0,
859 .id = 0,
860 .name = "<unknown client>"};
Girishf1d166c2023-07-20 22:35:29 +0000861 auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
862 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Chong Zhang97d367b2020-09-16 12:53:14 -0700863
Girishf1d166c2023-07-20 22:35:29 +0000864 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700865
866 return Status::ok();
867}
868
Chong Zhang97d367b2020-09-16 12:53:14 -0700869void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000870 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700871
872 removeProcessInfoOverride_l(pid);
873}
874
875void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
876 auto it = mProcessInfoOverrideMap.find(pid);
877 if (it == mProcessInfoOverrideMap.end()) {
878 return;
879 }
880
881 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700882 mProcessInfoOverrideMap.erase(pid);
883}
884
Girish9128e242022-11-23 20:52:29 +0000885Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
886 int32_t pid = clientInfo.pid;
887 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700888 String8 log = String8::format(
889 "markClientForPendingRemoval(pid %d, clientId %lld)",
890 pid, (long long) clientId);
891 mServiceLog->add(log);
892
Girish434b4d82023-07-11 23:24:54 +0000893 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100894 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800895 pid_t callingPid = IPCThreadState::self()->getCallingPid();
896 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
897 pid, callingPid);
898 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700899 }
Girish434b4d82023-07-11 23:24:54 +0000900 PidResourceInfosMap::iterator found = mMap.find(pid);
901 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700902 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
903 pid, (long long)clientId);
904 return Status::ok();
905 }
Girish434b4d82023-07-11 23:24:54 +0000906 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700907
Girish434b4d82023-07-11 23:24:54 +0000908 ResourceInfos::iterator foundClient = infos.find(clientId);
909 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700910 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
911 return Status::ok();
912 }
913
Girish434b4d82023-07-11 23:24:54 +0000914 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700915 info.pendingRemoval = true;
916 return Status::ok();
917}
918
Wonsik Kim271429d2020-10-01 10:12:56 -0700919Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
920 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
921 mServiceLog->add(log);
922
Girish56fda312023-10-12 21:32:35 +0000923 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700924 {
Girish434b4d82023-07-11 23:24:54 +0000925 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100926 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800927 pid_t callingPid = IPCThreadState::self()->getCallingPid();
928 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
929 pid, callingPid);
930 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700931 }
932
933 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
934 MediaResource::Type::kNonSecureCodec,
935 MediaResource::Type::kGraphicMemory,
936 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100937 switch (type) {
938 // Codec resources are segregated by audio, video and image domains.
939 case MediaResource::Type::kSecureCodec:
940 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000941 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
942 MediaResource::SubType::kSwAudioCodec,
943 MediaResource::SubType::kHwVideoCodec,
944 MediaResource::SubType::kSwVideoCodec,
945 MediaResource::SubType::kHwImageCodec,
946 MediaResource::SubType::kSwImageCodec}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100947 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000948 uid_t uid = 0;
949 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish56fda312023-10-12 21:32:35 +0000950 targetClients.emplace_back(pid, uid, client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100951 continue;
952 }
953 }
954 break;
955 // Non-codec resources are shared by audio, video and image codecs (no subtype).
956 default:
957 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000958 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100959 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +0000960 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish56fda312023-10-12 21:32:35 +0000961 targetClients.emplace_back(pid, uid, client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100962 }
963 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700964 }
965 }
966 }
967
Girish56fda312023-10-12 21:32:35 +0000968 if (!targetClients.empty()) {
969 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700970 }
971 return Status::ok();
972}
973
Henry Fang32762922020-01-28 18:40:39 -0800974bool ResourceManagerService::getPriority_l(int pid, int* priority) {
975 int newPid = pid;
976
977 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
978 newPid = mOverridePidMap[pid];
979 ALOGD("getPriority_l: use override pid %d instead original pid %d",
980 newPid, pid);
981 }
982
983 return mProcessInfo->getPriority(newPid, priority);
984}
985
Girish56fda312023-10-12 21:32:35 +0000986bool ResourceManagerService::getAllClients_l(
987 const ResourceRequestInfo& resourceRequestInfo,
988 std::vector<ClientInfo>& clientsInfo) {
989 MediaResource::Type type = resourceRequestInfo.mResource->type;
990 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000991
Girish434b4d82023-07-11 23:24:54 +0000992 for (auto& [pid, infos] : mMap) {
993 for (const auto& [id, info] : infos) {
994 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000995 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700996 // some higher/equal priority process owns the resource,
997 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000998 ALOGE("%s: can't reclaim resource %s from pid %d",
999 __func__, asString(type), pid);
1000 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -07001001 return false;
1002 }
Girish56fda312023-10-12 21:32:35 +00001003 clientsInfo.emplace_back(pid, info.uid, info.client);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001004 }
1005 }
1006 }
Girish56fda312023-10-12 21:32:35 +00001007 if (clientsInfo.size() == 0) {
1008 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001009 }
Ronghua Wu231c3d12015-03-11 15:10:32 -07001010 return true;
1011}
1012
Girishaff0ef22023-10-13 00:22:39 +00001013// Process priority (oom score) based reclaim:
1014// - Find a process with lowest priority (than that of calling process).
1015// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +00001016bool ResourceManagerService::getLowestPriorityBiggestClient_l(
1017 const ResourceRequestInfo& resourceRequestInfo,
1018 ClientInfo& clientsInfo) {
1019 int callingPid = resourceRequestInfo.mCallingPid;
1020 MediaResource::Type type = resourceRequestInfo.mResource->type;
1021 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001022 int lowestPriorityPid;
1023 int lowestPriority;
1024 int callingPriority;
Girish9128e242022-11-23 20:52:29 +00001025 uid_t uid = 0;
Girish56fda312023-10-12 21:32:35 +00001026 std::shared_ptr<IResourceManagerClient> client;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001027
Henry Fang32762922020-01-28 18:40:39 -08001028 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +00001029 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001030 return false;
1031 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001032 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001033 return false;
1034 }
1035 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +00001036 ALOGE("%s: lowest priority %d vs caller priority %d",
1037 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001038 return false;
1039 }
1040
Girish56fda312023-10-12 21:32:35 +00001041 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, &client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001042 return false;
1043 }
Girish9128e242022-11-23 20:52:29 +00001044
Girish56fda312023-10-12 21:32:35 +00001045 clientsInfo.mPid = lowestPriorityPid;
1046 clientsInfo.mUid = uid;
1047 clientsInfo.mClient = client;
Girishaff0ef22023-10-13 00:22:39 +00001048 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
1049 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001050 return true;
1051}
1052
Brian Lindahl64ee9452022-01-14 13:31:16 +01001053bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1054 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001055 int pid = -1;
1056 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001057 for (auto& [tempPid, infos] : mMap) {
1058 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001059 // no client on this process.
1060 continue;
1061 }
Girish434b4d82023-07-11 23:24:54 +00001062 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001063 // doesn't have the requested resource type
1064 continue;
1065 }
Girish434b4d82023-07-11 23:24:54 +00001066 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001067 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001068 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1069 // TODO: remove this pid from mMap?
1070 continue;
1071 }
1072 if (pid == -1 || tempPriority > priority) {
1073 // initial the value
1074 pid = tempPid;
1075 priority = tempPriority;
1076 }
1077 }
1078 if (pid != -1) {
1079 *lowestPriorityPid = pid;
1080 *lowestPriority = priority;
1081 }
1082 return (pid != -1);
1083}
1084
1085bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1086 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001087 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001088 return false;
1089 }
1090
1091 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001092 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001093 return false;
1094 }
1095
1096 return (callingPidPriority < priority);
1097}
1098
Brian Lindahl64ee9452022-01-14 13:31:16 +01001099bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001100 MediaResource::SubType subType, uid_t& uid,
1101 std::shared_ptr<IResourceManagerClient> *client) {
1102 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001103}
1104
1105bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001106 MediaResource::SubType subType, uid_t& uid,
1107 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001108 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001109 PidResourceInfosMap::iterator found = mMap.find(pid);
1110 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001111 ALOGE_IF(!pendingRemovalOnly,
1112 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001113 return false;
1114 }
1115
Chong Zhangfdd512a2019-11-22 11:03:14 -08001116 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001117 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001118 const ResourceInfos& infos = found->second;
1119 for (const auto& [id, info] : infos) {
1120 const ResourceList& resources = info.resources;
1121 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001122 continue;
1123 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001124 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001125 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001126 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001127 if (resource.value > largestValue) {
1128 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001129 clientTemp = info.client;
1130 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001131 }
1132 }
1133 }
1134 }
1135
1136 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001137 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001138 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1139 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001140 return false;
1141 }
1142
1143 *client = clientTemp;
1144 return true;
1145}
1146
Girish1f002cf2023-02-17 00:36:29 +00001147Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1148 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1149 return Status::ok();
1150}
1151
1152Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1153 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1154 return Status::ok();
1155}
1156
1157Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1158 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1159 return Status::ok();
1160}
1161
Girishde8eb592023-04-13 18:49:17 +00001162Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1163 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1164 return Status::ok();
1165}
1166
Girish1f002cf2023-02-17 00:36:29 +00001167long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1168 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1169}
1170
1171long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1172 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1173}
1174
Ronghua Wu231c3d12015-03-11 15:10:32 -07001175} // namespace android