blob: 64534bf4fe34692e3882d989f35dce8da8bb9b67 [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"
Ronghua Wu231c3d12015-03-11 15:10:32 -070034
35namespace android {
36
37template <typename T>
38static String8 getString(const Vector<T> &items) {
39 String8 itemsStr;
40 for (size_t i = 0; i < items.size(); ++i) {
41 itemsStr.appendFormat("%s ", items[i].toString().string());
42 }
43 return itemsStr;
44}
45
Ronghua Wuea15fd22016-03-03 13:35:05 -080046static bool hasResourceType(MediaResource::Type type, Vector<MediaResource> resources) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070047 for (size_t i = 0; i < resources.size(); ++i) {
48 if (resources[i].mType == type) {
49 return true;
50 }
51 }
52 return false;
53}
54
Ronghua Wuea15fd22016-03-03 13:35:05 -080055static bool hasResourceType(MediaResource::Type type, ResourceInfos infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070056 for (size_t i = 0; i < infos.size(); ++i) {
57 if (hasResourceType(type, infos[i].resources)) {
58 return true;
59 }
60 }
61 return false;
62}
63
64static ResourceInfos& getResourceInfosForEdit(
65 int pid,
66 PidResourceInfosMap& map) {
67 ssize_t index = map.indexOfKey(pid);
68 if (index < 0) {
69 // new pid
70 ResourceInfos infosForPid;
71 map.add(pid, infosForPid);
72 }
73
74 return map.editValueFor(pid);
75}
76
77static ResourceInfo& getResourceInfoForEdit(
78 int64_t clientId,
79 const sp<IResourceManagerClient> client,
80 ResourceInfos& infos) {
81 for (size_t i = 0; i < infos.size(); ++i) {
82 if (infos[i].clientId == clientId) {
83 return infos.editItemAt(i);
84 }
85 }
86 ResourceInfo info;
87 info.clientId = clientId;
88 info.client = client;
89 infos.push_back(info);
90 return infos.editItemAt(infos.size() - 1);
91}
92
Dongwon Kangfe508d32015-12-15 14:22:05 +090093static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) {
94 static const char* const kServiceName = "media_resource_monitor";
95 sp<IBinder> binder = defaultServiceManager()->getService(String16(kServiceName));
96 if (binder != NULL) {
97 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
98 for (size_t i = 0; i < resources.size(); ++i) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -070099 if (resources[i].mSubType == MediaResource::kAudioCodec) {
100 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
101 } else if (resources[i].mSubType == MediaResource::kVideoCodec) {
102 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
103 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900104 }
105 }
106}
107
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700108status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700109 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700110
dcashman014e91e2015-09-11 09:33:01 -0700111 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
112 result.format("Permission Denial: "
113 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
114 IPCThreadState::self()->getCallingPid(),
115 IPCThreadState::self()->getCallingUid());
116 write(fd, result.string(), result.size());
117 return PERMISSION_DENIED;
118 }
119
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700120 PidResourceInfosMap mapCopy;
121 bool supportsMultipleSecureCodecs;
122 bool supportsSecureWithNonSecureCodec;
123 String8 serviceLog;
124 {
125 Mutex::Autolock lock(mLock);
126 mapCopy = mMap; // Shadow copy, real copy will happen on write.
127 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
128 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
129 serviceLog = mServiceLog->toString(" " /* linePrefix */);
130 }
131
132 const size_t SIZE = 256;
133 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700134 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
135 result.append(buffer);
136 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700137 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700138 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700139 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
140 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700141 result.append(buffer);
142
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700143 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700144 for (size_t i = 0; i < mapCopy.size(); ++i) {
145 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700146 result.append(buffer);
147
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700148 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700149 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700150 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700151 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
152 result.append(buffer);
153
154 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
155 result.append(buffer);
156
157 Vector<MediaResource> resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700158 result.append(" Resources:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700159 for (size_t k = 0; k < resources.size(); ++k) {
160 snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
161 result.append(buffer);
162 }
163 }
164 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700165 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700166 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700167
168 write(fd, result.string(), result.size());
169 return OK;
170}
171
Ronghua Wu231c3d12015-03-11 15:10:32 -0700172ResourceManagerService::ResourceManagerService()
173 : mProcessInfo(new ProcessInfo()),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700174 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700175 mSupportsMultipleSecureCodecs(true),
176 mSupportsSecureWithNonSecureCodec(true) {}
177
178ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo)
179 : mProcessInfo(processInfo),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700180 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700181 mSupportsMultipleSecureCodecs(true),
182 mSupportsSecureWithNonSecureCodec(true) {}
183
184ResourceManagerService::~ResourceManagerService() {}
185
186void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700187 String8 log = String8::format("config(%s)", getString(policies).string());
188 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700189
190 Mutex::Autolock lock(mLock);
191 for (size_t i = 0; i < policies.size(); ++i) {
192 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700193 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700194 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700195 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700196 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700197 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700198 }
199 }
200}
201
202void ResourceManagerService::addResource(
203 int pid,
204 int64_t clientId,
205 const sp<IResourceManagerClient> client,
206 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700207 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700208 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700209 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210
211 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800212 if (!mProcessInfo->isValidPid(pid)) {
213 ALOGE("Rejected addResource call with invalid pid.");
214 return;
215 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700216 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
217 ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700218 // TODO: do the merge instead of append.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700219 info.resources.appendVector(resources);
Dongwon Kangfe508d32015-12-15 14:22:05 +0900220 notifyResourceGranted(pid, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700221}
222
Ronghua Wu37c89242015-07-15 12:23:48 -0700223void ResourceManagerService::removeResource(int pid, int64_t clientId) {
224 String8 log = String8::format(
225 "removeResource(pid %d, clientId %lld)",
226 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700227 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700228
229 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800230 if (!mProcessInfo->isValidPid(pid)) {
231 ALOGE("Rejected removeResource call with invalid pid.");
232 return;
233 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700234 ssize_t index = mMap.indexOfKey(pid);
235 if (index < 0) {
236 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
237 return;
238 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700239 bool found = false;
Ronghua Wu37c89242015-07-15 12:23:48 -0700240 ResourceInfos &infos = mMap.editValueAt(index);
241 for (size_t j = 0; j < infos.size(); ++j) {
242 if (infos[j].clientId == clientId) {
243 j = infos.removeAt(j);
244 found = true;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700245 break;
246 }
247 }
248 if (!found) {
249 ALOGV("didn't find client");
250 }
251}
252
Ronghua Wu05d89f12015-07-07 16:47:42 -0700253void ResourceManagerService::getClientForResource_l(
254 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) {
255 if (res == NULL) {
256 return;
257 }
258 sp<IResourceManagerClient> client;
259 if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) {
260 clients->push_back(client);
261 }
262}
263
Ronghua Wu231c3d12015-03-11 15:10:32 -0700264bool ResourceManagerService::reclaimResource(
265 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700266 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700267 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700268 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700269
270 Vector<sp<IResourceManagerClient>> clients;
271 {
272 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800273 if (!mProcessInfo->isValidPid(callingPid)) {
274 ALOGE("Rejected reclaimResource call with invalid callingPid.");
275 return false;
276 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700277 const MediaResource *secureCodec = NULL;
278 const MediaResource *nonSecureCodec = NULL;
279 const MediaResource *graphicMemory = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700280 for (size_t i = 0; i < resources.size(); ++i) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800281 MediaResource::Type type = resources[i].mType;
282 if (resources[i].mType == MediaResource::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700283 secureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800284 } else if (type == MediaResource::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700285 nonSecureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800286 } else if (type == MediaResource::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700287 graphicMemory = &resources[i];
288 }
289 }
290
291 // first pass to handle secure/non-secure codec conflict
292 if (secureCodec != NULL) {
293 if (!mSupportsMultipleSecureCodecs) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800294 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700295 return false;
296 }
297 }
298 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800299 if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700300 return false;
301 }
302 }
303 }
304 if (nonSecureCodec != NULL) {
305 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800306 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700307 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700308 }
309 }
310 }
311
312 if (clients.size() == 0) {
313 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700314 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700315 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700316
317 if (clients.size() == 0) {
318 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700319 getClientForResource_l(callingPid, secureCodec, &clients);
320 getClientForResource_l(callingPid, nonSecureCodec, &clients);
321 }
322
323 if (clients.size() == 0) {
324 // if we are here, run the fourth pass to free one codec with the different type.
325 if (secureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800326 MediaResource temp(MediaResource::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700327 getClientForResource_l(callingPid, &temp, &clients);
328 }
329 if (nonSecureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800330 MediaResource temp(MediaResource::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700331 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700332 }
333 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700334 }
335
336 if (clients.size() == 0) {
337 return false;
338 }
339
Ronghua Wu67e7f542015-03-13 10:47:08 -0700340 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700341 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700342 log = String8::format("reclaimResource from client %p", clients[i].get());
343 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700344 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700345 failedClient = clients[i];
346 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700347 }
348 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700349
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700350 if (failedClient == NULL) {
351 return true;
352 }
353
Ronghua Wu67e7f542015-03-13 10:47:08 -0700354 {
355 Mutex::Autolock lock(mLock);
356 bool found = false;
357 for (size_t i = 0; i < mMap.size(); ++i) {
358 ResourceInfos &infos = mMap.editValueAt(i);
359 for (size_t j = 0; j < infos.size();) {
360 if (infos[j].client == failedClient) {
361 j = infos.removeAt(j);
362 found = true;
363 } else {
364 ++j;
365 }
366 }
367 if (found) {
368 break;
369 }
370 }
371 if (!found) {
372 ALOGV("didn't find failed client");
373 }
374 }
375
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700376 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700377}
378
379bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800380 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700381 Vector<sp<IResourceManagerClient>> temp;
382 for (size_t i = 0; i < mMap.size(); ++i) {
383 ResourceInfos &infos = mMap.editValueAt(i);
384 for (size_t j = 0; j < infos.size(); ++j) {
385 if (hasResourceType(type, infos[j].resources)) {
386 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
387 // some higher/equal priority process owns the resource,
388 // this request can't be fulfilled.
389 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800390 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700391 return false;
392 }
393 temp.push_back(infos[j].client);
394 }
395 }
396 }
397 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800398 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700399 return true;
400 }
401 clients->appendVector(temp);
402 return true;
403}
404
405bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800406 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700407 int lowestPriorityPid;
408 int lowestPriority;
409 int callingPriority;
410 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
411 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
412 callingPid);
413 return false;
414 }
415 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
416 return false;
417 }
418 if (lowestPriority <= callingPriority) {
419 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
420 lowestPriority, callingPriority);
421 return false;
422 }
423
424 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
425 return false;
426 }
427 return true;
428}
429
430bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800431 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700432 int pid = -1;
433 int priority = -1;
434 for (size_t i = 0; i < mMap.size(); ++i) {
435 if (mMap.valueAt(i).size() == 0) {
436 // no client on this process.
437 continue;
438 }
439 if (!hasResourceType(type, mMap.valueAt(i))) {
440 // doesn't have the requested resource type
441 continue;
442 }
443 int tempPid = mMap.keyAt(i);
444 int tempPriority;
445 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
446 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
447 // TODO: remove this pid from mMap?
448 continue;
449 }
450 if (pid == -1 || tempPriority > priority) {
451 // initial the value
452 pid = tempPid;
453 priority = tempPriority;
454 }
455 }
456 if (pid != -1) {
457 *lowestPriorityPid = pid;
458 *lowestPriority = priority;
459 }
460 return (pid != -1);
461}
462
463bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
464 int callingPidPriority;
465 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
466 return false;
467 }
468
469 int priority;
470 if (!mProcessInfo->getPriority(pid, &priority)) {
471 return false;
472 }
473
474 return (callingPidPriority < priority);
475}
476
477bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800478 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700479 ssize_t index = mMap.indexOfKey(pid);
480 if (index < 0) {
481 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
482 return false;
483 }
484
485 sp<IResourceManagerClient> clientTemp;
486 uint64_t largestValue = 0;
487 const ResourceInfos &infos = mMap.valueAt(index);
488 for (size_t i = 0; i < infos.size(); ++i) {
489 Vector<MediaResource> resources = infos[i].resources;
490 for (size_t j = 0; j < resources.size(); ++j) {
491 if (resources[j].mType == type) {
492 if (resources[j].mValue > largestValue) {
493 largestValue = resources[j].mValue;
494 clientTemp = infos[i].client;
495 }
496 }
497 }
498 }
499
500 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800501 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700502 return false;
503 }
504
505 *client = clientTemp;
506 return true;
507}
508
509} // namespace android