blob: d832ed86580d0144833be54c8d275d794cc6437b [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),
Yao Chen9a43b4f2019-04-10 10:43:20 -070074 mShouldPersistHistory(config.persist_locally()) {
Yangster-macb142cc82018-03-30 15:22:08 -070075 // Init the ttl end timestamp.
Yangster-mac15f6bbc2018-04-08 11:52:26 -070076 refreshTtl(timeBaseNs);
Yangster-macb142cc82018-03-30 15:22:08 -070077
Chenjie Yue2219202018-06-08 10:07:51 -070078 mConfigValid = initStatsdConfig(
79 key, config, *uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor,
80 timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
81 mAllMetricProducers, mAllAnomalyTrackers, mAllPeriodicAlarmTrackers,
Yangster-mac849dfdc22018-10-12 15:41:45 -070082 mConditionToMetricMap, mTrackerToMetricMap, mTrackerToConditionMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -070083 mActivationAtomTrackerToMetricMap, mDeactivationAtomTrackerToMetricMap,
Jeffrey Huang38d70262020-03-19 17:28:59 -070084 mAlertTrackerMap, mMetricIndexesWithActivation, mNoReportMetricIds);
Yao Chenb3561512017-11-21 18:07:17 -080085
Yangster-mac1c58f042018-05-17 15:52:51 -070086 mHashStringsInReport = config.hash_strings_in_metric_report();
dwchen730403e2018-10-29 11:41:56 -070087 mVersionStringsInReport = config.version_strings_in_metric_report();
88 mInstallerInReport = config.installer_in_metric_report();
Yangster-mac1c58f042018-05-17 15:52:51 -070089
Tej Singh3be093b2020-03-04 20:08:38 -080090 // Init allowed pushed atom uids.
Yao Chen147ce602017-12-22 14:35:34 -080091 if (config.allowed_log_source_size() == 0) {
David Chen8faaa012018-02-28 15:54:36 -080092 mConfigValid = false;
93 ALOGE("Log source whitelist is empty! This config won't get any data. Suggest adding at "
94 "least AID_SYSTEM and AID_STATSD to the allowed_log_source field.");
Yao Chend10f7b12017-12-18 12:53:50 -080095 } else {
Yao Chen147ce602017-12-22 14:35:34 -080096 for (const auto& source : config.allowed_log_source()) {
97 auto it = UidMap::sAidToUidMapping.find(source);
98 if (it != UidMap::sAidToUidMapping.end()) {
99 mAllowedUid.push_back(it->second);
100 } else {
101 mAllowedPkg.push_back(source);
102 }
103 }
Yao Chend10f7b12017-12-18 12:53:50 -0800104
105 if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
106 ALOGE("Too many log sources. This is likely to be an error in the config.");
107 mConfigValid = false;
108 } else {
109 initLogSourceWhiteList();
110 }
111 }
112
Tej Singh3be093b2020-03-04 20:08:38 -0800113 // Init default allowed pull atom uids.
114 int numPullPackages = 0;
115 for (const string& pullSource : config.default_pull_packages()) {
116 auto it = UidMap::sAidToUidMapping.find(pullSource);
117 if (it != UidMap::sAidToUidMapping.end()) {
118 numPullPackages++;
119 mDefaultPullUids.insert(it->second);
120 } else {
121 ALOGE("Default pull atom packages must be in sAidToUidMapping");
122 mConfigValid = false;
123 }
124 }
125 // Init per-atom pull atom packages.
126 for (const PullAtomPackages& pullAtomPackages : config.pull_atom_packages()) {
127 int32_t atomId = pullAtomPackages.atom_id();
128 for (const string& pullPackage : pullAtomPackages.packages()) {
129 numPullPackages++;
130 auto it = UidMap::sAidToUidMapping.find(pullPackage);
131 if (it != UidMap::sAidToUidMapping.end()) {
132 mPullAtomUids[atomId].insert(it->second);
133 } else {
134 mPullAtomPackages[atomId].insert(pullPackage);
135 }
136 }
137 }
138 if (numPullPackages > StatsdStats::kMaxPullAtomPackages) {
139 ALOGE("Too many sources in default_pull_packages and pull_atom_packages. This is likely to "
140 "be an error in the config");
141 mConfigValid = false;
142 } else {
143 initPullAtomSources();
144 }
145 mPullerManager->RegisterPullUidProvider(mConfigKey, this);
146
David Chenfaa1af52018-03-30 15:14:04 -0700147 // Store the sub-configs used.
148 for (const auto& annotation : config.annotation()) {
149 mAnnotations.emplace_back(annotation.field_int64(), annotation.field_int32());
150 }
151
Yao Chenb3561512017-11-21 18:07:17 -0800152 // Guardrail. Reject the config if it's too big.
153 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
154 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800155 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -0800156 ALOGE("This config is too big! Reject!");
157 mConfigValid = false;
158 }
Bookatz1476ef22018-02-13 12:26:01 -0800159 if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) {
160 ALOGE("This config has too many alerts! Reject!");
161 mConfigValid = false;
162 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800163
164 mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) ||
165 (mAllMetricProducers.size() == 0);
166 bool isActive = mIsAlwaysActive;
167 for (int metric : mMetricIndexesWithActivation) {
168 isActive |= mAllMetricProducers[metric]->isActive();
169 }
170 mIsActive = isActive;
171 VLOG("mIsActive is initialized to %d", mIsActive)
172
Yao Chenf09569f2017-12-13 17:00:51 -0800173 // no matter whether this config is valid, log it in the stats.
David Chenfaa1af52018-03-30 15:14:04 -0700174 StatsdStats::getInstance().noteConfigReceived(
175 key, mAllMetricProducers.size(), mAllConditionTrackers.size(), mAllAtomMatchers.size(),
176 mAllAnomalyTrackers.size(), mAnnotations, mConfigValid);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800177 // Check active
178 for (const auto& metric : mAllMetricProducers) {
179 if (metric->isActive()) {
180 mIsActive = true;
181 break;
182 }
183 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700184}
185
186MetricsManager::~MetricsManager() {
tsaichristined21aacf2019-10-07 14:47:38 -0700187 for (auto it : mAllMetricProducers) {
188 for (int atomId : it->getSlicedStateAtoms()) {
189 StateManager::getInstance().unregisterListener(atomId, it);
190 }
191 }
Tej Singh3be093b2020-03-04 20:08:38 -0800192 mPullerManager->UnregisterPullUidProvider(mConfigKey);
tsaichristined21aacf2019-10-07 14:47:38 -0700193
Bookatzd3606c72017-10-19 10:13:49 -0700194 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -0700195}
196
Yao Chend10f7b12017-12-18 12:53:50 -0800197void MetricsManager::initLogSourceWhiteList() {
198 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
199 mAllowedLogSources.clear();
200 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
201
202 for (const auto& pkg : mAllowedPkg) {
203 auto uids = mUidMap->getAppUid(pkg);
204 mAllowedLogSources.insert(uids.begin(), uids.end());
205 }
206 if (DEBUG) {
207 for (const auto& uid : mAllowedLogSources) {
208 VLOG("Allowed uid %d", uid);
209 }
210 }
211}
212
Tej Singh3be093b2020-03-04 20:08:38 -0800213void MetricsManager::initPullAtomSources() {
214 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
215 mCombinedPullAtomUids.clear();
216 for (const auto& [atomId, uids] : mPullAtomUids) {
217 mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end());
218 }
219 for (const auto& [atomId, packages] : mPullAtomPackages) {
220 for (const string& pkg : packages) {
221 set<int32_t> uids = mUidMap->getAppUid(pkg);
222 mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end());
223 }
224 }
225}
226
Yao Chencaf339d2017-10-06 16:01:10 -0700227bool MetricsManager::isConfigValid() const {
228 return mConfigValid;
229}
230
Yangster-macb142cc82018-03-30 15:22:08 -0700231void MetricsManager::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -0800232 const int64_t version) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800233 // Inform all metric producers.
234 for (auto it : mAllMetricProducers) {
235 it->notifyAppUpgrade(eventTimeNs, apk, uid, version);
236 }
Yao Chend10f7b12017-12-18 12:53:50 -0800237 // check if we care this package
Tej Singh3be093b2020-03-04 20:08:38 -0800238 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) {
239 // We will re-initialize the whole list because we don't want to keep the multi mapping of
240 // UID<->pkg inside MetricsManager to reduce the memory usage.
241 initLogSourceWhiteList();
Yao Chend10f7b12017-12-18 12:53:50 -0800242 }
Tej Singh3be093b2020-03-04 20:08:38 -0800243
244 for (const auto& it : mPullAtomPackages) {
245 if (it.second.find(apk) != it.second.end()) {
246 initPullAtomSources();
247 return;
248 }
249 }
Yao Chend10f7b12017-12-18 12:53:50 -0800250}
251
Yangster-macb142cc82018-03-30 15:22:08 -0700252void MetricsManager::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
David Chen27785a82018-01-19 17:06:45 -0800253 const int uid) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800254 // Inform all metric producers.
255 for (auto it : mAllMetricProducers) {
256 it->notifyAppRemoved(eventTimeNs, apk, uid);
257 }
Yao Chend10f7b12017-12-18 12:53:50 -0800258 // check if we care this package
Tej Singh3be093b2020-03-04 20:08:38 -0800259 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) {
260 // We will re-initialize the whole list because we don't want to keep the multi mapping of
261 // UID<->pkg inside MetricsManager to reduce the memory usage.
262 initLogSourceWhiteList();
Yao Chend10f7b12017-12-18 12:53:50 -0800263 }
Tej Singh3be093b2020-03-04 20:08:38 -0800264
265 for (const auto& it : mPullAtomPackages) {
266 if (it.second.find(apk) != it.second.end()) {
267 initPullAtomSources();
268 return;
269 }
270 }
Yao Chend10f7b12017-12-18 12:53:50 -0800271}
272
Yangster-macb142cc82018-03-30 15:22:08 -0700273void MetricsManager::onUidMapReceived(const int64_t& eventTimeNs) {
Tej Singh9ec159a2019-11-14 11:59:48 -0800274 // Purposefully don't inform metric producers on a new snapshot
275 // because we don't need to flush partial buckets.
276 // This occurs if a new user is added/removed or statsd crashes.
Tej Singh3be093b2020-03-04 20:08:38 -0800277 initPullAtomSources();
278
Yao Chend10f7b12017-12-18 12:53:50 -0800279 if (mAllowedPkg.size() == 0) {
280 return;
281 }
282 initLogSourceWhiteList();
283}
284
Tej Singh3be093b2020-03-04 20:08:38 -0800285void MetricsManager::init() {
286 for (const auto& producer : mAllMetricProducers) {
287 producer->prepareFirstBucket();
288 }
289}
290
291vector<int32_t> MetricsManager::getPullAtomUids(int32_t atomId) {
292 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
293 vector<int32_t> uids;
294 const auto& it = mCombinedPullAtomUids.find(atomId);
295 if (it != mCombinedPullAtomUids.end()) {
296 uids.insert(uids.end(), it->second.begin(), it->second.end());
297 }
298 uids.insert(uids.end(), mDefaultPullUids.begin(), mDefaultPullUids.end());
299 return uids;
300}
301
Yao Chen884c8c12018-01-26 10:36:25 -0800302void MetricsManager::dumpStates(FILE* out, bool verbose) {
303 fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str());
304 {
305 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
306 for (const auto& source : mAllowedLogSources) {
307 fprintf(out, "%d ", source);
308 }
309 }
310 fprintf(out, "\n");
311 for (const auto& producer : mAllMetricProducers) {
312 producer->dumpStates(out, verbose);
313 }
314}
315
Yangster-macb142cc82018-03-30 15:22:08 -0700316void MetricsManager::dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800317 for (const auto& producer : mAllMetricProducers) {
318 producer->dropData(dropTimeNs);
319 }
320}
321
Yangster-mace68f3a52018-04-04 00:01:43 -0700322void MetricsManager::onDumpReport(const int64_t dumpTimeStampNs,
323 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700324 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000325 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700326 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700327 ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -0700328 VLOG("=========================Metric Reports Start==========================");
329 // one StatsLogReport per MetricProduer
Yangster-mac94e197c2018-01-02 16:03:03 -0800330 for (const auto& producer : mAllMetricProducers) {
331 if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) {
Yangster-mac9def8e32018-04-17 13:55:51 -0700332 uint64_t token = protoOutput->start(
333 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
Yangster-mac1c58f042018-05-17 15:52:51 -0700334 if (mHashStringsInReport) {
Bookatzff71cad2018-09-20 17:17:49 -0700335 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000336 dumpLatency, str_set, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700337 } else {
Bookatzff71cad2018-09-20 17:17:49 -0700338 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000339 dumpLatency, nullptr, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700340 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800341 protoOutput->end(token);
Yangster-maca802d732018-04-24 07:50:38 -0700342 } else {
343 producer->clearPastBuckets(dumpTimeStampNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800344 }
Yao Chen729093d2017-10-16 10:33:26 -0700345 }
David Chenfaa1af52018-03-30 15:14:04 -0700346 for (const auto& annotation : mAnnotations) {
347 uint64_t token = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
348 FIELD_ID_ANNOTATIONS);
349 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ANNOTATIONS_INT64,
350 (long long)annotation.first);
351 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_ANNOTATIONS_INT32, annotation.second);
352 protoOutput->end(token);
353 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700354
Yangster-mac330af582018-02-08 15:24:38 -0800355 mLastReportTimeNs = dumpTimeStampNs;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -0800356 mLastReportWallClockNs = getWallClockNs();
Yao Chen729093d2017-10-16 10:33:26 -0700357 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -0700358}
359
Yao Chencaf339d2017-10-06 16:01:10 -0700360
Andrei Oneada01ea52019-01-30 15:28:36 +0000361bool MetricsManager::checkLogCredentials(const LogEvent& event) {
362 if (android::util::AtomsInfo::kWhitelistedAtoms.find(event.GetTagId()) !=
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700363 android::util::AtomsInfo::kWhitelistedAtoms.end())
Andrei Oneada01ea52019-01-30 15:28:36 +0000364 {
365 return true;
366 }
367 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
368 if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) {
369 VLOG("log source %d not on the whitelist", event.GetUid());
370 return false;
371 }
372 return true;
373}
374
375bool MetricsManager::eventSanityCheck(const LogEvent& event) {
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800376 if (event.GetTagId() == util::APP_BREADCRUMB_REPORTED) {
David Chenb639d142018-02-14 17:29:54 -0800377 // Check that app breadcrumb reported fields are valid.
David Chendaa9f3a2017-12-28 16:52:22 -0800378 status_t err = NO_ERROR;
Bookatzb223c4e2018-02-01 15:35:04 -0800379
380 // Uid is 3rd from last field and must match the caller's uid,
381 // unless that caller is statsd itself (statsd is allowed to spoof uids).
382 long appHookUid = event.GetLong(event.size()-2, &err);
David Chen77ef6712018-02-23 18:23:42 -0800383 if (err != NO_ERROR ) {
384 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000385 return false;
David Chen77ef6712018-02-23 18:23:42 -0800386 }
Bookatzb223c4e2018-02-01 15:35:04 -0800387 int32_t loggerUid = event.GetUid();
David Chen77ef6712018-02-23 18:23:42 -0800388 if (loggerUid != appHookUid && loggerUid != AID_STATSD) {
389 VLOG("APP_BREADCRUMB_REPORTED has invalid uid: claimed %ld but caller is %d",
390 appHookUid, loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000391 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800392 }
Bookatzb223c4e2018-02-01 15:35:04 -0800393
David Chendaa9f3a2017-12-28 16:52:22 -0800394 // The state must be from 0,3. This part of code must be manually updated.
Bookatzb223c4e2018-02-01 15:35:04 -0800395 long appHookState = event.GetLong(event.size(), &err);
David Chen77ef6712018-02-23 18:23:42 -0800396 if (err != NO_ERROR ) {
397 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the state field");
Andrei Oneada01ea52019-01-30 15:28:36 +0000398 return false;
David Chen77ef6712018-02-23 18:23:42 -0800399 } else if (appHookState < 0 || appHookState > 3) {
400 VLOG("APP_BREADCRUMB_REPORTED does not have valid state %ld", appHookState);
Andrei Oneada01ea52019-01-30 15:28:36 +0000401 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800402 }
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800403 } else if (event.GetTagId() == util::DAVEY_OCCURRED) {
Tej Singhbb8554a2018-01-26 11:59:14 -0800404 // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp.
405 // Check that the davey duration is reasonable. Max length check is for privacy.
406 status_t err = NO_ERROR;
David Chen77ef6712018-02-23 18:23:42 -0800407
408 // Uid is the first field provided.
409 long jankUid = event.GetLong(1, &err);
410 if (err != NO_ERROR ) {
411 VLOG("Davey occurred had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000412 return false;
David Chen77ef6712018-02-23 18:23:42 -0800413 }
414 int32_t loggerUid = event.GetUid();
415 if (loggerUid != jankUid && loggerUid != AID_STATSD) {
416 VLOG("DAVEY_OCCURRED has invalid uid: claimed %ld but caller is %d", jankUid,
417 loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000418 return false;
David Chen77ef6712018-02-23 18:23:42 -0800419 }
420
Tej Singhbb8554a2018-01-26 11:59:14 -0800421 long duration = event.GetLong(event.size(), &err);
David Chen77ef6712018-02-23 18:23:42 -0800422 if (err != NO_ERROR ) {
423 VLOG("Davey occurred had error when parsing the duration");
Andrei Oneada01ea52019-01-30 15:28:36 +0000424 return false;
David Chen77ef6712018-02-23 18:23:42 -0800425 } else if (duration > 100000) {
Tej Singhbb8554a2018-01-26 11:59:14 -0800426 VLOG("Davey duration is unreasonably long: %ld", duration);
Andrei Oneada01ea52019-01-30 15:28:36 +0000427 return false;
Tej Singhbb8554a2018-01-26 11:59:14 -0800428 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000429 }
430
431 return true;
432}
433
434// Consume the stats log if it's interesting to this metric.
435void MetricsManager::onLogEvent(const LogEvent& event) {
436 if (!mConfigValid) {
437 return;
438 }
439
440 if (!checkLogCredentials(event)) {
441 return;
442 }
443
444 if (!eventSanityCheck(event)) {
445 return;
Yao Chend10f7b12017-12-18 12:53:50 -0800446 }
447
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700448 int tagId = event.GetTagId();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700449 int64_t eventTimeNs = event.GetElapsedTimestampNs();
450
Tej Singh6ede28b2019-01-29 17:06:54 -0800451 bool isActive = mIsAlwaysActive;
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700452
453 // Set of metrics that are still active after flushing.
454 unordered_set<int> activeMetricsIndices;
455
456 // Update state of all metrics w/ activation conditions as of eventTimeNs.
457 for (int metricIndex : mMetricIndexesWithActivation) {
458 const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex];
459 metric->flushIfExpire(eventTimeNs);
460 if (metric->isActive()) {
461 // If this metric w/ activation condition is still active after
462 // flushing, remember it.
463 activeMetricsIndices.insert(metricIndex);
464 }
Yangster-mac849dfdc22018-10-12 15:41:45 -0700465 }
466
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700467 mIsActive = isActive || !activeMetricsIndices.empty();
Tej Singh6ede28b2019-01-29 17:06:54 -0800468
Yao Chen44cf27c2017-09-14 22:32:50 -0700469 if (mTagIds.find(tagId) == mTagIds.end()) {
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700470 // Not interesting...
Yao Chen44cf27c2017-09-14 22:32:50 -0700471 return;
472 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700473
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800474 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700475
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700476 // Evaluate all atom matchers.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800477 for (auto& matcher : mAllAtomMatchers) {
478 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700479 }
480
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700481 // Set of metrics that received an activation cancellation.
482 unordered_set<int> metricIndicesWithCanceledActivations;
483
484 // Determine which metric activations received a cancellation and cancel them.
485 for (const auto& it : mDeactivationAtomTrackerToMetricMap) {
486 if (matcherCache[it.first] == MatchingState::kMatched) {
487 for (int metricIndex : it.second) {
488 mAllMetricProducers[metricIndex]->cancelEventActivation(it.first);
489 metricIndicesWithCanceledActivations.insert(metricIndex);
490 }
491 }
492 }
493
494 // Determine whether any metrics are no longer active after cancelling metric activations.
495 for (const int metricIndex : metricIndicesWithCanceledActivations) {
496 const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex];
497 metric->flushIfExpire(eventTimeNs);
498 if (!metric->isActive()) {
499 activeMetricsIndices.erase(metricIndex);
500 }
501 }
502
503 isActive |= !activeMetricsIndices.empty();
504
505
506 // Determine which metric activations should be turned on and turn them on
Yangster-mac849dfdc22018-10-12 15:41:45 -0700507 for (const auto& it : mActivationAtomTrackerToMetricMap) {
508 if (matcherCache[it.first] == MatchingState::kMatched) {
509 for (int metricIndex : it.second) {
510 mAllMetricProducers[metricIndex]->activate(it.first, eventTimeNs);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800511 isActive |= mAllMetricProducers[metricIndex]->isActive();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700512 }
513 }
514 }
515
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800516 mIsActive = isActive;
517
Yao Chencaf339d2017-10-06 16:01:10 -0700518 // A bitmap to see which ConditionTracker needs to be re-evaluated.
519 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
520
521 for (const auto& pair : mTrackerToConditionMap) {
522 if (matcherCache[pair.first] == MatchingState::kMatched) {
523 const auto& conditionList = pair.second;
524 for (const int conditionIndex : conditionList) {
525 conditionToBeEvaluated[conditionIndex] = true;
526 }
527 }
528 }
529
530 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
531 ConditionState::kNotEvaluated);
532 // A bitmap to track if a condition has changed value.
533 vector<bool> changedCache(mAllConditionTrackers.size(), false);
534 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
535 if (conditionToBeEvaluated[i] == false) {
536 continue;
537 }
Yao Chencaf339d2017-10-06 16:01:10 -0700538 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
539 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800540 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700541 }
542
543 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800544 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700545 continue;
546 }
547 auto pair = mConditionToMetricMap.find(i);
548 if (pair != mConditionToMetricMap.end()) {
549 auto& metricList = pair->second;
550 for (auto metricIndex : metricList) {
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700551 // Metric cares about non sliced condition, and it's changed.
Yao Chen729093d2017-10-16 10:33:26 -0700552 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800553 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700554 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700555 eventTimeNs);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700556 // Metric cares about sliced conditions, and it may have changed. Send
Yao Chen729093d2017-10-16 10:33:26 -0700557 // notification, and the metric can query the sliced conditions that are
558 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800559 } else {
Yao Chen427d3722018-03-22 15:21:52 -0700560 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700561 eventTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700562 }
Yao Chencaf339d2017-10-06 16:01:10 -0700563 }
564 }
565 }
566
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800567 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
568 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700569 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800570 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Yangster-mac94e197c2018-01-02 16:03:03 -0800571 mAllAtomMatchers[i]->getId());
Yao Chencaf339d2017-10-06 16:01:10 -0700572 auto pair = mTrackerToMetricMap.find(i);
573 if (pair != mTrackerToMetricMap.end()) {
574 auto& metricList = pair->second;
575 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700576 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800577 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700578 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700579 }
580 }
581 }
582}
583
Yangster-mac932ecec2018-02-01 10:23:52 -0800584void MetricsManager::onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700585 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800586 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800587 for (const auto& itr : mAllAnomalyTrackers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800588 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800589 }
590}
591
Yangster-mac932ecec2018-02-01 10:23:52 -0800592void MetricsManager::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700593 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800594 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
595 for (const auto& itr : mAllPeriodicAlarmTrackers) {
596 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800597 }
598}
599
yro69007c82017-10-26 20:42:57 -0700600// Returns the total byte size of all metrics managed by a single config source.
601size_t MetricsManager::byteSize() {
602 size_t totalSize = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800603 for (const auto& metricProducer : mAllMetricProducers) {
yro69007c82017-10-26 20:42:57 -0700604 totalSize += metricProducer->byteSize();
605 }
606 return totalSize;
607}
608
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700609void MetricsManager::loadActiveConfig(const ActiveConfig& config, int64_t currentTimeNs) {
610 if (config.metric_size() == 0) {
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800611 ALOGW("No active metric for config %s", mConfigKey.ToString().c_str());
612 return;
613 }
614
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700615 for (int i = 0; i < config.metric_size(); i++) {
616 const auto& activeMetric = config.metric(i);
617 for (int metricIndex : mMetricIndexesWithActivation) {
618 const auto& metric = mAllMetricProducers[metricIndex];
619 if (metric->getMetricId() == activeMetric.id()) {
620 VLOG("Setting active metric: %lld", (long long)metric->getMetricId());
621 metric->loadActiveMetric(activeMetric, currentTimeNs);
Tej Singh16ca28f2019-06-24 11:58:23 -0700622 if (!mIsActive && metric->isActive()) {
623 StatsdStats::getInstance().noteActiveStatusChanged(mConfigKey,
624 /*activate=*/ true);
625 }
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700626 mIsActive |= metric->isActive();
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800627 }
628 }
629 }
630}
631
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700632void MetricsManager::writeActiveConfigToProtoOutputStream(
Tej Singhf53d4452019-05-09 18:17:59 -0700633 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700634 proto->write(FIELD_TYPE_INT64 | FIELD_ID_ACTIVE_CONFIG_ID, (long long)mConfigKey.GetId());
635 proto->write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVE_CONFIG_UID, mConfigKey.GetUid());
636 for (int metricIndex : mMetricIndexesWithActivation) {
637 const auto& metric = mAllMetricProducers[metricIndex];
638 const uint64_t metricToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
639 FIELD_ID_ACTIVE_CONFIG_METRIC);
Tej Singhf53d4452019-05-09 18:17:59 -0700640 metric->writeActiveMetricToProtoOutputStream(currentTimeNs, reason, proto);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700641 proto->end(metricToken);
642 }
643}
644
Jeffrey Huangb8f54032020-03-23 13:42:42 -0700645bool MetricsManager::writeMetadataToProto(int64_t currentWallClockTimeNs,
646 int64_t systemElapsedTimeNs,
647 metadata::StatsMetadata* statsMetadata) {
648 bool metadataWritten = false;
649 metadata::ConfigKey* configKey = statsMetadata->mutable_config_key();
650 configKey->set_config_id(mConfigKey.GetId());
651 configKey->set_uid(mConfigKey.GetUid());
652 for (const auto& anomalyTracker : mAllAnomalyTrackers) {
653 metadata::AlertMetadata* alertMetadata = statsMetadata->add_alert_metadata();
654 bool alertWritten = anomalyTracker->writeAlertMetadataToProto(currentWallClockTimeNs,
655 systemElapsedTimeNs, alertMetadata);
656 if (!alertWritten) {
657 statsMetadata->mutable_alert_metadata()->RemoveLast();
658 }
659 metadataWritten |= alertWritten;
660 }
661 return metadataWritten;
662}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700663
Jeffrey Huang475677e2020-03-30 19:52:07 -0700664void MetricsManager::loadMetadata(const metadata::StatsMetadata& metadata,
665 int64_t currentWallClockTimeNs,
666 int64_t systemElapsedTimeNs) {
667 for (const metadata::AlertMetadata& alertMetadata : metadata.alert_metadata()) {
668 int64_t alertId = alertMetadata.alert_id();
669 auto it = mAlertTrackerMap.find(alertId);
670 if (it == mAlertTrackerMap.end()) {
671 ALOGE("No anomalyTracker found for alertId %lld", (long long) alertId);
672 continue;
673 }
674 mAllAnomalyTrackers[it->second]->loadAlertMetadata(alertMetadata,
675 currentWallClockTimeNs,
676 systemElapsedTimeNs);
677 }
678}
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700679
Yao Chen44cf27c2017-09-14 22:32:50 -0700680} // namespace statsd
681} // namespace os
682} // namespace android