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