blob: c636a0f15fa1a2dfb91e473998fa69f045a34133 [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>
Chong Zhang97d367b2020-09-16 12:53:14 -070022#include <mutex>
Wonsik Kimd20e9362020-04-28 10:42:57 -070023
Chong Zhangfdd512a2019-11-22 11:03:14 -080024#include <aidl/android/media/BnResourceManagerService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <arpa/inet.h>
Chong Zhang181e6952019-10-09 13:23:39 -070026#include <media/MediaResource.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <utils/Errors.h>
28#include <utils/KeyedVector.h>
29#include <utils/String8.h>
30#include <utils/threads.h>
31#include <utils/Vector.h>
32
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;
40
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;
Chong Zhang181e6952019-10-09 13:23:39 -070046
47typedef std::map<std::tuple<
Jooyung Han3d564ff2020-02-22 00:46:06 +090048 MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
Chong Zhang181e6952019-10-09 13:23:39 -070049 MediaResourceParcel> ResourceList;
50
Ronghua Wu231c3d12015-03-11 15:10:32 -070051struct ResourceInfo {
52 int64_t clientId;
Chong Zhangee33d642019-08-08 14:26:43 -070053 uid_t uid;
Chong Zhangfdd512a2019-11-22 11:03:14 -080054 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang97d367b2020-09-16 12:53:14 -070055 uintptr_t cookie{0};
Chong Zhangfb092d32019-08-12 09:45:44 -070056 ResourceList resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -070057 bool pendingRemoval{false};
Ronghua Wu231c3d12015-03-11 15:10:32 -070058};
59
Chong Zhangfb092d32019-08-12 09:45:44 -070060// TODO: convert these to std::map
61typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
Ronghua Wu231c3d12015-03-11 15:10:32 -070062typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
63
Chong Zhangfdd512a2019-11-22 11:03:14 -080064class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -070065public:
Chong Zhangdd726802019-08-21 17:24:13 -070066 struct SystemCallbackInterface : public RefBase {
67 virtual void noteStartVideo(int uid) = 0;
68 virtual void noteStopVideo(int uid) = 0;
69 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -080070 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -070071 };
72
Ronghua Wu231c3d12015-03-11 15:10:32 -070073 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -080074 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -070075
Chong Zhangfdd512a2019-11-22 11:03:14 -080076 virtual inline binder_status_t dump(
77 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070078
Ronghua Wu231c3d12015-03-11 15:10:32 -070079 ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +010080 explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
Chong Zhangdd726802019-08-21 17:24:13 -070081 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -080082 virtual ~ResourceManagerService();
Brian Lindahl64ee9452022-01-14 13:31:16 +010083 void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
Ronghua Wu231c3d12015-03-11 15:10:32 -070084
85 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070086 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070087
Brian Lindahl64ee9452022-01-14 13:31:16 +010088 Status addResource(int32_t pid, int32_t uid, int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -080089 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -070090 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070091
Brian Lindahl64ee9452022-01-14 13:31:16 +010092 Status removeResource(int32_t pid, int64_t clientId,
Chong Zhang181e6952019-10-09 13:23:39 -070093 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -070094
Chong Zhang181e6952019-10-09 13:23:39 -070095 Status removeClient(int32_t pid, int64_t clientId) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070096
Ronghua Wu05d89f12015-07-07 16:47:42 -070097 // Tries to reclaim resource from processes with lower priority than the calling process
98 // according to the requested resources.
99 // Returns true if any resource has been reclaimed, otherwise returns false.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100100 Status reclaimResource(int32_t callingPid, const std::vector<MediaResourceParcel>& resources,
Chong Zhang181e6952019-10-09 13:23:39 -0700101 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102
Brian Lindahl64ee9452022-01-14 13:31:16 +0100103 Status overridePid(int originalPid, int newPid) override;
Henry Fang32762922020-01-28 18:40:39 -0800104
Brian Lindahl64ee9452022-01-14 13:31:16 +0100105 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client, int pid,
106 int procState, int oomScore) override;
Chong Zhang97d367b2020-09-16 12:53:14 -0700107
Wonsik Kimd20e9362020-04-28 10:42:57 -0700108 Status markClientForPendingRemoval(int32_t pid, int64_t clientId) override;
109
Wonsik Kim271429d2020-10-01 10:12:56 -0700110 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
111
Chong Zhang181e6952019-10-09 13:23:39 -0700112 Status removeResource(int pid, int64_t clientId, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900113
Ronghua Wu231c3d12015-03-11 15:10:32 -0700114private:
115 friend class ResourceManagerServiceTest;
Chong Zhang97d367b2020-09-16 12:53:14 -0700116 friend class DeathNotifier;
117 friend class OverrideProcessInfoDeathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700118
Wonsik Kim271429d2020-10-01 10:12:56 -0700119 // Reclaims resources from |clients|. Returns true if reclaim succeeded
120 // for all clients.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100121 bool reclaimUnconditionallyFrom(const Vector<std::shared_ptr<IResourceManagerClient>> &clients);
Wonsik Kim271429d2020-10-01 10:12:56 -0700122
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123 // Gets the list of all the clients who own the specified resource type.
124 // Returns false if any client belongs to a process with higher priority than the
125 // calling process. The clients will remain unchanged if returns false.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100126 bool getAllClients_l(int callingPid, MediaResource::Type type, MediaResource::SubType subType,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800127 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700128
129 // Gets the client who owns specified resource type from lowest possible priority process.
130 // Returns false if the calling process priority is not higher than the lowest process
131 // priority. The client will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800132 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Brian Lindahl64ee9452022-01-14 13:31:16 +0100133 MediaResource::SubType subType, std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700134
135 // Gets lowest priority process that has the specified resource type.
136 // Returns false if failed. The output parameters will remain unchanged if failed.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100137 bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType, int *pid,
138 int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700139
140 // Gets the client who owns biggest piece of specified resource type from pid.
Brian Lindahl64ee9452022-01-14 13:31:16 +0100141 // Returns false with no change to client if there are no clients holdiing resources of thisi
142 // type.
143 bool getBiggestClient_l(int pid, MediaResource::Type type, MediaResource::SubType subType,
Wonsik Kimd20e9362020-04-28 10:42:57 -0700144 std::shared_ptr<IResourceManagerClient> *client,
145 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,
148 MediaResource::SubType subType, std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700149
150 bool isCallingPriorityHigher_l(int callingPid, int pid);
151
Chong Zhangfdd512a2019-11-22 11:03:14 -0800152 // A helper function basically calls getLowestPriorityBiggestClient_l and add
153 // the result client to the given Vector.
154 void getClientForResource_l(int callingPid, const MediaResourceParcel *res,
155 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700156
Chong Zhang181e6952019-10-09 13:23:39 -0700157 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
158 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700159
Robert Shihc3af31b2019-09-20 21:45:01 -0700160 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700161 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
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);
Arun Johnsond1f59732022-03-25 17:10:29 +0000169 uintptr_t addCookieAndLink_l(const std::shared_ptr<IResourceManagerClient>& client,
170 const sp<DeathNotifier>& notifier);
171 void removeCookieAndUnlink_l(const std::shared_ptr<IResourceManagerClient>& client,
172 uintptr_t cookie);
Chong Zhang97d367b2020-09-16 12:53:14 -0700173
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174 mutable Mutex mLock;
175 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700176 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700177 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700178 PidResourceInfosMap mMap;
179 bool mSupportsMultipleSecureCodecs;
180 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700181 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800182 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Chong Zhang97d367b2020-09-16 12:53:14 -0700183 struct ProcessInfoOverride {
184 uintptr_t cookie;
185 std::shared_ptr<IResourceManagerClient> client;
186 };
Henry Fang32762922020-01-28 18:40:39 -0800187 std::map<int, int> mOverridePidMap;
Chong Zhang97d367b2020-09-16 12:53:14 -0700188 std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap;
189 static std::mutex sCookieLock;
190 static uintptr_t sCookieCounter GUARDED_BY(sCookieLock);
191 static std::map<uintptr_t, sp<DeathNotifier> > sCookieToDeathNotifierMap
192 GUARDED_BY(sCookieLock);
Chong Zhanga9d45c72020-09-09 12:41:17 -0700193 std::shared_ptr<ResourceObserverService> mObserverService;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700194};
195
196// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700197} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700198
Chong Zhang181e6952019-10-09 13:23:39 -0700199#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H