blob: 69ebf240cd957dbc1741490369d32aca6193e5d0 [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
Brian Lindahl64ee9452022-01-14 13:31:16 +010076 void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
Ronghua Wu231c3d12015-03-11 15:10:32 -070077
78 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070079 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070080
Girish9128e242022-11-23 20:52:29 +000081 Status addResource(const ClientInfoParcel& clientInfo,
82 const std::shared_ptr<IResourceManagerClient>& client,
83 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070084
Girish9128e242022-11-23 20:52:29 +000085 Status removeResource(const ClientInfoParcel& clientInfo,
86 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -070087
Girish9128e242022-11-23 20:52:29 +000088 Status removeClient(const ClientInfoParcel& clientInfo) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070089
Ronghua Wu05d89f12015-07-07 16:47:42 -070090 // Tries to reclaim resource from processes with lower priority than the calling process
91 // according to the requested resources.
92 // Returns true if any resource has been reclaimed, otherwise returns false.
Girish9128e242022-11-23 20:52:29 +000093 Status reclaimResource(const ClientInfoParcel& clientInfo,
94 const std::vector<MediaResourceParcel>& resources,
95 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070096
Girish9128e242022-11-23 20:52:29 +000097 Status overridePid(int32_t originalPid, int32_t newPid) override;
Henry Fang32762922020-01-28 18:40:39 -080098
Girish9128e242022-11-23 20:52:29 +000099 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
100 int32_t pid, int32_t procState, int32_t oomScore) override;
Chong Zhang97d367b2020-09-16 12:53:14 -0700101
Girish9128e242022-11-23 20:52:29 +0000102 Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700103
Wonsik Kim271429d2020-10-01 10:12:56 -0700104 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
105
Girish9128e242022-11-23 20:52:29 +0000106 Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900107
Girish1f002cf2023-02-17 00:36:29 +0000108 Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
109
110 Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
111
112 Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
113
Girishde8eb592023-04-13 18:49:17 +0000114 Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
115
Ronghua Wu231c3d12015-03-11 15:10:32 -0700116private:
117 friend class ResourceManagerServiceTest;
Girishab17b0f2023-11-20 06:00:44 +0000118 friend class ResourceManagerServiceTestBase;
Chong Zhang97d367b2020-09-16 12:53:14 -0700119 friend class DeathNotifier;
120 friend class OverrideProcessInfoDeathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700121
Wonsik Kim271429d2020-10-01 10:12:56 -0700122 // Reclaims resources from |clients|. Returns true if reclaim succeeded
123 // for all clients.
Girish434b4d82023-07-11 23:24:54 +0000124 bool reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000125 const std::vector<ClientInfo>& targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700126
Ronghua Wu231c3d12015-03-11 15:10:32 -0700127 // Gets the list of all the clients who own the specified resource type.
128 // Returns false if any client belongs to a process with higher priority than the
129 // calling process. The clients will remain unchanged if returns false.
Girish56fda312023-10-12 21:32:35 +0000130 bool getAllClients_l(const ResourceRequestInfo& resourceRequestInfo,
131 std::vector<ClientInfo>& clientsInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700132
133 // Gets the client who owns specified resource type from lowest possible priority process.
134 // Returns false if the calling process priority is not higher than the lowest process
135 // priority. The client will remain unchanged if returns false.
Girish56fda312023-10-12 21:32:35 +0000136 bool getLowestPriorityBiggestClient_l(
137 const ResourceRequestInfo& resourceRequestInfo,
138 ClientInfo& clientInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139
140 // Gets the client who owns biggest piece of specified resource type from pid.
Girish56fda312023-10-12 21:32:35 +0000141 // Returns false with no change to client if there are no clients holding resources of this
Brian Lindahl64ee9452022-01-14 13:31:16 +0100142 // type.
143 bool getBiggestClient_l(int pid, MediaResource::Type type, MediaResource::SubType subType,
Girishab17b0f2023-11-20 06:00:44 +0000144 ClientInfo& clientsInfo,
Girish56fda312023-10-12 21:32:35 +0000145 bool pendingRemovalOnly = false);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100146 // Same method as above, but with pendingRemovalOnly as true.
147 bool getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girishab17b0f2023-11-20 06:00:44 +0000148 MediaResource::SubType subType,
149 ClientInfo& clientsInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700150
Girish56fda312023-10-12 21:32:35 +0000151 // A helper function that returns true if the callingPid has higher priority than pid.
152 // Returns false otherwise.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700153 bool isCallingPriorityHigher_l(int callingPid, int pid);
154
Girish56fda312023-10-12 21:32:35 +0000155 // A helper function basically calls getLowestPriorityBiggestClient_l and adds
Chong Zhangfdd512a2019-11-22 11:03:14 -0800156 // the result client to the given Vector.
Girish56fda312023-10-12 21:32:35 +0000157 void getClientForResource_l(const ResourceRequestInfo& resourceRequestInfo,
158 std::vector<ClientInfo>& clientsInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700159
Girishab17b0f2023-11-20 06:00:44 +0000160 void onFirstAdded(const MediaResourceParcel& res, uid_t uid);
161 void onLastRemoved(const MediaResourceParcel& res, uid_t uid);
Robert Shihc3af31b2019-09-20 21:45:01 -0700162
Henry Fang32762922020-01-28 18:40:39 -0800163 // Get priority from process's pid
164 bool getPriority_l(int pid, int* priority);
165
Chong Zhang97d367b2020-09-16 12:53:14 -0700166 void removeProcessInfoOverride(int pid);
167
168 void removeProcessInfoOverride_l(int pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700169
Girish9128e242022-11-23 20:52:29 +0000170 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000171 const std::vector<ClientInfo>& targetClients,
172 bool reclaimed);
Girish9128e242022-11-23 20:52:29 +0000173
Girishab17b0f2023-11-20 06:00:44 +0000174 // Get the client for given pid and the clientId from the map
175 std::shared_ptr<IResourceManagerClient> getClient(int pid, const int64_t& clientId) const;
176
177 // Remove the client for given pid and the clientId from the map
178 bool removeClient(int pid, const int64_t& clientId);
179
Girish56fda312023-10-12 21:32:35 +0000180 // The following utility functions are used only for testing by ResourceManagerServiceTest
181 // Gets lowest priority process that has the specified resource type.
182 // Returns false if failed. The output parameters will remain unchanged if failed.
183 bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType,
184 int* lowestPriorityPid, int* lowestPriority);
Girish1f002cf2023-02-17 00:36:29 +0000185 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
186 long getPeakConcurrentPixelCount(int pid) const;
187 // Get the current concurrent pixel count (associated with the video codecs) for the process.
188 long getCurrentConcurrentPixelCount(int pid) const;
Girish1484e5d2023-11-20 06:00:44 +0000189 // To create object of type ResourceManagerServiceNew
190 static std::shared_ptr<ResourceManagerService> CreateNew(
191 const sp<ProcessInfoInterface>& processInfo,
192 const sp<SystemCallbackInterface>& systemResource);
Girish1f002cf2023-02-17 00:36:29 +0000193
Girish434b4d82023-07-11 23:24:54 +0000194 mutable std::mutex mLock;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700195 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700196 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700197 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700198 PidResourceInfosMap mMap;
199 bool mSupportsMultipleSecureCodecs;
200 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700201 int32_t mCpuBoostCount;
Chong Zhang97d367b2020-09-16 12:53:14 -0700202 struct ProcessInfoOverride {
Girishf1d166c2023-07-20 22:35:29 +0000203 std::shared_ptr<DeathNotifier> deathNotifier = nullptr;
Chong Zhang97d367b2020-09-16 12:53:14 -0700204 std::shared_ptr<IResourceManagerClient> client;
205 };
Henry Fang32762922020-01-28 18:40:39 -0800206 std::map<int, int> mOverridePidMap;
Chong Zhang97d367b2020-09-16 12:53:14 -0700207 std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap;
Chong Zhanga9d45c72020-09-09 12:41:17 -0700208 std::shared_ptr<ResourceObserverService> mObserverService;
Girish1f002cf2023-02-17 00:36:29 +0000209 std::unique_ptr<ResourceManagerMetrics> mResourceManagerMetrics;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210};
211
212// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700213} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700214
Chong Zhang181e6952019-10-09 13:23:39 -0700215#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H