blob: de68fbceb7a9dfe11aebfd8aab9b0ae8dd4c40c0 [file] [log] [blame]
David Chende701692017-10-05 13:16:02 -07001/*
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 Chend6896892017-10-25 11:49:03 -070020#include "config/ConfigKey.h"
21#include "config/ConfigListener.h"
David Chende701692017-10-05 13:16:02 -070022#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "packages/PackageInfoListener.h"
David Chende701692017-10-05 13:16:02 -070024
25#include <binder/IResultReceiver.h>
26#include <binder/IShellCallback.h>
David Chend6896892017-10-25 11:49:03 -070027#include <gtest/gtest_prod.h>
David Chende701692017-10-05 13:16:02 -070028#include <log/logprint.h>
David Chende701692017-10-05 13:16:02 -070029#include <stdio.h>
David Chende701692017-10-05 13:16:02 -070030#include <utils/RefBase.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031#include <mutex>
32#include <set>
33#include <string>
34#include <unordered_map>
David Chende701692017-10-05 13:16:02 -070035
36using namespace std;
37
38namespace android {
39namespace os {
40namespace statsd {
41
42struct AppData {
43 const string packageName;
44 int versionCode;
45
Joe Onorato9fc9edf2017-10-15 20:08:52 -070046 AppData(const string& a, const int v) : packageName(a), versionCode(v){};
David Chende701692017-10-05 13:16:02 -070047};
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 Onorato9fc9edf2017-10-15 20:08:52 -070051class UidMap : public virtual android::RefBase {
David Chende701692017-10-05 13:16:02 -070052public:
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 Chend6896892017-10-25 11:49:03 -070057 // TODO: Add safeguards to call clearOutput if there's too much data already stored.
David Chende701692017-10-05 13:16:02 -070058 void updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode,
59 const vector<String16>& packageName);
60
David Chend6896892017-10-25 11:49:03 -070061 // 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 Chende701692017-10-05 13:16:02 -070065 // 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 Chende701692017-10-05 13:16:02 -070070 // 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 Chend6896892017-10-25 11:49:03 -070081 // 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 Chen21582962017-11-01 17:32:46 -070087 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 Chend6896892017-10-25 11:49:03 -070093 // 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 Chende701692017-10-05 13:16:02 -0700100
101private:
David Chend6896892017-10-25 11:49:03 -0700102 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 Chende701692017-10-05 13:16:02 -0700112 // TODO: Use shared_mutex for improved read-locking if a library can be found in Android.
113 mutable mutex mMutex;
David Chen21582962017-11-01 17:32:46 -0700114 mutable mutex mIsolatedMutex;
David Chende701692017-10-05 13:16:02 -0700115
David Chend6896892017-10-25 11:49:03 -0700116 // 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 Chende701692017-10-05 13:16:02 -0700118 std::unordered_multimap<int, AppData> mMap;
119
David Chen21582962017-11-01 17:32:46 -0700120 // 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 Chende701692017-10-05 13:16:02 -0700124 // 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 Chend6896892017-10-25 11:49:03 -0700129
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 Chende701692017-10-05 13:16:02 -0700140};
141
142} // namespace statsd
143} // namespace os
144} // namespace android
145
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700146#endif // STATSD_UIDMAP_H