David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #ifndef STATSD_UIDMAP_H |
| 18 | #define STATSD_UIDMAP_H |
| 19 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 20 | #include "config/ConfigKey.h" |
| 21 | #include "config/ConfigListener.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 22 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "packages/PackageInfoListener.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 24 | |
| 25 | #include <binder/IResultReceiver.h> |
| 26 | #include <binder/IShellCallback.h> |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 27 | #include <gtest/gtest_prod.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 28 | #include <log/logprint.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 30 | #include <utils/RefBase.h> |
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 | |
| 36 | using namespace std; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace os { |
| 40 | namespace statsd { |
| 41 | |
| 42 | struct AppData { |
| 43 | const string packageName; |
| 44 | int versionCode; |
| 45 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 46 | AppData(const string& a, const int v) : packageName(a), versionCode(v){}; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // UidMap keeps track of what the corresponding app name (APK name) and version code for every uid |
| 50 | // at any given moment. This map must be updated by StatsCompanionService. |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 51 | class UidMap : public virtual android::RefBase { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 52 | public: |
| 53 | /* |
| 54 | * All three inputs must be the same size, and the jth element in each array refers to the same |
| 55 | * tuple, ie. uid[j] corresponds to packageName[j] with versionCode[j]. |
| 56 | */ |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 57 | // TODO: Add safeguards to call clearOutput if there's too much data already stored. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 58 | void updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode, |
| 59 | const vector<String16>& packageName); |
| 60 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 61 | // TODO: Add safeguards to call clearOutput if there's too much data already stored. |
| 62 | void updateApp(const String16& packageName, const int32_t& uid, const int32_t& versionCode); |
| 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 | |
| 68 | int getAppVersion(int uid, const string& packageName) const; |
| 69 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 70 | // Helper for debugging contents of this uid map. Can be triggered with: |
| 71 | // adb shell cmd stats print-uid-map |
| 72 | void printUidMap(FILE* out); |
| 73 | |
| 74 | // Commands for indicating to the map that a producer should be notified if an app is updated. |
| 75 | // This allows the metric producer to distinguish when the same uid or app represents a |
| 76 | // different version of an app. |
| 77 | void addListener(sp<PackageInfoListener> producer); |
| 78 | // Remove the listener from the set of metric producers that subscribe to updates. |
| 79 | void removeListener(sp<PackageInfoListener> producer); |
| 80 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 81 | // Informs uid map that a config is added/updated. Used for keeping mConfigKeys up to date. |
| 82 | void OnConfigUpdated(const ConfigKey& key); |
| 83 | |
| 84 | // Informs uid map that a config is removed. Used for keeping mConfigKeys up to date. |
| 85 | void OnConfigRemoved(const ConfigKey& key); |
| 86 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 87 | void assignIsolatedUid(int isolatedUid, int parentUid); |
| 88 | void removeIsolatedUid(int isolatedUid, int parentUid); |
| 89 | |
| 90 | // Returns the parent uid if it exists. Otherwise, returns the same uid that was passed-in. |
| 91 | int getParentUidOrSelf(int uid); |
| 92 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 93 | // Gets the output. If every config key has received the output, then the output is cleared. |
| 94 | UidMapping getOutput(const ConfigKey& key); |
| 95 | |
| 96 | // Forces the output to be cleared. We still generate a snapshot based on the current state. |
| 97 | // This results in extra data uploaded but helps us reconstruct the uid mapping on the server |
| 98 | // in case we lose a previous upload. |
| 99 | void clearOutput(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 100 | |
| 101 | private: |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 102 | void updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
| 103 | const vector<int32_t>& versionCode, const vector<String16>& packageName); |
| 104 | |
| 105 | // TODO: Add safeguards to call clearOutput if there's too much data already stored. |
| 106 | void updateApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid, |
| 107 | const int32_t& versionCode); |
| 108 | void removeApp(const int64_t& timestamp, const String16& packageName, const int32_t& uid); |
| 109 | |
| 110 | UidMapping getOutput(const int64_t& timestamp, const ConfigKey& key); |
| 111 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 112 | // TODO: Use shared_mutex for improved read-locking if a library can be found in Android. |
| 113 | mutable mutex mMutex; |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 114 | mutable mutex mIsolatedMutex; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 115 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 116 | // Maps uid to application data. This must be multimap since there is a feature in Android for |
| 117 | // multiple apps to share the same uid. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 118 | std::unordered_multimap<int, AppData> mMap; |
| 119 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 120 | // Maps isolated uid to the parent uid. Any metrics for an isolated uid will instead contribute |
| 121 | // to the parent uid. |
| 122 | std::unordered_map<int, int> mIsolatedUidMap; |
| 123 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 124 | // We prepare the output proto as apps are updated, so that we can grab the current output. |
| 125 | UidMapping mOutput; |
| 126 | |
| 127 | // Metric producers that should be notified if there's an upgrade in any app. |
| 128 | set<sp<PackageInfoListener>> mSubscribers; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 129 | |
| 130 | // Mapping of config keys we're aware of to the epoch time they last received an update. This |
| 131 | // lets us know it's safe to delete events older than the oldest update. The value is nanosec. |
| 132 | // Value of -1 denotes this config key has never received an upload. |
| 133 | std::unordered_map<ConfigKey, int64_t> mLastUpdatePerConfigKey; |
| 134 | |
| 135 | // Returns the minimum value from mConfigKeys. |
| 136 | int64_t getMinimumTimestampNs(); |
| 137 | |
| 138 | // Allows unit-test to access private methods. |
| 139 | FRIEND_TEST(UidMapTest, TestClearingOutput); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // namespace statsd |
| 143 | } // namespace os |
| 144 | } // namespace android |
| 145 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 146 | #endif // STATSD_UIDMAP_H |