blob: b0fa5454362051df62e8b4789f4dadc966bb77d6 [file] [log] [blame]
Chong Zhangacb33502020-04-20 11:04:48 -07001/*
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 Zhang75222182020-04-29 14:43:42 -070017//#define LOG_NDEBUG 0
Chong Zhangacb33502020-04-20 11:04:48 -070018#define LOG_TAG "TranscodingUidPolicy"
19
Chong Zhangad8fdfc2020-10-21 19:05:15 -070020#include <android/activity_manager.h>
Chong Zhang97d367b2020-09-16 12:53:14 -070021#include <android/binder_manager.h>
22#include <android/binder_process.h>
Chong Zhangacb33502020-04-20 11:04:48 -070023#include <inttypes.h>
24#include <media/TranscodingUidPolicy.h>
25#include <utils/Log.h>
26
27#include <utility>
28
29namespace android {
30
31constexpr static uid_t OFFLINE_UID = -1;
Chong Zhangad8fdfc2020-10-21 19:05:15 -070032constexpr static int32_t IMPORTANCE_UNKNOWN = INT32_MAX;
Chong Zhangacb33502020-04-20 11:04:48 -070033
34TranscodingUidPolicy::TranscodingUidPolicy()
Dan Albertb253cec2021-01-21 20:28:04 +000035 : mUidObserver(nullptr),
36 mRegistered(false),
37 mTopUidState(IMPORTANCE_UNKNOWN) {
Chong Zhangacb33502020-04-20 11:04:48 -070038 registerSelf();
39}
40
41TranscodingUidPolicy::~TranscodingUidPolicy() {
42 unregisterSelf();
43}
44
Chong Zhangad8fdfc2020-10-21 19:05:15 -070045void 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 Zhangacb33502020-04-20 11:04:48 -070050void TranscodingUidPolicy::registerSelf() {
Jiyong Parkff998612021-01-15 16:01:38 +090051 if (__builtin_available(android 31, *)) {
52 mUidObserver = AActivityManager_addUidImportanceListener(&OnUidImportance, -1, (void*)this);
53 }
Chong Zhangacb33502020-04-20 11:04:48 -070054
Chong Zhangad8fdfc2020-10-21 19:05:15 -070055 if (mUidObserver == nullptr) {
56 ALOGE("Failed to register uid observer");
57 return;
Chong Zhangacb33502020-04-20 11:04:48 -070058 }
Chong Zhangad8fdfc2020-10-21 19:05:15 -070059
60 Mutex::Autolock _l(mUidLock);
61 mRegistered = true;
62 ALOGI("Registered uid observer");
Chong Zhangacb33502020-04-20 11:04:48 -070063}
64
65void TranscodingUidPolicy::unregisterSelf() {
Jiyong Parkff998612021-01-15 16:01:38 +090066 if (__builtin_available(android 31, *)) {
67 AActivityManager_removeUidImportanceListener(mUidObserver);
68 mUidObserver = nullptr;
Chong Zhangacb33502020-04-20 11:04:48 -070069
Jiyong Parkff998612021-01-15 16:01:38 +090070 Mutex::Autolock _l(mUidLock);
71 mRegistered = false;
72 ALOGI("Unregistered uid observer");
73 } else {
74 ALOGE("Failed to unregister uid observer");
75 }
Chong Zhangacb33502020-04-20 11:04:48 -070076}
77
78void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) {
79 mUidPolicyCallback = cb;
80}
81
82void 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 Zhangad8fdfc2020-10-21 19:05:15 -070093 int32_t state = IMPORTANCE_UNKNOWN;
Jiyong Parkff998612021-01-15 16:01:38 +090094 if (__builtin_available(android 31, *)) {
95 if (mRegistered && AActivityManager_isUidActive(uid)) {
96 state = AActivityManager_getUidImportance(uid);
97 }
Chong Zhangacb33502020-04-20 11:04:48 -070098 }
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
108void 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
129bool TranscodingUidPolicy::isUidOnTop(uid_t uid) {
130 Mutex::Autolock _l(mUidLock);
131
Dan Albertb253cec2021-01-21 20:28:04 +0000132 return mTopUidState != IMPORTANCE_UNKNOWN &&
133 mTopUidState == getProcState_l(uid);
Chong Zhangacb33502020-04-20 11:04:48 -0700134}
135
136std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const {
137 Mutex::Autolock _l(mUidLock);
138
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700139 if (mTopUidState == IMPORTANCE_UNKNOWN) {
Chong Zhangacb33502020-04-20 11:04:48 -0700140 return std::unordered_set<uid_t>();
141 }
142
143 return mStateUidMap.at(mTopUidState);
144}
145
146void 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 Zhangad8fdfc2020-10-21 19:05:15 -0700157 bool isUidCurrentTop =
Dan Albertb253cec2021-01-21 20:28:04 +0000158 mTopUidState != IMPORTANCE_UNKNOWN &&
159 mStateUidMap[mTopUidState].count(uid) > 0;
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700160 bool isNewStateHigherThanTop =
161 procState != IMPORTANCE_UNKNOWN &&
Dan Albertb253cec2021-01-21 20:28:04 +0000162 (procState <= mTopUidState ||
163 mTopUidState == IMPORTANCE_UNKNOWN);
Chong Zhangacb33502020-04-20 11:04:48 -0700164 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
190void TranscodingUidPolicy::updateTopUid_l() {
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700191 mTopUidState = IMPORTANCE_UNKNOWN;
Chong Zhang75222182020-04-29 14:43:42 -0700192
193 // Find the lowest uid state (ignoring PROCESS_STATE_UNKNOWN) with some monitored uids.
Chong Zhangacb33502020-04-20 11:04:48 -0700194 for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) {
Dan Albertb253cec2021-01-21 20:28:04 +0000195 if (stateIt->first != IMPORTANCE_UNKNOWN &&
196 !stateIt->second.empty()) {
Chong Zhangacb33502020-04-20 11:04:48 -0700197 mTopUidState = stateIt->first;
198 break;
199 }
200 }
201
202 ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState);
203}
204
205int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) {
206 auto it = mUidStateMap.find(uid);
207 if (it != mUidStateMap.end()) {
208 return it->second;
209 }
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700210 return IMPORTANCE_UNKNOWN;
Chong Zhangacb33502020-04-20 11:04:48 -0700211}
212
213} // namespace android