blob: 28bfd3ff515126636521d04ca721071df47fabd1 [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>
24#include <dirent.h>
25#include <media/stagefright/ProcessInfo.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <unistd.h>
31
32#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070033#include "ServiceLog.h"
Chong Zhang79d2b282018-04-17 14:14:31 -070034#include "mediautils/SchedulingPolicyService.h"
35#include <cutils/sched_policy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070036namespace android {
37
Wonsik Kim3e378962017-01-05 17:00:02 +090038namespace {
39
40class DeathNotifier : public IBinder::DeathRecipient {
41public:
42 DeathNotifier(const wp<ResourceManagerService> &service, int pid, int64_t clientId)
43 : mService(service), mPid(pid), mClientId(clientId) {}
44
45 virtual void binderDied(const wp<IBinder> & /* who */) override {
46 // Don't check for pid validity since we know it's already dead.
47 sp<ResourceManagerService> service = mService.promote();
48 if (service == nullptr) {
49 ALOGW("ResourceManagerService is dead as well.");
50 return;
51 }
52 service->removeResource(mPid, mClientId, false);
53 }
54
55private:
56 wp<ResourceManagerService> mService;
57 int mPid;
58 int64_t mClientId;
59};
60
61} // namespace
62
Ronghua Wu231c3d12015-03-11 15:10:32 -070063template <typename T>
64static String8 getString(const Vector<T> &items) {
65 String8 itemsStr;
66 for (size_t i = 0; i < items.size(); ++i) {
67 itemsStr.appendFormat("%s ", items[i].toString().string());
68 }
69 return itemsStr;
70}
71
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070072static bool hasResourceType(MediaResource::Type type, const Vector<MediaResource>& resources) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070073 for (size_t i = 0; i < resources.size(); ++i) {
74 if (resources[i].mType == type) {
75 return true;
76 }
77 }
78 return false;
79}
80
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070081static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070082 for (size_t i = 0; i < infos.size(); ++i) {
83 if (hasResourceType(type, infos[i].resources)) {
84 return true;
85 }
86 }
87 return false;
88}
89
90static ResourceInfos& getResourceInfosForEdit(
91 int pid,
92 PidResourceInfosMap& map) {
93 ssize_t index = map.indexOfKey(pid);
94 if (index < 0) {
95 // new pid
96 ResourceInfos infosForPid;
97 map.add(pid, infosForPid);
98 }
99
100 return map.editValueFor(pid);
101}
102
103static ResourceInfo& getResourceInfoForEdit(
104 int64_t clientId,
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -0700105 const sp<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700106 ResourceInfos& infos) {
107 for (size_t i = 0; i < infos.size(); ++i) {
108 if (infos[i].clientId == clientId) {
109 return infos.editItemAt(i);
110 }
111 }
112 ResourceInfo info;
113 info.clientId = clientId;
114 info.client = client;
Chong Zhang79d2b282018-04-17 14:14:31 -0700115 info.cpuBoost = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700116 infos.push_back(info);
117 return infos.editItemAt(infos.size() - 1);
118}
119
Dongwon Kangfe508d32015-12-15 14:22:05 +0900120static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) {
121 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700122 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900123 if (binder != NULL) {
124 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
125 for (size_t i = 0; i < resources.size(); ++i) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700126 if (resources[i].mSubType == MediaResource::kAudioCodec) {
127 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
128 } else if (resources[i].mSubType == MediaResource::kVideoCodec) {
129 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
130 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900131 }
132 }
133}
134
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700135status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700136 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700137
dcashman014e91e2015-09-11 09:33:01 -0700138 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
139 result.format("Permission Denial: "
140 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
141 IPCThreadState::self()->getCallingPid(),
142 IPCThreadState::self()->getCallingUid());
143 write(fd, result.string(), result.size());
144 return PERMISSION_DENIED;
145 }
146
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700147 PidResourceInfosMap mapCopy;
148 bool supportsMultipleSecureCodecs;
149 bool supportsSecureWithNonSecureCodec;
150 String8 serviceLog;
151 {
152 Mutex::Autolock lock(mLock);
153 mapCopy = mMap; // Shadow copy, real copy will happen on write.
154 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
155 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
156 serviceLog = mServiceLog->toString(" " /* linePrefix */);
157 }
158
159 const size_t SIZE = 256;
160 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700161 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
162 result.append(buffer);
163 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700164 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700165 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700166 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
167 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700168 result.append(buffer);
169
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700170 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700171 for (size_t i = 0; i < mapCopy.size(); ++i) {
172 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700173 result.append(buffer);
174
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700175 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700177 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700178 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
179 result.append(buffer);
180
181 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
182 result.append(buffer);
183
184 Vector<MediaResource> resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700185 result.append(" Resources:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700186 for (size_t k = 0; k < resources.size(); ++k) {
187 snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
188 result.append(buffer);
189 }
190 }
191 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700192 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700193 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700194
195 write(fd, result.string(), result.size());
196 return OK;
197}
198
Ronghua Wu231c3d12015-03-11 15:10:32 -0700199ResourceManagerService::ResourceManagerService()
Dongwon Kang2642c842016-03-23 18:07:29 -0700200 : ResourceManagerService(new ProcessInfo()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700201
202ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo)
203 : mProcessInfo(processInfo),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700204 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700205 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700206 mSupportsSecureWithNonSecureCodec(true),
207 mCpuBoostCount(0) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700208
209ResourceManagerService::~ResourceManagerService() {}
210
211void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700212 String8 log = String8::format("config(%s)", getString(policies).string());
213 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700214
215 Mutex::Autolock lock(mLock);
216 for (size_t i = 0; i < policies.size(); ++i) {
217 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700218 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700219 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700220 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700221 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700222 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700223 }
224 }
225}
226
227void ResourceManagerService::addResource(
228 int pid,
229 int64_t clientId,
230 const sp<IResourceManagerClient> client,
231 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700232 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700233 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700234 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700235
236 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800237 if (!mProcessInfo->isValidPid(pid)) {
238 ALOGE("Rejected addResource call with invalid pid.");
239 return;
240 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700241 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
242 ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700243 // TODO: do the merge instead of append.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700244 info.resources.appendVector(resources);
Chong Zhang79d2b282018-04-17 14:14:31 -0700245
246 for (size_t i = 0; i < resources.size(); ++i) {
247 if (resources[i].mType == MediaResource::kCpuBoost && !info.cpuBoost) {
248 info.cpuBoost = true;
249 // Request it on every new instance of kCpuBoost, as the media.codec
250 // could have died, if we only do it the first time subsequent instances
251 // never gets the boost.
252 if (requestCpusetBoost(true, this) != OK) {
253 ALOGW("couldn't request cpuset boost");
254 }
255 mCpuBoostCount++;
256 }
257 }
Wonsik Kim3e378962017-01-05 17:00:02 +0900258 if (info.deathNotifier == nullptr) {
259 info.deathNotifier = new DeathNotifier(this, pid, clientId);
260 IInterface::asBinder(client)->linkToDeath(info.deathNotifier);
261 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900262 notifyResourceGranted(pid, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700263}
264
Ronghua Wu37c89242015-07-15 12:23:48 -0700265void ResourceManagerService::removeResource(int pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900266 removeResource(pid, clientId, true);
267}
268
269void ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700270 String8 log = String8::format(
271 "removeResource(pid %d, clientId %lld)",
272 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700273 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700274
275 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900276 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800277 ALOGE("Rejected removeResource call with invalid pid.");
278 return;
279 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700280 ssize_t index = mMap.indexOfKey(pid);
281 if (index < 0) {
282 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
283 return;
284 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700285 bool found = false;
Ronghua Wu37c89242015-07-15 12:23:48 -0700286 ResourceInfos &infos = mMap.editValueAt(index);
287 for (size_t j = 0; j < infos.size(); ++j) {
288 if (infos[j].clientId == clientId) {
Chong Zhang79d2b282018-04-17 14:14:31 -0700289 if (infos[j].cpuBoost && mCpuBoostCount > 0) {
290 if (--mCpuBoostCount == 0) {
291 requestCpusetBoost(false, this);
292 }
293 }
Wonsik Kim3e378962017-01-05 17:00:02 +0900294 IInterface::asBinder(infos[j].client)->unlinkToDeath(infos[j].deathNotifier);
Ronghua Wu37c89242015-07-15 12:23:48 -0700295 j = infos.removeAt(j);
296 found = true;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700297 break;
298 }
299 }
300 if (!found) {
301 ALOGV("didn't find client");
302 }
303}
304
Ronghua Wu05d89f12015-07-07 16:47:42 -0700305void ResourceManagerService::getClientForResource_l(
306 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) {
307 if (res == NULL) {
308 return;
309 }
310 sp<IResourceManagerClient> client;
311 if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) {
312 clients->push_back(client);
313 }
314}
315
Ronghua Wu231c3d12015-03-11 15:10:32 -0700316bool ResourceManagerService::reclaimResource(
317 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700318 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700319 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700320 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700321
322 Vector<sp<IResourceManagerClient>> clients;
323 {
324 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800325 if (!mProcessInfo->isValidPid(callingPid)) {
326 ALOGE("Rejected reclaimResource call with invalid callingPid.");
327 return false;
328 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700329 const MediaResource *secureCodec = NULL;
330 const MediaResource *nonSecureCodec = NULL;
331 const MediaResource *graphicMemory = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700332 for (size_t i = 0; i < resources.size(); ++i) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800333 MediaResource::Type type = resources[i].mType;
334 if (resources[i].mType == MediaResource::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700335 secureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800336 } else if (type == MediaResource::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700337 nonSecureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800338 } else if (type == MediaResource::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700339 graphicMemory = &resources[i];
340 }
341 }
342
343 // first pass to handle secure/non-secure codec conflict
344 if (secureCodec != NULL) {
345 if (!mSupportsMultipleSecureCodecs) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800346 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700347 return false;
348 }
349 }
350 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800351 if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700352 return false;
353 }
354 }
355 }
356 if (nonSecureCodec != NULL) {
357 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800358 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700359 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700360 }
361 }
362 }
363
364 if (clients.size() == 0) {
365 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700366 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700367 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700368
369 if (clients.size() == 0) {
370 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700371 getClientForResource_l(callingPid, secureCodec, &clients);
372 getClientForResource_l(callingPid, nonSecureCodec, &clients);
373 }
374
375 if (clients.size() == 0) {
376 // if we are here, run the fourth pass to free one codec with the different type.
377 if (secureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800378 MediaResource temp(MediaResource::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700379 getClientForResource_l(callingPid, &temp, &clients);
380 }
381 if (nonSecureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800382 MediaResource temp(MediaResource::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700383 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700384 }
385 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700386 }
387
388 if (clients.size() == 0) {
389 return false;
390 }
391
Ronghua Wu67e7f542015-03-13 10:47:08 -0700392 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700393 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700394 log = String8::format("reclaimResource from client %p", clients[i].get());
395 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700396 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700397 failedClient = clients[i];
398 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700399 }
400 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700401
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700402 if (failedClient == NULL) {
403 return true;
404 }
405
Ronghua Wu67e7f542015-03-13 10:47:08 -0700406 {
407 Mutex::Autolock lock(mLock);
408 bool found = false;
409 for (size_t i = 0; i < mMap.size(); ++i) {
410 ResourceInfos &infos = mMap.editValueAt(i);
411 for (size_t j = 0; j < infos.size();) {
412 if (infos[j].client == failedClient) {
413 j = infos.removeAt(j);
414 found = true;
415 } else {
416 ++j;
417 }
418 }
419 if (found) {
420 break;
421 }
422 }
423 if (!found) {
424 ALOGV("didn't find failed client");
425 }
426 }
427
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700428 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700429}
430
431bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800432 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700433 Vector<sp<IResourceManagerClient>> temp;
434 for (size_t i = 0; i < mMap.size(); ++i) {
435 ResourceInfos &infos = mMap.editValueAt(i);
436 for (size_t j = 0; j < infos.size(); ++j) {
437 if (hasResourceType(type, infos[j].resources)) {
438 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
439 // some higher/equal priority process owns the resource,
440 // this request can't be fulfilled.
441 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800442 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700443 return false;
444 }
445 temp.push_back(infos[j].client);
446 }
447 }
448 }
449 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800450 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700451 return true;
452 }
453 clients->appendVector(temp);
454 return true;
455}
456
457bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800458 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700459 int lowestPriorityPid;
460 int lowestPriority;
461 int callingPriority;
462 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
463 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
464 callingPid);
465 return false;
466 }
467 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
468 return false;
469 }
470 if (lowestPriority <= callingPriority) {
471 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
472 lowestPriority, callingPriority);
473 return false;
474 }
475
476 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
477 return false;
478 }
479 return true;
480}
481
482bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800483 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700484 int pid = -1;
485 int priority = -1;
486 for (size_t i = 0; i < mMap.size(); ++i) {
487 if (mMap.valueAt(i).size() == 0) {
488 // no client on this process.
489 continue;
490 }
491 if (!hasResourceType(type, mMap.valueAt(i))) {
492 // doesn't have the requested resource type
493 continue;
494 }
495 int tempPid = mMap.keyAt(i);
496 int tempPriority;
497 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
498 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
499 // TODO: remove this pid from mMap?
500 continue;
501 }
502 if (pid == -1 || tempPriority > priority) {
503 // initial the value
504 pid = tempPid;
505 priority = tempPriority;
506 }
507 }
508 if (pid != -1) {
509 *lowestPriorityPid = pid;
510 *lowestPriority = priority;
511 }
512 return (pid != -1);
513}
514
515bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
516 int callingPidPriority;
517 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
518 return false;
519 }
520
521 int priority;
522 if (!mProcessInfo->getPriority(pid, &priority)) {
523 return false;
524 }
525
526 return (callingPidPriority < priority);
527}
528
529bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800530 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700531 ssize_t index = mMap.indexOfKey(pid);
532 if (index < 0) {
533 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
534 return false;
535 }
536
537 sp<IResourceManagerClient> clientTemp;
538 uint64_t largestValue = 0;
539 const ResourceInfos &infos = mMap.valueAt(index);
540 for (size_t i = 0; i < infos.size(); ++i) {
541 Vector<MediaResource> resources = infos[i].resources;
542 for (size_t j = 0; j < resources.size(); ++j) {
543 if (resources[j].mType == type) {
544 if (resources[j].mValue > largestValue) {
545 largestValue = resources[j].mValue;
546 clientTemp = infos[i].client;
547 }
548 }
549 }
550 }
551
552 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800553 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700554 return false;
555 }
556
557 *client = clientTemp;
558 return true;
559}
560
561} // namespace android