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