blob: 1e65f58882331b3adff16711f9cfc65cb823d7fa [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 */
16#define LOG_TAG "MetricManager"
17#define DEBUG true // STOPSHIP if true
18#define VLOG(...) \
19 if (DEBUG) ALOGD(__VA_ARGS__);
20
21#include "MetricsManager.h"
22#include <cutils/log.h>
23#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070024#include "../condition/CombinationConditionTracker.h"
25#include "../condition/SimpleConditionTracker.h"
26#include "../matchers/CombinationLogMatchingTracker.h"
27#include "../matchers/SimpleLogMatchingTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include "CountMetricProducer.h"
Yao Chencaf339d2017-10-06 16:01:10 -070029#include "metrics_manager_util.h"
30#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070031
32using std::make_unique;
33using std::set;
34using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070035using std::unordered_map;
36using std::vector;
37
38namespace android {
39namespace os {
40namespace statsd {
41
Yao Chencaf339d2017-10-06 16:01:10 -070042MetricsManager::MetricsManager(const StatsdConfig& config) {
43 mConfigValid = initStatsdConfig(config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers,
44 mAllMetricProducers, mConditionToMetricMap, mTrackerToMetricMap,
45 mTrackerToConditionMap);
Yao Chen44cf27c2017-09-14 22:32:50 -070046}
47
48MetricsManager::~MetricsManager() {
49 VLOG("~MetricManager()");
50}
51
Yao Chencaf339d2017-10-06 16:01:10 -070052bool MetricsManager::isConfigValid() const {
53 return mConfigValid;
54}
55
Yao Chen44cf27c2017-09-14 22:32:50 -070056void MetricsManager::finish() {
Yao Chencaf339d2017-10-06 16:01:10 -070057 for (auto& metricProducer : mAllMetricProducers) {
58 metricProducer->finish();
Yao Chen44cf27c2017-09-14 22:32:50 -070059 }
60}
61
62// Consume the stats log if it's interesting to this metric.
63void MetricsManager::onLogEvent(const log_msg& logMsg) {
Yao Chencaf339d2017-10-06 16:01:10 -070064 if (!mConfigValid) {
65 return;
66 }
67
Yao Chen44cf27c2017-09-14 22:32:50 -070068 int tagId = getTagId(logMsg);
69 if (mTagIds.find(tagId) == mTagIds.end()) {
70 // not interesting...
71 return;
72 }
Yao Chen44cf27c2017-09-14 22:32:50 -070073
Yao Chencaf339d2017-10-06 16:01:10 -070074 // Since at least one of the metrics is interested in this event, we parse it now.
75 LogEventWrapper event = parseLogEvent(logMsg);
76 vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed);
77
78 for (auto& matcher : mAllLogEntryMatchers) {
79 matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -070080 }
81
Yao Chencaf339d2017-10-06 16:01:10 -070082 // A bitmap to see which ConditionTracker needs to be re-evaluated.
83 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
84
85 for (const auto& pair : mTrackerToConditionMap) {
86 if (matcherCache[pair.first] == MatchingState::kMatched) {
87 const auto& conditionList = pair.second;
88 for (const int conditionIndex : conditionList) {
89 conditionToBeEvaluated[conditionIndex] = true;
90 }
91 }
92 }
93
94 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
95 ConditionState::kNotEvaluated);
96 // A bitmap to track if a condition has changed value.
97 vector<bool> changedCache(mAllConditionTrackers.size(), false);
98 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
99 if (conditionToBeEvaluated[i] == false) {
100 continue;
101 }
102
103 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
104 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
105 changedCache);
106 if (changedCache[i]) {
107 auto pair = mConditionToMetricMap.find(i);
108 if (pair != mConditionToMetricMap.end()) {
109 auto& metricList = pair->second;
110 for (auto metricIndex : metricList) {
111 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700112 }
Yao Chencaf339d2017-10-06 16:01:10 -0700113 }
114 }
115 }
116
117 // For matched LogEntryMatchers, tell relevant metrics that a matched event has come.
118 for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) {
119 if (matcherCache[i] == MatchingState::kMatched) {
120 auto pair = mTrackerToMetricMap.find(i);
121 if (pair != mTrackerToMetricMap.end()) {
122 auto& metricList = pair->second;
123 for (const int metricIndex : metricList) {
124 mAllMetricProducers[metricIndex]->onMatchedLogEvent(event);
125 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700126 }
127 }
128 }
129}
130
131} // namespace statsd
132} // namespace os
133} // namespace android