blob: 877c44da8a4a58e49e61384817d6197ce91b231b [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
Chong Zhangfdd512a2019-11-22 11:03:14 -080022#include <android/binder_manager.h>
23#include <android/binder_process.h>
Dongwon Kangfe508d32015-12-15 14:22:05 +090024#include <binder/IMediaResourceMonitor.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070026#include <cutils/sched_policy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <dirent.h>
Chong Zhang181e6952019-10-09 13:23:39 -070028#include <media/MediaResourcePolicy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070029#include <media/stagefright/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070030#include <mediautils/BatteryNotifier.h>
31#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070032#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <unistd.h>
37
38#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070039#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070040
Ronghua Wu231c3d12015-03-11 15:10:32 -070041namespace android {
42
Chong Zhangfdd512a2019-11-22 11:03:14 -080043DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
44 int pid, int64_t clientId)
45 : mService(service), mPid(pid), mClientId(clientId) {}
Wonsik Kim3e378962017-01-05 17:00:02 +090046
Chong Zhangfdd512a2019-11-22 11:03:14 -080047//static
48void DeathNotifier::BinderDiedCallback(void* cookie) {
49 auto thiz = static_cast<DeathNotifier*>(cookie);
50 thiz->binderDied();
51}
Wonsik Kim3e378962017-01-05 17:00:02 +090052
Chong Zhangfdd512a2019-11-22 11:03:14 -080053void DeathNotifier::binderDied() {
54 // Don't check for pid validity since we know it's already dead.
55 std::shared_ptr<ResourceManagerService> service = mService.lock();
56 if (service == nullptr) {
57 ALOGW("ResourceManagerService is dead as well.");
58 return;
Wonsik Kim3e378962017-01-05 17:00:02 +090059 }
Chong Zhangfdd512a2019-11-22 11:03:14 -080060 service->removeResource(mPid, mClientId, false);
61}
Wonsik Kim3e378962017-01-05 17:00:02 +090062
Ronghua Wu231c3d12015-03-11 15:10:32 -070063template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070064static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070065 String8 itemsStr;
66 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070067 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070068 }
69 return itemsStr;
70}
71
Chong Zhangfb092d32019-08-12 09:45:44 -070072static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
73 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070074 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070075 return true;
76 }
77 }
78 return false;
79}
80
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070081static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070082 for (size_t i = 0; i < infos.size(); ++i) {
83 if (hasResourceType(type, infos[i].resources)) {
84 return true;
85 }
86 }
87 return false;
88}
89
90static ResourceInfos& getResourceInfosForEdit(
91 int pid,
92 PidResourceInfosMap& map) {
93 ssize_t index = map.indexOfKey(pid);
94 if (index < 0) {
95 // new pid
96 ResourceInfos infosForPid;
97 map.add(pid, infosForPid);
98 }
99
100 return map.editValueFor(pid);
101}
102
103static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700104 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800106 const std::shared_ptr<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700107 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700108 ssize_t index = infos.indexOfKey(clientId);
109
110 if (index < 0) {
111 ResourceInfo info;
112 info.uid = uid;
113 info.clientId = clientId;
114 info.client = client;
115
116 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700117 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700118
119 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700120}
121
Chong Zhang181e6952019-10-09 13:23:39 -0700122static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900123 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700124 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900125 if (binder != NULL) {
126 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
127 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700128 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700129 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700130 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700131 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
132 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900133 }
134 }
135}
136
Chong Zhangfdd512a2019-11-22 11:03:14 -0800137binder_status_t ResourceManagerService::dump(
138 int fd, const char** /*args*/, uint32_t /*numArgs*/) {
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",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800144 AIBinder_getCallingPid(),
145 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700146 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 {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800209 SystemCallbackImpl() : mClientToken(new BBinder()) {}
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 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800220 virtual bool requestCpusetBoost(bool enable) override {
221 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700222 }
223
224protected:
225 virtual ~SystemCallbackImpl() {}
226
227private:
228 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800229 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700230};
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 Zhangfdd512a2019-11-22 11:03:14 -0800243 mCpuBoostCount(0),
244 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700245 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700246}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700247
Chong Zhangfdd512a2019-11-22 11:03:14 -0800248//static
249void ResourceManagerService::instantiate() {
250 std::shared_ptr<ResourceManagerService> service =
251 ::ndk::SharedRefBase::make<ResourceManagerService>();
252 binder_status_t status =
253 AServiceManager_addService(service->asBinder().get(), getServiceName());
254 if (status != STATUS_OK) {
255 return;
256 }
257 // TODO: mediaserver main() is already starting the thread pool,
258 // move this to mediaserver main() when other services in mediaserver
259 // are converted to ndk-platform aidl.
260 //ABinderProcess_startThreadPool();
261}
262
Ronghua Wu231c3d12015-03-11 15:10:32 -0700263ResourceManagerService::~ResourceManagerService() {}
264
Chong Zhang181e6952019-10-09 13:23:39 -0700265Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700266 String8 log = String8::format("config(%s)", getString(policies).string());
267 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700268
269 Mutex::Autolock lock(mLock);
270 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700271 const std::string &type = policies[i].type;
272 const std::string &value = policies[i].value;
273 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700274 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700275 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700276 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700277 }
278 }
Chong Zhang181e6952019-10-09 13:23:39 -0700279 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700280}
281
Chong Zhangfb092d32019-08-12 09:45:44 -0700282void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700283 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700284 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700285 if (resource.type == MediaResource::Type::kCpuBoost
286 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700287 // Request it on every new instance of kCpuBoost, as the media.codec
288 // could have died, if we only do it the first time subsequent instances
289 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800290 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700291 ALOGW("couldn't request cpuset boost");
292 }
293 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700294 } else if (resource.type == MediaResource::Type::kBattery
295 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700296 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700297 }
298}
299
300void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700301 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
302 if (resource.type == MediaResource::Type::kCpuBoost
303 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700304 && mCpuBoostCount > 0) {
305 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800306 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700307 }
Chong Zhang181e6952019-10-09 13:23:39 -0700308 } else if (resource.type == MediaResource::Type::kBattery
309 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700310 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700311 }
312}
313
Robert Shihc3af31b2019-09-20 21:45:01 -0700314void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700315 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
316 // The resource entry on record is maintained to be in [0,INT64_MAX].
317 // Clamp if merging in the new resource value causes it to go out of bound.
318 // Note that the new resource value could be negative, eg.DrmSession, the
319 // value goes lower when the session is used more often. During reclaim
320 // the session with the highest value (lowest usage) would be closed.
321 if (r2.value < INT64_MAX - r1.value) {
322 r1.value += r2.value;
323 if (r1.value < 0) {
324 r1.value = 0;
325 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700326 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700327 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700328 }
329}
330
Chong Zhang181e6952019-10-09 13:23:39 -0700331Status ResourceManagerService::addResource(
332 int32_t pid,
333 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700334 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800335 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700336 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700337 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700338 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700339 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700340
341 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800342 if (!mProcessInfo->isValidPid(pid)) {
343 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700344 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800345 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700346 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700347 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700348
349 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700350 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700351 const auto resType = std::tuple(res.type, res.subType, res.id);
352
353 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
354 ALOGW("Ignoring request to remove negative value of non-drm resource");
355 continue;
356 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700357 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700358 if (res.value <= 0) {
359 // We can't init a new entry with negative value, although it's allowed
360 // to merge in negative values after the initial add.
361 ALOGW("Ignoring request to add new resource entry with value <= 0");
362 continue;
363 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700364 onFirstAdded(res, info);
365 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700366 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700367 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700368 }
369 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700370 if (info.deathNotifier == nullptr && client != nullptr) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800371 info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId);
372 AIBinder_linkToDeath(client->asBinder().get(),
373 mDeathRecipient.get(), info.deathNotifier.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900374 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900375 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700376 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700377}
378
Chong Zhang181e6952019-10-09 13:23:39 -0700379Status ResourceManagerService::removeResource(
380 int32_t pid, int64_t clientId,
381 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700382 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
383 pid, (long long) clientId, getString(resources).string());
384 mServiceLog->add(log);
385
386 Mutex::Autolock lock(mLock);
387 if (!mProcessInfo->isValidPid(pid)) {
388 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700389 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700390 }
391 ssize_t index = mMap.indexOfKey(pid);
392 if (index < 0) {
393 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700394 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700395 }
396 ResourceInfos &infos = mMap.editValueAt(index);
397
398 index = infos.indexOfKey(clientId);
399 if (index < 0) {
400 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700401 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700402 }
403
404 ResourceInfo &info = infos.editValueAt(index);
405
406 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700407 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700408 const auto resType = std::tuple(res.type, res.subType, res.id);
409
410 if (res.value < 0) {
411 ALOGW("Ignoring request to remove negative value of resource");
412 continue;
413 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700414 // ignore if we don't have it
415 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700416 MediaResourceParcel &resource = info.resources[resType];
417 if (resource.value > res.value) {
418 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700419 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700420 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700421 info.resources.erase(resType);
422 }
423 }
424 }
Chong Zhang181e6952019-10-09 13:23:39 -0700425 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700426}
427
Chong Zhang181e6952019-10-09 13:23:39 -0700428Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900429 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700430 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900431}
432
Chong Zhang181e6952019-10-09 13:23:39 -0700433Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700434 String8 log = String8::format(
435 "removeResource(pid %d, clientId %lld)",
436 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700437 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700438
439 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900440 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800441 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700442 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800443 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700444 ssize_t index = mMap.indexOfKey(pid);
445 if (index < 0) {
446 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700447 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700448 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700449 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700450
451 index = infos.indexOfKey(clientId);
452 if (index < 0) {
453 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700454 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700455 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700456
457 const ResourceInfo &info = infos[index];
458 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
459 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700460 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700461
Chong Zhangfdd512a2019-11-22 11:03:14 -0800462 AIBinder_unlinkToDeath(info.client->asBinder().get(),
463 mDeathRecipient.get(), info.deathNotifier.get());
Chong Zhangfb092d32019-08-12 09:45:44 -0700464
465 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700466 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700467}
468
Ronghua Wu05d89f12015-07-07 16:47:42 -0700469void ResourceManagerService::getClientForResource_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800470 int callingPid, const MediaResourceParcel *res,
471 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700472 if (res == NULL) {
473 return;
474 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800475 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700476 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700477 clients->push_back(client);
478 }
479}
480
Chong Zhang181e6952019-10-09 13:23:39 -0700481Status ResourceManagerService::reclaimResource(
482 int32_t callingPid,
483 const std::vector<MediaResourceParcel>& resources,
484 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700485 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700486 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700487 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700488 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700489
Chong Zhangfdd512a2019-11-22 11:03:14 -0800490 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700491 {
492 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800493 if (!mProcessInfo->isValidPid(callingPid)) {
494 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700495 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800496 }
Chong Zhang181e6952019-10-09 13:23:39 -0700497 const MediaResourceParcel *secureCodec = NULL;
498 const MediaResourceParcel *nonSecureCodec = NULL;
499 const MediaResourceParcel *graphicMemory = NULL;
500 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700501 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700502 MediaResource::Type type = resources[i].type;
503 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700504 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700505 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700506 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700507 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700508 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700509 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700510 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700511 }
512 }
513
514 // first pass to handle secure/non-secure codec conflict
515 if (secureCodec != NULL) {
516 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700517 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
518 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700519 }
520 }
521 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700522 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
523 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700524 }
525 }
526 }
527 if (nonSecureCodec != NULL) {
528 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700529 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
530 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700531 }
532 }
533 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700534 if (drmSession != NULL) {
535 getClientForResource_l(callingPid, drmSession, &clients);
536 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700537 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700538 }
539 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700540
541 if (clients.size() == 0) {
542 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700543 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700544 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700545
546 if (clients.size() == 0) {
547 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700548 getClientForResource_l(callingPid, secureCodec, &clients);
549 getClientForResource_l(callingPid, nonSecureCodec, &clients);
550 }
551
552 if (clients.size() == 0) {
553 // if we are here, run the fourth pass to free one codec with the different type.
554 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700555 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700556 getClientForResource_l(callingPid, &temp, &clients);
557 }
558 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700559 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700560 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700561 }
562 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700563 }
564
565 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700566 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700567 }
568
Chong Zhangfdd512a2019-11-22 11:03:14 -0800569 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700570 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700571 log = String8::format("reclaimResource from client %p", clients[i].get());
572 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700573 bool success;
574 Status status = clients[i]->reclaimResource(&success);
575 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700576 failedClient = clients[i];
577 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700578 }
579 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700580
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700581 if (failedClient == NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700582 *_aidl_return = true;
583 return Status::ok();
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700584 }
585
Ronghua Wu67e7f542015-03-13 10:47:08 -0700586 {
587 Mutex::Autolock lock(mLock);
588 bool found = false;
589 for (size_t i = 0; i < mMap.size(); ++i) {
590 ResourceInfos &infos = mMap.editValueAt(i);
591 for (size_t j = 0; j < infos.size();) {
592 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700593 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700594 found = true;
595 } else {
596 ++j;
597 }
598 }
599 if (found) {
600 break;
601 }
602 }
603 if (!found) {
604 ALOGV("didn't find failed client");
605 }
606 }
607
Chong Zhang181e6952019-10-09 13:23:39 -0700608 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700609}
610
611bool ResourceManagerService::getAllClients_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800612 int callingPid, MediaResource::Type type,
613 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
614 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700615 for (size_t i = 0; i < mMap.size(); ++i) {
616 ResourceInfos &infos = mMap.editValueAt(i);
617 for (size_t j = 0; j < infos.size(); ++j) {
618 if (hasResourceType(type, infos[j].resources)) {
619 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
620 // some higher/equal priority process owns the resource,
621 // this request can't be fulfilled.
622 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800623 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700624 return false;
625 }
626 temp.push_back(infos[j].client);
627 }
628 }
629 }
630 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800631 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700632 return true;
633 }
634 clients->appendVector(temp);
635 return true;
636}
637
638bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800639 int callingPid, MediaResource::Type type,
640 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700641 int lowestPriorityPid;
642 int lowestPriority;
643 int callingPriority;
644 if (!mProcessInfo->getPriority(callingPid, &callingPriority)) {
645 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
646 callingPid);
647 return false;
648 }
649 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
650 return false;
651 }
652 if (lowestPriority <= callingPriority) {
653 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
654 lowestPriority, callingPriority);
655 return false;
656 }
657
658 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
659 return false;
660 }
661 return true;
662}
663
664bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800665 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700666 int pid = -1;
667 int priority = -1;
668 for (size_t i = 0; i < mMap.size(); ++i) {
669 if (mMap.valueAt(i).size() == 0) {
670 // no client on this process.
671 continue;
672 }
673 if (!hasResourceType(type, mMap.valueAt(i))) {
674 // doesn't have the requested resource type
675 continue;
676 }
677 int tempPid = mMap.keyAt(i);
678 int tempPriority;
679 if (!mProcessInfo->getPriority(tempPid, &tempPriority)) {
680 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
681 // TODO: remove this pid from mMap?
682 continue;
683 }
684 if (pid == -1 || tempPriority > priority) {
685 // initial the value
686 pid = tempPid;
687 priority = tempPriority;
688 }
689 }
690 if (pid != -1) {
691 *lowestPriorityPid = pid;
692 *lowestPriority = priority;
693 }
694 return (pid != -1);
695}
696
697bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
698 int callingPidPriority;
699 if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) {
700 return false;
701 }
702
703 int priority;
704 if (!mProcessInfo->getPriority(pid, &priority)) {
705 return false;
706 }
707
708 return (callingPidPriority < priority);
709}
710
711bool ResourceManagerService::getBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800712 int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700713 ssize_t index = mMap.indexOfKey(pid);
714 if (index < 0) {
715 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
716 return false;
717 }
718
Chong Zhangfdd512a2019-11-22 11:03:14 -0800719 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700720 uint64_t largestValue = 0;
721 const ResourceInfos &infos = mMap.valueAt(index);
722 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700723 const ResourceList &resources = infos[i].resources;
724 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700725 const MediaResourceParcel &resource = it->second;
726 if (resource.type == type) {
727 if (resource.value > largestValue) {
728 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700729 clientTemp = infos[i].client;
730 }
731 }
732 }
733 }
734
735 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800736 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700737 return false;
738 }
739
740 *client = clientTemp;
741 return true;
742}
743
Ronghua Wu231c3d12015-03-11 15:10:32 -0700744} // namespace android