• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **
3 ** Copyright 2023, 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_RESOURCEMANAGERSERVICENEW_H
19 #define ANDROID_MEDIA_RESOURCEMANAGERSERVICENEW_H
20 
21 #include "ResourceManagerService.h"
22 
23 namespace android {
24 
25 class IReclaimPolicy;
26 class IResourceModel;
27 class ResourceTracker;
28 
29 //
30 // A newer implementation of IResourceManagerService, which
31 // eventually will replace the older implementation in ResourceManagerService.
32 //
33 // To make the transition easier, this implementation overrides the
34 // private virtual methods from ResourceManagerService.
35 //
36 // This implementation is devised to abstract and integrate:
37 //   - resources into an independent abstraction
38 //   - resource model as a separate interface (and implementation)
39 //   - reclaim policy as a separate interface (and implementation)
40 //
41 class ResourceManagerServiceNew : public ResourceManagerService {
42 public:
43 
44     explicit ResourceManagerServiceNew(const sp<ProcessInfoInterface>& processInfo,
45                                        const sp<SystemCallbackInterface>& systemResource);
46     virtual ~ResourceManagerServiceNew();
47 
48     // IResourceManagerService interface
49     Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
50 
51     Status addResource(const ClientInfoParcel& clientInfo,
52                        const std::shared_ptr<IResourceManagerClient>& client,
53                        const std::vector<MediaResourceParcel>& resources) override;
54 
55     Status removeResource(const ClientInfoParcel& clientInfo,
56                           const std::vector<MediaResourceParcel>& resources) override;
57 
58     Status removeClient(const ClientInfoParcel& clientInfo) override;
59 
60     Status reclaimResource(const ClientInfoParcel& clientInfo,
61                            const std::vector<MediaResourceParcel>& resources,
62                            bool* _aidl_return) override;
63 
64     Status overridePid(int32_t originalPid, int32_t newPid) override;
65 
66     Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client,
67                                int32_t pid, int32_t procState, int32_t oomScore) override;
68 
69     Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override;
70 
71     Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
72 
73     Status notifyClientCreated(const ClientInfoParcel& clientInfo) override;
74 
75     Status notifyClientStarted(const ClientConfigParcel& clientConfig) override;
76 
77     Status notifyClientStopped(const ClientConfigParcel& clientConfig) override;
78 
79     Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override;
80 
81     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
82 
83     friend class ResourceTracker;
84 
85 private:
86 
87     // Set up the Resource models.
88     void setUpResourceModels();
89 
90     // Set up the Reclaim Policies.
91     void setUpReclaimPolicies();
92 
93     // From the list of clients, pick/select client(s) based on the reclaim policy.
94     void getClientForResource_l(
95         const ReclaimRequestInfo& reclaimRequestInfo,
96         const std::vector<ClientInfo>& clients,
97         std::vector<ClientInfo>& targetClients);
98 
99     // Initializes the internal state of the ResourceManagerService
100     void init() override;
101 
102     void setObserverService(
103             const std::shared_ptr<ResourceObserverService>& observerService) override;
104 
105     // Gets the list of all the clients who own the specified resource type.
106     // Returns false if any client belongs to a process with higher priority than the
107     // calling process. The clients will remain unchanged if returns false.
108     bool getTargetClients(
109         const ClientInfoParcel& clientInfo,
110         const std::vector<MediaResourceParcel>& resources,
111         std::vector<ClientInfo>& targetClients) override;
112 
113     // Removes the pid from the override map.
114     void removeProcessInfoOverride(int pid) override;
115 
116     // override the pid of given process
117     bool overridePid_l(int32_t originalPid, int32_t newPid) override;
118 
119     // override the process info of given process
120     bool overrideProcessInfo_l(const std::shared_ptr<IResourceManagerClient>& client,
121                                int pid, int procState, int oomScore) override;
122 
123     // Get priority from process's pid
124     bool getPriority_l(int pid, int* priority) const override;
125 
126     // Get the client for given pid and the clientId from the map
127     std::shared_ptr<IResourceManagerClient> getClient_l(
128         int pid, const int64_t& clientId) const override;
129 
130     // Remove the client for given pid and the clientId from the map
131     bool removeClient_l(int pid, const int64_t& clientId) override;
132 
133     // Get all the resource status for dump
134     void getResourceDump(std::string& resourceLog) const override;
135 
136     // Returns a unmodifiable reference to the internal resource state as a map
137     const std::map<int, ResourceInfos>& getResourceMap() const override;
138 
139     Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid) override;
140 
141     // The following utility functions are used only for testing by ResourceManagerServiceTest
142     // START: TEST only functions
143     // Gets the list of all the clients who own the specified resource type.
144     // Returns false if any client belongs to a process with higher priority than the
145     // calling process. The clients will remain unchanged if returns false.
146     bool getAllClients_l(const ResourceRequestInfo& resourceRequestInfo,
147                          std::vector<ClientInfo>& clientsInfo) override;
148 
149     // Gets the client who owns specified resource type from lowest possible priority process.
150     // Returns false if the calling process priority is not higher than the lowest process
151     // priority. The client will remain unchanged if returns false.
152     bool getLowestPriorityBiggestClient_l(
153         const ResourceRequestInfo& resourceRequestInfo,
154         ClientInfo& clientInfo) override;
155 
156     // Gets lowest priority process that has the specified resource type.
157     // Returns false if failed. The output parameters will remain unchanged if failed.
158     bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType,
159                                 int* lowestPriorityPid, int* lowestPriority) override;
160 
161     // enable/disable process priority based reclaim and client importance based reclaim
162     void setReclaimPolicy(bool processPriority, bool clientImportance) override;
163     // END: TEST only functions
164 
165 private:
166     std::shared_ptr<ResourceTracker> mResourceTracker;
167     std::unique_ptr<IResourceModel> mDefaultResourceModel;
168     std::vector<std::unique_ptr<IReclaimPolicy>> mReclaimPolicies;
169 };
170 
171 // ----------------------------------------------------------------------------
172 } // namespace android
173 
174 #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICENEW_H
175