blob: a05a346a70c80e5455213cd572f49b53610e6987 [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
Ronghua Wu231c3d12015-03-11 15:10:32 -070033namespace android {
34
Chong Zhangfdd512a2019-11-22 11:03:14 -080035class DeathNotifier;
36class ResourceManagerService;
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
50typedef std::map<std::tuple<
Jooyung Han3d564ff2020-02-22 00:46:06 +090051 MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
Chong Zhang181e6952019-10-09 13:23:39 -070052 MediaResourceParcel> ResourceList;
53
Ronghua Wu231c3d12015-03-11 15:10:32 -070054struct ResourceInfo {
Chong Zhangee33d642019-08-08 14:26:43 -070055 uid_t uid;
Girish9128e242022-11-23 20:52:29 +000056 int64_t clientId;
57 std::string name;
Chong Zhangfdd512a2019-11-22 11:03:14 -080058 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang97d367b2020-09-16 12:53:14 -070059 uintptr_t cookie{0};
Chong Zhangfb092d32019-08-12 09:45:44 -070060 ResourceList resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -070061 bool pendingRemoval{false};
Ronghua Wu231c3d12015-03-11 15:10:32 -070062};
63
Girish1f002cf2023-02-17 00:36:29 +000064// vector of <PID, UID>
Girish9128e242022-11-23 20:52:29 +000065typedef std::vector<std::pair<int32_t, uid_t>> PidUidVector;
66
Girish434b4d82023-07-11 23:24:54 +000067typedef std::map<int64_t, ResourceInfo> ResourceInfos;
68typedef std::map<int, ResourceInfos> PidResourceInfosMap;
Ronghua Wu231c3d12015-03-11 15:10:32 -070069
Chong Zhangfdd512a2019-11-22 11:03:14 -080070class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -070071public:
Chong Zhangdd726802019-08-21 17:24:13 -070072 struct SystemCallbackInterface : public RefBase {
73 virtual void noteStartVideo(int uid) = 0;
74 virtual void noteStopVideo(int uid) = 0;
75 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -080076 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -070077 };
78
Ronghua Wu231c3d12015-03-11 15:10:32 -070079 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -080080 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -070081
Chong Zhangfdd512a2019-11-22 11:03:14 -080082 virtual inline binder_status_t dump(
83 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070084
Ronghua Wu231c3d12015-03-11 15:10:32 -070085 ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +010086 explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -070087 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -080088 virtual ~ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +010089 void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
Ronghua Wu231c3d12015-03-11 15:10:32 -070090
91 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070092 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070093
Girish9128e242022-11-23 20:52:29 +000094 Status addResource(const ClientInfoParcel& clientInfo,
95 const std::shared_ptr<IResourceManagerClient>& client,
96 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070097
Girish9128e242022-11-23 20:52:29 +000098 Status removeResource(const ClientInfoParcel& clientInfo,
99 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -0700100
Girish9128e242022-11-23 20:52:29 +0000101 Status removeClient(const ClientInfoParcel& clientInfo) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102
Ronghua Wu05d89f12015-07-07 16:47:42 -0700103 // Tries to reclaim resource from processes with lower priority than the calling process
104 // according to the requested resources.
105 // Returns true if any resource has been reclaimed, otherwise returns false.
Girish9128e242022-11-23 20:52:29 +0000106 Status reclaimResource(const ClientInfoParcel& clientInfo,
107 const std::vector<MediaResourceParcel>& resources,
108 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700109
Girish9128e242022-11-23 20:52:29 +0000110 Status overridePid(int32_t originalPid, int32_t newPid) override;
Henry Fang32762922020-01-28 18:40:39 -0800111
Girish9128e242022-11-23 20:52:29 +0000112 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
113 int32_t pid, int32_t procState, int32_t oomScore) override;
Chong Zhang97d367b2020-09-16 12:53:14 -0700114
Girish9128e242022-11-23 20:52:29 +0000115 Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700116
Wonsik Kim271429d2020-10-01 10:12:56 -0700117 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
118
Girish9128e242022-11-23 20:52:29 +0000119 Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900120
Girish1f002cf2023-02-17 00:36:29 +0000121 Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
122
123 Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
124
125 Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
126
Girishde8eb592023-04-13 18:49:17 +0000127 Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
128
Ronghua Wu231c3d12015-03-11 15:10:32 -0700129private:
130 friend class ResourceManagerServiceTest;
Chong Zhang97d367b2020-09-16 12:53:14 -0700131 friend class DeathNotifier;
132 friend class OverrideProcessInfoDeathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700133
Wonsik Kim271429d2020-10-01 10:12:56 -0700134 // Reclaims resources from |clients|. Returns true if reclaim succeeded
135 // for all clients.
Girish434b4d82023-07-11 23:24:54 +0000136 bool reclaimUnconditionallyFrom(
137 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700138
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139 // Gets the list of all the clients who own the specified resource type.
140 // Returns false if any client belongs to a process with higher priority than the
141 // calling process. The clients will remain unchanged if returns false.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100142 bool getAllClients_l(int callingPid, MediaResource::Type type, MediaResource::SubType subType,
Girish9128e242022-11-23 20:52:29 +0000143 PidUidVector* idList,
Girish434b4d82023-07-11 23:24:54 +0000144 std::vector<std::shared_ptr<IResourceManagerClient>>* clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700145
146 // Gets the client who owns specified resource type from lowest possible priority process.
147 // Returns false if the calling process priority is not higher than the lowest process
148 // priority. The client will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800149 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +0000150 MediaResource::SubType subType, PidUidVector* idList,
151 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700152
153 // Gets lowest priority process that has the specified resource type.
154 // Returns false if failed. The output parameters will remain unchanged if failed.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100155 bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType, int *pid,
156 int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700157
158 // Gets the client who owns biggest piece of specified resource type from pid.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100159 // Returns false with no change to client if there are no clients holdiing resources of thisi
160 // type.
161 bool getBiggestClient_l(int pid, MediaResource::Type type, MediaResource::SubType subType,
Girish9128e242022-11-23 20:52:29 +0000162 uid_t& uid, std::shared_ptr<IResourceManagerClient> *client,
Wonsik Kimd20e9362020-04-28 10:42:57 -0700163 bool pendingRemovalOnly = false);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100164 // Same method as above, but with pendingRemovalOnly as true.
165 bool getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish9128e242022-11-23 20:52:29 +0000166 MediaResource::SubType subType, uid_t& uid,
167 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700168
169 bool isCallingPriorityHigher_l(int callingPid, int pid);
170
Chong Zhangfdd512a2019-11-22 11:03:14 -0800171 // A helper function basically calls getLowestPriorityBiggestClient_l and add
172 // the result client to the given Vector.
173 void getClientForResource_l(int callingPid, const MediaResourceParcel *res,
Girish9128e242022-11-23 20:52:29 +0000174 PidUidVector* idList,
Girish434b4d82023-07-11 23:24:54 +0000175 std::vector<std::shared_ptr<IResourceManagerClient>>* clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700176
Chong Zhang181e6952019-10-09 13:23:39 -0700177 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
178 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700179
Robert Shihc3af31b2019-09-20 21:45:01 -0700180 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700181 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700182
Henry Fang32762922020-01-28 18:40:39 -0800183 // Get priority from process's pid
184 bool getPriority_l(int pid, int* priority);
185
Chong Zhang97d367b2020-09-16 12:53:14 -0700186 void removeProcessInfoOverride(int pid);
187
188 void removeProcessInfoOverride_l(int pid);
Arun Johnsond1f59732022-03-25 17:10:29 +0000189 uintptr_t addCookieAndLink_l(const std::shared_ptr<IResourceManagerClient>& client,
190 const sp<DeathNotifier>& notifier);
191 void removeCookieAndUnlink_l(const std::shared_ptr<IResourceManagerClient>& client,
192 uintptr_t cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700193
Girish9128e242022-11-23 20:52:29 +0000194 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish434b4d82023-07-11 23:24:54 +0000195 const std::vector<std::shared_ptr<IResourceManagerClient>>& clients,
Girish9128e242022-11-23 20:52:29 +0000196 const PidUidVector& idList, bool reclaimed);
197
Girish1f002cf2023-02-17 00:36:29 +0000198 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
199 long getPeakConcurrentPixelCount(int pid) const;
200 // Get the current concurrent pixel count (associated with the video codecs) for the process.
201 long getCurrentConcurrentPixelCount(int pid) const;
202
Girish434b4d82023-07-11 23:24:54 +0000203 mutable std::mutex mLock;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700204 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700205 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700206 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700207 PidResourceInfosMap mMap;
208 bool mSupportsMultipleSecureCodecs;
209 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700210 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800211 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Chong Zhang97d367b2020-09-16 12:53:14 -0700212 struct ProcessInfoOverride {
213 uintptr_t cookie;
214 std::shared_ptr<IResourceManagerClient> client;
215 };
Henry Fang32762922020-01-28 18:40:39 -0800216 std::map<int, int> mOverridePidMap;
Chong Zhang97d367b2020-09-16 12:53:14 -0700217 std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap;
218 static std::mutex sCookieLock;
219 static uintptr_t sCookieCounter GUARDED_BY(sCookieLock);
220 static std::map<uintptr_t, sp<DeathNotifier> > sCookieToDeathNotifierMap
221 GUARDED_BY(sCookieLock);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700222 std::shared_ptr<ResourceObserverService> mObserverService;
Girish1f002cf2023-02-17 00:36:29 +0000223 std::unique_ptr<ResourceManagerMetrics> mResourceManagerMetrics;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700224};
225
226// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700227} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700228
Chong Zhang181e6952019-10-09 13:23:39 -0700229#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H