blob: 19532375b5c677c93691dedf7a8e62a1bca0dc07 [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>
Ronghua Wu231c3d12015-03-11 15:10:32 -070032
Steven Moreland0a0ff0b2021-04-02 00:06:01 +000033#include "IMediaResourceMonitor.h"
Girish1f002cf2023-02-17 00:36:29 +000034#include "ResourceManagerMetrics.h"
Chong Zhanga9d45c72020-09-09 12:41:17 -070035#include "ResourceObserverService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070036#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070037
Ronghua Wu231c3d12015-03-11 15:10:32 -070038namespace android {
39
Girish434b4d82023-07-11 23:24:54 +000040static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +090041 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -070042 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +090043 if (binder != NULL) {
44 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
45 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +010046 switch (resources[i].subType) {
Girisha5a2d672023-09-20 18:40:20 +000047 case MediaResource::SubType::kHwAudioCodec:
48 case MediaResource::SubType::kSwAudioCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010049 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
50 break;
Girisha5a2d672023-09-20 18:40:20 +000051 case MediaResource::SubType::kHwVideoCodec:
52 case MediaResource::SubType::kSwVideoCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010053 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
54 break;
Girisha5a2d672023-09-20 18:40:20 +000055 case MediaResource::SubType::kHwImageCodec:
56 case MediaResource::SubType::kSwImageCodec:
Brian Lindahl64ee9452022-01-14 13:31:16 +010057 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_IMAGE_CODEC);
58 break;
59 case MediaResource::SubType::kUnspecifiedSubType:
60 break;
Dongwon Kang69c23dd2016-03-22 15:22:45 -070061 }
Dongwon Kangfe508d32015-12-15 14:22:05 +090062 }
63 }
64}
65
Brian Lindahl64ee9452022-01-14 13:31:16 +010066binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -070067 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -070068
dcashman014e91e2015-09-11 09:33:01 -070069 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
70 result.format("Permission Denial: "
71 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -080072 AIBinder_getCallingPid(),
73 AIBinder_getCallingUid());
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +000074 write(fd, result.c_str(), result.size());
dcashman014e91e2015-09-11 09:33:01 -070075 return PERMISSION_DENIED;
76 }
77
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070078 PidResourceInfosMap mapCopy;
79 bool supportsMultipleSecureCodecs;
80 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -080081 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070082 String8 serviceLog;
83 {
Girish434b4d82023-07-11 23:24:54 +000084 std::scoped_lock lock{mLock};
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070085 mapCopy = mMap; // Shadow copy, real copy will happen on write.
86 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
87 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
88 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -080089 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070090 }
91
92 const size_t SIZE = 256;
93 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -070094 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
95 result.append(buffer);
96 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070097 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070098 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -070099 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
100 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700101 result.append(buffer);
102
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700103 result.append(" Processes:\n");
Girish434b4d82023-07-11 23:24:54 +0000104 for (const auto& [pid, infos] : mapCopy) {
Girish9128e242022-11-23 20:52:29 +0000105 snprintf(buffer, SIZE, " Pid: %d\n", pid);
106 result.append(buffer);
107 int priority = 0;
108 if (getPriority_l(pid, &priority)) {
109 snprintf(buffer, SIZE, " Priority: %d\n", priority);
110 } else {
111 snprintf(buffer, SIZE, " Priority: <unknown>\n");
112 }
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700113 result.append(buffer);
114
Girish434b4d82023-07-11 23:24:54 +0000115 for (const auto& [infoKey, info] : infos) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700116 result.append(" Client:\n");
Girish434b4d82023-07-11 23:24:54 +0000117 snprintf(buffer, SIZE, " Id: %lld\n", (long long)info.clientId);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700118 result.append(buffer);
119
Girish434b4d82023-07-11 23:24:54 +0000120 std::string clientName = info.name;
Chong Zhang181e6952019-10-09 13:23:39 -0700121 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700122 result.append(buffer);
123
Girish434b4d82023-07-11 23:24:54 +0000124 const ResourceList& resources = info.resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700125 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700126 for (auto it = resources.begin(); it != resources.end(); it++) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000127 snprintf(buffer, SIZE, " %s\n", toString(it->second).c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700128 result.append(buffer);
129 }
130 }
131 }
Henry Fang32762922020-01-28 18:40:39 -0800132 result.append(" Process Pid override:\n");
133 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
134 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
135 it->first, it->second);
136 result.append(buffer);
137 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700138 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700139 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700140
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000141 write(fd, result.c_str(), result.size());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700142 return OK;
143}
144
Brian Lindahl64ee9452022-01-14 13:31:16 +0100145struct SystemCallbackImpl : public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800146 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700147
Chong Zhangdd726802019-08-21 17:24:13 -0700148 virtual void noteStartVideo(int uid) override {
149 BatteryNotifier::getInstance().noteStartVideo(uid);
150 }
151 virtual void noteStopVideo(int uid) override {
152 BatteryNotifier::getInstance().noteStopVideo(uid);
153 }
154 virtual void noteResetVideo() override {
155 BatteryNotifier::getInstance().noteResetVideo();
156 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800157 virtual bool requestCpusetBoost(bool enable) override {
158 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700159 }
160
161protected:
162 virtual ~SystemCallbackImpl() {}
163
164private:
165 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800166 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700167};
168
169ResourceManagerService::ResourceManagerService()
170 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
171
Brian Lindahl64ee9452022-01-14 13:31:16 +0100172ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700173 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700175 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700176 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700177 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700178 mSupportsSecureWithNonSecureCodec(true),
Girishab17b0f2023-11-20 06:00:44 +0000179 mCpuBoostCount(0) {
Chong Zhangdd726802019-08-21 17:24:13 -0700180 mSystemCB->noteResetVideo();
Girish1f002cf2023-02-17 00:36:29 +0000181 // Create ResourceManagerMetrics that handles all the metrics.
182 mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
Chong Zhangee33d642019-08-08 14:26:43 -0700183}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700184
Chong Zhangfdd512a2019-11-22 11:03:14 -0800185//static
186void ResourceManagerService::instantiate() {
Girishab17b0f2023-11-20 06:00:44 +0000187 std::shared_ptr<ResourceManagerService> service = Create();
Chong Zhangfdd512a2019-11-22 11:03:14 -0800188 binder_status_t status =
Charles Chen5b097ef2023-03-15 01:21:22 +0000189 AServiceManager_addServiceWithFlags(
Charles Chene55549f2023-03-15 01:21:22 +0000190 service->asBinder().get(), getServiceName(),
191 AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800192 if (status != STATUS_OK) {
193 return;
194 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700195
196 std::shared_ptr<ResourceObserverService> observerService =
197 ResourceObserverService::instantiate();
198
199 if (observerService != nullptr) {
200 service->setObserverService(observerService);
201 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800202 // TODO: mediaserver main() is already starting the thread pool,
203 // move this to mediaserver main() when other services in mediaserver
204 // are converted to ndk-platform aidl.
205 //ABinderProcess_startThreadPool();
206}
207
Girishab17b0f2023-11-20 06:00:44 +0000208std::shared_ptr<ResourceManagerService> ResourceManagerService::Create() {
209 return Create(new ProcessInfo(), new SystemCallbackImpl());
210}
211
212std::shared_ptr<ResourceManagerService> ResourceManagerService::Create(
213 const sp<ProcessInfoInterface>& processInfo,
214 const sp<SystemCallbackInterface>& systemResource) {
215 return ::ndk::SharedRefBase::make<ResourceManagerService>(processInfo, systemResource);
216}
217
Ronghua Wu231c3d12015-03-11 15:10:32 -0700218ResourceManagerService::~ResourceManagerService() {}
219
Chong Zhanga9d45c72020-09-09 12:41:17 -0700220void ResourceManagerService::setObserverService(
221 const std::shared_ptr<ResourceObserverService>& observerService) {
222 mObserverService = observerService;
223}
224
Chong Zhang181e6952019-10-09 13:23:39 -0700225Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000226 String8 log = String8::format("config(%s)", getString(policies).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700227 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700228
Girish434b4d82023-07-11 23:24:54 +0000229 std::scoped_lock lock{mLock};
Ronghua Wu231c3d12015-03-11 15:10:32 -0700230 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700231 const std::string &type = policies[i].type;
232 const std::string &value = policies[i].value;
233 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700234 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700235 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700236 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700237 }
238 }
Chong Zhang181e6952019-10-09 13:23:39 -0700239 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700240}
241
Girishab17b0f2023-11-20 06:00:44 +0000242void ResourceManagerService::onFirstAdded(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700243 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700244 if (resource.type == MediaResource::Type::kCpuBoost
245 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700246 // Request it on every new instance of kCpuBoost, as the media.codec
247 // could have died, if we only do it the first time subsequent instances
248 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800249 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700250 ALOGW("couldn't request cpuset boost");
251 }
252 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700253 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000254 && (resource.subType == MediaResource::SubType::kHwVideoCodec
255 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000256 mSystemCB->noteStartVideo(uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700257 }
258}
259
Girishab17b0f2023-11-20 06:00:44 +0000260void ResourceManagerService::onLastRemoved(const MediaResourceParcel& resource, uid_t uid) {
Chong Zhang181e6952019-10-09 13:23:39 -0700261 if (resource.type == MediaResource::Type::kCpuBoost
262 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700263 && mCpuBoostCount > 0) {
264 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800265 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700266 }
Chong Zhang181e6952019-10-09 13:23:39 -0700267 } else if (resource.type == MediaResource::Type::kBattery
Girisha5a2d672023-09-20 18:40:20 +0000268 && (resource.subType == MediaResource::SubType::kHwVideoCodec
269 || resource.subType == MediaResource::SubType::kSwVideoCodec)) {
Girishab17b0f2023-11-20 06:00:44 +0000270 mSystemCB->noteStopVideo(uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700271 }
272}
273
Girish9128e242022-11-23 20:52:29 +0000274Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800275 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700276 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000277 int32_t pid = clientInfo.pid;
278 int32_t uid = clientInfo.uid;
279 int64_t clientId = clientInfo.id;
Girish9128e242022-11-23 20:52:29 +0000280 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000281 pid, uid, (long long) clientId, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700282 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700283
Girish434b4d82023-07-11 23:24:54 +0000284 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100285 if (!mProcessInfo->isPidUidTrusted(pid, uid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800286 pid_t callingPid = IPCThreadState::self()->getCallingPid();
287 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100288 ALOGW("%s called with untrusted pid %d or uid %d, using calling pid %d, uid %d",
289 __FUNCTION__, pid, uid, callingPid, callingUid);
Chong Zhangc7303e82020-12-02 16:38:52 -0800290 pid = callingPid;
291 uid = callingUid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800292 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700293 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Girish27365ed2023-10-11 20:20:55 +0000294 ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700295 ResourceList resourceAdded;
Chong Zhang79d2b282018-04-17 14:14:31 -0700296
297 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700298 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700299 const auto resType = std::tuple(res.type, res.subType, res.id);
300
301 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
302 ALOGW("Ignoring request to remove negative value of non-drm resource");
303 continue;
304 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700305 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700306 if (res.value <= 0) {
307 // We can't init a new entry with negative value, although it's allowed
308 // to merge in negative values after the initial add.
309 ALOGW("Ignoring request to add new resource entry with value <= 0");
310 continue;
311 }
Girishab17b0f2023-11-20 06:00:44 +0000312 onFirstAdded(res, info.uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700313 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700314 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700315 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700316 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700317 // Add it to the list of added resources for observers.
318 auto it = resourceAdded.find(resType);
319 if (it == resourceAdded.end()) {
320 resourceAdded[resType] = res;
321 } else {
322 mergeResources(it->second, res);
323 }
Chong Zhang79d2b282018-04-17 14:14:31 -0700324 }
Girishf1d166c2023-07-20 22:35:29 +0000325 if (info.deathNotifier == nullptr && client != nullptr) {
Girishab17b0f2023-11-20 06:00:44 +0000326 info.deathNotifier = DeathNotifier::Create(
327 client, ref<ResourceManagerService>(), clientInfo);
Wonsik Kim3e378962017-01-05 17:00:02 +0900328 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700329 if (mObserverService != nullptr && !resourceAdded.empty()) {
330 mObserverService->onResourceAdded(uid, pid, resourceAdded);
331 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900332 notifyResourceGranted(pid, resources);
Girish9128e242022-11-23 20:52:29 +0000333
Chong Zhang181e6952019-10-09 13:23:39 -0700334 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700335}
336
Girish9128e242022-11-23 20:52:29 +0000337Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo,
Chong Zhang181e6952019-10-09 13:23:39 -0700338 const std::vector<MediaResourceParcel>& resources) {
Girish9128e242022-11-23 20:52:29 +0000339 int32_t pid = clientInfo.pid;
340 int32_t uid = clientInfo.uid;
341 int64_t clientId = clientInfo.id;
342 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000343 pid, uid, (long long) clientId, getString(resources).c_str());
Chong Zhangfb092d32019-08-12 09:45:44 -0700344 mServiceLog->add(log);
345
Girish434b4d82023-07-11 23:24:54 +0000346 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100347 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800348 pid_t callingPid = IPCThreadState::self()->getCallingPid();
349 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
350 pid, callingPid);
351 pid = callingPid;
Chong Zhangfb092d32019-08-12 09:45:44 -0700352 }
Girish434b4d82023-07-11 23:24:54 +0000353 PidResourceInfosMap::iterator found = mMap.find(pid);
354 if (found == mMap.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700355 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700356 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700357 }
Girish434b4d82023-07-11 23:24:54 +0000358 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700359
Girish434b4d82023-07-11 23:24:54 +0000360 ResourceInfos::iterator foundClient = infos.find(clientId);
361 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700362 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700363 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700364 }
365
Girish434b4d82023-07-11 23:24:54 +0000366 ResourceInfo& info = foundClient->second;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700367 ResourceList resourceRemoved;
Chong Zhangfb092d32019-08-12 09:45:44 -0700368 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700369 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700370 const auto resType = std::tuple(res.type, res.subType, res.id);
371
372 if (res.value < 0) {
373 ALOGW("Ignoring request to remove negative value of resource");
374 continue;
375 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700376 // ignore if we don't have it
377 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700378 MediaResourceParcel &resource = info.resources[resType];
Chong Zhanga9d45c72020-09-09 12:41:17 -0700379 MediaResourceParcel actualRemoved = res;
Chong Zhang181e6952019-10-09 13:23:39 -0700380 if (resource.value > res.value) {
381 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700382 } else {
Girishab17b0f2023-11-20 06:00:44 +0000383 onLastRemoved(res, info.uid);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700384 actualRemoved.value = resource.value;
Chong Zhang102b3e32020-11-02 12:21:50 -0800385 info.resources.erase(resType);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700386 }
387
388 // Add it to the list of removed resources for observers.
389 auto it = resourceRemoved.find(resType);
390 if (it == resourceRemoved.end()) {
391 resourceRemoved[resType] = actualRemoved;
392 } else {
393 mergeResources(it->second, actualRemoved);
Chong Zhangfb092d32019-08-12 09:45:44 -0700394 }
395 }
396 }
Chong Zhanga9d45c72020-09-09 12:41:17 -0700397 if (mObserverService != nullptr && !resourceRemoved.empty()) {
398 mObserverService->onResourceRemoved(info.uid, pid, resourceRemoved);
399 }
Chong Zhang181e6952019-10-09 13:23:39 -0700400 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700401}
402
Girish9128e242022-11-23 20:52:29 +0000403Status ResourceManagerService::removeClient(const ClientInfoParcel& clientInfo) {
404 removeResource(clientInfo, true /*checkValid*/);
Chong Zhang181e6952019-10-09 13:23:39 -0700405 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900406}
407
Girish9128e242022-11-23 20:52:29 +0000408Status ResourceManagerService::removeResource(const ClientInfoParcel& clientInfo, bool checkValid) {
409 int32_t pid = clientInfo.pid;
410 int32_t uid = clientInfo.uid;
411 int64_t clientId = clientInfo.id;
412 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
413 pid, uid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700414 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700415
Girish434b4d82023-07-11 23:24:54 +0000416 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100417 if (checkValid && !mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800418 pid_t callingPid = IPCThreadState::self()->getCallingPid();
419 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
420 pid, callingPid);
421 pid = callingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800422 }
Girish434b4d82023-07-11 23:24:54 +0000423 PidResourceInfosMap::iterator found = mMap.find(pid);
424 if (found == mMap.end()) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700425 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700426 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700427 }
Girish434b4d82023-07-11 23:24:54 +0000428 ResourceInfos& infos = found->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700429
Girish434b4d82023-07-11 23:24:54 +0000430 ResourceInfos::iterator foundClient = infos.find(clientId);
431 if (foundClient == infos.end()) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700432 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700433 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700434 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700435
Girish434b4d82023-07-11 23:24:54 +0000436 const ResourceInfo& info = foundClient->second;
Chong Zhangfb092d32019-08-12 09:45:44 -0700437 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
Girishab17b0f2023-11-20 06:00:44 +0000438 onLastRemoved(it->second, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700439 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700440
Girish1f002cf2023-02-17 00:36:29 +0000441 // Since this client has been removed, update the metrics collector.
442 mResourceManagerMetrics->notifyClientReleased(clientInfo);
Girish9128e242022-11-23 20:52:29 +0000443
Chong Zhanga9d45c72020-09-09 12:41:17 -0700444 if (mObserverService != nullptr && !info.resources.empty()) {
445 mObserverService->onResourceRemoved(info.uid, pid, info.resources);
446 }
447
Girish434b4d82023-07-11 23:24:54 +0000448 infos.erase(foundClient);
Chong Zhang181e6952019-10-09 13:23:39 -0700449 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700450}
451
Girish56fda312023-10-12 21:32:35 +0000452void ResourceManagerService::getClientForResource_l(
453 const ResourceRequestInfo& resourceRequestInfo,
454 std::vector<ClientInfo>& clientsInfo) {
Girishaff0ef22023-10-13 00:22:39 +0000455 int callingPid = resourceRequestInfo.mCallingPid;
Girish56fda312023-10-12 21:32:35 +0000456 const MediaResourceParcel* res = resourceRequestInfo.mResource;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700457 if (res == NULL) {
458 return;
459 }
Girishaff0ef22023-10-13 00:22:39 +0000460
461 // Before looking into other processes, check if we have clients marked for
462 // pending removal in the same process.
Girishab17b0f2023-11-20 06:00:44 +0000463 ClientInfo clientInfo;
464 if (getBiggestClientPendingRemoval_l(callingPid, res->type, res->subType, clientInfo)) {
465 clientsInfo.emplace_back(clientInfo);
Girishaff0ef22023-10-13 00:22:39 +0000466 return;
467 }
468
469 // Now find client(s) from a lowest priority process that has needed resources.
Girish56fda312023-10-12 21:32:35 +0000470 if (getLowestPriorityBiggestClient_l(resourceRequestInfo, clientInfo)) {
471 clientsInfo.push_back(clientInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700472 }
473}
474
Girish9128e242022-11-23 20:52:29 +0000475Status ResourceManagerService::reclaimResource(const ClientInfoParcel& clientInfo,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100476 const std::vector<MediaResourceParcel>& resources, bool* _aidl_return) {
Girish9128e242022-11-23 20:52:29 +0000477 int32_t callingPid = clientInfo.pid;
478 std::string clientName = clientInfo.name;
479 String8 log = String8::format("reclaimResource(callingPid %d, uid %d resources %s)",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000480 callingPid, clientInfo.uid, getString(resources).c_str());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700481 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700482 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700483
Girish56fda312023-10-12 21:32:35 +0000484 std::vector<ClientInfo> targetClients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700485 {
Girish434b4d82023-07-11 23:24:54 +0000486 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100487 if (!mProcessInfo->isPidTrusted(callingPid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800488 pid_t actualCallingPid = IPCThreadState::self()->getCallingPid();
489 ALOGW("%s called with untrusted pid %d, using actual calling pid %d", __FUNCTION__,
490 callingPid, actualCallingPid);
491 callingPid = actualCallingPid;
Ronghua Wud11c43a2016-01-27 16:26:12 -0800492 }
Chong Zhang181e6952019-10-09 13:23:39 -0700493 const MediaResourceParcel *secureCodec = NULL;
494 const MediaResourceParcel *nonSecureCodec = NULL;
495 const MediaResourceParcel *graphicMemory = NULL;
496 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700497 for (size_t i = 0; i < resources.size(); ++i) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100498 switch (resources[i].type) {
499 case MediaResource::Type::kSecureCodec:
500 secureCodec = &resources[i];
501 break;
502 case MediaResource::Type::kNonSecureCodec:
503 nonSecureCodec = &resources[i];
504 break;
505 case MediaResource::Type::kGraphicMemory:
506 graphicMemory = &resources[i];
507 break;
508 case MediaResource::Type::kDrmSession:
509 drmSession = &resources[i];
510 break;
511 default:
512 break;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700513 }
514 }
515
516 // first pass to handle secure/non-secure codec conflict
517 if (secureCodec != NULL) {
Girish56fda312023-10-12 21:32:35 +0000518 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
519 .subType = secureCodec->subType};
520 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
Ronghua Wu05d89f12015-07-07 16:47:42 -0700521 if (!mSupportsMultipleSecureCodecs) {
Girish56fda312023-10-12 21:32:35 +0000522 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700523 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700524 }
525 }
526 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000527 mediaResource.type = MediaResource::Type::kNonSecureCodec;
528 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700529 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700530 }
531 }
532 }
533 if (nonSecureCodec != NULL) {
534 if (!mSupportsSecureWithNonSecureCodec) {
Girish56fda312023-10-12 21:32:35 +0000535 MediaResourceParcel mediaResource{.type = MediaResource::Type::kSecureCodec,
536 .subType = nonSecureCodec->subType};
537 ResourceRequestInfo resourceRequestInfo{callingPid, &mediaResource};
538 if (!getAllClients_l(resourceRequestInfo, targetClients)) {
Chong Zhang181e6952019-10-09 13:23:39 -0700539 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700540 }
541 }
542 }
Girish56fda312023-10-12 21:32:35 +0000543
Robert Shihc3af31b2019-09-20 21:45:01 -0700544 if (drmSession != NULL) {
Girish56fda312023-10-12 21:32:35 +0000545 ResourceRequestInfo resourceRequestInfo{callingPid, drmSession};
546 getClientForResource_l(resourceRequestInfo, targetClients);
547 if (targetClients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700548 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700549 }
550 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700551
Girish56fda312023-10-12 21:32:35 +0000552 if (targetClients.size() == 0 && graphicMemory != nullptr) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700553 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Girish56fda312023-10-12 21:32:35 +0000554 ResourceRequestInfo resourceRequestInfo{callingPid, graphicMemory};
555 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700556 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700557
Girish56fda312023-10-12 21:32:35 +0000558 if (targetClients.size() == 0) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700559 // if we are here, run the third pass to free one codec with the same type.
Girish56fda312023-10-12 21:32:35 +0000560 if (secureCodec != nullptr) {
561 ResourceRequestInfo resourceRequestInfo{callingPid, secureCodec};
562 getClientForResource_l(resourceRequestInfo, targetClients);
563 }
564 if (nonSecureCodec != nullptr) {
565 ResourceRequestInfo resourceRequestInfo{callingPid, nonSecureCodec};
566 getClientForResource_l(resourceRequestInfo, targetClients);
567 }
Ronghua Wu05d89f12015-07-07 16:47:42 -0700568 }
569
Girish56fda312023-10-12 21:32:35 +0000570 if (targetClients.size() == 0) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700571 // if we are here, run the fourth pass to free one codec with the different type.
Girish56fda312023-10-12 21:32:35 +0000572 if (secureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700573 MediaResource temp(MediaResource::Type::kNonSecureCodec, secureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000574 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
575 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700576 }
Girish56fda312023-10-12 21:32:35 +0000577 if (nonSecureCodec != nullptr) {
Wonsik Kimb3639012022-03-16 13:57:41 -0700578 MediaResource temp(MediaResource::Type::kSecureCodec, nonSecureCodec->subType, 1);
Girish56fda312023-10-12 21:32:35 +0000579 ResourceRequestInfo resourceRequestInfo{callingPid, &temp};
580 getClientForResource_l(resourceRequestInfo, targetClients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700581 }
582 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700583 }
584
Girish56fda312023-10-12 21:32:35 +0000585 *_aidl_return = reclaimUnconditionallyFrom(targetClients);
Girish9128e242022-11-23 20:52:29 +0000586
587 // Log Reclaim Pushed Atom to statsd
Girish56fda312023-10-12 21:32:35 +0000588 pushReclaimAtom(clientInfo, targetClients, *_aidl_return);
Girish9128e242022-11-23 20:52:29 +0000589
Wonsik Kim271429d2020-10-01 10:12:56 -0700590 return Status::ok();
591}
592
Girish9128e242022-11-23 20:52:29 +0000593void ResourceManagerService::pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000594 const std::vector<ClientInfo>& targetClients,
595 bool reclaimed) {
Girish9128e242022-11-23 20:52:29 +0000596 int32_t callingPid = clientInfo.pid;
Girish9128e242022-11-23 20:52:29 +0000597 int requesterPriority = -1;
598 getPriority_l(callingPid, &requesterPriority);
Girish1f002cf2023-02-17 00:36:29 +0000599 std::vector<int> priorities;
600 priorities.push_back(requesterPriority);
Girish9128e242022-11-23 20:52:29 +0000601
Girish56fda312023-10-12 21:32:35 +0000602 for (const ClientInfo& targetClient : targetClients) {
Girish9128e242022-11-23 20:52:29 +0000603 int targetPriority = -1;
Girish56fda312023-10-12 21:32:35 +0000604 getPriority_l(targetClient.mPid, &targetPriority);
Girish1f002cf2023-02-17 00:36:29 +0000605 priorities.push_back(targetPriority);
Girish9128e242022-11-23 20:52:29 +0000606 }
Girish56fda312023-10-12 21:32:35 +0000607 mResourceManagerMetrics->pushReclaimAtom(clientInfo, priorities, targetClients, reclaimed);
Girish9128e242022-11-23 20:52:29 +0000608}
609
Girishab17b0f2023-11-20 06:00:44 +0000610std::shared_ptr<IResourceManagerClient> ResourceManagerService::getClient(
611 int pid, const int64_t& clientId) const {
612 std::map<int, ResourceInfos>::const_iterator found = mMap.find(pid);
613 if (found == mMap.end()) {
614 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
615 return nullptr;
616 }
617
618 const ResourceInfos& infos = found->second;
619 ResourceInfos::const_iterator foundClient = infos.find(clientId);
620 if (foundClient == infos.end()) {
621 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
622 return nullptr;
623 }
624
625 return foundClient->second.client;
626}
627
628bool ResourceManagerService::removeClient(int pid, const int64_t& clientId) {
629 std::map<int, ResourceInfos>::iterator found = mMap.find(pid);
630 if (found == mMap.end()) {
631 ALOGV("%s: didn't find pid %d for clientId %lld", __func__, pid, (long long) clientId);
632 return false;
633 }
634
635 ResourceInfos& infos = found->second;
636 ResourceInfos::iterator foundClient = infos.find(clientId);
637 if (foundClient == infos.end()) {
638 ALOGV("%s: didn't find clientId %lld", __func__, (long long) clientId);
639 return false;
640 }
641
642 infos.erase(foundClient);
643 return true;
644}
645
Brian Lindahl64ee9452022-01-14 13:31:16 +0100646bool ResourceManagerService::reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000647 const std::vector<ClientInfo>& targetClients) {
648 if (targetClients.size() == 0) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700649 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700650 }
651
Girishab17b0f2023-11-20 06:00:44 +0000652 int64_t failedClientId = -1;
653 int32_t failedClientPid = -1;
Girish56fda312023-10-12 21:32:35 +0000654 for (const ClientInfo& targetClient : targetClients) {
Girishab17b0f2023-11-20 06:00:44 +0000655 std::shared_ptr<IResourceManagerClient> client = getClient(
656 targetClient.mPid, targetClient.mClientId);
657 if (client == nullptr) {
Girish56fda312023-10-12 21:32:35 +0000658 // skip already released clients.
659 continue;
660 }
Girishab17b0f2023-11-20 06:00:44 +0000661 String8 log = String8::format("reclaimResource from client %p", client.get());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700662 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700663 bool success;
Girishab17b0f2023-11-20 06:00:44 +0000664 Status status = client->reclaimResource(&success);
Chong Zhang181e6952019-10-09 13:23:39 -0700665 if (!status.isOk() || !success) {
Girishab17b0f2023-11-20 06:00:44 +0000666 failedClientId = targetClient.mClientId;
667 failedClientPid = targetClient.mPid;
Ronghua Wu67e7f542015-03-13 10:47:08 -0700668 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700669 }
670 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700671
Girishab17b0f2023-11-20 06:00:44 +0000672 if (failedClientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700673 return true;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700674 }
675
Ronghua Wu67e7f542015-03-13 10:47:08 -0700676 {
Girish434b4d82023-07-11 23:24:54 +0000677 std::scoped_lock lock{mLock};
Girishab17b0f2023-11-20 06:00:44 +0000678 bool found = removeClient(failedClientPid, failedClientId);
Brian Lindahl9f626cf2022-04-25 13:29:59 +0200679 if (found) {
680 ALOGW("Failed to reclaim resources from client with pid %d", failedClientPid);
681 } else {
682 ALOGW("Failed to reclaim resources from unlocateable client");
Ronghua Wu67e7f542015-03-13 10:47:08 -0700683 }
684 }
685
Wonsik Kim271429d2020-10-01 10:12:56 -0700686 return false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700687}
688
Brian Lindahl64ee9452022-01-14 13:31:16 +0100689Status ResourceManagerService::overridePid(int originalPid, int newPid) {
Henry Fang32762922020-01-28 18:40:39 -0800690 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
691 originalPid, newPid);
692 mServiceLog->add(log);
693
694 // allow if this is called from the same process or the process has
695 // permission.
696 if ((AIBinder_getCallingPid() != getpid()) &&
697 (checkCallingPermission(String16(
698 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
699 ALOGE(
700 "Permission Denial: can't access overridePid method from pid=%d, "
701 "self pid=%d\n",
702 AIBinder_getCallingPid(), getpid());
703 return Status::fromServiceSpecificError(PERMISSION_DENIED);
704 }
705
706 {
Girish434b4d82023-07-11 23:24:54 +0000707 std::scoped_lock lock{mLock};
Henry Fang32762922020-01-28 18:40:39 -0800708 mOverridePidMap.erase(originalPid);
709 if (newPid != -1) {
710 mOverridePidMap.emplace(originalPid, newPid);
Girish1f002cf2023-02-17 00:36:29 +0000711 mResourceManagerMetrics->addPid(newPid);
Henry Fang32762922020-01-28 18:40:39 -0800712 }
713 }
714
715 return Status::ok();
716}
717
Chong Zhang97d367b2020-09-16 12:53:14 -0700718Status ResourceManagerService::overrideProcessInfo(
Brian Lindahl64ee9452022-01-14 13:31:16 +0100719 const std::shared_ptr<IResourceManagerClient>& client, int pid, int procState,
Chong Zhang97d367b2020-09-16 12:53:14 -0700720 int oomScore) {
721 String8 log = String8::format("overrideProcessInfo(pid %d, procState %d, oomScore %d)",
722 pid, procState, oomScore);
723 mServiceLog->add(log);
724
725 // Only allow the override if the caller already can access process state and oom scores.
726 int callingPid = AIBinder_getCallingPid();
727 if (callingPid != getpid() && (callingPid != pid || !checkCallingPermission(String16(
728 "android.permission.GET_PROCESS_STATE_AND_OOM_SCORE")))) {
729 ALOGE("Permission Denial: overrideProcessInfo method from pid=%d", callingPid);
730 return Status::fromServiceSpecificError(PERMISSION_DENIED);
731 }
732
733 if (client == nullptr) {
734 return Status::fromServiceSpecificError(BAD_VALUE);
735 }
736
Girish434b4d82023-07-11 23:24:54 +0000737 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700738 removeProcessInfoOverride_l(pid);
739
740 if (!mProcessInfo->overrideProcessInfo(pid, procState, oomScore)) {
741 // Override value is rejected by ProcessInfo.
742 return Status::fromServiceSpecificError(BAD_VALUE);
743 }
744
Girish1f002cf2023-02-17 00:36:29 +0000745 ClientInfoParcel clientInfo{.pid = static_cast<int32_t>(pid),
746 .uid = 0,
747 .id = 0,
748 .name = "<unknown client>"};
Girishab17b0f2023-11-20 06:00:44 +0000749 auto deathNotifier = DeathNotifier::Create(
750 client, ref<ResourceManagerService>(), clientInfo, true);
Chong Zhang97d367b2020-09-16 12:53:14 -0700751
Girishf1d166c2023-07-20 22:35:29 +0000752 mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});
Chong Zhang97d367b2020-09-16 12:53:14 -0700753
754 return Status::ok();
755}
756
Chong Zhang97d367b2020-09-16 12:53:14 -0700757void ResourceManagerService::removeProcessInfoOverride(int pid) {
Girish434b4d82023-07-11 23:24:54 +0000758 std::scoped_lock lock{mLock};
Chong Zhang97d367b2020-09-16 12:53:14 -0700759
760 removeProcessInfoOverride_l(pid);
761}
762
763void ResourceManagerService::removeProcessInfoOverride_l(int pid) {
764 auto it = mProcessInfoOverrideMap.find(pid);
765 if (it == mProcessInfoOverrideMap.end()) {
766 return;
767 }
768
769 mProcessInfo->removeProcessInfoOverride(pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700770 mProcessInfoOverrideMap.erase(pid);
771}
772
Girish9128e242022-11-23 20:52:29 +0000773Status ResourceManagerService::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
774 int32_t pid = clientInfo.pid;
775 int64_t clientId = clientInfo.id;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700776 String8 log = String8::format(
777 "markClientForPendingRemoval(pid %d, clientId %lld)",
778 pid, (long long) clientId);
779 mServiceLog->add(log);
780
Girish434b4d82023-07-11 23:24:54 +0000781 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100782 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800783 pid_t callingPid = IPCThreadState::self()->getCallingPid();
784 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
785 pid, callingPid);
786 pid = callingPid;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700787 }
Girish434b4d82023-07-11 23:24:54 +0000788 PidResourceInfosMap::iterator found = mMap.find(pid);
789 if (found == mMap.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700790 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
791 pid, (long long)clientId);
792 return Status::ok();
793 }
Girish434b4d82023-07-11 23:24:54 +0000794 ResourceInfos& infos = found->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700795
Girish434b4d82023-07-11 23:24:54 +0000796 ResourceInfos::iterator foundClient = infos.find(clientId);
797 if (foundClient == infos.end()) {
Wonsik Kimd20e9362020-04-28 10:42:57 -0700798 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
799 return Status::ok();
800 }
801
Girish434b4d82023-07-11 23:24:54 +0000802 ResourceInfo& info = foundClient->second;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700803 info.pendingRemoval = true;
804 return Status::ok();
805}
806
Wonsik Kim271429d2020-10-01 10:12:56 -0700807Status ResourceManagerService::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
808 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
809 mServiceLog->add(log);
810
Girish56fda312023-10-12 21:32:35 +0000811 std::vector<ClientInfo> targetClients;
Wonsik Kim271429d2020-10-01 10:12:56 -0700812 {
Girish434b4d82023-07-11 23:24:54 +0000813 std::scoped_lock lock{mLock};
Brian Lindahlcf3bafb2022-01-27 14:21:38 +0100814 if (!mProcessInfo->isPidTrusted(pid)) {
Chong Zhangc7303e82020-12-02 16:38:52 -0800815 pid_t callingPid = IPCThreadState::self()->getCallingPid();
816 ALOGW("%s called with untrusted pid %d, using calling pid %d", __FUNCTION__,
817 pid, callingPid);
818 pid = callingPid;
Wonsik Kim271429d2020-10-01 10:12:56 -0700819 }
820
821 for (MediaResource::Type type : {MediaResource::Type::kSecureCodec,
822 MediaResource::Type::kNonSecureCodec,
823 MediaResource::Type::kGraphicMemory,
824 MediaResource::Type::kDrmSession}) {
Brian Lindahl64ee9452022-01-14 13:31:16 +0100825 switch (type) {
826 // Codec resources are segregated by audio, video and image domains.
827 case MediaResource::Type::kSecureCodec:
828 case MediaResource::Type::kNonSecureCodec:
Girisha5a2d672023-09-20 18:40:20 +0000829 for (MediaResource::SubType subType : {MediaResource::SubType::kHwAudioCodec,
830 MediaResource::SubType::kSwAudioCodec,
831 MediaResource::SubType::kHwVideoCodec,
832 MediaResource::SubType::kSwVideoCodec,
833 MediaResource::SubType::kHwImageCodec,
834 MediaResource::SubType::kSwImageCodec}) {
Girishab17b0f2023-11-20 06:00:44 +0000835 ClientInfo clientInfo;
836 if (getBiggestClientPendingRemoval_l(pid, type, subType, clientInfo)) {
837 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100838 continue;
839 }
840 }
841 break;
842 // Non-codec resources are shared by audio, video and image codecs (no subtype).
843 default:
Girishab17b0f2023-11-20 06:00:44 +0000844 ClientInfo clientInfo;
Brian Lindahl64ee9452022-01-14 13:31:16 +0100845 if (getBiggestClientPendingRemoval_l(pid, type,
Girishab17b0f2023-11-20 06:00:44 +0000846 MediaResource::SubType::kUnspecifiedSubType, clientInfo)) {
847 targetClients.emplace_back(clientInfo);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100848 }
849 break;
Wonsik Kim271429d2020-10-01 10:12:56 -0700850 }
851 }
852 }
853
Girish56fda312023-10-12 21:32:35 +0000854 if (!targetClients.empty()) {
855 reclaimUnconditionallyFrom(targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700856 }
857 return Status::ok();
858}
859
Henry Fang32762922020-01-28 18:40:39 -0800860bool ResourceManagerService::getPriority_l(int pid, int* priority) {
861 int newPid = pid;
862
863 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
864 newPid = mOverridePidMap[pid];
865 ALOGD("getPriority_l: use override pid %d instead original pid %d",
866 newPid, pid);
867 }
868
869 return mProcessInfo->getPriority(newPid, priority);
870}
871
Girish56fda312023-10-12 21:32:35 +0000872bool ResourceManagerService::getAllClients_l(
873 const ResourceRequestInfo& resourceRequestInfo,
874 std::vector<ClientInfo>& clientsInfo) {
875 MediaResource::Type type = resourceRequestInfo.mResource->type;
876 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Girish9128e242022-11-23 20:52:29 +0000877
Girish434b4d82023-07-11 23:24:54 +0000878 for (auto& [pid, infos] : mMap) {
879 for (const auto& [id, info] : infos) {
880 if (hasResourceType(type, subType, info.resources)) {
Girish56fda312023-10-12 21:32:35 +0000881 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, pid)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700882 // some higher/equal priority process owns the resource,
883 // this request can't be fulfilled.
Girish56fda312023-10-12 21:32:35 +0000884 ALOGE("%s: can't reclaim resource %s from pid %d",
885 __func__, asString(type), pid);
886 clientsInfo.clear();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700887 return false;
888 }
Girishab17b0f2023-11-20 06:00:44 +0000889 clientsInfo.emplace_back(pid, info.uid, info.clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700890 }
891 }
892 }
Girish56fda312023-10-12 21:32:35 +0000893 if (clientsInfo.size() == 0) {
894 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700895 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700896 return true;
897}
898
Girishaff0ef22023-10-13 00:22:39 +0000899// Process priority (oom score) based reclaim:
900// - Find a process with lowest priority (than that of calling process).
901// - Find the bigegst client (with required resources) from that process.
Girish56fda312023-10-12 21:32:35 +0000902bool ResourceManagerService::getLowestPriorityBiggestClient_l(
903 const ResourceRequestInfo& resourceRequestInfo,
Girishab17b0f2023-11-20 06:00:44 +0000904 ClientInfo& clientInfo) {
Girish56fda312023-10-12 21:32:35 +0000905 int callingPid = resourceRequestInfo.mCallingPid;
906 MediaResource::Type type = resourceRequestInfo.mResource->type;
907 MediaResource::SubType subType = resourceRequestInfo.mResource->subType;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700908 int lowestPriorityPid;
909 int lowestPriority;
910 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700911
Henry Fang32762922020-01-28 18:40:39 -0800912 if (!getPriority_l(callingPid, &callingPriority)) {
Girishaff0ef22023-10-13 00:22:39 +0000913 ALOGE("%s: can't get process priority for pid %d", __func__, callingPid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700914 return false;
915 }
Brian Lindahl64ee9452022-01-14 13:31:16 +0100916 if (!getLowestPriorityPid_l(type, subType, &lowestPriorityPid, &lowestPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700917 return false;
918 }
919 if (lowestPriority <= callingPriority) {
Girishaff0ef22023-10-13 00:22:39 +0000920 ALOGE("%s: lowest priority %d vs caller priority %d",
921 __func__, lowestPriority, callingPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700922 return false;
923 }
924
Girishab17b0f2023-11-20 06:00:44 +0000925 if (!getBiggestClient_l(lowestPriorityPid, type, subType, clientInfo)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700926 return false;
927 }
Girish9128e242022-11-23 20:52:29 +0000928
Girishaff0ef22023-10-13 00:22:39 +0000929 ALOGI("%s: CallingProcess(%d:%d) will reclaim from the lowestPriorityProcess(%d:%d)",
930 __func__, callingPid, callingPriority, lowestPriorityPid, lowestPriority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700931 return true;
932}
933
Brian Lindahl64ee9452022-01-14 13:31:16 +0100934bool ResourceManagerService::getLowestPriorityPid_l(MediaResource::Type type,
935 MediaResource::SubType subType, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700936 int pid = -1;
937 int priority = -1;
Girish434b4d82023-07-11 23:24:54 +0000938 for (auto& [tempPid, infos] : mMap) {
939 if (infos.size() == 0) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700940 // no client on this process.
941 continue;
942 }
Girish434b4d82023-07-11 23:24:54 +0000943 if (!hasResourceType(type, subType, infos)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700944 // doesn't have the requested resource type
945 continue;
946 }
Girish434b4d82023-07-11 23:24:54 +0000947 int tempPriority = -1;
Henry Fang32762922020-01-28 18:40:39 -0800948 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700949 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
950 // TODO: remove this pid from mMap?
951 continue;
952 }
953 if (pid == -1 || tempPriority > priority) {
954 // initial the value
955 pid = tempPid;
956 priority = tempPriority;
957 }
958 }
959 if (pid != -1) {
960 *lowestPriorityPid = pid;
961 *lowestPriority = priority;
962 }
963 return (pid != -1);
964}
965
966bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
967 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800968 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700969 return false;
970 }
971
972 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800973 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700974 return false;
975 }
976
977 return (callingPidPriority < priority);
978}
979
Brian Lindahl64ee9452022-01-14 13:31:16 +0100980bool ResourceManagerService::getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +0000981 MediaResource::SubType subType, ClientInfo& clientInfo) {
982 return getBiggestClient_l(pid, type, subType, clientInfo, true /* pendingRemovalOnly */);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100983}
984
985bool ResourceManagerService::getBiggestClient_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +0000986 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
Girish434b4d82023-07-11 23:24:54 +0000987 PidResourceInfosMap::iterator found = mMap.find(pid);
988 if (found == mMap.end()) {
Wonsik Kim271429d2020-10-01 10:12:56 -0700989 ALOGE_IF(!pendingRemovalOnly,
990 "getBiggestClient_l: can't find resource info for pid %d", pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700991 return false;
992 }
993
Girishab17b0f2023-11-20 06:00:44 +0000994 uid_t uid = -1;
995 int64_t clientId = -1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700996 uint64_t largestValue = 0;
Girish434b4d82023-07-11 23:24:54 +0000997 const ResourceInfos& infos = found->second;
998 for (const auto& [id, info] : infos) {
999 const ResourceList& resources = info.resources;
1000 if (pendingRemovalOnly && !info.pendingRemoval) {
Wonsik Kimd20e9362020-04-28 10:42:57 -07001001 continue;
1002 }
Chong Zhangfb092d32019-08-12 09:45:44 -07001003 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -07001004 const MediaResourceParcel &resource = it->second;
Brian Lindahl64ee9452022-01-14 13:31:16 +01001005 if (hasResourceType(type, subType, resource)) {
Chong Zhang181e6952019-10-09 13:23:39 -07001006 if (resource.value > largestValue) {
1007 largestValue = resource.value;
Girishab17b0f2023-11-20 06:00:44 +00001008 clientId = info.clientId;
Girish434b4d82023-07-11 23:24:54 +00001009 uid = info.uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001010 }
1011 }
1012 }
1013 }
1014
Girishab17b0f2023-11-20 06:00:44 +00001015 if (clientId == -1) {
Wonsik Kim271429d2020-10-01 10:12:56 -07001016 ALOGE_IF(!pendingRemovalOnly,
Brian Lindahl64ee9452022-01-14 13:31:16 +01001017 "getBiggestClient_l: can't find resource type %s and subtype %s for pid %d",
1018 asString(type), asString(subType), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -07001019 return false;
1020 }
1021
Girishab17b0f2023-11-20 06:00:44 +00001022 clientInfo.mPid = pid;
1023 clientInfo.mUid = uid;
1024 clientInfo.mClientId = clientId;
Ronghua Wu231c3d12015-03-11 15:10:32 -07001025 return true;
1026}
1027
Girish1f002cf2023-02-17 00:36:29 +00001028Status ResourceManagerService::notifyClientCreated(const ClientInfoParcel& clientInfo) {
1029 mResourceManagerMetrics->notifyClientCreated(clientInfo);
1030 return Status::ok();
1031}
1032
1033Status ResourceManagerService::notifyClientStarted(const ClientConfigParcel& clientConfig) {
1034 mResourceManagerMetrics->notifyClientStarted(clientConfig);
1035 return Status::ok();
1036}
1037
1038Status ResourceManagerService::notifyClientStopped(const ClientConfigParcel& clientConfig) {
1039 mResourceManagerMetrics->notifyClientStopped(clientConfig);
1040 return Status::ok();
1041}
1042
Girishde8eb592023-04-13 18:49:17 +00001043Status ResourceManagerService::notifyClientConfigChanged(const ClientConfigParcel& clientConfig) {
1044 mResourceManagerMetrics->notifyClientConfigChanged(clientConfig);
1045 return Status::ok();
1046}
1047
Girish1f002cf2023-02-17 00:36:29 +00001048long ResourceManagerService::getPeakConcurrentPixelCount(int pid) const {
1049 return mResourceManagerMetrics->getPeakConcurrentPixelCount(pid);
1050}
1051
1052long ResourceManagerService::getCurrentConcurrentPixelCount(int pid) const {
1053 return mResourceManagerMetrics->getCurrentConcurrentPixelCount(pid);
1054}
1055
Ronghua Wu231c3d12015-03-11 15:10:32 -07001056} // namespace android