Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 1 | /* |
| 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" |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 23 | #include "ResourceTracker.h" |
| 24 | #include "ServiceLog.h" |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | ResourceManagerServiceNew::ResourceManagerServiceNew( |
| 29 | const sp<ProcessInfoInterface>& processInfo, |
| 30 | const sp<SystemCallbackInterface>& systemResource) : |
| 31 | ResourceManagerService(processInfo, systemResource) {} |
| 32 | |
| 33 | ResourceManagerServiceNew::~ResourceManagerServiceNew() {} |
| 34 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 35 | void ResourceManagerServiceNew::init() { |
| 36 | // Create the Resource Tracker |
| 37 | mResourceTracker = std::make_shared<ResourceTracker>(ref<ResourceManagerServiceNew>(), |
| 38 | mProcessInfo); |
| 39 | } |
| 40 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 41 | Status ResourceManagerServiceNew::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
| 42 | return ResourceManagerService::config(policies); |
| 43 | } |
| 44 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 45 | void ResourceManagerServiceNew::setObserverService( |
| 46 | const std::shared_ptr<ResourceObserverService>& observerService) { |
| 47 | ResourceManagerService::setObserverService(observerService); |
| 48 | mResourceTracker->setResourceObserverService(observerService); |
| 49 | } |
| 50 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 51 | Status ResourceManagerServiceNew::addResource( |
| 52 | const ClientInfoParcel& clientInfo, |
| 53 | const std::shared_ptr<IResourceManagerClient>& client, |
| 54 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 55 | 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(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Status ResourceManagerServiceNew::removeResource( |
| 70 | const ClientInfoParcel& clientInfo, |
| 71 | const std::vector<MediaResourceParcel>& resources) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 72 | 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(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Status ResourceManagerServiceNew::removeClient(const ClientInfoParcel& clientInfo) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 85 | removeResource(clientInfo, true /*checkValid*/); |
| 86 | return Status::ok(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | Status ResourceManagerServiceNew::removeResource(const ClientInfoParcel& clientInfo, |
| 90 | bool checkValid) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 91 | 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(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Status 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 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 112 | bool ResourceManagerServiceNew::overridePid_l(int32_t originalPid, int32_t newPid) { |
| 113 | return mResourceTracker->overridePid(originalPid, newPid); |
| 114 | } |
| 115 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 116 | Status ResourceManagerServiceNew::overridePid(int originalPid, int newPid) { |
| 117 | return ResourceManagerService::overridePid(originalPid, newPid); |
| 118 | } |
| 119 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 120 | bool 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 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 128 | Status 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 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 136 | void ResourceManagerServiceNew::removeProcessInfoOverride(int pid) { |
| 137 | std::scoped_lock lock{mLock}; |
| 138 | |
| 139 | mResourceTracker->removeProcessInfoOverride(pid); |
| 140 | } |
| 141 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 142 | Status ResourceManagerServiceNew::markClientForPendingRemoval(const ClientInfoParcel& clientInfo) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 143 | 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(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | Status ResourceManagerServiceNew::reclaimResourcesFromClientsPendingRemoval(int32_t pid) { |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 156 | 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(); |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Status ResourceManagerServiceNew::notifyClientCreated(const ClientInfoParcel& clientInfo) { |
| 172 | return ResourceManagerService::notifyClientCreated(clientInfo); |
| 173 | } |
| 174 | |
| 175 | Status ResourceManagerServiceNew::notifyClientStarted(const ClientConfigParcel& clientConfig) { |
| 176 | return ResourceManagerService::notifyClientStarted(clientConfig); |
| 177 | } |
| 178 | |
| 179 | Status ResourceManagerServiceNew::notifyClientStopped(const ClientConfigParcel& clientConfig) { |
| 180 | return ResourceManagerService::notifyClientStopped(clientConfig); |
| 181 | } |
| 182 | |
| 183 | Status ResourceManagerServiceNew::notifyClientConfigChanged( |
| 184 | const ClientConfigParcel& clientConfig) { |
| 185 | return ResourceManagerService::notifyClientConfigChanged(clientConfig); |
| 186 | } |
| 187 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 188 | void ResourceManagerServiceNew::getResourceDump(std::string& resourceLog) const { |
| 189 | std::scoped_lock lock{mLock}; |
| 190 | mResourceTracker->dump(resourceLog); |
| 191 | } |
| 192 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 193 | binder_status_t ResourceManagerServiceNew::dump(int fd, const char** args, uint32_t numArgs) { |
| 194 | return ResourceManagerService::dump(fd, args, numArgs); |
| 195 | } |
| 196 | |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame^] | 197 | bool ResourceManagerServiceNew::getPriority_l(int pid, int* priority) const { |
| 198 | return mResourceTracker->getPriority(pid, priority); |
| 199 | } |
| 200 | |
| 201 | bool 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 | |
| 209 | bool 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 | |
| 215 | bool 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 | |
| 239 | std::shared_ptr<IResourceManagerClient> ResourceManagerServiceNew::getClient( |
| 240 | int pid, const int64_t& clientId) const { |
| 241 | return mResourceTracker->getClient(pid, clientId); |
| 242 | } |
| 243 | |
| 244 | bool ResourceManagerServiceNew::removeClient(int pid, const int64_t& clientId) { |
| 245 | return mResourceTracker->removeClient(pid, clientId); |
| 246 | } |
| 247 | |
| 248 | const std::map<int, ResourceInfos>& ResourceManagerServiceNew::getResourceMap() const { |
| 249 | return mResourceTracker->getResourceMap(); |
| 250 | } |
| 251 | |
Girish | 1484e5d | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 252 | } // namespace android |