blob: de7e4a3245c872eb23aa68feb8c76b3e9c6e9335 [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;
Chong Zhanga9d45c72020-09-09 12:41:17 -070036class ResourceObserverService;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070037class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070038struct ProcessInfoInterface;
Girish1f002cf2023-02-17 00:36:29 +000039class ResourceManagerMetrics;
Ronghua Wu231c3d12015-03-11 15:10:32 -070040
Chong Zhangfdd512a2019-11-22 11:03:14 -080041using Status = ::ndk::ScopedAStatus;
42using ::aidl::android::media::IResourceManagerClient;
43using ::aidl::android::media::BnResourceManagerService;
44using ::aidl::android::media::MediaResourceParcel;
45using ::aidl::android::media::MediaResourcePolicyParcel;
Girish9128e242022-11-23 20:52:29 +000046using ::aidl::android::media::ClientInfoParcel;
Girish1f002cf2023-02-17 00:36:29 +000047using ::aidl::android::media::ClientConfigParcel;
Chong Zhang181e6952019-10-09 13:23:39 -070048
49typedef std::map<std::tuple<
Jooyung Han3d564ff2020-02-22 00:46:06 +090050 MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
Chong Zhang181e6952019-10-09 13:23:39 -070051 MediaResourceParcel> ResourceList;
52
Ronghua Wu231c3d12015-03-11 15:10:32 -070053struct ResourceInfo {
Chong Zhangee33d642019-08-08 14:26:43 -070054 uid_t uid;
Girish9128e242022-11-23 20:52:29 +000055 int64_t clientId;
56 std::string name;
Chong Zhangfdd512a2019-11-22 11:03:14 -080057 std::shared_ptr<IResourceManagerClient> client;
Girishf1d166c2023-07-20 22:35:29 +000058 std::shared_ptr<DeathNotifier> deathNotifier = nullptr;
Chong Zhangfb092d32019-08-12 09:45:44 -070059 ResourceList resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -070060 bool pendingRemoval{false};
Ronghua Wu231c3d12015-03-11 15:10:32 -070061};
62
Girish56fda312023-10-12 21:32:35 +000063/*
64 * Resource request info that encapsulates
65 * - the calling/requesting process pid.
66 * - the resource requesting (to be reclaimed from others)
67 */
68struct ResourceRequestInfo {
69 // uid of the calling/requesting process.
70 int mCallingPid = -1;
71 // resources requested.
72 const ::aidl::android::media::MediaResourceParcel* mResource;
73};
74
75/*
76 * Structure that defines the Client - a possible target to relcaim from.
77 * This encapsulates pid, uid of the process and the client.
78 * based on the reclaim policy.
79 */
80struct ClientInfo {
81 // pid of the process.
82 pid_t mPid;
83 // uid of the process.
84 uid_t mUid;
85 // Client to relcaim from.
86 std::shared_ptr<::aidl::android::media::IResourceManagerClient> mClient;
87 ClientInfo(
88 pid_t pid = -1,
89 uid_t uid = -1,
90 const std::shared_ptr<::aidl::android::media::IResourceManagerClient>& client = nullptr)
91 : mPid(pid),
92 mUid(uid),
93 mClient(client) {
94 }
95};
Girish9128e242022-11-23 20:52:29 +000096
Girish434b4d82023-07-11 23:24:54 +000097typedef std::map<int64_t, ResourceInfo> ResourceInfos;
98typedef std::map<int, ResourceInfos> PidResourceInfosMap;
Ronghua Wu231c3d12015-03-11 15:10:32 -070099
Chong Zhangfdd512a2019-11-22 11:03:14 -0800100class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700101public:
Chong Zhangdd726802019-08-21 17:24:13 -0700102 struct SystemCallbackInterface : public RefBase {
103 virtual void noteStartVideo(int uid) = 0;
104 virtual void noteStopVideo(int uid) = 0;
105 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800106 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -0700107 };
108
Ronghua Wu231c3d12015-03-11 15:10:32 -0700109 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800110 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700111
Chong Zhangfdd512a2019-11-22 11:03:14 -0800112 virtual inline binder_status_t dump(
113 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700114
Ronghua Wu231c3d12015-03-11 15:10:32 -0700115 ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +0100116 explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -0700117 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800118 virtual ~ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +0100119 void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700120
121 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -0700122 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123
Girish9128e242022-11-23 20:52:29 +0000124 Status addResource(const ClientInfoParcel& clientInfo,
125 const std::shared_ptr<IResourceManagerClient>& client,
126 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700127
Girish9128e242022-11-23 20:52:29 +0000128 Status removeResource(const ClientInfoParcel& clientInfo,
129 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -0700130
Girish9128e242022-11-23 20:52:29 +0000131 Status removeClient(const ClientInfoParcel& clientInfo) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700132
Ronghua Wu05d89f12015-07-07 16:47:42 -0700133 // Tries to reclaim resource from processes with lower priority than the calling process
134 // according to the requested resources.
135 // Returns true if any resource has been reclaimed, otherwise returns false.
Girish9128e242022-11-23 20:52:29 +0000136 Status reclaimResource(const ClientInfoParcel& clientInfo,
137 const std::vector<MediaResourceParcel>& resources,
138 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139
Girish9128e242022-11-23 20:52:29 +0000140 Status overridePid(int32_t originalPid, int32_t newPid) override;
Henry Fang32762922020-01-28 18:40:39 -0800141
Girish9128e242022-11-23 20:52:29 +0000142 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
143 int32_t pid, int32_t procState, int32_t oomScore) override;
Chong Zhang97d367b2020-09-16 12:53:14 -0700144
Girish9128e242022-11-23 20:52:29 +0000145 Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700146
Wonsik Kim271429d2020-10-01 10:12:56 -0700147 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
148
Girish9128e242022-11-23 20:52:29 +0000149 Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900150
Girish1f002cf2023-02-17 00:36:29 +0000151 Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
152
153 Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
154
155 Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
156
Girishde8eb592023-04-13 18:49:17 +0000157 Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
158
Ronghua Wu231c3d12015-03-11 15:10:32 -0700159private:
160 friend class ResourceManagerServiceTest;
Chong Zhang97d367b2020-09-16 12:53:14 -0700161 friend class DeathNotifier;
162 friend class OverrideProcessInfoDeathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700163
Wonsik Kim271429d2020-10-01 10:12:56 -0700164 // Reclaims resources from |clients|. Returns true if reclaim succeeded
165 // for all clients.
Girish434b4d82023-07-11 23:24:54 +0000166 bool reclaimUnconditionallyFrom(
Girish56fda312023-10-12 21:32:35 +0000167 const std::vector<ClientInfo>& targetClients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700168
Ronghua Wu231c3d12015-03-11 15:10:32 -0700169 // Gets the list of all the clients who own the specified resource type.
170 // Returns false if any client belongs to a process with higher priority than the
171 // calling process. The clients will remain unchanged if returns false.
Girish56fda312023-10-12 21:32:35 +0000172 bool getAllClients_l(const ResourceRequestInfo& resourceRequestInfo,
173 std::vector<ClientInfo>& clientsInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174
175 // Gets the client who owns specified resource type from lowest possible priority process.
176 // Returns false if the calling process priority is not higher than the lowest process
177 // priority. The client will remain unchanged if returns false.
Girish56fda312023-10-12 21:32:35 +0000178 bool getLowestPriorityBiggestClient_l(
179 const ResourceRequestInfo& resourceRequestInfo,
180 ClientInfo& clientInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700181
182 // Gets the client who owns biggest piece of specified resource type from pid.
Girish56fda312023-10-12 21:32:35 +0000183 // Returns false with no change to client if there are no clients holding resources of this
Brian Lindahl64ee9452022-01-14 13:31:16 +0100184 // type.
185 bool getBiggestClient_l(int pid, MediaResource::Type type, MediaResource::SubType subType,
Girish56fda312023-10-12 21:32:35 +0000186 uid_t& uid, std::shared_ptr<IResourceManagerClient> *client,
187 bool pendingRemovalOnly = false);
Brian Lindahl64ee9452022-01-14 13:31:16 +0100188 // Same method as above, but with pendingRemovalOnly as true.
189 bool getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
Girish56fda312023-10-12 21:32:35 +0000190 MediaResource::SubType subType, uid_t& uid,
191 std::shared_ptr<IResourceManagerClient>* client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700192
Girish56fda312023-10-12 21:32:35 +0000193 // A helper function that returns true if the callingPid has higher priority than pid.
194 // Returns false otherwise.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700195 bool isCallingPriorityHigher_l(int callingPid, int pid);
196
Girish56fda312023-10-12 21:32:35 +0000197 // A helper function basically calls getLowestPriorityBiggestClient_l and adds
Chong Zhangfdd512a2019-11-22 11:03:14 -0800198 // the result client to the given Vector.
Girish56fda312023-10-12 21:32:35 +0000199 void getClientForResource_l(const ResourceRequestInfo& resourceRequestInfo,
200 std::vector<ClientInfo>& clientsInfo);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700201
Chong Zhang181e6952019-10-09 13:23:39 -0700202 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
203 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700204
Robert Shihc3af31b2019-09-20 21:45:01 -0700205 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700206 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700207
Henry Fang32762922020-01-28 18:40:39 -0800208 // Get priority from process's pid
209 bool getPriority_l(int pid, int* priority);
210
Chong Zhang97d367b2020-09-16 12:53:14 -0700211 void removeProcessInfoOverride(int pid);
212
213 void removeProcessInfoOverride_l(int pid);
Chong Zhang97d367b2020-09-16 12:53:14 -0700214
Girish9128e242022-11-23 20:52:29 +0000215 void pushReclaimAtom(const ClientInfoParcel& clientInfo,
Girish56fda312023-10-12 21:32:35 +0000216 const std::vector<ClientInfo>& targetClients,
217 bool reclaimed);
Girish9128e242022-11-23 20:52:29 +0000218
Girish56fda312023-10-12 21:32:35 +0000219 // The following utility functions are used only for testing by ResourceManagerServiceTest
220 // Gets lowest priority process that has the specified resource type.
221 // Returns false if failed. The output parameters will remain unchanged if failed.
222 bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType,
223 int* lowestPriorityPid, int* lowestPriority);
Girish1f002cf2023-02-17 00:36:29 +0000224 // Get the peak concurrent pixel count (associated with the video codecs) for the process.
225 long getPeakConcurrentPixelCount(int pid) const;
226 // Get the current concurrent pixel count (associated with the video codecs) for the process.
227 long getCurrentConcurrentPixelCount(int pid) const;
228
Girish434b4d82023-07-11 23:24:54 +0000229 mutable std::mutex mLock;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700230 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700231 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700232 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700233 PidResourceInfosMap mMap;
234 bool mSupportsMultipleSecureCodecs;
235 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700236 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800237 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
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