blob: 045aa3dcddb950e448e2b156c852d45e3fb6899d [file] [log] [blame]
Girish1484e5d2023-11-20 06:00:44 +00001/*
2**
3** Copyright 2023, 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 "ResourceManagerServiceNew"
20#include <utils/Log.h>
21
22#include "ResourceManagerServiceNew.h"
Girish6a6044d2023-11-22 21:23:14 +000023#include "ResourceTracker.h"
24#include "ServiceLog.h"
Girish1484e5d2023-11-20 06:00:44 +000025
26namespace android {
27
28ResourceManagerServiceNew::ResourceManagerServiceNew(
29 const sp<ProcessInfoInterface>& processInfo,
30 const sp<SystemCallbackInterface>& systemResource) :
31 ResourceManagerService(processInfo, systemResource) {}
32
33ResourceManagerServiceNew::~ResourceManagerServiceNew() {}
34
Girish6a6044d2023-11-22 21:23:14 +000035void ResourceManagerServiceNew::init() {
36 // Create the Resource Tracker
37 mResourceTracker = std::make_shared<ResourceTracker>(ref<ResourceManagerServiceNew>(),
38 mProcessInfo);
39}
40
Girish1484e5d2023-11-20 06:00:44 +000041Status ResourceManagerServiceNew::config(const std::vector<MediaResourcePolicyParcel>& policies) {
42 return ResourceManagerService::config(policies);
43}
44
Girish6a6044d2023-11-22 21:23:14 +000045void ResourceManagerServiceNew::setObserverService(
46 const std::shared_ptr<ResourceObserverService>& observerService) {
47 ResourceManagerService::setObserverService(observerService);
48 mResourceTracker->setResourceObserverService(observerService);
49}
50
Girish1484e5d2023-11-20 06:00:44 +000051Status ResourceManagerServiceNew::addResource(
52 const ClientInfoParcel& clientInfo,
53 const std::shared_ptr<IResourceManagerClient>& client,
54 const std::vector<MediaResourceParcel>& resources) {
Girish6a6044d2023-11-22 21:23:14 +000055 int32_t pid = clientInfo.pid;
56 int32_t uid = clientInfo.uid;
57 int64_t clientId = clientInfo.id;
58 String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
59 pid, uid, (long long) clientId, getString(resources).c_str());
60 mServiceLog->add(log);
61
62 std::scoped_lock lock{mLock};
63 mResourceTracker->addResource(clientInfo, client, resources);
64 notifyResourceGranted(pid, resources);
65
66 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +000067}
68
69Status ResourceManagerServiceNew::removeResource(
70 const ClientInfoParcel& clientInfo,
71 const std::vector<MediaResourceParcel>& resources) {
Girish6a6044d2023-11-22 21:23:14 +000072 int32_t pid = clientInfo.pid;
73 int32_t uid = clientInfo.uid;
74 int64_t clientId = clientInfo.id;
75 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld, resources %s)",
76 pid, uid, (long long) clientId, getString(resources).c_str());
77 mServiceLog->add(log);
78
79 std::scoped_lock lock{mLock};
80 mResourceTracker->removeResource(clientInfo, resources);
81 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +000082}
83
84Status ResourceManagerServiceNew::removeClient(const ClientInfoParcel& clientInfo) {
Girish6a6044d2023-11-22 21:23:14 +000085 removeResource(clientInfo, true /*checkValid*/);
86 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +000087}
88
89Status ResourceManagerServiceNew::removeResource(const ClientInfoParcel& clientInfo,
90 bool checkValid) {
Girish6a6044d2023-11-22 21:23:14 +000091 int32_t pid = clientInfo.pid;
92 int32_t uid = clientInfo.uid;
93 int64_t clientId = clientInfo.id;
94 String8 log = String8::format("removeResource(pid %d, uid %d clientId %lld)",
95 pid, uid, (long long) clientId);
96 mServiceLog->add(log);
97
98 std::scoped_lock lock{mLock};
99 if (mResourceTracker->removeResource(clientInfo, checkValid)) {
100 notifyClientReleased(clientInfo);
101 }
102 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +0000103}
104
105Status ResourceManagerServiceNew::reclaimResource(
106 const ClientInfoParcel& clientInfo,
107 const std::vector<MediaResourceParcel>& resources,
108 bool* _aidl_return) {
109 return ResourceManagerService::reclaimResource(clientInfo, resources, _aidl_return);
110}
111
Girish6a6044d2023-11-22 21:23:14 +0000112bool ResourceManagerServiceNew::overridePid_l(int32_t originalPid, int32_t newPid) {
113 return mResourceTracker->overridePid(originalPid, newPid);
114}
115
Girish1484e5d2023-11-20 06:00:44 +0000116Status ResourceManagerServiceNew::overridePid(int originalPid, int newPid) {
117 return ResourceManagerService::overridePid(originalPid, newPid);
118}
119
Girish6a6044d2023-11-22 21:23:14 +0000120bool ResourceManagerServiceNew::overrideProcessInfo_l(
121 const std::shared_ptr<IResourceManagerClient>& client,
122 int pid,
123 int procState,
124 int oomScore) {
125 return mResourceTracker->overrideProcessInfo(client, pid, procState, oomScore);
126}
127
Girish1484e5d2023-11-20 06:00:44 +0000128Status ResourceManagerServiceNew::overrideProcessInfo(
129 const std::shared_ptr<IResourceManagerClient>& client,
130 int pid,
131 int procState,
132 int oomScore) {
133 return ResourceManagerService::overrideProcessInfo(client, pid, procState, oomScore);
134}
135
Girish6a6044d2023-11-22 21:23:14 +0000136void ResourceManagerServiceNew::removeProcessInfoOverride(int pid) {
137 std::scoped_lock lock{mLock};
138
139 mResourceTracker->removeProcessInfoOverride(pid);
140}
141
Girish1484e5d2023-11-20 06:00:44 +0000142Status ResourceManagerServiceNew::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) {
Girish6a6044d2023-11-22 21:23:14 +0000143 int32_t pid = clientInfo.pid;
144 int64_t clientId = clientInfo.id;
145 String8 log = String8::format(
146 "markClientForPendingRemoval(pid %d, clientId %lld)",
147 pid, (long long) clientId);
148 mServiceLog->add(log);
149
150 std::scoped_lock lock{mLock};
151 mResourceTracker->markClientForPendingRemoval(clientInfo);
152 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +0000153}
154
155Status ResourceManagerServiceNew::reclaimResourcesFromClientsPendingRemoval(int32_t pid) {
Girish6a6044d2023-11-22 21:23:14 +0000156 String8 log = String8::format("reclaimResourcesFromClientsPendingRemoval(pid %d)", pid);
157 mServiceLog->add(log);
158
159 std::vector<ClientInfo> targetClients;
160 {
161 std::scoped_lock lock{mLock};
162 mResourceTracker->getClientsMarkedPendingRemoval(pid, targetClients);
163 }
164
165 if (!targetClients.empty()) {
166 reclaimUnconditionallyFrom(targetClients);
167 }
168 return Status::ok();
Girish1484e5d2023-11-20 06:00:44 +0000169}
170
171Status ResourceManagerServiceNew::notifyClientCreated(const ClientInfoParcel& clientInfo) {
172 return ResourceManagerService::notifyClientCreated(clientInfo);
173}
174
175Status ResourceManagerServiceNew::notifyClientStarted(const ClientConfigParcel& clientConfig) {
176 return ResourceManagerService::notifyClientStarted(clientConfig);
177}
178
179Status ResourceManagerServiceNew::notifyClientStopped(const ClientConfigParcel& clientConfig) {
180 return ResourceManagerService::notifyClientStopped(clientConfig);
181}
182
183Status ResourceManagerServiceNew::notifyClientConfigChanged(
184 const ClientConfigParcel& clientConfig) {
185 return ResourceManagerService::notifyClientConfigChanged(clientConfig);
186}
187
Girish6a6044d2023-11-22 21:23:14 +0000188void ResourceManagerServiceNew::getResourceDump(std::string& resourceLog) const {
189 std::scoped_lock lock{mLock};
190 mResourceTracker->dump(resourceLog);
191}
192
Girish1484e5d2023-11-20 06:00:44 +0000193binder_status_t ResourceManagerServiceNew::dump(int fd, const char** args, uint32_t numArgs) {
194 return ResourceManagerService::dump(fd, args, numArgs);
195}
196
Girish6a6044d2023-11-22 21:23:14 +0000197bool ResourceManagerServiceNew::getPriority_l(int pid, int* priority) const {
198 return mResourceTracker->getPriority(pid, priority);
199}
200
201bool ResourceManagerServiceNew::getLowestPriorityPid_l(
202 MediaResource::Type type, MediaResource::SubType subType,
203 int* lowestPriorityPid, int* lowestPriority) {
204 return mResourceTracker->getLowestPriorityPid(type, subType,
205 *lowestPriorityPid,
206 *lowestPriority);
207}
208
209bool ResourceManagerServiceNew::getBiggestClient_l(int pid, MediaResource::Type type,
210 MediaResource::SubType subType, ClientInfo& clientInfo, bool pendingRemovalOnly) {
211 return mResourceTracker->getBiggestClient(pid, type, subType,
212 clientInfo, pendingRemovalOnly);
213}
214
215bool ResourceManagerServiceNew::getAllClients_l(
216 const ResourceRequestInfo& resourceRequestInfo,
217 std::vector<ClientInfo>& clientsInfo) {
218 MediaResource::Type type = resourceRequestInfo.mResource->type;
219 // Get list of all the clients that has requested resources.
220 std::vector<ClientInfo> clients;
221 mResourceTracker->getAllClients(resourceRequestInfo, clients);
222
223 // Check is there any high priority process holding up the resources already.
224 for (const ClientInfo& info : clients) {
225 if (!isCallingPriorityHigher_l(resourceRequestInfo.mCallingPid, info.mPid)) {
226 // some higher/equal priority process owns the resource,
227 // this request can't be fulfilled.
228 ALOGE("%s: can't reclaim resource %s from pid %d", __func__, asString(type), info.mPid);
229 return false;
230 }
231 clientsInfo.emplace_back(info);
232 }
233 if (clientsInfo.size() == 0) {
234 ALOGV("%s: didn't find any resource %s", __func__, asString(type));
235 }
236 return true;
237}
238
239std::shared_ptr<IResourceManagerClient> ResourceManagerServiceNew::getClient(
240 int pid, const int64_t& clientId) const {
241 return mResourceTracker->getClient(pid, clientId);
242}
243
244bool ResourceManagerServiceNew::removeClient(int pid, const int64_t& clientId) {
245 return mResourceTracker->removeClient(pid, clientId);
246}
247
248const std::map<int, ResourceInfos>& ResourceManagerServiceNew::getResourceMap() const {
249 return mResourceTracker->getResourceMap();
250}
251
Girish1484e5d2023-11-20 06:00:44 +0000252} // namespace android