Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 16 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame^] | 17 | #include "Log.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 18 | #define VLOG(...) \ |
| 19 | if (DEBUG) ALOGD(__VA_ARGS__); |
| 20 | |
| 21 | #include "MetricsManager.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 22 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | #include "../condition/CombinationConditionTracker.h" |
| 24 | #include "../condition/SimpleConditionTracker.h" |
| 25 | #include "../matchers/CombinationLogMatchingTracker.h" |
| 26 | #include "../matchers/SimpleLogMatchingTracker.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 27 | #include "CountMetricProducer.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | #include "metrics_manager_util.h" |
| 29 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 30 | |
| 31 | using std::make_unique; |
| 32 | using std::set; |
| 33 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 34 | using std::unordered_map; |
| 35 | using std::vector; |
| 36 | |
| 37 | namespace android { |
| 38 | namespace os { |
| 39 | namespace statsd { |
| 40 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 41 | MetricsManager::MetricsManager(const StatsdConfig& config) { |
| 42 | mConfigValid = initStatsdConfig(config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers, |
| 43 | mAllMetricProducers, mConditionToMetricMap, mTrackerToMetricMap, |
| 44 | mTrackerToConditionMap); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | MetricsManager::~MetricsManager() { |
| 48 | VLOG("~MetricManager()"); |
| 49 | } |
| 50 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 51 | bool MetricsManager::isConfigValid() const { |
| 52 | return mConfigValid; |
| 53 | } |
| 54 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 55 | void MetricsManager::finish() { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 56 | for (auto& metricProducer : mAllMetricProducers) { |
| 57 | metricProducer->finish(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | // Consume the stats log if it's interesting to this metric. |
| 62 | void MetricsManager::onLogEvent(const log_msg& logMsg) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 63 | if (!mConfigValid) { |
| 64 | return; |
| 65 | } |
| 66 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 67 | int tagId = getTagId(logMsg); |
| 68 | if (mTagIds.find(tagId) == mTagIds.end()) { |
| 69 | // not interesting... |
| 70 | return; |
| 71 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 72 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 73 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 81 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 111 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 112 | } |
| 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } // namespace statsd |
| 131 | } // namespace os |
| 132 | } // namespace android |