• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
18 #ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
19 #define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
20 
21 #include <map>
22 #include <mutex>
23 
24 #include <aidl/android/media/BnResourceManagerService.h>
25 #include <arpa/inet.h>
26 #include <media/MediaResource.h>
27 #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 
33 namespace android {
34 
35 class DeathNotifier;
36 class ResourceManagerService;
37 class ResourceObserverService;
38 class ServiceLog;
39 struct ProcessInfoInterface;
40 
41 using Status = ::ndk::ScopedAStatus;
42 using ::aidl::android::media::IResourceManagerClient;
43 using ::aidl::android::media::BnResourceManagerService;
44 using ::aidl::android::media::MediaResourceParcel;
45 using ::aidl::android::media::MediaResourcePolicyParcel;
46 
47 typedef std::map<std::tuple<
48         MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
49         MediaResourceParcel> ResourceList;
50 
51 struct ResourceInfo {
52     int64_t clientId;
53     uid_t uid;
54     std::shared_ptr<IResourceManagerClient> client;
55     uintptr_t cookie{0};
56     ResourceList resources;
57     bool pendingRemoval{false};
58 };
59 
60 // TODO: convert these to std::map
61 typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
62 typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
63 
64 class ResourceManagerService : public BnResourceManagerService {
65 public:
66     struct SystemCallbackInterface : public RefBase {
67         virtual void noteStartVideo(int uid) = 0;
68         virtual void noteStopVideo(int uid) = 0;
69         virtual void noteResetVideo() = 0;
70         virtual bool requestCpusetBoost(bool enable) = 0;
71     };
72 
getServiceName()73     static char const *getServiceName() { return "media.resource_manager"; }
74     static void instantiate();
75 
76     virtual inline binder_status_t dump(
77             int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
78 
79     ResourceManagerService();
80     explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
81             const sp<SystemCallbackInterface> &systemResource);
82     virtual ~ResourceManagerService();
83     void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
84 
85     // IResourceManagerService interface
86     Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
87 
88     Status addResource(int32_t pid, int32_t uid, int64_t clientId,
89             const std::shared_ptr<IResourceManagerClient>& client,
90             const std::vector<MediaResourceParcel>& resources) override;
91 
92     Status removeResource(int32_t pid, int64_t clientId,
93             const std::vector<MediaResourceParcel>& resources) override;
94 
95     Status removeClient(int32_t pid, int64_t clientId) override;
96 
97     // 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.
100     Status reclaimResource(int32_t callingPid, const std::vector<MediaResourceParcel>& resources,
101             bool* _aidl_return) override;
102 
103     Status overridePid(int originalPid, int newPid) override;
104 
105     Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client, int pid,
106             int procState, int oomScore) override;
107 
108     Status markClientForPendingRemoval(int32_t pid, int64_t clientId) override;
109 
110     Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
111 
112     Status removeResource(int pid, int64_t clientId, bool checkValid);
113 
114 private:
115     friend class ResourceManagerServiceTest;
116     friend class DeathNotifier;
117     friend class OverrideProcessInfoDeathNotifier;
118 
119     // Reclaims resources from |clients|. Returns true if reclaim succeeded
120     // for all clients.
121     bool reclaimUnconditionallyFrom(const Vector<std::shared_ptr<IResourceManagerClient>> &clients);
122 
123     // 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.
126     bool getAllClients_l(int callingPid, MediaResource::Type type, MediaResource::SubType subType,
127             Vector<std::shared_ptr<IResourceManagerClient>> *clients);
128 
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.
132     bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
133             MediaResource::SubType subType, std::shared_ptr<IResourceManagerClient> *client);
134 
135     // Gets lowest priority process that has the specified resource type.
136     // Returns false if failed. The output parameters will remain unchanged if failed.
137     bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType, int *pid,
138                 int *priority);
139 
140     // Gets the client who owns biggest piece of specified resource type from pid.
141     // 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,
144             std::shared_ptr<IResourceManagerClient> *client,
145             bool pendingRemovalOnly = false);
146     // 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);
149 
150     bool isCallingPriorityHigher_l(int callingPid, int pid);
151 
152     // 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);
156 
157     void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
158     void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
159 
160     // Merge r2 into r1
161     void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
162 
163     // Get priority from process's pid
164     bool getPriority_l(int pid, int* priority);
165 
166     void removeProcessInfoOverride(int pid);
167 
168     void removeProcessInfoOverride_l(int pid);
169     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);
173 
174     mutable Mutex mLock;
175     sp<ProcessInfoInterface> mProcessInfo;
176     sp<SystemCallbackInterface> mSystemCB;
177     sp<ServiceLog> mServiceLog;
178     PidResourceInfosMap mMap;
179     bool mSupportsMultipleSecureCodecs;
180     bool mSupportsSecureWithNonSecureCodec;
181     int32_t mCpuBoostCount;
182     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
183     struct ProcessInfoOverride {
184         uintptr_t cookie;
185         std::shared_ptr<IResourceManagerClient> client;
186     };
187     std::map<int, int> mOverridePidMap;
188     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);
193     std::shared_ptr<ResourceObserverService> mObserverService;
194 };
195 
196 // ----------------------------------------------------------------------------
197 } // namespace android
198 
199 #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
200