blob: b72a2b928d99aac4afe5fc08422a1baf5640ef9f [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
20#include <binder/ActivityManager.h>
21#include <cutils/misc.h> // FIRST_APPLICATION_UID
22#include <inttypes.h>
23#include <media/TranscodingUidPolicy.h>
24#include <utils/Log.h>
25
26#include <utility>
27
28namespace android {
29
30constexpr static uid_t OFFLINE_UID = -1;
31constexpr static const char* kTranscodingTag = "transcoding";
32
33struct TranscodingUidPolicy::UidObserver : public BnUidObserver,
34 public virtual IBinder::DeathRecipient {
35 explicit UidObserver(TranscodingUidPolicy* owner) : mOwner(owner) {}
36
37 // IUidObserver
38 void onUidGone(uid_t uid, bool disabled) override;
39 void onUidActive(uid_t uid) override;
40 void onUidIdle(uid_t uid, bool disabled) override;
41 void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq,
42 int32_t capability) override;
43
44 // IBinder::DeathRecipient implementation
45 void binderDied(const wp<IBinder>& who) override;
46
47 TranscodingUidPolicy* mOwner;
48};
49
50void TranscodingUidPolicy::UidObserver::onUidGone(uid_t uid __unused, bool disabled __unused) {}
51
52void TranscodingUidPolicy::UidObserver::onUidActive(uid_t uid __unused) {}
53
54void TranscodingUidPolicy::UidObserver::onUidIdle(uid_t uid __unused, bool disabled __unused) {}
55
56void TranscodingUidPolicy::UidObserver::onUidStateChanged(uid_t uid, int32_t procState,
57 int64_t procStateSeq __unused,
58 int32_t capability __unused) {
59 mOwner->onUidStateChanged(uid, procState);
60}
61
62void TranscodingUidPolicy::UidObserver::binderDied(const wp<IBinder>& /*who*/) {
63 ALOGW("TranscodingUidPolicy: ActivityManager has died");
64 // TODO(chz): this is a rare event (since if the AMS is dead, the system is
65 // probably dead as well). But we should try to reconnect.
66 mOwner->setUidObserverRegistered(false);
67}
68
69////////////////////////////////////////////////////////////////////////////
70
71TranscodingUidPolicy::TranscodingUidPolicy()
72 : mAm(std::make_shared<ActivityManager>()),
73 mUidObserver(new UidObserver(this)),
74 mRegistered(false),
75 mTopUidState(ActivityManager::PROCESS_STATE_UNKNOWN) {
76 registerSelf();
77}
78
79TranscodingUidPolicy::~TranscodingUidPolicy() {
80 unregisterSelf();
81}
82
83void TranscodingUidPolicy::registerSelf() {
84 status_t res = mAm->linkToDeath(mUidObserver.get());
85 mAm->registerUidObserver(
86 mUidObserver.get(),
87 ActivityManager::UID_OBSERVER_GONE | ActivityManager::UID_OBSERVER_IDLE |
88 ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE,
89 ActivityManager::PROCESS_STATE_UNKNOWN, String16(kTranscodingTag));
90
91 if (res == OK) {
92 Mutex::Autolock _l(mUidLock);
93
94 mRegistered = true;
95 ALOGI("TranscodingUidPolicy: Registered with ActivityManager");
96 } else {
97 mAm->unregisterUidObserver(mUidObserver.get());
98 }
99}
100
101void TranscodingUidPolicy::unregisterSelf() {
102 mAm->unregisterUidObserver(mUidObserver.get());
103 mAm->unlinkToDeath(mUidObserver.get());
104
105 Mutex::Autolock _l(mUidLock);
106
107 mRegistered = false;
108
109 ALOGI("TranscodingUidPolicy: Unregistered with ActivityManager");
110}
111
112void TranscodingUidPolicy::setUidObserverRegistered(bool registered) {
113 Mutex::Autolock _l(mUidLock);
114
115 mRegistered = registered;
116}
117
118void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) {
119 mUidPolicyCallback = cb;
120}
121
122void TranscodingUidPolicy::registerMonitorUid(uid_t uid) {
123 Mutex::Autolock _l(mUidLock);
124 if (uid == OFFLINE_UID) {
125 ALOGW("Ignoring the offline uid");
126 return;
127 }
128 if (mUidStateMap.find(uid) != mUidStateMap.end()) {
129 ALOGE("%s: Trying to register uid: %d which is already monitored!", __FUNCTION__, uid);
130 return;
131 }
132
133 int32_t state = ActivityManager::PROCESS_STATE_UNKNOWN;
Hui Yu799ba4f2020-05-05 17:04:15 -0700134 if (mRegistered && mAm->isUidActive(uid, String16(kTranscodingTag))) {
Chong Zhangacb33502020-04-20 11:04:48 -0700135 state = mAm->getUidProcessState(uid, String16(kTranscodingTag));
136 }
137
138 ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state);
139
140 mUidStateMap.emplace(std::pair<uid_t, int32_t>(uid, state));
141 mStateUidMap[state].insert(uid);
142
143 updateTopUid_l();
144}
145
146void TranscodingUidPolicy::unregisterMonitorUid(uid_t uid) {
147 Mutex::Autolock _l(mUidLock);
148
149 auto it = mUidStateMap.find(uid);
150 if (it == mUidStateMap.end()) {
151 ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid);
152 return;
153 }
154
155 auto stateIt = mStateUidMap.find(it->second);
156 if (stateIt != mStateUidMap.end()) {
157 stateIt->second.erase(uid);
158 if (stateIt->second.empty()) {
159 mStateUidMap.erase(stateIt);
160 }
161 }
162 mUidStateMap.erase(it);
163
164 updateTopUid_l();
165}
166
167bool TranscodingUidPolicy::isUidOnTop(uid_t uid) {
168 Mutex::Autolock _l(mUidLock);
169
170 return mTopUidState != ActivityManager::PROCESS_STATE_UNKNOWN &&
171 mTopUidState == getProcState_l(uid);
172}
173
174std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const {
175 Mutex::Autolock _l(mUidLock);
176
177 if (mTopUidState == ActivityManager::PROCESS_STATE_UNKNOWN) {
178 return std::unordered_set<uid_t>();
179 }
180
181 return mStateUidMap.at(mTopUidState);
182}
183
184void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) {
185 ALOGV("onUidStateChanged: %u, procState %d", uid, procState);
186
187 bool topUidSetChanged = false;
188 std::unordered_set<uid_t> topUids;
189 {
190 Mutex::Autolock _l(mUidLock);
191 auto it = mUidStateMap.find(uid);
192 if (it != mUidStateMap.end() && it->second != procState) {
193 // Top set changed if 1) the uid is in the current top uid set, or 2) the
194 // new procState is at least the same priority as the current top uid state.
195 bool isUidCurrentTop = mTopUidState != ActivityManager::PROCESS_STATE_UNKNOWN &&
196 mStateUidMap[mTopUidState].count(uid) > 0;
197 bool isNewStateHigherThanTop = procState != ActivityManager::PROCESS_STATE_UNKNOWN &&
198 (procState <= mTopUidState ||
199 mTopUidState == ActivityManager::PROCESS_STATE_UNKNOWN);
200 topUidSetChanged = (isUidCurrentTop || isNewStateHigherThanTop);
201
202 // Move uid to the new procState.
203 mStateUidMap[it->second].erase(uid);
204 mStateUidMap[procState].insert(uid);
205 it->second = procState;
206
207 if (topUidSetChanged) {
208 updateTopUid_l();
209
210 // Make a copy of the uid set for callback.
211 topUids = mStateUidMap[mTopUidState];
212 }
213 }
214 }
215
216 ALOGV("topUidSetChanged: %d", topUidSetChanged);
217
218 if (topUidSetChanged) {
219 auto callback = mUidPolicyCallback.lock();
220 if (callback != nullptr) {
221 callback->onTopUidsChanged(topUids);
222 }
223 }
224}
225
226void TranscodingUidPolicy::updateTopUid_l() {
Chong Zhangacb33502020-04-20 11:04:48 -0700227 mTopUidState = ActivityManager::PROCESS_STATE_UNKNOWN;
Chong Zhang75222182020-04-29 14:43:42 -0700228
229 // Find the lowest uid state (ignoring PROCESS_STATE_UNKNOWN) with some monitored uids.
Chong Zhangacb33502020-04-20 11:04:48 -0700230 for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) {
231 if (stateIt->first != ActivityManager::PROCESS_STATE_UNKNOWN && !stateIt->second.empty()) {
232 mTopUidState = stateIt->first;
233 break;
234 }
235 }
236
237 ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState);
238}
239
240int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) {
241 auto it = mUidStateMap.find(uid);
242 if (it != mUidStateMap.end()) {
243 return it->second;
244 }
245 return ActivityManager::PROCESS_STATE_UNKNOWN;
246}
247
248} // namespace android