Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_SERVICE_UTILS_EVICTION_POLICY_MANAGER_H |
| 18 | #define ANDROID_SERVICE_UTILS_EVICTION_POLICY_MANAGER_H |
| 19 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 20 | #include <utils/Condition.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 21 | #include <utils/Mutex.h> |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 22 | #include <utils/Timers.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | #include <set> |
| 28 | #include <map> |
| 29 | #include <memory> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace resource_policy { |
| 33 | |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame^] | 34 | // Values from frameworks/base/services/core/java/com/android/server/am/ProcessList.java |
| 35 | const int32_t INVALID_ADJ = -10000; |
| 36 | const int32_t UNKNOWN_ADJ = 1001; |
| 37 | const int32_t CACHED_APP_MAX_ADJ = 999; |
| 38 | const int32_t CACHED_APP_MIN_ADJ = 900; |
| 39 | const int32_t CACHED_APP_LMK_FIRST_ADJ = 950; |
| 40 | const int32_t CACHED_APP_IMPORTANCE_LEVELS = 5; |
| 41 | const int32_t SERVICE_B_ADJ = 800; |
| 42 | const int32_t PREVIOUS_APP_ADJ = 700; |
| 43 | const int32_t HOME_APP_ADJ = 600; |
| 44 | const int32_t SERVICE_ADJ = 500; |
| 45 | const int32_t HEAVY_WEIGHT_APP_ADJ = 400; |
| 46 | const int32_t BACKUP_APP_ADJ = 300; |
| 47 | const int32_t PERCEPTIBLE_LOW_APP_ADJ = 250; |
| 48 | const int32_t PERCEPTIBLE_MEDIUM_APP_ADJ = 225; |
| 49 | const int32_t PERCEPTIBLE_APP_ADJ = 200; |
| 50 | const int32_t VISIBLE_APP_ADJ = 100; |
| 51 | const int32_t VISIBLE_APP_LAYER_MAX = PERCEPTIBLE_APP_ADJ - VISIBLE_APP_ADJ - 1; |
| 52 | const int32_t PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ = 50; |
| 53 | const int32_t FOREGROUND_APP_ADJ = 0; |
| 54 | const int32_t PERSISTENT_SERVICE_ADJ = -700; |
| 55 | const int32_t PERSISTENT_PROC_ADJ = -800; |
| 56 | const int32_t SYSTEM_ADJ = -900; |
| 57 | const int32_t NATIVE_ADJ = -1000; |
| 58 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 59 | class ClientPriority { |
| 60 | public: |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Choosing to set mIsVendorClient through a parameter instead of calling |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 63 | * getCurrentServingCall() == BinderCallType::HWBINDER to protect against the |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 64 | * case where the construction is offloaded to another thread which isn't a |
| 65 | * hwbinder thread. |
| 66 | */ |
| 67 | ClientPriority(int32_t score, int32_t state, bool isVendorClient) : |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame^] | 68 | mScore((score == INVALID_ADJ) ? UNKNOWN_ADJ : score), |
| 69 | mState(state), |
| 70 | mIsVendorClient(isVendorClient) { } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 71 | |
| 72 | int32_t getScore() const { return mScore; } |
| 73 | int32_t getState() const { return mState; } |
| 74 | |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 75 | void setScore(int32_t score) { |
| 76 | // For vendor clients, the score is set once and for all during |
| 77 | // construction. Otherwise, it can get reset each time cameraserver |
| 78 | // queries ActivityManagerService for oom_adj scores / states . |
| 79 | if (!mIsVendorClient) { |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame^] | 80 | mScore = (score == INVALID_ADJ) ? UNKNOWN_ADJ : score; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void setState(int32_t state) { |
| 85 | // For vendor clients, the score is set once and for all during |
| 86 | // construction. Otherwise, it can get reset each time cameraserver |
| 87 | // queries ActivityManagerService for oom_adj scores / states |
| 88 | // (ActivityManagerService returns a vendor process' state as |
| 89 | // PROCESS_STATE_NONEXISTENT. |
| 90 | if (!mIsVendorClient) { |
| 91 | mState = state; |
| 92 | } |
| 93 | } |
| 94 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 95 | bool operator==(const ClientPriority& rhs) const { |
| 96 | return (this->mScore == rhs.mScore) && (this->mState == rhs.mState); |
| 97 | } |
| 98 | |
| 99 | bool operator< (const ClientPriority& rhs) const { |
| 100 | if (this->mScore == rhs.mScore) { |
| 101 | return this->mState < rhs.mState; |
| 102 | } else { |
| 103 | return this->mScore < rhs.mScore; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool operator> (const ClientPriority& rhs) const { |
| 108 | return rhs < *this; |
| 109 | } |
| 110 | |
| 111 | bool operator<=(const ClientPriority& rhs) const { |
| 112 | return !(*this > rhs); |
| 113 | } |
| 114 | |
| 115 | bool operator>=(const ClientPriority& rhs) const { |
| 116 | return !(*this < rhs); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | int32_t mScore; |
| 121 | int32_t mState; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 122 | bool mIsVendorClient = false; |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 125 | // -------------------------------------------------------------------------------- |
| 126 | |
| 127 | /** |
| 128 | * The ClientDescriptor class is a container for a given key/value pair identifying a shared |
| 129 | * resource, and the corresponding cost, priority, owner ID, and conflicting keys list used |
| 130 | * in determining eviction behavior. |
| 131 | * |
| 132 | * Aside from the priority, these values are immutable once the ClientDescriptor has been |
| 133 | * constructed. |
| 134 | */ |
| 135 | template<class KEY, class VALUE> |
| 136 | class ClientDescriptor final { |
| 137 | public: |
| 138 | ClientDescriptor(const KEY& key, const VALUE& value, int32_t cost, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 139 | const std::set<KEY>& conflictingKeys, int32_t score, int32_t ownerId, int32_t state, |
| 140 | bool isVendorClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 141 | ClientDescriptor(KEY&& key, VALUE&& value, int32_t cost, std::set<KEY>&& conflictingKeys, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 142 | int32_t score, int32_t ownerId, int32_t state, bool isVendorClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 143 | |
| 144 | ~ClientDescriptor(); |
| 145 | |
| 146 | /** |
| 147 | * Return the key for this descriptor. |
| 148 | */ |
| 149 | const KEY& getKey() const; |
| 150 | |
| 151 | /** |
| 152 | * Return the value for this descriptor. |
| 153 | */ |
| 154 | const VALUE& getValue() const; |
| 155 | |
| 156 | /** |
| 157 | * Return the cost for this descriptor. |
| 158 | */ |
| 159 | int32_t getCost() const; |
| 160 | |
| 161 | /** |
| 162 | * Return the priority for this descriptor. |
| 163 | */ |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 164 | const ClientPriority &getPriority() const; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Return the owner ID for this descriptor. |
| 168 | */ |
| 169 | int32_t getOwnerId() const; |
| 170 | |
| 171 | /** |
| 172 | * Return true if the given key is in this descriptor's conflicting keys list. |
| 173 | */ |
| 174 | bool isConflicting(const KEY& key) const; |
| 175 | |
| 176 | /** |
| 177 | * Return the set of all conflicting keys for this descriptor. |
| 178 | */ |
| 179 | std::set<KEY> getConflicting() const; |
| 180 | |
| 181 | /** |
| 182 | * Set the proirity for this descriptor. |
| 183 | */ |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 184 | void setPriority(const ClientPriority& priority); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 185 | |
| 186 | // This class is ordered by key |
| 187 | template<class K, class V> |
| 188 | friend bool operator < (const ClientDescriptor<K, V>& a, const ClientDescriptor<K, V>& b); |
| 189 | |
| 190 | private: |
| 191 | KEY mKey; |
| 192 | VALUE mValue; |
| 193 | int32_t mCost; |
| 194 | std::set<KEY> mConflicting; |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 195 | ClientPriority mPriority; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 196 | int32_t mOwnerId; |
| 197 | }; // class ClientDescriptor |
| 198 | |
| 199 | template<class K, class V> |
| 200 | bool operator < (const ClientDescriptor<K, V>& a, const ClientDescriptor<K, V>& b) { |
| 201 | return a.mKey < b.mKey; |
| 202 | } |
| 203 | |
| 204 | template<class KEY, class VALUE> |
| 205 | ClientDescriptor<KEY, VALUE>::ClientDescriptor(const KEY& key, const VALUE& value, int32_t cost, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 206 | const std::set<KEY>& conflictingKeys, int32_t score, int32_t ownerId, int32_t state, |
| 207 | bool isVendorClient) : |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 208 | mKey{key}, mValue{value}, mCost{cost}, mConflicting{conflictingKeys}, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 209 | mPriority(score, state, isVendorClient), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 210 | mOwnerId{ownerId} {} |
| 211 | |
| 212 | template<class KEY, class VALUE> |
| 213 | ClientDescriptor<KEY, VALUE>::ClientDescriptor(KEY&& key, VALUE&& value, int32_t cost, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 214 | std::set<KEY>&& conflictingKeys, int32_t score, int32_t ownerId, int32_t state, |
| 215 | bool isVendorClient) : |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 216 | mKey{std::forward<KEY>(key)}, mValue{std::forward<VALUE>(value)}, mCost{cost}, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 217 | mConflicting{std::forward<std::set<KEY>>(conflictingKeys)}, |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 218 | mPriority(score, state, isVendorClient), mOwnerId{ownerId} {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 219 | |
| 220 | template<class KEY, class VALUE> |
| 221 | ClientDescriptor<KEY, VALUE>::~ClientDescriptor() {} |
| 222 | |
| 223 | template<class KEY, class VALUE> |
| 224 | const KEY& ClientDescriptor<KEY, VALUE>::getKey() const { |
| 225 | return mKey; |
| 226 | } |
| 227 | |
| 228 | template<class KEY, class VALUE> |
| 229 | const VALUE& ClientDescriptor<KEY, VALUE>::getValue() const { |
| 230 | return mValue; |
| 231 | } |
| 232 | |
| 233 | template<class KEY, class VALUE> |
| 234 | int32_t ClientDescriptor<KEY, VALUE>::getCost() const { |
| 235 | return mCost; |
| 236 | } |
| 237 | |
| 238 | template<class KEY, class VALUE> |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 239 | const ClientPriority& ClientDescriptor<KEY, VALUE>::getPriority() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 240 | return mPriority; |
| 241 | } |
| 242 | |
| 243 | template<class KEY, class VALUE> |
| 244 | int32_t ClientDescriptor<KEY, VALUE>::getOwnerId() const { |
| 245 | return mOwnerId; |
| 246 | } |
| 247 | |
| 248 | template<class KEY, class VALUE> |
| 249 | bool ClientDescriptor<KEY, VALUE>::isConflicting(const KEY& key) const { |
| 250 | if (key == mKey) return true; |
| 251 | for (const auto& x : mConflicting) { |
| 252 | if (key == x) return true; |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | template<class KEY, class VALUE> |
| 258 | std::set<KEY> ClientDescriptor<KEY, VALUE>::getConflicting() const { |
| 259 | return mConflicting; |
| 260 | } |
| 261 | |
| 262 | template<class KEY, class VALUE> |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 263 | void ClientDescriptor<KEY, VALUE>::setPriority(const ClientPriority& priority) { |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 264 | // We don't use the usual copy constructor here since we want to remember |
| 265 | // whether a client is a vendor client or not. This could have been wiped |
| 266 | // off in the incoming priority argument since an AIDL thread might have |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 267 | // called getCurrentServingCall() == BinderCallType::HWBINDER after refreshing |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 268 | // priorities for old clients through ProcessInfoService::getProcessStatesScoresFromPids(). |
| 269 | mPriority.setScore(priority.getScore()); |
| 270 | mPriority.setState(priority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // -------------------------------------------------------------------------------- |
| 274 | |
| 275 | /** |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 276 | * A default class implementing the LISTENER interface used by ClientManager. |
| 277 | */ |
| 278 | template<class KEY, class VALUE> |
| 279 | class DefaultEventListener { |
| 280 | public: |
| 281 | void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor); |
| 282 | void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor); |
| 283 | }; |
| 284 | |
| 285 | template<class KEY, class VALUE> |
| 286 | void DefaultEventListener<KEY, VALUE>::onClientAdded( |
| 287 | const ClientDescriptor<KEY, VALUE>& /*descriptor*/) {} |
| 288 | |
| 289 | template<class KEY, class VALUE> |
| 290 | void DefaultEventListener<KEY, VALUE>::onClientRemoved( |
| 291 | const ClientDescriptor<KEY, VALUE>& /*descriptor*/) {} |
| 292 | |
| 293 | // -------------------------------------------------------------------------------- |
| 294 | |
| 295 | /** |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 296 | * The ClientManager class wraps an LRU-ordered list of active clients and implements eviction |
| 297 | * behavior for handling shared resource access. |
| 298 | * |
| 299 | * When adding a new descriptor, eviction behavior is as follows: |
| 300 | * - Keys are unique, adding a descriptor with the same key as an existing descriptor will |
| 301 | * result in the lower-priority of the two being removed. Priority ties result in the |
| 302 | * LRU descriptor being evicted (this means the incoming descriptor be added in this case). |
| 303 | * - Any descriptors with keys that are in the incoming descriptor's 'conflicting keys' list |
| 304 | * will be removed if they have an equal or lower priority than the incoming descriptor; |
| 305 | * if any have a higher priority, the incoming descriptor is removed instead. |
| 306 | * - If the sum of all descriptors' costs, including the incoming descriptor's, is more than |
| 307 | * the max cost allowed for this ClientManager, descriptors with non-zero cost, equal or lower |
| 308 | * priority, and a different owner will be evicted in LRU order until either the cost is less |
| 309 | * than the max cost, or all descriptors meeting this criteria have been evicted and the |
| 310 | * incoming descriptor has the highest priority. Otherwise, the incoming descriptor is |
| 311 | * removed instead. |
| 312 | */ |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 313 | template<class KEY, class VALUE, class LISTENER=DefaultEventListener<KEY, VALUE>> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 314 | class ClientManager { |
| 315 | public: |
| 316 | // The default maximum "cost" allowed before evicting |
| 317 | static constexpr int32_t DEFAULT_MAX_COST = 100; |
| 318 | |
| 319 | ClientManager(); |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 320 | explicit ClientManager(int32_t totalCost); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * Add a given ClientDescriptor to the managed list. ClientDescriptors for clients that |
| 324 | * are evicted by this action are returned in a vector. |
| 325 | * |
| 326 | * This may return the ClientDescriptor passed in if it would be evicted. |
| 327 | */ |
| 328 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> addAndEvict( |
| 329 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client); |
| 330 | |
| 331 | /** |
| 332 | * Given a map containing owner (pid) -> priority mappings, update the priority of each |
| 333 | * ClientDescriptor with an owner in this mapping. |
| 334 | */ |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 335 | void updatePriorities(const std::map<int32_t,ClientPriority>& ownerPriorityList); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * Remove all ClientDescriptors. |
| 339 | */ |
| 340 | void removeAll(); |
| 341 | |
| 342 | /** |
| 343 | * Remove and return the ClientDescriptor with a given key. |
| 344 | */ |
| 345 | std::shared_ptr<ClientDescriptor<KEY, VALUE>> remove(const KEY& key); |
| 346 | |
| 347 | /** |
| 348 | * Remove the given ClientDescriptor. |
| 349 | */ |
| 350 | void remove(const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& value); |
| 351 | |
| 352 | /** |
| 353 | * Return a vector of the ClientDescriptors that would be evicted by adding the given |
| 354 | * ClientDescriptor. |
| 355 | * |
| 356 | * This may return the ClientDescriptor passed in if it would be evicted. |
| 357 | */ |
| 358 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> wouldEvict( |
| 359 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const; |
| 360 | |
| 361 | /** |
| 362 | * Return a vector of active ClientDescriptors that prevent this client from being added. |
| 363 | */ |
| 364 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> getIncompatibleClients( |
| 365 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const; |
| 366 | |
| 367 | /** |
| 368 | * Return a vector containing all currently active ClientDescriptors. |
| 369 | */ |
| 370 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> getAll() const; |
| 371 | |
| 372 | /** |
| 373 | * Return a vector containing all keys of currently active ClientDescriptors. |
| 374 | */ |
| 375 | std::vector<KEY> getAllKeys() const; |
| 376 | |
| 377 | /** |
| 378 | * Return a vector of the owner tags of all currently active ClientDescriptors (duplicates |
| 379 | * will be removed). |
| 380 | */ |
| 381 | std::vector<int32_t> getAllOwners() const; |
| 382 | |
| 383 | /** |
| 384 | * Return the ClientDescriptor corresponding to the given key, or an empty shared pointer |
| 385 | * if none exists. |
| 386 | */ |
| 387 | std::shared_ptr<ClientDescriptor<KEY, VALUE>> get(const KEY& key) const; |
| 388 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Block until the given client is no longer in the active clients list, or the timeout |
| 391 | * occurred. |
| 392 | * |
| 393 | * Returns NO_ERROR if this succeeded, -ETIMEDOUT on a timeout, or a negative error code on |
| 394 | * failure. |
| 395 | */ |
| 396 | status_t waitUntilRemoved(const std::shared_ptr<ClientDescriptor<KEY, VALUE>> client, |
| 397 | nsecs_t timeout) const; |
| 398 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 399 | /** |
| 400 | * Set the current listener for client add/remove events. |
| 401 | * |
| 402 | * The listener instance must inherit from the LISTENER class and implement the following |
| 403 | * methods: |
| 404 | * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor); |
| 405 | * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor); |
| 406 | * |
| 407 | * These callback methods will be called with the ClientManager's lock held, and should |
| 408 | * not call any further ClientManager methods. |
| 409 | * |
| 410 | * The onClientRemoved method will be called when the client has been removed or evicted |
| 411 | * from the ClientManager that this event listener has been added to. The onClientAdded |
| 412 | * method will be called when the client has been added to the ClientManager that this |
| 413 | * event listener has been added to. |
| 414 | */ |
| 415 | void setListener(const std::shared_ptr<LISTENER>& listener); |
| 416 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 417 | protected: |
| 418 | ~ClientManager(); |
| 419 | |
| 420 | private: |
| 421 | |
| 422 | /** |
| 423 | * Return a vector of the ClientDescriptors that would be evicted by adding the given |
| 424 | * ClientDescriptor. If returnIncompatibleClients is set to true, instead, return the |
| 425 | * vector of ClientDescriptors that are higher priority than the incoming client and |
| 426 | * either conflict with this client, or contribute to the resource cost if that would |
| 427 | * prevent the incoming client from being added. |
| 428 | * |
| 429 | * This may return the ClientDescriptor passed in. |
| 430 | */ |
| 431 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> wouldEvictLocked( |
| 432 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client, |
| 433 | bool returnIncompatibleClients = false) const; |
| 434 | |
| 435 | int64_t getCurrentCostLocked() const; |
| 436 | |
| 437 | mutable Mutex mLock; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 438 | mutable Condition mRemovedCondition; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 439 | int32_t mMaxCost; |
| 440 | // LRU ordered, most recent at end |
| 441 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> mClients; |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 442 | std::shared_ptr<LISTENER> mListener; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 443 | }; // class ClientManager |
| 444 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 445 | template<class KEY, class VALUE, class LISTENER> |
| 446 | ClientManager<KEY, VALUE, LISTENER>::ClientManager() : |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 447 | ClientManager(DEFAULT_MAX_COST) {} |
| 448 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 449 | template<class KEY, class VALUE, class LISTENER> |
| 450 | ClientManager<KEY, VALUE, LISTENER>::ClientManager(int32_t totalCost) : mMaxCost(totalCost) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 451 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 452 | template<class KEY, class VALUE, class LISTENER> |
| 453 | ClientManager<KEY, VALUE, LISTENER>::~ClientManager() {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 454 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 455 | template<class KEY, class VALUE, class LISTENER> |
| 456 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> |
| 457 | ClientManager<KEY, VALUE, LISTENER>::wouldEvict( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 458 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const { |
| 459 | Mutex::Autolock lock(mLock); |
| 460 | return wouldEvictLocked(client); |
| 461 | } |
| 462 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 463 | template<class KEY, class VALUE, class LISTENER> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 464 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 465 | ClientManager<KEY, VALUE, LISTENER>::getIncompatibleClients( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 466 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const { |
| 467 | Mutex::Autolock lock(mLock); |
| 468 | return wouldEvictLocked(client, /*returnIncompatibleClients*/true); |
| 469 | } |
| 470 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 471 | template<class KEY, class VALUE, class LISTENER> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 472 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 473 | ClientManager<KEY, VALUE, LISTENER>::wouldEvictLocked( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 474 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client, |
| 475 | bool returnIncompatibleClients) const { |
| 476 | |
| 477 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> evictList; |
| 478 | |
| 479 | // Disallow null clients, return input |
| 480 | if (client == nullptr) { |
| 481 | evictList.push_back(client); |
| 482 | return evictList; |
| 483 | } |
| 484 | |
| 485 | const KEY& key = client->getKey(); |
| 486 | int32_t cost = client->getCost(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 487 | ClientPriority priority = client->getPriority(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 488 | int32_t owner = client->getOwnerId(); |
| 489 | |
| 490 | int64_t totalCost = getCurrentCostLocked() + cost; |
| 491 | |
| 492 | // Determine the MRU of the owners tied for having the highest priority |
| 493 | int32_t highestPriorityOwner = owner; |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 494 | ClientPriority highestPriority = priority; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 495 | for (const auto& i : mClients) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 496 | ClientPriority curPriority = i->getPriority(); |
| 497 | if (curPriority <= highestPriority) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 498 | highestPriority = curPriority; |
| 499 | highestPriorityOwner = i->getOwnerId(); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (highestPriority == priority) { |
| 504 | // Switch back owner if the incoming client has the highest priority, as it is MRU |
| 505 | highestPriorityOwner = owner; |
| 506 | } |
| 507 | |
| 508 | // Build eviction list of clients to remove |
| 509 | for (const auto& i : mClients) { |
| 510 | const KEY& curKey = i->getKey(); |
| 511 | int32_t curCost = i->getCost(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 512 | ClientPriority curPriority = i->getPriority(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 513 | int32_t curOwner = i->getOwnerId(); |
| 514 | |
| 515 | bool conflicting = (curKey == key || i->isConflicting(key) || |
| 516 | client->isConflicting(curKey)); |
| 517 | |
| 518 | if (!returnIncompatibleClients) { |
| 519 | // Find evicted clients |
| 520 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 521 | if (conflicting && curPriority < priority) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 522 | // Pre-existing conflicting client with higher priority exists |
| 523 | evictList.clear(); |
| 524 | evictList.push_back(client); |
| 525 | return evictList; |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 526 | } else if (conflicting && owner == curOwner) { |
| 527 | // Pre-existing conflicting client with the same client owner exists |
| 528 | // Open the same device twice -> most recent open wins |
| 529 | // Otherwise let the existing client wins to avoid behaviors difference |
| 530 | // due to how HAL advertising conflicting devices (which is hidden from |
| 531 | // application) |
| 532 | if (curKey == key) { |
| 533 | evictList.push_back(i); |
| 534 | totalCost -= curCost; |
| 535 | } else { |
| 536 | evictList.clear(); |
| 537 | evictList.push_back(client); |
| 538 | return evictList; |
| 539 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 540 | } else if (conflicting || ((totalCost > mMaxCost && curCost > 0) && |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 541 | (curPriority >= priority) && |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 542 | !(highestPriorityOwner == owner && owner == curOwner))) { |
| 543 | // Add a pre-existing client to the eviction list if: |
| 544 | // - We are adding a client with higher priority that conflicts with this one. |
| 545 | // - The total cost including the incoming client's is more than the allowable |
| 546 | // maximum, and the client has a non-zero cost, lower priority, and a different |
| 547 | // owner than the incoming client when the incoming client has the |
| 548 | // highest priority. |
| 549 | evictList.push_back(i); |
| 550 | totalCost -= curCost; |
| 551 | } |
| 552 | } else { |
| 553 | // Find clients preventing the incoming client from being added |
| 554 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 555 | if (curPriority < priority && (conflicting || (totalCost > mMaxCost && curCost > 0))) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 556 | // Pre-existing conflicting client with higher priority exists |
| 557 | evictList.push_back(i); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // Immediately return the incompatible clients if we are calculating these instead |
| 563 | if (returnIncompatibleClients) { |
| 564 | return evictList; |
| 565 | } |
| 566 | |
| 567 | // If the total cost is too high, return the input unless the input has the highest priority |
| 568 | if (totalCost > mMaxCost && highestPriorityOwner != owner) { |
| 569 | evictList.clear(); |
| 570 | evictList.push_back(client); |
| 571 | return evictList; |
| 572 | } |
| 573 | |
| 574 | return evictList; |
| 575 | |
| 576 | } |
| 577 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 578 | template<class KEY, class VALUE, class LISTENER> |
| 579 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> |
| 580 | ClientManager<KEY, VALUE, LISTENER>::addAndEvict( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 581 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) { |
| 582 | Mutex::Autolock lock(mLock); |
| 583 | auto evicted = wouldEvictLocked(client); |
| 584 | auto it = evicted.begin(); |
| 585 | if (it != evicted.end() && *it == client) { |
| 586 | return evicted; |
| 587 | } |
| 588 | |
| 589 | auto iter = evicted.cbegin(); |
| 590 | |
Ruben Brunk | 8050721 | 2015-05-14 13:50:57 -0700 | [diff] [blame] | 591 | if (iter != evicted.cend()) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 592 | |
| 593 | if (mListener != nullptr) mListener->onClientRemoved(**iter); |
| 594 | |
Ruben Brunk | 8050721 | 2015-05-14 13:50:57 -0700 | [diff] [blame] | 595 | // Remove evicted clients from list |
| 596 | mClients.erase(std::remove_if(mClients.begin(), mClients.end(), |
| 597 | [&iter] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) { |
| 598 | if (curClientPtr->getKey() == (*iter)->getKey()) { |
| 599 | iter++; |
| 600 | return true; |
| 601 | } |
| 602 | return false; |
| 603 | }), mClients.end()); |
| 604 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 605 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 606 | if (mListener != nullptr) mListener->onClientAdded(*client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 607 | mClients.push_back(client); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 608 | mRemovedCondition.broadcast(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 609 | |
| 610 | return evicted; |
| 611 | } |
| 612 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 613 | template<class KEY, class VALUE, class LISTENER> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 614 | std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 615 | ClientManager<KEY, VALUE, LISTENER>::getAll() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 616 | Mutex::Autolock lock(mLock); |
| 617 | return mClients; |
| 618 | } |
| 619 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 620 | template<class KEY, class VALUE, class LISTENER> |
| 621 | std::vector<KEY> ClientManager<KEY, VALUE, LISTENER>::getAllKeys() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 622 | Mutex::Autolock lock(mLock); |
| 623 | std::vector<KEY> keys(mClients.size()); |
| 624 | for (const auto& i : mClients) { |
| 625 | keys.push_back(i->getKey()); |
| 626 | } |
| 627 | return keys; |
| 628 | } |
| 629 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 630 | template<class KEY, class VALUE, class LISTENER> |
| 631 | std::vector<int32_t> ClientManager<KEY, VALUE, LISTENER>::getAllOwners() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 632 | Mutex::Autolock lock(mLock); |
| 633 | std::set<int32_t> owners; |
| 634 | for (const auto& i : mClients) { |
| 635 | owners.emplace(i->getOwnerId()); |
| 636 | } |
| 637 | return std::vector<int32_t>(owners.begin(), owners.end()); |
| 638 | } |
| 639 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 640 | template<class KEY, class VALUE, class LISTENER> |
| 641 | void ClientManager<KEY, VALUE, LISTENER>::updatePriorities( |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 642 | const std::map<int32_t,ClientPriority>& ownerPriorityList) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 643 | Mutex::Autolock lock(mLock); |
| 644 | for (auto& i : mClients) { |
| 645 | auto j = ownerPriorityList.find(i->getOwnerId()); |
| 646 | if (j != ownerPriorityList.end()) { |
| 647 | i->setPriority(j->second); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 652 | template<class KEY, class VALUE, class LISTENER> |
| 653 | std::shared_ptr<ClientDescriptor<KEY, VALUE>> ClientManager<KEY, VALUE, LISTENER>::get( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 654 | const KEY& key) const { |
| 655 | Mutex::Autolock lock(mLock); |
| 656 | for (const auto& i : mClients) { |
| 657 | if (i->getKey() == key) return i; |
| 658 | } |
| 659 | return std::shared_ptr<ClientDescriptor<KEY, VALUE>>(nullptr); |
| 660 | } |
| 661 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 662 | template<class KEY, class VALUE, class LISTENER> |
| 663 | void ClientManager<KEY, VALUE, LISTENER>::removeAll() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 664 | Mutex::Autolock lock(mLock); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 665 | if (mListener != nullptr) { |
| 666 | for (const auto& i : mClients) { |
| 667 | mListener->onClientRemoved(*i); |
| 668 | } |
| 669 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 670 | mClients.clear(); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 671 | mRemovedCondition.broadcast(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 674 | template<class KEY, class VALUE, class LISTENER> |
| 675 | std::shared_ptr<ClientDescriptor<KEY, VALUE>> ClientManager<KEY, VALUE, LISTENER>::remove( |
| 676 | const KEY& key) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 677 | Mutex::Autolock lock(mLock); |
| 678 | |
| 679 | std::shared_ptr<ClientDescriptor<KEY, VALUE>> ret; |
| 680 | |
| 681 | // Remove evicted clients from list |
| 682 | mClients.erase(std::remove_if(mClients.begin(), mClients.end(), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 683 | [this, &key, &ret] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 684 | if (curClientPtr->getKey() == key) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 685 | if (mListener != nullptr) mListener->onClientRemoved(*curClientPtr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 686 | ret = curClientPtr; |
| 687 | return true; |
| 688 | } |
| 689 | return false; |
| 690 | }), mClients.end()); |
| 691 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 692 | mRemovedCondition.broadcast(); |
| 693 | return ret; |
| 694 | } |
| 695 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 696 | template<class KEY, class VALUE, class LISTENER> |
| 697 | status_t ClientManager<KEY, VALUE, LISTENER>::waitUntilRemoved( |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 698 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>> client, |
| 699 | nsecs_t timeout) const { |
| 700 | status_t ret = NO_ERROR; |
| 701 | Mutex::Autolock lock(mLock); |
| 702 | |
| 703 | bool isRemoved = false; |
| 704 | |
| 705 | // Figure out what time in the future we should hit the timeout |
| 706 | nsecs_t failTime = systemTime(SYSTEM_TIME_MONOTONIC) + timeout; |
| 707 | |
| 708 | while (!isRemoved) { |
| 709 | isRemoved = true; |
| 710 | for (const auto& i : mClients) { |
| 711 | if (i == client) { |
| 712 | isRemoved = false; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (!isRemoved) { |
| 717 | ret = mRemovedCondition.waitRelative(mLock, timeout); |
| 718 | if (ret != NO_ERROR) { |
| 719 | break; |
| 720 | } |
| 721 | timeout = failTime - systemTime(SYSTEM_TIME_MONOTONIC); |
| 722 | } |
| 723 | } |
| 724 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 725 | return ret; |
| 726 | } |
| 727 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 728 | template<class KEY, class VALUE, class LISTENER> |
| 729 | void ClientManager<KEY, VALUE, LISTENER>::setListener(const std::shared_ptr<LISTENER>& listener) { |
| 730 | Mutex::Autolock lock(mLock); |
| 731 | mListener = listener; |
| 732 | } |
| 733 | |
| 734 | template<class KEY, class VALUE, class LISTENER> |
| 735 | void ClientManager<KEY, VALUE, LISTENER>::remove( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 736 | const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& value) { |
| 737 | Mutex::Autolock lock(mLock); |
| 738 | // Remove evicted clients from list |
| 739 | mClients.erase(std::remove_if(mClients.begin(), mClients.end(), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 740 | [this, &value] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 741 | if (curClientPtr == value) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 742 | if (mListener != nullptr) mListener->onClientRemoved(*curClientPtr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 743 | return true; |
| 744 | } |
| 745 | return false; |
| 746 | }), mClients.end()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 747 | mRemovedCondition.broadcast(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 750 | template<class KEY, class VALUE, class LISTENER> |
| 751 | int64_t ClientManager<KEY, VALUE, LISTENER>::getCurrentCostLocked() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 752 | int64_t totalCost = 0; |
| 753 | for (const auto& x : mClients) { |
| 754 | totalCost += x->getCost(); |
| 755 | } |
| 756 | return totalCost; |
| 757 | } |
| 758 | |
| 759 | // -------------------------------------------------------------------------------- |
| 760 | |
| 761 | }; // namespace resource_policy |
| 762 | }; // namespace android |
| 763 | |
| 764 | #endif // ANDROID_SERVICE_UTILS_EVICTION_POLICY_MANAGER_H |