blob: cb7420683380b36579d98911f2095f4d22f5d58d [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>
24#include "CountMetricProducer.h"
25#include "parse_util.h"
26
27using std::make_unique;
28using std::set;
29using std::string;
30using std::unique_ptr;
31using std::unordered_map;
32using std::vector;
33
34namespace android {
35namespace os {
36namespace statsd {
37
38MetricsManager::MetricsManager(const StatsdConfig& config) : mConfig(config), mLogMatchers() {
39 std::unordered_map<string, LogEntryMatcher> matcherMap;
40 std::unordered_map<string, sp<ConditionTracker>> conditionMap;
41
42 for (int i = 0; i < config.log_entry_matcher_size(); i++) {
43 const LogEntryMatcher& logMatcher = config.log_entry_matcher(i);
44 mMatchers.push_back(logMatcher);
45
46 matcherMap[config.log_entry_matcher(i).name()] = logMatcher;
47
48 mLogMatchers[logMatcher.name()] = vector<unique_ptr<MetricProducer>>();
49 // Collect all the tag ids that are interesting
50 set<int> tagIds = LogEntryMatcherManager::getTagIdsFromMatcher(logMatcher);
51
52 mTagIds.insert(tagIds.begin(), tagIds.end());
53 }
54
55 for (int i = 0; i < config.condition_size(); i++) {
56 const Condition& condition = config.condition(i);
57 conditionMap[condition.name()] = new ConditionTracker(condition);
58 }
59
60 // Build MetricProducers for each metric defined in config.
61 // (1) build CountMetricProducer
62 for (int i = 0; i < config.count_metric_size(); i++) {
63 const CountMetric& metric = config.count_metric(i);
64 auto it = mLogMatchers.find(metric.what());
65 if (it == mLogMatchers.end()) {
66 ALOGW("cannot find the LogEntryMatcher %s in config", metric.what().c_str());
67 continue;
68 }
69
70 if (metric.has_condition()) {
71 auto condition_it = conditionMap.find(metric.condition());
72 if (condition_it == conditionMap.end()) {
73 ALOGW("cannot find the Condition %s in the config", metric.condition().c_str());
74 continue;
75 }
76 it->second.push_back(make_unique<CountMetricProducer>(metric, condition_it->second));
77 } else {
78 it->second.push_back(make_unique<CountMetricProducer>(metric));
79 }
80 }
81
82 // TODO: build other types of metrics too.
83}
84
85MetricsManager::~MetricsManager() {
86 VLOG("~MetricManager()");
87}
88
89void MetricsManager::finish() {
90 for (auto const& entryPair : mLogMatchers) {
91 for (auto const& metric : entryPair.second) {
92 metric->finish();
93 }
94 }
95}
96
97// Consume the stats log if it's interesting to this metric.
98void MetricsManager::onLogEvent(const log_msg& logMsg) {
99 int tagId = getTagId(logMsg);
100 if (mTagIds.find(tagId) == mTagIds.end()) {
101 // not interesting...
102 return;
103 }
104 // Since at least one of the metrics is interested in this event, we parse it now.
105 LogEventWrapper event = LogEntryMatcherManager::parseLogEvent(logMsg);
106
107 // Evaluate the conditions. Order matters, this should happen
108 // before sending the event to metrics
109 for (auto& condition : mConditionTracker) {
110 condition->evaluateCondition(event);
111 }
112
113 // Now find out which LogMatcher matches this event, and let relevant metrics know.
114 for (auto matcher : mMatchers) {
115 if (LogEntryMatcherManager::matches(matcher, event)) {
116 auto it = mLogMatchers.find(matcher.name());
117 if (it != mLogMatchers.end()) {
118 for (auto const& it2 : it->second) {
119 // Only metrics that matches this event get notified.
120 it2->onMatchedLogEvent(event);
121 }
122 } else {
123 // TODO: we should remove any redundant matchers that the config provides.
124 ALOGW("Matcher not used by any metrics.");
125 }
126 }
127 }
128}
129
130} // namespace statsd
131} // namespace os
132} // namespace android