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 | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 19 | #include "statslog.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 20 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 21 | #include "CountMetricProducer.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 22 | #include "condition/CombinationConditionTracker.h" |
| 23 | #include "condition/SimpleConditionTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 24 | #include "guardrail/StatsdStats.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 25 | #include "matchers/CombinationLogMatchingTracker.h" |
| 26 | #include "matchers/SimpleLogMatchingTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 27 | #include "metrics_manager_util.h" |
| 28 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 29 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 30 | #include <log/logprint.h> |
Bookatz | 6f19790 | 2018-02-05 12:30:14 -0800 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 32 | #include <utils/SystemClock.h> |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 33 | |
| 34 | using android::util::FIELD_COUNT_REPEATED; |
| 35 | using android::util::FIELD_TYPE_MESSAGE; |
| 36 | using android::util::ProtoOutputStream; |
| 37 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 38 | using std::make_unique; |
| 39 | using std::set; |
| 40 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 41 | using std::unordered_map; |
| 42 | using std::vector; |
| 43 | |
| 44 | namespace android { |
| 45 | namespace os { |
| 46 | namespace statsd { |
| 47 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 48 | const int FIELD_ID_METRICS = 1; |
| 49 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 50 | MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config, |
| 51 | const long timeBaseSec, sp<UidMap> uidMap) |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 52 | : mConfigKey(key), mUidMap(uidMap), mLastReportTimeNs(0) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 53 | mConfigValid = |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 54 | initStatsdConfig(key, config, *uidMap, timeBaseSec, mTagIds, mAllAtomMatchers, mAllConditionTrackers, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 55 | mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 56 | mTrackerToMetricMap, mTrackerToConditionMap, mNoReportMetricIds); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 57 | |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 58 | if (config.allowed_log_source_size() == 0) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 59 | // TODO(b/70794411): uncomment the following line and remove the hard coded log source |
| 60 | // after all configs have the log source added. |
| 61 | // mConfigValid = false; |
| 62 | // ALOGE("Log source white list is empty! This config won't get any data."); |
| 63 | |
Bookatz | 6f19790 | 2018-02-05 12:30:14 -0800 | [diff] [blame] | 64 | mAllowedUid.push_back(AID_ROOT); |
| 65 | mAllowedUid.push_back(AID_STATSD); |
| 66 | mAllowedUid.push_back(AID_SYSTEM); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 67 | mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end()); |
| 68 | } else { |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 69 | for (const auto& source : config.allowed_log_source()) { |
| 70 | auto it = UidMap::sAidToUidMapping.find(source); |
| 71 | if (it != UidMap::sAidToUidMapping.end()) { |
| 72 | mAllowedUid.push_back(it->second); |
| 73 | } else { |
| 74 | mAllowedPkg.push_back(source); |
| 75 | } |
| 76 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 77 | |
| 78 | if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) { |
| 79 | ALOGE("Too many log sources. This is likely to be an error in the config."); |
| 80 | mConfigValid = false; |
| 81 | } else { |
| 82 | initLogSourceWhiteList(); |
| 83 | } |
| 84 | } |
| 85 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 86 | // Guardrail. Reject the config if it's too big. |
| 87 | if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig || |
| 88 | mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig || |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 89 | mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 90 | ALOGE("This config is too big! Reject!"); |
| 91 | mConfigValid = false; |
| 92 | } |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 93 | |
| 94 | // TODO: add alert size. |
| 95 | // no matter whether this config is valid, log it in the stats. |
| 96 | StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(), |
| 97 | mAllConditionTrackers.size(), |
| 98 | mAllAtomMatchers.size(), 0, mConfigValid); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | MetricsManager::~MetricsManager() { |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 102 | VLOG("~MetricsManager()"); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 105 | void MetricsManager::initLogSourceWhiteList() { |
| 106 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 107 | mAllowedLogSources.clear(); |
| 108 | mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end()); |
| 109 | |
| 110 | for (const auto& pkg : mAllowedPkg) { |
| 111 | auto uids = mUidMap->getAppUid(pkg); |
| 112 | mAllowedLogSources.insert(uids.begin(), uids.end()); |
| 113 | } |
| 114 | if (DEBUG) { |
| 115 | for (const auto& uid : mAllowedLogSources) { |
| 116 | VLOG("Allowed uid %d", uid); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 121 | bool MetricsManager::isConfigValid() const { |
| 122 | return mConfigValid; |
| 123 | } |
| 124 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 125 | void MetricsManager::notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid, |
| 126 | const int64_t version) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 127 | // check if we care this package |
| 128 | if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) { |
| 129 | return; |
| 130 | } |
| 131 | // We will re-initialize the whole list because we don't want to keep the multi mapping of |
| 132 | // UID<->pkg inside MetricsManager to reduce the memory usage. |
| 133 | initLogSourceWhiteList(); |
| 134 | } |
| 135 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 136 | void MetricsManager::notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, |
| 137 | const int uid) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 138 | // check if we care this package |
| 139 | if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) { |
| 140 | return; |
| 141 | } |
| 142 | // We will re-initialize the whole list because we don't want to keep the multi mapping of |
| 143 | // UID<->pkg inside MetricsManager to reduce the memory usage. |
| 144 | initLogSourceWhiteList(); |
| 145 | } |
| 146 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 147 | void MetricsManager::onUidMapReceived(const uint64_t& eventTimeNs) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 148 | if (mAllowedPkg.size() == 0) { |
| 149 | return; |
| 150 | } |
| 151 | initLogSourceWhiteList(); |
| 152 | } |
| 153 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 154 | void MetricsManager::onDumpReport(const uint64_t& dumpTimeStampNs, ConfigMetricsReport* report) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 155 | for (const auto& producer : mAllMetricProducers) { |
| 156 | if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) { |
| 157 | producer->onDumpReport(dumpTimeStampNs, report->add_metrics()); |
| 158 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 162 | void MetricsManager::dumpStates(FILE* out, bool verbose) { |
| 163 | fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str()); |
| 164 | { |
| 165 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 166 | for (const auto& source : mAllowedLogSources) { |
| 167 | fprintf(out, "%d ", source); |
| 168 | } |
| 169 | } |
| 170 | fprintf(out, "\n"); |
| 171 | for (const auto& producer : mAllMetricProducers) { |
| 172 | producer->dumpStates(out, verbose); |
| 173 | } |
| 174 | } |
| 175 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 176 | void MetricsManager::onDumpReport(ProtoOutputStream* protoOutput) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 177 | VLOG("=========================Metric Reports Start=========================="); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 178 | uint64_t dumpTimeStampNs = time(nullptr) * NS_PER_SEC; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 179 | // one StatsLogReport per MetricProduer |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 180 | for (const auto& producer : mAllMetricProducers) { |
| 181 | if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) { |
| 182 | long long token = |
| 183 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS); |
| 184 | producer->onDumpReport(dumpTimeStampNs, protoOutput); |
| 185 | protoOutput->end(token); |
| 186 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 187 | } |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 188 | mLastReportTimeNs = ::android::elapsedRealtimeNano(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 189 | VLOG("=========================Metric Reports End=========================="); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 192 | // Consume the stats log if it's interesting to this metric. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 193 | void MetricsManager::onLogEvent(const LogEvent& event) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 194 | if (!mConfigValid) { |
| 195 | return; |
| 196 | } |
| 197 | |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 198 | if (event.GetTagId() == android::util::APP_HOOK) { // Check that app hook fields are valid. |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 199 | // TODO: Find a way to make these checks easier to maintain if the app hooks get changed. |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 200 | status_t err = NO_ERROR; |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 201 | |
| 202 | // Uid is 3rd from last field and must match the caller's uid, |
| 203 | // unless that caller is statsd itself (statsd is allowed to spoof uids). |
| 204 | long appHookUid = event.GetLong(event.size()-2, &err); |
| 205 | int32_t loggerUid = event.GetUid(); |
Bookatz | 6f19790 | 2018-02-05 12:30:14 -0800 | [diff] [blame] | 206 | if (err != NO_ERROR || (loggerUid != appHookUid && loggerUid != AID_STATSD)) { |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 207 | VLOG("AppHook has invalid uid: claimed %ld but caller is %d", appHookUid, loggerUid); |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 208 | return; |
| 209 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 210 | |
| 211 | // Label is 2nd from last field and must be from [0, 15]. |
| 212 | long appHookLabel = event.GetLong(event.size()-1, &err); |
| 213 | if (err != NO_ERROR || appHookLabel < 0 || appHookLabel > 15) { |
| 214 | VLOG("AppHook does not have valid label %ld", appHookLabel); |
| 215 | return; |
| 216 | } |
| 217 | |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 218 | // The state must be from 0,3. This part of code must be manually updated. |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 219 | long appHookState = event.GetLong(event.size(), &err); |
| 220 | if (err != NO_ERROR || appHookState < 0 || appHookState > 3) { |
| 221 | VLOG("AppHook does not have valid state %ld", appHookState); |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 224 | } else if (event.GetTagId() == android::util::DAVEY_OCCURRED) { |
| 225 | // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp. |
| 226 | // Check that the davey duration is reasonable. Max length check is for privacy. |
| 227 | status_t err = NO_ERROR; |
| 228 | long duration = event.GetLong(event.size(), &err); |
| 229 | if (err != NO_ERROR || duration > 100000) { |
| 230 | VLOG("Davey duration is unreasonably long: %ld", duration); |
| 231 | return; |
| 232 | } |
| 233 | } else { |
| 234 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 235 | if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) { |
| 236 | VLOG("log source %d not on the whitelist", event.GetUid()); |
| 237 | return; |
| 238 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 241 | int tagId = event.GetTagId(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 242 | uint64_t eventTime = event.GetTimestampNs(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 243 | if (mTagIds.find(tagId) == mTagIds.end()) { |
| 244 | // not interesting... |
| 245 | return; |
| 246 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 247 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 248 | vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 249 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 250 | for (auto& matcher : mAllAtomMatchers) { |
| 251 | matcher->onLogEvent(event, mAllAtomMatchers, matcherCache); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 254 | // A bitmap to see which ConditionTracker needs to be re-evaluated. |
| 255 | vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false); |
| 256 | |
| 257 | for (const auto& pair : mTrackerToConditionMap) { |
| 258 | if (matcherCache[pair.first] == MatchingState::kMatched) { |
| 259 | const auto& conditionList = pair.second; |
| 260 | for (const int conditionIndex : conditionList) { |
| 261 | conditionToBeEvaluated[conditionIndex] = true; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | vector<ConditionState> conditionCache(mAllConditionTrackers.size(), |
| 267 | ConditionState::kNotEvaluated); |
| 268 | // A bitmap to track if a condition has changed value. |
| 269 | vector<bool> changedCache(mAllConditionTrackers.size(), false); |
| 270 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 271 | if (conditionToBeEvaluated[i] == false) { |
| 272 | continue; |
| 273 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 274 | sp<ConditionTracker>& condition = mAllConditionTrackers[i]; |
| 275 | condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 276 | changedCache); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 280 | if (changedCache[i] == false) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 281 | continue; |
| 282 | } |
| 283 | auto pair = mConditionToMetricMap.find(i); |
| 284 | if (pair != mConditionToMetricMap.end()) { |
| 285 | auto& metricList = pair->second; |
| 286 | for (auto metricIndex : metricList) { |
| 287 | // metric cares about non sliced condition, and it's changed. |
| 288 | // Push the new condition to it directly. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 289 | if (!mAllMetricProducers[metricIndex]->isConditionSliced()) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 290 | mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i], |
| 291 | eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 292 | // metric cares about sliced conditions, and it may have changed. Send |
| 293 | // notification, and the metric can query the sliced conditions that are |
| 294 | // interesting to it. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 295 | } else { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 296 | mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 297 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 302 | // For matched AtomMatchers, tell relevant metrics that a matched event has come. |
| 303 | for (size_t i = 0; i < mAllAtomMatchers.size(); i++) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 304 | if (matcherCache[i] == MatchingState::kMatched) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 305 | StatsdStats::getInstance().noteMatcherMatched(mConfigKey, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 306 | mAllAtomMatchers[i]->getId()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 307 | auto pair = mTrackerToMetricMap.find(i); |
| 308 | if (pair != mTrackerToMetricMap.end()) { |
| 309 | auto& metricList = pair->second; |
| 310 | for (const int metricIndex : metricList) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 311 | // pushed metrics are never scheduled pulls |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 312 | mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 313 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 319 | void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs, |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 320 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 321 | for (const auto& itr : mAllAnomalyTrackers) { |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 322 | itr->informAlarmsFired(timestampNs, anomalySet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) { |
| 327 | for (auto& itr : mAllAnomalyTrackers) { |
| 328 | itr->setAnomalyMonitor(anomalyMonitor); |
| 329 | } |
| 330 | } |
| 331 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 332 | // Returns the total byte size of all metrics managed by a single config source. |
| 333 | size_t MetricsManager::byteSize() { |
| 334 | size_t totalSize = 0; |
| 335 | for (auto metricProducer : mAllMetricProducers) { |
| 336 | totalSize += metricProducer->byteSize(); |
| 337 | } |
| 338 | return totalSize; |
| 339 | } |
| 340 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 341 | } // namespace statsd |
| 342 | } // namespace os |
| 343 | } // namespace android |