blob: 5916b040889ccab1abd920370730c7ccb5da7582 [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"
23#include "matchers/CombinationLogMatchingTracker.h"
24#include "matchers/SimpleLogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070025#include "metrics_manager_util.h"
26#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070027
Yao Chen93fe3a32017-11-02 13:52:59 -070028#include <log/logprint.h>
Yao Chen44cf27c2017-09-14 22:32:50 -070029using std::make_unique;
30using std::set;
31using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070032using std::unordered_map;
33using std::vector;
34
35namespace android {
36namespace os {
37namespace statsd {
38
Yao Chencaf339d2017-10-06 16:01:10 -070039MetricsManager::MetricsManager(const StatsdConfig& config) {
40 mConfigValid = initStatsdConfig(config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -080041 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
42 mTrackerToMetricMap, mTrackerToConditionMap);
Yao Chen44cf27c2017-09-14 22:32:50 -070043}
44
45MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -070046 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -070047}
48
Yao Chencaf339d2017-10-06 16:01:10 -070049bool MetricsManager::isConfigValid() const {
50 return mConfigValid;
51}
52
Yao Chen44cf27c2017-09-14 22:32:50 -070053void MetricsManager::finish() {
Yao Chencaf339d2017-10-06 16:01:10 -070054 for (auto& metricProducer : mAllMetricProducers) {
55 metricProducer->finish();
Yao Chen44cf27c2017-09-14 22:32:50 -070056 }
57}
58
yro17adac92017-11-08 23:16:29 -080059vector<std::unique_ptr<vector<uint8_t>>> MetricsManager::onDumpReport() {
Yao Chen729093d2017-10-16 10:33:26 -070060 VLOG("=========================Metric Reports Start==========================");
61 // one StatsLogReport per MetricProduer
yro17adac92017-11-08 23:16:29 -080062 vector<std::unique_ptr<vector<uint8_t>>> reportList;
Yao Chen729093d2017-10-16 10:33:26 -070063 for (auto& metric : mAllMetricProducers) {
64 reportList.push_back(metric->onDumpReport());
65 }
66 VLOG("=========================Metric Reports End==========================");
67 return reportList;
68}
69
Yao Chen44cf27c2017-09-14 22:32:50 -070070// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070071void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -070072 if (!mConfigValid) {
73 return;
74 }
75
Joe Onoratoc4dfae52017-10-17 23:38:21 -070076 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -070077 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -070078 if (mTagIds.find(tagId) == mTagIds.end()) {
79 // not interesting...
80 return;
81 }
Yao Chen44cf27c2017-09-14 22:32:50 -070082
Yao Chencaf339d2017-10-06 16:01:10 -070083 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
84
85 for (auto& matcher : mAllLogEntryMatchers) {
86 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070087 }
88
Yao Chencaf339d2017-10-06 16:01:10 -070089 // A bitmap to see which ConditionTracker needs to be re-evaluated.
90 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
91
92 for (const auto& pair : mTrackerToConditionMap) {
93 if (matcherCache[pair.first] == MatchingState::kMatched) {
94 const auto& conditionList = pair.second;
95 for (const int conditionIndex : conditionList) {
96 conditionToBeEvaluated[conditionIndex] = true;
97 }
98 }
99 }
100
101 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
102 ConditionState::kNotEvaluated);
103 // A bitmap to track if a condition has changed value.
104 vector<bool> changedCache(mAllConditionTrackers.size(), false);
105 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
106 if (conditionToBeEvaluated[i] == false) {
107 continue;
108 }
Yao Chencaf339d2017-10-06 16:01:10 -0700109 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
110 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800111 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700112 }
113
114 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800115 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700116 continue;
117 }
118 auto pair = mConditionToMetricMap.find(i);
119 if (pair != mConditionToMetricMap.end()) {
120 auto& metricList = pair->second;
121 for (auto metricIndex : metricList) {
122 // metric cares about non sliced condition, and it's changed.
123 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800124 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700125 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
126 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700127 // metric cares about sliced conditions, and it may have changed. Send
128 // notification, and the metric can query the sliced conditions that are
129 // interesting to it.
Yao Chen967b2052017-11-07 16:36:43 -0800130 } else if (mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700131 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700132 }
Yao Chencaf339d2017-10-06 16:01:10 -0700133 }
134 }
135 }
136
137 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
138 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
139 if (matcherCache[i] == MatchingState::kMatched) {
140 auto pair = mTrackerToMetricMap.find(i);
141 if (pair != mTrackerToMetricMap.end()) {
142 auto& metricList = pair->second;
143 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700144 // pushed metrics are never scheduled pulls
Yangster1d4d6862017-10-31 12:58:51 -0700145 mAllMetricProducers[metricIndex]->onMatchedLogEvent(
146 i, event, false /* schedulePull */);
Yao Chencaf339d2017-10-06 16:01:10 -0700147 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700148 }
149 }
150 }
151}
152
Yangster-mace2cd6d52017-11-09 20:38:30 -0800153void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
154 sp<const AnomalyAlarm> anomaly) {
155 for (const auto& itr : mAllAnomalyTrackers) {
156 itr->declareAnomaly(timestampNs);
157 }
158}
159
160void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
161 for (auto& itr : mAllAnomalyTrackers) {
162 itr->setAnomalyMonitor(anomalyMonitor);
163 }
164}
165
yro69007c82017-10-26 20:42:57 -0700166// Returns the total byte size of all metrics managed by a single config source.
167size_t MetricsManager::byteSize() {
168 size_t totalSize = 0;
169 for (auto metricProducer : mAllMetricProducers) {
170 totalSize += metricProducer->byteSize();
171 }
172 return totalSize;
173}
174
Yao Chen44cf27c2017-09-14 22:32:50 -0700175} // namespace statsd
176} // namespace os
177} // namespace android