blob: 3c093f9f8559134c8a289a2459ded840b7068982 [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
22#include <binder/IServiceManager.h>
23#include <dirent.h>
24#include <media/stagefright/ProcessInfo.h>
25#include <string.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <unistd.h>
30
31#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070032#include "ServiceLog.h"
Ronghua Wu231c3d12015-03-11 15:10:32 -070033
34namespace android {
35
36template <typename T>
37static String8 getString(const Vector<T> &items) {
38 String8 itemsStr;
39 for (size_t i = 0; i < items.size(); ++i) {
40 itemsStr.appendFormat("%s ", items[i].toString().string());
41 }
42 return itemsStr;
43}
44
45static bool hasResourceType(String8 type, Vector<MediaResource> resources) {
46 for (size_t i = 0; i < resources.size(); ++i) {
47 if (resources[i].mType == type) {
48 return true;
49 }
50 }
51 return false;
52}
53
54static bool hasResourceType(String8 type, ResourceInfos infos) {
55 for (size_t i = 0; i < infos.size(); ++i) {
56 if (hasResourceType(type, infos[i].resources)) {
57 return true;
58 }
59 }
60 return false;
61}
62
63static ResourceInfos& getResourceInfosForEdit(
64 int pid,
65 PidResourceInfosMap& map) {
66 ssize_t index = map.indexOfKey(pid);
67 if (index < 0) {
68 // new pid
69 ResourceInfos infosForPid;
70 map.add(pid, infosForPid);
71 }
72
73 return map.editValueFor(pid);
74}
75
76static ResourceInfo& getResourceInfoForEdit(
77 int64_t clientId,
78 const sp<IResourceManagerClient> client,
79 ResourceInfos& infos) {
80 for (size_t i = 0; i < infos.size(); ++i) {
81 if (infos[i].clientId == clientId) {
82 return infos.editItemAt(i);
83 }
84 }
85 ResourceInfo info;
86 info.clientId = clientId;
87 info.client = client;
88 infos.push_back(info);
89 return infos.editItemAt(infos.size() - 1);
90}
91
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070092status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070093 Mutex::Autolock lock(mLock);
94
95 String8 result;
96 const size_t SIZE = 256;
97 char buffer[SIZE];
98
99 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
100 result.append(buffer);
101 result.append(" Policies:\n");
102 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", mSupportsMultipleSecureCodecs);
103 result.append(buffer);
104 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", mSupportsSecureWithNonSecureCodec);
105 result.append(buffer);
106
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700107 result.append(" Processes:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700108 for (size_t i = 0; i < mMap.size(); ++i) {
109 snprintf(buffer, SIZE, " Pid: %d\n", mMap.keyAt(i));
110 result.append(buffer);
111
112 const ResourceInfos &infos = mMap.valueAt(i);
113 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700114 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700115 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
116 result.append(buffer);
117
118 snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
119 result.append(buffer);
120
121 Vector<MediaResource> resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700122 result.append(" Resources:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700123 for (size_t k = 0; k < resources.size(); ++k) {
124 snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
125 result.append(buffer);
126 }
127 }
128 }
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700129 result.append(" Logs:\n");
130 result.append(mServiceLog->toString());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700131
132 write(fd, result.string(), result.size());
133 return OK;
134}
135
Ronghua Wu231c3d12015-03-11 15:10:32 -0700136ResourceManagerService::ResourceManagerService()
137 : mProcessInfo(new ProcessInfo()),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700138 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139 mSupportsMultipleSecureCodecs(true),
140 mSupportsSecureWithNonSecureCodec(true) {}
141
142ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo)
143 : mProcessInfo(processInfo),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700144 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700145 mSupportsMultipleSecureCodecs(true),
146 mSupportsSecureWithNonSecureCodec(true) {}
147
148ResourceManagerService::~ResourceManagerService() {}
149
150void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700151 String8 log = String8::format("config(%s)", getString(policies).string());
152 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700153
154 Mutex::Autolock lock(mLock);
155 for (size_t i = 0; i < policies.size(); ++i) {
156 String8 type = policies[i].mType;
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700157 String8 value = policies[i].mValue;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700158 if (type == kPolicySupportsMultipleSecureCodecs) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700159 mSupportsMultipleSecureCodecs = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700160 } else if (type == kPolicySupportsSecureWithNonSecureCodec) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700161 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700162 }
163 }
164}
165
166void ResourceManagerService::addResource(
167 int pid,
168 int64_t clientId,
169 const sp<IResourceManagerClient> client,
170 const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700171 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700172 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700173 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174
175 Mutex::Autolock lock(mLock);
176 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
177 ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700178 // TODO: do the merge instead of append.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700179 info.resources.appendVector(resources);
180}
181
182void ResourceManagerService::removeResource(int64_t clientId) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700183 String8 log = String8::format("removeResource(%lld)", (long long) clientId);
184 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700185
186 Mutex::Autolock lock(mLock);
187 bool found = false;
188 for (size_t i = 0; i < mMap.size(); ++i) {
189 ResourceInfos &infos = mMap.editValueAt(i);
190 for (size_t j = 0; j < infos.size();) {
191 if (infos[j].clientId == clientId) {
192 j = infos.removeAt(j);
193 found = true;
194 } else {
195 ++j;
196 }
197 }
198 if (found) {
199 break;
200 }
201 }
202 if (!found) {
203 ALOGV("didn't find client");
204 }
205}
206
207bool ResourceManagerService::reclaimResource(
208 int callingPid, const Vector<MediaResource> &resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700209 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700211 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700212
213 Vector<sp<IResourceManagerClient>> clients;
214 {
215 Mutex::Autolock lock(mLock);
216 // first pass to handle secure/non-secure codec conflict
217 for (size_t i = 0; i < resources.size(); ++i) {
218 String8 type = resources[i].mType;
219 if (type == kResourceSecureCodec) {
220 if (!mSupportsMultipleSecureCodecs) {
221 if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) {
222 return false;
223 }
224 }
225 if (!mSupportsSecureWithNonSecureCodec) {
226 if (!getAllClients_l(callingPid, String8(kResourceNonSecureCodec), &clients)) {
227 return false;
228 }
229 }
230 } else if (type == kResourceNonSecureCodec) {
231 if (!mSupportsSecureWithNonSecureCodec) {
232 if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) {
233 return false;
234 }
235 }
236 }
237 }
238
239 if (clients.size() == 0) {
240 // if no secure/non-secure codec conflict, run second pass to handle other resources.
241 for (size_t i = 0; i < resources.size(); ++i) {
242 String8 type = resources[i].mType;
243 if (type == kResourceGraphicMemory) {
244 sp<IResourceManagerClient> client;
245 if (!getLowestPriorityBiggestClient_l(callingPid, type, &client)) {
246 return false;
247 }
248 clients.push_back(client);
249 }
250 }
251 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700252
253 if (clients.size() == 0) {
254 // if we are here, run the third pass to free one codec with the same type.
255 for (size_t i = 0; i < resources.size(); ++i) {
256 String8 type = resources[i].mType;
257 if (type == kResourceSecureCodec || type == kResourceNonSecureCodec) {
258 sp<IResourceManagerClient> client;
259 if (!getLowestPriorityBiggestClient_l(callingPid, type, &client)) {
260 return false;
261 }
262 clients.push_back(client);
263 }
264 }
265 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700266 }
267
268 if (clients.size() == 0) {
269 return false;
270 }
271
Ronghua Wu67e7f542015-03-13 10:47:08 -0700272 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700273 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700274 log = String8::format("reclaimResource from client %p", clients[i].get());
275 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700276 if (!clients[i]->reclaimResource()) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700277 failedClient = clients[i];
278 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700279 }
280 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700281
282 {
283 Mutex::Autolock lock(mLock);
284 bool found = false;
285 for (size_t i = 0; i < mMap.size(); ++i) {
286 ResourceInfos &infos = mMap.editValueAt(i);
287 for (size_t j = 0; j < infos.size();) {
288 if (infos[j].client == failedClient) {
289 j = infos.removeAt(j);
290 found = true;
291 } else {
292 ++j;
293 }
294 }
295 if (found) {
296 break;
297 }
298 }
299 if (!found) {
300 ALOGV("didn't find failed client");
301 }
302 }
303
304 return (failedClient == NULL);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700305}
306
307bool ResourceManagerService::getAllClients_l(
308 int callingPid, const String8 &type, Vector<sp<IResourceManagerClient>> *clients) {
309 Vector<sp<IResourceManagerClient>> temp;
310 for (size_t i = 0; i < mMap.size(); ++i) {
311 ResourceInfos &infos = mMap.editValueAt(i);
312 for (size_t j = 0; j < infos.size(); ++j) {
313 if (hasResourceType(type, infos[j].resources)) {
314 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
315 // some higher/equal priority process owns the resource,
316 // this request can't be fulfilled.
317 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
318 type.string(), mMap.keyAt(i));
319 return false;
320 }
321 temp.push_back(infos[j].client);
322 }
323 }
324 }
325 if (temp.size() == 0) {
326 ALOGV("getAllClients_l: didn't find any resource %s", type.string());
327 return true;
328 }
329 clients->appendVector(temp);
330 return true;
331}
332
333bool ResourceManagerService::getLowestPriorityBiggestClient_l(
334 int callingPid, const String8 &type, sp<IResourceManagerClient> *client) {
335 int lowestPriorityPid;
336 int lowestPriority;
337 int callingPriority;
338 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
339 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
340 callingPid);
341 return false;
342 }
343 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
344 return false;
345 }
346 if (lowestPriority <= callingPriority) {
347 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
348 lowestPriority, callingPriority);
349 return false;
350 }
351
352 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
353 return false;
354 }
355 return true;
356}
357
358bool ResourceManagerService::getLowestPriorityPid_l(
359 const String8 &type, int *lowestPriorityPid, int *lowestPriority) {
360 int pid = -1;
361 int priority = -1;
362 for (size_t i = 0; i < mMap.size(); ++i) {
363 if (mMap.valueAt(i).size() == 0) {
364 // no client on this process.
365 continue;
366 }
367 if (!hasResourceType(type, mMap.valueAt(i))) {
368 // doesn't have the requested resource type
369 continue;
370 }
371 int tempPid = mMap.keyAt(i);
372 int tempPriority;
373 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
374 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
375 // TODO: remove this pid from mMap?
376 continue;
377 }
378 if (pid == -1 || tempPriority > priority) {
379 // initial the value
380 pid = tempPid;
381 priority = tempPriority;
382 }
383 }
384 if (pid != -1) {
385 *lowestPriorityPid = pid;
386 *lowestPriority = priority;
387 }
388 return (pid != -1);
389}
390
391bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
392 int callingPidPriority;
393 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
394 return false;
395 }
396
397 int priority;
398 if (!mProcessInfo->getPriority(pid, &priority)) {
399 return false;
400 }
401
402 return (callingPidPriority < priority);
403}
404
405bool ResourceManagerService::getBiggestClient_l(
406 int pid, const String8 &type, sp<IResourceManagerClient> *client) {
407 ssize_t index = mMap.indexOfKey(pid);
408 if (index < 0) {
409 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
410 return false;
411 }
412
413 sp<IResourceManagerClient> clientTemp;
414 uint64_t largestValue = 0;
415 const ResourceInfos &infos = mMap.valueAt(index);
416 for (size_t i = 0; i < infos.size(); ++i) {
417 Vector<MediaResource> resources = infos[i].resources;
418 for (size_t j = 0; j < resources.size(); ++j) {
419 if (resources[j].mType == type) {
420 if (resources[j].mValue > largestValue) {
421 largestValue = resources[j].mValue;
422 clientTemp = infos[i].client;
423 }
424 }
425 }
426 }
427
428 if (clientTemp == NULL) {
429 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", type.string(), pid);
430 return false;
431 }
432
433 *client = clientTemp;
434 return true;
435}
436
437} // namespace android