blob: 16952283e5d6bc0b752c9651f7c9ef829408d4c5 [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>
Steven Morelandeb009aa2021-04-02 00:31:10 +000024#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>
Ronghua Wu231c3d12015-03-11 15:10:32 -070029#include <media/stagefright/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070030#include <mediautils/BatteryNotifier.h>
31#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070032#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <unistd.h>
37
Steven Moreland0a0ff0b2021-04-02 00:06:01 +000038#include "IMediaResourceMonitor.h"
Ronghua Wu231c3d12015-03-11 15:10:32 -070039#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070040#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070041
Ronghua Wu231c3d12015-03-11 15:10:32 -070042namespace android {
43
Chong Zhangfdd512a2019-11-22 11:03:14 -080044DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
45 int pid, int64_t clientId)
46 : mService(service), mPid(pid), mClientId(clientId) {}
Wonsik Kim3e378962017-01-05 17:00:02 +090047
Chong Zhangfdd512a2019-11-22 11:03:14 -080048//static
49void DeathNotifier::BinderDiedCallback(void* cookie) {
50 auto thiz = static_cast<DeathNotifier*>(cookie);
51 thiz->binderDied();
52}
Wonsik Kim3e378962017-01-05 17:00:02 +090053
Chong Zhangfdd512a2019-11-22 11:03:14 -080054void DeathNotifier::binderDied() {
55 // Don't check for pid validity since we know it's already dead.
56 std::shared_ptr<ResourceManagerService> service = mService.lock();
57 if (service == nullptr) {
58 ALOGW("ResourceManagerService is dead as well.");
59 return;
Wonsik Kim3e378962017-01-05 17:00:02 +090060 }
Henry Fang32762922020-01-28 18:40:39 -080061
62 service->overridePid(mPid, -1);
Henry Fangbf0d7ca2020-06-29 13:11:39 -070063 // thiz is freed in the call below, so it must be last call referring thiz
64 service->removeResource(mPid, mClientId, false);
65
Chong Zhangfdd512a2019-11-22 11:03:14 -080066}
Wonsik Kim3e378962017-01-05 17:00:02 +090067
Ronghua Wu231c3d12015-03-11 15:10:32 -070068template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070069static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 String8 itemsStr;
71 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070072 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070073 }
74 return itemsStr;
75}
76
Chong Zhangfb092d32019-08-12 09:45:44 -070077static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
78 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070079 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070080 return true;
81 }
82 }
83 return false;
84}
85
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070086static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070087 for (size_t i = 0; i < infos.size(); ++i) {
88 if (hasResourceType(type, infos[i].resources)) {
89 return true;
90 }
91 }
92 return false;
93}
94
95static ResourceInfos& getResourceInfosForEdit(
96 int pid,
97 PidResourceInfosMap& map) {
98 ssize_t index = map.indexOfKey(pid);
99 if (index < 0) {
100 // new pid
101 ResourceInfos infosForPid;
102 map.add(pid, infosForPid);
103 }
104
105 return map.editValueFor(pid);
106}
107
108static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700109 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700110 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800111 const std::shared_ptr<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700112 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700113 ssize_t index = infos.indexOfKey(clientId);
114
115 if (index < 0) {
116 ResourceInfo info;
117 info.uid = uid;
118 info.clientId = clientId;
119 info.client = client;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700120 info.pendingRemoval = false;
Chong Zhangfb092d32019-08-12 09:45:44 -0700121
122 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700124
125 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126}
127
Chong Zhang181e6952019-10-09 13:23:39 -0700128static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900129 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700130 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900131 if (binder != NULL) {
132 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
133 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700134 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700135 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700136 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700137 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
138 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900139 }
140 }
141}
142
Chong Zhangfdd512a2019-11-22 11:03:14 -0800143binder_status_t ResourceManagerService::dump(
144 int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700145 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700146
dcashman014e91e2015-09-11 09:33:01 -0700147 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
148 result.format("Permission Denial: "
149 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800150 AIBinder_getCallingPid(),
151 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700152 write(fd, result.string(), result.size());
153 return PERMISSION_DENIED;
154 }
155
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700156 PidResourceInfosMap mapCopy;
157 bool supportsMultipleSecureCodecs;
158 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800159 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700160 String8 serviceLog;
161 {
162 Mutex::Autolock lock(mLock);
163 mapCopy = mMap; // Shadow copy, real copy will happen on write.
164 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
165 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
166 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800167 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700168 }
169
170 const size_t SIZE = 256;
171 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700172 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
173 result.append(buffer);
174 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700175 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700177 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
178 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700179 result.append(buffer);
180
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700181 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700182 for (size_t i = 0; i < mapCopy.size(); ++i) {
183 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700184 result.append(buffer);
185
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700186 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700187 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700188 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700189 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
190 result.append(buffer);
191
Chong Zhang181e6952019-10-09 13:23:39 -0700192 std::string clientName;
193 Status status = infos[j].client->getName(&clientName);
194 if (!status.isOk()) {
195 clientName = "<unknown client>";
196 }
197 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700198 result.append(buffer);
199
Chong Zhangfb092d32019-08-12 09:45:44 -0700200 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700201 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700202 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700203 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700204 result.append(buffer);
205 }
206 }
207 }
Henry Fang32762922020-01-28 18:40:39 -0800208 result.append(" Process Pid override:\n");
209 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
210 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
211 it->first, it->second);
212 result.append(buffer);
213 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700214 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700215 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700216
217 write(fd, result.string(), result.size());
218 return OK;
219}
220
Chong Zhangdd726802019-08-21 17:24:13 -0700221struct SystemCallbackImpl :
222 public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800223 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700224
Chong Zhangdd726802019-08-21 17:24:13 -0700225 virtual void noteStartVideo(int uid) override {
226 BatteryNotifier::getInstance().noteStartVideo(uid);
227 }
228 virtual void noteStopVideo(int uid) override {
229 BatteryNotifier::getInstance().noteStopVideo(uid);
230 }
231 virtual void noteResetVideo() override {
232 BatteryNotifier::getInstance().noteResetVideo();
233 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800234 virtual bool requestCpusetBoost(bool enable) override {
235 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700236 }
237
238protected:
239 virtual ~SystemCallbackImpl() {}
240
241private:
242 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800243 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700244};
245
246ResourceManagerService::ResourceManagerService()
247 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
248
249ResourceManagerService::ResourceManagerService(
250 const sp<ProcessInfoInterface> &processInfo,
251 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700253 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700254 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700255 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700256 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800257 mCpuBoostCount(0),
258 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700259 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700260}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700261
Chong Zhangfdd512a2019-11-22 11:03:14 -0800262//static
263void ResourceManagerService::instantiate() {
264 std::shared_ptr<ResourceManagerService> service =
265 ::ndk::SharedRefBase::make<ResourceManagerService>();
266 binder_status_t status =
267 AServiceManager_addService(service->asBinder().get(), getServiceName());
268 if (status != STATUS_OK) {
269 return;
270 }
271 // TODO: mediaserver main() is already starting the thread pool,
272 // move this to mediaserver main() when other services in mediaserver
273 // are converted to ndk-platform aidl.
274 //ABinderProcess_startThreadPool();
275}
276
Ronghua Wu231c3d12015-03-11 15:10:32 -0700277ResourceManagerService::~ResourceManagerService() {}
278
Chong Zhang181e6952019-10-09 13:23:39 -0700279Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700280 String8 log = String8::format("config(%s)", getString(policies).string());
281 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700282
283 Mutex::Autolock lock(mLock);
284 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700285 const std::string &type = policies[i].type;
286 const std::string &value = policies[i].value;
287 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700288 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700289 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700290 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700291 }
292 }
Chong Zhang181e6952019-10-09 13:23:39 -0700293 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700294}
295
Chong Zhangfb092d32019-08-12 09:45:44 -0700296void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700297 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700298 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700299 if (resource.type == MediaResource::Type::kCpuBoost
300 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700301 // Request it on every new instance of kCpuBoost, as the media.codec
302 // could have died, if we only do it the first time subsequent instances
303 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800304 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700305 ALOGW("couldn't request cpuset boost");
306 }
307 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700308 } else if (resource.type == MediaResource::Type::kBattery
309 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700310 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700311 }
312}
313
314void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700315 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
316 if (resource.type == MediaResource::Type::kCpuBoost
317 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700318 && mCpuBoostCount > 0) {
319 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800320 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700321 }
Chong Zhang181e6952019-10-09 13:23:39 -0700322 } else if (resource.type == MediaResource::Type::kBattery
323 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700324 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700325 }
326}
327
Robert Shihc3af31b2019-09-20 21:45:01 -0700328void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700329 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
330 // The resource entry on record is maintained to be in [0,INT64_MAX].
331 // Clamp if merging in the new resource value causes it to go out of bound.
332 // Note that the new resource value could be negative, eg.DrmSession, the
333 // value goes lower when the session is used more often. During reclaim
334 // the session with the highest value (lowest usage) would be closed.
335 if (r2.value < INT64_MAX - r1.value) {
336 r1.value += r2.value;
337 if (r1.value < 0) {
338 r1.value = 0;
339 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700340 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700341 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700342 }
343}
344
Chong Zhang181e6952019-10-09 13:23:39 -0700345Status ResourceManagerService::addResource(
346 int32_t pid,
347 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700348 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800349 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700350 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700351 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700352 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700353 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700354
355 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800356 if (!mProcessInfo->isValidPid(pid)) {
357 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700358 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800359 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700360 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700361 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700362
363 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700364 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700365 const auto resType = std::tuple(res.type, res.subType, res.id);
366
367 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
368 ALOGW("Ignoring request to remove negative value of non-drm resource");
369 continue;
370 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700371 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700372 if (res.value <= 0) {
373 // We can't init a new entry with negative value, although it's allowed
374 // to merge in negative values after the initial add.
375 ALOGW("Ignoring request to add new resource entry with value <= 0");
376 continue;
377 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700378 onFirstAdded(res, info);
379 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700380 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700381 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700382 }
383 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700384 if (info.deathNotifier == nullptr && client != nullptr) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800385 info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId);
386 AIBinder_linkToDeath(client->asBinder().get(),
387 mDeathRecipient.get(), info.deathNotifier.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900388 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900389 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700390 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700391}
392
Chong Zhang181e6952019-10-09 13:23:39 -0700393Status ResourceManagerService::removeResource(
394 int32_t pid, int64_t clientId,
395 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700396 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
397 pid, (long long) clientId, getString(resources).string());
398 mServiceLog->add(log);
399
400 Mutex::Autolock lock(mLock);
401 if (!mProcessInfo->isValidPid(pid)) {
402 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700403 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700404 }
405 ssize_t index = mMap.indexOfKey(pid);
406 if (index < 0) {
407 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700408 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700409 }
410 ResourceInfos &infos = mMap.editValueAt(index);
411
412 index = infos.indexOfKey(clientId);
413 if (index < 0) {
414 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700415 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700416 }
417
418 ResourceInfo &info = infos.editValueAt(index);
419
420 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700421 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700422 const auto resType = std::tuple(res.type, res.subType, res.id);
423
424 if (res.value < 0) {
425 ALOGW("Ignoring request to remove negative value of resource");
426 continue;
427 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700428 // ignore if we don't have it
429 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700430 MediaResourceParcel &resource = info.resources[resType];
431 if (resource.value > res.value) {
432 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700433 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700434 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700435 info.resources.erase(resType);
436 }
437 }
438 }
Chong Zhang181e6952019-10-09 13:23:39 -0700439 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700440}
441
Chong Zhang181e6952019-10-09 13:23:39 -0700442Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900443 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700444 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900445}
446
Chong Zhang181e6952019-10-09 13:23:39 -0700447Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700448 String8 log = String8::format(
449 "removeResource(pid %d, clientId %lld)",
450 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700451 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700452
453 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900454 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800455 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700456 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800457 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700458 ssize_t index = mMap.indexOfKey(pid);
459 if (index < 0) {
460 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700461 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700462 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700463 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700464
465 index = infos.indexOfKey(clientId);
466 if (index < 0) {
467 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700468 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700469 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700470
471 const ResourceInfo &info = infos[index];
472 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
473 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700474 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700475
Chong Zhangfdd512a2019-11-22 11:03:14 -0800476 AIBinder_unlinkToDeath(info.client->asBinder().get(),
477 mDeathRecipient.get(), info.deathNotifier.get());
Chong Zhangfb092d32019-08-12 09:45:44 -0700478
479 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700480 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700481}
482
Ronghua Wu05d89f12015-07-07 16:47:42 -0700483void ResourceManagerService::getClientForResource_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800484 int callingPid, const MediaResourceParcel *res,
485 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700486 if (res == NULL) {
487 return;
488 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800489 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700490 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700491 clients->push_back(client);
492 }
493}
494
Chong Zhang181e6952019-10-09 13:23:39 -0700495Status ResourceManagerService::reclaimResource(
496 int32_t callingPid,
497 const std::vector<MediaResourceParcel>& resources,
498 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700499 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700500 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700501 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700502 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700503
Chong Zhangfdd512a2019-11-22 11:03:14 -0800504 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700505 {
506 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800507 if (!mProcessInfo->isValidPid(callingPid)) {
508 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700509 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800510 }
Chong Zhang181e6952019-10-09 13:23:39 -0700511 const MediaResourceParcel *secureCodec = NULL;
512 const MediaResourceParcel *nonSecureCodec = NULL;
513 const MediaResourceParcel *graphicMemory = NULL;
514 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700515 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700516 MediaResource::Type type = resources[i].type;
517 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700518 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700519 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700520 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700521 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700522 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700523 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700524 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700525 }
526 }
527
528 // first pass to handle secure/non-secure codec conflict
529 if (secureCodec != NULL) {
530 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700531 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
532 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700533 }
534 }
535 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700536 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
537 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700538 }
539 }
540 }
541 if (nonSecureCodec != NULL) {
542 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700543 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
544 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700545 }
546 }
547 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700548 if (drmSession != NULL) {
549 getClientForResource_l(callingPid, drmSession, &clients);
550 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700551 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700552 }
553 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700554
555 if (clients.size() == 0) {
556 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700557 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700558 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700559
560 if (clients.size() == 0) {
561 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700562 getClientForResource_l(callingPid, secureCodec, &clients);
563 getClientForResource_l(callingPid, nonSecureCodec, &clients);
564 }
565
566 if (clients.size() == 0) {
567 // if we are here, run the fourth pass to free one codec with the different type.
568 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700569 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700570 getClientForResource_l(callingPid, &temp, &clients);
571 }
572 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700573 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700574 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700575 }
576 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700577 }
578
Wonsik Kim271429d2020-10-01 10:12:56 -0700579 *_aidl_return = reclaimInternal(clients);
580 return Status::ok();
581}
582
583bool ResourceManagerService::reclaimInternal(
584 const Vector<std::shared_ptr<IResourceManagerClient>> &clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700585 if (clients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700586 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700587 }
588
Chong Zhangfdd512a2019-11-22 11:03:14 -0800589 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700590 for (size_t i = 0; i < clients.size(); ++i) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700591 String8 log = String8::format("reclaimResource from client %p", clients[i].get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700592 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700593 bool success;
594 Status status = clients[i]->reclaimResource(&success);
595 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700596 failedClient = clients[i];
597 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700598 }
599 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700600
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700601 if (failedClient == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700602 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700603 }
604
Ronghua Wu67e7f542015-03-13 10:47:08 -0700605 {
606 Mutex::Autolock lock(mLock);
607 bool found = false;
608 for (size_t i = 0; i < mMap.size(); ++i) {
609 ResourceInfos &infos = mMap.editValueAt(i);
610 for (size_t j = 0; j < infos.size();) {
611 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700612 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700613 found = true;
614 } else {
615 ++j;
616 }
617 }
618 if (found) {
619 break;
620 }
621 }
622 if (!found) {
623 ALOGV("didn't find failed client");
624 }
625 }
626
Wonsik Kim271429d2020-10-01 10:12:56 -0700627 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700628}
629
Henry Fang32762922020-01-28 18:40:39 -0800630Status ResourceManagerService::overridePid(
631 int originalPid,
632 int newPid) {
633 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
634 originalPid, newPid);
635 mServiceLog->add(log);
636
637 // allow if this is called from the same process or the process has
638 // permission.
639 if ((AIBinder_getCallingPid() != getpid()) &&
640 (checkCallingPermission(String16(
641 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
642 ALOGE(
643 "Permission Denial: can't access overridePid method from pid=%d, "
644 "self pid=%d\n",
645 AIBinder_getCallingPid(), getpid());
646 return Status::fromServiceSpecificError(PERMISSION_DENIED);
647 }
648
649 {
650 Mutex::Autolock lock(mLock);
651 mOverridePidMap.erase(originalPid);
652 if (newPid != -1) {
653 mOverridePidMap.emplace(originalPid, newPid);
654 }
655 }
656
657 return Status::ok();
658}
659
Wonsik Kimd20e9362020-04-28 10:42:57 -0700660Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) {
661 String8 log = String8::format(
662 "markClientForPendingRemoval(pid %d, clientId %lld)",
663 pid, (long long) clientId);
664 mServiceLog->add(log);
665
666 Mutex::Autolock lock(mLock);
667 if (!mProcessInfo->isValidPid(pid)) {
668 ALOGE("Rejected markClientForPendingRemoval call with invalid pid.");
669 return Status::fromServiceSpecificError(BAD_VALUE);
670 }
671 ssize_t index = mMap.indexOfKey(pid);
672 if (index < 0) {
673 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
674 pid, (long long)clientId);
675 return Status::ok();
676 }
677 ResourceInfos &infos = mMap.editValueAt(index);
678
679 index = infos.indexOfKey(clientId);
680 if (index < 0) {
681 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
682 return Status::ok();
683 }
684
685 ResourceInfo &info = infos.editValueAt(index);
686 info.pendingRemoval = true;
687 return Status::ok();
688}
689
Wonsik Kim271429d2020-10-01 10:12:56 -0700690Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
691 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
692 mServiceLog->add(log);
693
694 Vector<std::shared_ptr<IResourceManagerClient>> clients;
695 {
696 Mutex::Autolock lock(mLock);
697 if (!mProcessInfo->isValidPid(pid)) {
698 ALOGE("Rejected reclaimResourcesFromClientsPendingRemoval call with invalid pid.");
699 return Status::fromServiceSpecificError(BAD_VALUE);
700 }
701
702 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
703 MediaResource::Type::kNonSecureCodec,
704 MediaResource::Type::kGraphicMemory,
705 MediaResource::Type::kDrmSession}) {
706 std::shared_ptr<IResourceManagerClient> client;
707 if (getBiggestClient_l(pid, type, &client, true /* pendingRemovalOnly */)) {
708 clients.add(client);
709 break;
710 }
711 }
712 }
713
714 if (!clients.empty()) {
715 reclaimInternal(clients);
716 }
717 return Status::ok();
718}
719
Henry Fang32762922020-01-28 18:40:39 -0800720bool ResourceManagerService::getPriority_l(int pid, int* priority) {
721 int newPid = pid;
722
723 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
724 newPid = mOverridePidMap[pid];
725 ALOGD("getPriority_l: use override pid %d instead original pid %d",
726 newPid, pid);
727 }
728
729 return mProcessInfo->getPriority(newPid, priority);
730}
731
Ronghua Wu231c3d12015-03-11 15:10:32 -0700732bool ResourceManagerService::getAllClients_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800733 int callingPid, MediaResource::Type type,
734 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
735 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700736 for (size_t i = 0; i < mMap.size(); ++i) {
737 ResourceInfos &infos = mMap.editValueAt(i);
738 for (size_t j = 0; j < infos.size(); ++j) {
739 if (hasResourceType(type, infos[j].resources)) {
740 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
741 // some higher/equal priority process owns the resource,
742 // this request can't be fulfilled.
743 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800744 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700745 return false;
746 }
747 temp.push_back(infos[j].client);
748 }
749 }
750 }
751 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800752 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700753 return true;
754 }
755 clients->appendVector(temp);
756 return true;
757}
758
759bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800760 int callingPid, MediaResource::Type type,
761 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700762 int lowestPriorityPid;
763 int lowestPriority;
764 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700765
766 // Before looking into other processes, check if we have clients marked for
767 // pending removal in the same process.
768 if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) {
769 return true;
770 }
Henry Fang32762922020-01-28 18:40:39 -0800771 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700772 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
773 callingPid);
774 return false;
775 }
776 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
777 return false;
778 }
779 if (lowestPriority <= callingPriority) {
780 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
781 lowestPriority, callingPriority);
782 return false;
783 }
784
785 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
786 return false;
787 }
788 return true;
789}
790
791bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800792 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700793 int pid = -1;
794 int priority = -1;
795 for (size_t i = 0; i < mMap.size(); ++i) {
796 if (mMap.valueAt(i).size() == 0) {
797 // no client on this process.
798 continue;
799 }
800 if (!hasResourceType(type, mMap.valueAt(i))) {
801 // doesn't have the requested resource type
802 continue;
803 }
804 int tempPid = mMap.keyAt(i);
805 int tempPriority;
Henry Fang32762922020-01-28 18:40:39 -0800806 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700807 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
808 // TODO: remove this pid from mMap?
809 continue;
810 }
811 if (pid == -1 || tempPriority > priority) {
812 // initial the value
813 pid = tempPid;
814 priority = tempPriority;
815 }
816 }
817 if (pid != -1) {
818 *lowestPriorityPid = pid;
819 *lowestPriority = priority;
820 }
821 return (pid != -1);
822}
823
824bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
825 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800826 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700827 return false;
828 }
829
830 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800831 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700832 return false;
833 }
834
835 return (callingPidPriority < priority);
836}
837
838bool ResourceManagerService::getBiggestClient_l(
Wonsik Kimd20e9362020-04-28 10:42:57 -0700839 int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client,
840 bool pendingRemovalOnly) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700841 ssize_t index = mMap.indexOfKey(pid);
842 if (index < 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700843 ALOGE_IF(!pendingRemovalOnly,
844 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700845 return false;
846 }
847
Chong Zhangfdd512a2019-11-22 11:03:14 -0800848 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700849 uint64_t largestValue = 0;
850 const ResourceInfos &infos = mMap.valueAt(index);
851 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700852 const ResourceList &resources = infos[i].resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700853 if (pendingRemovalOnly && !infos[i].pendingRemoval) {
854 continue;
855 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700856 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700857 const MediaResourceParcel &resource = it->second;
858 if (resource.type == type) {
859 if (resource.value > largestValue) {
860 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700861 clientTemp = infos[i].client;
862 }
863 }
864 }
865 }
866
867 if (clientTemp == NULL) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700868 ALOGE_IF(!pendingRemovalOnly,
869 "getBiggestClient_l: can't find resource type %s for pid %d",
870 asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700871 return false;
872 }
873
874 *client = clientTemp;
875 return true;
876}
877
Ronghua Wu231c3d12015-03-11 15:10:32 -0700878} // namespace android