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