blob: 68794e49597d92bac7d82bba5a62c099e66f6d20 [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>
Chong Zhangc7303e82020-12-02 16:38:52 -080024#include <binder/IPCThreadState.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>
Chong Zhang181e6952019-10-09 13:23:39 -070027#include <media/MediaResourcePolicy.h>
Girish1f002cf2023-02-17 00:36:29 +000028#include <media/stagefright/foundation/ABase.h>
Chong Zhangee33d642019-08-08 14:26:43 -070029#include <mediautils/BatteryNotifier.h>
Girish1f002cf2023-02-17 00:36:29 +000030#include <mediautils/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070031#include <mediautils/SchedulingPolicyService.h>
Girish1484e5d2023-11-20 06:00:44 +000032#include <com_android_media_codec_flags.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070033
Steven Moreland0a0ff0b2021-04-02 00:06:01 +000034#include "IMediaResourceMonitor.h"
Girish1f002cf2023-02-17 00:36:29 +000035#include "ResourceManagerMetrics.h"
Girish1484e5d2023-11-20 06:00:44 +000036#include "ResourceManagerServiceNew.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070037#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070038#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070039
Girish1484e5d2023-11-20 06:00:44 +000040namespace CodecFeatureFlags = com::android::media::codec::flags;
41
Ronghua Wu231c3d12015-03-11 15:10:32 -070042namespace android {
43
Girish434b4d82023-07-11 23:24:54 +000044static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +090045 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -070046 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +090047 if (binder != NULL) {
48 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
49 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +010050 switch (resources[i].subType) {
Girisha5a2d672023-09-20 18:40:20 +000051 case MediaResource::SubType::kHwAudioCodec:
52 case MediaResource::SubType::kSwAudioCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010053 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
54 break;
Girisha5a2d672023-09-20 18:40:20 +000055 case MediaResource::SubType::kHwVideoCodec:
56 case MediaResource::SubType::kSwVideoCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010057 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
58 break;
Girisha5a2d672023-09-20 18:40:20 +000059 case MediaResource::SubType::kHwImageCodec:
60 case MediaResource::SubType::kSwImageCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010061 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
62 break;
63 case MediaResource::SubType::kUnspecifiedSubType:
64 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -070065 }
Dongwon Kangfe508d32015-12-15 14:22:05 +090066 }
67 }
68}
69
Brian Lindahl64ee9452022-01-14 13:31:16 +010070binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070071 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -070072
dcashman014e91e2015-09-11 09:33:01 -070073 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
74 result.format("Permission Denial: "
75 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -080076 AIBinder_getCallingPid(),
77 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +000078 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -070079 return PERMISSION_DENIED;
80 }
81
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070082 PidResourceInfosMap mapCopy;
83 bool supportsMultipleSecureCodecs;
84 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -080085 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070086 String8 serviceLog;
87 {
Girish434b4d82023-07-11 23:24:54 +000088 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070089 mapCopy = mMap; // Shadow copy, real copy will happen on write.
90 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
91 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
92 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -080093 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070094 }
95
96 const size_t SIZE = 256;
97 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -070098 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
99 result.append(buffer);
100 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700101 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700102 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700103 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
104 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700105 result.append(buffer);
106
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700107 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000108 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000109 snprintf(buffer, SIZE, " Pid: %d\n", pid);
110 result.append(buffer);
111 int priority = 0;
112 if (getPriority_l(pid, &priority)) {
113 snprintf(buffer, SIZE, " Priority: %d\n", priority);
114 } else {
115 snprintf(buffer, SIZE, " Priority: <unknown>\n");
116 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700117 result.append(buffer);
118
Girish434b4d82023-07-11 23:24:54 +0000119 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700120 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000121 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700122 result.append(buffer);
123
Girish434b4d82023-07-11 23:24:54 +0000124 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700125 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700126 result.append(buffer);
127
Girish434b4d82023-07-11 23:24:54 +0000128 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700129 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700130 for (auto it = resources.begin(); it != resources.end(); it++) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000131 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700132 result.append(buffer);
133 }
134 }
135 }
Henry Fang32762922020-01-28 18:40:39 -0800136 result.append(" Process Pid override:\n");
137 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
138 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
139 it->first, it->second);
140 result.append(buffer);
141 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700142 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700143 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700144
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000145 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700146 return OK;
147}
148
Brian Lindahl64ee9452022-01-14 13:31:16 +0100149struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800150 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700151
Chong Zhangdd726802019-08-21 17:24:13 -0700152 virtual void noteStartVideo(int uid) override {
153 BatteryNotifier::getInstance().noteStartVideo(uid);
154 }
155 virtual void noteStopVideo(int uid) override {
156 BatteryNotifier::getInstance().noteStopVideo(uid);
157 }
158 virtual void noteResetVideo() override {
159 BatteryNotifier::getInstance().noteResetVideo();
160 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800161 virtual bool requestCpusetBoost(bool enable) override {
162 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700163 }
164
165protected:
166 virtual ~SystemCallbackImpl() {}
167
168private:
169 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800170 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700171};
172
173ResourceManagerService::ResourceManagerService()
174 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
175
Brian Lindahl64ee9452022-01-14 13:31:16 +0100176ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700177 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700178 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700179 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700180 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700181 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700182 mSupportsSecureWithNonSecureCodec(true),
Girishab17b0f2023-11-20 06:00:44 +0000183 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700184 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000185 // Create ResourceManagerMetrics that handles all the metrics.
186 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700187}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700188
Chong Zhangfdd512a2019-11-22 11:03:14 -0800189//static
190void ResourceManagerService::instantiate() {
Girishab17b0f2023-11-20 06:00:44 +0000191 std::shared_ptr<ResourceManagerService> service = Create();
Chong Zhangfdd512a2019-11-22 11:03:14 -0800192 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000193 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000194 service->asBinder().get(), getServiceName(),
195 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800196 if (status != STATUS_OK) {
197 return;
198 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700199
200 std::shared_ptr<ResourceObserverService> observerService =
201 ResourceObserverService::instantiate();
202
203 if (observerService != nullptr) {
204 service->setObserverService(observerService);
205 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800206 // TODO: mediaserver main() is already starting the thread pool,
207 // move this to mediaserver main() when other services in mediaserver
208 // are converted to ndk-platform aidl.
209 //ABinderProcess_startThreadPool();
210}
211
Girishab17b0f2023-11-20 06:00:44 +0000212std::shared_ptr<ResourceManagerService> ResourceManagerService::Create() {
213 return Create(new ProcessInfo(), new SystemCallbackImpl());
214}
215
216std::shared_ptr<ResourceManagerService> ResourceManagerService::Create(
217 const sp<ProcessInfoInterface>& processInfo,
218 const sp<SystemCallbackInterface>& systemResource) {
Girish1484e5d2023-11-20 06:00:44 +0000219 // If codec importance feature is on, create the refactored implementation.
220 if (CodecFeatureFlags::codec_importance()) {
221 return ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo, systemResource);
222 }
Girishab17b0f2023-11-20 06:00:44 +0000223 return ::ndk::SharedRefBase::make<ResourceManagerService>(processInfo, systemResource);
224}
225
Girish1484e5d2023-11-20 06:00:44 +0000226// TEST only function.
227std::shared_ptr<ResourceManagerService> ResourceManagerService::CreateNew(
228 const sp<ProcessInfoInterface>& processInfo,
229 const sp<SystemCallbackInterface>& systemResource) {
230 return ::ndk::SharedRefBase::make<ResourceManagerServiceNew>(processInfo, systemResource);
231}
232
Ronghua Wu231c3d12015-03-11 15:10:32 -0700233ResourceManagerService::~ResourceManagerService() {}
234
Chong Zhanga9d45c72020-09-09 12:41:17 -0700235void ResourceManagerService::setObserverService(
236 const std::shared_ptr<ResourceObserverService>& observerService) {
237 mObserverService = observerService;
238}
239
Chong Zhang181e6952019-10-09 13:23:39 -0700240Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000241 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700242 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700243
Girish434b4d82023-07-11 23:24:54 +0000244 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700245 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700246 const std::string &type = policies[i].type;
247 const std::string &value = policies[i].value;
248 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700249 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700250 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700251 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252 }
253 }
Chong Zhang181e6952019-10-09 13:23:39 -0700254 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700255}
256
Girishab17b0f2023-11-20 06:00:44 +0000257void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700258 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700259 if (resource.type == MediaResource::Type::kCpuBoost
260 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700261 // Request it on every new instance of kCpuBoost, as the media.codec
262 // could have died, if we only do it the first time subsequent instances
263 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800264 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700265 ALOGW("couldn't request cpuset boost");
266 }
267 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700268 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000269 && (resource.subType == MediaResource::SubType::kHwVideoCodec
270 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000271 mSystemCB->noteStartVideo(uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700272 }
273}
274
Girishab17b0f2023-11-20 06:00:44 +0000275void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhang181e6952019-10-09 13:23:39 -0700276 if (resource.type == MediaResource::Type::kCpuBoost
277 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700278 && mCpuBoostCount > 0) {
279 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800280 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700281 }
Chong Zhang181e6952019-10-09 13:23:39 -0700282 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000283 && (resource.subType == MediaResource::SubType::kHwVideoCodec
284 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000285 mSystemCB->noteStopVideo(uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700286 }
287}
288
Girish9128e242022-11-23 20:52:29 +0000289Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800290 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700291 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000292 int32_t pid = clientInfo.pid;
293 int32_t uid = clientInfo.uid;
294 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000295 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000296 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700297 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298
Girish434b4d82023-07-11 23:24:54 +0000299 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100300 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800301 pid_t callingPid = IPCThreadState::self()->getCallingPid();
302 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100303 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
304 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800305 pid = callingPid;
306 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800307 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700308 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000309 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700310 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700311
312 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700313 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700314 const auto resType = std::tuple(res.type, res.subType, res.id);
315
316 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
317 ALOGW("Ignoring request to remove negative value of non-drm resource");
318 continue;
319 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700320 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700321 if (res.value <= 0) {
322 // We can't init a new entry with negative value, although it's allowed
323 // to merge in negative values after the initial add.
324 ALOGW("Ignoring request to add new resource entry with value <= 0");
325 continue;
326 }
Girishab17b0f2023-11-20 06:00:44 +0000327 onFirstAdded(res, info.uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700328 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700329 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700330 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700331 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700332 // Add it to the list of added resources for observers.
333 auto it = resourceAdded.find(resType);
334 if (it == resourceAdded.end()) {
335 resourceAdded[resType] = res;
336 } else {
337 mergeResources(it->second, res);
338 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700339 }
Girishf1d166c2023-07-20 22:35:29 +0000340 if (info.deathNotifier == nullptr && client != nullptr) {
Girishab17b0f2023-11-20 06:00:44 +0000341 info.deathNotifier = DeathNotifier::Create(
342 client, ref<ResourceManagerService>(), clientInfo);
Wonsik Kim3e378962017-01-05 17:00:02 +0900343 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700344 if (mObserverService != nullptr && !resourceAdded.empty()) {
345 mObserverService->onResourceAdded(uid, pid, resourceAdded);
346 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900347 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000348
Chong Zhang181e6952019-10-09 13:23:39 -0700349 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700350}
351
Girish9128e242022-11-23 20:52:29 +0000352Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700353 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000354 int32_t pid = clientInfo.pid;
355 int32_t uid = clientInfo.uid;
356 int64_t clientId = clientInfo.id;
357 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000358 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700359 mServiceLog->add(log);
360
Girish434b4d82023-07-11 23:24:54 +0000361 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100362 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800363 pid_t callingPid = IPCThreadState::self()->getCallingPid();
364 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
365 pid, callingPid);
366 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700367 }
Girish434b4d82023-07-11 23:24:54 +0000368 PidResourceInfosMap::iterator found = mMap.find(pid);
369 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700370 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700371 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700372 }
Girish434b4d82023-07-11 23:24:54 +0000373 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700374
Girish434b4d82023-07-11 23:24:54 +0000375 ResourceInfos::iterator foundClient = infos.find(clientId);
376 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700377 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700378 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700379 }
380
Girish434b4d82023-07-11 23:24:54 +0000381 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700382 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700383 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700384 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700385 const auto resType = std::tuple(res.type, res.subType, res.id);
386
387 if (res.value < 0) {
388 ALOGW("Ignoring request to remove negative value of resource");
389 continue;
390 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700391 // ignore if we don't have it
392 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700393 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700394 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700395 if (resource.value > res.value) {
396 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700397 } else {
Girishab17b0f2023-11-20 06:00:44 +0000398 onLastRemoved(res, info.uid);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700399 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800400 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700401 }
402
403 // Add it to the list of removed resources for observers.
404 auto it = resourceRemoved.find(resType);
405 if (it == resourceRemoved.end()) {
406 resourceRemoved[resType] = actualRemoved;
407 } else {
408 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700409 }
410 }
411 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700412 if (mObserverService != nullptr && !resourceRemoved.empty()) {
413 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
414 }
Chong Zhang181e6952019-10-09 13:23:39 -0700415 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700416}
417
Girish9128e242022-11-23 20:52:29 +0000418Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
419 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700420 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900421}
422
Girish9128e242022-11-23 20:52:29 +0000423Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
424 int32_t pid = clientInfo.pid;
425 int32_t uid = clientInfo.uid;
426 int64_t clientId = clientInfo.id;
427 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
428 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700429 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700430
Girish434b4d82023-07-11 23:24:54 +0000431 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100432 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800433 pid_t callingPid = IPCThreadState::self()->getCallingPid();
434 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
435 pid, callingPid);
436 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800437 }
Girish434b4d82023-07-11 23:24:54 +0000438 PidResourceInfosMap::iterator found = mMap.find(pid);
439 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700440 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700441 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700442 }
Girish434b4d82023-07-11 23:24:54 +0000443 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700444
Girish434b4d82023-07-11 23:24:54 +0000445 ResourceInfos::iterator foundClient = infos.find(clientId);
446 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700447 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700448 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700449 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700450
Girish434b4d82023-07-11 23:24:54 +0000451 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700452 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
Girishab17b0f2023-11-20 06:00:44 +0000453 onLastRemoved(it->second, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700454 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700455
Girish1f002cf2023-02-17 00:36:29 +0000456 // Since this client has been removed, update the metrics collector.
457 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000458
Chong Zhanga9d45c72020-09-09 12:41:17 -0700459 if (mObserverService != nullptr && !info.resources.empty()) {
460 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
461 }
462
Girish434b4d82023-07-11 23:24:54 +0000463 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700464 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700465}
466
Girish56fda312023-10-12 21:32:35 +0000467void ResourceManagerService::getClientForResource_l(
468 const ResourceRequestInfo& resourceRequestInfo,
469 std::vector<ClientInfo>& clientsInfo) {
Girishaff0ef22023-10-13 00:22:39 +0000470 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000471 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700472 if (res == NULL) {
473 return;
474 }
Girishaff0ef22023-10-13 00:22:39 +0000475
476 // Before looking into other processes, check if we have clients marked for
477 // pending removal in the same process.
Girishab17b0f2023-11-20 06:00:44 +0000478 ClientInfo clientInfo;
479 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, clientInfo)) {
480 clientsInfo.emplace_back(clientInfo);
Girishaff0ef22023-10-13 00:22:39 +0000481 return;
482 }
483
484 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000485 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
486 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700487 }
488}
489
Girish9128e242022-11-23 20:52:29 +0000490Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100491 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000492 int32_t callingPid = clientInfo.pid;
493 std::string clientName = clientInfo.name;
494 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000495 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700496 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700497 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700498
Girish56fda312023-10-12 21:32:35 +0000499 std::vector<ClientInfo> targetClients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700500 {
Girish434b4d82023-07-11 23:24:54 +0000501 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100502 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800503 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
504 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
505 callingPid, actualCallingPid);
506 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800507 }
Chong Zhang181e6952019-10-09 13:23:39 -0700508 const MediaResourceParcel *secureCodec = NULL;
509 const MediaResourceParcel *nonSecureCodec = NULL;
510 const MediaResourceParcel *graphicMemory = NULL;
511 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700512 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100513 switch (resources[i].type) {
514 case MediaResource::Type::kSecureCodec:
515 secureCodec = &resources[i];
516 break;
517 case MediaResource::Type::kNonSecureCodec:
518 nonSecureCodec = &resources[i];
519 break;
520 case MediaResource::Type::kGraphicMemory:
521 graphicMemory = &resources[i];
522 break;
523 case MediaResource::Type::kDrmSession:
524 drmSession = &resources[i];
525 break;
526 default:
527 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700528 }
529 }
530
531 // first pass to handle secure/non-secure codec conflict
532 if (secureCodec != NULL) {
Girish56fda312023-10-12 21:32:35 +0000533 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
534 .subType = secureCodec->subType};
535 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
Ronghua Wu05d89f12015-07-07 16:47:42 -0700536 if (!mSupportsMultipleSecureCodecs) {
Girish56fda312023-10-12 21:32:35 +0000537 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700538 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700539 }
540 }
541 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000542 mediaResource.type = MediaResource::Type::kNonSecureCodec;
543 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700544 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700545 }
546 }
547 }
548 if (nonSecureCodec != NULL) {
549 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000550 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
551 .subType = nonSecureCodec->subType};
552 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
553 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700554 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700555 }
556 }
557 }
Girish56fda312023-10-12 21:32:35 +0000558
Robert Shihc3af31b2019-09-20 21:45:01 -0700559 if (drmSession != NULL) {
Girish56fda312023-10-12 21:32:35 +0000560 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
561 getClientForResource_l(resourceRequestInfo, targetClients);
562 if (targetClients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700563 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700564 }
565 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700566
Girish56fda312023-10-12 21:32:35 +0000567 if (targetClients.size() == 0 && graphicMemory != nullptr) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700568 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish56fda312023-10-12 21:32:35 +0000569 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
570 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700571 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700572
Girish56fda312023-10-12 21:32:35 +0000573 if (targetClients.size() == 0) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700574 // if we are here, run the third pass to free one codec with the same type.
Girish56fda312023-10-12 21:32:35 +0000575 if (secureCodec != nullptr) {
576 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
577 getClientForResource_l(resourceRequestInfo, targetClients);
578 }
579 if (nonSecureCodec != nullptr) {
580 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
581 getClientForResource_l(resourceRequestInfo, targetClients);
582 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700583 }
584
Girish56fda312023-10-12 21:32:35 +0000585 if (targetClients.size() == 0) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700586 // if we are here, run the fourth pass to free one codec with the different type.
Girish56fda312023-10-12 21:32:35 +0000587 if (secureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700588 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000589 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
590 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700591 }
Girish56fda312023-10-12 21:32:35 +0000592 if (nonSecureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700593 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000594 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
595 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700596 }
597 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700598 }
599
Girish56fda312023-10-12 21:32:35 +0000600 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000601
602 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000603 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000604
Wonsik Kim271429d2020-10-01 10:12:56 -0700605 return Status::ok();
606}
607
Girish9128e242022-11-23 20:52:29 +0000608void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000609 const std::vector<ClientInfo>& targetClients,
610 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000611 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000612 int requesterPriority = -1;
613 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000614 std::vector<int> priorities;
615 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000616
Girish56fda312023-10-12 21:32:35 +0000617 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000618 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000619 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000620 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000621 }
Girish56fda312023-10-12 21:32:35 +0000622 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000623}
624
Girishab17b0f2023-11-20 06:00:44 +0000625std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient(
626 int pid, const int64_t& clientId) const {
627 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
628 if (found == mMap.end()) {
629 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
630 return nullptr;
631 }
632
633 const ResourceInfos& infos = found->second;
634 ResourceInfos::const_iterator foundClient = infos.find(clientId);
635 if (foundClient == infos.end()) {
636 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
637 return nullptr;
638 }
639
640 return foundClient->second.client;
641}
642
643bool ResourceManagerService::removeClient(int pid, const int64_t& clientId) {
644 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
645 if (found == mMap.end()) {
646 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
647 return false;
648 }
649
650 ResourceInfos& infos = found->second;
651 ResourceInfos::iterator foundClient = infos.find(clientId);
652 if (foundClient == infos.end()) {
653 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
654 return false;
655 }
656
657 infos.erase(foundClient);
658 return true;
659}
660
Brian Lindahl64ee9452022-01-14 13:31:16 +0100661bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000662 const std::vector<ClientInfo>& targetClients) {
663 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700664 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700665 }
666
Girishab17b0f2023-11-20 06:00:44 +0000667 int64_t failedClientId = -1;
668 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000669 for (const ClientInfo& targetClient : targetClients) {
Girishab17b0f2023-11-20 06:00:44 +0000670 std::shared_ptr<IResourceManagerClient> client = getClient(
671 targetClient.mPid, targetClient.mClientId);
672 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000673 // skip already released clients.
674 continue;
675 }
Girishab17b0f2023-11-20 06:00:44 +0000676 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700677 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700678 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000679 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700680 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000681 failedClientId = targetClient.mClientId;
682 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700683 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700684 }
685 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700686
Girishab17b0f2023-11-20 06:00:44 +0000687 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700688 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700689 }
690
Ronghua Wu67e7f542015-03-13 10:47:08 -0700691 {
Girish434b4d82023-07-11 23:24:54 +0000692 std::scoped_lock lock{mLock};
Girishab17b0f2023-11-20 06:00:44 +0000693 bool found = removeClient(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200694 if (found) {
695 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
696 } else {
697 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700698 }
699 }
700
Wonsik Kim271429d2020-10-01 10:12:56 -0700701 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700702}
703
Brian Lindahl64ee9452022-01-14 13:31:16 +0100704Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800705 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
706 originalPid, newPid);
707 mServiceLog->add(log);
708
709 // allow if this is called from the same process or the process has
710 // permission.
711 if ((AIBinder_getCallingPid() != getpid()) &&
712 (checkCallingPermission(String16(
713 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
714 ALOGE(
715 "Permission Denial: can't access overridePid method from pid=%d, "
716 "self pid=%d\n",
717 AIBinder_getCallingPid(), getpid());
718 return Status::fromServiceSpecificError(PERMISSION_DENIED);
719 }
720
721 {
Girish434b4d82023-07-11 23:24:54 +0000722 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800723 mOverridePidMap.erase(originalPid);
724 if (newPid != -1) {
725 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000726 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800727 }
728 }
729
730 return Status::ok();
731}
732
Chong Zhang97d367b2020-09-16 12:53:14 -0700733Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100734 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700735 int oomScore) {
736 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
737 pid, procState, oomScore);
738 mServiceLog->add(log);
739
740 // Only allow the override if the caller already can access process state and oom scores.
741 int callingPid = AIBinder_getCallingPid();
742 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
743 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
744 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
745 return Status::fromServiceSpecificError(PERMISSION_DENIED);
746 }
747
748 if (client == nullptr) {
749 return Status::fromServiceSpecificError(BAD_VALUE);
750 }
751
Girish434b4d82023-07-11 23:24:54 +0000752 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700753 removeProcessInfoOverride_l(pid);
754
755 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
756 // Override value is rejected by ProcessInfo.
757 return Status::fromServiceSpecificError(BAD_VALUE);
758 }
759
Girish1f002cf2023-02-17 00:36:29 +0000760 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
761 .uid = 0,
762 .id = 0,
763 .name = "<unknown client>"};
Girishab17b0f2023-11-20 06:00:44 +0000764 auto deathNotifier = DeathNotifier::Create(
765 client, ref<ResourceManagerService>(), clientInfo, true);
Chong Zhang97d367b2020-09-16 12:53:14 -0700766
Girishf1d166c2023-07-20 22:35:29 +0000767 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700768
769 return Status::ok();
770}
771
Chong Zhang97d367b2020-09-16 12:53:14 -0700772void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000773 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700774
775 removeProcessInfoOverride_l(pid);
776}
777
778void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
779 auto it = mProcessInfoOverrideMap.find(pid);
780 if (it == mProcessInfoOverrideMap.end()) {
781 return;
782 }
783
784 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700785 mProcessInfoOverrideMap.erase(pid);
786}
787
Girish9128e242022-11-23 20:52:29 +0000788Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
789 int32_t pid = clientInfo.pid;
790 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700791 String8 log = String8::format(
792 "markClientForPendingRemoval(pid %d, clientId %lld)",
793 pid, (long long) clientId);
794 mServiceLog->add(log);
795
Girish434b4d82023-07-11 23:24:54 +0000796 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100797 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800798 pid_t callingPid = IPCThreadState::self()->getCallingPid();
799 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
800 pid, callingPid);
801 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700802 }
Girish434b4d82023-07-11 23:24:54 +0000803 PidResourceInfosMap::iterator found = mMap.find(pid);
804 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700805 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
806 pid, (long long)clientId);
807 return Status::ok();
808 }
Girish434b4d82023-07-11 23:24:54 +0000809 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700810
Girish434b4d82023-07-11 23:24:54 +0000811 ResourceInfos::iterator foundClient = infos.find(clientId);
812 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700813 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
814 return Status::ok();
815 }
816
Girish434b4d82023-07-11 23:24:54 +0000817 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700818 info.pendingRemoval = true;
819 return Status::ok();
820}
821
Wonsik Kim271429d2020-10-01 10:12:56 -0700822Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
823 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
824 mServiceLog->add(log);
825
Girish56fda312023-10-12 21:32:35 +0000826 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700827 {
Girish434b4d82023-07-11 23:24:54 +0000828 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100829 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800830 pid_t callingPid = IPCThreadState::self()->getCallingPid();
831 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
832 pid, callingPid);
833 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700834 }
835
836 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
837 MediaResource::Type::kNonSecureCodec,
838 MediaResource::Type::kGraphicMemory,
839 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100840 switch (type) {
841 // Codec resources are segregated by audio, video and image domains.
842 case MediaResource::Type::kSecureCodec:
843 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000844 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
845 MediaResource::SubType::kSwAudioCodec,
846 MediaResource::SubType::kHwVideoCodec,
847 MediaResource::SubType::kSwVideoCodec,
848 MediaResource::SubType::kHwImageCodec,
849 MediaResource::SubType::kSwImageCodec}) {
Girishab17b0f2023-11-20 06:00:44 +0000850 ClientInfo clientInfo;
851 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
852 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100853 continue;
854 }
855 }
856 break;
857 // Non-codec resources are shared by audio, video and image codecs (no subtype).
858 default:
Girishab17b0f2023-11-20 06:00:44 +0000859 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100860 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000861 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
862 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100863 }
864 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700865 }
866 }
867 }
868
Girish56fda312023-10-12 21:32:35 +0000869 if (!targetClients.empty()) {
870 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700871 }
872 return Status::ok();
873}
874
Henry Fang32762922020-01-28 18:40:39 -0800875bool ResourceManagerService::getPriority_l(int pid, int* priority) {
876 int newPid = pid;
877
878 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
879 newPid = mOverridePidMap[pid];
880 ALOGD("getPriority_l: use override pid %d instead original pid %d",
881 newPid, pid);
882 }
883
884 return mProcessInfo->getPriority(newPid, priority);
885}
886
Girish56fda312023-10-12 21:32:35 +0000887bool ResourceManagerService::getAllClients_l(
888 const ResourceRequestInfo& resourceRequestInfo,
889 std::vector<ClientInfo>& clientsInfo) {
890 MediaResource::Type type = resourceRequestInfo.mResource->type;
891 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000892
Girish434b4d82023-07-11 23:24:54 +0000893 for (auto& [pid, infos] : mMap) {
894 for (const auto& [id, info] : infos) {
895 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000896 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700897 // some higher/equal priority process owns the resource,
898 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000899 ALOGE("%s: can't reclaim resource %s from pid %d",
900 __func__, asString(type), pid);
901 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700902 return false;
903 }
Girishab17b0f2023-11-20 06:00:44 +0000904 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700905 }
906 }
907 }
Girish56fda312023-10-12 21:32:35 +0000908 if (clientsInfo.size() == 0) {
909 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700910 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700911 return true;
912}
913
Girishaff0ef22023-10-13 00:22:39 +0000914// Process priority (oom score) based reclaim:
915// - Find a process with lowest priority (than that of calling process).
916// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000917bool ResourceManagerService::getLowestPriorityBiggestClient_l(
918 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000919 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000920 int callingPid = resourceRequestInfo.mCallingPid;
921 MediaResource::Type type = resourceRequestInfo.mResource->type;
922 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700923 int lowestPriorityPid;
924 int lowestPriority;
925 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700926
Henry Fang32762922020-01-28 18:40:39 -0800927 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000928 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700929 return false;
930 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100931 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700932 return false;
933 }
934 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000935 ALOGE("%s: lowest priority %d vs caller priority %d",
936 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700937 return false;
938 }
939
Girishab17b0f2023-11-20 06:00:44 +0000940 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700941 return false;
942 }
Girish9128e242022-11-23 20:52:29 +0000943
Girishaff0ef22023-10-13 00:22:39 +0000944 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
945 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700946 return true;
947}
948
Brian Lindahl64ee9452022-01-14 13:31:16 +0100949bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
950 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700951 int pid = -1;
952 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000953 for (auto& [tempPid, infos] : mMap) {
954 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700955 // no client on this process.
956 continue;
957 }
Girish434b4d82023-07-11 23:24:54 +0000958 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700959 // doesn't have the requested resource type
960 continue;
961 }
Girish434b4d82023-07-11 23:24:54 +0000962 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -0800963 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700964 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
965 // TODO: remove this pid from mMap?
966 continue;
967 }
968 if (pid == -1 || tempPriority > priority) {
969 // initial the value
970 pid = tempPid;
971 priority = tempPriority;
972 }
973 }
974 if (pid != -1) {
975 *lowestPriorityPid = pid;
976 *lowestPriority = priority;
977 }
978 return (pid != -1);
979}
980
981bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
982 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800983 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700984 return false;
985 }
986
987 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800988 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700989 return false;
990 }
991
992 return (callingPidPriority < priority);
993}
994
Brian Lindahl64ee9452022-01-14 13:31:16 +0100995bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +0000996 MediaResource::SubType subType, ClientInfo& clientInfo) {
997 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100998}
999
1000bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +00001001 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +00001002 PidResourceInfosMap::iterator found = mMap.find(pid);
1003 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001004 ALOGE_IF(!pendingRemovalOnly,
1005 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001006 return false;
1007 }
1008
Girishab17b0f2023-11-20 06:00:44 +00001009 uid_t uid = -1;
1010 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001011 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +00001012 const ResourceInfos& infos = found->second;
1013 for (const auto& [id, info] : infos) {
1014 const ResourceList& resources = info.resources;
1015 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001016 continue;
1017 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001018 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001019 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001020 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001021 if (resource.value > largestValue) {
1022 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001023 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001024 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001025 }
1026 }
1027 }
1028 }
1029
Girishab17b0f2023-11-20 06:00:44 +00001030 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001031 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001032 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1033 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001034 return false;
1035 }
1036
Girishab17b0f2023-11-20 06:00:44 +00001037 clientInfo.mPid = pid;
1038 clientInfo.mUid = uid;
1039 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001040 return true;
1041}
1042
Girish1f002cf2023-02-17 00:36:29 +00001043Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1044 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1045 return Status::ok();
1046}
1047
1048Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1049 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1050 return Status::ok();
1051}
1052
1053Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1054 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1055 return Status::ok();
1056}
1057
Girishde8eb592023-04-13 18:49:17 +00001058Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1059 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1060 return Status::ok();
1061}
1062
Girish1f002cf2023-02-17 00:36:29 +00001063long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1064 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1065}
1066
1067long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1068 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1069}
1070
Ronghua Wu231c3d12015-03-11 15:10:32 -07001071} // namespace android