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" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame^] | 21 | #include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | #include "packages/PackageInfoListener.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> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 30 | #include <mutex> |
| 31 | #include <set> |
| 32 | #include <string> |
| 33 | #include <unordered_map> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | namespace android { |
| 38 | namespace os { |
| 39 | namespace statsd { |
| 40 | |
| 41 | struct AppData { |
| 42 | const string packageName; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 43 | int64_t versionCode; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 44 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 45 | AppData(const string& a, const int64_t v) : packageName(a), versionCode(v){}; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | // UidMap keeps track of what the corresponding app name (APK name) and version code for every uid |
| 49 | // at any given moment. This map must be updated by StatsCompanionService. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 50 | class UidMap : public virtual android::RefBase { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 51 | public: |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 52 | UidMap(); |
| 53 | ~UidMap(); |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 54 | static const std::map<std::string, uint32_t> sAidToUidMapping; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 55 | /* |
| 56 | * All three inputs must be the same size, and the jth element in each array refers to the same |
| 57 | * tuple, ie. uid[j] corresponds to packageName[j] with versionCode[j]. |
| 58 | */ |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 59 | void updateMap(const vector<int32_t>& uid, const vector<int64_t>& versionCode, |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 60 | const vector<String16>& packageName); |
| 61 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 62 | void updateApp(const String16& packageName, const int32_t& uid, const int64_t& versionCode); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 63 | void removeApp(const String16& packageName, const int32_t& uid); |
| 64 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 65 | // Returns true if the given uid contains the specified app (eg. com.google.android.gms). |
| 66 | bool hasApp(int uid, const string& packageName) const; |
| 67 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 68 | // Returns the app names from uid. |
| 69 | std::set<string> getAppNamesFromUid(const int32_t& uid, bool returnNormalized) const; |
| 70 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 71 | int64_t getAppVersion(int uid, const string& packageName) const; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 72 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 73 | // Helper for debugging contents of this uid map. Can be triggered with: |
| 74 | // adb shell cmd stats print-uid-map |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 75 | void printUidMap(FILE* out) const; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 76 | |
| 77 | // Commands for indicating to the map that a producer should be notified if an app is updated. |
| 78 | // This allows the metric producer to distinguish when the same uid or app represents a |
| 79 | // different version of an app. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 80 | void addListener(wp<PackageInfoListener> producer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 81 | // 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] | 82 | void removeListener(wp<PackageInfoListener> producer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 83 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 84 | // Informs uid map that a config is added/updated. Used for keeping mConfigKeys up to date. |
| 85 | void OnConfigUpdated(const ConfigKey& key); |
| 86 | |
| 87 | // Informs uid map that a config is removed. Used for keeping mConfigKeys up to date. |
| 88 | void OnConfigRemoved(const ConfigKey& key); |
| 89 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 90 | void assignIsolatedUid(int isolatedUid, int parentUid); |
| 91 | void removeIsolatedUid(int isolatedUid, int parentUid); |
| 92 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 93 | // 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] | 94 | virtual int getHostUidOrSelf(int uid) const; |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 95 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 96 | // Gets the output. If every config key has received the output, then the output is cleared. |
| 97 | UidMapping getOutput(const ConfigKey& key); |
| 98 | |
| 99 | // Forces the output to be cleared. We still generate a snapshot based on the current state. |
| 100 | // This results in extra data uploaded but helps us reconstruct the uid mapping on the server |
| 101 | // in case we lose a previous upload. |
| 102 | void clearOutput(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 103 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 104 | // Get currently cached value of memory used by UID map. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 105 | size_t getBytesUsed() const; |
| 106 | |
| 107 | std::set<int32_t> getAppUid(const string& package) const; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 108 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 109 | private: |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 110 | std::set<string> getAppNamesFromUidLocked(const int32_t& uid, bool returnNormalized) const; |
| 111 | string normalizeAppName(const string& appName) const; |
| 112 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 113 | void updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 114 | const vector<int64_t>& versionCode, const vector<String16>& packageName); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 115 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 116 | void updateApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid, |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 117 | const int64_t& versionCode); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 118 | void removeApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid); |
| 119 | |
| 120 | UidMapping getOutput(const int64_t& timestamp, const ConfigKey& key); |
| 121 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 122 | void getListenerListCopyLocked(std::vector<wp<PackageInfoListener>>* output); |
| 123 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 124 | // TODO: Use shared_mutex for improved read-locking if a library can be found in Android. |
| 125 | mutable mutex mMutex; |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 126 | mutable mutex mIsolatedMutex; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 127 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 128 | // Maps uid to application data. This must be multimap since there is a feature in Android for |
| 129 | // multiple apps to share the same uid. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 130 | std::unordered_multimap<int, AppData> mMap; |
| 131 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 132 | // Maps isolated uid to the parent uid. Any metrics for an isolated uid will instead contribute |
| 133 | // to the parent uid. |
| 134 | std::unordered_map<int, int> mIsolatedUidMap; |
| 135 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 136 | // We prepare the output proto as apps are updated, so that we can grab the current output. |
| 137 | UidMapping mOutput; |
| 138 | |
| 139 | // 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] | 140 | set<wp<PackageInfoListener>> mSubscribers; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 141 | |
| 142 | // Mapping of config keys we're aware of to the epoch time they last received an update. This |
| 143 | // lets us know it's safe to delete events older than the oldest update. The value is nanosec. |
| 144 | // Value of -1 denotes this config key has never received an upload. |
| 145 | std::unordered_map<ConfigKey, int64_t> mLastUpdatePerConfigKey; |
| 146 | |
| 147 | // Returns the minimum value from mConfigKeys. |
| 148 | int64_t getMinimumTimestampNs(); |
| 149 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 150 | // If our current used bytes is above the limit, then we clear out the earliest snapshot. If |
| 151 | // there are no more snapshots, then we clear out the earliest delta. We repeat the deletions |
| 152 | // until the memory consumed by mOutput is below the specified limit. |
| 153 | void ensureBytesUsedBelowLimit(); |
| 154 | |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 155 | // 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] | 156 | // specified in StatsdStats.h with the rest of the guardrails. |
David Chen | c0f6f63 | 2018-01-18 16:02:42 -0800 | [diff] [blame] | 157 | size_t maxBytesOverride = 0; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 158 | |
| 159 | // Cache the size of mOutput; |
| 160 | size_t mBytesUsed; |
| 161 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 162 | // Allows unit-test to access private methods. |
| 163 | FRIEND_TEST(UidMapTest, TestClearingOutput); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 164 | FRIEND_TEST(UidMapTest, TestMemoryComputed); |
| 165 | FRIEND_TEST(UidMapTest, TestMemoryGuardrail); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | } // namespace statsd |
| 169 | } // namespace os |
| 170 | } // namespace android |