blob: 78bb58765cfdc2f20d3feb2c1024e6898819bb96 [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
Wonsik Kim3e378962017-01-05 17:00:02 +090037namespace {
38
39class DeathNotifier : public IBinder::DeathRecipient {
40public:
41 DeathNotifier(const wp<ResourceManagerService> &service, int pid, int64_t clientId)
42 : mService(service), mPid(pid), mClientId(clientId) {}
43
44 virtual void binderDied(const wp<IBinder> & /* who */) override {
45 // Don't check for pid validity since we know it's already dead.
46 sp<ResourceManagerService> service = mService.promote();
47 if (service == nullptr) {
48 ALOGW("ResourceManagerService is dead as well.");
49 return;
50 }
51 service->removeResource(mPid, mClientId, false);
52 }
53
54private:
55 wp<ResourceManagerService> mService;
56 int mPid;
57 int64_t mClientId;
58};
59
60} // namespace
61
Ronghua Wu231c3d12015-03-11 15:10:32 -070062template <typename T>
63static String8 getString(const Vector<T> &items) {
64 String8 itemsStr;
65 for (size_t i = 0; i < items.size(); ++i) {
66 itemsStr.appendFormat("%s ", items[i].toString().string());
67 }
68 return itemsStr;
69}
70
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070071static bool hasResourceType(MediaResource::Type type, const Vector<MediaResource>& resources) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070072 for (size_t i = 0; i < resources.size(); ++i) {
73 if (resources[i].mType == type) {
74 return true;
75 }
76 }
77 return false;
78}
79
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070080static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070081 for (size_t i = 0; i < infos.size(); ++i) {
82 if (hasResourceType(type, infos[i].resources)) {
83 return true;
84 }
85 }
86 return false;
87}
88
89static ResourceInfos& getResourceInfosForEdit(
90 int pid,
91 PidResourceInfosMap& map) {
92 ssize_t index = map.indexOfKey(pid);
93 if (index < 0) {
94 // new pid
95 ResourceInfos infosForPid;
96 map.add(pid, infosForPid);
97 }
98
99 return map.editValueFor(pid);
100}
101
102static ResourceInfo& getResourceInfoForEdit(
103 int64_t clientId,
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -0700104 const sp<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105 ResourceInfos& infos) {
106 for (size_t i = 0; i < infos.size(); ++i) {
107 if (infos[i].clientId == clientId) {
108 return infos.editItemAt(i);
109 }
110 }
111 ResourceInfo info;
112 info.clientId = clientId;
113 info.client = client;
114 infos.push_back(info);
115 return infos.editItemAt(infos.size() - 1);
116}
117
Dongwon Kangfe508d32015-12-15 14:22:05 +0900118static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) {
119 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700120 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900121 if (binder != NULL) {
122 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
123 for (size_t i = 0; i < resources.size(); ++i) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700124 if (resources[i].mSubType == MediaResource::kAudioCodec) {
125 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
126 } else if (resources[i].mSubType == MediaResource::kVideoCodec) {
127 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
128 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900129 }
130 }
131}
132
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700133status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700134 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700135
dcashman014e91e2015-09-11 09:33:01 -0700136 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
137 result.format("Permission Denial: "
138 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
139 IPCThreadState::self()->getCallingPid(),
140 IPCThreadState::self()->getCallingUid());
141 write(fd, result.string(), result.size());
142 return PERMISSION_DENIED;
143 }
144
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700145 PidResourceInfosMap mapCopy;
146 bool supportsMultipleSecureCodecs;
147 bool supportsSecureWithNonSecureCodec;
148 String8 serviceLog;
149 {
150 Mutex::Autolock lock(mLock);
151 mapCopy = mMap; // Shadow copy, real copy will happen on write.
152 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
153 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
154 serviceLog = mServiceLog->toString(" " /* linePrefix */);
155 }
156
157 const size_t SIZE = 256;
158 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700159 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
160 result.append(buffer);
161 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700162 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700163 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700164 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
165 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700166 result.append(buffer);
167
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700168 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700169 for (size_t i = 0; i < mapCopy.size(); ++i) {
170 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700171 result.append(buffer);
172
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700173 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700174 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700175 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
177 result.append(buffer);
178
179 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
180 result.append(buffer);
181
182 Vector<MediaResource> resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700183 result.append(" Resources:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700184 for (size_t k = 0; k < resources.size(); ++k) {
185 snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
186 result.append(buffer);
187 }
188 }
189 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700190 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700191 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700192
193 write(fd, result.string(), result.size());
194 return OK;
195}
196
Ronghua Wu231c3d12015-03-11 15:10:32 -0700197ResourceManagerService::ResourceManagerService()
Dongwon Kang2642c842016-03-23 18:07:29 -0700198 : ResourceManagerService(new ProcessInfo()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700199
200ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo)
201 : mProcessInfo(processInfo),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700202 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700203 mSupportsMultipleSecureCodecs(true),
204 mSupportsSecureWithNonSecureCodec(true) {}
205
206ResourceManagerService::~ResourceManagerService() {}
207
208void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700209 String8 log = String8::format("config(%s)", getString(policies).string());
210 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700211
212 Mutex::Autolock lock(mLock);
213 for (size_t i = 0; i < policies.size(); ++i) {
214 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700215 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700216 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700217 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700218 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700219 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700220 }
221 }
222}
223
224void ResourceManagerService::addResource(
225 int pid,
226 int64_t clientId,
227 const sp<IResourceManagerClient> client,
228 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700229 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700230 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700231 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700232
233 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800234 if (!mProcessInfo->isValidPid(pid)) {
235 ALOGE("Rejected addResource call with invalid pid.");
236 return;
237 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700238 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
239 ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700240 // TODO: do the merge instead of append.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700241 info.resources.appendVector(resources);
Wonsik Kim3e378962017-01-05 17:00:02 +0900242 if (info.deathNotifier == nullptr) {
243 info.deathNotifier = new DeathNotifier(this, pid, clientId);
244 IInterface::asBinder(client)->linkToDeath(info.deathNotifier);
245 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900246 notifyResourceGranted(pid, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700247}
248
Ronghua Wu37c89242015-07-15 12:23:48 -0700249void ResourceManagerService::removeResource(int pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900250 removeResource(pid, clientId, true);
251}
252
253void ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700254 String8 log = String8::format(
255 "removeResource(pid %d, clientId %lld)",
256 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700257 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700258
259 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900260 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800261 ALOGE("Rejected removeResource call with invalid pid.");
262 return;
263 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700264 ssize_t index = mMap.indexOfKey(pid);
265 if (index < 0) {
266 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
267 return;
268 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700269 bool found = false;
Ronghua Wu37c89242015-07-15 12:23:48 -0700270 ResourceInfos &infos = mMap.editValueAt(index);
271 for (size_t j = 0; j < infos.size(); ++j) {
272 if (infos[j].clientId == clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900273 IInterface::asBinder(infos[j].client)->unlinkToDeath(infos[j].deathNotifier);
Ronghua Wu37c89242015-07-15 12:23:48 -0700274 j = infos.removeAt(j);
275 found = true;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700276 break;
277 }
278 }
279 if (!found) {
280 ALOGV("didn't find client");
281 }
282}
283
Ronghua Wu05d89f12015-07-07 16:47:42 -0700284void ResourceManagerService::getClientForResource_l(
285 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) {
286 if (res == NULL) {
287 return;
288 }
289 sp<IResourceManagerClient> client;
290 if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) {
291 clients->push_back(client);
292 }
293}
294
Ronghua Wu231c3d12015-03-11 15:10:32 -0700295bool ResourceManagerService::reclaimResource(
296 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700297 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700299 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700300
301 Vector<sp<IResourceManagerClient>> clients;
302 {
303 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800304 if (!mProcessInfo->isValidPid(callingPid)) {
305 ALOGE("Rejected reclaimResource call with invalid callingPid.");
306 return false;
307 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700308 const MediaResource *secureCodec = NULL;
309 const MediaResource *nonSecureCodec = NULL;
310 const MediaResource *graphicMemory = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700311 for (size_t i = 0; i < resources.size(); ++i) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800312 MediaResource::Type type = resources[i].mType;
313 if (resources[i].mType == MediaResource::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700314 secureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800315 } else if (type == MediaResource::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700316 nonSecureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800317 } else if (type == MediaResource::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700318 graphicMemory = &resources[i];
319 }
320 }
321
322 // first pass to handle secure/non-secure codec conflict
323 if (secureCodec != NULL) {
324 if (!mSupportsMultipleSecureCodecs) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800325 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700326 return false;
327 }
328 }
329 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800330 if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700331 return false;
332 }
333 }
334 }
335 if (nonSecureCodec != NULL) {
336 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800337 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700338 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700339 }
340 }
341 }
342
343 if (clients.size() == 0) {
344 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700345 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700346 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700347
348 if (clients.size() == 0) {
349 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700350 getClientForResource_l(callingPid, secureCodec, &clients);
351 getClientForResource_l(callingPid, nonSecureCodec, &clients);
352 }
353
354 if (clients.size() == 0) {
355 // if we are here, run the fourth pass to free one codec with the different type.
356 if (secureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800357 MediaResource temp(MediaResource::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700358 getClientForResource_l(callingPid, &temp, &clients);
359 }
360 if (nonSecureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800361 MediaResource temp(MediaResource::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700362 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700363 }
364 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700365 }
366
367 if (clients.size() == 0) {
368 return false;
369 }
370
Ronghua Wu67e7f542015-03-13 10:47:08 -0700371 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700372 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700373 log = String8::format("reclaimResource from client %p", clients[i].get());
374 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700375 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700376 failedClient = clients[i];
377 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700378 }
379 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700380
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700381 if (failedClient == NULL) {
382 return true;
383 }
384
Ronghua Wu67e7f542015-03-13 10:47:08 -0700385 {
386 Mutex::Autolock lock(mLock);
387 bool found = false;
388 for (size_t i = 0; i < mMap.size(); ++i) {
389 ResourceInfos &infos = mMap.editValueAt(i);
390 for (size_t j = 0; j < infos.size();) {
391 if (infos[j].client == failedClient) {
392 j = infos.removeAt(j);
393 found = true;
394 } else {
395 ++j;
396 }
397 }
398 if (found) {
399 break;
400 }
401 }
402 if (!found) {
403 ALOGV("didn't find failed client");
404 }
405 }
406
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700407 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700408}
409
410bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800411 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700412 Vector<sp<IResourceManagerClient>> temp;
413 for (size_t i = 0; i < mMap.size(); ++i) {
414 ResourceInfos &infos = mMap.editValueAt(i);
415 for (size_t j = 0; j < infos.size(); ++j) {
416 if (hasResourceType(type, infos[j].resources)) {
417 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
418 // some higher/equal priority process owns the resource,
419 // this request can't be fulfilled.
420 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800421 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700422 return false;
423 }
424 temp.push_back(infos[j].client);
425 }
426 }
427 }
428 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800429 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700430 return true;
431 }
432 clients->appendVector(temp);
433 return true;
434}
435
436bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800437 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700438 int lowestPriorityPid;
439 int lowestPriority;
440 int callingPriority;
441 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
442 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
443 callingPid);
444 return false;
445 }
446 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
447 return false;
448 }
449 if (lowestPriority <= callingPriority) {
450 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
451 lowestPriority, callingPriority);
452 return false;
453 }
454
455 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
456 return false;
457 }
458 return true;
459}
460
461bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800462 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700463 int pid = -1;
464 int priority = -1;
465 for (size_t i = 0; i < mMap.size(); ++i) {
466 if (mMap.valueAt(i).size() == 0) {
467 // no client on this process.
468 continue;
469 }
470 if (!hasResourceType(type, mMap.valueAt(i))) {
471 // doesn't have the requested resource type
472 continue;
473 }
474 int tempPid = mMap.keyAt(i);
475 int tempPriority;
476 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
477 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
478 // TODO: remove this pid from mMap?
479 continue;
480 }
481 if (pid == -1 || tempPriority > priority) {
482 // initial the value
483 pid = tempPid;
484 priority = tempPriority;
485 }
486 }
487 if (pid != -1) {
488 *lowestPriorityPid = pid;
489 *lowestPriority = priority;
490 }
491 return (pid != -1);
492}
493
494bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
495 int callingPidPriority;
496 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
497 return false;
498 }
499
500 int priority;
501 if (!mProcessInfo->getPriority(pid, &priority)) {
502 return false;
503 }
504
505 return (callingPidPriority < priority);
506}
507
508bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800509 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700510 ssize_t index = mMap.indexOfKey(pid);
511 if (index < 0) {
512 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
513 return false;
514 }
515
516 sp<IResourceManagerClient> clientTemp;
517 uint64_t largestValue = 0;
518 const ResourceInfos &infos = mMap.valueAt(index);
519 for (size_t i = 0; i < infos.size(); ++i) {
520 Vector<MediaResource> resources = infos[i].resources;
521 for (size_t j = 0; j < resources.size(); ++j) {
522 if (resources[j].mType == type) {
523 if (resources[j].mValue > largestValue) {
524 largestValue = resources[j].mValue;
525 clientTemp = infos[i].client;
526 }
527 }
528 }
529 }
530
531 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800532 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700533 return false;
534 }
535
536 *client = clientTemp;
537 return true;
538}
539
540} // namespace android