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 updateResource(const ClientInfoParcel& clientInfo, 56 const std::vector<MediaResourceParcel>& resources) override; 57 58 Status removeResource(const ClientInfoParcel& clientInfo, 59 const std::vector<MediaResourceParcel>& resources) override; 60 61 Status removeClient(const ClientInfoParcel& clientInfo) override; 62 63 Status reclaimResource(const ClientInfoParcel& clientInfo, 64 const std::vector<MediaResourceParcel>& resources, 65 bool* _aidl_return) override; 66 67 Status overridePid(int32_t originalPid, int32_t newPid) override; 68 69 Status overrideProcessInfo(const std::shared_ptr<IResourceManagerClient>& client, 70 int32_t pid, int32_t procState, int32_t oomScore) override; 71 72 Status markClientForPendingRemoval(const ClientInfoParcel& clientInfo) override; 73 74 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override; 75 76 Status notifyClientCreated(const ClientInfoParcel& clientInfo) override; 77 78 Status notifyClientStarted(const ClientConfigParcel& clientConfig) override; 79 80 Status notifyClientStopped(const ClientConfigParcel& clientConfig) override; 81 82 Status notifyClientConfigChanged(const ClientConfigParcel& clientConfig) override; 83 84 Status getMediaResourceUsageReport(std::vector<MediaResourceParcel>* resources) override; 85 86 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; 87 88 friend class ResourceTracker; 89 90 private: 91 92 // Set up the Resource models. 93 void setUpResourceModels(); 94 95 // Set up the Reclaim Policies. 96 void setUpReclaimPolicies(); 97 98 // From the list of clients, pick/select client(s) based on the reclaim policy. 99 void getClientForResource_l( 100 const ReclaimRequestInfo& reclaimRequestInfo, 101 const std::vector<ClientInfo>& clients, 102 std::vector<ClientInfo>& targetClients); 103 104 // Initializes the internal state of the ResourceManagerService 105 void init() override; 106 107 void setObserverService( 108 const std::shared_ptr<ResourceObserverService>& observerService) override; 109 110 // Gets the list of all the clients who own the specified resource type. 111 // Returns false if any client belongs to a process with higher priority than the 112 // calling process. The clients will remain unchanged if returns false. 113 bool getTargetClients( 114 const ClientInfoParcel& clientInfo, 115 const std::vector<MediaResourceParcel>& resources, 116 std::vector<ClientInfo>& targetClients) override; 117 118 // Removes the pid from the override map. 119 void removeProcessInfoOverride(int pid) override; 120 121 // override the pid of given process 122 bool overridePid_l(int32_t originalPid, int32_t newPid) override; 123 124 // override the process info of given process 125 bool overrideProcessInfo_l(const std::shared_ptr<IResourceManagerClient>& client, 126 int pid, int procState, int oomScore) override; 127 128 // Get priority from process's pid 129 bool getPriority_l(int pid, int* priority) const override; 130 131 // Get the client for given pid and the clientId from the map 132 std::shared_ptr<IResourceManagerClient> getClient_l( 133 int pid, const int64_t& clientId) const override; 134 135 // Remove the client for given pid and the clientId from the map 136 bool removeClient_l(int pid, const int64_t& clientId) override; 137 138 // Get all the resource status for dump 139 void getResourceDump(std::string& resourceLog) const override; 140 141 // Returns a unmodifiable reference to the internal resource state as a map 142 const std::map<int, ResourceInfos>& getResourceMap() const override; 143 144 Status removeResource(const ClientInfoParcel& clientInfo, bool checkValid) override; 145 146 // The following utility functions are used only for testing by ResourceManagerServiceTest 147 // START: TEST only functions 148 // Gets the list of all the clients who own the specified resource type. 149 // Returns false if any client belongs to a process with higher priority than the 150 // calling process. The clients will remain unchanged if returns false. 151 bool getAllClients_l(const ResourceRequestInfo& resourceRequestInfo, 152 std::vector<ClientInfo>& clientsInfo) override; 153 154 // Gets the client who owns specified resource type from lowest possible priority process. 155 // Returns false if the calling process priority is not higher than the lowest process 156 // priority. The client will remain unchanged if returns false. 157 bool getLowestPriorityBiggestClient_l( 158 const ResourceRequestInfo& resourceRequestInfo, 159 ClientInfo& clientInfo) override; 160 161 // Gets lowest priority process that has the specified resource type. 162 // Returns false if failed. The output parameters will remain unchanged if failed. 163 bool getLowestPriorityPid_l(MediaResource::Type type, MediaResource::SubType subType, 164 int* lowestPriorityPid, int* lowestPriority) override; 165 166 // enable/disable process priority based reclaim and client importance based reclaim 167 void setReclaimPolicy(bool processPriority, bool clientImportance) override; 168 // END: TEST only functions 169 170 private: 171 std::shared_ptr<ResourceTracker> mResourceTracker; 172 std::unique_ptr<IResourceModel> mDefaultResourceModel; 173 std::vector<std::unique_ptr<IReclaimPolicy>> mReclaimPolicies; 174 }; 175 176 // ---------------------------------------------------------------------------- 177 } // namespace android 178 179 #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICENEW_H 180