blob: 117a2114e174221dee5c4174e435a08bf7873977 [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
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070074static bool hasResourceType(MediaResource::Type type, const Vector<MediaResource>& resources) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070075 for (size_t i = 0; i < resources.size(); ++i) {
76 if (resources[i].mType == type) {
77 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) {
110 for (size_t i = 0; i < infos.size(); ++i) {
111 if (infos[i].clientId == clientId) {
112 return infos.editItemAt(i);
113 }
114 }
115 ResourceInfo info;
Chong Zhangee33d642019-08-08 14:26:43 -0700116 info.uid = uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700117 info.clientId = clientId;
118 info.client = client;
Chong Zhang79d2b282018-04-17 14:14:31 -0700119 info.cpuBoost = false;
Chong Zhangee33d642019-08-08 14:26:43 -0700120 info.batteryNoted = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700121 infos.push_back(info);
122 return infos.editItemAt(infos.size() - 1);
123}
124
Dongwon Kangfe508d32015-12-15 14:22:05 +0900125static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) {
126 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700127 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900128 if (binder != NULL) {
129 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
130 for (size_t i = 0; i < resources.size(); ++i) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700131 if (resources[i].mSubType == MediaResource::kAudioCodec) {
132 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
133 } else if (resources[i].mSubType == MediaResource::kVideoCodec) {
134 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
135 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900136 }
137 }
138}
139
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700140status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700141 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700142
dcashman014e91e2015-09-11 09:33:01 -0700143 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
144 result.format("Permission Denial: "
145 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
146 IPCThreadState::self()->getCallingPid(),
147 IPCThreadState::self()->getCallingUid());
148 write(fd, result.string(), result.size());
149 return PERMISSION_DENIED;
150 }
151
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700152 PidResourceInfosMap mapCopy;
153 bool supportsMultipleSecureCodecs;
154 bool supportsSecureWithNonSecureCodec;
155 String8 serviceLog;
156 {
157 Mutex::Autolock lock(mLock);
158 mapCopy = mMap; // Shadow copy, real copy will happen on write.
159 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
160 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
161 serviceLog = mServiceLog->toString(" " /* linePrefix */);
162 }
163
164 const size_t SIZE = 256;
165 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700166 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
167 result.append(buffer);
168 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700169 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700170 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700171 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
172 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700173 result.append(buffer);
174
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700175 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700176 for (size_t i = 0; i < mapCopy.size(); ++i) {
177 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700178 result.append(buffer);
179
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700180 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700181 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700182 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700183 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
184 result.append(buffer);
185
186 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
187 result.append(buffer);
188
189 Vector<MediaResource> resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700190 result.append(" Resources:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700191 for (size_t k = 0; k < resources.size(); ++k) {
192 snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
193 result.append(buffer);
194 }
195 }
196 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700197 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700198 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700199
200 write(fd, result.string(), result.size());
201 return OK;
202}
203
Ronghua Wu231c3d12015-03-11 15:10:32 -0700204ResourceManagerService::ResourceManagerService()
Dongwon Kang2642c842016-03-23 18:07:29 -0700205 : ResourceManagerService(new ProcessInfo()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700206
207ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo)
208 : mProcessInfo(processInfo),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700209 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700211 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangee33d642019-08-08 14:26:43 -0700212 mCpuBoostCount(0) {
213 BatteryNotifier::getInstance().noteResetVideo();
214}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700215
216ResourceManagerService::~ResourceManagerService() {}
217
218void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700219 String8 log = String8::format("config(%s)", getString(policies).string());
220 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700221
222 Mutex::Autolock lock(mLock);
223 for (size_t i = 0; i < policies.size(); ++i) {
224 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700225 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700226 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700227 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700228 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700229 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700230 }
231 }
232}
233
234void ResourceManagerService::addResource(
235 int pid,
Chong Zhangee33d642019-08-08 14:26:43 -0700236 int uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700237 int64_t clientId,
238 const sp<IResourceManagerClient> client,
239 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700240 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700241 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700242 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700243
244 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800245 if (!mProcessInfo->isValidPid(pid)) {
246 ALOGE("Rejected addResource call with invalid pid.");
247 return;
248 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700249 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700250 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700251 // TODO: do the merge instead of append.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252 info.resources.appendVector(resources);
Chong Zhang79d2b282018-04-17 14:14:31 -0700253
254 for (size_t i = 0; i < resources.size(); ++i) {
255 if (resources[i].mType == MediaResource::kCpuBoost && !info.cpuBoost) {
256 info.cpuBoost = true;
257 // Request it on every new instance of kCpuBoost, as the media.codec
258 // could have died, if we only do it the first time subsequent instances
259 // never gets the boost.
260 if (requestCpusetBoost(true, this) != OK) {
261 ALOGW("couldn't request cpuset boost");
262 }
263 mCpuBoostCount++;
Chong Zhangee33d642019-08-08 14:26:43 -0700264 } else if (resources[i].mType == MediaResource::kBattery
265 && resources[i].mSubType == MediaResource::kVideoCodec
266 && !info.batteryNoted) {
267 info.batteryNoted = true;
268 BatteryNotifier::getInstance().noteStartVideo(info.uid);
Chong Zhang79d2b282018-04-17 14:14:31 -0700269 }
270 }
Wonsik Kim3e378962017-01-05 17:00:02 +0900271 if (info.deathNotifier == nullptr) {
272 info.deathNotifier = new DeathNotifier(this, pid, clientId);
273 IInterface::asBinder(client)->linkToDeath(info.deathNotifier);
274 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900275 notifyResourceGranted(pid, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700276}
277
Ronghua Wu37c89242015-07-15 12:23:48 -0700278void ResourceManagerService::removeResource(int pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900279 removeResource(pid, clientId, true);
280}
281
282void ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700283 String8 log = String8::format(
284 "removeResource(pid %d, clientId %lld)",
285 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700286 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700287
288 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900289 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800290 ALOGE("Rejected removeResource call with invalid pid.");
291 return;
292 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700293 ssize_t index = mMap.indexOfKey(pid);
294 if (index < 0) {
295 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
296 return;
297 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298 bool found = false;
Ronghua Wu37c89242015-07-15 12:23:48 -0700299 ResourceInfos &infos = mMap.editValueAt(index);
300 for (size_t j = 0; j < infos.size(); ++j) {
301 if (infos[j].clientId == clientId) {
Chong Zhang79d2b282018-04-17 14:14:31 -0700302 if (infos[j].cpuBoost && mCpuBoostCount > 0) {
303 if (--mCpuBoostCount == 0) {
304 requestCpusetBoost(false, this);
305 }
306 }
Chong Zhangee33d642019-08-08 14:26:43 -0700307 if (infos[j].batteryNoted) {
308 BatteryNotifier::getInstance().noteStopVideo(infos[j].uid);
309 }
Wonsik Kim3e378962017-01-05 17:00:02 +0900310 IInterface::asBinder(infos[j].client)->unlinkToDeath(infos[j].deathNotifier);
Ronghua Wu37c89242015-07-15 12:23:48 -0700311 j = infos.removeAt(j);
312 found = true;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700313 break;
314 }
315 }
316 if (!found) {
317 ALOGV("didn't find client");
318 }
319}
320
Ronghua Wu05d89f12015-07-07 16:47:42 -0700321void ResourceManagerService::getClientForResource_l(
322 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) {
323 if (res == NULL) {
324 return;
325 }
326 sp<IResourceManagerClient> client;
327 if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) {
328 clients->push_back(client);
329 }
330}
331
Ronghua Wu231c3d12015-03-11 15:10:32 -0700332bool ResourceManagerService::reclaimResource(
333 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700334 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700335 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700336 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700337
338 Vector<sp<IResourceManagerClient>> clients;
339 {
340 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800341 if (!mProcessInfo->isValidPid(callingPid)) {
342 ALOGE("Rejected reclaimResource call with invalid callingPid.");
343 return false;
344 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700345 const MediaResource *secureCodec = NULL;
346 const MediaResource *nonSecureCodec = NULL;
347 const MediaResource *graphicMemory = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700348 for (size_t i = 0; i < resources.size(); ++i) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800349 MediaResource::Type type = resources[i].mType;
350 if (resources[i].mType == MediaResource::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700351 secureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800352 } else if (type == MediaResource::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700353 nonSecureCodec = &resources[i];
Ronghua Wuea15fd22016-03-03 13:35:05 -0800354 } else if (type == MediaResource::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700355 graphicMemory = &resources[i];
356 }
357 }
358
359 // first pass to handle secure/non-secure codec conflict
360 if (secureCodec != NULL) {
361 if (!mSupportsMultipleSecureCodecs) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800362 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700363 return false;
364 }
365 }
366 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800367 if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700368 return false;
369 }
370 }
371 }
372 if (nonSecureCodec != NULL) {
373 if (!mSupportsSecureWithNonSecureCodec) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800374 if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700375 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700376 }
377 }
378 }
379
380 if (clients.size() == 0) {
381 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700382 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700383 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700384
385 if (clients.size() == 0) {
386 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700387 getClientForResource_l(callingPid, secureCodec, &clients);
388 getClientForResource_l(callingPid, nonSecureCodec, &clients);
389 }
390
391 if (clients.size() == 0) {
392 // if we are here, run the fourth pass to free one codec with the different type.
393 if (secureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800394 MediaResource temp(MediaResource::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700395 getClientForResource_l(callingPid, &temp, &clients);
396 }
397 if (nonSecureCodec != NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800398 MediaResource temp(MediaResource::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700399 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700400 }
401 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700402 }
403
404 if (clients.size() == 0) {
405 return false;
406 }
407
Ronghua Wu67e7f542015-03-13 10:47:08 -0700408 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700409 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700410 log = String8::format("reclaimResource from client %p", clients[i].get());
411 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700412 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700413 failedClient = clients[i];
414 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700415 }
416 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700417
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700418 if (failedClient == NULL) {
419 return true;
420 }
421
Ronghua Wu67e7f542015-03-13 10:47:08 -0700422 {
423 Mutex::Autolock lock(mLock);
424 bool found = false;
425 for (size_t i = 0; i < mMap.size(); ++i) {
426 ResourceInfos &infos = mMap.editValueAt(i);
427 for (size_t j = 0; j < infos.size();) {
428 if (infos[j].client == failedClient) {
429 j = infos.removeAt(j);
430 found = true;
431 } else {
432 ++j;
433 }
434 }
435 if (found) {
436 break;
437 }
438 }
439 if (!found) {
440 ALOGV("didn't find failed client");
441 }
442 }
443
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700444 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700445}
446
447bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800448 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700449 Vector<sp<IResourceManagerClient>> temp;
450 for (size_t i = 0; i < mMap.size(); ++i) {
451 ResourceInfos &infos = mMap.editValueAt(i);
452 for (size_t j = 0; j < infos.size(); ++j) {
453 if (hasResourceType(type, infos[j].resources)) {
454 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
455 // some higher/equal priority process owns the resource,
456 // this request can't be fulfilled.
457 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800458 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700459 return false;
460 }
461 temp.push_back(infos[j].client);
462 }
463 }
464 }
465 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800466 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700467 return true;
468 }
469 clients->appendVector(temp);
470 return true;
471}
472
473bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800474 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700475 int lowestPriorityPid;
476 int lowestPriority;
477 int callingPriority;
478 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
479 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
480 callingPid);
481 return false;
482 }
483 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
484 return false;
485 }
486 if (lowestPriority <= callingPriority) {
487 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
488 lowestPriority, callingPriority);
489 return false;
490 }
491
492 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
493 return false;
494 }
495 return true;
496}
497
498bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800499 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700500 int pid = -1;
501 int priority = -1;
502 for (size_t i = 0; i < mMap.size(); ++i) {
503 if (mMap.valueAt(i).size() == 0) {
504 // no client on this process.
505 continue;
506 }
507 if (!hasResourceType(type, mMap.valueAt(i))) {
508 // doesn't have the requested resource type
509 continue;
510 }
511 int tempPid = mMap.keyAt(i);
512 int tempPriority;
513 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
514 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
515 // TODO: remove this pid from mMap?
516 continue;
517 }
518 if (pid == -1 || tempPriority > priority) {
519 // initial the value
520 pid = tempPid;
521 priority = tempPriority;
522 }
523 }
524 if (pid != -1) {
525 *lowestPriorityPid = pid;
526 *lowestPriority = priority;
527 }
528 return (pid != -1);
529}
530
531bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
532 int callingPidPriority;
533 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
534 return false;
535 }
536
537 int priority;
538 if (!mProcessInfo->getPriority(pid, &priority)) {
539 return false;
540 }
541
542 return (callingPidPriority < priority);
543}
544
545bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800546 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700547 ssize_t index = mMap.indexOfKey(pid);
548 if (index < 0) {
549 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
550 return false;
551 }
552
553 sp<IResourceManagerClient> clientTemp;
554 uint64_t largestValue = 0;
555 const ResourceInfos &infos = mMap.valueAt(index);
556 for (size_t i = 0; i < infos.size(); ++i) {
557 Vector<MediaResource> resources = infos[i].resources;
558 for (size_t j = 0; j < resources.size(); ++j) {
559 if (resources[j].mType == type) {
560 if (resources[j].mValue > largestValue) {
561 largestValue = resources[j].mValue;
562 clientTemp = infos[i].client;
563 }
564 }
565 }
566 }
567
568 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800569 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700570 return false;
571 }
572
573 *client = clientTemp;
574 return true;
575}
576
577} // namespace android