blob: a5900f4bd438ba91bc380c56df29bb5eca991442 [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 */
Yao Chen44cf27c2017-09-14 22:32:50 -070016#define DEBUG true // 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 Chen93fe3a32017-11-02 13:52:59 -070019
Yao Chen44cf27c2017-09-14 22:32:50 -070020#include "CountMetricProducer.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070021#include "condition/CombinationConditionTracker.h"
22#include "condition/SimpleConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "guardrail/StatsdStats.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070024#include "matchers/CombinationLogMatchingTracker.h"
25#include "matchers/SimpleLogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070026#include "metrics_manager_util.h"
27#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070028
Yao Chen93fe3a32017-11-02 13:52:59 -070029#include <log/logprint.h>
Yao Chen288c6002017-12-12 13:43:18 -080030
31using android::util::FIELD_COUNT_REPEATED;
32using android::util::FIELD_TYPE_MESSAGE;
33using android::util::ProtoOutputStream;
34
Yao Chen44cf27c2017-09-14 22:32:50 -070035using std::make_unique;
36using std::set;
37using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070038using std::unordered_map;
39using std::vector;
40
41namespace android {
42namespace os {
43namespace statsd {
44
Yao Chen288c6002017-12-12 13:43:18 -080045const int FIELD_ID_METRICS = 1;
46
Chenjie Yu85ed8382017-12-14 16:48:54 -080047MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config, const long timeBaseSec) : mConfigKey(key) {
Yao Chenb3561512017-11-21 18:07:17 -080048 mConfigValid =
Chenjie Yu85ed8382017-12-14 16:48:54 -080049 initStatsdConfig(key, config, timeBaseSec, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
Yao Chenb3561512017-11-21 18:07:17 -080050 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
51 mTrackerToMetricMap, mTrackerToConditionMap);
52
Yao Chenb3561512017-11-21 18:07:17 -080053 // Guardrail. Reject the config if it's too big.
54 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
55 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080056 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -080057 ALOGE("This config is too big! Reject!");
58 mConfigValid = false;
59 }
Yao Chenf09569f2017-12-13 17:00:51 -080060
61 // TODO: add alert size.
62 // no matter whether this config is valid, log it in the stats.
63 StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
64 mAllConditionTrackers.size(),
65 mAllAtomMatchers.size(), 0, mConfigValid);
Yao Chen44cf27c2017-09-14 22:32:50 -070066}
67
68MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -070069 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -070070}
71
Yao Chencaf339d2017-10-06 16:01:10 -070072bool MetricsManager::isConfigValid() const {
73 return mConfigValid;
74}
75
Yao Chen288c6002017-12-12 13:43:18 -080076void MetricsManager::onDumpReport(ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -070077 VLOG("=========================Metric Reports Start==========================");
Yao Chen288c6002017-12-12 13:43:18 -080078 uint64_t dumpTimeStampNs = time(nullptr) * NS_PER_SEC;
Yao Chen729093d2017-10-16 10:33:26 -070079 // one StatsLogReport per MetricProduer
Yao Chen729093d2017-10-16 10:33:26 -070080 for (auto& metric : mAllMetricProducers) {
Yao Chen288c6002017-12-12 13:43:18 -080081 long long token =
82 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
83 metric->onDumpReport(dumpTimeStampNs, protoOutput);
84 protoOutput->end(token);
Yao Chen729093d2017-10-16 10:33:26 -070085 }
86 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -070087}
88
Yao Chen44cf27c2017-09-14 22:32:50 -070089// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070090void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -070091 if (!mConfigValid) {
92 return;
93 }
94
Joe Onoratoc4dfae52017-10-17 23:38:21 -070095 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -070096 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -070097 if (mTagIds.find(tagId) == mTagIds.end()) {
98 // not interesting...
99 return;
100 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700101
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800102 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700103
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800104 for (auto& matcher : mAllAtomMatchers) {
105 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700106 }
107
Yao Chencaf339d2017-10-06 16:01:10 -0700108 // A bitmap to see which ConditionTracker needs to be re-evaluated.
109 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
110
111 for (const auto& pair : mTrackerToConditionMap) {
112 if (matcherCache[pair.first] == MatchingState::kMatched) {
113 const auto& conditionList = pair.second;
114 for (const int conditionIndex : conditionList) {
115 conditionToBeEvaluated[conditionIndex] = true;
116 }
117 }
118 }
119
120 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
121 ConditionState::kNotEvaluated);
122 // A bitmap to track if a condition has changed value.
123 vector<bool> changedCache(mAllConditionTrackers.size(), false);
124 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
125 if (conditionToBeEvaluated[i] == false) {
126 continue;
127 }
Yao Chencaf339d2017-10-06 16:01:10 -0700128 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
129 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800130 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700131 }
132
133 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800134 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700135 continue;
136 }
137 auto pair = mConditionToMetricMap.find(i);
138 if (pair != mConditionToMetricMap.end()) {
139 auto& metricList = pair->second;
140 for (auto metricIndex : metricList) {
141 // metric cares about non sliced condition, and it's changed.
142 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800143 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700144 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
145 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700146 // metric cares about sliced conditions, and it may have changed. Send
147 // notification, and the metric can query the sliced conditions that are
148 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800149 } else {
Yao Chen5154a3792017-10-30 22:57:06 -0700150 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700151 }
Yao Chencaf339d2017-10-06 16:01:10 -0700152 }
153 }
154 }
155
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800156 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
157 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700158 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800159 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800160 mAllAtomMatchers[i]->getName());
Yao Chencaf339d2017-10-06 16:01:10 -0700161 auto pair = mTrackerToMetricMap.find(i);
162 if (pair != mTrackerToMetricMap.end()) {
163 auto& metricList = pair->second;
164 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700165 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800166 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700167 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700168 }
169 }
170 }
171}
172
Yangster-mace2cd6d52017-11-09 20:38:30 -0800173void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
Bookatzcc5adef22017-11-21 14:36:23 -0800174 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800175 for (const auto& itr : mAllAnomalyTrackers) {
Bookatzcc5adef22017-11-21 14:36:23 -0800176 itr->informAlarmsFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800177 }
178}
179
180void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
181 for (auto& itr : mAllAnomalyTrackers) {
182 itr->setAnomalyMonitor(anomalyMonitor);
183 }
184}
185
yro69007c82017-10-26 20:42:57 -0700186// Returns the total byte size of all metrics managed by a single config source.
187size_t MetricsManager::byteSize() {
188 size_t totalSize = 0;
189 for (auto metricProducer : mAllMetricProducers) {
190 totalSize += metricProducer->byteSize();
191 }
192 return totalSize;
193}
194
Yao Chen44cf27c2017-09-14 22:32:50 -0700195} // namespace statsd
196} // namespace os
197} // namespace android