• 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 <set>
23 #include <mutex>
24 #include <string>
25 
26 #include <aidl/android/media/BnResourceManagerService.h>
27 #include <arpa/inet.h>
28 #include <media/MediaResource.h>
29 #include <utils/Errors.h>
30 #include <utils/KeyedVector.h>
31 #include <utils/String8.h>
32 #include <utils/threads.h>
33 #include <utils/Vector.h>
34 
35 namespace android {
36 
37 class DeathNotifier;
38 class ResourceManagerService;
39 class ResourceObserverService;
40 class ServiceLog;
41 struct ProcessInfoInterface;
42 class ResourceManagerMetrics;
43 
44 using Status = ::ndk::ScopedAStatus;
45 using ::aidl::android::media::IResourceManagerClient;
46 using ::aidl::android::media::BnResourceManagerService;
47 using ::aidl::android::media::MediaResourceParcel;
48 using ::aidl::android::media::MediaResourcePolicyParcel;
49 using ::aidl::android::media::ClientInfoParcel;
50 using ::aidl::android::media::ClientConfigParcel;
51 
52 typedef std::map<std::tuple<
53         MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
54         MediaResourceParcel> ResourceList;
55 
56 struct ResourceInfo {
57     uid_t uid;
58     int64_t clientId;
59     std::string name;
60     std::shared_ptr<IResourceManagerClient> client;
61     uintptr_t cookie{0};
62     ResourceList resources;
63     bool pendingRemoval{false};
64 };
65 
66 // vector of <PID, UID>
67 typedef std::vector<std::pair<int32_t, uid_t>> PidUidVector;
68 
69 // TODO: convert these to std::map
70 typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
71 typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
72 
73 class ResourceManagerService : public BnResourceManagerService {
74 public:
75     struct SystemCallbackInterface : public RefBase {
76         virtual void noteStartVideo(int uid) = 0;
77         virtual void noteStopVideo(int uid) = 0;
78         virtual void noteResetVideo() = 0;
79         virtual bool requestCpusetBoost(bool enable) = 0;
80     };
81 
getServiceName()82     static char const *getServiceName() { return "media.resource_manager"; }
83     static void instantiate();
84 
85     virtual inline binder_status_t dump(
86             int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
87 
88     ResourceManagerService();
89     explicit ResourceManagerService(const sp<ProcessInfoInterface> &processInfo,
90             const sp<SystemCallbackInterface> &systemResource);
91     virtual ~ResourceManagerService();
92     void setObserverService(const std::shared_ptr<ResourceObserverService>& observerService);
93 
94     // IResourceManagerService interface
95     Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
96 
97     Status addResource(const ClientInfoParcel& clientInfo,
98                        const std::shared_ptr<IResourceManagerClient>& client,
99                        const std::vector<MediaResourceParcel>& resources) override;
100 
101     Status removeResource(const ClientInfoParcel& clientInfo,
102                           const std::vector<MediaResourceParcel>& resources) override;
103 
104     Status removeClient(const ClientInfoParcel& clientInfo) override;
105 
106     // Tries to reclaim resource from processes with lower priority than the calling process
107     // according to the requested resources.
108     // Returns true if any resource has been reclaimed, otherwise returns false.
109     Status reclaimResource(const ClientInfoParcel& clientInfo,
110                            const std::vector<MediaResourceParcel>& resources,
111                            bool* _aidl_return) override;
112 
113     Status overridePid(int32_t originalPid, int32_t newPid) override;
114 
115     Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
116                                int32_t pid, int32_t procState, int32_t oomScore) override;
117 
118     Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
119 
120     Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
121 
122     Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid);
123 
124     Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
125 
126     Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
127 
128     Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
129 
130     Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
131 
132 private:
133     friend class ResourceManagerServiceTest;
134     friend class DeathNotifier;
135     friend class OverrideProcessInfoDeathNotifier;
136 
137     // Reclaims resources from |clients|. Returns true if reclaim succeeded
138     // for all clients.
139     bool reclaimUnconditionallyFrom(const Vector<std::shared_ptr<IResourceManagerClient>> &clients);
140 
141     // Gets the list of all the clients who own the specified resource type.
142     // Returns false if any client belongs to a process with higher priority than the
143     // calling process. The clients will remain unchanged if returns false.
144     bool getAllClients_l(int callingPid, MediaResource::Type type, MediaResource::SubType subType,
145             PidUidVector* idList,
146             Vector<std::shared_ptr<IResourceManagerClient>> *clients);
147 
148     // Gets the client who owns specified resource type from lowest possible priority process.
149     // Returns false if the calling process priority is not higher than the lowest process
150     // priority. The client will remain unchanged if returns false.
151     bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
152             MediaResource::SubType subType, PidUidVector* idList,
153             std::shared_ptr<IResourceManagerClient> *client);
154 
155     // Gets lowest priority process that has the specified resource type.
156     // Returns false if failed. The output parameters will remain unchanged if failed.
157     bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType, int *pid,
158                 int *priority);
159 
160     // Gets the client who owns biggest piece of specified resource type from pid.
161     // Returns false with no change to client if there are no clients holdiing resources of thisi
162     // type.
163     bool getBiggestClient_l(int pid, MediaResource::Type type, MediaResource::SubType subType,
164             uid_t& uid, std::shared_ptr<IResourceManagerClient> *client,
165             bool pendingRemovalOnly = false);
166     // Same method as above, but with pendingRemovalOnly as true.
167     bool getBiggestClientPendingRemoval_l(int pid, MediaResource::Type type,
168             MediaResource::SubType subType, uid_t& uid,
169             std::shared_ptr<IResourceManagerClient> *client);
170 
171     bool isCallingPriorityHigher_l(int callingPid, int pid);
172 
173     // A helper function basically calls getLowestPriorityBiggestClient_l and add
174     // the result client to the given Vector.
175     void getClientForResource_l(int callingPid, const MediaResourceParcel *res,
176             PidUidVector* idList,
177             Vector<std::shared_ptr<IResourceManagerClient>> *clients);
178 
179     void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
180     void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
181 
182     // Merge r2 into r1
183     void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
184 
185     // Get priority from process's pid
186     bool getPriority_l(int pid, int* priority);
187 
188     void removeProcessInfoOverride(int pid);
189 
190     void removeProcessInfoOverride_l(int pid);
191     uintptr_t addCookieAndLink_l(const std::shared_ptr<IResourceManagerClient>& client,
192                                  const sp<DeathNotifier>& notifier);
193     void removeCookieAndUnlink_l(const std::shared_ptr<IResourceManagerClient>& client,
194                                  uintptr_t cookie);
195 
196     void pushReclaimAtom(const ClientInfoParcel& clientInfo,
197                          const Vector<std::shared_ptr<IResourceManagerClient>>& clients,
198                          const PidUidVector& idList, bool reclaimed);
199 
200     // Get the peak concurrent pixel count (associated with the video codecs) for the process.
201     long getPeakConcurrentPixelCount(int pid) const;
202     // Get the current concurrent pixel count (associated with the video codecs) for the process.
203     long getCurrentConcurrentPixelCount(int pid) const;
204 
205     mutable Mutex mLock;
206     sp<ProcessInfoInterface> mProcessInfo;
207     sp<SystemCallbackInterface> mSystemCB;
208     sp<ServiceLog> mServiceLog;
209     PidResourceInfosMap mMap;
210     bool mSupportsMultipleSecureCodecs;
211     bool mSupportsSecureWithNonSecureCodec;
212     int32_t mCpuBoostCount;
213     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
214     struct ProcessInfoOverride {
215         uintptr_t cookie;
216         std::shared_ptr<IResourceManagerClient> client;
217     };
218     std::map<int, int> mOverridePidMap;
219     std::map<pid_t, ProcessInfoOverride> mProcessInfoOverrideMap;
220     static std::mutex sCookieLock;
221     static uintptr_t sCookieCounter GUARDED_BY(sCookieLock);
222     static std::map<uintptr_t, sp<DeathNotifier> > sCookieToDeathNotifierMap
223             GUARDED_BY(sCookieLock);
224     std::shared_ptr<ResourceObserverService> mObserverService;
225     std::unique_ptr<ResourceManagerMetrics> mResourceManagerMetrics;
226 };
227 
228 // ----------------------------------------------------------------------------
229 } // namespace android
230 
231 #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
232