Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 18 | #define LOG_TAG "TranscodingUidPolicy" |
| 19 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 20 | #include <android/activity_manager.h> |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | #include <media/TranscodingUidPolicy.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <utility> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | constexpr static uid_t OFFLINE_UID = -1; |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 32 | constexpr static int32_t IMPORTANCE_UNKNOWN = INT32_MAX; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 33 | |
| 34 | TranscodingUidPolicy::TranscodingUidPolicy() |
Dan Albert | b253cec | 2021-01-21 20:28:04 +0000 | [diff] [blame] | 35 | : mUidObserver(nullptr), |
| 36 | mRegistered(false), |
| 37 | mTopUidState(IMPORTANCE_UNKNOWN) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 38 | registerSelf(); |
| 39 | } |
| 40 | |
| 41 | TranscodingUidPolicy::~TranscodingUidPolicy() { |
| 42 | unregisterSelf(); |
| 43 | } |
| 44 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 45 | void TranscodingUidPolicy::OnUidImportance(uid_t uid, int32_t uidImportance, void* cookie) { |
| 46 | TranscodingUidPolicy* owner = reinterpret_cast<TranscodingUidPolicy*>(cookie); |
| 47 | owner->onUidStateChanged(uid, uidImportance); |
| 48 | } |
| 49 | |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 50 | void TranscodingUidPolicy::registerSelf() { |
Jiyong Park | ff99861 | 2021-01-15 16:01:38 +0900 | [diff] [blame] | 51 | if (__builtin_available(android 31, *)) { |
| 52 | mUidObserver = AActivityManager_addUidImportanceListener(&OnUidImportance, -1, (void*)this); |
| 53 | } |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 54 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 55 | if (mUidObserver == nullptr) { |
| 56 | ALOGE("Failed to register uid observer"); |
| 57 | return; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 58 | } |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 59 | |
| 60 | Mutex::Autolock _l(mUidLock); |
| 61 | mRegistered = true; |
| 62 | ALOGI("Registered uid observer"); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void TranscodingUidPolicy::unregisterSelf() { |
Jiyong Park | ff99861 | 2021-01-15 16:01:38 +0900 | [diff] [blame] | 66 | if (__builtin_available(android 31, *)) { |
| 67 | AActivityManager_removeUidImportanceListener(mUidObserver); |
| 68 | mUidObserver = nullptr; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 69 | |
Jiyong Park | ff99861 | 2021-01-15 16:01:38 +0900 | [diff] [blame] | 70 | Mutex::Autolock _l(mUidLock); |
| 71 | mRegistered = false; |
| 72 | ALOGI("Unregistered uid observer"); |
| 73 | } else { |
| 74 | ALOGE("Failed to unregister uid observer"); |
| 75 | } |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) { |
| 79 | mUidPolicyCallback = cb; |
| 80 | } |
| 81 | |
| 82 | void TranscodingUidPolicy::registerMonitorUid(uid_t uid) { |
| 83 | Mutex::Autolock _l(mUidLock); |
| 84 | if (uid == OFFLINE_UID) { |
| 85 | ALOGW("Ignoring the offline uid"); |
| 86 | return; |
| 87 | } |
| 88 | if (mUidStateMap.find(uid) != mUidStateMap.end()) { |
| 89 | ALOGE("%s: Trying to register uid: %d which is already monitored!", __FUNCTION__, uid); |
| 90 | return; |
| 91 | } |
| 92 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 93 | int32_t state = IMPORTANCE_UNKNOWN; |
Jiyong Park | ff99861 | 2021-01-15 16:01:38 +0900 | [diff] [blame] | 94 | if (__builtin_available(android 31, *)) { |
| 95 | if (mRegistered && AActivityManager_isUidActive(uid)) { |
| 96 | state = AActivityManager_getUidImportance(uid); |
| 97 | } |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state); |
| 101 | |
| 102 | mUidStateMap.emplace(std::pair<uid_t, int32_t>(uid, state)); |
| 103 | mStateUidMap[state].insert(uid); |
| 104 | |
| 105 | updateTopUid_l(); |
| 106 | } |
| 107 | |
| 108 | void TranscodingUidPolicy::unregisterMonitorUid(uid_t uid) { |
| 109 | Mutex::Autolock _l(mUidLock); |
| 110 | |
| 111 | auto it = mUidStateMap.find(uid); |
| 112 | if (it == mUidStateMap.end()) { |
| 113 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | auto stateIt = mStateUidMap.find(it->second); |
| 118 | if (stateIt != mStateUidMap.end()) { |
| 119 | stateIt->second.erase(uid); |
| 120 | if (stateIt->second.empty()) { |
| 121 | mStateUidMap.erase(stateIt); |
| 122 | } |
| 123 | } |
| 124 | mUidStateMap.erase(it); |
| 125 | |
| 126 | updateTopUid_l(); |
| 127 | } |
| 128 | |
| 129 | bool TranscodingUidPolicy::isUidOnTop(uid_t uid) { |
| 130 | Mutex::Autolock _l(mUidLock); |
| 131 | |
Dan Albert | b253cec | 2021-01-21 20:28:04 +0000 | [diff] [blame] | 132 | return mTopUidState != IMPORTANCE_UNKNOWN && |
| 133 | mTopUidState == getProcState_l(uid); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const { |
| 137 | Mutex::Autolock _l(mUidLock); |
| 138 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 139 | if (mTopUidState == IMPORTANCE_UNKNOWN) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 140 | return std::unordered_set<uid_t>(); |
| 141 | } |
| 142 | |
| 143 | return mStateUidMap.at(mTopUidState); |
| 144 | } |
| 145 | |
| 146 | void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) { |
| 147 | ALOGV("onUidStateChanged: %u, procState %d", uid, procState); |
| 148 | |
| 149 | bool topUidSetChanged = false; |
| 150 | std::unordered_set<uid_t> topUids; |
| 151 | { |
| 152 | Mutex::Autolock _l(mUidLock); |
| 153 | auto it = mUidStateMap.find(uid); |
| 154 | if (it != mUidStateMap.end() && it->second != procState) { |
| 155 | // Top set changed if 1) the uid is in the current top uid set, or 2) the |
| 156 | // new procState is at least the same priority as the current top uid state. |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 157 | bool isUidCurrentTop = |
Dan Albert | b253cec | 2021-01-21 20:28:04 +0000 | [diff] [blame] | 158 | mTopUidState != IMPORTANCE_UNKNOWN && |
| 159 | mStateUidMap[mTopUidState].count(uid) > 0; |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 160 | bool isNewStateHigherThanTop = |
| 161 | procState != IMPORTANCE_UNKNOWN && |
Dan Albert | b253cec | 2021-01-21 20:28:04 +0000 | [diff] [blame] | 162 | (procState <= mTopUidState || |
| 163 | mTopUidState == IMPORTANCE_UNKNOWN); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 164 | topUidSetChanged = (isUidCurrentTop || isNewStateHigherThanTop); |
| 165 | |
| 166 | // Move uid to the new procState. |
| 167 | mStateUidMap[it->second].erase(uid); |
| 168 | mStateUidMap[procState].insert(uid); |
| 169 | it->second = procState; |
| 170 | |
| 171 | if (topUidSetChanged) { |
| 172 | updateTopUid_l(); |
| 173 | |
| 174 | // Make a copy of the uid set for callback. |
| 175 | topUids = mStateUidMap[mTopUidState]; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | ALOGV("topUidSetChanged: %d", topUidSetChanged); |
| 181 | |
| 182 | if (topUidSetChanged) { |
| 183 | auto callback = mUidPolicyCallback.lock(); |
| 184 | if (callback != nullptr) { |
| 185 | callback->onTopUidsChanged(topUids); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void TranscodingUidPolicy::updateTopUid_l() { |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 191 | mTopUidState = IMPORTANCE_UNKNOWN; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 192 | |
| 193 | // Find the lowest uid state (ignoring PROCESS_STATE_UNKNOWN) with some monitored uids. |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 194 | for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) { |
Dan Albert | b253cec | 2021-01-21 20:28:04 +0000 | [diff] [blame] | 195 | if (stateIt->first != IMPORTANCE_UNKNOWN && |
| 196 | !stateIt->second.empty()) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 197 | mTopUidState = stateIt->first; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState); |
| 203 | } |
| 204 | |
| 205 | int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) { |
| 206 | auto it = mUidStateMap.find(uid); |
| 207 | if (it != mUidStateMap.end()) { |
| 208 | return it->second; |
| 209 | } |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 210 | return IMPORTANCE_UNKNOWN; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } // namespace android |