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 | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 16 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 19 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "packages/UidMap.h" |
| 21 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 22 | #include <android/os/IStatsCompanionService.h> |
| 23 | #include <binder/IServiceManager.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 24 | #include <utils/Errors.h> |
| 25 | |
| 26 | using namespace android; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 32 | UidMap::UidMap() : mBytesUsed(0) { |
| 33 | } |
| 34 | UidMap::~UidMap() { |
| 35 | } |
| 36 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 37 | bool UidMap::hasApp(int uid, const string& packageName) const { |
| 38 | lock_guard<mutex> lock(mMutex); |
| 39 | |
| 40 | auto range = mMap.equal_range(uid); |
| 41 | for (auto it = range.first; it != range.second; ++it) { |
| 42 | if (it->second.packageName == packageName) { |
| 43 | return true; |
| 44 | } |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | int UidMap::getAppVersion(int uid, const string& packageName) const { |
| 50 | lock_guard<mutex> lock(mMutex); |
| 51 | |
| 52 | auto range = mMap.equal_range(uid); |
| 53 | for (auto it = range.first; it != range.second; ++it) { |
| 54 | if (it->second.packageName == packageName) { |
| 55 | return it->second.versionCode; |
| 56 | } |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 61 | void UidMap::updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode, |
| 62 | const vector<String16>& packageName) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 63 | updateMap(time(nullptr) * NS_PER_SEC, uid, versionCode, packageName); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
| 67 | const vector<int32_t>& versionCode, const vector<String16>& packageName) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 68 | lock_guard<mutex> lock(mMutex); // Exclusively lock for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 69 | |
| 70 | mMap.clear(); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 71 | for (size_t j = 0; j < uid.size(); j++) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 72 | mMap.insert(make_pair(uid[j], |
| 73 | AppData(string(String8(packageName[j]).string()), versionCode[j]))); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 76 | auto snapshot = mOutput.add_snapshots(); |
| 77 | snapshot->set_timestamp_nanos(timestamp); |
| 78 | for (size_t j = 0; j < uid.size(); j++) { |
| 79 | auto t = snapshot->add_package_info(); |
| 80 | t->set_name(string(String8(packageName[j]).string())); |
| 81 | t->set_version(int(versionCode[j])); |
| 82 | t->set_uid(uid[j]); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 83 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 84 | mBytesUsed += snapshot->ByteSize(); |
| 85 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
| 86 | StatsdStats::getInstance().setUidMapSnapshots(mOutput.snapshots_size()); |
| 87 | ensureBytesUsedBelowLimit(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 90 | void UidMap::updateApp(const String16& app_16, const int32_t& uid, const int32_t& versionCode) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 91 | updateApp(time(nullptr) * NS_PER_SEC, app_16, uid, versionCode); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid, |
| 95 | const int32_t& versionCode) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 96 | lock_guard<mutex> lock(mMutex); |
| 97 | |
| 98 | string app = string(String8(app_16).string()); |
| 99 | |
| 100 | // Notify any interested producers that this app has updated |
| 101 | for (auto it : mSubscribers) { |
| 102 | it->notifyAppUpgrade(app, uid, versionCode); |
| 103 | } |
| 104 | |
| 105 | auto log = mOutput.add_changes(); |
| 106 | log->set_deletion(false); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 107 | log->set_timestamp_nanos(timestamp); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 108 | log->set_app(app); |
| 109 | log->set_uid(uid); |
| 110 | log->set_version(versionCode); |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 111 | mBytesUsed += log->ByteSize(); |
| 112 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
| 113 | StatsdStats::getInstance().setUidMapChanges(mOutput.changes_size()); |
| 114 | ensureBytesUsedBelowLimit(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 115 | |
| 116 | auto range = mMap.equal_range(int(uid)); |
| 117 | for (auto it = range.first; it != range.second; ++it) { |
| 118 | if (it->second.packageName == app) { |
| 119 | it->second.versionCode = int(versionCode); |
| 120 | return; |
| 121 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 122 | VLOG("updateApp failed to find the app %s with uid %i to update", app.c_str(), uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Otherwise, we need to add an app at this uid. |
| 127 | mMap.insert(make_pair(uid, AppData(app, int(versionCode)))); |
| 128 | } |
| 129 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 130 | void UidMap::ensureBytesUsedBelowLimit() { |
| 131 | size_t limit; |
| 132 | if (maxBytesOverride <= 0) { |
| 133 | limit = StatsdStats::kMaxBytesUsedUidMap; |
| 134 | } else { |
| 135 | limit = maxBytesOverride; |
| 136 | } |
| 137 | while (mBytesUsed > limit) { |
| 138 | VLOG("Bytes used %zu is above limit %zu, need to delete something", mBytesUsed, limit); |
| 139 | if (mOutput.snapshots_size() > 0) { |
| 140 | auto snapshots = mOutput.mutable_snapshots(); |
| 141 | snapshots->erase(snapshots->begin()); // Remove first snapshot. |
| 142 | StatsdStats::getInstance().noteUidMapDropped(1, 0); |
| 143 | } else if (mOutput.changes_size() > 0) { |
| 144 | auto changes = mOutput.mutable_changes(); |
| 145 | changes->DeleteSubrange(0, 1); |
| 146 | StatsdStats::getInstance().noteUidMapDropped(0, 1); |
| 147 | } |
| 148 | mBytesUsed = mOutput.ByteSize(); |
| 149 | } |
| 150 | } |
| 151 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 152 | void UidMap::removeApp(const String16& app_16, const int32_t& uid) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 153 | removeApp(time(nullptr) * NS_PER_SEC, app_16, uid); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 154 | } |
| 155 | void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 156 | lock_guard<mutex> lock(mMutex); |
| 157 | |
| 158 | string app = string(String8(app_16).string()); |
| 159 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 160 | for (auto it : mSubscribers) { |
| 161 | it->notifyAppRemoved(app, uid); |
| 162 | } |
| 163 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 164 | auto log = mOutput.add_changes(); |
| 165 | log->set_deletion(true); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 166 | log->set_timestamp_nanos(timestamp); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 167 | log->set_app(app); |
| 168 | log->set_uid(uid); |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 169 | mBytesUsed += log->ByteSize(); |
| 170 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
| 171 | StatsdStats::getInstance().setUidMapChanges(mOutput.changes_size()); |
| 172 | ensureBytesUsedBelowLimit(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 173 | |
| 174 | auto range = mMap.equal_range(int(uid)); |
| 175 | for (auto it = range.first; it != range.second; ++it) { |
| 176 | if (it->second.packageName == app) { |
| 177 | mMap.erase(it); |
| 178 | return; |
| 179 | } |
| 180 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 181 | VLOG("removeApp failed to find the app %s with uid %i to remove", app.c_str(), uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | void UidMap::addListener(sp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 186 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 187 | mSubscribers.insert(producer); |
| 188 | } |
| 189 | |
| 190 | void UidMap::removeListener(sp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 191 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 192 | mSubscribers.erase(producer); |
| 193 | } |
| 194 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 195 | void UidMap::assignIsolatedUid(int isolatedUid, int parentUid) { |
| 196 | lock_guard<mutex> lock(mIsolatedMutex); |
| 197 | |
| 198 | mIsolatedUidMap[isolatedUid] = parentUid; |
| 199 | } |
| 200 | |
| 201 | void UidMap::removeIsolatedUid(int isolatedUid, int parentUid) { |
| 202 | lock_guard<mutex> lock(mIsolatedMutex); |
| 203 | |
| 204 | auto it = mIsolatedUidMap.find(isolatedUid); |
| 205 | if (it != mIsolatedUidMap.end()) { |
| 206 | mIsolatedUidMap.erase(it); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | int UidMap::getParentUidOrSelf(int uid) { |
| 211 | lock_guard<mutex> lock(mIsolatedMutex); |
| 212 | |
| 213 | auto it = mIsolatedUidMap.find(uid); |
| 214 | if (it != mIsolatedUidMap.end()) { |
| 215 | return it->second; |
| 216 | } |
| 217 | return uid; |
| 218 | } |
| 219 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 220 | void UidMap::clearOutput() { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 221 | mOutput.Clear(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 222 | // Re-initialize the initial state for the outputs. This results in extra data being uploaded |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 223 | // but helps ensure we can re-construct the UID->app name, versionCode mapping in server. |
| 224 | auto snapshot = mOutput.add_snapshots(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 225 | for (auto it : mMap) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 226 | auto t = snapshot->add_package_info(); |
| 227 | t->set_name(it.second.packageName); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 228 | t->set_version(it.second.versionCode); |
| 229 | t->set_uid(it.first); |
| 230 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 231 | |
| 232 | // Also update the guardrail trackers. |
| 233 | StatsdStats::getInstance().setUidMapChanges(0); |
| 234 | StatsdStats::getInstance().setUidMapSnapshots(1); |
| 235 | mBytesUsed = snapshot->ByteSize(); |
| 236 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 237 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 238 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 239 | int64_t UidMap::getMinimumTimestampNs() { |
| 240 | int64_t m = 0; |
| 241 | for (auto it : mLastUpdatePerConfigKey) { |
| 242 | if (m == 0) { |
| 243 | m = it.second; |
| 244 | } else if (it.second < m) { |
| 245 | m = it.second; |
| 246 | } |
| 247 | } |
| 248 | return m; |
| 249 | } |
| 250 | |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 251 | size_t UidMap::getBytesUsed() { |
| 252 | return mBytesUsed; |
| 253 | } |
| 254 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 255 | UidMapping UidMap::getOutput(const ConfigKey& key) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 256 | return getOutput(time(nullptr) * NS_PER_SEC, key); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | UidMapping UidMap::getOutput(const int64_t& timestamp, const ConfigKey& key) { |
| 260 | lock_guard<mutex> lock(mMutex); // Lock for updates |
| 261 | |
| 262 | auto ret = UidMapping(mOutput); // Copy that will be returned. |
| 263 | int64_t prevMin = getMinimumTimestampNs(); |
| 264 | mLastUpdatePerConfigKey[key] = timestamp; |
| 265 | int64_t newMin = getMinimumTimestampNs(); |
| 266 | |
| 267 | if (newMin > prevMin) { |
| 268 | int64_t cutoff_nanos = newMin; |
| 269 | auto snapshots = mOutput.mutable_snapshots(); |
| 270 | auto it_snapshots = snapshots->cbegin(); |
| 271 | while (it_snapshots != snapshots->cend()) { |
| 272 | if (it_snapshots->timestamp_nanos() < cutoff_nanos) { |
| 273 | // it_snapshots now points to the following element. |
| 274 | it_snapshots = snapshots->erase(it_snapshots); |
| 275 | } else { |
| 276 | ++it_snapshots; |
| 277 | } |
| 278 | } |
| 279 | auto deltas = mOutput.mutable_changes(); |
| 280 | auto it_deltas = deltas->cbegin(); |
| 281 | while (it_deltas != deltas->cend()) { |
| 282 | if (it_deltas->timestamp_nanos() < cutoff_nanos) { |
| 283 | // it_deltas now points to the following element. |
| 284 | it_deltas = deltas->erase(it_deltas); |
| 285 | } else { |
| 286 | ++it_deltas; |
| 287 | } |
| 288 | } |
| 289 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 290 | mBytesUsed = mOutput.ByteSize(); // Compute actual size after potential deletions. |
| 291 | StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed); |
| 292 | StatsdStats::getInstance().setUidMapChanges(mOutput.changes_size()); |
| 293 | StatsdStats::getInstance().setUidMapSnapshots(mOutput.snapshots_size()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | void UidMap::printUidMap(FILE* out) { |
| 298 | lock_guard<mutex> lock(mMutex); |
| 299 | |
| 300 | for (auto it : mMap) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 301 | fprintf(out, "%s, v%d (%i)\n", it.second.packageName.c_str(), it.second.versionCode, |
| 302 | it.first); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 306 | void UidMap::OnConfigUpdated(const ConfigKey& key) { |
| 307 | mLastUpdatePerConfigKey[key] = -1; |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame^] | 308 | |
| 309 | // Ensure there is at least one snapshot available since this configuration also needs to know |
| 310 | // what all the uid's represent. |
| 311 | if (mOutput.snapshots_size() == 0) { |
| 312 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 313 | // Get statscompanion service from service manager |
| 314 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 315 | if (sm != nullptr) { |
| 316 | const String16 name("statscompanion"); |
| 317 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 318 | if (statsCompanion == nullptr) { |
| 319 | ALOGW("statscompanion service unavailable!"); |
| 320 | return; |
| 321 | } |
| 322 | statsCompanion->triggerUidSnapshot(); |
| 323 | } |
| 324 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void UidMap::OnConfigRemoved(const ConfigKey& key) { |
| 328 | mLastUpdatePerConfigKey.erase(key); |
| 329 | } |
| 330 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 331 | } // namespace statsd |
| 332 | } // namespace os |
| 333 | } // namespace android |