blob: bdcd5e43814efbe111f68379e0649c8c595dce50 [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
Dongwon Kangfe508d32015-12-15 14:22:05 +090022#include <binder/IMediaResourceMonitor.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070023#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070024#include <cutils/sched_policy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <dirent.h>
26#include <media/stagefright/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070027#include <mediautils/BatteryNotifier.h>
28#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070029#include <string.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/time.h>
33#include <unistd.h>
34
35#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070036#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070037
Ronghua Wu231c3d12015-03-11 15:10:32 -070038namespace android {
39
Wonsik Kim3e378962017-01-05 17:00:02 +090040namespace {
41
42class DeathNotifier : public IBinder::DeathRecipient {
43public:
44 DeathNotifier(const wp<ResourceManagerService> &service, int pid, int64_t clientId)
45 : mService(service), mPid(pid), mClientId(clientId) {}
46
47 virtual void binderDied(const wp<IBinder> & /* who */) override {
48 // Don't check for pid validity since we know it's already dead.
49 sp<ResourceManagerService> service = mService.promote();
50 if (service == nullptr) {
51 ALOGW("ResourceManagerService is dead as well.");
52 return;
53 }
54 service->removeResource(mPid, mClientId, false);
55 }
56
57private:
58 wp<ResourceManagerService> mService;
59 int mPid;
60 int64_t mClientId;
61};
62
63} // namespace
64
Ronghua Wu231c3d12015-03-11 15:10:32 -070065template <typename T>
66static String8 getString(const Vector<T> &items) {
67 String8 itemsStr;
68 for (size_t i = 0; i < items.size(); ++i) {
69 itemsStr.appendFormat("%s ", items[i].toString().string());
70 }
71 return itemsStr;
72}
73
Chong Zhangfb092d32019-08-12 09:45:44 -070074static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
75 for (auto it = resources.begin(); it != resources.end(); it++) {
76 if (it->second.mType == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070077 return true;
78 }
79 }
80 return false;
81}
82
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070083static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 for (size_t i = 0; i < infos.size(); ++i) {
85 if (hasResourceType(type, infos[i].resources)) {
86 return true;
87 }
88 }
89 return false;
90}
91
92static ResourceInfos& getResourceInfosForEdit(
93 int pid,
94 PidResourceInfosMap& map) {
95 ssize_t index = map.indexOfKey(pid);
96 if (index < 0) {
97 // new pid
98 ResourceInfos infosForPid;
99 map.add(pid, infosForPid);
100 }
101
102 return map.editValueFor(pid);
103}
104
105static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700106 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700107 int64_t clientId,
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -0700108 const sp<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700109 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700110 ssize_t index = infos.indexOfKey(clientId);
111
112 if (index < 0) {
113 ResourceInfo info;
114 info.uid = uid;
115 info.clientId = clientId;
116 info.client = client;
117
118 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700119 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700120
121 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700122}
123
Dongwon Kangfe508d32015-12-15 14:22:05 +0900124static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) {
125 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700126 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900127 if (binder != NULL) {
128 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
129 for (size_t i = 0; i < resources.size(); ++i) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700130 if (resources[i].mSubType == MediaResource::kAudioCodec) {
131 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
132 } else if (resources[i].mSubType == MediaResource::kVideoCodec) {
133 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
134 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900135 }
136 }
137}
138
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700139status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700140 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700141
dcashman014e91e2015-09-11 09:33:01 -0700142 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
143 result.format("Permission Denial: "
144 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
145 IPCThreadState::self()->getCallingPid(),
146 IPCThreadState::self()->getCallingUid());
147 write(fd, result.string(), result.size());
148 return PERMISSION_DENIED;
149 }
150
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700151 PidResourceInfosMap mapCopy;
152 bool supportsMultipleSecureCodecs;
153 bool supportsSecureWithNonSecureCodec;
154 String8 serviceLog;
155 {
156 Mutex::Autolock lock(mLock);
157 mapCopy = mMap; // Shadow copy, real copy will happen on write.
158 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
159 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
160 serviceLog = mServiceLog->toString(" " /* linePrefix */);
161 }
162
163 const size_t SIZE = 256;
164 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700165 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
166 result.append(buffer);
167 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700168 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700169 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700170 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
171 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700172 result.append(buffer);
173
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700174 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700175 for (size_t i = 0; i < mapCopy.size(); ++i) {
176 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700177 result.append(buffer);
178
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700179 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700180 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700181 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700182 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
183 result.append(buffer);
184
185 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
186 result.append(buffer);
187
Chong Zhangfb092d32019-08-12 09:45:44 -0700188 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700189 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700190 for (auto it = resources.begin(); it != resources.end(); it++) {
191 snprintf(buffer, SIZE, " %s\n", it->second.toString().string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700192 result.append(buffer);
193 }
194 }
195 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700196 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700197 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700198
199 write(fd, result.string(), result.size());
200 return OK;
201}
202
Chong Zhangdd726802019-08-21 17:24:13 -0700203struct SystemCallbackImpl :
204 public ResourceManagerService::SystemCallbackInterface {
205 SystemCallbackImpl() {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700206
Chong Zhangdd726802019-08-21 17:24:13 -0700207 virtual void noteStartVideo(int uid) override {
208 BatteryNotifier::getInstance().noteStartVideo(uid);
209 }
210 virtual void noteStopVideo(int uid) override {
211 BatteryNotifier::getInstance().noteStopVideo(uid);
212 }
213 virtual void noteResetVideo() override {
214 BatteryNotifier::getInstance().noteResetVideo();
215 }
216 virtual bool requestCpusetBoost(
217 bool enable, const sp<IInterface> &client) override {
218 return android::requestCpusetBoost(enable, client);
219 }
220
221protected:
222 virtual ~SystemCallbackImpl() {}
223
224private:
225 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
226};
227
228ResourceManagerService::ResourceManagerService()
229 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
230
231ResourceManagerService::ResourceManagerService(
232 const sp<ProcessInfoInterface> &processInfo,
233 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700234 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700235 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700236 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700237 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700238 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangee33d642019-08-08 14:26:43 -0700239 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700240 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700241}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700242
243ResourceManagerService::~ResourceManagerService() {}
244
245void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700246 String8 log = String8::format("config(%s)", getString(policies).string());
247 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700248
249 Mutex::Autolock lock(mLock);
250 for (size_t i = 0; i < policies.size(); ++i) {
251 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700252 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700253 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700254 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700255 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700256 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700257 }
258 }
259}
260
Chong Zhangfb092d32019-08-12 09:45:44 -0700261void ResourceManagerService::onFirstAdded(
262 const MediaResource& resource, const ResourceInfo& clientInfo) {
263 // first time added
264 if (resource.mType == MediaResource::kCpuBoost
265 && resource.mSubType == MediaResource::kUnspecifiedSubType) {
266 // Request it on every new instance of kCpuBoost, as the media.codec
267 // could have died, if we only do it the first time subsequent instances
268 // never gets the boost.
Chong Zhangdd726802019-08-21 17:24:13 -0700269 if (mSystemCB->requestCpusetBoost(true, this) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700270 ALOGW("couldn't request cpuset boost");
271 }
272 mCpuBoostCount++;
273 } else if (resource.mType == MediaResource::kBattery
274 && resource.mSubType == MediaResource::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700275 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700276 }
277}
278
279void ResourceManagerService::onLastRemoved(
280 const MediaResource& resource, const ResourceInfo& clientInfo) {
281 if (resource.mType == MediaResource::kCpuBoost
282 && resource.mSubType == MediaResource::kUnspecifiedSubType
283 && mCpuBoostCount > 0) {
284 if (--mCpuBoostCount == 0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700285 mSystemCB->requestCpusetBoost(false, this);
Chong Zhangfb092d32019-08-12 09:45:44 -0700286 }
287 } else if (resource.mType == MediaResource::kBattery
288 && resource.mSubType == MediaResource::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700289 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700290 }
291}
292
Ronghua Wu231c3d12015-03-11 15:10:32 -0700293void ResourceManagerService::addResource(
294 int pid,
Chong Zhangee33d642019-08-08 14:26:43 -0700295 int uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700296 int64_t clientId,
297 const sp<IResourceManagerClient> client,
298 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700299 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700300 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700301 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700302
303 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800304 if (!mProcessInfo->isValidPid(pid)) {
305 ALOGE("Rejected addResource call with invalid pid.");
306 return;
307 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700308 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700309 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700310
311 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700312 const auto resType = std::make_pair(resources[i].mType, resources[i].mSubType);
313 if (info.resources.find(resType) == info.resources.end()) {
314 onFirstAdded(resources[i], info);
315 info.resources[resType] = resources[i];
316 } else {
317 info.resources[resType].mValue += resources[i].mValue;
Chong Zhang79d2b282018-04-17 14:14:31 -0700318 }
319 }
Wonsik Kim3e378962017-01-05 17:00:02 +0900320 if (info.deathNotifier == nullptr) {
321 info.deathNotifier = new DeathNotifier(this, pid, clientId);
322 IInterface::asBinder(client)->linkToDeath(info.deathNotifier);
323 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900324 notifyResourceGranted(pid, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700325}
326
Chong Zhangfb092d32019-08-12 09:45:44 -0700327void ResourceManagerService::removeResource(int pid, int64_t clientId,
328 const Vector<MediaResource> &resources) {
329 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
330 pid, (long long) clientId, getString(resources).string());
331 mServiceLog->add(log);
332
333 Mutex::Autolock lock(mLock);
334 if (!mProcessInfo->isValidPid(pid)) {
335 ALOGE("Rejected removeResource call with invalid pid.");
336 return;
337 }
338 ssize_t index = mMap.indexOfKey(pid);
339 if (index < 0) {
340 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
341 return;
342 }
343 ResourceInfos &infos = mMap.editValueAt(index);
344
345 index = infos.indexOfKey(clientId);
346 if (index < 0) {
347 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
348 return;
349 }
350
351 ResourceInfo &info = infos.editValueAt(index);
352
353 for (size_t i = 0; i < resources.size(); ++i) {
354 const auto resType = std::make_pair(resources[i].mType, resources[i].mSubType);
355 // ignore if we don't have it
356 if (info.resources.find(resType) != info.resources.end()) {
357 MediaResource &resource = info.resources[resType];
358 if (resource.mValue > resources[i].mValue) {
359 resource.mValue -= resources[i].mValue;
360 } else {
361 onLastRemoved(resources[i], info);
362 info.resources.erase(resType);
363 }
364 }
365 }
366}
367
368void ResourceManagerService::removeClient(int pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900369 removeResource(pid, clientId, true);
370}
371
372void ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700373 String8 log = String8::format(
374 "removeResource(pid %d, clientId %lld)",
375 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700376 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700377
378 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900379 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800380 ALOGE("Rejected removeResource call with invalid pid.");
381 return;
382 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700383 ssize_t index = mMap.indexOfKey(pid);
384 if (index < 0) {
385 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
386 return;
387 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700388 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700389
390 index = infos.indexOfKey(clientId);
391 if (index < 0) {
392 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
393 return;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700394 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700395
396 const ResourceInfo &info = infos[index];
397 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
398 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700399 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700400
401 IInterface::asBinder(info.client)->unlinkToDeath(info.deathNotifier);
402
403 infos.removeItemsAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700404}
405
Ronghua Wu05d89f12015-07-07 16:47:42 -0700406void ResourceManagerService::getClientForResource_l(
407 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) {
408 if (res == NULL) {
409 return;
410 }
411 sp<IResourceManagerClient> client;
412 if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) {
413 clients->push_back(client);
414 }
415}
416
Ronghua Wu231c3d12015-03-11 15:10:32 -0700417bool ResourceManagerService::reclaimResource(
418 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700419 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700420 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700421 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700422
423 Vector<sp<IResourceManagerClient>> clients;
424 {
425 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800426 if (!mProcessInfo->isValidPid(callingPid)) {
427 ALOGE("Rejected reclaimResource call with invalid callingPid.");
428 return false;
429 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700430 const MediaResource *secureCodec = NULL;
431 const MediaResource *nonSecureCodec = NULL;
432 const MediaResource *graphicMemory = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700433 for (size_t i = 0; i < resources.size(); ++i) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800434 MediaResource::Type type = resources[i].mType;
435 if (resources[i].mType == MediaResource::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700436 secureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800437 } else if (type == MediaResource::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700438 nonSecureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800439 } else if (type == MediaResource::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700440 graphicMemory = &resources[i];
441 }
442 }
443
444 // first pass to handle secure/non-secure codec conflict
445 if (secureCodec != NULL) {
446 if (!mSupportsMultipleSecureCodecs) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800447 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700448 return false;
449 }
450 }
451 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800452 if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700453 return false;
454 }
455 }
456 }
457 if (nonSecureCodec != NULL) {
458 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800459 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700460 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700461 }
462 }
463 }
464
465 if (clients.size() == 0) {
466 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700467 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700468 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700469
470 if (clients.size() == 0) {
471 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700472 getClientForResource_l(callingPid, secureCodec, &clients);
473 getClientForResource_l(callingPid, nonSecureCodec, &clients);
474 }
475
476 if (clients.size() == 0) {
477 // if we are here, run the fourth pass to free one codec with the different type.
478 if (secureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800479 MediaResource temp(MediaResource::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700480 getClientForResource_l(callingPid, &temp, &clients);
481 }
482 if (nonSecureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800483 MediaResource temp(MediaResource::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700484 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700485 }
486 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700487 }
488
489 if (clients.size() == 0) {
490 return false;
491 }
492
Ronghua Wu67e7f542015-03-13 10:47:08 -0700493 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700494 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700495 log = String8::format("reclaimResource from client %p", clients[i].get());
496 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700497 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700498 failedClient = clients[i];
499 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700500 }
501 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700502
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700503 if (failedClient == NULL) {
504 return true;
505 }
506
Ronghua Wu67e7f542015-03-13 10:47:08 -0700507 {
508 Mutex::Autolock lock(mLock);
509 bool found = false;
510 for (size_t i = 0; i < mMap.size(); ++i) {
511 ResourceInfos &infos = mMap.editValueAt(i);
512 for (size_t j = 0; j < infos.size();) {
513 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700514 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700515 found = true;
516 } else {
517 ++j;
518 }
519 }
520 if (found) {
521 break;
522 }
523 }
524 if (!found) {
525 ALOGV("didn't find failed client");
526 }
527 }
528
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700529 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700530}
531
532bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800533 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700534 Vector<sp<IResourceManagerClient>> temp;
535 for (size_t i = 0; i < mMap.size(); ++i) {
536 ResourceInfos &infos = mMap.editValueAt(i);
537 for (size_t j = 0; j < infos.size(); ++j) {
538 if (hasResourceType(type, infos[j].resources)) {
539 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
540 // some higher/equal priority process owns the resource,
541 // this request can't be fulfilled.
542 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800543 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700544 return false;
545 }
546 temp.push_back(infos[j].client);
547 }
548 }
549 }
550 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800551 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700552 return true;
553 }
554 clients->appendVector(temp);
555 return true;
556}
557
558bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800559 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700560 int lowestPriorityPid;
561 int lowestPriority;
562 int callingPriority;
563 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
564 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
565 callingPid);
566 return false;
567 }
568 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
569 return false;
570 }
571 if (lowestPriority <= callingPriority) {
572 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
573 lowestPriority, callingPriority);
574 return false;
575 }
576
577 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
578 return false;
579 }
580 return true;
581}
582
583bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800584 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700585 int pid = -1;
586 int priority = -1;
587 for (size_t i = 0; i < mMap.size(); ++i) {
588 if (mMap.valueAt(i).size() == 0) {
589 // no client on this process.
590 continue;
591 }
592 if (!hasResourceType(type, mMap.valueAt(i))) {
593 // doesn't have the requested resource type
594 continue;
595 }
596 int tempPid = mMap.keyAt(i);
597 int tempPriority;
598 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
599 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
600 // TODO: remove this pid from mMap?
601 continue;
602 }
603 if (pid == -1 || tempPriority > priority) {
604 // initial the value
605 pid = tempPid;
606 priority = tempPriority;
607 }
608 }
609 if (pid != -1) {
610 *lowestPriorityPid = pid;
611 *lowestPriority = priority;
612 }
613 return (pid != -1);
614}
615
616bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
617 int callingPidPriority;
618 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
619 return false;
620 }
621
622 int priority;
623 if (!mProcessInfo->getPriority(pid, &priority)) {
624 return false;
625 }
626
627 return (callingPidPriority < priority);
628}
629
630bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800631 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700632 ssize_t index = mMap.indexOfKey(pid);
633 if (index < 0) {
634 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
635 return false;
636 }
637
638 sp<IResourceManagerClient> clientTemp;
639 uint64_t largestValue = 0;
640 const ResourceInfos &infos = mMap.valueAt(index);
641 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700642 const ResourceList &resources = infos[i].resources;
643 for (auto it = resources.begin(); it != resources.end(); it++) {
644 const MediaResource &resource = it->second;
645 if (resource.mType == type) {
646 if (resource.mValue > largestValue) {
647 largestValue = resource.mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700648 clientTemp = infos[i].client;
649 }
650 }
651 }
652 }
653
654 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800655 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700656 return false;
657 }
658
659 *client = clientTemp;
660 return true;
661}
662
663} // namespace android