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, 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 | */ |
| 16 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
| 19 | #include "packages/UidMap.h" |
| 20 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | using namespace android; |
| 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
| 29 | bool UidMap::hasApp(int uid, const string& packageName) const { |
| 30 | lock_guard<mutex> lock(mMutex); |
| 31 | |
| 32 | auto range = mMap.equal_range(uid); |
| 33 | for (auto it = range.first; it != range.second; ++it) { |
| 34 | if (it->second.packageName == packageName) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | int UidMap::getAppVersion(int uid, const string& packageName) const { |
| 42 | lock_guard<mutex> lock(mMutex); |
| 43 | |
| 44 | auto range = mMap.equal_range(uid); |
| 45 | for (auto it = range.first; it != range.second; ++it) { |
| 46 | if (it->second.packageName == packageName) { |
| 47 | return it->second.versionCode; |
| 48 | } |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 53 | void UidMap::updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode, |
| 54 | const vector<String16>& packageName) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 55 | updateMap(time(nullptr) * 1000000000, uid, versionCode, packageName); |
| 56 | } |
| 57 | |
| 58 | void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid, |
| 59 | const vector<int32_t>& versionCode, const vector<String16>& packageName) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 60 | lock_guard<mutex> lock(mMutex); // Exclusively lock for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 61 | |
| 62 | mMap.clear(); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 63 | for (size_t j = 0; j < uid.size(); j++) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 64 | mMap.insert(make_pair(uid[j], |
| 65 | AppData(string(String8(packageName[j]).string()), versionCode[j]))); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 66 | } |
| 67 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 68 | auto snapshot = mOutput.add_snapshots(); |
| 69 | snapshot->set_timestamp_nanos(timestamp); |
| 70 | for (size_t j = 0; j < uid.size(); j++) { |
| 71 | auto t = snapshot->add_package_info(); |
| 72 | t->set_name(string(String8(packageName[j]).string())); |
| 73 | t->set_version(int(versionCode[j])); |
| 74 | t->set_uid(uid[j]); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 78 | void UidMap::updateApp(const String16& app_16, const int32_t& uid, const int32_t& versionCode) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 79 | updateApp(time(nullptr) * 1000000000, app_16, uid, versionCode); |
| 80 | } |
| 81 | |
| 82 | void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid, |
| 83 | const int32_t& versionCode) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 84 | lock_guard<mutex> lock(mMutex); |
| 85 | |
| 86 | string app = string(String8(app_16).string()); |
| 87 | |
| 88 | // Notify any interested producers that this app has updated |
| 89 | for (auto it : mSubscribers) { |
| 90 | it->notifyAppUpgrade(app, uid, versionCode); |
| 91 | } |
| 92 | |
| 93 | auto log = mOutput.add_changes(); |
| 94 | log->set_deletion(false); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 95 | log->set_timestamp_nanos(timestamp); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 96 | log->set_app(app); |
| 97 | log->set_uid(uid); |
| 98 | log->set_version(versionCode); |
| 99 | |
| 100 | auto range = mMap.equal_range(int(uid)); |
| 101 | for (auto it = range.first; it != range.second; ++it) { |
| 102 | if (it->second.packageName == app) { |
| 103 | it->second.versionCode = int(versionCode); |
| 104 | return; |
| 105 | } |
| 106 | ALOGD("updateApp failed to find the app %s with uid %i to update", app.c_str(), uid); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Otherwise, we need to add an app at this uid. |
| 111 | mMap.insert(make_pair(uid, AppData(app, int(versionCode)))); |
| 112 | } |
| 113 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 114 | void UidMap::removeApp(const String16& app_16, const int32_t& uid) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 115 | removeApp(time(nullptr) * 1000000000, app_16, uid); |
| 116 | } |
| 117 | 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] | 118 | lock_guard<mutex> lock(mMutex); |
| 119 | |
| 120 | string app = string(String8(app_16).string()); |
| 121 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 122 | for (auto it : mSubscribers) { |
| 123 | it->notifyAppRemoved(app, uid); |
| 124 | } |
| 125 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 126 | auto log = mOutput.add_changes(); |
| 127 | log->set_deletion(true); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 128 | log->set_timestamp_nanos(timestamp); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 129 | log->set_app(app); |
| 130 | log->set_uid(uid); |
| 131 | |
| 132 | auto range = mMap.equal_range(int(uid)); |
| 133 | for (auto it = range.first; it != range.second; ++it) { |
| 134 | if (it->second.packageName == app) { |
| 135 | mMap.erase(it); |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | ALOGD("removeApp failed to find the app %s with uid %i to remove", app.c_str(), uid); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | void UidMap::addListener(sp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 144 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 145 | mSubscribers.insert(producer); |
| 146 | } |
| 147 | |
| 148 | void UidMap::removeListener(sp<PackageInfoListener> producer) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 149 | lock_guard<mutex> lock(mMutex); // Lock for updates |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 150 | mSubscribers.erase(producer); |
| 151 | } |
| 152 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 153 | void UidMap::clearOutput() { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 154 | mOutput.Clear(); |
| 155 | |
| 156 | // 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] | 157 | // but helps ensure we can re-construct the UID->app name, versionCode mapping in server. |
| 158 | auto snapshot = mOutput.add_snapshots(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 159 | for (auto it : mMap) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 160 | auto t = snapshot->add_package_info(); |
| 161 | t->set_name(it.second.packageName); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 162 | t->set_version(it.second.versionCode); |
| 163 | t->set_uid(it.first); |
| 164 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 165 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 166 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 167 | int64_t UidMap::getMinimumTimestampNs() { |
| 168 | int64_t m = 0; |
| 169 | for (auto it : mLastUpdatePerConfigKey) { |
| 170 | if (m == 0) { |
| 171 | m = it.second; |
| 172 | } else if (it.second < m) { |
| 173 | m = it.second; |
| 174 | } |
| 175 | } |
| 176 | return m; |
| 177 | } |
| 178 | |
| 179 | UidMapping UidMap::getOutput(const ConfigKey& key) { |
| 180 | return getOutput(time(nullptr) * 1000000000, key); |
| 181 | } |
| 182 | |
| 183 | UidMapping UidMap::getOutput(const int64_t& timestamp, const ConfigKey& key) { |
| 184 | lock_guard<mutex> lock(mMutex); // Lock for updates |
| 185 | |
| 186 | auto ret = UidMapping(mOutput); // Copy that will be returned. |
| 187 | int64_t prevMin = getMinimumTimestampNs(); |
| 188 | mLastUpdatePerConfigKey[key] = timestamp; |
| 189 | int64_t newMin = getMinimumTimestampNs(); |
| 190 | |
| 191 | if (newMin > prevMin) { |
| 192 | int64_t cutoff_nanos = newMin; |
| 193 | auto snapshots = mOutput.mutable_snapshots(); |
| 194 | auto it_snapshots = snapshots->cbegin(); |
| 195 | while (it_snapshots != snapshots->cend()) { |
| 196 | if (it_snapshots->timestamp_nanos() < cutoff_nanos) { |
| 197 | // it_snapshots now points to the following element. |
| 198 | it_snapshots = snapshots->erase(it_snapshots); |
| 199 | } else { |
| 200 | ++it_snapshots; |
| 201 | } |
| 202 | } |
| 203 | auto deltas = mOutput.mutable_changes(); |
| 204 | auto it_deltas = deltas->cbegin(); |
| 205 | while (it_deltas != deltas->cend()) { |
| 206 | if (it_deltas->timestamp_nanos() < cutoff_nanos) { |
| 207 | // it_deltas now points to the following element. |
| 208 | it_deltas = deltas->erase(it_deltas); |
| 209 | } else { |
| 210 | ++it_deltas; |
| 211 | } |
| 212 | } |
| 213 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | void UidMap::printUidMap(FILE* out) { |
| 218 | lock_guard<mutex> lock(mMutex); |
| 219 | |
| 220 | for (auto it : mMap) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 221 | fprintf(out, "%s, v%d (%i)\n", it.second.packageName.c_str(), it.second.versionCode, |
| 222 | it.first); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 226 | void UidMap::OnConfigUpdated(const ConfigKey& key) { |
| 227 | mLastUpdatePerConfigKey[key] = -1; |
| 228 | } |
| 229 | |
| 230 | void UidMap::OnConfigRemoved(const ConfigKey& key) { |
| 231 | mLastUpdatePerConfigKey.erase(key); |
| 232 | } |
| 233 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 234 | } // namespace statsd |
| 235 | } // namespace os |
| 236 | } // namespace android |