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" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 23 | #include "guardrail/StatsdStats.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 24 | #include "matchers/CombinationLogMatchingTracker.h" |
| 25 | #include "matchers/SimpleLogMatchingTracker.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 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 29 | #include <log/logprint.h> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 30 | using std::make_unique; |
| 31 | using std::set; |
| 32 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 33 | using std::unordered_map; |
| 34 | using std::vector; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace os { |
| 38 | namespace statsd { |
| 39 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 40 | MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config) : mConfigKey(key) { |
| 41 | mConfigValid = |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 42 | initStatsdConfig(key, config, mTagIds, mAllAtomMatchers, mAllConditionTrackers, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 43 | mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap, |
| 44 | mTrackerToMetricMap, mTrackerToConditionMap); |
| 45 | |
| 46 | // TODO: add alert size. |
| 47 | // no matter whether this config is valid, log it in the stats. |
| 48 | StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(), |
| 49 | mAllConditionTrackers.size(), |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 50 | mAllAtomMatchers.size(), 0, mConfigValid); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 51 | // Guardrail. Reject the config if it's too big. |
| 52 | if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig || |
| 53 | mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig || |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 54 | mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 55 | ALOGE("This config is too big! Reject!"); |
| 56 | mConfigValid = false; |
| 57 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | MetricsManager::~MetricsManager() { |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 61 | VLOG("~MetricsManager()"); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 64 | bool MetricsManager::isConfigValid() const { |
| 65 | return mConfigValid; |
| 66 | } |
| 67 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 68 | void MetricsManager::finish() { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 69 | for (auto& metricProducer : mAllMetricProducers) { |
| 70 | metricProducer->finish(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 74 | vector<std::unique_ptr<vector<uint8_t>>> MetricsManager::onDumpReport() { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 75 | VLOG("=========================Metric Reports Start=========================="); |
| 76 | // one StatsLogReport per MetricProduer |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 77 | vector<std::unique_ptr<vector<uint8_t>>> reportList; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 78 | for (auto& metric : mAllMetricProducers) { |
| 79 | reportList.push_back(metric->onDumpReport()); |
| 80 | } |
| 81 | VLOG("=========================Metric Reports End=========================="); |
| 82 | return reportList; |
| 83 | } |
| 84 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 85 | // Consume the stats log if it's interesting to this metric. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 86 | void MetricsManager::onLogEvent(const LogEvent& event) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 87 | if (!mConfigValid) { |
| 88 | return; |
| 89 | } |
| 90 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 91 | int tagId = event.GetTagId(); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 92 | uint64_t eventTime = event.GetTimestampNs(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 93 | if (mTagIds.find(tagId) == mTagIds.end()) { |
| 94 | // not interesting... |
| 95 | return; |
| 96 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 97 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 98 | vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 99 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 100 | for (auto& matcher : mAllAtomMatchers) { |
| 101 | matcher->onLogEvent(event, mAllAtomMatchers, matcherCache); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 104 | // A bitmap to see which ConditionTracker needs to be re-evaluated. |
| 105 | vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false); |
| 106 | |
| 107 | for (const auto& pair : mTrackerToConditionMap) { |
| 108 | if (matcherCache[pair.first] == MatchingState::kMatched) { |
| 109 | const auto& conditionList = pair.second; |
| 110 | for (const int conditionIndex : conditionList) { |
| 111 | conditionToBeEvaluated[conditionIndex] = true; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | vector<ConditionState> conditionCache(mAllConditionTrackers.size(), |
| 117 | ConditionState::kNotEvaluated); |
| 118 | // A bitmap to track if a condition has changed value. |
| 119 | vector<bool> changedCache(mAllConditionTrackers.size(), false); |
| 120 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 121 | if (conditionToBeEvaluated[i] == false) { |
| 122 | continue; |
| 123 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 124 | sp<ConditionTracker>& condition = mAllConditionTrackers[i]; |
| 125 | condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 126 | changedCache); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 130 | if (changedCache[i] == false) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | auto pair = mConditionToMetricMap.find(i); |
| 134 | if (pair != mConditionToMetricMap.end()) { |
| 135 | auto& metricList = pair->second; |
| 136 | for (auto metricIndex : metricList) { |
| 137 | // metric cares about non sliced condition, and it's changed. |
| 138 | // Push the new condition to it directly. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 139 | if (!mAllMetricProducers[metricIndex]->isConditionSliced()) { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 140 | mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i], |
| 141 | eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 142 | // metric cares about sliced conditions, and it may have changed. Send |
| 143 | // notification, and the metric can query the sliced conditions that are |
| 144 | // interesting to it. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 145 | } else { |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 146 | mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 147 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 152 | // For matched AtomMatchers, tell relevant metrics that a matched event has come. |
| 153 | for (size_t i = 0; i < mAllAtomMatchers.size(); i++) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 154 | if (matcherCache[i] == MatchingState::kMatched) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 155 | StatsdStats::getInstance().noteMatcherMatched(mConfigKey, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame^] | 156 | mAllAtomMatchers[i]->getName()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 157 | auto pair = mTrackerToMetricMap.find(i); |
| 158 | if (pair != mTrackerToMetricMap.end()) { |
| 159 | auto& metricList = pair->second; |
| 160 | for (const int metricIndex : metricList) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 161 | // pushed metrics are never scheduled pulls |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 162 | mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event, |
| 163 | false /* schedulePull */); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 164 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 170 | void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs, |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 171 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 172 | for (const auto& itr : mAllAnomalyTrackers) { |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 173 | itr->informAlarmsFired(timestampNs, anomalySet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) { |
| 178 | for (auto& itr : mAllAnomalyTrackers) { |
| 179 | itr->setAnomalyMonitor(anomalyMonitor); |
| 180 | } |
| 181 | } |
| 182 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 183 | // Returns the total byte size of all metrics managed by a single config source. |
| 184 | size_t MetricsManager::byteSize() { |
| 185 | size_t totalSize = 0; |
| 186 | for (auto metricProducer : mAllMetricProducers) { |
| 187 | totalSize += metricProducer->byteSize(); |
| 188 | } |
| 189 | return totalSize; |
| 190 | } |
| 191 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 192 | } // namespace statsd |
| 193 | } // namespace os |
| 194 | } // namespace android |