blob: 4fa3965a9dd289111559d5364d248a3b909b74c9 [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 Chen44cf27c2017-09-14 22:32:50 -070077 if (mTagIds.find(tagId) == mTagIds.end()) {
78 // not interesting...
79 return;
80 }
Yao Chen44cf27c2017-09-14 22:32:50 -070081
Yao Chencaf339d2017-10-06 16:01:10 -070082 // Since at least one of the metrics is interested in this event, we parse it now.
Yao Chen729093d2017-10-16 10:33:26 -070083 ALOGD("%s", event.ToString().c_str());
Yao Chencaf339d2017-10-06 16:01:10 -070084 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
85
86 for (auto& matcher : mAllLogEntryMatchers) {
87 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070088 }
89
Yao Chencaf339d2017-10-06 16:01:10 -070090 // A bitmap to see which ConditionTracker needs to be re-evaluated.
91 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
92
93 for (const auto& pair : mTrackerToConditionMap) {
94 if (matcherCache[pair.first] == MatchingState::kMatched) {
95 const auto& conditionList = pair.second;
96 for (const int conditionIndex : conditionList) {
97 conditionToBeEvaluated[conditionIndex] = true;
98 }
99 }
100 }
101
102 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
103 ConditionState::kNotEvaluated);
104 // A bitmap to track if a condition has changed value.
105 vector<bool> changedCache(mAllConditionTrackers.size(), false);
Yao Chen729093d2017-10-16 10:33:26 -0700106 vector<bool> slicedChangedCache(mAllConditionTrackers.size(), false);
Yao Chencaf339d2017-10-06 16:01:10 -0700107 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
108 if (conditionToBeEvaluated[i] == false) {
109 continue;
110 }
Yao Chencaf339d2017-10-06 16:01:10 -0700111 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
112 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen729093d2017-10-16 10:33:26 -0700113 changedCache, slicedChangedCache);
114 }
115
116 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
117 if (changedCache[i] == false && slicedChangedCache[i] == false) {
118 continue;
119 }
120 auto pair = mConditionToMetricMap.find(i);
121 if (pair != mConditionToMetricMap.end()) {
122 auto& metricList = pair->second;
123 for (auto metricIndex : metricList) {
124 // metric cares about non sliced condition, and it's changed.
125 // Push the new condition to it directly.
126 if (!mAllMetricProducers[metricIndex]->isConditionSliced() && changedCache[i]) {
Yao Chencaf339d2017-10-06 16:01:10 -0700127 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i]);
Yao Chen729093d2017-10-16 10:33:26 -0700128 // metric cares about sliced conditions, and it may have changed. Send
129 // notification, and the metric can query the sliced conditions that are
130 // interesting to it.
131 } else if (mAllMetricProducers[metricIndex]->isConditionSliced() &&
132 slicedChangedCache[i]) {
133 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange();
Yao Chen44cf27c2017-09-14 22:32:50 -0700134 }
Yao Chencaf339d2017-10-06 16:01:10 -0700135 }
136 }
137 }
138
139 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
140 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
141 if (matcherCache[i] == MatchingState::kMatched) {
142 auto pair = mTrackerToMetricMap.find(i);
143 if (pair != mTrackerToMetricMap.end()) {
144 auto& metricList = pair->second;
145 for (const int metricIndex : metricList) {
Yao Chen729093d2017-10-16 10:33:26 -0700146 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700147 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700148 }
149 }
150 }
151}
152
yro69007c82017-10-26 20:42:57 -0700153// Returns the total byte size of all metrics managed by a single config source.
154size_t MetricsManager::byteSize() {
155 size_t totalSize = 0;
156 for (auto metricProducer : mAllMetricProducers) {
157 totalSize += metricProducer->byteSize();
158 }
159 return totalSize;
160}
161
Yao Chen44cf27c2017-09-14 22:32:50 -0700162} // namespace statsd
163} // namespace os
164} // namespace android