1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker **
3*ec779b8eSAndroid Build Coastguard Worker ** Copyright 2023, The Android Open Source Project
4*ec779b8eSAndroid Build Coastguard Worker **
5*ec779b8eSAndroid Build Coastguard Worker ** Licensed under the Apache License, Version 2.0 (the "License");
6*ec779b8eSAndroid Build Coastguard Worker ** you may not use this file except in compliance with the License.
7*ec779b8eSAndroid Build Coastguard Worker ** You may obtain a copy of the License at
8*ec779b8eSAndroid Build Coastguard Worker **
9*ec779b8eSAndroid Build Coastguard Worker ** http://www.apache.org/licenses/LICENSE-2.0
10*ec779b8eSAndroid Build Coastguard Worker **
11*ec779b8eSAndroid Build Coastguard Worker ** Unless required by applicable law or agreed to in writing, software
12*ec779b8eSAndroid Build Coastguard Worker ** distributed under the License is distributed on an "AS IS" BASIS,
13*ec779b8eSAndroid Build Coastguard Worker ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*ec779b8eSAndroid Build Coastguard Worker ** See the License for the specific language governing permissions and
15*ec779b8eSAndroid Build Coastguard Worker ** limitations under the License.
16*ec779b8eSAndroid Build Coastguard Worker */
17*ec779b8eSAndroid Build Coastguard Worker
18*ec779b8eSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
19*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "ClientImportanceReclaimPolicy"
20*ec779b8eSAndroid Build Coastguard Worker #include <utils/Log.h>
21*ec779b8eSAndroid Build Coastguard Worker
22*ec779b8eSAndroid Build Coastguard Worker #include "ResourceTracker.h"
23*ec779b8eSAndroid Build Coastguard Worker #include "ResourceManagerService.h"
24*ec779b8eSAndroid Build Coastguard Worker #include "ClientImportanceReclaimPolicy.h"
25*ec779b8eSAndroid Build Coastguard Worker
26*ec779b8eSAndroid Build Coastguard Worker namespace android {
27*ec779b8eSAndroid Build Coastguard Worker
28*ec779b8eSAndroid Build Coastguard Worker using aidl::android::media::IResourceManagerClient;
29*ec779b8eSAndroid Build Coastguard Worker
ClientImportanceReclaimPolicy(const std::shared_ptr<ResourceTracker> & resourceTracker)30*ec779b8eSAndroid Build Coastguard Worker ClientImportanceReclaimPolicy::ClientImportanceReclaimPolicy(
31*ec779b8eSAndroid Build Coastguard Worker const std::shared_ptr<ResourceTracker>& resourceTracker)
32*ec779b8eSAndroid Build Coastguard Worker : mResourceTracker(resourceTracker) {
33*ec779b8eSAndroid Build Coastguard Worker }
34*ec779b8eSAndroid Build Coastguard Worker
~ClientImportanceReclaimPolicy()35*ec779b8eSAndroid Build Coastguard Worker ClientImportanceReclaimPolicy::~ClientImportanceReclaimPolicy() {
36*ec779b8eSAndroid Build Coastguard Worker }
37*ec779b8eSAndroid Build Coastguard Worker
38*ec779b8eSAndroid Build Coastguard Worker // Find the biggest client from the same process with the lowest importance
39*ec779b8eSAndroid Build Coastguard Worker // than that of the requesting client.
getClients(const ReclaimRequestInfo & reclaimRequestInfo,const std::vector<ClientInfo> & clients,std::vector<ClientInfo> & targetClients)40*ec779b8eSAndroid Build Coastguard Worker bool ClientImportanceReclaimPolicy::getClients(const ReclaimRequestInfo& reclaimRequestInfo,
41*ec779b8eSAndroid Build Coastguard Worker const std::vector<ClientInfo>& clients,
42*ec779b8eSAndroid Build Coastguard Worker std::vector<ClientInfo>& targetClients) {
43*ec779b8eSAndroid Build Coastguard Worker pid_t callingPid = reclaimRequestInfo.mCallingPid;
44*ec779b8eSAndroid Build Coastguard Worker int32_t callingImportance = reclaimRequestInfo.mCallingClientImportance;
45*ec779b8eSAndroid Build Coastguard Worker MediaResource::Type type = reclaimRequestInfo.mResources[0].type;
46*ec779b8eSAndroid Build Coastguard Worker MediaResource::SubType subType = reclaimRequestInfo.mResources[0].subType;
47*ec779b8eSAndroid Build Coastguard Worker ClientInfo targetClient;
48*ec779b8eSAndroid Build Coastguard Worker // Look to find the biggest client with lowest importance from the same process that
49*ec779b8eSAndroid Build Coastguard Worker // has the other resources and with the given primary type.
50*ec779b8eSAndroid Build Coastguard Worker bool found = false;
51*ec779b8eSAndroid Build Coastguard Worker MediaResource::SubType primarySubType = subType;
52*ec779b8eSAndroid Build Coastguard Worker for (size_t index = 1; !found && (index < reclaimRequestInfo.mResources.size()); index++) {
53*ec779b8eSAndroid Build Coastguard Worker MediaResource::Type type = reclaimRequestInfo.mResources[index].type;
54*ec779b8eSAndroid Build Coastguard Worker MediaResource::SubType subType = reclaimRequestInfo.mResources[index].subType;
55*ec779b8eSAndroid Build Coastguard Worker found = mResourceTracker->getLeastImportantBiggestClient(
56*ec779b8eSAndroid Build Coastguard Worker callingPid, callingImportance,
57*ec779b8eSAndroid Build Coastguard Worker type, subType, primarySubType,
58*ec779b8eSAndroid Build Coastguard Worker clients, targetClient);
59*ec779b8eSAndroid Build Coastguard Worker }
60*ec779b8eSAndroid Build Coastguard Worker // If no success, then select the biggest client of primary type with lowest importance
61*ec779b8eSAndroid Build Coastguard Worker // from the same process.
62*ec779b8eSAndroid Build Coastguard Worker if (!found) {
63*ec779b8eSAndroid Build Coastguard Worker found = mResourceTracker->getLeastImportantBiggestClient(
64*ec779b8eSAndroid Build Coastguard Worker callingPid, callingImportance,
65*ec779b8eSAndroid Build Coastguard Worker type, subType, MediaResource::SubType::kUnspecifiedSubType,
66*ec779b8eSAndroid Build Coastguard Worker clients, targetClient);
67*ec779b8eSAndroid Build Coastguard Worker }
68*ec779b8eSAndroid Build Coastguard Worker // If we haven't found a client yet, then select the biggest client of different type
69*ec779b8eSAndroid Build Coastguard Worker // with lowest importance from the same process.
70*ec779b8eSAndroid Build Coastguard Worker // This is applicable for codec type only.
71*ec779b8eSAndroid Build Coastguard Worker if (!found) {
72*ec779b8eSAndroid Build Coastguard Worker if (type != MediaResource::Type::kSecureCodec &&
73*ec779b8eSAndroid Build Coastguard Worker type != MediaResource::Type::kNonSecureCodec) {
74*ec779b8eSAndroid Build Coastguard Worker return false;
75*ec779b8eSAndroid Build Coastguard Worker }
76*ec779b8eSAndroid Build Coastguard Worker MediaResourceType otherType = (type == MediaResource::Type::kSecureCodec) ?
77*ec779b8eSAndroid Build Coastguard Worker MediaResource::Type::kNonSecureCodec : MediaResource::Type::kSecureCodec;
78*ec779b8eSAndroid Build Coastguard Worker if (!mResourceTracker->getLeastImportantBiggestClient(
79*ec779b8eSAndroid Build Coastguard Worker callingPid, callingImportance,
80*ec779b8eSAndroid Build Coastguard Worker otherType, subType, MediaResource::SubType::kUnspecifiedSubType,
81*ec779b8eSAndroid Build Coastguard Worker clients, targetClient)) {
82*ec779b8eSAndroid Build Coastguard Worker return false;
83*ec779b8eSAndroid Build Coastguard Worker }
84*ec779b8eSAndroid Build Coastguard Worker }
85*ec779b8eSAndroid Build Coastguard Worker targetClients.emplace_back(targetClient);
86*ec779b8eSAndroid Build Coastguard Worker return true;
87*ec779b8eSAndroid Build Coastguard Worker }
88*ec779b8eSAndroid Build Coastguard Worker } // namespace android
89