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 | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 20 | #include <aidl/android/media/BnResourceManagerClient.h> |
| 21 | #include <aidl/android/media/IResourceManagerService.h> |
| 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 24 | #include <binder/ActivityManager.h> |
| 25 | #include <cutils/misc.h> // FIRST_APPLICATION_UID |
Chong Zhang | c37bdfe | 2020-10-06 13:54:09 -0700 | [diff] [blame] | 26 | #include <cutils/multiuser.h> |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
| 28 | #include <media/TranscodingUidPolicy.h> |
| 29 | #include <utils/Log.h> |
| 30 | |
| 31 | #include <utility> |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | constexpr static uid_t OFFLINE_UID = -1; |
| 36 | constexpr static const char* kTranscodingTag = "transcoding"; |
| 37 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 38 | /* |
| 39 | * The OOM score we're going to ask ResourceManager to use for our native transcoding |
| 40 | * service. ResourceManager issues reclaims based on these scores. It gets the scores |
| 41 | * from ActivityManagerService, which doesn't track native services. The values of the |
| 42 | * OOM scores are defined in: |
| 43 | * frameworks/base/services/core/java/com/android/server/am/ProcessList.java |
| 44 | * We use SERVICE_ADJ which is lower priority than an app possibly visible to the |
| 45 | * user, but higher priority than a cached app (which could be killed without disruption |
| 46 | * to the user). |
| 47 | */ |
| 48 | constexpr static int32_t SERVICE_ADJ = 500; |
| 49 | |
| 50 | using Status = ::ndk::ScopedAStatus; |
| 51 | using aidl::android::media::BnResourceManagerClient; |
| 52 | using aidl::android::media::IResourceManagerService; |
| 53 | |
| 54 | /* |
| 55 | * Placeholder ResourceManagerClient for registering process info override |
| 56 | * with the IResourceManagerService. This is only used as a token by the service |
| 57 | * to get notifications about binder death, not used for reclaiming resources. |
| 58 | */ |
| 59 | struct TranscodingUidPolicy::ResourceManagerClient : public BnResourceManagerClient { |
| 60 | explicit ResourceManagerClient() = default; |
| 61 | |
| 62 | Status reclaimResource(bool* _aidl_return) override { |
| 63 | *_aidl_return = false; |
| 64 | return Status::ok(); |
| 65 | } |
| 66 | |
| 67 | Status getName(::std::string* _aidl_return) override { |
| 68 | _aidl_return->clear(); |
| 69 | return Status::ok(); |
| 70 | } |
| 71 | |
| 72 | virtual ~ResourceManagerClient() = default; |
| 73 | }; |
| 74 | |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 75 | struct TranscodingUidPolicy::UidObserver : public BnUidObserver, |
| 76 | public virtual IBinder::DeathRecipient { |
| 77 | explicit UidObserver(TranscodingUidPolicy* owner) : mOwner(owner) {} |
| 78 | |
| 79 | // IUidObserver |
| 80 | void onUidGone(uid_t uid, bool disabled) override; |
| 81 | void onUidActive(uid_t uid) override; |
| 82 | void onUidIdle(uid_t uid, bool disabled) override; |
| 83 | void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq, |
| 84 | int32_t capability) override; |
| 85 | |
| 86 | // IBinder::DeathRecipient implementation |
| 87 | void binderDied(const wp<IBinder>& who) override; |
| 88 | |
| 89 | TranscodingUidPolicy* mOwner; |
| 90 | }; |
| 91 | |
| 92 | void TranscodingUidPolicy::UidObserver::onUidGone(uid_t uid __unused, bool disabled __unused) {} |
| 93 | |
| 94 | void TranscodingUidPolicy::UidObserver::onUidActive(uid_t uid __unused) {} |
| 95 | |
| 96 | void TranscodingUidPolicy::UidObserver::onUidIdle(uid_t uid __unused, bool disabled __unused) {} |
| 97 | |
| 98 | void TranscodingUidPolicy::UidObserver::onUidStateChanged(uid_t uid, int32_t procState, |
| 99 | int64_t procStateSeq __unused, |
| 100 | int32_t capability __unused) { |
| 101 | mOwner->onUidStateChanged(uid, procState); |
| 102 | } |
| 103 | |
| 104 | void TranscodingUidPolicy::UidObserver::binderDied(const wp<IBinder>& /*who*/) { |
| 105 | ALOGW("TranscodingUidPolicy: ActivityManager has died"); |
| 106 | // TODO(chz): this is a rare event (since if the AMS is dead, the system is |
| 107 | // probably dead as well). But we should try to reconnect. |
| 108 | mOwner->setUidObserverRegistered(false); |
| 109 | } |
| 110 | |
| 111 | //////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | TranscodingUidPolicy::TranscodingUidPolicy() |
| 114 | : mAm(std::make_shared<ActivityManager>()), |
| 115 | mUidObserver(new UidObserver(this)), |
| 116 | mRegistered(false), |
| 117 | mTopUidState(ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 118 | registerSelf(); |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 119 | setProcessInfoOverride(); |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TranscodingUidPolicy::~TranscodingUidPolicy() { |
| 123 | unregisterSelf(); |
| 124 | } |
| 125 | |
| 126 | void TranscodingUidPolicy::registerSelf() { |
| 127 | status_t res = mAm->linkToDeath(mUidObserver.get()); |
| 128 | mAm->registerUidObserver( |
| 129 | mUidObserver.get(), |
| 130 | ActivityManager::UID_OBSERVER_GONE | ActivityManager::UID_OBSERVER_IDLE | |
| 131 | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE, |
| 132 | ActivityManager::PROCESS_STATE_UNKNOWN, String16(kTranscodingTag)); |
| 133 | |
| 134 | if (res == OK) { |
| 135 | Mutex::Autolock _l(mUidLock); |
| 136 | |
| 137 | mRegistered = true; |
| 138 | ALOGI("TranscodingUidPolicy: Registered with ActivityManager"); |
| 139 | } else { |
| 140 | mAm->unregisterUidObserver(mUidObserver.get()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void TranscodingUidPolicy::unregisterSelf() { |
| 145 | mAm->unregisterUidObserver(mUidObserver.get()); |
| 146 | mAm->unlinkToDeath(mUidObserver.get()); |
| 147 | |
| 148 | Mutex::Autolock _l(mUidLock); |
| 149 | |
| 150 | mRegistered = false; |
| 151 | |
| 152 | ALOGI("TranscodingUidPolicy: Unregistered with ActivityManager"); |
| 153 | } |
| 154 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 155 | void TranscodingUidPolicy::setProcessInfoOverride() { |
| 156 | ::ndk::SpAIBinder binder(AServiceManager_getService("media.resource_manager")); |
| 157 | std::shared_ptr<IResourceManagerService> service = IResourceManagerService::fromBinder(binder); |
| 158 | if (service == nullptr) { |
| 159 | ALOGE("Failed to get IResourceManagerService"); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | mProcInfoOverrideClient = ::ndk::SharedRefBase::make<ResourceManagerClient>(); |
| 164 | Status status = service->overrideProcessInfo( |
| 165 | mProcInfoOverrideClient, getpid(), ActivityManager::PROCESS_STATE_SERVICE, SERVICE_ADJ); |
| 166 | if (!status.isOk()) { |
| 167 | ALOGW("Failed to setProcessInfoOverride."); |
| 168 | } |
| 169 | } |
| 170 | |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 171 | void TranscodingUidPolicy::setUidObserverRegistered(bool registered) { |
| 172 | Mutex::Autolock _l(mUidLock); |
| 173 | |
| 174 | mRegistered = registered; |
| 175 | } |
| 176 | |
| 177 | void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) { |
| 178 | mUidPolicyCallback = cb; |
| 179 | } |
| 180 | |
| 181 | void TranscodingUidPolicy::registerMonitorUid(uid_t uid) { |
| 182 | Mutex::Autolock _l(mUidLock); |
| 183 | if (uid == OFFLINE_UID) { |
| 184 | ALOGW("Ignoring the offline uid"); |
| 185 | return; |
| 186 | } |
| 187 | if (mUidStateMap.find(uid) != mUidStateMap.end()) { |
| 188 | ALOGE("%s: Trying to register uid: %d which is already monitored!", __FUNCTION__, uid); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | int32_t state = ActivityManager::PROCESS_STATE_UNKNOWN; |
Hui Yu | 799ba4f | 2020-05-05 17:04:15 -0700 | [diff] [blame] | 193 | if (mRegistered && mAm->isUidActive(uid, String16(kTranscodingTag))) { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 194 | state = mAm->getUidProcessState(uid, String16(kTranscodingTag)); |
| 195 | } |
| 196 | |
| 197 | ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state); |
| 198 | |
| 199 | mUidStateMap.emplace(std::pair<uid_t, int32_t>(uid, state)); |
| 200 | mStateUidMap[state].insert(uid); |
| 201 | |
| 202 | updateTopUid_l(); |
| 203 | } |
| 204 | |
| 205 | void TranscodingUidPolicy::unregisterMonitorUid(uid_t uid) { |
| 206 | Mutex::Autolock _l(mUidLock); |
| 207 | |
| 208 | auto it = mUidStateMap.find(uid); |
| 209 | if (it == mUidStateMap.end()) { |
| 210 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | auto stateIt = mStateUidMap.find(it->second); |
| 215 | if (stateIt != mStateUidMap.end()) { |
| 216 | stateIt->second.erase(uid); |
| 217 | if (stateIt->second.empty()) { |
| 218 | mStateUidMap.erase(stateIt); |
| 219 | } |
| 220 | } |
| 221 | mUidStateMap.erase(it); |
| 222 | |
| 223 | updateTopUid_l(); |
| 224 | } |
| 225 | |
| 226 | bool TranscodingUidPolicy::isUidOnTop(uid_t uid) { |
| 227 | Mutex::Autolock _l(mUidLock); |
| 228 | |
| 229 | return mTopUidState != ActivityManager::PROCESS_STATE_UNKNOWN && |
| 230 | mTopUidState == getProcState_l(uid); |
| 231 | } |
| 232 | |
| 233 | std::unordered_set<uid_t> TranscodingUidPolicy::getTopUids() const { |
| 234 | Mutex::Autolock _l(mUidLock); |
| 235 | |
| 236 | if (mTopUidState == ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 237 | return std::unordered_set<uid_t>(); |
| 238 | } |
| 239 | |
| 240 | return mStateUidMap.at(mTopUidState); |
| 241 | } |
| 242 | |
| 243 | void TranscodingUidPolicy::onUidStateChanged(uid_t uid, int32_t procState) { |
| 244 | ALOGV("onUidStateChanged: %u, procState %d", uid, procState); |
| 245 | |
| 246 | bool topUidSetChanged = false; |
| 247 | std::unordered_set<uid_t> topUids; |
| 248 | { |
| 249 | Mutex::Autolock _l(mUidLock); |
| 250 | auto it = mUidStateMap.find(uid); |
| 251 | if (it != mUidStateMap.end() && it->second != procState) { |
| 252 | // Top set changed if 1) the uid is in the current top uid set, or 2) the |
| 253 | // new procState is at least the same priority as the current top uid state. |
| 254 | bool isUidCurrentTop = mTopUidState != ActivityManager::PROCESS_STATE_UNKNOWN && |
| 255 | mStateUidMap[mTopUidState].count(uid) > 0; |
| 256 | bool isNewStateHigherThanTop = procState != ActivityManager::PROCESS_STATE_UNKNOWN && |
| 257 | (procState <= mTopUidState || |
| 258 | mTopUidState == ActivityManager::PROCESS_STATE_UNKNOWN); |
| 259 | topUidSetChanged = (isUidCurrentTop || isNewStateHigherThanTop); |
| 260 | |
| 261 | // Move uid to the new procState. |
| 262 | mStateUidMap[it->second].erase(uid); |
| 263 | mStateUidMap[procState].insert(uid); |
| 264 | it->second = procState; |
| 265 | |
| 266 | if (topUidSetChanged) { |
| 267 | updateTopUid_l(); |
| 268 | |
| 269 | // Make a copy of the uid set for callback. |
| 270 | topUids = mStateUidMap[mTopUidState]; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | ALOGV("topUidSetChanged: %d", topUidSetChanged); |
| 276 | |
| 277 | if (topUidSetChanged) { |
| 278 | auto callback = mUidPolicyCallback.lock(); |
| 279 | if (callback != nullptr) { |
| 280 | callback->onTopUidsChanged(topUids); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void TranscodingUidPolicy::updateTopUid_l() { |
Chong Zhang | acb3350 | 2020-04-20 11:04:48 -0700 | [diff] [blame] | 286 | mTopUidState = ActivityManager::PROCESS_STATE_UNKNOWN; |
Chong Zhang | 7522218 | 2020-04-29 14:43:42 -0700 | [diff] [blame] | 287 | |
| 288 | // 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] | 289 | for (auto stateIt = mStateUidMap.begin(); stateIt != mStateUidMap.end(); stateIt++) { |
| 290 | if (stateIt->first != ActivityManager::PROCESS_STATE_UNKNOWN && !stateIt->second.empty()) { |
| 291 | mTopUidState = stateIt->first; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | ALOGV("%s: top uid state is %d", __FUNCTION__, mTopUidState); |
| 297 | } |
| 298 | |
| 299 | int32_t TranscodingUidPolicy::getProcState_l(uid_t uid) { |
| 300 | auto it = mUidStateMap.find(uid); |
| 301 | if (it != mUidStateMap.end()) { |
| 302 | return it->second; |
| 303 | } |
| 304 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 305 | } |
| 306 | |
| 307 | } // namespace android |