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 | #include "MetricsManager.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 19 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 20 | #include "CountMetricProducer.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -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 | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 25 | #include "metrics_manager_util.h" |
| 26 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 27 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 28 | #include <log/logprint.h> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 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, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 41 | mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap, |
| 42 | mTrackerToMetricMap, 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 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 59 | vector<std::unique_ptr<vector<uint8_t>>> MetricsManager::onDumpReport() { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 60 | VLOG("=========================Metric Reports Start=========================="); |
| 61 | // one StatsLogReport per MetricProduer |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 62 | vector<std::unique_ptr<vector<uint8_t>>> reportList; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 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 | vector<MatchingState> matcherCache(mAllLogEntryMatchers.size(), MatchingState::kNotComputed); |
| 84 | |
| 85 | for (auto& matcher : mAllLogEntryMatchers) { |
| 86 | matcher->onLogEvent(event, mAllLogEntryMatchers, matcherCache); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 89 | // A bitmap to see which ConditionTracker needs to be re-evaluated. |
| 90 | vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false); |
| 91 | |
| 92 | for (const auto& pair : mTrackerToConditionMap) { |
| 93 | if (matcherCache[pair.first] == MatchingState::kMatched) { |
| 94 | const auto& conditionList = pair.second; |
| 95 | for (const int conditionIndex : conditionList) { |
| 96 | conditionToBeEvaluated[conditionIndex] = true; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | vector<ConditionState> conditionCache(mAllConditionTrackers.size(), |
| 102 | ConditionState::kNotEvaluated); |
| 103 | // A bitmap to track if a condition has changed value. |
| 104 | vector<bool> changedCache(mAllConditionTrackers.size(), false); |
| 105 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 106 | if (conditionToBeEvaluated[i] == false) { |
| 107 | continue; |
| 108 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 109 | sp<ConditionTracker>& condition = mAllConditionTrackers[i]; |
| 110 | condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 111 | changedCache); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 115 | if (changedCache[i] == false) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 116 | continue; |
| 117 | } |
| 118 | auto pair = mConditionToMetricMap.find(i); |
| 119 | if (pair != mConditionToMetricMap.end()) { |
| 120 | auto& metricList = pair->second; |
| 121 | for (auto metricIndex : metricList) { |
| 122 | // metric cares about non sliced condition, and it's changed. |
| 123 | // Push the new condition to it directly. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 124 | if (!mAllMetricProducers[metricIndex]->isConditionSliced()) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 125 | mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i], |
| 126 | eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | // metric cares about sliced conditions, and it may have changed. Send |
| 128 | // notification, and the metric can query the sliced conditions that are |
| 129 | // interesting to it. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 130 | } else if (mAllMetricProducers[metricIndex]->isConditionSliced()) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 131 | mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 132 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // For matched LogEntryMatchers, tell relevant metrics that a matched event has come. |
| 138 | for (size_t i = 0; i < mAllLogEntryMatchers.size(); i++) { |
| 139 | if (matcherCache[i] == MatchingState::kMatched) { |
| 140 | auto pair = mTrackerToMetricMap.find(i); |
| 141 | if (pair != mTrackerToMetricMap.end()) { |
| 142 | auto& metricList = pair->second; |
| 143 | for (const int metricIndex : metricList) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 144 | // pushed metrics are never scheduled pulls |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 145 | mAllMetricProducers[metricIndex]->onMatchedLogEvent( |
| 146 | i, event, false /* schedulePull */); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 147 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 153 | void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs, |
| 154 | sp<const AnomalyAlarm> anomaly) { |
| 155 | for (const auto& itr : mAllAnomalyTrackers) { |
| 156 | itr->declareAnomaly(timestampNs); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) { |
| 161 | for (auto& itr : mAllAnomalyTrackers) { |
| 162 | itr->setAnomalyMonitor(anomalyMonitor); |
| 163 | } |
| 164 | } |
| 165 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 166 | // Returns the total byte size of all metrics managed by a single config source. |
| 167 | size_t MetricsManager::byteSize() { |
| 168 | size_t totalSize = 0; |
| 169 | for (auto metricProducer : mAllMetricProducers) { |
| 170 | totalSize += metricProducer->byteSize(); |
| 171 | } |
| 172 | return totalSize; |
| 173 | } |
| 174 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 175 | } // namespace statsd |
| 176 | } // namespace os |
| 177 | } // namespace android |