blob: b77daf107503d8c670438b5b6cae805d3431c2f4 [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.
62void MetricsManager::onLogEvent(const log_msg& logMsg) {
Yao Chencaf339d2017-10-06 16:01:10 -070063 if (!mConfigValid) {
64 return;
65 }
66
Yao Chen44cf27c2017-09-14 22:32:50 -070067 int tagId = getTagId(logMsg);
68 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.
74 LogEventWrapper event = parseLogEvent(logMsg);
75 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
76
77 for (auto& matcher : mAllLogEntryMatchers) {
78 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070079 }
80
Yao Chencaf339d2017-10-06 16:01:10 -070081 // A bitmap to see which ConditionTracker needs to be re-evaluated.
82 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
83
84 for (const auto& pair : mTrackerToConditionMap) {
85 if (matcherCache[pair.first] == MatchingState::kMatched) {
86 const auto& conditionList = pair.second;
87 for (const int conditionIndex : conditionList) {
88 conditionToBeEvaluated[conditionIndex] = true;
89 }
90 }
91 }
92
93 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
94 ConditionState::kNotEvaluated);
95 // A bitmap to track if a condition has changed value.
96 vector<bool> changedCache(mAllConditionTrackers.size(), false);
97 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
98 if (conditionToBeEvaluated[i] == false) {
99 continue;
100 }
101
102 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
103 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
104 changedCache);
105 if (changedCache[i]) {
106 auto pair = mConditionToMetricMap.find(i);
107 if (pair != mConditionToMetricMap.end()) {
108 auto& metricList = pair->second;
109 for (auto metricIndex : metricList) {
110 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700111 }
Yao Chencaf339d2017-10-06 16:01:10 -0700112 }
113 }
114 }
115
116 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
117 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
118 if (matcherCache[i] == MatchingState::kMatched) {
119 auto pair = mTrackerToMetricMap.find(i);
120 if (pair != mTrackerToMetricMap.end()) {
121 auto& metricList = pair->second;
122 for (const int metricIndex : metricList) {
123 mAllMetricProducers[metricIndex]->onMatchedLogEvent(event);
124 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700125 }
126 }
127 }
128}
129
130} // namespace statsd
131} // namespace os
132} // namespace android