blob: f33c54cbc9af24047847dfb4ecd85b59d17f3a52 [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>
Chong Zhang5945dbe2021-04-21 17:34:51 -070024#include <media/TranscodingDefs.h>
Chong Zhangacb33502020-04-20 11:04:48 -070025#include <media/TranscodingUidPolicy.h>
26#include <utils/Log.h>
27
28#include <utility>
29
30namespace android {
31
32constexpr static uid_t OFFLINE_UID = -1;
Chong Zhangad8fdfc2020-10-21 19:05:15 -070033constexpr static int32_t IMPORTANCE_UNKNOWN = INT32_MAX;
Chong Zhangacb33502020-04-20 11:04:48 -070034
35TranscodingUidPolicy::TranscodingUidPolicy()
Chong Zhang8677f1f2021-01-21 20:37:35 +000036 : mUidObserver(nullptr), mRegistered(false), mTopUidState(IMPORTANCE_UNKNOWN) {
Chong Zhangacb33502020-04-20 11:04:48 -070037 registerSelf();
38}
39
40TranscodingUidPolicy::~TranscodingUidPolicy() {
41 unregisterSelf();
42}
43
Chong Zhangad8fdfc2020-10-21 19:05:15 -070044void TranscodingUidPolicy::OnUidImportance(uid_t uid, int32_t uidImportance, void* cookie) {
45 TranscodingUidPolicy* owner = reinterpret_cast<TranscodingUidPolicy*>(cookie);
46 owner->onUidStateChanged(uid, uidImportance);
47}
48
Chong Zhangacb33502020-04-20 11:04:48 -070049void TranscodingUidPolicy::registerSelf() {
Chong Zhang5c504ee2021-01-21 18:53:19 -080050 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
Jiyong Parkff998612021-01-15 16:01:38 +090051 mUidObserver = AActivityManager_addUidImportanceListener(&OnUidImportance, -1, (void*)this);
52 }
Chong Zhangacb33502020-04-20 11:04:48 -070053
Chong Zhangad8fdfc2020-10-21 19:05:15 -070054 if (mUidObserver == nullptr) {
55 ALOGE("Failed to register uid observer");
56 return;
Chong Zhangacb33502020-04-20 11:04:48 -070057 }
Chong Zhangad8fdfc2020-10-21 19:05:15 -070058
59 Mutex::Autolock _l(mUidLock);
60 mRegistered = true;
61 ALOGI("Registered uid observer");
Chong Zhangacb33502020-04-20 11:04:48 -070062}
63
64void TranscodingUidPolicy::unregisterSelf() {
Chong Zhang5c504ee2021-01-21 18:53:19 -080065 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
Jiyong Parkff998612021-01-15 16:01:38 +090066 AActivityManager_removeUidImportanceListener(mUidObserver);
67 mUidObserver = nullptr;
Chong Zhangacb33502020-04-20 11:04:48 -070068
Jiyong Parkff998612021-01-15 16:01:38 +090069 Mutex::Autolock _l(mUidLock);
70 mRegistered = false;
71 ALOGI("Unregistered uid observer");
72 } else {
73 ALOGE("Failed to unregister uid observer");
74 }
Chong Zhangacb33502020-04-20 11:04:48 -070075}
76
77void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) {
78 mUidPolicyCallback = cb;
79}
80
81void TranscodingUidPolicy::registerMonitorUid(uid_t uid) {
82 Mutex::Autolock _l(mUidLock);
83 if (uid == OFFLINE_UID) {
84 ALOGW("Ignoring the offline uid");
85 return;
86 }
87 if (mUidStateMap.find(uid) != mUidStateMap.end()) {
88 ALOGE("%s: Trying to register uid: %d which is already monitored!", __FUNCTION__, uid);
89 return;
90 }
91
Chong Zhangad8fdfc2020-10-21 19:05:15 -070092 int32_t state = IMPORTANCE_UNKNOWN;
Chong Zhang5c504ee2021-01-21 18:53:19 -080093 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
Jiyong Parkff998612021-01-15 16:01:38 +090094 if (mRegistered && AActivityManager_isUidActive(uid)) {
95 state = AActivityManager_getUidImportance(uid);
96 }
Chong Zhangacb33502020-04-20 11:04:48 -070097 }
98
99 ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state);
100
101 mUidStateMap.emplace(std::pair<uid_t, int32_t>(uid, state));
102 mStateUidMap[state].insert(uid);
103
104 updateTopUid_l();
105}
106
107void TranscodingUidPolicy::unregisterMonitorUid(uid_t uid) {
108 Mutex::Autolock _l(mUidLock);
109
110 auto it = mUidStateMap.find(uid);
111 if (it == mUidStateMap.end()) {
112 ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid);
113 return;
114 }
115
116 auto stateIt = mStateUidMap.find(it->second);
117 if (stateIt != mStateUidMap.end()) {
118 stateIt->second.erase(uid);
119 if (stateIt->second.empty()) {
120 mStateUidMap.erase(stateIt);
121 }
122 }
123 mUidStateMap.erase(it);
124
125 updateTopUid_l();
126}
127
128bool TranscodingUidPolicy::isUidOnTop(uid_t uid) {
129 Mutex::Autolock _l(mUidLock);
130
Chong Zhang8677f1f2021-01-21 20:37:35 +0000131 return mTopUidState != IMPORTANCE_UNKNOWN && mTopUidState == getProcState_l(uid);
Chong Zhangacb33502020-04-20 11:04:48 -0700132}
133
134std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const {
135 Mutex::Autolock _l(mUidLock);
136
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700137 if (mTopUidState == IMPORTANCE_UNKNOWN) {
Chong Zhangacb33502020-04-20 11:04:48 -0700138 return std::unordered_set<uid_t>();
139 }
140
141 return mStateUidMap.at(mTopUidState);
142}
143
144void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) {
Chong Zhang2a3c9672021-03-31 15:36:32 -0700145 ALOGV("onUidStateChanged: uid %u, procState %d", uid, procState);
Chong Zhangacb33502020-04-20 11:04:48 -0700146
147 bool topUidSetChanged = false;
Chong Zhang2a3c9672021-03-31 15:36:32 -0700148 bool isUidGone = false;
Chong Zhangacb33502020-04-20 11:04:48 -0700149 std::unordered_set<uid_t> topUids;
150 {
151 Mutex::Autolock _l(mUidLock);
152 auto it = mUidStateMap.find(uid);
153 if (it != mUidStateMap.end() && it->second != procState) {
Chong Zhang2a3c9672021-03-31 15:36:32 -0700154 isUidGone = (procState == AACTIVITYMANAGER_IMPORTANCE_GONE);
155
156 topUids = mStateUidMap[mTopUidState];
Chong Zhangacb33502020-04-20 11:04:48 -0700157
158 // Move uid to the new procState.
159 mStateUidMap[it->second].erase(uid);
160 mStateUidMap[procState].insert(uid);
161 it->second = procState;
162
Chong Zhang2a3c9672021-03-31 15:36:32 -0700163 updateTopUid_l();
164 if (topUids != mStateUidMap[mTopUidState]) {
Chong Zhangacb33502020-04-20 11:04:48 -0700165 // Make a copy of the uid set for callback.
166 topUids = mStateUidMap[mTopUidState];
Chong Zhang2a3c9672021-03-31 15:36:32 -0700167 topUidSetChanged = true;
Chong Zhangacb33502020-04-20 11:04:48 -0700168 }
169 }
170 }
171
Chong Zhang2a3c9672021-03-31 15:36:32 -0700172 ALOGV("topUidSetChanged: %d, isUidGone %d", topUidSetChanged, isUidGone);
Chong Zhangacb33502020-04-20 11:04:48 -0700173
174 if (topUidSetChanged) {
175 auto callback = mUidPolicyCallback.lock();
176 if (callback != nullptr) {
177 callback->onTopUidsChanged(topUids);
178 }
179 }
Chong Zhang2a3c9672021-03-31 15:36:32 -0700180 if (isUidGone) {
181 auto callback = mUidPolicyCallback.lock();
182 if (callback != nullptr) {
183 callback->onUidGone(uid);
184 }
185 }
Chong Zhangacb33502020-04-20 11:04:48 -0700186}
187
188void TranscodingUidPolicy::updateTopUid_l() {
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700189 mTopUidState = IMPORTANCE_UNKNOWN;
Chong Zhang75222182020-04-29 14:43:42 -0700190
191 // Find the lowest uid state (ignoring PROCESS_STATE_UNKNOWN) with some monitored uids.
Chong Zhangacb33502020-04-20 11:04:48 -0700192 for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) {
Chong Zhang8677f1f2021-01-21 20:37:35 +0000193 if (stateIt->first != IMPORTANCE_UNKNOWN && !stateIt->second.empty()) {
Chong Zhangacb33502020-04-20 11:04:48 -0700194 mTopUidState = stateIt->first;
195 break;
196 }
197 }
198
199 ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState);
200}
201
202int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) {
203 auto it = mUidStateMap.find(uid);
204 if (it != mUidStateMap.end()) {
205 return it->second;
206 }
Chong Zhangad8fdfc2020-10-21 19:05:15 -0700207 return IMPORTANCE_UNKNOWN;
Chong Zhangacb33502020-04-20 11:04:48 -0700208}
209
210} // namespace android