blob: e986c1ab2ddbe555260db441c5233a0b2df9714a [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 Chen44cf27c2017-09-14 22:32:50 -070030using std::make_unique;
31using std::set;
32using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070033using std::unordered_map;
34using std::vector;
35
36namespace android {
37namespace os {
38namespace statsd {
39
Yao Chenb3561512017-11-21 18:07:17 -080040MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config) : mConfigKey(key) {
41 mConfigValid =
42 initStatsdConfig(key, config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers,
43 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
44 mTrackerToMetricMap, mTrackerToConditionMap);
45
46 // TODO: add alert size.
47 // no matter whether this config is valid, log it in the stats.
48 StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
49 mAllConditionTrackers.size(),
50 mAllLogEntryMatchers.size(), 0, mConfigValid);
51 // Guardrail. Reject the config if it's too big.
52 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
53 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
54 mAllLogEntryMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
55 ALOGE("This config is too big! Reject!");
56 mConfigValid = false;
57 }
Yao Chen44cf27c2017-09-14 22:32:50 -070058}
59
60MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -070061 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -070062}
63
Yao Chencaf339d2017-10-06 16:01:10 -070064bool MetricsManager::isConfigValid() const {
65 return mConfigValid;
66}
67
Yao Chen44cf27c2017-09-14 22:32:50 -070068void MetricsManager::finish() {
Yao Chencaf339d2017-10-06 16:01:10 -070069 for (auto& metricProducer : mAllMetricProducers) {
70 metricProducer->finish();
Yao Chen44cf27c2017-09-14 22:32:50 -070071 }
72}
73
yro17adac92017-11-08 23:16:29 -080074vector<std::unique_ptr<vector<uint8_t>>> MetricsManager::onDumpReport() {
Yao Chen729093d2017-10-16 10:33:26 -070075 VLOG("=========================Metric Reports Start==========================");
76 // one StatsLogReport per MetricProduer
yro17adac92017-11-08 23:16:29 -080077 vector<std::unique_ptr<vector<uint8_t>>> reportList;
Yao Chen729093d2017-10-16 10:33:26 -070078 for (auto& metric : mAllMetricProducers) {
79 reportList.push_back(metric->onDumpReport());
80 }
81 VLOG("=========================Metric Reports End==========================");
82 return reportList;
83}
84
Yao Chen44cf27c2017-09-14 22:32:50 -070085// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070086void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -070087 if (!mConfigValid) {
88 return;
89 }
90
Joe Onoratoc4dfae52017-10-17 23:38:21 -070091 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -070092 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -070093 if (mTagIds.find(tagId) == mTagIds.end()) {
94 // not interesting...
95 return;
96 }
Yao Chen44cf27c2017-09-14 22:32:50 -070097
Yao Chencaf339d2017-10-06 16:01:10 -070098 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
99
100 for (auto& matcher : mAllLogEntryMatchers) {
101 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700102 }
103
Yao Chencaf339d2017-10-06 16:01:10 -0700104 // A bitmap to see which ConditionTracker needs to be re-evaluated.
105 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
106
107 for (const auto& pair : mTrackerToConditionMap) {
108 if (matcherCache[pair.first] == MatchingState::kMatched) {
109 const auto& conditionList = pair.second;
110 for (const int conditionIndex : conditionList) {
111 conditionToBeEvaluated[conditionIndex] = true;
112 }
113 }
114 }
115
116 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
117 ConditionState::kNotEvaluated);
118 // A bitmap to track if a condition has changed value.
119 vector<bool> changedCache(mAllConditionTrackers.size(), false);
120 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
121 if (conditionToBeEvaluated[i] == false) {
122 continue;
123 }
Yao Chencaf339d2017-10-06 16:01:10 -0700124 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
125 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800126 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700127 }
128
129 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800130 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700131 continue;
132 }
133 auto pair = mConditionToMetricMap.find(i);
134 if (pair != mConditionToMetricMap.end()) {
135 auto& metricList = pair->second;
136 for (auto metricIndex : metricList) {
137 // metric cares about non sliced condition, and it's changed.
138 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800139 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700140 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
141 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700142 // metric cares about sliced conditions, and it may have changed. Send
143 // notification, and the metric can query the sliced conditions that are
144 // interesting to it.
Yao Chen967b2052017-11-07 16:36:43 -0800145 } else if (mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700146 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700147 }
Yao Chencaf339d2017-10-06 16:01:10 -0700148 }
149 }
150 }
151
152 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
153 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
154 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800155 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
156 mAllLogEntryMatchers[i]->getName());
Yao Chencaf339d2017-10-06 16:01:10 -0700157 auto pair = mTrackerToMetricMap.find(i);
158 if (pair != mTrackerToMetricMap.end()) {
159 auto& metricList = pair->second;
160 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700161 // pushed metrics are never scheduled pulls
Yangster1d4d6862017-10-31 12:58:51 -0700162 mAllMetricProducers[metricIndex]->onMatchedLogEvent(
163 i, event, false /* schedulePull */);
Yao Chencaf339d2017-10-06 16:01:10 -0700164 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700165 }
166 }
167 }
168}
169
Yangster-mace2cd6d52017-11-09 20:38:30 -0800170void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
Bookatzcc5adef22017-11-21 14:36:23 -0800171 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800172 for (const auto& itr : mAllAnomalyTrackers) {
Bookatzcc5adef22017-11-21 14:36:23 -0800173 itr->informAlarmsFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800174 }
175}
176
177void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
178 for (auto& itr : mAllAnomalyTrackers) {
179 itr->setAnomalyMonitor(anomalyMonitor);
180 }
181}
182
yro69007c82017-10-26 20:42:57 -0700183// Returns the total byte size of all metrics managed by a single config source.
184size_t MetricsManager::byteSize() {
185 size_t totalSize = 0;
186 for (auto metricProducer : mAllMetricProducers) {
187 totalSize += metricProducer->byteSize();
188 }
189 return totalSize;
190}
191
Yao Chen44cf27c2017-09-14 22:32:50 -0700192} // namespace statsd
193} // namespace os
194} // namespace android