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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "TranscodingUidPolicy"
19
20 #include <android/activity_manager.h>
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <inttypes.h>
24 #include <media/TranscodingDefs.h>
25 #include <media/TranscodingUidPolicy.h>
26 #include <utils/Log.h>
27
28 #include <utility>
29
30 namespace android {
31
32 constexpr static uid_t OFFLINE_UID = -1;
33 constexpr static int32_t IMPORTANCE_UNKNOWN = INT32_MAX;
34
TranscodingUidPolicy()35 TranscodingUidPolicy::TranscodingUidPolicy()
36 : mUidObserver(nullptr), mRegistered(false), mTopUidState(IMPORTANCE_UNKNOWN) {
37 registerSelf();
38 }
39
~TranscodingUidPolicy()40 TranscodingUidPolicy::~TranscodingUidPolicy() {
41 unregisterSelf();
42 }
43
OnUidImportance(uid_t uid,int32_t uidImportance,void * cookie)44 void TranscodingUidPolicy::OnUidImportance(uid_t uid, int32_t uidImportance, void* cookie) {
45 TranscodingUidPolicy* owner = reinterpret_cast<TranscodingUidPolicy*>(cookie);
46 owner->onUidStateChanged(uid, uidImportance);
47 }
48
registerSelf()49 void TranscodingUidPolicy::registerSelf() {
50 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
51 mUidObserver = AActivityManager_addUidImportanceListener(&OnUidImportance, -1, (void*)this);
52 }
53
54 if (mUidObserver == nullptr) {
55 ALOGE("Failed to register uid observer");
56 return;
57 }
58
59 Mutex::Autolock _l(mUidLock);
60 mRegistered = true;
61 ALOGI("Registered uid observer");
62 }
63
unregisterSelf()64 void TranscodingUidPolicy::unregisterSelf() {
65 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
66 AActivityManager_removeUidImportanceListener(mUidObserver);
67 mUidObserver = nullptr;
68
69 Mutex::Autolock _l(mUidLock);
70 mRegistered = false;
71 ALOGI("Unregistered uid observer");
72 } else {
73 ALOGE("Failed to unregister uid observer");
74 }
75 }
76
setCallback(const std::shared_ptr<UidPolicyCallbackInterface> & cb)77 void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) {
78 mUidPolicyCallback = cb;
79 }
80
registerMonitorUid(uid_t uid)81 void 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
92 int32_t state = IMPORTANCE_UNKNOWN;
93 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
94 if (mRegistered && AActivityManager_isUidActive(uid)) {
95 state = AActivityManager_getUidImportance(uid);
96 }
97 }
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
unregisterMonitorUid(uid_t uid)107 void 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
isUidOnTop(uid_t uid)128 bool TranscodingUidPolicy::isUidOnTop(uid_t uid) {
129 Mutex::Autolock _l(mUidLock);
130
131 return mTopUidState != IMPORTANCE_UNKNOWN && mTopUidState == getProcState_l(uid);
132 }
133
getTopUids() const134 std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const {
135 Mutex::Autolock _l(mUidLock);
136
137 if (mTopUidState == IMPORTANCE_UNKNOWN) {
138 return std::unordered_set<uid_t>();
139 }
140
141 return mStateUidMap.at(mTopUidState);
142 }
143
onUidStateChanged(uid_t uid,int32_t procState)144 void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) {
145 ALOGV("onUidStateChanged: uid %u, procState %d", uid, procState);
146
147 bool topUidSetChanged = false;
148 bool isUidGone = false;
149 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) {
154 isUidGone = (procState == AACTIVITYMANAGER_IMPORTANCE_GONE);
155
156 topUids = mStateUidMap[mTopUidState];
157
158 // Move uid to the new procState.
159 mStateUidMap[it->second].erase(uid);
160 mStateUidMap[procState].insert(uid);
161 it->second = procState;
162
163 updateTopUid_l();
164 if (topUids != mStateUidMap[mTopUidState]) {
165 // Make a copy of the uid set for callback.
166 topUids = mStateUidMap[mTopUidState];
167 topUidSetChanged = true;
168 }
169 }
170 }
171
172 ALOGV("topUidSetChanged: %d, isUidGone %d", topUidSetChanged, isUidGone);
173
174 if (topUidSetChanged) {
175 auto callback = mUidPolicyCallback.lock();
176 if (callback != nullptr) {
177 callback->onTopUidsChanged(topUids);
178 }
179 }
180 if (isUidGone) {
181 auto callback = mUidPolicyCallback.lock();
182 if (callback != nullptr) {
183 callback->onUidGone(uid);
184 }
185 }
186 }
187
updateTopUid_l()188 void TranscodingUidPolicy::updateTopUid_l() {
189 mTopUidState = IMPORTANCE_UNKNOWN;
190
191 // Find the lowest uid state (ignoring PROCESS_STATE_UNKNOWN) with some monitored uids.
192 for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) {
193 if (stateIt->first != IMPORTANCE_UNKNOWN && !stateIt->second.empty()) {
194 mTopUidState = stateIt->first;
195 break;
196 }
197 }
198
199 ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState);
200 }
201
getProcState_l(uid_t uid)202 int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) {
203 auto it = mUidStateMap.find(uid);
204 if (it != mUidStateMap.end()) {
205 return it->second;
206 }
207 return IMPORTANCE_UNKNOWN;
208 }
209
210 } // namespace android
211