blob: 5d1ba2bde939079337fee4475614fc7dda0ca294 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
2**
3** Copyright 2015, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "ResourceManagerService"
20#include <utils/Log.h>
21
Chong Zhangfdd512a2019-11-22 11:03:14 -080022#include <android/binder_manager.h>
23#include <android/binder_process.h>
Chong Zhangc7303e82020-12-02 16:38:52 -080024#include <binder/IPCThreadState.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070026#include <cutils/sched_policy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <dirent.h>
Chong Zhang181e6952019-10-09 13:23:39 -070028#include <media/MediaResourcePolicy.h>
Girish1f002cf2023-02-17 00:36:29 +000029#include <media/stagefright/foundation/ABase.h>
Chong Zhangee33d642019-08-08 14:26:43 -070030#include <mediautils/BatteryNotifier.h>
Girish1f002cf2023-02-17 00:36:29 +000031#include <mediautils/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070032#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070033#include <string.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <unistd.h>
38
Steven Moreland0a0ff0b2021-04-02 00:06:01 +000039#include "IMediaResourceMonitor.h"
Girish1f002cf2023-02-17 00:36:29 +000040#include "ResourceManagerMetrics.h"
Ronghua Wu231c3d12015-03-11 15:10:32 -070041#include "ResourceManagerService.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070042#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070043#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070044
Ronghua Wu231c3d12015-03-11 15:10:32 -070045namespace android {
46
Girishf1d166c2023-07-20 22:35:29 +000047class DeathNotifier : public std::enable_shared_from_this<DeathNotifier> {
Chong Zhang97d367b2020-09-16 12:53:14 -070048
Girishf1d166c2023-07-20 22:35:29 +000049 // BinderDiedContext defines the cookie that is passed as DeathRecipient.
50 // Since this can maintain more context than a raw pointer, we can
51 // validate the scope of DeathNotifier, before deferencing it upon the binder death.
52 struct BinderDiedContext {
53 std::weak_ptr<DeathNotifier> mDeathNotifier;
54 };
Chong Zhang97d367b2020-09-16 12:53:14 -070055public:
Girishf1d166c2023-07-20 22:35:29 +000056 DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
57 const std::shared_ptr<ResourceManagerService>& service,
58 const ClientInfoParcel& clientInfo,
59 AIBinder_DeathRecipient* recipient);
Chong Zhang97d367b2020-09-16 12:53:14 -070060
Girishf1d166c2023-07-20 22:35:29 +000061 virtual ~DeathNotifier() {
62 unlink();
63 }
64
65 void unlink() {
66 if (mClient != nullptr) {
67 // Register for the callbacks by linking to death notification.
68 AIBinder_unlinkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
69 mClient = nullptr;
70 }
71 }
Chong Zhang97d367b2020-09-16 12:53:14 -070072
73 // Implement death recipient
74 static void BinderDiedCallback(void* cookie);
Girishf1d166c2023-07-20 22:35:29 +000075 static void BinderUnlinkedCallback(void* cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -070076 virtual void binderDied();
77
Girishf1d166c2023-07-20 22:35:29 +000078private:
79 void link() {
80 // Create the context that is passed as cookie to the binder death notification.
81 // The context gets deleted at BinderUnlinkedCallback.
82 mCookie = new BinderDiedContext{.mDeathNotifier = weak_from_this()};
83 // Register for the callbacks by linking to death notification.
84 AIBinder_linkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
85 }
86
Chong Zhang97d367b2020-09-16 12:53:14 -070087protected:
Girishf1d166c2023-07-20 22:35:29 +000088 std::shared_ptr<IResourceManagerClient> mClient;
Chong Zhang97d367b2020-09-16 12:53:14 -070089 std::weak_ptr<ResourceManagerService> mService;
Girish1f002cf2023-02-17 00:36:29 +000090 const ClientInfoParcel mClientInfo;
Girishf1d166c2023-07-20 22:35:29 +000091 AIBinder_DeathRecipient* mRecipient;
92 BinderDiedContext* mCookie;
Chong Zhang97d367b2020-09-16 12:53:14 -070093};
94
Girishf1d166c2023-07-20 22:35:29 +000095DeathNotifier::DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
96 const std::shared_ptr<ResourceManagerService>& service,
97 const ClientInfoParcel& clientInfo,
98 AIBinder_DeathRecipient* recipient)
99 : mClient(client), mService(service), mClientInfo(clientInfo),
100 mRecipient(recipient), mCookie(nullptr) {
101 link();
102}
103
104//static
105void DeathNotifier::BinderUnlinkedCallback(void* cookie) {
106 BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie);
107 // Since we don't need the context anymore, we are deleting it now.
108 delete context;
109}
Wonsik Kim3e378962017-01-05 17:00:02 +0900110
Chong Zhangfdd512a2019-11-22 11:03:14 -0800111//static
112void DeathNotifier::BinderDiedCallback(void* cookie) {
Girishf1d166c2023-07-20 22:35:29 +0000113 BinderDiedContext* context = reinterpret_cast<BinderDiedContext*>(cookie);
114
115 // Validate the context and check if the DeathNotifier object is still in scope.
116 if (context != nullptr) {
117 std::shared_ptr<DeathNotifier> thiz = context->mDeathNotifier.lock();
118 if (thiz != nullptr) {
119 thiz->binderDied();
120 } else {
121 ALOGI("DeathNotifier is out of scope already");
Chong Zhang97d367b2020-09-16 12:53:14 -0700122 }
Chong Zhang97d367b2020-09-16 12:53:14 -0700123 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800124}
Wonsik Kim3e378962017-01-05 17:00:02 +0900125
Chong Zhangfdd512a2019-11-22 11:03:14 -0800126void DeathNotifier::binderDied() {
127 // Don't check for pid validity since we know it's already dead.
128 std::shared_ptr<ResourceManagerService> service = mService.lock();
129 if (service == nullptr) {
130 ALOGW("ResourceManagerService is dead as well.");
131 return;
Wonsik Kim3e378962017-01-05 17:00:02 +0900132 }
Henry Fang32762922020-01-28 18:40:39 -0800133
Girish1f002cf2023-02-17 00:36:29 +0000134 service->overridePid(mClientInfo.pid, -1);
Henry Fangb35141c2020-06-29 13:11:39 -0700135 // thiz is freed in the call below, so it must be last call referring thiz
Girish1f002cf2023-02-17 00:36:29 +0000136 service->removeResource(mClientInfo, false /*checkValid*/);
Chong Zhang97d367b2020-09-16 12:53:14 -0700137}
Henry Fangb35141c2020-06-29 13:11:39 -0700138
Chong Zhang97d367b2020-09-16 12:53:14 -0700139class OverrideProcessInfoDeathNotifier : public DeathNotifier {
140public:
Girishf1d166c2023-07-20 22:35:29 +0000141 OverrideProcessInfoDeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
142 const std::shared_ptr<ResourceManagerService>& service,
143 const ClientInfoParcel& clientInfo,
144 AIBinder_DeathRecipient* recipient)
145 : DeathNotifier(client, service, clientInfo, recipient) {}
Chong Zhang97d367b2020-09-16 12:53:14 -0700146
147 virtual ~OverrideProcessInfoDeathNotifier() {}
148
149 virtual void binderDied();
150};
151
152void OverrideProcessInfoDeathNotifier::binderDied() {
153 // Don't check for pid validity since we know it's already dead.
154 std::shared_ptr<ResourceManagerService> service = mService.lock();
155 if (service == nullptr) {
156 ALOGW("ResourceManagerService is dead as well.");
157 return;
158 }
159
Girish1f002cf2023-02-17 00:36:29 +0000160 service->removeProcessInfoOverride(mClientInfo.pid);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800161}
Wonsik Kim3e378962017-01-05 17:00:02 +0900162
Ronghua Wu231c3d12015-03-11 15:10:32 -0700163template <typename T>
Girish434b4d82023-07-11 23:24:54 +0000164static String8 getString(const std::vector<T>& items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700165 String8 itemsStr;
166 for (size_t i = 0; i < items.size(); ++i) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000167 itemsStr.appendFormat("%s ", toString(items[i]).c_str());
Ronghua Wu231c3d12015-03-11 15:10:32 -0700168 }
169 return itemsStr;
170}
171
Brian Lindahl64ee9452022-01-14 13:31:16 +0100172static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
Girish429f8592022-10-13 04:27:00 +0000173 const MediaResourceParcel& resource) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100174 if (type != resource.type) {
175 return false;
176 }
177 switch (type) {
178 // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
179 // compare the subtypes as well.
180 case MediaResource::Type::kSecureCodec:
181 case MediaResource::Type::kNonSecureCodec:
182 if (resource.subType == subType) {
183 return true;
184 }
185 break;
186 // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
187 default:
188 return true;
189 }
190 return false;
191}
192
193static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
194 const ResourceList& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700195 for (auto it = resources.begin(); it != resources.end(); it++) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100196 if (hasResourceType(type, subType, it->second)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700197 return true;
198 }
199 }
200 return false;
201}
202
Brian Lindahl64ee9452022-01-14 13:31:16 +0100203static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
204 const ResourceInfos& infos) {
Girish434b4d82023-07-11 23:24:54 +0000205 for (const auto& [id, info] : infos) {
206 if (hasResourceType(type, subType, info.resources)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700207 return true;
208 }
209 }
210 return false;
211}
212
Brian Lindahl64ee9452022-01-14 13:31:16 +0100213static ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
Girish434b4d82023-07-11 23:24:54 +0000214 PidResourceInfosMap::iterator found = map.find(pid);
215 if (found == map.end()) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700216 // new pid
217 ResourceInfos infosForPid;
Girish434b4d82023-07-11 23:24:54 +0000218 auto [it, inserted] = map.emplace(pid, infosForPid);
219 found = it;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700220 }
221
Girish434b4d82023-07-11 23:24:54 +0000222 return found->second;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700223}
224
Brian Lindahl64ee9452022-01-14 13:31:16 +0100225static ResourceInfo& getResourceInfoForEdit(uid_t uid, int64_t clientId,
Girish9128e242022-11-23 20:52:29 +0000226 const std::string& name,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100227 const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
Girish434b4d82023-07-11 23:24:54 +0000228 ResourceInfos::iterator found = infos.find(clientId);
Chong Zhangfb092d32019-08-12 09:45:44 -0700229
Girish434b4d82023-07-11 23:24:54 +0000230 if (found == infos.end()) {
231 ResourceInfo info{.uid = uid,
232 .clientId = clientId,
233 .name = name.empty()? "<unknown client>" : name,
234 .client = client,
Girishf1d166c2023-07-20 22:35:29 +0000235 .deathNotifier = nullptr,
Girish434b4d82023-07-11 23:24:54 +0000236 .pendingRemoval = false};
237 auto [it, inserted] = infos.emplace(clientId, info);
238 found = it;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700239 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700240
Girish434b4d82023-07-11 23:24:54 +0000241 return found->second;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700242}
243
Girish434b4d82023-07-11 23:24:54 +0000244static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900245 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700246 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900247 if (binder != NULL) {
248 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
249 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100250 switch (resources[i].subType) {
251 case MediaResource::SubType::kAudioCodec:
252 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
253 break;
254 case MediaResource::SubType::kVideoCodec:
255 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
256 break;
257 case MediaResource::SubType::kImageCodec:
258 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
259 break;
260 case MediaResource::SubType::kUnspecifiedSubType:
261 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700262 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900263 }
264 }
265}
266
Brian Lindahl64ee9452022-01-14 13:31:16 +0100267binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700268 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700269
dcashman014e91e2015-09-11 09:33:01 -0700270 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
271 result.format("Permission Denial: "
272 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800273 AIBinder_getCallingPid(),
274 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000275 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -0700276 return PERMISSION_DENIED;
277 }
278
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700279 PidResourceInfosMap mapCopy;
280 bool supportsMultipleSecureCodecs;
281 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800282 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700283 String8 serviceLog;
284 {
Girish434b4d82023-07-11 23:24:54 +0000285 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700286 mapCopy = mMap; // Shadow copy, real copy will happen on write.
287 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
288 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
289 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800290 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700291 }
292
293 const size_t SIZE = 256;
294 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700295 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
296 result.append(buffer);
297 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700298 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700299 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700300 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
301 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700302 result.append(buffer);
303
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700304 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000305 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000306 snprintf(buffer, SIZE, " Pid: %d\n", pid);
307 result.append(buffer);
308 int priority = 0;
309 if (getPriority_l(pid, &priority)) {
310 snprintf(buffer, SIZE, " Priority: %d\n", priority);
311 } else {
312 snprintf(buffer, SIZE, " Priority: <unknown>\n");
313 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700314 result.append(buffer);
315
Girish434b4d82023-07-11 23:24:54 +0000316 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700317 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000318 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700319 result.append(buffer);
320
Girish434b4d82023-07-11 23:24:54 +0000321 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700322 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700323 result.append(buffer);
324
Girish434b4d82023-07-11 23:24:54 +0000325 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700326 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700327 for (auto it = resources.begin(); it != resources.end(); it++) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000328 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700329 result.append(buffer);
330 }
331 }
332 }
Henry Fang32762922020-01-28 18:40:39 -0800333 result.append(" Process Pid override:\n");
334 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
335 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
336 it->first, it->second);
337 result.append(buffer);
338 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700339 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700340 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700341
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000342 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700343 return OK;
344}
345
Brian Lindahl64ee9452022-01-14 13:31:16 +0100346struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800347 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700348
Chong Zhangdd726802019-08-21 17:24:13 -0700349 virtual void noteStartVideo(int uid) override {
350 BatteryNotifier::getInstance().noteStartVideo(uid);
351 }
352 virtual void noteStopVideo(int uid) override {
353 BatteryNotifier::getInstance().noteStopVideo(uid);
354 }
355 virtual void noteResetVideo() override {
356 BatteryNotifier::getInstance().noteResetVideo();
357 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800358 virtual bool requestCpusetBoost(bool enable) override {
359 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700360 }
361
362protected:
363 virtual ~SystemCallbackImpl() {}
364
365private:
366 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800367 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700368};
369
370ResourceManagerService::ResourceManagerService()
371 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
372
Brian Lindahl64ee9452022-01-14 13:31:16 +0100373ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700374 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700375 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700376 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700377 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700378 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700379 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800380 mCpuBoostCount(0),
Girishf1d166c2023-07-20 22:35:29 +0000381 mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient(
382 AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback))) {
Chong Zhangdd726802019-08-21 17:24:13 -0700383 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000384 // Create ResourceManagerMetrics that handles all the metrics.
385 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700386}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700387
Chong Zhangfdd512a2019-11-22 11:03:14 -0800388//static
389void ResourceManagerService::instantiate() {
390 std::shared_ptr<ResourceManagerService> service =
391 ::ndk::SharedRefBase::make<ResourceManagerService>();
392 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000393 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000394 service->asBinder().get(), getServiceName(),
395 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800396 if (status != STATUS_OK) {
397 return;
398 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700399
400 std::shared_ptr<ResourceObserverService> observerService =
401 ResourceObserverService::instantiate();
402
403 if (observerService != nullptr) {
404 service->setObserverService(observerService);
405 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800406 // TODO: mediaserver main() is already starting the thread pool,
407 // move this to mediaserver main() when other services in mediaserver
408 // are converted to ndk-platform aidl.
409 //ABinderProcess_startThreadPool();
410}
411
Ronghua Wu231c3d12015-03-11 15:10:32 -0700412ResourceManagerService::~ResourceManagerService() {}
413
Chong Zhanga9d45c72020-09-09 12:41:17 -0700414void ResourceManagerService::setObserverService(
415 const std::shared_ptr<ResourceObserverService>& observerService) {
416 mObserverService = observerService;
417}
418
Chong Zhang181e6952019-10-09 13:23:39 -0700419Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000420 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700421 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700422
Girish434b4d82023-07-11 23:24:54 +0000423 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700424 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700425 const std::string &type = policies[i].type;
426 const std::string &value = policies[i].value;
427 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700428 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700429 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700430 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700431 }
432 }
Chong Zhang181e6952019-10-09 13:23:39 -0700433 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700434}
435
Brian Lindahl64ee9452022-01-14 13:31:16 +0100436void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource,
437 const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700438 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700439 if (resource.type == MediaResource::Type::kCpuBoost
440 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700441 // Request it on every new instance of kCpuBoost, as the media.codec
442 // could have died, if we only do it the first time subsequent instances
443 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800444 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700445 ALOGW("couldn't request cpuset boost");
446 }
447 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700448 } else if (resource.type == MediaResource::Type::kBattery
449 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700450 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700451 }
452}
453
Brian Lindahl64ee9452022-01-14 13:31:16 +0100454void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource,
455 const ResourceInfo& clientInfo) {
Chong Zhang181e6952019-10-09 13:23:39 -0700456 if (resource.type == MediaResource::Type::kCpuBoost
457 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700458 && mCpuBoostCount > 0) {
459 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800460 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700461 }
Chong Zhang181e6952019-10-09 13:23:39 -0700462 } else if (resource.type == MediaResource::Type::kBattery
463 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700464 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700465 }
466}
467
Brian Lindahl64ee9452022-01-14 13:31:16 +0100468void ResourceManagerService::mergeResources(MediaResourceParcel& r1,
469 const MediaResourceParcel& r2) {
Chong Zhang181e6952019-10-09 13:23:39 -0700470 // The resource entry on record is maintained to be in [0,INT64_MAX].
471 // Clamp if merging in the new resource value causes it to go out of bound.
472 // Note that the new resource value could be negative, eg.DrmSession, the
473 // value goes lower when the session is used more often. During reclaim
474 // the session with the highest value (lowest usage) would be closed.
475 if (r2.value < INT64_MAX - r1.value) {
476 r1.value += r2.value;
477 if (r1.value < 0) {
478 r1.value = 0;
479 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700480 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700481 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700482 }
483}
484
Girish9128e242022-11-23 20:52:29 +0000485Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800486 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700487 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000488 int32_t pid = clientInfo.pid;
489 int32_t uid = clientInfo.uid;
490 int64_t clientId = clientInfo.id;
491 const std::string& name = clientInfo.name;
492 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000493 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700494 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700495
Girish434b4d82023-07-11 23:24:54 +0000496 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100497 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800498 pid_t callingPid = IPCThreadState::self()->getCallingPid();
499 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100500 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
501 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800502 pid = callingPid;
503 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800504 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700505 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish9128e242022-11-23 20:52:29 +0000506 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700507 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700508
509 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700510 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700511 const auto resType = std::tuple(res.type, res.subType, res.id);
512
513 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
514 ALOGW("Ignoring request to remove negative value of non-drm resource");
515 continue;
516 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700517 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700518 if (res.value <= 0) {
519 // We can't init a new entry with negative value, although it's allowed
520 // to merge in negative values after the initial add.
521 ALOGW("Ignoring request to add new resource entry with value <= 0");
522 continue;
523 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700524 onFirstAdded(res, info);
525 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700526 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700527 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700528 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700529 // Add it to the list of added resources for observers.
530 auto it = resourceAdded.find(resType);
531 if (it == resourceAdded.end()) {
532 resourceAdded[resType] = res;
533 } else {
534 mergeResources(it->second, res);
535 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700536 }
Girishf1d166c2023-07-20 22:35:29 +0000537 if (info.deathNotifier == nullptr && client != nullptr) {
538 info.deathNotifier = std::make_shared<DeathNotifier>(
539 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900540 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700541 if (mObserverService != nullptr && !resourceAdded.empty()) {
542 mObserverService->onResourceAdded(uid, pid, resourceAdded);
543 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900544 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000545
Chong Zhang181e6952019-10-09 13:23:39 -0700546 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700547}
548
Girish9128e242022-11-23 20:52:29 +0000549Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700550 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000551 int32_t pid = clientInfo.pid;
552 int32_t uid = clientInfo.uid;
553 int64_t clientId = clientInfo.id;
554 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000555 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700556 mServiceLog->add(log);
557
Girish434b4d82023-07-11 23:24:54 +0000558 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100559 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800560 pid_t callingPid = IPCThreadState::self()->getCallingPid();
561 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
562 pid, callingPid);
563 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700564 }
Girish434b4d82023-07-11 23:24:54 +0000565 PidResourceInfosMap::iterator found = mMap.find(pid);
566 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700567 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700568 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700569 }
Girish434b4d82023-07-11 23:24:54 +0000570 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700571
Girish434b4d82023-07-11 23:24:54 +0000572 ResourceInfos::iterator foundClient = infos.find(clientId);
573 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700574 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700575 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700576 }
577
Girish434b4d82023-07-11 23:24:54 +0000578 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700579 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700580 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700581 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700582 const auto resType = std::tuple(res.type, res.subType, res.id);
583
584 if (res.value < 0) {
585 ALOGW("Ignoring request to remove negative value of resource");
586 continue;
587 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700588 // ignore if we don't have it
589 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700590 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700591 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700592 if (resource.value > res.value) {
593 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700594 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700595 onLastRemoved(res, info);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700596 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800597 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700598 }
599
600 // Add it to the list of removed resources for observers.
601 auto it = resourceRemoved.find(resType);
602 if (it == resourceRemoved.end()) {
603 resourceRemoved[resType] = actualRemoved;
604 } else {
605 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700606 }
607 }
608 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700609 if (mObserverService != nullptr && !resourceRemoved.empty()) {
610 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
611 }
Chong Zhang181e6952019-10-09 13:23:39 -0700612 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700613}
614
Girish9128e242022-11-23 20:52:29 +0000615Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
616 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700617 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900618}
619
Girish9128e242022-11-23 20:52:29 +0000620Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
621 int32_t pid = clientInfo.pid;
622 int32_t uid = clientInfo.uid;
623 int64_t clientId = clientInfo.id;
624 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
625 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700626 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700627
Girish434b4d82023-07-11 23:24:54 +0000628 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100629 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800630 pid_t callingPid = IPCThreadState::self()->getCallingPid();
631 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
632 pid, callingPid);
633 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800634 }
Girish434b4d82023-07-11 23:24:54 +0000635 PidResourceInfosMap::iterator found = mMap.find(pid);
636 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700637 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700638 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700639 }
Girish434b4d82023-07-11 23:24:54 +0000640 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700641
Girish434b4d82023-07-11 23:24:54 +0000642 ResourceInfos::iterator foundClient = infos.find(clientId);
643 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700644 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700645 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700646 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700647
Girish434b4d82023-07-11 23:24:54 +0000648 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700649 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
650 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700651 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700652
Girish1f002cf2023-02-17 00:36:29 +0000653 // Since this client has been removed, update the metrics collector.
654 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000655
Chong Zhanga9d45c72020-09-09 12:41:17 -0700656 if (mObserverService != nullptr && !info.resources.empty()) {
657 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
658 }
659
Girish434b4d82023-07-11 23:24:54 +0000660 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700661 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700662}
663
Girish9128e242022-11-23 20:52:29 +0000664void ResourceManagerService::getClientForResource_l(int callingPid,
665 const MediaResourceParcel *res,
666 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +0000667 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700668 if (res == NULL) {
669 return;
670 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800671 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000672 if (getLowestPriorityBiggestClient_l(callingPid, res->type, res->subType, idVector, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700673 clients->push_back(client);
674 }
675}
676
Girish9128e242022-11-23 20:52:29 +0000677Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100678 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000679 int32_t callingPid = clientInfo.pid;
680 std::string clientName = clientInfo.name;
681 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000682 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700683 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700684 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700685
Girish434b4d82023-07-11 23:24:54 +0000686 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Girish9128e242022-11-23 20:52:29 +0000687 PidUidVector idVector;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700688 {
Girish434b4d82023-07-11 23:24:54 +0000689 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100690 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800691 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
692 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
693 callingPid, actualCallingPid);
694 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800695 }
Chong Zhang181e6952019-10-09 13:23:39 -0700696 const MediaResourceParcel *secureCodec = NULL;
697 const MediaResourceParcel *nonSecureCodec = NULL;
698 const MediaResourceParcel *graphicMemory = NULL;
699 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700700 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100701 switch (resources[i].type) {
702 case MediaResource::Type::kSecureCodec:
703 secureCodec = &resources[i];
704 break;
705 case MediaResource::Type::kNonSecureCodec:
706 nonSecureCodec = &resources[i];
707 break;
708 case MediaResource::Type::kGraphicMemory:
709 graphicMemory = &resources[i];
710 break;
711 case MediaResource::Type::kDrmSession:
712 drmSession = &resources[i];
713 break;
714 default:
715 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700716 }
717 }
718
719 // first pass to handle secure/non-secure codec conflict
720 if (secureCodec != NULL) {
721 if (!mSupportsMultipleSecureCodecs) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100722 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000723 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700724 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700725 }
726 }
727 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100728 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000729 secureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700730 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700731 }
732 }
733 }
734 if (nonSecureCodec != NULL) {
735 if (!mSupportsSecureWithNonSecureCodec) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100736 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec,
Girish9128e242022-11-23 20:52:29 +0000737 nonSecureCodec->subType, &idVector, &clients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700738 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700739 }
740 }
741 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700742 if (drmSession != NULL) {
Girish9128e242022-11-23 20:52:29 +0000743 getClientForResource_l(callingPid, drmSession, &idVector, &clients);
Robert Shihc3af31b2019-09-20 21:45:01 -0700744 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700745 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700746 }
747 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700748
749 if (clients.size() == 0) {
750 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish9128e242022-11-23 20:52:29 +0000751 getClientForResource_l(callingPid, graphicMemory, &idVector, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700752 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700753
754 if (clients.size() == 0) {
755 // if we are here, run the third pass to free one codec with the same type.
Girish9128e242022-11-23 20:52:29 +0000756 getClientForResource_l(callingPid, secureCodec, &idVector, &clients);
757 getClientForResource_l(callingPid, nonSecureCodec, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700758 }
759
760 if (clients.size() == 0) {
761 // if we are here, run the fourth pass to free one codec with the different type.
762 if (secureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700763 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000764 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700765 }
766 if (nonSecureCodec != NULL) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700767 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish9128e242022-11-23 20:52:29 +0000768 getClientForResource_l(callingPid, &temp, &idVector, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700769 }
770 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700771 }
772
Brian Lindahl64ee9452022-01-14 13:31:16 +0100773 *_aidl_return = reclaimUnconditionallyFrom(clients);
Girish9128e242022-11-23 20:52:29 +0000774
775 // Log Reclaim Pushed Atom to statsd
776 pushReclaimAtom(clientInfo, clients, idVector, *_aidl_return);
777
Wonsik Kim271429d2020-10-01 10:12:56 -0700778 return Status::ok();
779}
780
Girish9128e242022-11-23 20:52:29 +0000781void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish434b4d82023-07-11 23:24:54 +0000782 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish9128e242022-11-23 20:52:29 +0000783 const PidUidVector& idVector, bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000784 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000785 int requesterPriority = -1;
786 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000787 std::vector<int> priorities;
788 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000789
Girish1f002cf2023-02-17 00:36:29 +0000790 for (PidUidVector::const_reference id : idVector) {
Girish9128e242022-11-23 20:52:29 +0000791 int targetPriority = -1;
792 getPriority_l(id.first, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000793 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000794 }
Girish1f002cf2023-02-17 00:36:29 +0000795 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, clients,
796 idVector, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000797}
798
Brian Lindahl64ee9452022-01-14 13:31:16 +0100799bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish434b4d82023-07-11 23:24:54 +0000800 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700801 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700802 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700803 }
804
Chong Zhangfdd512a2019-11-22 11:03:14 -0800805 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700806 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700807 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700808 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700809 bool success;
810 Status status = clients[i]->reclaimResource(&success);
811 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700812 failedClient = clients[i];
813 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700814 }
815 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700816
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700817 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700818 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700819 }
820
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200821 int failedClientPid = -1;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700822 {
Girish434b4d82023-07-11 23:24:54 +0000823 std::scoped_lock lock{mLock};
Ronghua Wu67e7f542015-03-13 10:47:08 -0700824 bool found = false;
Girish434b4d82023-07-11 23:24:54 +0000825 for (auto& [pid, infos] : mMap) {
826 for (const auto& [id, info] : infos) {
827 if (info.client == failedClient) {
828 infos.erase(id);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700829 found = true;
Girish434b4d82023-07-11 23:24:54 +0000830 break;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700831 }
832 }
833 if (found) {
Girish434b4d82023-07-11 23:24:54 +0000834 failedClientPid = pid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700835 break;
836 }
837 }
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200838 if (found) {
839 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
840 } else {
841 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700842 }
843 }
844
Wonsik Kim271429d2020-10-01 10:12:56 -0700845 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700846}
847
Brian Lindahl64ee9452022-01-14 13:31:16 +0100848Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800849 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
850 originalPid, newPid);
851 mServiceLog->add(log);
852
853 // allow if this is called from the same process or the process has
854 // permission.
855 if ((AIBinder_getCallingPid() != getpid()) &&
856 (checkCallingPermission(String16(
857 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
858 ALOGE(
859 "Permission Denial: can't access overridePid method from pid=%d, "
860 "self pid=%d\n",
861 AIBinder_getCallingPid(), getpid());
862 return Status::fromServiceSpecificError(PERMISSION_DENIED);
863 }
864
865 {
Girish434b4d82023-07-11 23:24:54 +0000866 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800867 mOverridePidMap.erase(originalPid);
868 if (newPid != -1) {
869 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000870 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800871 }
872 }
873
874 return Status::ok();
875}
876
Chong Zhang97d367b2020-09-16 12:53:14 -0700877Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100878 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700879 int oomScore) {
880 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
881 pid, procState, oomScore);
882 mServiceLog->add(log);
883
884 // Only allow the override if the caller already can access process state and oom scores.
885 int callingPid = AIBinder_getCallingPid();
886 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
887 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
888 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
889 return Status::fromServiceSpecificError(PERMISSION_DENIED);
890 }
891
892 if (client == nullptr) {
893 return Status::fromServiceSpecificError(BAD_VALUE);
894 }
895
Girish434b4d82023-07-11 23:24:54 +0000896 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700897 removeProcessInfoOverride_l(pid);
898
899 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
900 // Override value is rejected by ProcessInfo.
901 return Status::fromServiceSpecificError(BAD_VALUE);
902 }
903
Girish1f002cf2023-02-17 00:36:29 +0000904 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
905 .uid = 0,
906 .id = 0,
907 .name = "<unknown client>"};
Girishf1d166c2023-07-20 22:35:29 +0000908 auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
909 client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
Chong Zhang97d367b2020-09-16 12:53:14 -0700910
Girishf1d166c2023-07-20 22:35:29 +0000911 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700912
913 return Status::ok();
914}
915
Chong Zhang97d367b2020-09-16 12:53:14 -0700916void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000917 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700918
919 removeProcessInfoOverride_l(pid);
920}
921
922void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
923 auto it = mProcessInfoOverrideMap.find(pid);
924 if (it == mProcessInfoOverrideMap.end()) {
925 return;
926 }
927
928 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700929 mProcessInfoOverrideMap.erase(pid);
930}
931
Girish9128e242022-11-23 20:52:29 +0000932Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
933 int32_t pid = clientInfo.pid;
934 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700935 String8 log = String8::format(
936 "markClientForPendingRemoval(pid %d, clientId %lld)",
937 pid, (long long) clientId);
938 mServiceLog->add(log);
939
Girish434b4d82023-07-11 23:24:54 +0000940 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100941 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800942 pid_t callingPid = IPCThreadState::self()->getCallingPid();
943 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
944 pid, callingPid);
945 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700946 }
Girish434b4d82023-07-11 23:24:54 +0000947 PidResourceInfosMap::iterator found = mMap.find(pid);
948 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700949 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
950 pid, (long long)clientId);
951 return Status::ok();
952 }
Girish434b4d82023-07-11 23:24:54 +0000953 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700954
Girish434b4d82023-07-11 23:24:54 +0000955 ResourceInfos::iterator foundClient = infos.find(clientId);
956 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700957 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
958 return Status::ok();
959 }
960
Girish434b4d82023-07-11 23:24:54 +0000961 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700962 info.pendingRemoval = true;
963 return Status::ok();
964}
965
Wonsik Kim271429d2020-10-01 10:12:56 -0700966Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
967 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
968 mServiceLog->add(log);
969
Girish434b4d82023-07-11 23:24:54 +0000970 std::vector<std::shared_ptr<IResourceManagerClient>> clients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700971 {
Girish434b4d82023-07-11 23:24:54 +0000972 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100973 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800974 pid_t callingPid = IPCThreadState::self()->getCallingPid();
975 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
976 pid, callingPid);
977 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700978 }
979
980 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
981 MediaResource::Type::kNonSecureCodec,
982 MediaResource::Type::kGraphicMemory,
983 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100984 switch (type) {
985 // Codec resources are segregated by audio, video and image domains.
986 case MediaResource::Type::kSecureCodec:
987 case MediaResource::Type::kNonSecureCodec:
988 for (MediaResource::SubType subType : {MediaResource::SubType::kAudioCodec,
989 MediaResource::SubType::kVideoCodec,
990 MediaResource::SubType::kImageCodec}) {
991 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +0000992 uid_t uid = 0;
993 if (getBiggestClientPendingRemoval_l(pid, type, subType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +0000994 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100995 continue;
996 }
997 }
998 break;
999 // Non-codec resources are shared by audio, video and image codecs (no subtype).
1000 default:
1001 std::shared_ptr<IResourceManagerClient> client;
Girish9128e242022-11-23 20:52:29 +00001002 uid_t uid = 0;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001003 if (getBiggestClientPendingRemoval_l(pid, type,
Girish9128e242022-11-23 20:52:29 +00001004 MediaResource::SubType::kUnspecifiedSubType, uid, &client)) {
Girish434b4d82023-07-11 23:24:54 +00001005 clients.push_back(client);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001006 }
1007 break;
Wonsik Kim271429d2020-10-01 10:12:56 -07001008 }
1009 }
1010 }
1011
1012 if (!clients.empty()) {
Brian Lindahl64ee9452022-01-14 13:31:16 +01001013 reclaimUnconditionallyFrom(clients);
Wonsik Kim271429d2020-10-01 10:12:56 -07001014 }
1015 return Status::ok();
1016}
1017
Henry Fang32762922020-01-28 18:40:39 -08001018bool ResourceManagerService::getPriority_l(int pid, int* priority) {
1019 int newPid = pid;
1020
1021 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
1022 newPid = mOverridePidMap[pid];
1023 ALOGD("getPriority_l: use override pid %d instead original pid %d",
1024 newPid, pid);
1025 }
1026
1027 return mProcessInfo->getPriority(newPid, priority);
1028}
1029
Brian Lindahl64ee9452022-01-14 13:31:16 +01001030bool ResourceManagerService::getAllClients_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001031 MediaResource::SubType subType,
1032 PidUidVector* idVector,
Girish434b4d82023-07-11 23:24:54 +00001033 std::vector<std::shared_ptr<IResourceManagerClient>>* clients) {
1034 std::vector<std::shared_ptr<IResourceManagerClient>> temp;
Girish9128e242022-11-23 20:52:29 +00001035 PidUidVector tempIdList;
1036
Girish434b4d82023-07-11 23:24:54 +00001037 for (auto& [pid, infos] : mMap) {
1038 for (const auto& [id, info] : infos) {
1039 if (hasResourceType(type, subType, info.resources)) {
1040 if (!isCallingPriorityHigher_l(callingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001041 // some higher/equal priority process owns the resource,
1042 // this request can't be fulfilled.
1043 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Girish434b4d82023-07-11 23:24:54 +00001044 asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001045 return false;
1046 }
Girish434b4d82023-07-11 23:24:54 +00001047 temp.push_back(info.client);
1048 tempIdList.emplace_back(pid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001049 }
1050 }
1051 }
1052 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -08001053 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001054 return true;
1055 }
Girish434b4d82023-07-11 23:24:54 +00001056
1057 clients->insert(std::end(*clients), std::begin(temp), std::end(temp));
Girish9128e242022-11-23 20:52:29 +00001058 idVector->insert(std::end(*idVector), std::begin(tempIdList), std::end(tempIdList));
Ronghua Wu231c3d12015-03-11 15:10:32 -07001059 return true;
1060}
1061
Brian Lindahl64ee9452022-01-14 13:31:16 +01001062bool ResourceManagerService::getLowestPriorityBiggestClient_l(int callingPid,
Girish9128e242022-11-23 20:52:29 +00001063 MediaResource::Type type,
1064 MediaResource::SubType subType,
1065 PidUidVector* idVector,
Chong Zhangfdd512a2019-11-22 11:03:14 -08001066 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001067 int lowestPriorityPid;
1068 int lowestPriority;
1069 int callingPriority;
Girish9128e242022-11-23 20:52:29 +00001070 uid_t uid = 0;
Wonsik Kimd20e9362020-04-28 10:42:57 -07001071
1072 // Before looking into other processes, check if we have clients marked for
1073 // pending removal in the same process.
Girish9128e242022-11-23 20:52:29 +00001074 if (getBiggestClientPendingRemoval_l(callingPid, type, subType, uid, client)) {
1075 idVector->emplace_back(callingPid, uid);
Wonsik Kimd20e9362020-04-28 10:42:57 -07001076 return true;
1077 }
Henry Fang32762922020-01-28 18:40:39 -08001078 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001079 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
1080 callingPid);
1081 return false;
1082 }
Brian Lindahl64ee9452022-01-14 13:31:16 +01001083 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001084 return false;
1085 }
1086 if (lowestPriority <= callingPriority) {
1087 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
1088 lowestPriority, callingPriority);
1089 return false;
1090 }
1091
Girish9128e242022-11-23 20:52:29 +00001092 if (!getBiggestClient_l(lowestPriorityPid, type, subType, uid, client)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001093 return false;
1094 }
Girish9128e242022-11-23 20:52:29 +00001095
1096 idVector->emplace_back(lowestPriorityPid, uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001097 return true;
1098}
1099
Brian Lindahl64ee9452022-01-14 13:31:16 +01001100bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
1101 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001102 int pid = -1;
1103 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +00001104 for (auto& [tempPid, infos] : mMap) {
1105 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001106 // no client on this process.
1107 continue;
1108 }
Girish434b4d82023-07-11 23:24:54 +00001109 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001110 // doesn't have the requested resource type
1111 continue;
1112 }
Girish434b4d82023-07-11 23:24:54 +00001113 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -08001114 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001115 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
1116 // TODO: remove this pid from mMap?
1117 continue;
1118 }
1119 if (pid == -1 || tempPriority > priority) {
1120 // initial the value
1121 pid = tempPid;
1122 priority = tempPriority;
1123 }
1124 }
1125 if (pid != -1) {
1126 *lowestPriorityPid = pid;
1127 *lowestPriority = priority;
1128 }
1129 return (pid != -1);
1130}
1131
1132bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
1133 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -08001134 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001135 return false;
1136 }
1137
1138 int priority;
Henry Fang32762922020-01-28 18:40:39 -08001139 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -07001140 return false;
1141 }
1142
1143 return (callingPidPriority < priority);
1144}
1145
Brian Lindahl64ee9452022-01-14 13:31:16 +01001146bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001147 MediaResource::SubType subType, uid_t& uid,
1148 std::shared_ptr<IResourceManagerClient> *client) {
1149 return getBiggestClient_l(pid, type, subType, uid, client, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +01001150}
1151
1152bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +00001153 MediaResource::SubType subType, uid_t& uid,
1154 std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -07001155 bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001156 PidResourceInfosMap::iterator found = mMap.find(pid);
1157 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001158 ALOGE_IF(!pendingRemovalOnly,
1159 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001160 return false;
1161 }
1162
Chong Zhangfdd512a2019-11-22 11:03:14 -08001163 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001164 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001165 const ResourceInfos& infos = found->second;
1166 for (const auto& [id, info] : infos) {
1167 const ResourceList& resources = info.resources;
1168 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001169 continue;
1170 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001171 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001172 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001173 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001174 if (resource.value > largestValue) {
1175 largestValue = resource.value;
Girish434b4d82023-07-11 23:24:54 +00001176 clientTemp = info.client;
1177 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001178 }
1179 }
1180 }
1181 }
1182
1183 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001184 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001185 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1186 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001187 return false;
1188 }
1189
1190 *client = clientTemp;
1191 return true;
1192}
1193
Girish1f002cf2023-02-17 00:36:29 +00001194Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1195 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1196 return Status::ok();
1197}
1198
1199Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1200 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1201 return Status::ok();
1202}
1203
1204Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1205 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1206 return Status::ok();
1207}
1208
Girishde8eb592023-04-13 18:49:17 +00001209Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1210 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1211 return Status::ok();
1212}
1213
Girish1f002cf2023-02-17 00:36:29 +00001214long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1215 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1216}
1217
1218long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1219 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1220}
1221
Ronghua Wu231c3d12015-03-11 15:10:32 -07001222} // namespace android