blob: be5af007535d55257dae5947035e3ebb04d0da61 [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);
Henry Fang32762922020-01-28 18:40:39 -080061
62 service->overridePid(mPid, -1);
Chong Zhangfdd512a2019-11-22 11:03:14 -080063}
Wonsik Kim3e378962017-01-05 17:00:02 +090064
Ronghua Wu231c3d12015-03-11 15:10:32 -070065template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070066static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070067 String8 itemsStr;
68 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070069 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 }
71 return itemsStr;
72}
73
Chong Zhangfb092d32019-08-12 09:45:44 -070074static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
75 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070076 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070077 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,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800108 const std::shared_ptr<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700109 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700110 ssize_t index = infos.indexOfKey(clientId);
111
112 if (index < 0) {
113 ResourceInfo info;
114 info.uid = uid;
115 info.clientId = clientId;
116 info.client = client;
117
118 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700119 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700120
121 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700122}
123
Chong Zhang181e6952019-10-09 13:23:39 -0700124static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900125 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700126 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900127 if (binder != NULL) {
128 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
129 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700130 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700131 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700132 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700133 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
134 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900135 }
136 }
137}
138
Chong Zhangfdd512a2019-11-22 11:03:14 -0800139binder_status_t ResourceManagerService::dump(
140 int fd, const char** /*args*/, uint32_t /*numArgs*/) {
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",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800146 AIBinder_getCallingPid(),
147 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700148 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;
Henry Fang32762922020-01-28 18:40:39 -0800155 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700156 String8 serviceLog;
157 {
158 Mutex::Autolock lock(mLock);
159 mapCopy = mMap; // Shadow copy, real copy will happen on write.
160 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
161 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
162 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800163 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700164 }
165
166 const size_t SIZE = 256;
167 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700168 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
169 result.append(buffer);
170 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700171 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700172 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700173 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
174 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700175 result.append(buffer);
176
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700177 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700178 for (size_t i = 0; i < mapCopy.size(); ++i) {
179 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700180 result.append(buffer);
181
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700182 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700183 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700184 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700185 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
186 result.append(buffer);
187
Chong Zhang181e6952019-10-09 13:23:39 -0700188 std::string clientName;
189 Status status = infos[j].client->getName(&clientName);
190 if (!status.isOk()) {
191 clientName = "<unknown client>";
192 }
193 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700194 result.append(buffer);
195
Chong Zhangfb092d32019-08-12 09:45:44 -0700196 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700197 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700198 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700199 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700200 result.append(buffer);
201 }
202 }
203 }
Henry Fang32762922020-01-28 18:40:39 -0800204 result.append(" Process Pid override:\n");
205 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
206 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
207 it->first, it->second);
208 result.append(buffer);
209 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700210 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700211 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700212
213 write(fd, result.string(), result.size());
214 return OK;
215}
216
Chong Zhangdd726802019-08-21 17:24:13 -0700217struct SystemCallbackImpl :
218 public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800219 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700220
Chong Zhangdd726802019-08-21 17:24:13 -0700221 virtual void noteStartVideo(int uid) override {
222 BatteryNotifier::getInstance().noteStartVideo(uid);
223 }
224 virtual void noteStopVideo(int uid) override {
225 BatteryNotifier::getInstance().noteStopVideo(uid);
226 }
227 virtual void noteResetVideo() override {
228 BatteryNotifier::getInstance().noteResetVideo();
229 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800230 virtual bool requestCpusetBoost(bool enable) override {
231 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700232 }
233
234protected:
235 virtual ~SystemCallbackImpl() {}
236
237private:
238 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800239 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700240};
241
242ResourceManagerService::ResourceManagerService()
243 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
244
245ResourceManagerService::ResourceManagerService(
246 const sp<ProcessInfoInterface> &processInfo,
247 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700248 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700249 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700250 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700251 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700252 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800253 mCpuBoostCount(0),
254 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700255 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700256}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700257
Chong Zhangfdd512a2019-11-22 11:03:14 -0800258//static
259void ResourceManagerService::instantiate() {
260 std::shared_ptr<ResourceManagerService> service =
261 ::ndk::SharedRefBase::make<ResourceManagerService>();
262 binder_status_t status =
263 AServiceManager_addService(service->asBinder().get(), getServiceName());
264 if (status != STATUS_OK) {
265 return;
266 }
267 // TODO: mediaserver main() is already starting the thread pool,
268 // move this to mediaserver main() when other services in mediaserver
269 // are converted to ndk-platform aidl.
270 //ABinderProcess_startThreadPool();
271}
272
Ronghua Wu231c3d12015-03-11 15:10:32 -0700273ResourceManagerService::~ResourceManagerService() {}
274
Chong Zhang181e6952019-10-09 13:23:39 -0700275Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700276 String8 log = String8::format("config(%s)", getString(policies).string());
277 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700278
279 Mutex::Autolock lock(mLock);
280 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700281 const std::string &type = policies[i].type;
282 const std::string &value = policies[i].value;
283 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700284 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700285 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700286 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700287 }
288 }
Chong Zhang181e6952019-10-09 13:23:39 -0700289 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700290}
291
Chong Zhangfb092d32019-08-12 09:45:44 -0700292void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700293 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700294 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700295 if (resource.type == MediaResource::Type::kCpuBoost
296 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700297 // Request it on every new instance of kCpuBoost, as the media.codec
298 // could have died, if we only do it the first time subsequent instances
299 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800300 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700301 ALOGW("couldn't request cpuset boost");
302 }
303 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700304 } else if (resource.type == MediaResource::Type::kBattery
305 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700306 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700307 }
308}
309
310void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700311 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
312 if (resource.type == MediaResource::Type::kCpuBoost
313 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700314 && mCpuBoostCount > 0) {
315 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800316 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700317 }
Chong Zhang181e6952019-10-09 13:23:39 -0700318 } else if (resource.type == MediaResource::Type::kBattery
319 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700320 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700321 }
322}
323
Robert Shihc3af31b2019-09-20 21:45:01 -0700324void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700325 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
326 // The resource entry on record is maintained to be in [0,INT64_MAX].
327 // Clamp if merging in the new resource value causes it to go out of bound.
328 // Note that the new resource value could be negative, eg.DrmSession, the
329 // value goes lower when the session is used more often. During reclaim
330 // the session with the highest value (lowest usage) would be closed.
331 if (r2.value < INT64_MAX - r1.value) {
332 r1.value += r2.value;
333 if (r1.value < 0) {
334 r1.value = 0;
335 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700336 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700337 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700338 }
339}
340
Chong Zhang181e6952019-10-09 13:23:39 -0700341Status ResourceManagerService::addResource(
342 int32_t pid,
343 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700344 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800345 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700346 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700347 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700348 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700349 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700350
351 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800352 if (!mProcessInfo->isValidPid(pid)) {
353 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700354 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800355 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700356 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700357 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700358
359 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700360 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700361 const auto resType = std::tuple(res.type, res.subType, res.id);
362
363 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
364 ALOGW("Ignoring request to remove negative value of non-drm resource");
365 continue;
366 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700367 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700368 if (res.value <= 0) {
369 // We can't init a new entry with negative value, although it's allowed
370 // to merge in negative values after the initial add.
371 ALOGW("Ignoring request to add new resource entry with value <= 0");
372 continue;
373 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700374 onFirstAdded(res, info);
375 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700376 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700377 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700378 }
379 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700380 if (info.deathNotifier == nullptr && client != nullptr) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800381 info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId);
382 AIBinder_linkToDeath(client->asBinder().get(),
383 mDeathRecipient.get(), info.deathNotifier.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900384 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900385 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700386 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700387}
388
Chong Zhang181e6952019-10-09 13:23:39 -0700389Status ResourceManagerService::removeResource(
390 int32_t pid, int64_t clientId,
391 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700392 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
393 pid, (long long) clientId, getString(resources).string());
394 mServiceLog->add(log);
395
396 Mutex::Autolock lock(mLock);
397 if (!mProcessInfo->isValidPid(pid)) {
398 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700399 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700400 }
401 ssize_t index = mMap.indexOfKey(pid);
402 if (index < 0) {
403 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700404 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700405 }
406 ResourceInfos &infos = mMap.editValueAt(index);
407
408 index = infos.indexOfKey(clientId);
409 if (index < 0) {
410 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700411 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700412 }
413
414 ResourceInfo &info = infos.editValueAt(index);
415
416 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700417 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700418 const auto resType = std::tuple(res.type, res.subType, res.id);
419
420 if (res.value < 0) {
421 ALOGW("Ignoring request to remove negative value of resource");
422 continue;
423 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700424 // ignore if we don't have it
425 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700426 MediaResourceParcel &resource = info.resources[resType];
427 if (resource.value > res.value) {
428 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700429 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700430 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700431 info.resources.erase(resType);
432 }
433 }
434 }
Chong Zhang181e6952019-10-09 13:23:39 -0700435 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700436}
437
Chong Zhang181e6952019-10-09 13:23:39 -0700438Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900439 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700440 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900441}
442
Chong Zhang181e6952019-10-09 13:23:39 -0700443Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700444 String8 log = String8::format(
445 "removeResource(pid %d, clientId %lld)",
446 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700447 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700448
449 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900450 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800451 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700452 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800453 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700454 ssize_t index = mMap.indexOfKey(pid);
455 if (index < 0) {
456 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700457 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700458 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700459 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700460
461 index = infos.indexOfKey(clientId);
462 if (index < 0) {
463 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700464 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700465 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700466
467 const ResourceInfo &info = infos[index];
468 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
469 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700470 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700471
Chong Zhangfdd512a2019-11-22 11:03:14 -0800472 AIBinder_unlinkToDeath(info.client->asBinder().get(),
473 mDeathRecipient.get(), info.deathNotifier.get());
Chong Zhangfb092d32019-08-12 09:45:44 -0700474
475 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700476 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700477}
478
Ronghua Wu05d89f12015-07-07 16:47:42 -0700479void ResourceManagerService::getClientForResource_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800480 int callingPid, const MediaResourceParcel *res,
481 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700482 if (res == NULL) {
483 return;
484 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800485 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700486 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700487 clients->push_back(client);
488 }
489}
490
Chong Zhang181e6952019-10-09 13:23:39 -0700491Status ResourceManagerService::reclaimResource(
492 int32_t callingPid,
493 const std::vector<MediaResourceParcel>& resources,
494 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700495 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700496 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700497 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700498 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700499
Chong Zhangfdd512a2019-11-22 11:03:14 -0800500 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700501 {
502 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800503 if (!mProcessInfo->isValidPid(callingPid)) {
504 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700505 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800506 }
Chong Zhang181e6952019-10-09 13:23:39 -0700507 const MediaResourceParcel *secureCodec = NULL;
508 const MediaResourceParcel *nonSecureCodec = NULL;
509 const MediaResourceParcel *graphicMemory = NULL;
510 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700511 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700512 MediaResource::Type type = resources[i].type;
513 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700514 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700515 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700516 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700517 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700518 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700519 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700520 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700521 }
522 }
523
524 // first pass to handle secure/non-secure codec conflict
525 if (secureCodec != NULL) {
526 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700527 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
528 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700529 }
530 }
531 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700532 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
533 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700534 }
535 }
536 }
537 if (nonSecureCodec != NULL) {
538 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700539 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
540 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700541 }
542 }
543 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700544 if (drmSession != NULL) {
545 getClientForResource_l(callingPid, drmSession, &clients);
546 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700547 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700548 }
549 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700550
551 if (clients.size() == 0) {
552 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700553 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700554 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700555
556 if (clients.size() == 0) {
557 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700558 getClientForResource_l(callingPid, secureCodec, &clients);
559 getClientForResource_l(callingPid, nonSecureCodec, &clients);
560 }
561
562 if (clients.size() == 0) {
563 // if we are here, run the fourth pass to free one codec with the different type.
564 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700565 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700566 getClientForResource_l(callingPid, &temp, &clients);
567 }
568 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700569 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700570 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700571 }
572 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700573 }
574
575 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700576 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700577 }
578
Chong Zhangfdd512a2019-11-22 11:03:14 -0800579 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700580 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700581 log = String8::format("reclaimResource from client %p", clients[i].get());
582 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700583 bool success;
584 Status status = clients[i]->reclaimResource(&success);
585 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700586 failedClient = clients[i];
587 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700588 }
589 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700590
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700591 if (failedClient == NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700592 *_aidl_return = true;
593 return Status::ok();
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700594 }
595
Ronghua Wu67e7f542015-03-13 10:47:08 -0700596 {
597 Mutex::Autolock lock(mLock);
598 bool found = false;
599 for (size_t i = 0; i < mMap.size(); ++i) {
600 ResourceInfos &infos = mMap.editValueAt(i);
601 for (size_t j = 0; j < infos.size();) {
602 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700603 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700604 found = true;
605 } else {
606 ++j;
607 }
608 }
609 if (found) {
610 break;
611 }
612 }
613 if (!found) {
614 ALOGV("didn't find failed client");
615 }
616 }
617
Chong Zhang181e6952019-10-09 13:23:39 -0700618 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700619}
620
Henry Fang32762922020-01-28 18:40:39 -0800621Status ResourceManagerService::overridePid(
622 int originalPid,
623 int newPid) {
624 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
625 originalPid, newPid);
626 mServiceLog->add(log);
627
628 // allow if this is called from the same process or the process has
629 // permission.
630 if ((AIBinder_getCallingPid() != getpid()) &&
631 (checkCallingPermission(String16(
632 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
633 ALOGE(
634 "Permission Denial: can't access overridePid method from pid=%d, "
635 "self pid=%d\n",
636 AIBinder_getCallingPid(), getpid());
637 return Status::fromServiceSpecificError(PERMISSION_DENIED);
638 }
639
640 {
641 Mutex::Autolock lock(mLock);
642 mOverridePidMap.erase(originalPid);
643 if (newPid != -1) {
644 mOverridePidMap.emplace(originalPid, newPid);
645 }
646 }
647
648 return Status::ok();
649}
650
651bool ResourceManagerService::getPriority_l(int pid, int* priority) {
652 int newPid = pid;
653
654 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
655 newPid = mOverridePidMap[pid];
656 ALOGD("getPriority_l: use override pid %d instead original pid %d",
657 newPid, pid);
658 }
659
660 return mProcessInfo->getPriority(newPid, priority);
661}
662
Ronghua Wu231c3d12015-03-11 15:10:32 -0700663bool ResourceManagerService::getAllClients_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800664 int callingPid, MediaResource::Type type,
665 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
666 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700667 for (size_t i = 0; i < mMap.size(); ++i) {
668 ResourceInfos &infos = mMap.editValueAt(i);
669 for (size_t j = 0; j < infos.size(); ++j) {
670 if (hasResourceType(type, infos[j].resources)) {
671 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
672 // some higher/equal priority process owns the resource,
673 // this request can't be fulfilled.
674 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800675 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700676 return false;
677 }
678 temp.push_back(infos[j].client);
679 }
680 }
681 }
682 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800683 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700684 return true;
685 }
686 clients->appendVector(temp);
687 return true;
688}
689
690bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800691 int callingPid, MediaResource::Type type,
692 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700693 int lowestPriorityPid;
694 int lowestPriority;
695 int callingPriority;
Henry Fang32762922020-01-28 18:40:39 -0800696 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700697 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
698 callingPid);
699 return false;
700 }
701 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
702 return false;
703 }
704 if (lowestPriority <= callingPriority) {
705 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
706 lowestPriority, callingPriority);
707 return false;
708 }
709
710 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
711 return false;
712 }
713 return true;
714}
715
716bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800717 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700718 int pid = -1;
719 int priority = -1;
720 for (size_t i = 0; i < mMap.size(); ++i) {
721 if (mMap.valueAt(i).size() == 0) {
722 // no client on this process.
723 continue;
724 }
725 if (!hasResourceType(type, mMap.valueAt(i))) {
726 // doesn't have the requested resource type
727 continue;
728 }
729 int tempPid = mMap.keyAt(i);
730 int tempPriority;
Henry Fang32762922020-01-28 18:40:39 -0800731 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700732 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
733 // TODO: remove this pid from mMap?
734 continue;
735 }
736 if (pid == -1 || tempPriority > priority) {
737 // initial the value
738 pid = tempPid;
739 priority = tempPriority;
740 }
741 }
742 if (pid != -1) {
743 *lowestPriorityPid = pid;
744 *lowestPriority = priority;
745 }
746 return (pid != -1);
747}
748
749bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
750 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800751 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700752 return false;
753 }
754
755 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800756 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700757 return false;
758 }
759
760 return (callingPidPriority < priority);
761}
762
763bool ResourceManagerService::getBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800764 int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700765 ssize_t index = mMap.indexOfKey(pid);
766 if (index < 0) {
767 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
768 return false;
769 }
770
Chong Zhangfdd512a2019-11-22 11:03:14 -0800771 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700772 uint64_t largestValue = 0;
773 const ResourceInfos &infos = mMap.valueAt(index);
774 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700775 const ResourceList &resources = infos[i].resources;
776 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700777 const MediaResourceParcel &resource = it->second;
778 if (resource.type == type) {
779 if (resource.value > largestValue) {
780 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700781 clientTemp = infos[i].client;
782 }
783 }
784 }
785 }
786
787 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800788 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700789 return false;
790 }
791
792 *client = clientTemp;
793 return true;
794}
795
Ronghua Wu231c3d12015-03-11 15:10:32 -0700796} // namespace android