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() |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 35 | : mUidObserver(nullptr), |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 36 | mRegistered(false), |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 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() { |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 51 | mUidObserver = AActivityManager_addUidImportanceListener( |
| 52 | &OnUidImportance, -1, (void*)this); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 53 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 54 | if (mUidObserver == nullptr) { |
| 55 | ALOGE("Failed to register uid observer"); |
| 56 | return; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 57 | } |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 58 | |
| 59 | Mutex::Autolock _l(mUidLock); |
| 60 | mRegistered = true; |
| 61 | ALOGI("Registered uid observer"); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void TranscodingUidPolicy::unregisterSelf() { |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 65 | AActivityManager_removeUidImportanceListener(mUidObserver); |
| 66 | mUidObserver = nullptr; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 67 | |
| 68 | Mutex::Autolock _l(mUidLock); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 69 | mRegistered = false; |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 70 | ALOGI("Unregistered uid observer"); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) { |
| 74 | mUidPolicyCallback = cb; |
| 75 | } |
| 76 | |
| 77 | void TranscodingUidPolicy::registerMonitorUid(uid_t uid) { |
| 78 | Mutex::Autolock _l(mUidLock); |
| 79 | if (uid == OFFLINE_UID) { |
| 80 | ALOGW("Ignoring the offline uid"); |
| 81 | return; |
| 82 | } |
| 83 | if (mUidStateMap.find(uid) != mUidStateMap.end()) { |
| 84 | ALOGE("%s: Trying to register uid: %d which is already monitored!", __FUNCTION__, uid); |
| 85 | return; |
| 86 | } |
| 87 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 88 | int32_t state = IMPORTANCE_UNKNOWN; |
| 89 | if (mRegistered && AActivityManager_isUidActive(uid)) { |
| 90 | state = AActivityManager_getUidImportance(uid); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state); |
| 94 | |
| 95 | mUidStateMap.emplace(std::pair<uid_t, int32_t>(uid, state)); |
| 96 | mStateUidMap[state].insert(uid); |
| 97 | |
| 98 | updateTopUid_l(); |
| 99 | } |
| 100 | |
| 101 | void TranscodingUidPolicy::unregisterMonitorUid(uid_t uid) { |
| 102 | Mutex::Autolock _l(mUidLock); |
| 103 | |
| 104 | auto it = mUidStateMap.find(uid); |
| 105 | if (it == mUidStateMap.end()) { |
| 106 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | auto stateIt = mStateUidMap.find(it->second); |
| 111 | if (stateIt != mStateUidMap.end()) { |
| 112 | stateIt->second.erase(uid); |
| 113 | if (stateIt->second.empty()) { |
| 114 | mStateUidMap.erase(stateIt); |
| 115 | } |
| 116 | } |
| 117 | mUidStateMap.erase(it); |
| 118 | |
| 119 | updateTopUid_l(); |
| 120 | } |
| 121 | |
| 122 | bool TranscodingUidPolicy::isUidOnTop(uid_t uid) { |
| 123 | Mutex::Autolock _l(mUidLock); |
| 124 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 125 | return mTopUidState != IMPORTANCE_UNKNOWN && |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 126 | mTopUidState == getProcState_l(uid); |
| 127 | } |
| 128 | |
| 129 | std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const { |
| 130 | Mutex::Autolock _l(mUidLock); |
| 131 | |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 132 | if (mTopUidState == IMPORTANCE_UNKNOWN) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 133 | return std::unordered_set<uid_t>(); |
| 134 | } |
| 135 | |
| 136 | return mStateUidMap.at(mTopUidState); |
| 137 | } |
| 138 | |
| 139 | void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) { |
| 140 | ALOGV("onUidStateChanged: %u, procState %d", uid, procState); |
| 141 | |
| 142 | bool topUidSetChanged = false; |
| 143 | std::unordered_set<uid_t> topUids; |
| 144 | { |
| 145 | Mutex::Autolock _l(mUidLock); |
| 146 | auto it = mUidStateMap.find(uid); |
| 147 | if (it != mUidStateMap.end() && it->second != procState) { |
| 148 | // Top set changed if 1) the uid is in the current top uid set, or 2) the |
| 149 | // 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] | 150 | bool isUidCurrentTop = |
| 151 | mTopUidState != IMPORTANCE_UNKNOWN && |
| 152 | mStateUidMap[mTopUidState].count(uid) > 0; |
| 153 | bool isNewStateHigherThanTop = |
| 154 | procState != IMPORTANCE_UNKNOWN && |
| 155 | (procState <= mTopUidState || |
| 156 | mTopUidState == IMPORTANCE_UNKNOWN); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 157 | topUidSetChanged = (isUidCurrentTop || isNewStateHigherThanTop); |
| 158 | |
| 159 | // Move uid to the new procState. |
| 160 | mStateUidMap[it->second].erase(uid); |
| 161 | mStateUidMap[procState].insert(uid); |
| 162 | it->second = procState; |
| 163 | |
| 164 | if (topUidSetChanged) { |
| 165 | updateTopUid_l(); |
| 166 | |
| 167 | // Make a copy of the uid set for callback. |
| 168 | topUids = mStateUidMap[mTopUidState]; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | ALOGV("topUidSetChanged: %d", topUidSetChanged); |
| 174 | |
| 175 | if (topUidSetChanged) { |
| 176 | auto callback = mUidPolicyCallback.lock(); |
| 177 | if (callback != nullptr) { |
| 178 | callback->onTopUidsChanged(topUids); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void TranscodingUidPolicy::updateTopUid_l() { |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 184 | mTopUidState = IMPORTANCE_UNKNOWN; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 185 | |
| 186 | // 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] | 187 | for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) { |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 188 | if (stateIt->first != IMPORTANCE_UNKNOWN && |
| 189 | !stateIt->second.empty()) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 190 | mTopUidState = stateIt->first; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState); |
| 196 | } |
| 197 | |
| 198 | int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) { |
| 199 | auto it = mUidStateMap.find(uid); |
| 200 | if (it != mUidStateMap.end()) { |
| 201 | return it->second; |
| 202 | } |
Chong Zhang | ad8fdfc | 2020-10-21 19:05:15 -0700 | [diff] [blame] | 203 | return IMPORTANCE_UNKNOWN; |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace android |