blob: 7ee52c501f911c2e97646b04dc9a91f56985ab8b [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"
Chong Zhanga9d45c72020-09-09 12:41:17 -070039#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070040#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070041
Ronghua Wu231c3d12015-03-11 15:10:32 -070042namespace android {
43
Chong Zhangfdd512a2019-11-22 11:03:14 -080044DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
45 int pid, int64_t clientId)
46 : mService(service), mPid(pid), mClientId(clientId) {}
Wonsik Kim3e378962017-01-05 17:00:02 +090047
Chong Zhangfdd512a2019-11-22 11:03:14 -080048//static
49void DeathNotifier::BinderDiedCallback(void* cookie) {
50 auto thiz = static_cast<DeathNotifier*>(cookie);
51 thiz->binderDied();
52}
Wonsik Kim3e378962017-01-05 17:00:02 +090053
Chong Zhangfdd512a2019-11-22 11:03:14 -080054void DeathNotifier::binderDied() {
55 // Don't check for pid validity since we know it's already dead.
56 std::shared_ptr<ResourceManagerService> service = mService.lock();
57 if (service == nullptr) {
58 ALOGW("ResourceManagerService is dead as well.");
59 return;
Wonsik Kim3e378962017-01-05 17:00:02 +090060 }
Henry Fang32762922020-01-28 18:40:39 -080061
62 service->overridePid(mPid, -1);
Henry Fangb35141c2020-06-29 13:11:39 -070063 // thiz is freed in the call below, so it must be last call referring thiz
64 service->removeResource(mPid, mClientId, false);
65
Chong Zhangfdd512a2019-11-22 11:03:14 -080066}
Wonsik Kim3e378962017-01-05 17:00:02 +090067
Ronghua Wu231c3d12015-03-11 15:10:32 -070068template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070069static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 String8 itemsStr;
71 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070072 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070073 }
74 return itemsStr;
75}
76
Chong Zhangfb092d32019-08-12 09:45:44 -070077static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
78 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070079 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070080 return true;
81 }
82 }
83 return false;
84}
85
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070086static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070087 for (size_t i = 0; i < infos.size(); ++i) {
88 if (hasResourceType(type, infos[i].resources)) {
89 return true;
90 }
91 }
92 return false;
93}
94
95static ResourceInfos& getResourceInfosForEdit(
96 int pid,
97 PidResourceInfosMap& map) {
98 ssize_t index = map.indexOfKey(pid);
99 if (index < 0) {
100 // new pid
101 ResourceInfos infosForPid;
102 map.add(pid, infosForPid);
103 }
104
105 return map.editValueFor(pid);
106}
107
108static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700109 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700110 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800111 const std::shared_ptr<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700112 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700113 ssize_t index = infos.indexOfKey(clientId);
114
115 if (index < 0) {
116 ResourceInfo info;
117 info.uid = uid;
118 info.clientId = clientId;
119 info.client = client;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700120 info.pendingRemoval = false;
Chong Zhangfb092d32019-08-12 09:45:44 -0700121
122 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700124
125 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126}
127
Chong Zhang181e6952019-10-09 13:23:39 -0700128static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900129 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700130 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900131 if (binder != NULL) {
132 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
133 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700134 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700135 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700136 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700137 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
138 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900139 }
140 }
141}
142
Chong Zhangfdd512a2019-11-22 11:03:14 -0800143binder_status_t ResourceManagerService::dump(
144 int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700145 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700146
dcashman014e91e2015-09-11 09:33:01 -0700147 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
148 result.format("Permission Denial: "
149 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800150 AIBinder_getCallingPid(),
151 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700152 write(fd, result.string(), result.size());
153 return PERMISSION_DENIED;
154 }
155
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700156 PidResourceInfosMap mapCopy;
157 bool supportsMultipleSecureCodecs;
158 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800159 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700160 String8 serviceLog;
161 {
162 Mutex::Autolock lock(mLock);
163 mapCopy = mMap; // Shadow copy, real copy will happen on write.
164 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
165 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
166 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800167 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700168 }
169
170 const size_t SIZE = 256;
171 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700172 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
173 result.append(buffer);
174 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700175 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700177 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
178 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700179 result.append(buffer);
180
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700181 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700182 for (size_t i = 0; i < mapCopy.size(); ++i) {
183 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700184 result.append(buffer);
185
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700186 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700187 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700188 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700189 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
190 result.append(buffer);
191
Chong Zhang181e6952019-10-09 13:23:39 -0700192 std::string clientName;
193 Status status = infos[j].client->getName(&clientName);
194 if (!status.isOk()) {
195 clientName = "<unknown client>";
196 }
197 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700198 result.append(buffer);
199
Chong Zhangfb092d32019-08-12 09:45:44 -0700200 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700201 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700202 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700203 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700204 result.append(buffer);
205 }
206 }
207 }
Henry Fang32762922020-01-28 18:40:39 -0800208 result.append(" Process Pid override:\n");
209 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
210 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
211 it->first, it->second);
212 result.append(buffer);
213 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700214 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700215 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700216
217 write(fd, result.string(), result.size());
218 return OK;
219}
220
Chong Zhangdd726802019-08-21 17:24:13 -0700221struct SystemCallbackImpl :
222 public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800223 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700224
Chong Zhangdd726802019-08-21 17:24:13 -0700225 virtual void noteStartVideo(int uid) override {
226 BatteryNotifier::getInstance().noteStartVideo(uid);
227 }
228 virtual void noteStopVideo(int uid) override {
229 BatteryNotifier::getInstance().noteStopVideo(uid);
230 }
231 virtual void noteResetVideo() override {
232 BatteryNotifier::getInstance().noteResetVideo();
233 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800234 virtual bool requestCpusetBoost(bool enable) override {
235 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700236 }
237
238protected:
239 virtual ~SystemCallbackImpl() {}
240
241private:
242 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800243 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700244};
245
246ResourceManagerService::ResourceManagerService()
247 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
248
249ResourceManagerService::ResourceManagerService(
250 const sp<ProcessInfoInterface> &processInfo,
251 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700253 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700254 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700255 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700256 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800257 mCpuBoostCount(0),
258 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700259 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700260}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700261
Chong Zhangfdd512a2019-11-22 11:03:14 -0800262//static
263void ResourceManagerService::instantiate() {
264 std::shared_ptr<ResourceManagerService> service =
265 ::ndk::SharedRefBase::make<ResourceManagerService>();
266 binder_status_t status =
267 AServiceManager_addService(service->asBinder().get(), getServiceName());
268 if (status != STATUS_OK) {
269 return;
270 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700271
272 std::shared_ptr<ResourceObserverService> observerService =
273 ResourceObserverService::instantiate();
274
275 if (observerService != nullptr) {
276 service->setObserverService(observerService);
277 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800278 // TODO: mediaserver main() is already starting the thread pool,
279 // move this to mediaserver main() when other services in mediaserver
280 // are converted to ndk-platform aidl.
281 //ABinderProcess_startThreadPool();
282}
283
Ronghua Wu231c3d12015-03-11 15:10:32 -0700284ResourceManagerService::~ResourceManagerService() {}
285
Chong Zhanga9d45c72020-09-09 12:41:17 -0700286void ResourceManagerService::setObserverService(
287 const std::shared_ptr<ResourceObserverService>& observerService) {
288 mObserverService = observerService;
289}
290
Chong Zhang181e6952019-10-09 13:23:39 -0700291Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700292 String8 log = String8::format("config(%s)", getString(policies).string());
293 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700294
295 Mutex::Autolock lock(mLock);
296 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700297 const std::string &type = policies[i].type;
298 const std::string &value = policies[i].value;
299 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700300 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700301 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700302 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700303 }
304 }
Chong Zhang181e6952019-10-09 13:23:39 -0700305 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700306}
307
Chong Zhangfb092d32019-08-12 09:45:44 -0700308void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700309 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700310 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700311 if (resource.type == MediaResource::Type::kCpuBoost
312 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700313 // Request it on every new instance of kCpuBoost, as the media.codec
314 // could have died, if we only do it the first time subsequent instances
315 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800316 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700317 ALOGW("couldn't request cpuset boost");
318 }
319 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700320 } else if (resource.type == MediaResource::Type::kBattery
321 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700322 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700323 }
324}
325
326void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700327 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
328 if (resource.type == MediaResource::Type::kCpuBoost
329 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700330 && mCpuBoostCount > 0) {
331 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800332 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700333 }
Chong Zhang181e6952019-10-09 13:23:39 -0700334 } else if (resource.type == MediaResource::Type::kBattery
335 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700336 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700337 }
338}
339
Robert Shihc3af31b2019-09-20 21:45:01 -0700340void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700341 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
342 // The resource entry on record is maintained to be in [0,INT64_MAX].
343 // Clamp if merging in the new resource value causes it to go out of bound.
344 // Note that the new resource value could be negative, eg.DrmSession, the
345 // value goes lower when the session is used more often. During reclaim
346 // the session with the highest value (lowest usage) would be closed.
347 if (r2.value < INT64_MAX - r1.value) {
348 r1.value += r2.value;
349 if (r1.value < 0) {
350 r1.value = 0;
351 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700352 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700353 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700354 }
355}
356
Chong Zhang181e6952019-10-09 13:23:39 -0700357Status ResourceManagerService::addResource(
358 int32_t pid,
359 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700360 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800361 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700362 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700363 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700364 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700365 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700366
367 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800368 if (!mProcessInfo->isValidPid(pid)) {
369 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700370 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800371 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700372 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700373 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700374 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700375
376 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700377 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700378 const auto resType = std::tuple(res.type, res.subType, res.id);
379
380 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
381 ALOGW("Ignoring request to remove negative value of non-drm resource");
382 continue;
383 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700384 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700385 if (res.value <= 0) {
386 // We can't init a new entry with negative value, although it's allowed
387 // to merge in negative values after the initial add.
388 ALOGW("Ignoring request to add new resource entry with value <= 0");
389 continue;
390 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700391 onFirstAdded(res, info);
392 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700393 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700394 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700395 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700396 // Add it to the list of added resources for observers.
397 auto it = resourceAdded.find(resType);
398 if (it == resourceAdded.end()) {
399 resourceAdded[resType] = res;
400 } else {
401 mergeResources(it->second, res);
402 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700403 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700404 if (info.deathNotifier == nullptr && client != nullptr) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800405 info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId);
406 AIBinder_linkToDeath(client->asBinder().get(),
407 mDeathRecipient.get(), info.deathNotifier.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900408 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700409 if (mObserverService != nullptr && !resourceAdded.empty()) {
410 mObserverService->onResourceAdded(uid, pid, resourceAdded);
411 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900412 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700413 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700414}
415
Chong Zhang181e6952019-10-09 13:23:39 -0700416Status ResourceManagerService::removeResource(
417 int32_t pid, int64_t clientId,
418 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700419 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
420 pid, (long long) clientId, getString(resources).string());
421 mServiceLog->add(log);
422
423 Mutex::Autolock lock(mLock);
424 if (!mProcessInfo->isValidPid(pid)) {
425 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700426 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700427 }
428 ssize_t index = mMap.indexOfKey(pid);
429 if (index < 0) {
430 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700431 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700432 }
433 ResourceInfos &infos = mMap.editValueAt(index);
434
435 index = infos.indexOfKey(clientId);
436 if (index < 0) {
437 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700438 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700439 }
440
441 ResourceInfo &info = infos.editValueAt(index);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700442 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700443 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700444 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700445 const auto resType = std::tuple(res.type, res.subType, res.id);
446
447 if (res.value < 0) {
448 ALOGW("Ignoring request to remove negative value of resource");
449 continue;
450 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700451 // ignore if we don't have it
452 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700453 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700454 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700455 if (resource.value > res.value) {
456 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700457 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700458 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700459 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700460 actualRemoved.value = resource.value;
461 }
462
463 // Add it to the list of removed resources for observers.
464 auto it = resourceRemoved.find(resType);
465 if (it == resourceRemoved.end()) {
466 resourceRemoved[resType] = actualRemoved;
467 } else {
468 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700469 }
470 }
471 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700472 if (mObserverService != nullptr && !resourceRemoved.empty()) {
473 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
474 }
Chong Zhang181e6952019-10-09 13:23:39 -0700475 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700476}
477
Chong Zhang181e6952019-10-09 13:23:39 -0700478Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900479 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700480 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900481}
482
Chong Zhang181e6952019-10-09 13:23:39 -0700483Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700484 String8 log = String8::format(
485 "removeResource(pid %d, clientId %lld)",
486 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700487 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700488
489 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900490 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800491 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700492 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800493 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700494 ssize_t index = mMap.indexOfKey(pid);
495 if (index < 0) {
496 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700497 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700498 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700499 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700500
501 index = infos.indexOfKey(clientId);
502 if (index < 0) {
503 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700504 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700505 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700506
507 const ResourceInfo &info = infos[index];
508 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
509 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700510 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700511
Chong Zhangfdd512a2019-11-22 11:03:14 -0800512 AIBinder_unlinkToDeath(info.client->asBinder().get(),
513 mDeathRecipient.get(), info.deathNotifier.get());
Chong Zhangfb092d32019-08-12 09:45:44 -0700514
Chong Zhanga9d45c72020-09-09 12:41:17 -0700515 if (mObserverService != nullptr && !info.resources.empty()) {
516 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
517 }
518
Chong Zhangfb092d32019-08-12 09:45:44 -0700519 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700520 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700521}
522
Ronghua Wu05d89f12015-07-07 16:47:42 -0700523void ResourceManagerService::getClientForResource_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800524 int callingPid, const MediaResourceParcel *res,
525 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700526 if (res == NULL) {
527 return;
528 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800529 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700530 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700531 clients->push_back(client);
532 }
533}
534
Chong Zhang181e6952019-10-09 13:23:39 -0700535Status ResourceManagerService::reclaimResource(
536 int32_t callingPid,
537 const std::vector<MediaResourceParcel>& resources,
538 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700539 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700540 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700541 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700542 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700543
Chong Zhangfdd512a2019-11-22 11:03:14 -0800544 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700545 {
546 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800547 if (!mProcessInfo->isValidPid(callingPid)) {
548 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700549 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800550 }
Chong Zhang181e6952019-10-09 13:23:39 -0700551 const MediaResourceParcel *secureCodec = NULL;
552 const MediaResourceParcel *nonSecureCodec = NULL;
553 const MediaResourceParcel *graphicMemory = NULL;
554 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700555 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700556 MediaResource::Type type = resources[i].type;
557 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700558 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700559 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700560 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700561 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700562 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700563 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700564 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700565 }
566 }
567
568 // first pass to handle secure/non-secure codec conflict
569 if (secureCodec != NULL) {
570 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700571 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
572 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700573 }
574 }
575 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700576 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
577 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700578 }
579 }
580 }
581 if (nonSecureCodec != NULL) {
582 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700583 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
584 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700585 }
586 }
587 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700588 if (drmSession != NULL) {
589 getClientForResource_l(callingPid, drmSession, &clients);
590 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700591 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700592 }
593 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700594
595 if (clients.size() == 0) {
596 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700597 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700598 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700599
600 if (clients.size() == 0) {
601 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700602 getClientForResource_l(callingPid, secureCodec, &clients);
603 getClientForResource_l(callingPid, nonSecureCodec, &clients);
604 }
605
606 if (clients.size() == 0) {
607 // if we are here, run the fourth pass to free one codec with the different type.
608 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700609 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700610 getClientForResource_l(callingPid, &temp, &clients);
611 }
612 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700613 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700614 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700615 }
616 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700617 }
618
619 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700620 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700621 }
622
Chong Zhangfdd512a2019-11-22 11:03:14 -0800623 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700624 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700625 log = String8::format("reclaimResource from client %p", clients[i].get());
626 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700627 bool success;
628 Status status = clients[i]->reclaimResource(&success);
629 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700630 failedClient = clients[i];
631 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700632 }
633 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700634
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700635 if (failedClient == NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700636 *_aidl_return = true;
637 return Status::ok();
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700638 }
639
Ronghua Wu67e7f542015-03-13 10:47:08 -0700640 {
641 Mutex::Autolock lock(mLock);
642 bool found = false;
643 for (size_t i = 0; i < mMap.size(); ++i) {
644 ResourceInfos &infos = mMap.editValueAt(i);
645 for (size_t j = 0; j < infos.size();) {
646 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700647 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700648 found = true;
649 } else {
650 ++j;
651 }
652 }
653 if (found) {
654 break;
655 }
656 }
657 if (!found) {
658 ALOGV("didn't find failed client");
659 }
660 }
661
Chong Zhang181e6952019-10-09 13:23:39 -0700662 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700663}
664
Henry Fang32762922020-01-28 18:40:39 -0800665Status ResourceManagerService::overridePid(
666 int originalPid,
667 int newPid) {
668 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
669 originalPid, newPid);
670 mServiceLog->add(log);
671
672 // allow if this is called from the same process or the process has
673 // permission.
674 if ((AIBinder_getCallingPid() != getpid()) &&
675 (checkCallingPermission(String16(
676 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
677 ALOGE(
678 "Permission Denial: can't access overridePid method from pid=%d, "
679 "self pid=%d\n",
680 AIBinder_getCallingPid(), getpid());
681 return Status::fromServiceSpecificError(PERMISSION_DENIED);
682 }
683
684 {
685 Mutex::Autolock lock(mLock);
686 mOverridePidMap.erase(originalPid);
687 if (newPid != -1) {
688 mOverridePidMap.emplace(originalPid, newPid);
689 }
690 }
691
692 return Status::ok();
693}
694
Wonsik Kimd20e9362020-04-28 10:42:57 -0700695Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) {
696 String8 log = String8::format(
697 "markClientForPendingRemoval(pid %d, clientId %lld)",
698 pid, (long long) clientId);
699 mServiceLog->add(log);
700
701 Mutex::Autolock lock(mLock);
702 if (!mProcessInfo->isValidPid(pid)) {
703 ALOGE("Rejected markClientForPendingRemoval call with invalid pid.");
704 return Status::fromServiceSpecificError(BAD_VALUE);
705 }
706 ssize_t index = mMap.indexOfKey(pid);
707 if (index < 0) {
708 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
709 pid, (long long)clientId);
710 return Status::ok();
711 }
712 ResourceInfos &infos = mMap.editValueAt(index);
713
714 index = infos.indexOfKey(clientId);
715 if (index < 0) {
716 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
717 return Status::ok();
718 }
719
720 ResourceInfo &info = infos.editValueAt(index);
721 info.pendingRemoval = true;
722 return Status::ok();
723}
724
Henry Fang32762922020-01-28 18:40:39 -0800725bool ResourceManagerService::getPriority_l(int pid, int* priority) {
726 int newPid = pid;
727
728 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
729 newPid = mOverridePidMap[pid];
730 ALOGD("getPriority_l: use override pid %d instead original pid %d",
731 newPid, pid);
732 }
733
734 return mProcessInfo->getPriority(newPid, priority);
735}
736
Ronghua Wu231c3d12015-03-11 15:10:32 -0700737bool ResourceManagerService::getAllClients_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800738 int callingPid, MediaResource::Type type,
739 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
740 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700741 for (size_t i = 0; i < mMap.size(); ++i) {
742 ResourceInfos &infos = mMap.editValueAt(i);
743 for (size_t j = 0; j < infos.size(); ++j) {
744 if (hasResourceType(type, infos[j].resources)) {
745 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
746 // some higher/equal priority process owns the resource,
747 // this request can't be fulfilled.
748 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800749 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700750 return false;
751 }
752 temp.push_back(infos[j].client);
753 }
754 }
755 }
756 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800757 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700758 return true;
759 }
760 clients->appendVector(temp);
761 return true;
762}
763
764bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800765 int callingPid, MediaResource::Type type,
766 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700767 int lowestPriorityPid;
768 int lowestPriority;
769 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700770
771 // Before looking into other processes, check if we have clients marked for
772 // pending removal in the same process.
773 if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) {
774 return true;
775 }
Henry Fang32762922020-01-28 18:40:39 -0800776 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700777 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
778 callingPid);
779 return false;
780 }
781 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
782 return false;
783 }
784 if (lowestPriority <= callingPriority) {
785 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
786 lowestPriority, callingPriority);
787 return false;
788 }
789
790 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
791 return false;
792 }
793 return true;
794}
795
796bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800797 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700798 int pid = -1;
799 int priority = -1;
800 for (size_t i = 0; i < mMap.size(); ++i) {
801 if (mMap.valueAt(i).size() == 0) {
802 // no client on this process.
803 continue;
804 }
805 if (!hasResourceType(type, mMap.valueAt(i))) {
806 // doesn't have the requested resource type
807 continue;
808 }
809 int tempPid = mMap.keyAt(i);
810 int tempPriority;
Henry Fang32762922020-01-28 18:40:39 -0800811 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700812 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
813 // TODO: remove this pid from mMap?
814 continue;
815 }
816 if (pid == -1 || tempPriority > priority) {
817 // initial the value
818 pid = tempPid;
819 priority = tempPriority;
820 }
821 }
822 if (pid != -1) {
823 *lowestPriorityPid = pid;
824 *lowestPriority = priority;
825 }
826 return (pid != -1);
827}
828
829bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
830 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800831 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700832 return false;
833 }
834
835 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800836 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700837 return false;
838 }
839
840 return (callingPidPriority < priority);
841}
842
843bool ResourceManagerService::getBiggestClient_l(
Wonsik Kimd20e9362020-04-28 10:42:57 -0700844 int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client,
845 bool pendingRemovalOnly) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700846 ssize_t index = mMap.indexOfKey(pid);
847 if (index < 0) {
848 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
849 return false;
850 }
851
Chong Zhangfdd512a2019-11-22 11:03:14 -0800852 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700853 uint64_t largestValue = 0;
854 const ResourceInfos &infos = mMap.valueAt(index);
855 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700856 const ResourceList &resources = infos[i].resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700857 if (pendingRemovalOnly && !infos[i].pendingRemoval) {
858 continue;
859 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700860 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700861 const MediaResourceParcel &resource = it->second;
862 if (resource.type == type) {
863 if (resource.value > largestValue) {
864 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700865 clientTemp = infos[i].client;
866 }
867 }
868 }
869 }
870
871 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800872 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700873 return false;
874 }
875
876 *client = clientTemp;
877 return true;
878}
879
Ronghua Wu231c3d12015-03-11 15:10:32 -0700880} // namespace android