blob: 5b4ca806e494af43555a9c0e385507775fb606a0 [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#define VLOG(...) \
19 if (DEBUG) ALOGD(__VA_ARGS__);
20
21#include "MetricsManager.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070022#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070023#include "../condition/CombinationConditionTracker.h"
24#include "../condition/SimpleConditionTracker.h"
25#include "../matchers/CombinationLogMatchingTracker.h"
26#include "../matchers/SimpleLogMatchingTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070027#include "CountMetricProducer.h"
Yao Chencaf339d2017-10-06 16:01:10 -070028#include "metrics_manager_util.h"
29#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070030
31using std::make_unique;
32using std::set;
33using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070034using std::unordered_map;
35using std::vector;
36
37namespace android {
38namespace os {
39namespace statsd {
40
Yao Chencaf339d2017-10-06 16:01:10 -070041MetricsManager::MetricsManager(const StatsdConfig& config) {
42 mConfigValid = initStatsdConfig(config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers,
43 mAllMetricProducers, mConditionToMetricMap, mTrackerToMetricMap,
44 mTrackerToConditionMap);
Yao Chen44cf27c2017-09-14 22:32:50 -070045}
46
47MetricsManager::~MetricsManager() {
48 VLOG("~MetricManager()");
49}
50
Yao Chencaf339d2017-10-06 16:01:10 -070051bool MetricsManager::isConfigValid() const {
52 return mConfigValid;
53}
54
Yao Chen44cf27c2017-09-14 22:32:50 -070055void MetricsManager::finish() {
Yao Chencaf339d2017-10-06 16:01:10 -070056 for (auto& metricProducer : mAllMetricProducers) {
57 metricProducer->finish();
Yao Chen44cf27c2017-09-14 22:32:50 -070058 }
59}
60
61// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070062void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -070063 if (!mConfigValid) {
64 return;
65 }
66
Joe Onoratoc4dfae52017-10-17 23:38:21 -070067 int tagId = event.GetTagId();
Yao Chen44cf27c2017-09-14 22:32:50 -070068 if (mTagIds.find(tagId) == mTagIds.end()) {
69 // not interesting...
70 return;
71 }
Yao Chen44cf27c2017-09-14 22:32:50 -070072
Yao Chencaf339d2017-10-06 16:01:10 -070073 // Since at least one of the metrics is interested in this event, we parse it now.
Yao Chencaf339d2017-10-06 16:01:10 -070074 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
75
76 for (auto& matcher : mAllLogEntryMatchers) {
77 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070078 }
79
Yao Chencaf339d2017-10-06 16:01:10 -070080 // A bitmap to see which ConditionTracker needs to be re-evaluated.
81 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
82
83 for (const auto& pair : mTrackerToConditionMap) {
84 if (matcherCache[pair.first] == MatchingState::kMatched) {
85 const auto& conditionList = pair.second;
86 for (const int conditionIndex : conditionList) {
87 conditionToBeEvaluated[conditionIndex] = true;
88 }
89 }
90 }
91
92 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
93 ConditionState::kNotEvaluated);
94 // A bitmap to track if a condition has changed value.
95 vector<bool> changedCache(mAllConditionTrackers.size(), false);
96 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
97 if (conditionToBeEvaluated[i] == false) {
98 continue;
99 }
100
101 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
102 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
103 changedCache);
104 if (changedCache[i]) {
105 auto pair = mConditionToMetricMap.find(i);
106 if (pair != mConditionToMetricMap.end()) {
107 auto& metricList = pair->second;
108 for (auto metricIndex : metricList) {
109 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700110 }
Yao Chencaf339d2017-10-06 16:01:10 -0700111 }
112 }
113 }
114
115 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
116 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
117 if (matcherCache[i] == MatchingState::kMatched) {
118 auto pair = mTrackerToMetricMap.find(i);
119 if (pair != mTrackerToMetricMap.end()) {
120 auto& metricList = pair->second;
121 for (const int metricIndex : metricList) {
122 mAllMetricProducers[metricIndex]->onMatchedLogEvent(event);
123 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700124 }
125 }
126 }
127}
128
129} // namespace statsd
130} // namespace os
131} // namespace android