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, 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 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 17 | #pragma once |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 18 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 19 | #include "config/ConfigKey.h" |
| 20 | #include "config/ConfigListener.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "packages/PackageInfoListener.h" |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 22 | #include "stats_util.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 23 | |
| 24 | #include <binder/IResultReceiver.h> |
| 25 | #include <binder/IShellCallback.h> |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 26 | #include <gtest/gtest_prod.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 27 | #include <log/logprint.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 30 | #include <list> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | #include <mutex> |
| 32 | #include <set> |
| 33 | #include <string> |
| 34 | #include <unordered_map> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 35 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 36 | using namespace android; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 37 | using namespace std; |
| 38 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 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 | |
| 45 | struct AppData { |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 46 | int64_t versionCode; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 47 | bool deleted; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 48 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 49 | // Empty constructor needed for unordered map. |
| 50 | AppData() { |
| 51 | } |
| 52 | AppData(const int64_t v) : versionCode(v), deleted(false){}; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 55 | // When calling appendUidMap, we retrieve all the ChangeRecords since the last |
| 56 | // timestamp we called appendUidMap for this configuration key. |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 57 | struct ChangeRecord { |
| 58 | const bool deletion; |
| 59 | const int64_t timestampNs; |
| 60 | const string package; |
| 61 | const int32_t uid; |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 62 | const int64_t version; |
| 63 | const int64_t prevVersion; |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 64 | |
| 65 | ChangeRecord(const bool isDeletion, const int64_t timestampNs, const string& package, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 66 | const int32_t uid, const int64_t version, const int64_t prevVersion) |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 67 | : deletion(isDeletion), |
| 68 | timestampNs(timestampNs), |
| 69 | package(package), |
| 70 | uid(uid), |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 71 | version(version), |
| 72 | prevVersion(prevVersion) { |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | const unsigned int kBytesChangeRecord = sizeof(struct ChangeRecord); |
| 77 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 78 | // UidMap keeps track of what the corresponding app name (APK name) and version code for every uid |
| 79 | // at any given moment. This map must be updated by StatsCompanionService. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 80 | class UidMap : public virtual android::RefBase { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 81 | public: |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 82 | UidMap(); |
| 83 | ~UidMap(); |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 84 | static const std::map<std::string, uint32_t> sAidToUidMapping; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 85 | /* |
| 86 | * All three inputs must be the same size, and the jth element in each array refers to the same |
| 87 | * tuple, ie. uid[j] corresponds to packageName[j] with versionCode[j]. |
| 88 | */ |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 89 | void updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
| 90 | const vector<int64_t>& versionCode, const vector<String16>& packageName); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 91 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 92 | void updateApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid, |
| 93 | const int64_t& versionCode); |
| 94 | void removeApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 95 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 96 | // Returns true if the given uid contains the specified app (eg. com.google.android.gms). |
| 97 | bool hasApp(int uid, const string& packageName) const; |
| 98 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 99 | // Returns the app names from uid. |
| 100 | std::set<string> getAppNamesFromUid(const int32_t& uid, bool returnNormalized) const; |
| 101 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 102 | int64_t getAppVersion(int uid, const string& packageName) const; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 103 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 104 | // Helper for debugging contents of this uid map. Can be triggered with: |
| 105 | // adb shell cmd stats print-uid-map |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 106 | void printUidMap(FILE* out) const; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 107 | |
| 108 | // Commands for indicating to the map that a producer should be notified if an app is updated. |
| 109 | // This allows the metric producer to distinguish when the same uid or app represents a |
| 110 | // different version of an app. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 111 | void addListener(wp<PackageInfoListener> producer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 112 | // Remove the listener from the set of metric producers that subscribe to updates. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 113 | void removeListener(wp<PackageInfoListener> producer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 114 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 115 | // Informs uid map that a config is added/updated. Used for keeping mConfigKeys up to date. |
| 116 | void OnConfigUpdated(const ConfigKey& key); |
| 117 | |
| 118 | // Informs uid map that a config is removed. Used for keeping mConfigKeys up to date. |
| 119 | void OnConfigRemoved(const ConfigKey& key); |
| 120 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 121 | void assignIsolatedUid(int isolatedUid, int parentUid); |
| 122 | void removeIsolatedUid(int isolatedUid, int parentUid); |
| 123 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 124 | // Returns the host uid if it exists. Otherwise, returns the same uid that was passed-in. |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 125 | virtual int getHostUidOrSelf(int uid) const; |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 126 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 127 | // Gets all snapshots and changes that have occurred since the last output. |
| 128 | // If every config key has received a change or snapshot record, then this |
| 129 | // record is deleted. |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 130 | void appendUidMap(const int64_t& timestamp, const ConfigKey& key, |
| 131 | util::ProtoOutputStream* proto); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 132 | |
| 133 | // Forces the output to be cleared. We still generate a snapshot based on the current state. |
| 134 | // This results in extra data uploaded but helps us reconstruct the uid mapping on the server |
| 135 | // in case we lose a previous upload. |
| 136 | void clearOutput(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 137 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 138 | // Get currently cached value of memory used by UID map. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 139 | size_t getBytesUsed() const; |
| 140 | |
| 141 | std::set<int32_t> getAppUid(const string& package) const; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 142 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 143 | private: |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 144 | std::set<string> getAppNamesFromUidLocked(const int32_t& uid, bool returnNormalized) const; |
| 145 | string normalizeAppName(const string& appName) const; |
| 146 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 147 | void getListenerListCopyLocked(std::vector<wp<PackageInfoListener>>* output); |
| 148 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 149 | // TODO: Use shared_mutex for improved read-locking if a library can be found in Android. |
| 150 | mutable mutex mMutex; |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 151 | mutable mutex mIsolatedMutex; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 152 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 153 | struct PairHash { |
| 154 | size_t operator()(std::pair<int, string> p) const noexcept { |
| 155 | std::hash<std::string> hash_fn; |
| 156 | return hash_fn(std::to_string(p.first) + p.second); |
| 157 | } |
| 158 | }; |
| 159 | // Maps uid and package name to application data. |
| 160 | std::unordered_map<std::pair<int, string>, AppData, PairHash> mMap; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 161 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 162 | // Maps isolated uid to the parent uid. Any metrics for an isolated uid will instead contribute |
| 163 | // to the parent uid. |
| 164 | std::unordered_map<int, int> mIsolatedUidMap; |
| 165 | |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 166 | // Record the changes that can be provided with the uploads. |
| 167 | std::list<ChangeRecord> mChanges; |
| 168 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 169 | // Store which uid and apps represent deleted ones. |
| 170 | std::list<std::pair<int, string>> mDeletedApps; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 171 | |
| 172 | // Metric producers that should be notified if there's an upgrade in any app. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 173 | set<wp<PackageInfoListener>> mSubscribers; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 174 | |
| 175 | // Mapping of config keys we're aware of to the epoch time they last received an update. This |
| 176 | // lets us know it's safe to delete events older than the oldest update. The value is nanosec. |
| 177 | // Value of -1 denotes this config key has never received an upload. |
| 178 | std::unordered_map<ConfigKey, int64_t> mLastUpdatePerConfigKey; |
| 179 | |
| 180 | // Returns the minimum value from mConfigKeys. |
| 181 | int64_t getMinimumTimestampNs(); |
| 182 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 183 | // If our current used bytes is above the limit, then we clear out the earliest snapshot. If |
| 184 | // there are no more snapshots, then we clear out the earliest delta. We repeat the deletions |
| 185 | // until the memory consumed by mOutput is below the specified limit. |
| 186 | void ensureBytesUsedBelowLimit(); |
| 187 | |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 188 | // Override used for testing the max memory allowed by uid map. 0 means we use the value |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 189 | // specified in StatsdStats.h with the rest of the guardrails. |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 190 | size_t maxBytesOverride = 0; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 191 | |
| 192 | // Cache the size of mOutput; |
| 193 | size_t mBytesUsed; |
| 194 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 195 | // Allows unit-test to access private methods. |
| 196 | FRIEND_TEST(UidMapTest, TestClearingOutput); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 197 | FRIEND_TEST(UidMapTest, TestRemovedAppRetained); |
| 198 | FRIEND_TEST(UidMapTest, TestRemovedAppOverGuardrail); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 199 | FRIEND_TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 200 | FRIEND_TEST(UidMapTest, TestMemoryComputed); |
| 201 | FRIEND_TEST(UidMapTest, TestMemoryGuardrail); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | } // namespace statsd |
| 205 | } // namespace os |
| 206 | } // namespace android |