blob: 7e825efddb759e20c1decee6228f6478737d999d [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -07001/*
2 * Copyright (C) 2017 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 */
Yangster-mac754e29e2018-05-02 12:23:17 -070016#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
tsaichristined21aacf2019-10-07 14:47:38 -070018
Yao Chen44cf27c2017-09-14 22:32:50 -070019#include "MetricsManager.h"
tsaichristined21aacf2019-10-07 14:47:38 -070020
tsaichristined21aacf2019-10-07 14:47:38 -070021#include <private/android_filesystem_config.h>
Yao Chen93fe3a32017-11-02 13:52:59 -070022
Yao Chen44cf27c2017-09-14 22:32:50 -070023#include "CountMetricProducer.h"
Muhammad Qureshic8e22662019-11-20 17:18:03 -080024#include "atoms_info.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070025#include "condition/CombinationConditionTracker.h"
26#include "condition/SimpleConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080027#include "guardrail/StatsdStats.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070028#include "matchers/CombinationLogMatchingTracker.h"
29#include "matchers/SimpleLogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070030#include "metrics_manager_util.h"
tsaichristined21aacf2019-10-07 14:47:38 -070031#include "state/StateManager.h"
Yangster-mac330af582018-02-08 15:24:38 -080032#include "stats_log_util.h"
tsaichristined21aacf2019-10-07 14:47:38 -070033#include "stats_util.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080034#include "statslog_statsd.h"
Yao Chen288c6002017-12-12 13:43:18 -080035
36using android::util::FIELD_COUNT_REPEATED;
David Chenfaa1af52018-03-30 15:14:04 -070037using android::util::FIELD_TYPE_INT32;
38using android::util::FIELD_TYPE_INT64;
Yao Chen288c6002017-12-12 13:43:18 -080039using android::util::FIELD_TYPE_MESSAGE;
Yangster-mac9def8e32018-04-17 13:55:51 -070040using android::util::FIELD_TYPE_STRING;
Yao Chen288c6002017-12-12 13:43:18 -080041using android::util::ProtoOutputStream;
42
Yao Chen44cf27c2017-09-14 22:32:50 -070043using std::set;
44using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070045using std::vector;
46
47namespace android {
48namespace os {
49namespace statsd {
50
Yao Chen288c6002017-12-12 13:43:18 -080051const int FIELD_ID_METRICS = 1;
David Chenfaa1af52018-03-30 15:14:04 -070052const int FIELD_ID_ANNOTATIONS = 7;
53const int FIELD_ID_ANNOTATIONS_INT64 = 1;
54const int FIELD_ID_ANNOTATIONS_INT32 = 2;
Yao Chen288c6002017-12-12 13:43:18 -080055
Muhammad Qureshi844694b2019-04-05 10:10:40 -070056// for ActiveConfig
57const int FIELD_ID_ACTIVE_CONFIG_ID = 1;
58const int FIELD_ID_ACTIVE_CONFIG_UID = 2;
59const int FIELD_ID_ACTIVE_CONFIG_METRIC = 3;
60
Yao Chend10f7b12017-12-18 12:53:50 -080061MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070062 const int64_t timeBaseNs, const int64_t currentTimeNs,
Chenjie Yue2219202018-06-08 10:07:51 -070063 const sp<UidMap>& uidMap,
64 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -080065 const sp<AlarmMonitor>& anomalyAlarmMonitor,
66 const sp<AlarmMonitor>& periodicAlarmMonitor)
Chenjie Yue2219202018-06-08 10:07:51 -070067 : mConfigKey(key),
68 mUidMap(uidMap),
Yangster-macb142cc82018-03-30 15:22:08 -070069 mTtlNs(config.has_ttl_in_seconds() ? config.ttl_in_seconds() * NS_PER_SEC : -1),
70 mTtlEndNs(-1),
David Chen48944902018-05-03 10:29:11 -070071 mLastReportTimeNs(currentTimeNs),
Yao Chen9a43b4f2019-04-10 10:43:20 -070072 mLastReportWallClockNs(getWallClockNs()),
Tej Singh3be093b2020-03-04 20:08:38 -080073 mPullerManager(pullerManager),
Muhammad Qureshif6816462020-05-13 15:13:33 -070074 mWhitelistedAtomIds(config.whitelisted_atom_ids().begin(),
75 config.whitelisted_atom_ids().end()),
Yao Chen9a43b4f2019-04-10 10:43:20 -070076 mShouldPersistHistory(config.persist_locally()) {
Yangster-macb142cc82018-03-30 15:22:08 -070077 // Init the ttl end timestamp.
Yangster-mac15f6bbc2018-04-08 11:52:26 -070078 refreshTtl(timeBaseNs);
Yangster-macb142cc82018-03-30 15:22:08 -070079
Chenjie Yue2219202018-06-08 10:07:51 -070080 mConfigValid = initStatsdConfig(
81 key, config, *uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor,
82 timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
83 mAllMetricProducers, mAllAnomalyTrackers, mAllPeriodicAlarmTrackers,
Yangster-mac849dfdc22018-10-12 15:41:45 -070084 mConditionToMetricMap, mTrackerToMetricMap, mTrackerToConditionMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -070085 mActivationAtomTrackerToMetricMap, mDeactivationAtomTrackerToMetricMap,
Jeffrey Huang38d70262020-03-19 17:28:59 -070086 mAlertTrackerMap, mMetricIndexesWithActivation, mNoReportMetricIds);
Yao Chenb3561512017-11-21 18:07:17 -080087
Yangster-mac1c58f042018-05-17 15:52:51 -070088 mHashStringsInReport = config.hash_strings_in_metric_report();
dwchen730403e2018-10-29 11:41:56 -070089 mVersionStringsInReport = config.version_strings_in_metric_report();
90 mInstallerInReport = config.installer_in_metric_report();
Yangster-mac1c58f042018-05-17 15:52:51 -070091
Tej Singh3be093b2020-03-04 20:08:38 -080092 // Init allowed pushed atom uids.
Yao Chen147ce602017-12-22 14:35:34 -080093 if (config.allowed_log_source_size() == 0) {
David Chen8faaa012018-02-28 15:54:36 -080094 mConfigValid = false;
95 ALOGE("Log source whitelist is empty! This config won't get any data. Suggest adding at "
96 "least AID_SYSTEM and AID_STATSD to the allowed_log_source field.");
Yao Chend10f7b12017-12-18 12:53:50 -080097 } else {
Yao Chen147ce602017-12-22 14:35:34 -080098 for (const auto& source : config.allowed_log_source()) {
99 auto it = UidMap::sAidToUidMapping.find(source);
100 if (it != UidMap::sAidToUidMapping.end()) {
101 mAllowedUid.push_back(it->second);
102 } else {
103 mAllowedPkg.push_back(source);
104 }
105 }
Yao Chend10f7b12017-12-18 12:53:50 -0800106
107 if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
108 ALOGE("Too many log sources. This is likely to be an error in the config.");
109 mConfigValid = false;
110 } else {
111 initLogSourceWhiteList();
112 }
113 }
114
Tej Singh3be093b2020-03-04 20:08:38 -0800115 // Init default allowed pull atom uids.
116 int numPullPackages = 0;
117 for (const string& pullSource : config.default_pull_packages()) {
118 auto it = UidMap::sAidToUidMapping.find(pullSource);
119 if (it != UidMap::sAidToUidMapping.end()) {
120 numPullPackages++;
121 mDefaultPullUids.insert(it->second);
122 } else {
123 ALOGE("Default pull atom packages must be in sAidToUidMapping");
124 mConfigValid = false;
125 }
126 }
127 // Init per-atom pull atom packages.
128 for (const PullAtomPackages& pullAtomPackages : config.pull_atom_packages()) {
129 int32_t atomId = pullAtomPackages.atom_id();
130 for (const string& pullPackage : pullAtomPackages.packages()) {
131 numPullPackages++;
132 auto it = UidMap::sAidToUidMapping.find(pullPackage);
133 if (it != UidMap::sAidToUidMapping.end()) {
134 mPullAtomUids[atomId].insert(it->second);
135 } else {
136 mPullAtomPackages[atomId].insert(pullPackage);
137 }
138 }
139 }
140 if (numPullPackages > StatsdStats::kMaxPullAtomPackages) {
141 ALOGE("Too many sources in default_pull_packages and pull_atom_packages. This is likely to "
142 "be an error in the config");
143 mConfigValid = false;
144 } else {
145 initPullAtomSources();
146 }
147 mPullerManager->RegisterPullUidProvider(mConfigKey, this);
148
David Chenfaa1af52018-03-30 15:14:04 -0700149 // Store the sub-configs used.
150 for (const auto& annotation : config.annotation()) {
151 mAnnotations.emplace_back(annotation.field_int64(), annotation.field_int32());
152 }
153
Yao Chenb3561512017-11-21 18:07:17 -0800154 // Guardrail. Reject the config if it's too big.
155 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
156 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800157 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -0800158 ALOGE("This config is too big! Reject!");
159 mConfigValid = false;
160 }
Bookatz1476ef22018-02-13 12:26:01 -0800161 if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) {
162 ALOGE("This config has too many alerts! Reject!");
163 mConfigValid = false;
164 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800165
166 mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) ||
167 (mAllMetricProducers.size() == 0);
168 bool isActive = mIsAlwaysActive;
169 for (int metric : mMetricIndexesWithActivation) {
170 isActive |= mAllMetricProducers[metric]->isActive();
171 }
172 mIsActive = isActive;
173 VLOG("mIsActive is initialized to %d", mIsActive)
174
Yao Chenf09569f2017-12-13 17:00:51 -0800175 // no matter whether this config is valid, log it in the stats.
David Chenfaa1af52018-03-30 15:14:04 -0700176 StatsdStats::getInstance().noteConfigReceived(
177 key, mAllMetricProducers.size(), mAllConditionTrackers.size(), mAllAtomMatchers.size(),
178 mAllAnomalyTrackers.size(), mAnnotations, mConfigValid);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800179 // Check active
180 for (const auto& metric : mAllMetricProducers) {
181 if (metric->isActive()) {
182 mIsActive = true;
183 break;
184 }
185 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700186}
187
188MetricsManager::~MetricsManager() {
tsaichristined21aacf2019-10-07 14:47:38 -0700189 for (auto it : mAllMetricProducers) {
190 for (int atomId : it->getSlicedStateAtoms()) {
191 StateManager::getInstance().unregisterListener(atomId, it);
192 }
193 }
Tej Singh3eb9ced2020-04-20 22:04:38 -0700194 mPullerManager->UnregisterPullUidProvider(mConfigKey, this);
tsaichristined21aacf2019-10-07 14:47:38 -0700195
Bookatzd3606c72017-10-19 10:13:49 -0700196 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -0700197}
198
Yao Chend10f7b12017-12-18 12:53:50 -0800199void MetricsManager::initLogSourceWhiteList() {
200 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
201 mAllowedLogSources.clear();
202 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
203
204 for (const auto& pkg : mAllowedPkg) {
205 auto uids = mUidMap->getAppUid(pkg);
206 mAllowedLogSources.insert(uids.begin(), uids.end());
207 }
208 if (DEBUG) {
209 for (const auto& uid : mAllowedLogSources) {
210 VLOG("Allowed uid %d", uid);
211 }
212 }
213}
214
Tej Singh3be093b2020-03-04 20:08:38 -0800215void MetricsManager::initPullAtomSources() {
216 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
217 mCombinedPullAtomUids.clear();
218 for (const auto& [atomId, uids] : mPullAtomUids) {
219 mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end());
220 }
221 for (const auto& [atomId, packages] : mPullAtomPackages) {
222 for (const string& pkg : packages) {
223 set<int32_t> uids = mUidMap->getAppUid(pkg);
224 mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end());
225 }
226 }
227}
228
Yao Chencaf339d2017-10-06 16:01:10 -0700229bool MetricsManager::isConfigValid() const {
230 return mConfigValid;
231}
232
Yangster-macb142cc82018-03-30 15:22:08 -0700233void MetricsManager::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -0800234 const int64_t version) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800235 // Inform all metric producers.
Tej Singhe678cb72020-04-14 16:23:30 -0700236 for (const auto& it : mAllMetricProducers) {
237 it->notifyAppUpgrade(eventTimeNs);
Tej Singh9ec159a2019-11-14 11:59:48 -0800238 }
Yao Chend10f7b12017-12-18 12:53:50 -0800239 // check if we care this package
Tej Singh3be093b2020-03-04 20:08:38 -0800240 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) {
241 // We will re-initialize the whole list because we don't want to keep the multi mapping of
242 // UID<->pkg inside MetricsManager to reduce the memory usage.
243 initLogSourceWhiteList();
Yao Chend10f7b12017-12-18 12:53:50 -0800244 }
Tej Singh3be093b2020-03-04 20:08:38 -0800245
246 for (const auto& it : mPullAtomPackages) {
247 if (it.second.find(apk) != it.second.end()) {
248 initPullAtomSources();
249 return;
250 }
251 }
Yao Chend10f7b12017-12-18 12:53:50 -0800252}
253
Yangster-macb142cc82018-03-30 15:22:08 -0700254void MetricsManager::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
David Chen27785a82018-01-19 17:06:45 -0800255 const int uid) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800256 // Inform all metric producers.
Tej Singhe678cb72020-04-14 16:23:30 -0700257 for (const auto& it : mAllMetricProducers) {
258 it->notifyAppRemoved(eventTimeNs);
Tej Singh9ec159a2019-11-14 11:59:48 -0800259 }
Yao Chend10f7b12017-12-18 12:53:50 -0800260 // check if we care this package
Tej Singh3be093b2020-03-04 20:08:38 -0800261 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) {
262 // We will re-initialize the whole list because we don't want to keep the multi mapping of
263 // UID<->pkg inside MetricsManager to reduce the memory usage.
264 initLogSourceWhiteList();
Yao Chend10f7b12017-12-18 12:53:50 -0800265 }
Tej Singh3be093b2020-03-04 20:08:38 -0800266
267 for (const auto& it : mPullAtomPackages) {
268 if (it.second.find(apk) != it.second.end()) {
269 initPullAtomSources();
270 return;
271 }
272 }
Yao Chend10f7b12017-12-18 12:53:50 -0800273}
274
Yangster-macb142cc82018-03-30 15:22:08 -0700275void MetricsManager::onUidMapReceived(const int64_t& eventTimeNs) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800276 // Purposefully don't inform metric producers on a new snapshot
277 // because we don't need to flush partial buckets.
278 // This occurs if a new user is added/removed or statsd crashes.
Tej Singh3be093b2020-03-04 20:08:38 -0800279 initPullAtomSources();
280
Yao Chend10f7b12017-12-18 12:53:50 -0800281 if (mAllowedPkg.size() == 0) {
282 return;
283 }
284 initLogSourceWhiteList();
285}
286
Tej Singhe678cb72020-04-14 16:23:30 -0700287void MetricsManager::onStatsdInitCompleted(const int64_t& eventTimeNs) {
288 // Inform all metric producers.
289 for (const auto& it : mAllMetricProducers) {
290 it->onStatsdInitCompleted(eventTimeNs);
291 }
292}
293
Tej Singh3be093b2020-03-04 20:08:38 -0800294void MetricsManager::init() {
295 for (const auto& producer : mAllMetricProducers) {
296 producer->prepareFirstBucket();
297 }
298}
299
300vector<int32_t> MetricsManager::getPullAtomUids(int32_t atomId) {
301 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
302 vector<int32_t> uids;
303 const auto& it = mCombinedPullAtomUids.find(atomId);
304 if (it != mCombinedPullAtomUids.end()) {
305 uids.insert(uids.end(), it->second.begin(), it->second.end());
306 }
307 uids.insert(uids.end(), mDefaultPullUids.begin(), mDefaultPullUids.end());
308 return uids;
309}
310
Yao Chen884c8c12018-01-26 10:36:25 -0800311void MetricsManager::dumpStates(FILE* out, bool verbose) {
312 fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str());
313 {
314 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
315 for (const auto& source : mAllowedLogSources) {
316 fprintf(out, "%d ", source);
317 }
318 }
319 fprintf(out, "\n");
320 for (const auto& producer : mAllMetricProducers) {
321 producer->dumpStates(out, verbose);
322 }
323}
324
Yangster-macb142cc82018-03-30 15:22:08 -0700325void MetricsManager::dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800326 for (const auto& producer : mAllMetricProducers) {
327 producer->dropData(dropTimeNs);
328 }
329}
330
Yangster-mace68f3a52018-04-04 00:01:43 -0700331void MetricsManager::onDumpReport(const int64_t dumpTimeStampNs,
332 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700333 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000334 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700335 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700336 ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -0700337 VLOG("=========================Metric Reports Start==========================");
338 // one StatsLogReport per MetricProduer
Yangster-mac94e197c2018-01-02 16:03:03 -0800339 for (const auto& producer : mAllMetricProducers) {
340 if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) {
Yangster-mac9def8e32018-04-17 13:55:51 -0700341 uint64_t token = protoOutput->start(
342 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
Yangster-mac1c58f042018-05-17 15:52:51 -0700343 if (mHashStringsInReport) {
Bookatzff71cad2018-09-20 17:17:49 -0700344 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000345 dumpLatency, str_set, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700346 } else {
Bookatzff71cad2018-09-20 17:17:49 -0700347 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000348 dumpLatency, nullptr, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700349 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800350 protoOutput->end(token);
Yangster-maca802d732018-04-24 07:50:38 -0700351 } else {
352 producer->clearPastBuckets(dumpTimeStampNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800353 }
Yao Chen729093d2017-10-16 10:33:26 -0700354 }
David Chenfaa1af52018-03-30 15:14:04 -0700355 for (const auto& annotation : mAnnotations) {
356 uint64_t token = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
357 FIELD_ID_ANNOTATIONS);
358 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ANNOTATIONS_INT64,
359 (long long)annotation.first);
360 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_ANNOTATIONS_INT32, annotation.second);
361 protoOutput->end(token);
362 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700363
Jeffrey Huang289eae62020-06-10 16:28:23 -0700364 // Do not update the timestamps when data is not cleared to avoid timestamps from being
365 // misaligned.
366 if (erase_data) {
367 mLastReportTimeNs = dumpTimeStampNs;
368 mLastReportWallClockNs = getWallClockNs();
369 }
Yao Chen729093d2017-10-16 10:33:26 -0700370 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -0700371}
372
Yao Chencaf339d2017-10-06 16:01:10 -0700373
Andrei Oneada01ea52019-01-30 15:28:36 +0000374bool MetricsManager::checkLogCredentials(const LogEvent& event) {
Muhammad Qureshif6816462020-05-13 15:13:33 -0700375 // TODO(b/154856835): Remove this check once we get whitelist from the config.
Andrei Oneada01ea52019-01-30 15:28:36 +0000376 if (android::util::AtomsInfo::kWhitelistedAtoms.find(event.GetTagId()) !=
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700377 android::util::AtomsInfo::kWhitelistedAtoms.end())
Andrei Oneada01ea52019-01-30 15:28:36 +0000378 {
379 return true;
380 }
Muhammad Qureshif6816462020-05-13 15:13:33 -0700381
382 if (mWhitelistedAtomIds.find(event.GetTagId()) != mWhitelistedAtomIds.end()) {
383 return true;
384 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000385 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
386 if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) {
387 VLOG("log source %d not on the whitelist", event.GetUid());
388 return false;
389 }
390 return true;
391}
392
393bool MetricsManager::eventSanityCheck(const LogEvent& event) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800394 if (event.GetTagId() == util::APP_BREADCRUMB_REPORTED) {
David Chenb639d142018-02-14 17:29:54 -0800395 // Check that app breadcrumb reported fields are valid.
David Chendaa9f3a2017-12-28 16:52:22 -0800396 status_t err = NO_ERROR;
Bookatzb223c4e2018-02-01 15:35:04 -0800397
398 // Uid is 3rd from last field and must match the caller's uid,
399 // unless that caller is statsd itself (statsd is allowed to spoof uids).
400 long appHookUid = event.GetLong(event.size()-2, &err);
Ruchir Rastogid4cc6932020-04-20 11:51:14 -0700401 if (err != NO_ERROR) {
David Chen77ef6712018-02-23 18:23:42 -0800402 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000403 return false;
David Chen77ef6712018-02-23 18:23:42 -0800404 }
Ruchir Rastogid4cc6932020-04-20 11:51:14 -0700405
406 // Because the uid within the LogEvent may have been mapped from
407 // isolated to host, map the loggerUid similarly before comparing.
408 int32_t loggerUid = mUidMap->getHostUidOrSelf(event.GetUid());
David Chen77ef6712018-02-23 18:23:42 -0800409 if (loggerUid != appHookUid && loggerUid != AID_STATSD) {
410 VLOG("APP_BREADCRUMB_REPORTED has invalid uid: claimed %ld but caller is %d",
411 appHookUid, loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000412 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800413 }
Bookatzb223c4e2018-02-01 15:35:04 -0800414
David Chendaa9f3a2017-12-28 16:52:22 -0800415 // The state must be from 0,3. This part of code must be manually updated.
Bookatzb223c4e2018-02-01 15:35:04 -0800416 long appHookState = event.GetLong(event.size(), &err);
Ruchir Rastogid4cc6932020-04-20 11:51:14 -0700417 if (err != NO_ERROR) {
David Chen77ef6712018-02-23 18:23:42 -0800418 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the state field");
Andrei Oneada01ea52019-01-30 15:28:36 +0000419 return false;
David Chen77ef6712018-02-23 18:23:42 -0800420 } else if (appHookState < 0 || appHookState > 3) {
421 VLOG("APP_BREADCRUMB_REPORTED does not have valid state %ld", appHookState);
Andrei Oneada01ea52019-01-30 15:28:36 +0000422 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800423 }
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800424 } else if (event.GetTagId() == util::DAVEY_OCCURRED) {
Tej Singhbb8554a2018-01-26 11:59:14 -0800425 // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp.
426 // Check that the davey duration is reasonable. Max length check is for privacy.
427 status_t err = NO_ERROR;
David Chen77ef6712018-02-23 18:23:42 -0800428
429 // Uid is the first field provided.
430 long jankUid = event.GetLong(1, &err);
Ruchir Rastogid4cc6932020-04-20 11:51:14 -0700431 if (err != NO_ERROR) {
David Chen77ef6712018-02-23 18:23:42 -0800432 VLOG("Davey occurred had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000433 return false;
David Chen77ef6712018-02-23 18:23:42 -0800434 }
435 int32_t loggerUid = event.GetUid();
436 if (loggerUid != jankUid && loggerUid != AID_STATSD) {
437 VLOG("DAVEY_OCCURRED has invalid uid: claimed %ld but caller is %d", jankUid,
438 loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000439 return false;
David Chen77ef6712018-02-23 18:23:42 -0800440 }
441
Tej Singhbb8554a2018-01-26 11:59:14 -0800442 long duration = event.GetLong(event.size(), &err);
Ruchir Rastogid4cc6932020-04-20 11:51:14 -0700443 if (err != NO_ERROR) {
David Chen77ef6712018-02-23 18:23:42 -0800444 VLOG("Davey occurred had error when parsing the duration");
Andrei Oneada01ea52019-01-30 15:28:36 +0000445 return false;
David Chen77ef6712018-02-23 18:23:42 -0800446 } else if (duration > 100000) {
Tej Singhbb8554a2018-01-26 11:59:14 -0800447 VLOG("Davey duration is unreasonably long: %ld", duration);
Andrei Oneada01ea52019-01-30 15:28:36 +0000448 return false;
Tej Singhbb8554a2018-01-26 11:59:14 -0800449 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000450 }
451
452 return true;
453}
454
455// Consume the stats log if it's interesting to this metric.
456void MetricsManager::onLogEvent(const LogEvent& event) {
457 if (!mConfigValid) {
458 return;
459 }
460
461 if (!checkLogCredentials(event)) {
462 return;
463 }
464
465 if (!eventSanityCheck(event)) {
466 return;
Yao Chend10f7b12017-12-18 12:53:50 -0800467 }
468
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700469 int tagId = event.GetTagId();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700470 int64_t eventTimeNs = event.GetElapsedTimestampNs();
471
Tej Singh6ede28b2019-01-29 17:06:54 -0800472 bool isActive = mIsAlwaysActive;
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700473
474 // Set of metrics that are still active after flushing.
475 unordered_set<int> activeMetricsIndices;
476
477 // Update state of all metrics w/ activation conditions as of eventTimeNs.
478 for (int metricIndex : mMetricIndexesWithActivation) {
479 const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex];
480 metric->flushIfExpire(eventTimeNs);
481 if (metric->isActive()) {
482 // If this metric w/ activation condition is still active after
483 // flushing, remember it.
484 activeMetricsIndices.insert(metricIndex);
485 }
Yangster-mac849dfdc22018-10-12 15:41:45 -0700486 }
487
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700488 mIsActive = isActive || !activeMetricsIndices.empty();
Tej Singh6ede28b2019-01-29 17:06:54 -0800489
Yao Chen44cf27c2017-09-14 22:32:50 -0700490 if (mTagIds.find(tagId) == mTagIds.end()) {
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700491 // Not interesting...
Yao Chen44cf27c2017-09-14 22:32:50 -0700492 return;
493 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700494
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800495 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700496
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700497 // Evaluate all atom matchers.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800498 for (auto& matcher : mAllAtomMatchers) {
499 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700500 }
501
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700502 // Set of metrics that received an activation cancellation.
503 unordered_set<int> metricIndicesWithCanceledActivations;
504
505 // Determine which metric activations received a cancellation and cancel them.
506 for (const auto& it : mDeactivationAtomTrackerToMetricMap) {
507 if (matcherCache[it.first] == MatchingState::kMatched) {
508 for (int metricIndex : it.second) {
509 mAllMetricProducers[metricIndex]->cancelEventActivation(it.first);
510 metricIndicesWithCanceledActivations.insert(metricIndex);
511 }
512 }
513 }
514
515 // Determine whether any metrics are no longer active after cancelling metric activations.
516 for (const int metricIndex : metricIndicesWithCanceledActivations) {
517 const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex];
518 metric->flushIfExpire(eventTimeNs);
519 if (!metric->isActive()) {
520 activeMetricsIndices.erase(metricIndex);
521 }
522 }
523
524 isActive |= !activeMetricsIndices.empty();
525
526
527 // Determine which metric activations should be turned on and turn them on
Yangster-mac849dfdc22018-10-12 15:41:45 -0700528 for (const auto& it : mActivationAtomTrackerToMetricMap) {
529 if (matcherCache[it.first] == MatchingState::kMatched) {
530 for (int metricIndex : it.second) {
531 mAllMetricProducers[metricIndex]->activate(it.first, eventTimeNs);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800532 isActive |= mAllMetricProducers[metricIndex]->isActive();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700533 }
534 }
535 }
536
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800537 mIsActive = isActive;
538
Yao Chencaf339d2017-10-06 16:01:10 -0700539 // A bitmap to see which ConditionTracker needs to be re-evaluated.
540 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
541
542 for (const auto& pair : mTrackerToConditionMap) {
543 if (matcherCache[pair.first] == MatchingState::kMatched) {
544 const auto& conditionList = pair.second;
545 for (const int conditionIndex : conditionList) {
546 conditionToBeEvaluated[conditionIndex] = true;
547 }
548 }
549 }
550
551 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
552 ConditionState::kNotEvaluated);
553 // A bitmap to track if a condition has changed value.
554 vector<bool> changedCache(mAllConditionTrackers.size(), false);
555 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
556 if (conditionToBeEvaluated[i] == false) {
557 continue;
558 }
Yao Chencaf339d2017-10-06 16:01:10 -0700559 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
560 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800561 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700562 }
563
564 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800565 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700566 continue;
567 }
568 auto pair = mConditionToMetricMap.find(i);
569 if (pair != mConditionToMetricMap.end()) {
570 auto& metricList = pair->second;
571 for (auto metricIndex : metricList) {
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700572 // Metric cares about non sliced condition, and it's changed.
Yao Chen729093d2017-10-16 10:33:26 -0700573 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800574 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a372017-10-30 22:57:06 -0700575 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700576 eventTimeNs);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700577 // Metric cares about sliced conditions, and it may have changed. Send
Yao Chen729093d2017-10-16 10:33:26 -0700578 // notification, and the metric can query the sliced conditions that are
579 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800580 } else {
Yao Chen427d3722018-03-22 15:21:52 -0700581 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700582 eventTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700583 }
Yao Chencaf339d2017-10-06 16:01:10 -0700584 }
585 }
586 }
587
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800588 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
589 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700590 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800591 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Yangster-mac94e197c2018-01-02 16:03:03 -0800592 mAllAtomMatchers[i]->getId());
Yao Chencaf339d2017-10-06 16:01:10 -0700593 auto pair = mTrackerToMetricMap.find(i);
594 if (pair != mTrackerToMetricMap.end()) {
595 auto& metricList = pair->second;
596 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700597 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800598 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700599 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700600 }
601 }
602 }
603}
604
Yangster-mac932ecec2018-02-01 10:23:52 -0800605void MetricsManager::onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700606 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800607 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800608 for (const auto& itr : mAllAnomalyTrackers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800609 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800610 }
611}
612
Yangster-mac932ecec2018-02-01 10:23:52 -0800613void MetricsManager::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700614 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800615 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
616 for (const auto& itr : mAllPeriodicAlarmTrackers) {
617 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800618 }
619}
620
yro69007c82017-10-26 20:42:57 -0700621// Returns the total byte size of all metrics managed by a single config source.
622size_t MetricsManager::byteSize() {
623 size_t totalSize = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800624 for (const auto& metricProducer : mAllMetricProducers) {
yro69007c82017-10-26 20:42:57 -0700625 totalSize += metricProducer->byteSize();
626 }
627 return totalSize;
628}
629
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700630void MetricsManager::loadActiveConfig(const ActiveConfig& config, int64_t currentTimeNs) {
631 if (config.metric_size() == 0) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800632 ALOGW("No active metric for config %s", mConfigKey.ToString().c_str());
633 return;
634 }
635
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700636 for (int i = 0; i < config.metric_size(); i++) {
637 const auto& activeMetric = config.metric(i);
638 for (int metricIndex : mMetricIndexesWithActivation) {
639 const auto& metric = mAllMetricProducers[metricIndex];
640 if (metric->getMetricId() == activeMetric.id()) {
641 VLOG("Setting active metric: %lld", (long long)metric->getMetricId());
642 metric->loadActiveMetric(activeMetric, currentTimeNs);
Tej Singh16ca28f2019-06-24 11:58:23 -0700643 if (!mIsActive && metric->isActive()) {
644 StatsdStats::getInstance().noteActiveStatusChanged(mConfigKey,
645 /*activate=*/ true);
646 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700647 mIsActive |= metric->isActive();
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800648 }
649 }
650 }
651}
652
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700653void MetricsManager::writeActiveConfigToProtoOutputStream(
Tej Singhf53d4452019-05-09 18:17:59 -0700654 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700655 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ACTIVE_CONFIG_ID, (long long)mConfigKey.GetId());
656 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVE_CONFIG_UID, mConfigKey.GetUid());
657 for (int metricIndex : mMetricIndexesWithActivation) {
658 const auto& metric = mAllMetricProducers[metricIndex];
659 const uint64_t metricToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
660 FIELD_ID_ACTIVE_CONFIG_METRIC);
Tej Singhf53d4452019-05-09 18:17:59 -0700661 metric->writeActiveMetricToProtoOutputStream(currentTimeNs, reason, proto);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700662 proto->end(metricToken);
663 }
664}
665
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700666bool MetricsManager::writeMetadataToProto(int64_t currentWallClockTimeNs,
667 int64_t systemElapsedTimeNs,
668 metadata::StatsMetadata* statsMetadata) {
669 bool metadataWritten = false;
670 metadata::ConfigKey* configKey = statsMetadata->mutable_config_key();
671 configKey->set_config_id(mConfigKey.GetId());
672 configKey->set_uid(mConfigKey.GetUid());
673 for (const auto& anomalyTracker : mAllAnomalyTrackers) {
674 metadata::AlertMetadata* alertMetadata = statsMetadata->add_alert_metadata();
675 bool alertWritten = anomalyTracker->writeAlertMetadataToProto(currentWallClockTimeNs,
676 systemElapsedTimeNs, alertMetadata);
677 if (!alertWritten) {
678 statsMetadata->mutable_alert_metadata()->RemoveLast();
679 }
680 metadataWritten |= alertWritten;
681 }
682 return metadataWritten;
683}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700684
Jeffrey Huang475677e2020-03-30 19:52:07 -0700685void MetricsManager::loadMetadata(const metadata::StatsMetadata& metadata,
686 int64_t currentWallClockTimeNs,
687 int64_t systemElapsedTimeNs) {
688 for (const metadata::AlertMetadata& alertMetadata : metadata.alert_metadata()) {
689 int64_t alertId = alertMetadata.alert_id();
690 auto it = mAlertTrackerMap.find(alertId);
691 if (it == mAlertTrackerMap.end()) {
692 ALOGE("No anomalyTracker found for alertId %lld", (long long) alertId);
693 continue;
694 }
695 mAllAnomalyTrackers[it->second]->loadAlertMetadata(alertMetadata,
696 currentWallClockTimeNs,
697 systemElapsedTimeNs);
698 }
699}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700700
Yao Chen44cf27c2017-09-14 22:32:50 -0700701} // namespace statsd
702} // namespace os
703} // namespace android