blob: ae832c792923feeca5dc49361058bf0fdbb1b612 [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>
Chong Zhang181e6952019-10-09 13:23:39 -070026#include <media/MediaResourcePolicy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <media/stagefright/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070028#include <mediautils/BatteryNotifier.h>
29#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070030#include <string.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/time.h>
34#include <unistd.h>
35
36#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070037#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070038
Ronghua Wu231c3d12015-03-11 15:10:32 -070039namespace android {
40
Chong Zhang181e6952019-10-09 13:23:39 -070041namespace media {
Wonsik Kim3e378962017-01-05 17:00:02 +090042
43class DeathNotifier : public IBinder::DeathRecipient {
44public:
45 DeathNotifier(const wp<ResourceManagerService> &service, int pid, int64_t clientId)
46 : mService(service), mPid(pid), mClientId(clientId) {}
47
48 virtual void binderDied(const wp<IBinder> & /* who */) override {
49 // Don't check for pid validity since we know it's already dead.
50 sp<ResourceManagerService> service = mService.promote();
51 if (service == nullptr) {
52 ALOGW("ResourceManagerService is dead as well.");
53 return;
54 }
55 service->removeResource(mPid, mClientId, false);
56 }
57
58private:
59 wp<ResourceManagerService> mService;
60 int mPid;
61 int64_t mClientId;
62};
63
Ronghua Wu231c3d12015-03-11 15:10:32 -070064template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070065static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070066 String8 itemsStr;
67 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070068 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070069 }
70 return itemsStr;
71}
72
Chong Zhangfb092d32019-08-12 09:45:44 -070073static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
74 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070075 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070076 return true;
77 }
78 }
79 return false;
80}
81
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070082static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070083 for (size_t i = 0; i < infos.size(); ++i) {
84 if (hasResourceType(type, infos[i].resources)) {
85 return true;
86 }
87 }
88 return false;
89}
90
91static ResourceInfos& getResourceInfosForEdit(
92 int pid,
93 PidResourceInfosMap& map) {
94 ssize_t index = map.indexOfKey(pid);
95 if (index < 0) {
96 // new pid
97 ResourceInfos infosForPid;
98 map.add(pid, infosForPid);
99 }
100
101 return map.editValueFor(pid);
102}
103
104static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700105 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700106 int64_t clientId,
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -0700107 const sp<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700108 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700109 ssize_t index = infos.indexOfKey(clientId);
110
111 if (index < 0) {
112 ResourceInfo info;
113 info.uid = uid;
114 info.clientId = clientId;
115 info.client = client;
116
117 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700118 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700119
120 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700121}
122
Chong Zhang181e6952019-10-09 13:23:39 -0700123static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900124 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700125 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900126 if (binder != NULL) {
127 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
128 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700129 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700130 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700131 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700132 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
133 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900134 }
135 }
136}
137
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700138status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700139 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700140
dcashman014e91e2015-09-11 09:33:01 -0700141 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
142 result.format("Permission Denial: "
143 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
144 IPCThreadState::self()->getCallingPid(),
145 IPCThreadState::self()->getCallingUid());
146 write(fd, result.string(), result.size());
147 return PERMISSION_DENIED;
148 }
149
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700150 PidResourceInfosMap mapCopy;
151 bool supportsMultipleSecureCodecs;
152 bool supportsSecureWithNonSecureCodec;
153 String8 serviceLog;
154 {
155 Mutex::Autolock lock(mLock);
156 mapCopy = mMap; // Shadow copy, real copy will happen on write.
157 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
158 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
159 serviceLog = mServiceLog->toString(" " /* linePrefix */);
160 }
161
162 const size_t SIZE = 256;
163 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700164 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
165 result.append(buffer);
166 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700167 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700168 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700169 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
170 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700171 result.append(buffer);
172
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700173 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700174 for (size_t i = 0; i < mapCopy.size(); ++i) {
175 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 result.append(buffer);
177
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700178 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700179 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700180 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700181 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
182 result.append(buffer);
183
Chong Zhang181e6952019-10-09 13:23:39 -0700184 std::string clientName;
185 Status status = infos[j].client->getName(&clientName);
186 if (!status.isOk()) {
187 clientName = "<unknown client>";
188 }
189 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700190 result.append(buffer);
191
Chong Zhangfb092d32019-08-12 09:45:44 -0700192 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700193 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700194 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700195 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700196 result.append(buffer);
197 }
198 }
199 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700200 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700201 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700202
203 write(fd, result.string(), result.size());
204 return OK;
205}
206
Chong Zhangdd726802019-08-21 17:24:13 -0700207struct SystemCallbackImpl :
208 public ResourceManagerService::SystemCallbackInterface {
209 SystemCallbackImpl() {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210
Chong Zhangdd726802019-08-21 17:24:13 -0700211 virtual void noteStartVideo(int uid) override {
212 BatteryNotifier::getInstance().noteStartVideo(uid);
213 }
214 virtual void noteStopVideo(int uid) override {
215 BatteryNotifier::getInstance().noteStopVideo(uid);
216 }
217 virtual void noteResetVideo() override {
218 BatteryNotifier::getInstance().noteResetVideo();
219 }
220 virtual bool requestCpusetBoost(
221 bool enable, const sp<IInterface> &client) override {
222 return android::requestCpusetBoost(enable, client);
223 }
224
225protected:
226 virtual ~SystemCallbackImpl() {}
227
228private:
229 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
230};
231
232ResourceManagerService::ResourceManagerService()
233 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
234
235ResourceManagerService::ResourceManagerService(
236 const sp<ProcessInfoInterface> &processInfo,
237 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700238 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700239 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700240 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700241 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700242 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangee33d642019-08-08 14:26:43 -0700243 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700244 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700245}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700246
247ResourceManagerService::~ResourceManagerService() {}
248
Chong Zhang181e6952019-10-09 13:23:39 -0700249Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700250 String8 log = String8::format("config(%s)", getString(policies).string());
251 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252
253 Mutex::Autolock lock(mLock);
254 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700255 const std::string &type = policies[i].type;
256 const std::string &value = policies[i].value;
257 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700258 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700259 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700260 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700261 }
262 }
Chong Zhang181e6952019-10-09 13:23:39 -0700263 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700264}
265
Chong Zhangfb092d32019-08-12 09:45:44 -0700266void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700267 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700268 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700269 if (resource.type == MediaResource::Type::kCpuBoost
270 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700271 // Request it on every new instance of kCpuBoost, as the media.codec
272 // could have died, if we only do it the first time subsequent instances
273 // never gets the boost.
Chong Zhangdd726802019-08-21 17:24:13 -0700274 if (mSystemCB->requestCpusetBoost(true, this) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700275 ALOGW("couldn't request cpuset boost");
276 }
277 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700278 } else if (resource.type == MediaResource::Type::kBattery
279 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700280 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700281 }
282}
283
284void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700285 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
286 if (resource.type == MediaResource::Type::kCpuBoost
287 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700288 && mCpuBoostCount > 0) {
289 if (--mCpuBoostCount == 0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700290 mSystemCB->requestCpusetBoost(false, this);
Chong Zhangfb092d32019-08-12 09:45:44 -0700291 }
Chong Zhang181e6952019-10-09 13:23:39 -0700292 } else if (resource.type == MediaResource::Type::kBattery
293 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700294 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700295 }
296}
297
Robert Shihc3af31b2019-09-20 21:45:01 -0700298void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700299 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
300 // The resource entry on record is maintained to be in [0,INT64_MAX].
301 // Clamp if merging in the new resource value causes it to go out of bound.
302 // Note that the new resource value could be negative, eg.DrmSession, the
303 // value goes lower when the session is used more often. During reclaim
304 // the session with the highest value (lowest usage) would be closed.
305 if (r2.value < INT64_MAX - r1.value) {
306 r1.value += r2.value;
307 if (r1.value < 0) {
308 r1.value = 0;
309 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700310 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700311 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700312 }
313}
314
Chong Zhang181e6952019-10-09 13:23:39 -0700315Status ResourceManagerService::addResource(
316 int32_t pid,
317 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700318 int64_t clientId,
Chong Zhang181e6952019-10-09 13:23:39 -0700319 const sp<IResourceManagerClient>& client,
320 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700321 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700322 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700323 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700324
325 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800326 if (!mProcessInfo->isValidPid(pid)) {
327 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700328 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800329 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700330 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700331 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700332
333 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700334 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700335 const auto resType = std::tuple(res.type, res.subType, res.id);
336
337 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
338 ALOGW("Ignoring request to remove negative value of non-drm resource");
339 continue;
340 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700341 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700342 if (res.value <= 0) {
343 // We can't init a new entry with negative value, although it's allowed
344 // to merge in negative values after the initial add.
345 ALOGW("Ignoring request to add new resource entry with value <= 0");
346 continue;
347 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700348 onFirstAdded(res, info);
349 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700350 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700351 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700352 }
353 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700354 if (info.deathNotifier == nullptr && client != nullptr) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900355 info.deathNotifier = new DeathNotifier(this, pid, clientId);
356 IInterface::asBinder(client)->linkToDeath(info.deathNotifier);
357 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900358 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700359 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700360}
361
Chong Zhang181e6952019-10-09 13:23:39 -0700362Status ResourceManagerService::removeResource(
363 int32_t pid, int64_t clientId,
364 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700365 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
366 pid, (long long) clientId, getString(resources).string());
367 mServiceLog->add(log);
368
369 Mutex::Autolock lock(mLock);
370 if (!mProcessInfo->isValidPid(pid)) {
371 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700372 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700373 }
374 ssize_t index = mMap.indexOfKey(pid);
375 if (index < 0) {
376 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700377 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700378 }
379 ResourceInfos &infos = mMap.editValueAt(index);
380
381 index = infos.indexOfKey(clientId);
382 if (index < 0) {
383 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700384 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700385 }
386
387 ResourceInfo &info = infos.editValueAt(index);
388
389 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700390 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700391 const auto resType = std::tuple(res.type, res.subType, res.id);
392
393 if (res.value < 0) {
394 ALOGW("Ignoring request to remove negative value of resource");
395 continue;
396 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700397 // ignore if we don't have it
398 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700399 MediaResourceParcel &resource = info.resources[resType];
400 if (resource.value > res.value) {
401 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700402 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700403 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700404 info.resources.erase(resType);
405 }
406 }
407 }
Chong Zhang181e6952019-10-09 13:23:39 -0700408 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700409}
410
Chong Zhang181e6952019-10-09 13:23:39 -0700411Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900412 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700413 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900414}
415
Chong Zhang181e6952019-10-09 13:23:39 -0700416Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700417 String8 log = String8::format(
418 "removeResource(pid %d, clientId %lld)",
419 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700420 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700421
422 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900423 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800424 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700425 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800426 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700427 ssize_t index = mMap.indexOfKey(pid);
428 if (index < 0) {
429 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700430 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700431 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700432 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700433
434 index = infos.indexOfKey(clientId);
435 if (index < 0) {
436 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700437 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700438 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700439
440 const ResourceInfo &info = infos[index];
441 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
442 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700443 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700444
445 IInterface::asBinder(info.client)->unlinkToDeath(info.deathNotifier);
446
447 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700448 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700449}
450
Ronghua Wu05d89f12015-07-07 16:47:42 -0700451void ResourceManagerService::getClientForResource_l(
Chong Zhang181e6952019-10-09 13:23:39 -0700452 int callingPid, const MediaResourceParcel *res, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700453 if (res == NULL) {
454 return;
455 }
456 sp<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700457 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700458 clients->push_back(client);
459 }
460}
461
Chong Zhang181e6952019-10-09 13:23:39 -0700462Status ResourceManagerService::reclaimResource(
463 int32_t callingPid,
464 const std::vector<MediaResourceParcel>& resources,
465 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700466 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700467 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700468 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700469 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700470
471 Vector<sp<IResourceManagerClient>> clients;
472 {
473 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800474 if (!mProcessInfo->isValidPid(callingPid)) {
475 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700476 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800477 }
Chong Zhang181e6952019-10-09 13:23:39 -0700478 const MediaResourceParcel *secureCodec = NULL;
479 const MediaResourceParcel *nonSecureCodec = NULL;
480 const MediaResourceParcel *graphicMemory = NULL;
481 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700482 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700483 MediaResource::Type type = resources[i].type;
484 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700485 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700486 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700487 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700488 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700489 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700490 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700491 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700492 }
493 }
494
495 // first pass to handle secure/non-secure codec conflict
496 if (secureCodec != NULL) {
497 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700498 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
499 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700500 }
501 }
502 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700503 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
504 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700505 }
506 }
507 }
508 if (nonSecureCodec != NULL) {
509 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700510 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
511 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700512 }
513 }
514 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700515 if (drmSession != NULL) {
516 getClientForResource_l(callingPid, drmSession, &clients);
517 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700518 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700519 }
520 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700521
522 if (clients.size() == 0) {
523 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700524 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700525 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700526
527 if (clients.size() == 0) {
528 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700529 getClientForResource_l(callingPid, secureCodec, &clients);
530 getClientForResource_l(callingPid, nonSecureCodec, &clients);
531 }
532
533 if (clients.size() == 0) {
534 // if we are here, run the fourth pass to free one codec with the different type.
535 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700536 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700537 getClientForResource_l(callingPid, &temp, &clients);
538 }
539 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700540 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700541 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700542 }
543 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700544 }
545
546 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700547 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700548 }
549
Ronghua Wu67e7f542015-03-13 10:47:08 -0700550 sp<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700551 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700552 log = String8::format("reclaimResource from client %p", clients[i].get());
553 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700554 bool success;
555 Status status = clients[i]->reclaimResource(&success);
556 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700557 failedClient = clients[i];
558 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700559 }
560 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700561
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700562 if (failedClient == NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700563 *_aidl_return = true;
564 return Status::ok();
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700565 }
566
Ronghua Wu67e7f542015-03-13 10:47:08 -0700567 {
568 Mutex::Autolock lock(mLock);
569 bool found = false;
570 for (size_t i = 0; i < mMap.size(); ++i) {
571 ResourceInfos &infos = mMap.editValueAt(i);
572 for (size_t j = 0; j < infos.size();) {
573 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700574 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700575 found = true;
576 } else {
577 ++j;
578 }
579 }
580 if (found) {
581 break;
582 }
583 }
584 if (!found) {
585 ALOGV("didn't find failed client");
586 }
587 }
588
Chong Zhang181e6952019-10-09 13:23:39 -0700589 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700590}
591
592bool ResourceManagerService::getAllClients_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800593 int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700594 Vector<sp<IResourceManagerClient>> temp;
595 for (size_t i = 0; i < mMap.size(); ++i) {
596 ResourceInfos &infos = mMap.editValueAt(i);
597 for (size_t j = 0; j < infos.size(); ++j) {
598 if (hasResourceType(type, infos[j].resources)) {
599 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
600 // some higher/equal priority process owns the resource,
601 // this request can't be fulfilled.
602 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800603 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700604 return false;
605 }
606 temp.push_back(infos[j].client);
607 }
608 }
609 }
610 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800611 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700612 return true;
613 }
614 clients->appendVector(temp);
615 return true;
616}
617
618bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800619 int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700620 int lowestPriorityPid;
621 int lowestPriority;
622 int callingPriority;
623 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
624 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
625 callingPid);
626 return false;
627 }
628 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
629 return false;
630 }
631 if (lowestPriority <= callingPriority) {
632 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
633 lowestPriority, callingPriority);
634 return false;
635 }
636
637 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
638 return false;
639 }
640 return true;
641}
642
643bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800644 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700645 int pid = -1;
646 int priority = -1;
647 for (size_t i = 0; i < mMap.size(); ++i) {
648 if (mMap.valueAt(i).size() == 0) {
649 // no client on this process.
650 continue;
651 }
652 if (!hasResourceType(type, mMap.valueAt(i))) {
653 // doesn't have the requested resource type
654 continue;
655 }
656 int tempPid = mMap.keyAt(i);
657 int tempPriority;
658 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
659 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
660 // TODO: remove this pid from mMap?
661 continue;
662 }
663 if (pid == -1 || tempPriority > priority) {
664 // initial the value
665 pid = tempPid;
666 priority = tempPriority;
667 }
668 }
669 if (pid != -1) {
670 *lowestPriorityPid = pid;
671 *lowestPriority = priority;
672 }
673 return (pid != -1);
674}
675
676bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
677 int callingPidPriority;
678 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
679 return false;
680 }
681
682 int priority;
683 if (!mProcessInfo->getPriority(pid, &priority)) {
684 return false;
685 }
686
687 return (callingPidPriority < priority);
688}
689
690bool ResourceManagerService::getBiggestClient_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800691 int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700692 ssize_t index = mMap.indexOfKey(pid);
693 if (index < 0) {
694 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
695 return false;
696 }
697
698 sp<IResourceManagerClient> clientTemp;
699 uint64_t largestValue = 0;
700 const ResourceInfos &infos = mMap.valueAt(index);
701 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700702 const ResourceList &resources = infos[i].resources;
703 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700704 const MediaResourceParcel &resource = it->second;
705 if (resource.type == type) {
706 if (resource.value > largestValue) {
707 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700708 clientTemp = infos[i].client;
709 }
710 }
711 }
712 }
713
714 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800715 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700716 return false;
717 }
718
719 *client = clientTemp;
720 return true;
721}
722
Chong Zhang181e6952019-10-09 13:23:39 -0700723} // namespace media
Ronghua Wu231c3d12015-03-11 15:10:32 -0700724} // namespace android