blob: b0f0135f4121e64de53b3d811911e93d987e5047 [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
Yao Chenb3561512017-11-21 18:07:17 -080047MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config) : mConfigKey(key) {
48 mConfigValid =
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080049 initStatsdConfig(key, config, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
Yao Chenb3561512017-11-21 18:07:17 -080050 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
51 mTrackerToMetricMap, mTrackerToConditionMap);
52
53 // TODO: add alert size.
54 // no matter whether this config is valid, log it in the stats.
55 StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
56 mAllConditionTrackers.size(),
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080057 mAllAtomMatchers.size(), 0, mConfigValid);
Yao Chenb3561512017-11-21 18:07:17 -080058 // Guardrail. Reject the config if it's too big.
59 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
60 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080061 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -080062 ALOGE("This config is too big! Reject!");
63 mConfigValid = false;
64 }
Yao Chen44cf27c2017-09-14 22:32:50 -070065}
66
67MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -070068 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -070069}
70
Yao Chencaf339d2017-10-06 16:01:10 -070071bool MetricsManager::isConfigValid() const {
72 return mConfigValid;
73}
74
Yao Chen288c6002017-12-12 13:43:18 -080075void MetricsManager::onDumpReport(ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -070076 VLOG("=========================Metric Reports Start==========================");
Yao Chen288c6002017-12-12 13:43:18 -080077 uint64_t dumpTimeStampNs = time(nullptr) * NS_PER_SEC;
Yao Chen729093d2017-10-16 10:33:26 -070078 // one StatsLogReport per MetricProduer
Yao Chen729093d2017-10-16 10:33:26 -070079 for (auto& metric : mAllMetricProducers) {
Yao Chen288c6002017-12-12 13:43:18 -080080 long long token =
81 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
82 metric->onDumpReport(dumpTimeStampNs, protoOutput);
83 protoOutput->end(token);
Yao Chen729093d2017-10-16 10:33:26 -070084 }
85 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -070086}
87
Yao Chen44cf27c2017-09-14 22:32:50 -070088// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070089void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -070090 if (!mConfigValid) {
91 return;
92 }
93
Joe Onoratoc4dfae52017-10-17 23:38:21 -070094 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -070095 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -070096 if (mTagIds.find(tagId) == mTagIds.end()) {
97 // not interesting...
98 return;
99 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700100
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800101 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700102
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800103 for (auto& matcher : mAllAtomMatchers) {
104 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700105 }
106
Yao Chencaf339d2017-10-06 16:01:10 -0700107 // A bitmap to see which ConditionTracker needs to be re-evaluated.
108 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
109
110 for (const auto& pair : mTrackerToConditionMap) {
111 if (matcherCache[pair.first] == MatchingState::kMatched) {
112 const auto& conditionList = pair.second;
113 for (const int conditionIndex : conditionList) {
114 conditionToBeEvaluated[conditionIndex] = true;
115 }
116 }
117 }
118
119 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
120 ConditionState::kNotEvaluated);
121 // A bitmap to track if a condition has changed value.
122 vector<bool> changedCache(mAllConditionTrackers.size(), false);
123 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
124 if (conditionToBeEvaluated[i] == false) {
125 continue;
126 }
Yao Chencaf339d2017-10-06 16:01:10 -0700127 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
128 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800129 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700130 }
131
132 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800133 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700134 continue;
135 }
136 auto pair = mConditionToMetricMap.find(i);
137 if (pair != mConditionToMetricMap.end()) {
138 auto& metricList = pair->second;
139 for (auto metricIndex : metricList) {
140 // metric cares about non sliced condition, and it's changed.
141 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800142 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700143 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
144 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700145 // metric cares about sliced conditions, and it may have changed. Send
146 // notification, and the metric can query the sliced conditions that are
147 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800148 } else {
Yao Chen5154a3792017-10-30 22:57:06 -0700149 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700150 }
Yao Chencaf339d2017-10-06 16:01:10 -0700151 }
152 }
153 }
154
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800155 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
156 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700157 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800158 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800159 mAllAtomMatchers[i]->getName());
Yao Chencaf339d2017-10-06 16:01:10 -0700160 auto pair = mTrackerToMetricMap.find(i);
161 if (pair != mTrackerToMetricMap.end()) {
162 auto& metricList = pair->second;
163 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700164 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800165 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700166 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700167 }
168 }
169 }
170}
171
Yangster-mace2cd6d52017-11-09 20:38:30 -0800172void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
Bookatzcc5adef22017-11-21 14:36:23 -0800173 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800174 for (const auto& itr : mAllAnomalyTrackers) {
Bookatzcc5adef22017-11-21 14:36:23 -0800175 itr->informAlarmsFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800176 }
177}
178
179void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
180 for (auto& itr : mAllAnomalyTrackers) {
181 itr->setAnomalyMonitor(anomalyMonitor);
182 }
183}
184
yro69007c82017-10-26 20:42:57 -0700185// Returns the total byte size of all metrics managed by a single config source.
186size_t MetricsManager::byteSize() {
187 size_t totalSize = 0;
188 for (auto metricProducer : mAllMetricProducers) {
189 totalSize += metricProducer->byteSize();
190 }
191 return totalSize;
192}
193
Yao Chen44cf27c2017-09-14 22:32:50 -0700194} // namespace statsd
195} // namespace os
196} // namespace android