blob: d1df8aa751773a8200c6652876cde72be31f962d [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
19#include "MetricsManager.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070020#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070021#include "../condition/CombinationConditionTracker.h"
22#include "../condition/SimpleConditionTracker.h"
23#include "../matchers/CombinationLogMatchingTracker.h"
24#include "../matchers/SimpleLogMatchingTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070025#include "CountMetricProducer.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
29using 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,
41 mAllMetricProducers, mConditionToMetricMap, mTrackerToMetricMap,
42 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
Yao Chen729093d2017-10-16 10:33:26 -070059vector<StatsLogReport> MetricsManager::onDumpReport() {
60 VLOG("=========================Metric Reports Start==========================");
61 // one StatsLogReport per MetricProduer
62 vector<StatsLogReport> reportList;
63 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 // Since at least one of the metrics is interested in this event, we parse it now.
Yao Chen729093d2017-10-16 10:33:26 -070084 ALOGD("%s", event.ToString().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070085 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
86
87 for (auto& matcher : mAllLogEntryMatchers) {
88 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070089 }
90
Yao Chencaf339d2017-10-06 16:01:10 -070091 // A bitmap to see which ConditionTracker needs to be re-evaluated.
92 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
93
94 for (const auto& pair : mTrackerToConditionMap) {
95 if (matcherCache[pair.first] == MatchingState::kMatched) {
96 const auto& conditionList = pair.second;
97 for (const int conditionIndex : conditionList) {
98 conditionToBeEvaluated[conditionIndex] = true;
99 }
100 }
101 }
102
103 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
104 ConditionState::kNotEvaluated);
105 // A bitmap to track if a condition has changed value.
106 vector<bool> changedCache(mAllConditionTrackers.size(), false);
Yao Chen729093d2017-10-16 10:33:26 -0700107 vector<bool> slicedChangedCache(mAllConditionTrackers.size(), false);
Yao Chencaf339d2017-10-06 16:01:10 -0700108 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
109 if (conditionToBeEvaluated[i] == false) {
110 continue;
111 }
Yao Chencaf339d2017-10-06 16:01:10 -0700112 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
113 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen729093d2017-10-16 10:33:26 -0700114 changedCache, slicedChangedCache);
115 }
116
117 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
118 if (changedCache[i] == false && slicedChangedCache[i] == false) {
119 continue;
120 }
121 auto pair = mConditionToMetricMap.find(i);
122 if (pair != mConditionToMetricMap.end()) {
123 auto& metricList = pair->second;
124 for (auto metricIndex : metricList) {
125 // metric cares about non sliced condition, and it's changed.
126 // Push the new condition to it directly.
127 if (!mAllMetricProducers[metricIndex]->isConditionSliced() && changedCache[i]) {
Yao Chen5154a3792017-10-30 22:57:06 -0700128 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
129 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700130 // metric cares about sliced conditions, and it may have changed. Send
131 // notification, and the metric can query the sliced conditions that are
132 // interesting to it.
133 } else if (mAllMetricProducers[metricIndex]->isConditionSliced() &&
134 slicedChangedCache[i]) {
Yao Chen5154a3792017-10-30 22:57:06 -0700135 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700136 }
Yao Chencaf339d2017-10-06 16:01:10 -0700137 }
138 }
139 }
140
141 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
142 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
143 if (matcherCache[i] == MatchingState::kMatched) {
144 auto pair = mTrackerToMetricMap.find(i);
145 if (pair != mTrackerToMetricMap.end()) {
146 auto& metricList = pair->second;
147 for (const int metricIndex : metricList) {
Yao Chen729093d2017-10-16 10:33:26 -0700148 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700149 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700150 }
151 }
152 }
153}
154
yro69007c82017-10-26 20:42:57 -0700155// Returns the total byte size of all metrics managed by a single config source.
156size_t MetricsManager::byteSize() {
157 size_t totalSize = 0;
158 for (auto metricProducer : mAllMetricProducers) {
159 totalSize += metricProducer->byteSize();
160 }
161 return totalSize;
162}
163
Yao Chen44cf27c2017-09-14 22:32:50 -0700164} // namespace statsd
165} // namespace os
166} // namespace android