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 | |
| 19 | #include "MetricsManager.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 20 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | #include "../condition/CombinationConditionTracker.h" |
| 22 | #include "../condition/SimpleConditionTracker.h" |
| 23 | #include "../matchers/CombinationLogMatchingTracker.h" |
| 24 | #include "../matchers/SimpleLogMatchingTracker.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | #include "CountMetricProducer.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 26 | #include "metrics_manager_util.h" |
| 27 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | |
| 29 | using std::make_unique; |
| 30 | using std::set; |
| 31 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 32 | using std::unordered_map; |
| 33 | using std::vector; |
| 34 | |
| 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace statsd { |
| 38 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 39 | MetricsManager::MetricsManager(const StatsdConfig& config) { |
| 40 | mConfigValid = initStatsdConfig(config, mTagIds, mAllLogEntryMatchers, mAllConditionTrackers, |
| 41 | mAllMetricProducers, mConditionToMetricMap, mTrackerToMetricMap, |
| 42 | mTrackerToConditionMap); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | MetricsManager::~MetricsManager() { |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 46 | VLOG("~MetricsManager()"); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 49 | bool MetricsManager::isConfigValid() const { |
| 50 | return mConfigValid; |
| 51 | } |
| 52 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 53 | void MetricsManager::finish() { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 54 | for (auto& metricProducer : mAllMetricProducers) { |
| 55 | metricProducer->finish(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | vector<StatsLogReport> MetricsManager::onDumpReport() { |
| 60 | VLOG("=========================Metric Reports Start=========================="); |
| 61 | // one StatsLogReport per MetricProduer |
| 62 | vector<StatsLogReport> reportList; |
| 63 | for (auto& metric : mAllMetricProducers) { |
| 64 | reportList.push_back(metric->onDumpReport()); |
| 65 | } |
| 66 | VLOG("=========================Metric Reports End=========================="); |
| 67 | return reportList; |
| 68 | } |
| 69 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 70 | // Consume the stats log if it's interesting to this metric. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 71 | void MetricsManager::onLogEvent(const LogEvent& event) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 72 | if (!mConfigValid) { |
| 73 | return; |
| 74 | } |
| 75 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 76 | int tagId = event.GetTagId(); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 77 | uint64_t eventTime = event.GetTimestampNs(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 78 | if (mTagIds.find(tagId) == mTagIds.end()) { |
| 79 | // not interesting... |
| 80 | return; |
| 81 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 82 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 83 | // Since at least one of the metrics is interested in this event, we parse it now. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 84 | ALOGD("%s", event.ToString().c_str()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 85 | vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed); |
| 86 | |
| 87 | for (auto& matcher : mAllLogEntryMatchers) { |
| 88 | matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 91 | // A bitmap to see which ConditionTracker needs to be re-evaluated. |
| 92 | vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false); |
| 93 | |
| 94 | for (const auto& pair : mTrackerToConditionMap) { |
| 95 | if (matcherCache[pair.first] == MatchingState::kMatched) { |
| 96 | const auto& conditionList = pair.second; |
| 97 | for (const int conditionIndex : conditionList) { |
| 98 | conditionToBeEvaluated[conditionIndex] = true; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | vector<ConditionState> conditionCache(mAllConditionTrackers.size(), |
| 104 | ConditionState::kNotEvaluated); |
| 105 | // A bitmap to track if a condition has changed value. |
| 106 | vector<bool> changedCache(mAllConditionTrackers.size(), false); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 107 | vector<bool> slicedChangedCache(mAllConditionTrackers.size(), false); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 108 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 109 | if (conditionToBeEvaluated[i] == false) { |
| 110 | continue; |
| 111 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 112 | sp<ConditionTracker>& condition = mAllConditionTrackers[i]; |
| 113 | condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 114 | changedCache, slicedChangedCache); |
| 115 | } |
| 116 | |
| 117 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 118 | if (changedCache[i] == false && slicedChangedCache[i] == false) { |
| 119 | continue; |
| 120 | } |
| 121 | auto pair = mConditionToMetricMap.find(i); |
| 122 | if (pair != mConditionToMetricMap.end()) { |
| 123 | auto& metricList = pair->second; |
| 124 | for (auto metricIndex : metricList) { |
| 125 | // metric cares about non sliced condition, and it's changed. |
| 126 | // Push the new condition to it directly. |
| 127 | if (!mAllMetricProducers[metricIndex]->isConditionSliced() && changedCache[i]) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 128 | mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i], |
| 129 | eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 130 | // metric cares about sliced conditions, and it may have changed. Send |
| 131 | // notification, and the metric can query the sliced conditions that are |
| 132 | // interesting to it. |
| 133 | } else if (mAllMetricProducers[metricIndex]->isConditionSliced() && |
| 134 | slicedChangedCache[i]) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 135 | mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 136 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // For matched LogEntryMatchers, tell relevant metrics that a matched event has come. |
| 142 | for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) { |
| 143 | if (matcherCache[i] == MatchingState::kMatched) { |
| 144 | auto pair = mTrackerToMetricMap.find(i); |
| 145 | if (pair != mTrackerToMetricMap.end()) { |
| 146 | auto& metricList = pair->second; |
| 147 | for (const int metricIndex : metricList) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 148 | // pushed metrics are never scheduled pulls |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 149 | mAllMetricProducers[metricIndex]->onMatchedLogEvent( |
| 150 | i, event, false /* schedulePull */); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 151 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 157 | // Returns the total byte size of all metrics managed by a single config source. |
| 158 | size_t MetricsManager::byteSize() { |
| 159 | size_t totalSize = 0; |
| 160 | for (auto metricProducer : mAllMetricProducers) { |
| 161 | totalSize += metricProducer->byteSize(); |
| 162 | } |
| 163 | return totalSize; |
| 164 | } |
| 165 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 166 | } // namespace statsd |
| 167 | } // namespace os |
| 168 | } // namespace android |