David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, versionCode 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 | */ |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 16 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 19 | #include "stats_log_util.h" |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 20 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "packages/UidMap.h" |
| 22 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 23 | #include <android/os/IStatsCompanionService.h> |
| 24 | #include <binder/IServiceManager.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
| 28 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 29 | using namespace android; |
| 30 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 31 | using android::base::StringPrintf; |
| 32 | using android::util::FIELD_COUNT_REPEATED; |
| 33 | using android::util::FIELD_TYPE_BOOL; |
| 34 | using android::util::FIELD_TYPE_FLOAT; |
| 35 | using android::util::FIELD_TYPE_INT32; |
| 36 | using android::util::FIELD_TYPE_INT64; |
| 37 | using android::util::FIELD_TYPE_MESSAGE; |
| 38 | using android::util::FIELD_TYPE_STRING; |
| 39 | using android::util::ProtoOutputStream; |
| 40 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace os { |
| 43 | namespace statsd { |
| 44 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 45 | const int FIELD_ID_SNAPSHOT_PACKAGE_NAME = 1; |
| 46 | const int FIELD_ID_SNAPSHOT_PACKAGE_VERSION = 2; |
| 47 | const int FIELD_ID_SNAPSHOT_PACKAGE_UID = 3; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 48 | const int FIELD_ID_SNAPSHOT_PACKAGE_DELETED = 4; |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 49 | const int FIELD_ID_SNAPSHOT_TIMESTAMP = 1; |
| 50 | const int FIELD_ID_SNAPSHOT_PACKAGE_INFO = 2; |
| 51 | const int FIELD_ID_SNAPSHOTS = 1; |
| 52 | const int FIELD_ID_CHANGES = 2; |
| 53 | const int FIELD_ID_CHANGE_DELETION = 1; |
| 54 | const int FIELD_ID_CHANGE_TIMESTAMP = 2; |
| 55 | const int FIELD_ID_CHANGE_PACKAGE = 3; |
| 56 | const int FIELD_ID_CHANGE_UID = 4; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 57 | const int FIELD_ID_CHANGE_NEW_VERSION = 5; |
| 58 | const int FIELD_ID_CHANGE_PREV_VERSION = 6; |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 59 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 60 | UidMap::UidMap() : mBytesUsed(0) {} |
| 61 | |
| 62 | UidMap::~UidMap() {} |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 63 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 64 | bool UidMap::hasApp(int uid, const string& packageName) const { |
| 65 | lock_guard<mutex> lock(mMutex); |
| 66 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 67 | auto it = mMap.find(std::make_pair(uid, packageName)); |
| 68 | return it != mMap.end() && !it->second.deleted; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 71 | string UidMap::normalizeAppName(const string& appName) const { |
| 72 | string normalizedName = appName; |
| 73 | std::transform(normalizedName.begin(), normalizedName.end(), normalizedName.begin(), ::tolower); |
| 74 | return normalizedName; |
| 75 | } |
| 76 | |
| 77 | std::set<string> UidMap::getAppNamesFromUid(const int32_t& uid, bool returnNormalized) const { |
| 78 | lock_guard<mutex> lock(mMutex); |
| 79 | return getAppNamesFromUidLocked(uid,returnNormalized); |
| 80 | } |
| 81 | |
| 82 | std::set<string> UidMap::getAppNamesFromUidLocked(const int32_t& uid, bool returnNormalized) const { |
| 83 | std::set<string> names; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 84 | for (const auto& kv : mMap) { |
| 85 | if (kv.first.first == uid && !kv.second.deleted) { |
| 86 | names.insert(returnNormalized ? normalizeAppName(kv.first.second) : kv.first.second); |
| 87 | } |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 88 | } |
| 89 | return names; |
| 90 | } |
| 91 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 92 | int64_t UidMap::getAppVersion(int uid, const string& packageName) const { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 93 | lock_guard<mutex> lock(mMutex); |
| 94 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 95 | auto it = mMap.find(std::make_pair(uid, packageName)); |
| 96 | if (it == mMap.end() || it->second.deleted) { |
| 97 | return 0; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 98 | } |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 99 | return it->second.versionCode; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 103 | const vector<int64_t>& versionCode, const vector<String16>& packageName) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 104 | vector<wp<PackageInfoListener>> broadcastList; |
| 105 | { |
| 106 | lock_guard<mutex> lock(mMutex); // Exclusively lock for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 107 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 108 | std::unordered_map<std::pair<int, string>, AppData, PairHash> deletedApps; |
| 109 | |
| 110 | // Copy all the deleted apps. |
| 111 | for (const auto& kv : mMap) { |
| 112 | if (kv.second.deleted) { |
| 113 | deletedApps[kv.first] = kv.second; |
| 114 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 115 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 116 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 117 | mMap.clear(); |
| 118 | for (size_t j = 0; j < uid.size(); j++) { |
| 119 | string package = string(String8(packageName[j]).string()); |
| 120 | mMap[std::make_pair(uid[j], package)] = AppData(versionCode[j]); |
| 121 | } |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 122 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 123 | for (const auto& kv : deletedApps) { |
| 124 | auto mMapIt = mMap.find(kv.first); |
| 125 | if (mMapIt != mMap.end()) { |
| 126 | // Insert this deleted app back into the current map. |
| 127 | mMap[kv.first] = kv.second; |
| 128 | } |
| 129 | } |
| 130 | |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 131 | ensureBytesUsedBelowLimit(); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 132 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 133 | getListenerListCopyLocked(&broadcastList); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 134 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 135 | // To avoid invoking callback while holding the internal lock. we get a copy of the listener |
| 136 | // list and invoke the callback. It's still possible that after we copy the list, a |
| 137 | // listener removes itself before we call it. It's then the listener's job to handle it (expect |
| 138 | // the callback to be called after listener is removed, and the listener should properly |
| 139 | // ignore it). |
| 140 | for (auto weakPtr : broadcastList) { |
| 141 | auto strongPtr = weakPtr.promote(); |
| 142 | if (strongPtr != NULL) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 143 | strongPtr->onUidMapReceived(timestamp); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 146 | } |
| 147 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 148 | void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid, |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 149 | const int64_t& versionCode) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 150 | vector<wp<PackageInfoListener>> broadcastList; |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 151 | string appName = string(String8(app_16).string()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 152 | { |
| 153 | lock_guard<mutex> lock(mMutex); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 154 | int32_t prevVersion = 0; |
| 155 | bool found = false; |
| 156 | auto it = mMap.find(std::make_pair(uid, appName)); |
| 157 | if (it != mMap.end()) { |
| 158 | found = true; |
| 159 | prevVersion = it->second.versionCode; |
| 160 | it->second.versionCode = versionCode; |
| 161 | it->second.deleted = false; |
| 162 | } |
| 163 | if (!found) { |
| 164 | // Otherwise, we need to add an app at this uid. |
| 165 | mMap[std::make_pair(uid, appName)] = AppData(versionCode); |
| 166 | } else { |
| 167 | // Only notify the listeners if this is an app upgrade. If this app is being installed |
| 168 | // for the first time, then we don't notify the listeners. |
| 169 | getListenerListCopyLocked(&broadcastList); |
| 170 | } |
| 171 | mChanges.emplace_back(false, timestamp, appName, uid, versionCode, prevVersion); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 172 | mBytesUsed += kBytesChangeRecord; |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 173 | ensureBytesUsedBelowLimit(); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 174 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 175 | StatsdStats::getInstance().setUidMapChanges(mChanges.size()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 178 | for (auto weakPtr : broadcastList) { |
| 179 | auto strongPtr = weakPtr.promote(); |
| 180 | if (strongPtr != NULL) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 181 | strongPtr->notifyAppUpgrade(timestamp, appName, uid, versionCode); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 182 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 183 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 184 | } |
| 185 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 186 | void UidMap::ensureBytesUsedBelowLimit() { |
| 187 | size_t limit; |
| 188 | if (maxBytesOverride <= 0) { |
| 189 | limit = StatsdStats::kMaxBytesUsedUidMap; |
| 190 | } else { |
| 191 | limit = maxBytesOverride; |
| 192 | } |
| 193 | while (mBytesUsed > limit) { |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 194 | ALOGI("Bytes used %zu is above limit %zu, need to delete something", mBytesUsed, limit); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 195 | if (mChanges.size() > 0) { |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 196 | mBytesUsed -= kBytesChangeRecord; |
| 197 | mChanges.pop_front(); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 198 | StatsdStats::getInstance().noteUidMapDropped(1); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 199 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 203 | void UidMap::getListenerListCopyLocked(vector<wp<PackageInfoListener>>* output) { |
| 204 | for (auto weakIt = mSubscribers.begin(); weakIt != mSubscribers.end();) { |
| 205 | auto strongPtr = weakIt->promote(); |
| 206 | if (strongPtr != NULL) { |
| 207 | output->push_back(*weakIt); |
| 208 | weakIt++; |
| 209 | } else { |
| 210 | weakIt = mSubscribers.erase(weakIt); |
| 211 | VLOG("The UidMap listener is gone, remove it now"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 216 | void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid) { |
| 217 | vector<wp<PackageInfoListener>> broadcastList; |
| 218 | string app = string(String8(app_16).string()); |
| 219 | { |
| 220 | lock_guard<mutex> lock(mMutex); |
| 221 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 222 | int32_t prevVersion = 0; |
| 223 | auto key = std::make_pair(uid, app); |
| 224 | auto it = mMap.find(key); |
| 225 | if (it != mMap.end() && !it->second.deleted) { |
| 226 | prevVersion = it->second.versionCode; |
| 227 | it->second.deleted = true; |
| 228 | mDeletedApps.push_back(key); |
| 229 | } |
| 230 | if (mDeletedApps.size() > StatsdStats::kMaxDeletedAppsInUidMap) { |
| 231 | // Delete the oldest one. |
| 232 | auto oldest = mDeletedApps.front(); |
| 233 | mDeletedApps.pop_front(); |
| 234 | mMap.erase(oldest); |
| 235 | StatsdStats::getInstance().noteUidMapAppDeletionDropped(); |
| 236 | } |
| 237 | mChanges.emplace_back(true, timestamp, app, uid, 0, prevVersion); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 238 | mBytesUsed += kBytesChangeRecord; |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 239 | ensureBytesUsedBelowLimit(); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 240 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 241 | StatsdStats::getInstance().setUidMapChanges(mChanges.size()); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 242 | getListenerListCopyLocked(&broadcastList); |
| 243 | } |
| 244 | |
| 245 | for (auto weakPtr : broadcastList) { |
| 246 | auto strongPtr = weakPtr.promote(); |
| 247 | if (strongPtr != NULL) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 248 | strongPtr->notifyAppRemoved(timestamp, app, uid); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void UidMap::addListener(wp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 254 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 255 | mSubscribers.insert(producer); |
| 256 | } |
| 257 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 258 | void UidMap::removeListener(wp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 259 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 260 | mSubscribers.erase(producer); |
| 261 | } |
| 262 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 263 | void UidMap::assignIsolatedUid(int isolatedUid, int parentUid) { |
| 264 | lock_guard<mutex> lock(mIsolatedMutex); |
| 265 | |
| 266 | mIsolatedUidMap[isolatedUid] = parentUid; |
| 267 | } |
| 268 | |
| 269 | void UidMap::removeIsolatedUid(int isolatedUid, int parentUid) { |
| 270 | lock_guard<mutex> lock(mIsolatedMutex); |
| 271 | |
| 272 | auto it = mIsolatedUidMap.find(isolatedUid); |
| 273 | if (it != mIsolatedUidMap.end()) { |
| 274 | mIsolatedUidMap.erase(it); |
| 275 | } |
| 276 | } |
| 277 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 278 | int UidMap::getHostUidOrSelf(int uid) const { |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 279 | lock_guard<mutex> lock(mIsolatedMutex); |
| 280 | |
| 281 | auto it = mIsolatedUidMap.find(uid); |
| 282 | if (it != mIsolatedUidMap.end()) { |
| 283 | return it->second; |
| 284 | } |
| 285 | return uid; |
| 286 | } |
| 287 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 288 | void UidMap::clearOutput() { |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 289 | mChanges.clear(); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 290 | // Also update the guardrail trackers. |
| 291 | StatsdStats::getInstance().setUidMapChanges(0); |
David Chen | cfc311d | 2018-01-23 17:55:54 -0800 | [diff] [blame] | 292 | mBytesUsed = 0; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 293 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 294 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 295 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 296 | int64_t UidMap::getMinimumTimestampNs() { |
| 297 | int64_t m = 0; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 298 | for (const auto& kv : mLastUpdatePerConfigKey) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 299 | if (m == 0) { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 300 | m = kv.second; |
| 301 | } else if (kv.second < m) { |
| 302 | m = kv.second; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | return m; |
| 306 | } |
| 307 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 308 | size_t UidMap::getBytesUsed() const { |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 309 | return mBytesUsed; |
| 310 | } |
| 311 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 312 | void UidMap::appendUidMap(const int64_t& timestamp, const ConfigKey& key, |
| 313 | ProtoOutputStream* proto) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 314 | lock_guard<mutex> lock(mMutex); // Lock for updates |
| 315 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 316 | for (const ChangeRecord& record : mChanges) { |
| 317 | if (record.timestampNs > mLastUpdatePerConfigKey[key]) { |
| 318 | uint64_t changesToken = |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 319 | proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CHANGES); |
| 320 | proto->write(FIELD_TYPE_BOOL | FIELD_ID_CHANGE_DELETION, (bool)record.deletion); |
| 321 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_TIMESTAMP, |
| 322 | (long long)record.timestampNs); |
| 323 | proto->write(FIELD_TYPE_STRING | FIELD_ID_CHANGE_PACKAGE, record.package); |
| 324 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_UID, (int)record.uid); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 325 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_NEW_VERSION, (int)record.version); |
| 326 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_PREV_VERSION, (int)record.prevVersion); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 327 | proto->end(changesToken); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 331 | // Write snapshot from current uid map state. |
| 332 | uint64_t snapshotsToken = |
| 333 | proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SNAPSHOTS); |
| 334 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_SNAPSHOT_TIMESTAMP, (long long)timestamp); |
| 335 | for (const auto& kv : mMap) { |
| 336 | uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 337 | FIELD_ID_SNAPSHOT_PACKAGE_INFO); |
| 338 | proto->write(FIELD_TYPE_STRING | FIELD_ID_SNAPSHOT_PACKAGE_NAME, kv.first.second); |
| 339 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_SNAPSHOT_PACKAGE_VERSION, |
| 340 | (int)kv.second.versionCode); |
| 341 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_SNAPSHOT_PACKAGE_UID, kv.first.first); |
| 342 | proto->write(FIELD_TYPE_BOOL | FIELD_ID_SNAPSHOT_PACKAGE_DELETED, kv.second.deleted); |
| 343 | proto->end(token); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 344 | } |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 345 | proto->end(snapshotsToken); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 346 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 347 | int64_t prevMin = getMinimumTimestampNs(); |
| 348 | mLastUpdatePerConfigKey[key] = timestamp; |
| 349 | int64_t newMin = getMinimumTimestampNs(); |
| 350 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 351 | if (newMin > prevMin) { // Delete anything possible now that the minimum has |
| 352 | // moved forward. |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 353 | int64_t cutoff_nanos = newMin; |
Yao Chen | 34900c3 | 2018-03-19 13:43:29 -0700 | [diff] [blame] | 354 | for (auto it_changes = mChanges.begin(); it_changes != mChanges.end();) { |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 355 | if (it_changes->timestampNs < cutoff_nanos) { |
| 356 | mBytesUsed -= kBytesChangeRecord; |
Yao Chen | 34900c3 | 2018-03-19 13:43:29 -0700 | [diff] [blame] | 357 | it_changes = mChanges.erase(it_changes); |
| 358 | } else { |
| 359 | ++it_changes; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 363 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 364 | StatsdStats::getInstance().setUidMapChanges(mChanges.size()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 367 | void UidMap::printUidMap(FILE* out) const { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 368 | lock_guard<mutex> lock(mMutex); |
| 369 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 370 | for (const auto& kv : mMap) { |
| 371 | if (!kv.second.deleted) { |
| 372 | fprintf(out, "%s, v%" PRId64 " (%i)\n", kv.first.second.c_str(), kv.second.versionCode, |
| 373 | kv.first.first); |
| 374 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 378 | void UidMap::OnConfigUpdated(const ConfigKey& key) { |
| 379 | mLastUpdatePerConfigKey[key] = -1; |
| 380 | } |
| 381 | |
| 382 | void UidMap::OnConfigRemoved(const ConfigKey& key) { |
| 383 | mLastUpdatePerConfigKey.erase(key); |
| 384 | } |
| 385 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 386 | set<int32_t> UidMap::getAppUid(const string& package) const { |
| 387 | lock_guard<mutex> lock(mMutex); |
| 388 | |
| 389 | set<int32_t> results; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 390 | for (const auto& kv : mMap) { |
| 391 | if (kv.first.second == package && !kv.second.deleted) { |
| 392 | results.insert(kv.first.first); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | return results; |
| 396 | } |
| 397 | |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 398 | // Note not all the following AIDs are used as uids. Some are used only for gids. |
| 399 | // It's ok to leave them in the map, but we won't ever see them in the log's uid field. |
| 400 | // App's uid starts from 10000, and will not overlap with the following AIDs. |
| 401 | const std::map<string, uint32_t> UidMap::sAidToUidMapping = {{"AID_ROOT", 0}, |
| 402 | {"AID_SYSTEM", 1000}, |
| 403 | {"AID_RADIO", 1001}, |
| 404 | {"AID_BLUETOOTH", 1002}, |
| 405 | {"AID_GRAPHICS", 1003}, |
| 406 | {"AID_INPUT", 1004}, |
| 407 | {"AID_AUDIO", 1005}, |
| 408 | {"AID_CAMERA", 1006}, |
| 409 | {"AID_LOG", 1007}, |
| 410 | {"AID_COMPASS", 1008}, |
| 411 | {"AID_MOUNT", 1009}, |
| 412 | {"AID_WIFI", 1010}, |
| 413 | {"AID_ADB", 1011}, |
| 414 | {"AID_INSTALL", 1012}, |
| 415 | {"AID_MEDIA", 1013}, |
| 416 | {"AID_DHCP", 1014}, |
| 417 | {"AID_SDCARD_RW", 1015}, |
| 418 | {"AID_VPN", 1016}, |
| 419 | {"AID_KEYSTORE", 1017}, |
| 420 | {"AID_USB", 1018}, |
| 421 | {"AID_DRM", 1019}, |
| 422 | {"AID_MDNSR", 1020}, |
| 423 | {"AID_GPS", 1021}, |
| 424 | // {"AID_UNUSED1", 1022}, |
| 425 | {"AID_MEDIA_RW", 1023}, |
| 426 | {"AID_MTP", 1024}, |
| 427 | // {"AID_UNUSED2", 1025}, |
| 428 | {"AID_DRMRPC", 1026}, |
| 429 | {"AID_NFC", 1027}, |
| 430 | {"AID_SDCARD_R", 1028}, |
| 431 | {"AID_CLAT", 1029}, |
| 432 | {"AID_LOOP_RADIO", 1030}, |
| 433 | {"AID_MEDIA_DRM", 1031}, |
| 434 | {"AID_PACKAGE_INFO", 1032}, |
| 435 | {"AID_SDCARD_PICS", 1033}, |
| 436 | {"AID_SDCARD_AV", 1034}, |
| 437 | {"AID_SDCARD_ALL", 1035}, |
| 438 | {"AID_LOGD", 1036}, |
| 439 | {"AID_SHARED_RELRO", 1037}, |
| 440 | {"AID_DBUS", 1038}, |
| 441 | {"AID_TLSDATE", 1039}, |
| 442 | {"AID_MEDIA_EX", 1040}, |
| 443 | {"AID_AUDIOSERVER", 1041}, |
| 444 | {"AID_METRICS_COLL", 1042}, |
| 445 | {"AID_METRICSD", 1043}, |
| 446 | {"AID_WEBSERV", 1044}, |
| 447 | {"AID_DEBUGGERD", 1045}, |
| 448 | {"AID_MEDIA_CODEC", 1046}, |
| 449 | {"AID_CAMERASERVER", 1047}, |
| 450 | {"AID_FIREWALL", 1048}, |
| 451 | {"AID_TRUNKS", 1049}, |
| 452 | {"AID_NVRAM", 1050}, |
| 453 | {"AID_DNS", 1051}, |
| 454 | {"AID_DNS_TETHER", 1052}, |
| 455 | {"AID_WEBVIEW_ZYGOTE", 1053}, |
| 456 | {"AID_VEHICLE_NETWORK", 1054}, |
| 457 | {"AID_MEDIA_AUDIO", 1055}, |
| 458 | {"AID_MEDIA_VIDEO", 1056}, |
| 459 | {"AID_MEDIA_IMAGE", 1057}, |
| 460 | {"AID_TOMBSTONED", 1058}, |
| 461 | {"AID_MEDIA_OBB", 1059}, |
| 462 | {"AID_ESE", 1060}, |
| 463 | {"AID_OTA_UPDATE", 1061}, |
| 464 | {"AID_AUTOMOTIVE_EVS", 1062}, |
| 465 | {"AID_LOWPAN", 1063}, |
| 466 | {"AID_HSM", 1064}, |
Yao Chen | 29f79b5 | 2018-01-17 10:56:48 -0800 | [diff] [blame] | 467 | {"AID_RESERVED_DISK", 1065}, |
| 468 | {"AID_STATSD", 1066}, |
| 469 | {"AID_INCIDENTD", 1067}, |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 470 | {"AID_SHELL", 2000}, |
| 471 | {"AID_CACHE", 2001}, |
| 472 | {"AID_DIAG", 2002}}; |
| 473 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 474 | } // namespace statsd |
| 475 | } // namespace os |
Yao Chen | 29f79b5 | 2018-01-17 10:56:48 -0800 | [diff] [blame] | 476 | } // namespace android |