blob: 4b3bfd3c8b861b3bc0699d424eaee4e0e18b3bdd [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"
Yao Chen44cf27c2017-09-14 22:32:50 -070018#include "MetricsManager.h"
Yao Chend10f7b12017-12-18 12:53:50 -080019#include "statslog.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070020
Yao Chen44cf27c2017-09-14 22:32:50 -070021#include "CountMetricProducer.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070022#include "condition/CombinationConditionTracker.h"
23#include "condition/SimpleConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080024#include "guardrail/StatsdStats.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070025#include "matchers/CombinationLogMatchingTracker.h"
26#include "matchers/SimpleLogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070027#include "metrics_manager_util.h"
28#include "stats_util.h"
Yangster-mac330af582018-02-08 15:24:38 -080029#include "stats_log_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070030
Yao Chen93fe3a32017-11-02 13:52:59 -070031#include <log/logprint.h>
Bookatz6f197902018-02-05 12:30:14 -080032#include <private/android_filesystem_config.h>
David Chen16049572018-02-01 18:27:51 -080033#include <utils/SystemClock.h>
Yao Chen288c6002017-12-12 13:43:18 -080034
35using android::util::FIELD_COUNT_REPEATED;
David Chenfaa1af52018-03-30 15:14:04 -070036using android::util::FIELD_TYPE_INT32;
37using android::util::FIELD_TYPE_INT64;
Yao Chen288c6002017-12-12 13:43:18 -080038using android::util::FIELD_TYPE_MESSAGE;
Yangster-mac9def8e32018-04-17 13:55:51 -070039using android::util::FIELD_TYPE_STRING;
Yao Chen288c6002017-12-12 13:43:18 -080040using android::util::ProtoOutputStream;
41
Yao Chen44cf27c2017-09-14 22:32:50 -070042using std::make_unique;
43using std::set;
44using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070045using std::unordered_map;
46using std::vector;
47
48namespace android {
49namespace os {
50namespace statsd {
51
Yao Chen288c6002017-12-12 13:43:18 -080052const int FIELD_ID_METRICS = 1;
David Chenfaa1af52018-03-30 15:14:04 -070053const int FIELD_ID_ANNOTATIONS = 7;
54const int FIELD_ID_ANNOTATIONS_INT64 = 1;
55const int FIELD_ID_ANNOTATIONS_INT32 = 2;
Yao Chen288c6002017-12-12 13:43:18 -080056
Yao Chend10f7b12017-12-18 12:53:50 -080057MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070058 const int64_t timeBaseNs, const int64_t currentTimeNs,
Chenjie Yue2219202018-06-08 10:07:51 -070059 const sp<UidMap>& uidMap,
60 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -080061 const sp<AlarmMonitor>& anomalyAlarmMonitor,
62 const sp<AlarmMonitor>& periodicAlarmMonitor)
Chenjie Yue2219202018-06-08 10:07:51 -070063 : mConfigKey(key),
64 mUidMap(uidMap),
Yangster-macb142cc82018-03-30 15:22:08 -070065 mTtlNs(config.has_ttl_in_seconds() ? config.ttl_in_seconds() * NS_PER_SEC : -1),
66 mTtlEndNs(-1),
David Chen48944902018-05-03 10:29:11 -070067 mLastReportTimeNs(currentTimeNs),
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080068 mLastReportWallClockNs(getWallClockNs()) {
Yangster-macb142cc82018-03-30 15:22:08 -070069 // Init the ttl end timestamp.
Yangster-mac15f6bbc2018-04-08 11:52:26 -070070 refreshTtl(timeBaseNs);
Yangster-macb142cc82018-03-30 15:22:08 -070071
Chenjie Yue2219202018-06-08 10:07:51 -070072 mConfigValid = initStatsdConfig(
73 key, config, *uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor,
74 timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
75 mAllMetricProducers, mAllAnomalyTrackers, mAllPeriodicAlarmTrackers,
Yangster-mac849dfdc22018-10-12 15:41:45 -070076 mConditionToMetricMap, mTrackerToMetricMap, mTrackerToConditionMap,
77 mActivationAtomTrackerToMetricMap, mMetricIndexesWithActivation, mNoReportMetricIds);
Yao Chenb3561512017-11-21 18:07:17 -080078
Yangster-mac1c58f042018-05-17 15:52:51 -070079 mHashStringsInReport = config.hash_strings_in_metric_report();
dwchen730403e2018-10-29 11:41:56 -070080 mVersionStringsInReport = config.version_strings_in_metric_report();
81 mInstallerInReport = config.installer_in_metric_report();
Yangster-mac1c58f042018-05-17 15:52:51 -070082
Yao Chen147ce602017-12-22 14:35:34 -080083 if (config.allowed_log_source_size() == 0) {
David Chen8faaa012018-02-28 15:54:36 -080084 mConfigValid = false;
85 ALOGE("Log source whitelist is empty! This config won't get any data. Suggest adding at "
86 "least AID_SYSTEM and AID_STATSD to the allowed_log_source field.");
Yao Chend10f7b12017-12-18 12:53:50 -080087 } else {
Yao Chen147ce602017-12-22 14:35:34 -080088 for (const auto& source : config.allowed_log_source()) {
89 auto it = UidMap::sAidToUidMapping.find(source);
90 if (it != UidMap::sAidToUidMapping.end()) {
91 mAllowedUid.push_back(it->second);
92 } else {
93 mAllowedPkg.push_back(source);
94 }
95 }
Yao Chend10f7b12017-12-18 12:53:50 -080096
97 if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
98 ALOGE("Too many log sources. This is likely to be an error in the config.");
99 mConfigValid = false;
100 } else {
101 initLogSourceWhiteList();
102 }
103 }
104
David Chenfaa1af52018-03-30 15:14:04 -0700105 // Store the sub-configs used.
106 for (const auto& annotation : config.annotation()) {
107 mAnnotations.emplace_back(annotation.field_int64(), annotation.field_int32());
108 }
109
Yao Chenb3561512017-11-21 18:07:17 -0800110 // Guardrail. Reject the config if it's too big.
111 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
112 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800113 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -0800114 ALOGE("This config is too big! Reject!");
115 mConfigValid = false;
116 }
Bookatz1476ef22018-02-13 12:26:01 -0800117 if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) {
118 ALOGE("This config has too many alerts! Reject!");
119 mConfigValid = false;
120 }
Tej Singh6ede28b2019-01-29 17:06:54 -0800121
122 mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) ||
123 (mAllMetricProducers.size() == 0);
124 bool isActive = mIsAlwaysActive;
125 for (int metric : mMetricIndexesWithActivation) {
126 isActive |= mAllMetricProducers[metric]->isActive();
127 }
128 mIsActive = isActive;
129 VLOG("mIsActive is initialized to %d", mIsActive)
130
Yao Chenf09569f2017-12-13 17:00:51 -0800131 // no matter whether this config is valid, log it in the stats.
David Chenfaa1af52018-03-30 15:14:04 -0700132 StatsdStats::getInstance().noteConfigReceived(
133 key, mAllMetricProducers.size(), mAllConditionTrackers.size(), mAllAtomMatchers.size(),
134 mAllAnomalyTrackers.size(), mAnnotations, mConfigValid);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800135 // Check active
136 for (const auto& metric : mAllMetricProducers) {
137 if (metric->isActive()) {
138 mIsActive = true;
139 break;
140 }
141 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700142}
143
144MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -0700145 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -0700146}
147
Yao Chend10f7b12017-12-18 12:53:50 -0800148void MetricsManager::initLogSourceWhiteList() {
149 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
150 mAllowedLogSources.clear();
151 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
152
153 for (const auto& pkg : mAllowedPkg) {
154 auto uids = mUidMap->getAppUid(pkg);
155 mAllowedLogSources.insert(uids.begin(), uids.end());
156 }
157 if (DEBUG) {
158 for (const auto& uid : mAllowedLogSources) {
159 VLOG("Allowed uid %d", uid);
160 }
161 }
162}
163
Yao Chencaf339d2017-10-06 16:01:10 -0700164bool MetricsManager::isConfigValid() const {
165 return mConfigValid;
166}
167
Yangster-macb142cc82018-03-30 15:22:08 -0700168void MetricsManager::notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -0800169 const int64_t version) {
Yao Chend10f7b12017-12-18 12:53:50 -0800170 // check if we care this package
171 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
172 return;
173 }
174 // We will re-initialize the whole list because we don't want to keep the multi mapping of
175 // UID<->pkg inside MetricsManager to reduce the memory usage.
176 initLogSourceWhiteList();
177}
178
Yangster-macb142cc82018-03-30 15:22:08 -0700179void MetricsManager::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk,
David Chen27785a82018-01-19 17:06:45 -0800180 const int uid) {
Yao Chend10f7b12017-12-18 12:53:50 -0800181 // check if we care this package
182 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
183 return;
184 }
185 // We will re-initialize the whole list because we don't want to keep the multi mapping of
186 // UID<->pkg inside MetricsManager to reduce the memory usage.
187 initLogSourceWhiteList();
188}
189
Yangster-macb142cc82018-03-30 15:22:08 -0700190void MetricsManager::onUidMapReceived(const int64_t& eventTimeNs) {
Yao Chend10f7b12017-12-18 12:53:50 -0800191 if (mAllowedPkg.size() == 0) {
192 return;
193 }
194 initLogSourceWhiteList();
195}
196
Yao Chen884c8c12018-01-26 10:36:25 -0800197void MetricsManager::dumpStates(FILE* out, bool verbose) {
198 fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str());
199 {
200 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
201 for (const auto& source : mAllowedLogSources) {
202 fprintf(out, "%d ", source);
203 }
204 }
205 fprintf(out, "\n");
206 for (const auto& producer : mAllMetricProducers) {
207 producer->dumpStates(out, verbose);
208 }
209}
210
Yangster-macb142cc82018-03-30 15:22:08 -0700211void MetricsManager::dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800212 for (const auto& producer : mAllMetricProducers) {
213 producer->dropData(dropTimeNs);
214 }
215}
216
Yangster-mace68f3a52018-04-04 00:01:43 -0700217void MetricsManager::onDumpReport(const int64_t dumpTimeStampNs,
218 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700219 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000220 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700221 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700222 ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -0700223 VLOG("=========================Metric Reports Start==========================");
224 // one StatsLogReport per MetricProduer
Yangster-mac94e197c2018-01-02 16:03:03 -0800225 for (const auto& producer : mAllMetricProducers) {
226 if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) {
Yangster-mac9def8e32018-04-17 13:55:51 -0700227 uint64_t token = protoOutput->start(
228 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
Yangster-mac1c58f042018-05-17 15:52:51 -0700229 if (mHashStringsInReport) {
Bookatzff71cad2018-09-20 17:17:49 -0700230 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000231 dumpLatency, str_set, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700232 } else {
Bookatzff71cad2018-09-20 17:17:49 -0700233 producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000234 dumpLatency, nullptr, protoOutput);
Yangster-mac1c58f042018-05-17 15:52:51 -0700235 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800236 protoOutput->end(token);
Yangster-maca802d732018-04-24 07:50:38 -0700237 } else {
238 producer->clearPastBuckets(dumpTimeStampNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800239 }
Yao Chen729093d2017-10-16 10:33:26 -0700240 }
David Chenfaa1af52018-03-30 15:14:04 -0700241 for (const auto& annotation : mAnnotations) {
242 uint64_t token = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
243 FIELD_ID_ANNOTATIONS);
244 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ANNOTATIONS_INT64,
245 (long long)annotation.first);
246 protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_ANNOTATIONS_INT32, annotation.second);
247 protoOutput->end(token);
248 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700249
Yangster-mac330af582018-02-08 15:24:38 -0800250 mLastReportTimeNs = dumpTimeStampNs;
Yangster-mac3fa5d7f2018-03-10 21:50:27 -0800251 mLastReportWallClockNs = getWallClockNs();
Yao Chen729093d2017-10-16 10:33:26 -0700252 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -0700253}
254
Yao Chencaf339d2017-10-06 16:01:10 -0700255
Andrei Oneada01ea52019-01-30 15:28:36 +0000256bool MetricsManager::checkLogCredentials(const LogEvent& event) {
257 if (android::util::AtomsInfo::kWhitelistedAtoms.find(event.GetTagId()) !=
258 android::util::AtomsInfo::kWhitelistedAtoms.end())
259 {
260 return true;
261 }
262 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
263 if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) {
264 VLOG("log source %d not on the whitelist", event.GetUid());
265 return false;
266 }
267 return true;
268}
269
270bool MetricsManager::eventSanityCheck(const LogEvent& event) {
David Chenb639d142018-02-14 17:29:54 -0800271 if (event.GetTagId() == android::util::APP_BREADCRUMB_REPORTED) {
272 // Check that app breadcrumb reported fields are valid.
David Chendaa9f3a2017-12-28 16:52:22 -0800273 status_t err = NO_ERROR;
Bookatzb223c4e2018-02-01 15:35:04 -0800274
275 // Uid is 3rd from last field and must match the caller's uid,
276 // unless that caller is statsd itself (statsd is allowed to spoof uids).
277 long appHookUid = event.GetLong(event.size()-2, &err);
David Chen77ef6712018-02-23 18:23:42 -0800278 if (err != NO_ERROR ) {
279 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000280 return false;
David Chen77ef6712018-02-23 18:23:42 -0800281 }
Bookatzb223c4e2018-02-01 15:35:04 -0800282 int32_t loggerUid = event.GetUid();
David Chen77ef6712018-02-23 18:23:42 -0800283 if (loggerUid != appHookUid && loggerUid != AID_STATSD) {
284 VLOG("APP_BREADCRUMB_REPORTED has invalid uid: claimed %ld but caller is %d",
285 appHookUid, loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000286 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800287 }
Bookatzb223c4e2018-02-01 15:35:04 -0800288
David Chendaa9f3a2017-12-28 16:52:22 -0800289 // The state must be from 0,3. This part of code must be manually updated.
Bookatzb223c4e2018-02-01 15:35:04 -0800290 long appHookState = event.GetLong(event.size(), &err);
David Chen77ef6712018-02-23 18:23:42 -0800291 if (err != NO_ERROR ) {
292 VLOG("APP_BREADCRUMB_REPORTED had error when parsing the state field");
Andrei Oneada01ea52019-01-30 15:28:36 +0000293 return false;
David Chen77ef6712018-02-23 18:23:42 -0800294 } else if (appHookState < 0 || appHookState > 3) {
295 VLOG("APP_BREADCRUMB_REPORTED does not have valid state %ld", appHookState);
Andrei Oneada01ea52019-01-30 15:28:36 +0000296 return false;
David Chendaa9f3a2017-12-28 16:52:22 -0800297 }
Tej Singhbb8554a2018-01-26 11:59:14 -0800298 } else if (event.GetTagId() == android::util::DAVEY_OCCURRED) {
299 // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp.
300 // Check that the davey duration is reasonable. Max length check is for privacy.
301 status_t err = NO_ERROR;
David Chen77ef6712018-02-23 18:23:42 -0800302
303 // Uid is the first field provided.
304 long jankUid = event.GetLong(1, &err);
305 if (err != NO_ERROR ) {
306 VLOG("Davey occurred had error when parsing the uid");
Andrei Oneada01ea52019-01-30 15:28:36 +0000307 return false;
David Chen77ef6712018-02-23 18:23:42 -0800308 }
309 int32_t loggerUid = event.GetUid();
310 if (loggerUid != jankUid && loggerUid != AID_STATSD) {
311 VLOG("DAVEY_OCCURRED has invalid uid: claimed %ld but caller is %d", jankUid,
312 loggerUid);
Andrei Oneada01ea52019-01-30 15:28:36 +0000313 return false;
David Chen77ef6712018-02-23 18:23:42 -0800314 }
315
Tej Singhbb8554a2018-01-26 11:59:14 -0800316 long duration = event.GetLong(event.size(), &err);
David Chen77ef6712018-02-23 18:23:42 -0800317 if (err != NO_ERROR ) {
318 VLOG("Davey occurred had error when parsing the duration");
Andrei Oneada01ea52019-01-30 15:28:36 +0000319 return false;
David Chen77ef6712018-02-23 18:23:42 -0800320 } else if (duration > 100000) {
Tej Singhbb8554a2018-01-26 11:59:14 -0800321 VLOG("Davey duration is unreasonably long: %ld", duration);
Andrei Oneada01ea52019-01-30 15:28:36 +0000322 return false;
Tej Singhbb8554a2018-01-26 11:59:14 -0800323 }
Andrei Oneada01ea52019-01-30 15:28:36 +0000324 }
325
326 return true;
327}
328
329// Consume the stats log if it's interesting to this metric.
330void MetricsManager::onLogEvent(const LogEvent& event) {
331 if (!mConfigValid) {
332 return;
333 }
334
335 if (!checkLogCredentials(event)) {
336 return;
337 }
338
339 if (!eventSanityCheck(event)) {
340 return;
Yao Chend10f7b12017-12-18 12:53:50 -0800341 }
342
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700343 int tagId = event.GetTagId();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700344 int64_t eventTimeNs = event.GetElapsedTimestampNs();
345
Tej Singh6ede28b2019-01-29 17:06:54 -0800346 bool isActive = mIsAlwaysActive;
Yangster-mac849dfdc22018-10-12 15:41:45 -0700347 for (int metric : mMetricIndexesWithActivation) {
348 mAllMetricProducers[metric]->flushIfExpire(eventTimeNs);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800349 isActive |= mAllMetricProducers[metric]->isActive();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700350 }
351
Tej Singh6ede28b2019-01-29 17:06:54 -0800352 mIsActive = isActive;
353
Yao Chen44cf27c2017-09-14 22:32:50 -0700354 if (mTagIds.find(tagId) == mTagIds.end()) {
355 // not interesting...
356 return;
357 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700358
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800359 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700360
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800361 for (auto& matcher : mAllAtomMatchers) {
362 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700363 }
364
Yangster-mac849dfdc22018-10-12 15:41:45 -0700365 for (const auto& it : mActivationAtomTrackerToMetricMap) {
366 if (matcherCache[it.first] == MatchingState::kMatched) {
367 for (int metricIndex : it.second) {
368 mAllMetricProducers[metricIndex]->activate(it.first, eventTimeNs);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800369 isActive |= mAllMetricProducers[metricIndex]->isActive();
Yangster-mac849dfdc22018-10-12 15:41:45 -0700370 }
371 }
372 }
373
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800374 mIsActive = isActive;
375
Yao Chencaf339d2017-10-06 16:01:10 -0700376 // A bitmap to see which ConditionTracker needs to be re-evaluated.
377 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
378
379 for (const auto& pair : mTrackerToConditionMap) {
380 if (matcherCache[pair.first] == MatchingState::kMatched) {
381 const auto& conditionList = pair.second;
382 for (const int conditionIndex : conditionList) {
383 conditionToBeEvaluated[conditionIndex] = true;
384 }
385 }
386 }
387
388 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
389 ConditionState::kNotEvaluated);
390 // A bitmap to track if a condition has changed value.
391 vector<bool> changedCache(mAllConditionTrackers.size(), false);
392 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
393 if (conditionToBeEvaluated[i] == false) {
394 continue;
395 }
Yao Chencaf339d2017-10-06 16:01:10 -0700396 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
397 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800398 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700399 }
400
401 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800402 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700403 continue;
404 }
405 auto pair = mConditionToMetricMap.find(i);
406 if (pair != mConditionToMetricMap.end()) {
407 auto& metricList = pair->second;
408 for (auto metricIndex : metricList) {
409 // metric cares about non sliced condition, and it's changed.
410 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800411 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700412 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700413 eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700414 // metric cares about sliced conditions, and it may have changed. Send
415 // notification, and the metric can query the sliced conditions that are
416 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800417 } else {
Yao Chen427d3722018-03-22 15:21:52 -0700418 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(conditionCache[i],
Yangster-mac849dfdc22018-10-12 15:41:45 -0700419 eventTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700420 }
Yao Chencaf339d2017-10-06 16:01:10 -0700421 }
422 }
423 }
424
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800425 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
426 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700427 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800428 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Yangster-mac94e197c2018-01-02 16:03:03 -0800429 mAllAtomMatchers[i]->getId());
Yao Chencaf339d2017-10-06 16:01:10 -0700430 auto pair = mTrackerToMetricMap.find(i);
431 if (pair != mTrackerToMetricMap.end()) {
432 auto& metricList = pair->second;
433 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700434 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800435 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700436 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700437 }
438 }
439 }
440}
441
Yangster-mac932ecec2018-02-01 10:23:52 -0800442void MetricsManager::onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700443 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800444 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800445 for (const auto& itr : mAllAnomalyTrackers) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800446 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800447 }
448}
449
Yangster-mac932ecec2018-02-01 10:23:52 -0800450void MetricsManager::onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -0700451 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800452 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) {
453 for (const auto& itr : mAllPeriodicAlarmTrackers) {
454 itr->informAlarmsFired(timestampNs, alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800455 }
456}
457
yro69007c82017-10-26 20:42:57 -0700458// Returns the total byte size of all metrics managed by a single config source.
459size_t MetricsManager::byteSize() {
460 size_t totalSize = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800461 for (const auto& metricProducer : mAllMetricProducers) {
yro69007c82017-10-26 20:42:57 -0700462 totalSize += metricProducer->byteSize();
463 }
464 return totalSize;
465}
466
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800467void MetricsManager::setActiveMetrics(ActiveConfig config, int64_t currentTimeNs) {
468 if (config.active_metric_size() == 0) {
469 ALOGW("No active metric for config %s", mConfigKey.ToString().c_str());
470 return;
471 }
472
473 for (int i = 0; i < config.active_metric_size(); i++) {
474 for (int metric : mMetricIndexesWithActivation) {
475 if (mAllMetricProducers[metric]->getMetricId() == config.active_metric(i).metric_id()) {
476 VLOG("Setting active metric: %lld",
477 (long long)mAllMetricProducers[metric]->getMetricId());
478 mAllMetricProducers[metric]->setActive(
479 currentTimeNs, config.active_metric(i).time_to_live_nanos());
480 mIsActive = true;
481 }
482 }
483 }
484}
485
Yao Chen44cf27c2017-09-14 22:32:50 -0700486} // namespace statsd
487} // namespace os
488} // namespace android