blob: dcc0bb5a00522ea58d15991856bce5bacddd1cce [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
Chong Zhang181e6952019-10-09 13:23:39 -070018#ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
19#define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
Ronghua Wu231c3d12015-03-11 15:10:32 -070020
Wonsik Kimd20e9362020-04-28 10:42:57 -070021#include <map>
Girish9128e242022-11-23 20:52:29 +000022#include <set>
Chong Zhang97d367b2020-09-16 12:53:14 -070023#include <mutex>
Girish9128e242022-11-23 20:52:29 +000024#include <string>
Girish434b4d82023-07-11 23:24:54 +000025#include <vector>
Wonsik Kimd20e9362020-04-28 10:42:57 -070026
Chong Zhangfdd512a2019-11-22 11:03:14 -080027#include <aidl/android/media/BnResourceManagerService.h>
Chong Zhang181e6952019-10-09 13:23:39 -070028#include <media/MediaResource.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070029#include <utils/Errors.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070030#include <utils/String8.h>
31#include <utils/threads.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070032
Girishab17b0f2023-11-20 06:00:44 +000033#include "ResourceManagerServiceUtils.h"
34
Ronghua Wu231c3d12015-03-11 15:10:32 -070035namespace android {
36
Chong Zhanga9d45c72020-09-09 12:41:17 -070037class ResourceObserverService;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070038class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070039struct ProcessInfoInterface;
Girish1f002cf2023-02-17 00:36:29 +000040class ResourceManagerMetrics;
Ronghua Wu231c3d12015-03-11 15:10:32 -070041
Chong Zhangfdd512a2019-11-22 11:03:14 -080042using Status = ::ndk::ScopedAStatus;
43using ::aidl::android::media::IResourceManagerClient;
44using ::aidl::android::media::BnResourceManagerService;
45using ::aidl::android::media::MediaResourceParcel;
46using ::aidl::android::media::MediaResourcePolicyParcel;
Girish9128e242022-11-23 20:52:29 +000047using ::aidl::android::media::ClientInfoParcel;
Girish1f002cf2023-02-17 00:36:29 +000048using ::aidl::android::media::ClientConfigParcel;
Chong Zhang181e6952019-10-09 13:23:39 -070049
Chong Zhangfdd512a2019-11-22 11:03:14 -080050class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -070051public:
Chong Zhangdd726802019-08-21 17:24:13 -070052 struct SystemCallbackInterface : public RefBase {
53 virtual void noteStartVideo(int uid) = 0;
54 virtual void noteStopVideo(int uid) = 0;
55 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -080056 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -070057 };
58
Ronghua Wu231c3d12015-03-11 15:10:32 -070059 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -080060 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -070061
Girishab17b0f2023-11-20 06:00:44 +000062 // Static creation methods.
63 static std::shared_ptr<ResourceManagerService> Create();
64 static std::shared_ptr<ResourceManagerService> Create(
65 const sp<ProcessInfoInterface>& processInfo,
66 const sp<SystemCallbackInterface>& systemResource);
67
68 virtual binder_status_t dump(
Chong Zhangfdd512a2019-11-22 11:03:14 -080069 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070070
Ronghua Wu231c3d12015-03-11 15:10:32 -070071 ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +010072 explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -070073 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -080074 virtual ~ResourceManagerService();
Girishab17b0f2023-11-20 06:00:44 +000075
Girish6a6044d2023-11-22 21:23:14 +000076 virtual void setObserverService(
77 const std::shared_ptr<ResourceObserverService>& observerService);
Ronghua Wu231c3d12015-03-11 15:10:32 -070078
79 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070080 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070081
Girish9128e242022-11-23 20:52:29 +000082 Status addResource(const ClientInfoParcel& clientInfo,
83 const std::shared_ptr<IResourceManagerClient>& client,
84 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070085
Girish9128e242022-11-23 20:52:29 +000086 Status removeResource(const ClientInfoParcel& clientInfo,
87 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -070088
Girish9128e242022-11-23 20:52:29 +000089 Status removeClient(const ClientInfoParcel& clientInfo) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070090
Ronghua Wu05d89f12015-07-07 16:47:42 -070091 // Tries to reclaim resource from processes with lower priority than the calling process
92 // according to the requested resources.
93 // Returns true if any resource has been reclaimed, otherwise returns false.
Girish9128e242022-11-23 20:52:29 +000094 Status reclaimResource(const ClientInfoParcel& clientInfo,
95 const std::vector<MediaResourceParcel>& resources,
96 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070097
Girish9128e242022-11-23 20:52:29 +000098 Status overridePid(int32_t originalPid, int32_t newPid) override;
Henry Fang32762922020-01-28 18:40:39 -080099
Girish9128e242022-11-23 20:52:29 +0000100 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
101 int32_t pid, int32_t procState, int32_t oomScore) override;
Chong Zhang97d367b2020-09-16 12:53:14 -0700102
Girish9128e242022-11-23 20:52:29 +0000103 Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700104
Wonsik Kim271429d2020-10-01 10:12:56 -0700105 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
106
Girish1f002cf2023-02-17 00:36:29 +0000107 Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
108
109 Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
110
111 Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
112
Girishde8eb592023-04-13 18:49:17 +0000113 Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
114
Girish6a6044d2023-11-22 21:23:14 +0000115protected:
116 // To get notifications when a resource is added for the first time.
117 void onFirstAdded(const MediaResourceParcel& res, uid_t uid);
118 // To get notifications when a resource has been removed at last.
119 void onLastRemoved(const MediaResourceParcel& res, uid_t uid);
120
121 // Reclaims resources from |clients|. Returns true if reclaim succeeded
122 // for all clients.
123 bool reclaimUnconditionallyFrom(const std::vector<ClientInfo>& targetClients);
124
125 // A helper function that returns true if the callingPid has higher priority than pid.
126 // Returns false otherwise.
127 bool isCallingPriorityHigher_l(int callingPid, int pid);
128
129 // To notify the metrics about client being released.
130 void notifyClientReleased(const ClientInfoParcel& clientInfo);
131
132 virtual Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid);
133
Ronghua Wu231c3d12015-03-11 15:10:32 -0700134private:
135 friend class ResourceManagerServiceTest;
Girishab17b0f2023-11-20 06:00:44 +0000136 friend class ResourceManagerServiceTestBase;
Chong Zhang97d367b2020-09-16 12:53:14 -0700137 friend class DeathNotifier;
138 friend class OverrideProcessInfoDeathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139
Ronghua Wu231c3d12015-03-11 15:10:32 -0700140 // Gets the client who owns specified resource type from lowest possible priority process.
141 // Returns false if the calling process priority is not higher than the lowest process
142 // priority. The client will remain unchanged if returns false.
Girish56fda312023-10-12 21:32:35 +0000143 bool getLowestPriorityBiggestClient_l(
144 const ResourceRequestInfo& resourceRequestInfo,
145 ClientInfo& clientInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700146
Brian Lindahl64ee9452022-01-14 13:31:16 +0100147 // Same method as above, but with pendingRemovalOnly as true.
148 bool getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +0000149 MediaResource::SubType subType,
150 ClientInfo& clientsInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700151
Girish56fda312023-10-12 21:32:35 +0000152 // A helper function basically calls getLowestPriorityBiggestClient_l and adds
Chong Zhangfdd512a2019-11-22 11:03:14 -0800153 // the result client to the given Vector.
Girish56fda312023-10-12 21:32:35 +0000154 void getClientForResource_l(const ResourceRequestInfo& resourceRequestInfo,
155 std::vector<ClientInfo>& clientsInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700156
Girish6a6044d2023-11-22 21:23:14 +0000157 // Eventually we want to phase out this implementation of IResourceManagerService
158 // (ResourceManagerService) and replace that with the newer implementation
159 // (ResourceManagerServiceNew).
160 // So, marking the following methods as private virtual and for the newer implementation
161 // to override is the easiest way to maintain both implementation.
162
163 // Initializes the internal state of the ResourceManagerService
164 virtual void init();
165
166 // Gets the list of all the clients who own the specified resource type.
167 // Returns false if any client belongs to a process with higher priority than the
168 // calling process. The clients will remain unchanged if returns false.
169 virtual bool getAllClients_l(const ResourceRequestInfo& resourceRequestInfo,
170 std::vector<ClientInfo>& clientsInfo);
171
172 // Gets the client who owns biggest piece of specified resource type from pid.
173 // Returns false with no change to client if there are no clients holding resources of this
174 // type.
175 virtual bool getBiggestClient_l(int pid, MediaResource::Type type,
176 MediaResource::SubType subType,
177 ClientInfo& clientsInfo,
178 bool pendingRemovalOnly = false);
179
180 virtual bool overridePid_l(int32_t originalPid, int32_t newPid);
181
182 virtual bool overrideProcessInfo_l(const std::shared_ptr<IResourceManagerClient>& client,
183 int pid, int procState, int oomScore);
Robert Shihc3af31b2019-09-20 21:45:01 -0700184
Henry Fang32762922020-01-28 18:40:39 -0800185 // Get priority from process's pid
Girish6a6044d2023-11-22 21:23:14 +0000186 virtual bool getPriority_l(int pid, int* priority) const;
Henry Fang32762922020-01-28 18:40:39 -0800187
Girish6a6044d2023-11-22 21:23:14 +0000188 // Gets lowest priority process that has the specified resource type.
189 // Returns false if failed. The output parameters will remain unchanged if failed.
190 virtual bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType,
191 int* lowestPriorityPid, int* lowestPriority);
192
193 // Removes the pid from the override map.
194 virtual void removeProcessInfoOverride(int pid);
195
196 // Get the client for given pid and the clientId from the map
197 virtual std::shared_ptr<IResourceManagerClient> getClient(
198 int pid, const int64_t& clientId) const;
199
200 // Remove the client for given pid and the clientId from the map
201 virtual bool removeClient(int pid, const int64_t& clientId);
202
203 // Get all the resource status for dump
204 virtual void getResourceDump(std::string& resourceLog) const;
Chong Zhang97d367b2020-09-16 12:53:14 -0700205
206 void removeProcessInfoOverride_l(int pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700207
Girish6a6044d2023-11-22 21:23:14 +0000208 // A helper function that pushes Reclaim Atom (for metric collection).
Girish9128e242022-11-23 20:52:29 +0000209 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000210 const std::vector<ClientInfo>& targetClients,
211 bool reclaimed);
Girish9128e242022-11-23 20:52:29 +0000212
Girish56fda312023-10-12 21:32:35 +0000213 // The following utility functions are used only for testing by ResourceManagerServiceTest
Girish1f002cf2023-02-17 00:36:29 +0000214 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
215 long getPeakConcurrentPixelCount(int pid) const;
216 // Get the current concurrent pixel count (associated with the video codecs) for the process.
217 long getCurrentConcurrentPixelCount(int pid) const;
Girish1484e5d2023-11-20 06:00:44 +0000218 // To create object of type ResourceManagerServiceNew
219 static std::shared_ptr<ResourceManagerService> CreateNew(
220 const sp<ProcessInfoInterface>& processInfo,
221 const sp<SystemCallbackInterface>& systemResource);
Girish6a6044d2023-11-22 21:23:14 +0000222 // Returns a unmodifiable reference to the internal resource state as a map
223 virtual const std::map<int, ResourceInfos>& getResourceMap() const {
224 return mMap;
225 }
Girish1f002cf2023-02-17 00:36:29 +0000226
Girish6a6044d2023-11-22 21:23:14 +0000227protected:
Girish434b4d82023-07-11 23:24:54 +0000228 mutable std::mutex mLock;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700229 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700230 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700231 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700232 bool mSupportsMultipleSecureCodecs;
233 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700234 int32_t mCpuBoostCount;
Girish6a6044d2023-11-22 21:23:14 +0000235
236private:
237 PidResourceInfosMap mMap;
Chong Zhang97d367b2020-09-16 12:53:14 -0700238 struct ProcessInfoOverride {
Girishf1d166c2023-07-20 22:35:29 +0000239 std::shared_ptr<DeathNotifier> deathNotifier = nullptr;
Chong Zhang97d367b2020-09-16 12:53:14 -0700240 std::shared_ptr<IResourceManagerClient> client;
241 };
Henry Fang32762922020-01-28 18:40:39 -0800242 std::map<int, int> mOverridePidMap;
Chong Zhang97d367b2020-09-16 12:53:14 -0700243 std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700244 std::shared_ptr<ResourceObserverService> mObserverService;
Girish1f002cf2023-02-17 00:36:29 +0000245 std::unique_ptr<ResourceManagerMetrics> mResourceManagerMetrics;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700246};
247
248// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700249} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700250
Chong Zhang181e6952019-10-09 13:23:39 -0700251#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H