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" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 18 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 19 | #include "MetricsManager.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 20 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 21 | #include <private/android_filesystem_config.h> |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 22 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 23 | #include "CountMetricProducer.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 24 | #include "condition/CombinationConditionTracker.h" |
| 25 | #include "condition/SimpleConditionTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 26 | #include "guardrail/StatsdStats.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 27 | #include "matchers/CombinationLogMatchingTracker.h" |
| 28 | #include "matchers/SimpleLogMatchingTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | #include "metrics_manager_util.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 30 | #include "state/StateManager.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 31 | #include "stats_log_util.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 32 | #include "stats_util.h" |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 33 | #include "statslog_statsd.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::set; |
| 43 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 44 | using std::vector; |
| 45 | |
| 46 | namespace android { |
| 47 | namespace os { |
| 48 | namespace statsd { |
| 49 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 50 | const int FIELD_ID_METRICS = 1; |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 51 | const int FIELD_ID_ANNOTATIONS = 7; |
| 52 | const int FIELD_ID_ANNOTATIONS_INT64 = 1; |
| 53 | const int FIELD_ID_ANNOTATIONS_INT32 = 2; |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 54 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 55 | // for ActiveConfig |
| 56 | const int FIELD_ID_ACTIVE_CONFIG_ID = 1; |
| 57 | const int FIELD_ID_ACTIVE_CONFIG_UID = 2; |
| 58 | const int FIELD_ID_ACTIVE_CONFIG_METRIC = 3; |
| 59 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 60 | MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 61 | const int64_t timeBaseNs, const int64_t currentTimeNs, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 62 | const sp<UidMap>& uidMap, |
| 63 | const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 64 | const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 65 | const sp<AlarmMonitor>& periodicAlarmMonitor) |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 66 | : mConfigKey(key), |
| 67 | mUidMap(uidMap), |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 68 | mTtlNs(config.has_ttl_in_seconds() ? config.ttl_in_seconds() * NS_PER_SEC : -1), |
| 69 | mTtlEndNs(-1), |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 70 | mLastReportTimeNs(currentTimeNs), |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 71 | mLastReportWallClockNs(getWallClockNs()), |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 72 | mPullerManager(pullerManager), |
Muhammad Qureshi | f681646 | 2020-05-13 15:13:33 -0700 | [diff] [blame] | 73 | mWhitelistedAtomIds(config.whitelisted_atom_ids().begin(), |
| 74 | config.whitelisted_atom_ids().end()), |
Yao Chen | 9a43b4f | 2019-04-10 10:43:20 -0700 | [diff] [blame] | 75 | mShouldPersistHistory(config.persist_locally()) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 76 | // Init the ttl end timestamp. |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 77 | refreshTtl(timeBaseNs); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 78 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 79 | mConfigValid = initStatsdConfig( |
| 80 | key, config, *uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, |
| 81 | timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, mAllConditionTrackers, |
| 82 | mAllMetricProducers, mAllAnomalyTrackers, mAllPeriodicAlarmTrackers, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 83 | mConditionToMetricMap, mTrackerToMetricMap, mTrackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 84 | mActivationAtomTrackerToMetricMap, mDeactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 85 | mAlertTrackerMap, mMetricIndexesWithActivation, mNoReportMetricIds); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 86 | |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 87 | mHashStringsInReport = config.hash_strings_in_metric_report(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 88 | mVersionStringsInReport = config.version_strings_in_metric_report(); |
| 89 | mInstallerInReport = config.installer_in_metric_report(); |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 90 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 91 | // Init allowed pushed atom uids. |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 92 | if (config.allowed_log_source_size() == 0) { |
David Chen | 8faaa01 | 2018-02-28 15:54:36 -0800 | [diff] [blame] | 93 | mConfigValid = false; |
| 94 | ALOGE("Log source whitelist is empty! This config won't get any data. Suggest adding at " |
| 95 | "least AID_SYSTEM and AID_STATSD to the allowed_log_source field."); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 96 | } else { |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 97 | for (const auto& source : config.allowed_log_source()) { |
| 98 | auto it = UidMap::sAidToUidMapping.find(source); |
| 99 | if (it != UidMap::sAidToUidMapping.end()) { |
| 100 | mAllowedUid.push_back(it->second); |
| 101 | } else { |
| 102 | mAllowedPkg.push_back(source); |
| 103 | } |
| 104 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 105 | |
| 106 | if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) { |
| 107 | ALOGE("Too many log sources. This is likely to be an error in the config."); |
| 108 | mConfigValid = false; |
| 109 | } else { |
| 110 | initLogSourceWhiteList(); |
| 111 | } |
| 112 | } |
| 113 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 114 | // Init default allowed pull atom uids. |
| 115 | int numPullPackages = 0; |
| 116 | for (const string& pullSource : config.default_pull_packages()) { |
| 117 | auto it = UidMap::sAidToUidMapping.find(pullSource); |
| 118 | if (it != UidMap::sAidToUidMapping.end()) { |
| 119 | numPullPackages++; |
| 120 | mDefaultPullUids.insert(it->second); |
| 121 | } else { |
| 122 | ALOGE("Default pull atom packages must be in sAidToUidMapping"); |
| 123 | mConfigValid = false; |
| 124 | } |
| 125 | } |
| 126 | // Init per-atom pull atom packages. |
| 127 | for (const PullAtomPackages& pullAtomPackages : config.pull_atom_packages()) { |
| 128 | int32_t atomId = pullAtomPackages.atom_id(); |
| 129 | for (const string& pullPackage : pullAtomPackages.packages()) { |
| 130 | numPullPackages++; |
| 131 | auto it = UidMap::sAidToUidMapping.find(pullPackage); |
| 132 | if (it != UidMap::sAidToUidMapping.end()) { |
| 133 | mPullAtomUids[atomId].insert(it->second); |
| 134 | } else { |
| 135 | mPullAtomPackages[atomId].insert(pullPackage); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if (numPullPackages > StatsdStats::kMaxPullAtomPackages) { |
| 140 | ALOGE("Too many sources in default_pull_packages and pull_atom_packages. This is likely to " |
| 141 | "be an error in the config"); |
| 142 | mConfigValid = false; |
| 143 | } else { |
| 144 | initPullAtomSources(); |
| 145 | } |
| 146 | mPullerManager->RegisterPullUidProvider(mConfigKey, this); |
| 147 | |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 148 | // Store the sub-configs used. |
| 149 | for (const auto& annotation : config.annotation()) { |
| 150 | mAnnotations.emplace_back(annotation.field_int64(), annotation.field_int32()); |
| 151 | } |
| 152 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 153 | // Guardrail. Reject the config if it's too big. |
| 154 | if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig || |
| 155 | mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig || |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 156 | mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 157 | ALOGE("This config is too big! Reject!"); |
| 158 | mConfigValid = false; |
| 159 | } |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 160 | if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) { |
| 161 | ALOGE("This config has too many alerts! Reject!"); |
| 162 | mConfigValid = false; |
| 163 | } |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 164 | |
| 165 | mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) || |
| 166 | (mAllMetricProducers.size() == 0); |
| 167 | bool isActive = mIsAlwaysActive; |
| 168 | for (int metric : mMetricIndexesWithActivation) { |
| 169 | isActive |= mAllMetricProducers[metric]->isActive(); |
| 170 | } |
| 171 | mIsActive = isActive; |
| 172 | VLOG("mIsActive is initialized to %d", mIsActive) |
| 173 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 174 | // no matter whether this config is valid, log it in the stats. |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 175 | StatsdStats::getInstance().noteConfigReceived( |
| 176 | key, mAllMetricProducers.size(), mAllConditionTrackers.size(), mAllAtomMatchers.size(), |
| 177 | mAllAnomalyTrackers.size(), mAnnotations, mConfigValid); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 178 | // Check active |
| 179 | for (const auto& metric : mAllMetricProducers) { |
| 180 | if (metric->isActive()) { |
| 181 | mIsActive = true; |
| 182 | break; |
| 183 | } |
| 184 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | MetricsManager::~MetricsManager() { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 188 | for (auto it : mAllMetricProducers) { |
| 189 | for (int atomId : it->getSlicedStateAtoms()) { |
| 190 | StateManager::getInstance().unregisterListener(atomId, it); |
| 191 | } |
| 192 | } |
Tej Singh | 3eb9ced | 2020-04-20 22:04:38 -0700 | [diff] [blame] | 193 | mPullerManager->UnregisterPullUidProvider(mConfigKey, this); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 194 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 195 | VLOG("~MetricsManager()"); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 198 | void MetricsManager::initLogSourceWhiteList() { |
| 199 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 200 | mAllowedLogSources.clear(); |
| 201 | mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end()); |
| 202 | |
| 203 | for (const auto& pkg : mAllowedPkg) { |
| 204 | auto uids = mUidMap->getAppUid(pkg); |
| 205 | mAllowedLogSources.insert(uids.begin(), uids.end()); |
| 206 | } |
| 207 | if (DEBUG) { |
| 208 | for (const auto& uid : mAllowedLogSources) { |
| 209 | VLOG("Allowed uid %d", uid); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 214 | void MetricsManager::initPullAtomSources() { |
| 215 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 216 | mCombinedPullAtomUids.clear(); |
| 217 | for (const auto& [atomId, uids] : mPullAtomUids) { |
| 218 | mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end()); |
| 219 | } |
| 220 | for (const auto& [atomId, packages] : mPullAtomPackages) { |
| 221 | for (const string& pkg : packages) { |
| 222 | set<int32_t> uids = mUidMap->getAppUid(pkg); |
| 223 | mCombinedPullAtomUids[atomId].insert(uids.begin(), uids.end()); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 228 | bool MetricsManager::isConfigValid() const { |
| 229 | return mConfigValid; |
| 230 | } |
| 231 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 232 | 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] | 233 | const int64_t version) { |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 234 | // Inform all metric producers. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 235 | for (const auto& it : mAllMetricProducers) { |
| 236 | it->notifyAppUpgrade(eventTimeNs); |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 237 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 238 | // check if we care this package |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 239 | if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) { |
| 240 | // We will re-initialize the whole list because we don't want to keep the multi mapping of |
| 241 | // UID<->pkg inside MetricsManager to reduce the memory usage. |
| 242 | initLogSourceWhiteList(); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 243 | } |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 244 | |
| 245 | for (const auto& it : mPullAtomPackages) { |
| 246 | if (it.second.find(apk) != it.second.end()) { |
| 247 | initPullAtomSources(); |
| 248 | return; |
| 249 | } |
| 250 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 253 | void MetricsManager::notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 254 | const int uid) { |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 255 | // Inform all metric producers. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 256 | for (const auto& it : mAllMetricProducers) { |
| 257 | it->notifyAppRemoved(eventTimeNs); |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 258 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 259 | // check if we care this package |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 260 | if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) != mAllowedPkg.end()) { |
| 261 | // We will re-initialize the whole list because we don't want to keep the multi mapping of |
| 262 | // UID<->pkg inside MetricsManager to reduce the memory usage. |
| 263 | initLogSourceWhiteList(); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 264 | } |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 265 | |
| 266 | for (const auto& it : mPullAtomPackages) { |
| 267 | if (it.second.find(apk) != it.second.end()) { |
| 268 | initPullAtomSources(); |
| 269 | return; |
| 270 | } |
| 271 | } |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 274 | void MetricsManager::onUidMapReceived(const int64_t& eventTimeNs) { |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 275 | // Purposefully don't inform metric producers on a new snapshot |
| 276 | // because we don't need to flush partial buckets. |
| 277 | // This occurs if a new user is added/removed or statsd crashes. |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 278 | initPullAtomSources(); |
| 279 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 280 | if (mAllowedPkg.size() == 0) { |
| 281 | return; |
| 282 | } |
| 283 | initLogSourceWhiteList(); |
| 284 | } |
| 285 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 286 | void MetricsManager::onStatsdInitCompleted(const int64_t& eventTimeNs) { |
| 287 | // Inform all metric producers. |
| 288 | for (const auto& it : mAllMetricProducers) { |
| 289 | it->onStatsdInitCompleted(eventTimeNs); |
| 290 | } |
| 291 | } |
| 292 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 293 | void MetricsManager::init() { |
| 294 | for (const auto& producer : mAllMetricProducers) { |
| 295 | producer->prepareFirstBucket(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | vector<int32_t> MetricsManager::getPullAtomUids(int32_t atomId) { |
| 300 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 301 | vector<int32_t> uids; |
| 302 | const auto& it = mCombinedPullAtomUids.find(atomId); |
| 303 | if (it != mCombinedPullAtomUids.end()) { |
| 304 | uids.insert(uids.end(), it->second.begin(), it->second.end()); |
| 305 | } |
| 306 | uids.insert(uids.end(), mDefaultPullUids.begin(), mDefaultPullUids.end()); |
| 307 | return uids; |
| 308 | } |
| 309 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 310 | void MetricsManager::dumpStates(FILE* out, bool verbose) { |
| 311 | fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str()); |
| 312 | { |
| 313 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 314 | for (const auto& source : mAllowedLogSources) { |
| 315 | fprintf(out, "%d ", source); |
| 316 | } |
| 317 | } |
| 318 | fprintf(out, "\n"); |
| 319 | for (const auto& producer : mAllMetricProducers) { |
| 320 | producer->dumpStates(out, verbose); |
| 321 | } |
| 322 | } |
| 323 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 324 | void MetricsManager::dropData(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 325 | for (const auto& producer : mAllMetricProducers) { |
| 326 | producer->dropData(dropTimeNs); |
| 327 | } |
| 328 | } |
| 329 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 330 | void MetricsManager::onDumpReport(const int64_t dumpTimeStampNs, |
| 331 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 332 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 333 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 334 | std::set<string> *str_set, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 335 | ProtoOutputStream* protoOutput) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 336 | VLOG("=========================Metric Reports Start=========================="); |
| 337 | // one StatsLogReport per MetricProduer |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 338 | for (const auto& producer : mAllMetricProducers) { |
| 339 | if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) { |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 340 | uint64_t token = protoOutput->start( |
| 341 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS); |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 342 | if (mHashStringsInReport) { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 343 | producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 344 | dumpLatency, str_set, protoOutput); |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 345 | } else { |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 346 | producer->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 347 | dumpLatency, nullptr, protoOutput); |
Yangster-mac | 1c58f04 | 2018-05-17 15:52:51 -0700 | [diff] [blame] | 348 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 349 | protoOutput->end(token); |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 350 | } else { |
| 351 | producer->clearPastBuckets(dumpTimeStampNs); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 352 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 353 | } |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 354 | for (const auto& annotation : mAnnotations) { |
| 355 | uint64_t token = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 356 | FIELD_ID_ANNOTATIONS); |
| 357 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ANNOTATIONS_INT64, |
| 358 | (long long)annotation.first); |
| 359 | protoOutput->write(FIELD_TYPE_INT32 | FIELD_ID_ANNOTATIONS_INT32, annotation.second); |
| 360 | protoOutput->end(token); |
| 361 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 362 | |
Jeffrey Huang | 289eae6 | 2020-06-10 16:28:23 -0700 | [diff] [blame] | 363 | // Do not update the timestamps when data is not cleared to avoid timestamps from being |
| 364 | // misaligned. |
| 365 | if (erase_data) { |
| 366 | mLastReportTimeNs = dumpTimeStampNs; |
| 367 | mLastReportWallClockNs = getWallClockNs(); |
| 368 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 369 | VLOG("=========================Metric Reports End=========================="); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 372 | |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 373 | bool MetricsManager::checkLogCredentials(const LogEvent& event) { |
Muhammad Qureshi | f681646 | 2020-05-13 15:13:33 -0700 | [diff] [blame] | 374 | if (mWhitelistedAtomIds.find(event.GetTagId()) != mWhitelistedAtomIds.end()) { |
| 375 | return true; |
| 376 | } |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 377 | std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex); |
| 378 | if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) { |
| 379 | VLOG("log source %d not on the whitelist", event.GetUid()); |
| 380 | return false; |
| 381 | } |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | bool MetricsManager::eventSanityCheck(const LogEvent& event) { |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 386 | if (event.GetTagId() == util::APP_BREADCRUMB_REPORTED) { |
David Chen | b639d14 | 2018-02-14 17:29:54 -0800 | [diff] [blame] | 387 | // Check that app breadcrumb reported fields are valid. |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 388 | status_t err = NO_ERROR; |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 389 | |
| 390 | // Uid is 3rd from last field and must match the caller's uid, |
| 391 | // unless that caller is statsd itself (statsd is allowed to spoof uids). |
| 392 | long appHookUid = event.GetLong(event.size()-2, &err); |
Ruchir Rastogi | d4cc693 | 2020-04-20 11:51:14 -0700 | [diff] [blame] | 393 | if (err != NO_ERROR) { |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 394 | VLOG("APP_BREADCRUMB_REPORTED had error when parsing the uid"); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 395 | return false; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 396 | } |
Ruchir Rastogi | d4cc693 | 2020-04-20 11:51:14 -0700 | [diff] [blame] | 397 | |
| 398 | // Because the uid within the LogEvent may have been mapped from |
| 399 | // isolated to host, map the loggerUid similarly before comparing. |
| 400 | int32_t loggerUid = mUidMap->getHostUidOrSelf(event.GetUid()); |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 401 | if (loggerUid != appHookUid && loggerUid != AID_STATSD) { |
| 402 | VLOG("APP_BREADCRUMB_REPORTED has invalid uid: claimed %ld but caller is %d", |
| 403 | appHookUid, loggerUid); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 404 | return false; |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 405 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 406 | |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 407 | // 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] | 408 | long appHookState = event.GetLong(event.size(), &err); |
Ruchir Rastogi | d4cc693 | 2020-04-20 11:51:14 -0700 | [diff] [blame] | 409 | if (err != NO_ERROR) { |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 410 | VLOG("APP_BREADCRUMB_REPORTED had error when parsing the state field"); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 411 | return false; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 412 | } else if (appHookState < 0 || appHookState > 3) { |
| 413 | VLOG("APP_BREADCRUMB_REPORTED does not have valid state %ld", appHookState); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 414 | return false; |
David Chen | daa9f3a | 2017-12-28 16:52:22 -0800 | [diff] [blame] | 415 | } |
Jeffrey Huang | 74fc435 | 2020-03-06 15:18:33 -0800 | [diff] [blame] | 416 | } else if (event.GetTagId() == util::DAVEY_OCCURRED) { |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 417 | // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp. |
| 418 | // Check that the davey duration is reasonable. Max length check is for privacy. |
| 419 | status_t err = NO_ERROR; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 420 | |
| 421 | // Uid is the first field provided. |
| 422 | long jankUid = event.GetLong(1, &err); |
Ruchir Rastogi | d4cc693 | 2020-04-20 11:51:14 -0700 | [diff] [blame] | 423 | if (err != NO_ERROR) { |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 424 | VLOG("Davey occurred had error when parsing the uid"); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 425 | return false; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 426 | } |
| 427 | int32_t loggerUid = event.GetUid(); |
| 428 | if (loggerUid != jankUid && loggerUid != AID_STATSD) { |
| 429 | VLOG("DAVEY_OCCURRED has invalid uid: claimed %ld but caller is %d", jankUid, |
| 430 | loggerUid); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 431 | return false; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 434 | long duration = event.GetLong(event.size(), &err); |
Ruchir Rastogi | d4cc693 | 2020-04-20 11:51:14 -0700 | [diff] [blame] | 435 | if (err != NO_ERROR) { |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 436 | VLOG("Davey occurred had error when parsing the duration"); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 437 | return false; |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 438 | } else if (duration > 100000) { |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 439 | VLOG("Davey duration is unreasonably long: %ld", duration); |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 440 | return false; |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 441 | } |
Andrei Onea | da01ea5 | 2019-01-30 15:28:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | // Consume the stats log if it's interesting to this metric. |
| 448 | void MetricsManager::onLogEvent(const LogEvent& event) { |
| 449 | if (!mConfigValid) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (!checkLogCredentials(event)) { |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (!eventSanityCheck(event)) { |
| 458 | return; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 461 | int tagId = event.GetTagId(); |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 462 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
| 463 | |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 464 | bool isActive = mIsAlwaysActive; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 465 | |
| 466 | // Set of metrics that are still active after flushing. |
| 467 | unordered_set<int> activeMetricsIndices; |
| 468 | |
| 469 | // Update state of all metrics w/ activation conditions as of eventTimeNs. |
| 470 | for (int metricIndex : mMetricIndexesWithActivation) { |
| 471 | const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex]; |
| 472 | metric->flushIfExpire(eventTimeNs); |
| 473 | if (metric->isActive()) { |
| 474 | // If this metric w/ activation condition is still active after |
| 475 | // flushing, remember it. |
| 476 | activeMetricsIndices.insert(metricIndex); |
| 477 | } |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 480 | mIsActive = isActive || !activeMetricsIndices.empty(); |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 481 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 482 | if (mTagIds.find(tagId) == mTagIds.end()) { |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 483 | // Not interesting... |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 484 | return; |
| 485 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 486 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 487 | vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 488 | |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 489 | // Evaluate all atom matchers. |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 490 | for (auto& matcher : mAllAtomMatchers) { |
| 491 | matcher->onLogEvent(event, mAllAtomMatchers, matcherCache); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 494 | // Set of metrics that received an activation cancellation. |
| 495 | unordered_set<int> metricIndicesWithCanceledActivations; |
| 496 | |
| 497 | // Determine which metric activations received a cancellation and cancel them. |
| 498 | for (const auto& it : mDeactivationAtomTrackerToMetricMap) { |
| 499 | if (matcherCache[it.first] == MatchingState::kMatched) { |
| 500 | for (int metricIndex : it.second) { |
| 501 | mAllMetricProducers[metricIndex]->cancelEventActivation(it.first); |
| 502 | metricIndicesWithCanceledActivations.insert(metricIndex); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // Determine whether any metrics are no longer active after cancelling metric activations. |
| 508 | for (const int metricIndex : metricIndicesWithCanceledActivations) { |
| 509 | const sp<MetricProducer>& metric = mAllMetricProducers[metricIndex]; |
| 510 | metric->flushIfExpire(eventTimeNs); |
| 511 | if (!metric->isActive()) { |
| 512 | activeMetricsIndices.erase(metricIndex); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | isActive |= !activeMetricsIndices.empty(); |
| 517 | |
| 518 | |
| 519 | // Determine which metric activations should be turned on and turn them on |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 520 | for (const auto& it : mActivationAtomTrackerToMetricMap) { |
| 521 | if (matcherCache[it.first] == MatchingState::kMatched) { |
| 522 | for (int metricIndex : it.second) { |
| 523 | mAllMetricProducers[metricIndex]->activate(it.first, eventTimeNs); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 524 | isActive |= mAllMetricProducers[metricIndex]->isActive(); |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 529 | mIsActive = isActive; |
| 530 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 531 | // A bitmap to see which ConditionTracker needs to be re-evaluated. |
| 532 | vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false); |
| 533 | |
| 534 | for (const auto& pair : mTrackerToConditionMap) { |
| 535 | if (matcherCache[pair.first] == MatchingState::kMatched) { |
| 536 | const auto& conditionList = pair.second; |
| 537 | for (const int conditionIndex : conditionList) { |
| 538 | conditionToBeEvaluated[conditionIndex] = true; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | vector<ConditionState> conditionCache(mAllConditionTrackers.size(), |
| 544 | ConditionState::kNotEvaluated); |
| 545 | // A bitmap to track if a condition has changed value. |
| 546 | vector<bool> changedCache(mAllConditionTrackers.size(), false); |
| 547 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
| 548 | if (conditionToBeEvaluated[i] == false) { |
| 549 | continue; |
| 550 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 551 | sp<ConditionTracker>& condition = mAllConditionTrackers[i]; |
| 552 | condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache, |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 553 | changedCache); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | for (size_t i = 0; i < mAllConditionTrackers.size(); i++) { |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 557 | if (changedCache[i] == false) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 558 | continue; |
| 559 | } |
| 560 | auto pair = mConditionToMetricMap.find(i); |
| 561 | if (pair != mConditionToMetricMap.end()) { |
| 562 | auto& metricList = pair->second; |
| 563 | for (auto metricIndex : metricList) { |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 564 | // Metric cares about non sliced condition, and it's changed. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 565 | // Push the new condition to it directly. |
Yao Chen | 967b205 | 2017-11-07 16:36:43 -0800 | [diff] [blame] | 566 | if (!mAllMetricProducers[metricIndex]->isConditionSliced()) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 567 | mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i], |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 568 | eventTimeNs); |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 569 | // Metric cares about sliced conditions, and it may have changed. Send |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 570 | // notification, and the metric can query the sliced conditions that are |
| 571 | // interesting to it. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 572 | } else { |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 573 | mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(conditionCache[i], |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 574 | eventTimeNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 575 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 580 | // For matched AtomMatchers, tell relevant metrics that a matched event has come. |
| 581 | for (size_t i = 0; i < mAllAtomMatchers.size(); i++) { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 582 | if (matcherCache[i] == MatchingState::kMatched) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 583 | StatsdStats::getInstance().noteMatcherMatched(mConfigKey, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 584 | mAllAtomMatchers[i]->getId()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 585 | auto pair = mTrackerToMetricMap.find(i); |
| 586 | if (pair != mTrackerToMetricMap.end()) { |
| 587 | auto& metricList = pair->second; |
| 588 | for (const int metricIndex : metricList) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 589 | // pushed metrics are never scheduled pulls |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 590 | mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 591 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 597 | void MetricsManager::onAnomalyAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 598 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 599 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 600 | for (const auto& itr : mAllAnomalyTrackers) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 601 | itr->informAlarmsFired(timestampNs, alarmSet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 605 | void MetricsManager::onPeriodicAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 606 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 607 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet) { |
| 608 | for (const auto& itr : mAllPeriodicAlarmTrackers) { |
| 609 | itr->informAlarmsFired(timestampNs, alarmSet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 613 | // Returns the total byte size of all metrics managed by a single config source. |
| 614 | size_t MetricsManager::byteSize() { |
| 615 | size_t totalSize = 0; |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 616 | for (const auto& metricProducer : mAllMetricProducers) { |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 617 | totalSize += metricProducer->byteSize(); |
| 618 | } |
| 619 | return totalSize; |
| 620 | } |
| 621 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 622 | void MetricsManager::loadActiveConfig(const ActiveConfig& config, int64_t currentTimeNs) { |
| 623 | if (config.metric_size() == 0) { |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 624 | ALOGW("No active metric for config %s", mConfigKey.ToString().c_str()); |
| 625 | return; |
| 626 | } |
| 627 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 628 | for (int i = 0; i < config.metric_size(); i++) { |
| 629 | const auto& activeMetric = config.metric(i); |
| 630 | for (int metricIndex : mMetricIndexesWithActivation) { |
| 631 | const auto& metric = mAllMetricProducers[metricIndex]; |
| 632 | if (metric->getMetricId() == activeMetric.id()) { |
| 633 | VLOG("Setting active metric: %lld", (long long)metric->getMetricId()); |
| 634 | metric->loadActiveMetric(activeMetric, currentTimeNs); |
Tej Singh | 16ca28f | 2019-06-24 11:58:23 -0700 | [diff] [blame] | 635 | if (!mIsActive && metric->isActive()) { |
| 636 | StatsdStats::getInstance().noteActiveStatusChanged(mConfigKey, |
| 637 | /*activate=*/ true); |
| 638 | } |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 639 | mIsActive |= metric->isActive(); |
Chenjie Yu | c7939cb | 2019-02-04 17:25:45 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 645 | void MetricsManager::writeActiveConfigToProtoOutputStream( |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 646 | int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) { |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 647 | proto->write(FIELD_TYPE_INT64 | FIELD_ID_ACTIVE_CONFIG_ID, (long long)mConfigKey.GetId()); |
| 648 | proto->write(FIELD_TYPE_INT32 | FIELD_ID_ACTIVE_CONFIG_UID, mConfigKey.GetUid()); |
| 649 | for (int metricIndex : mMetricIndexesWithActivation) { |
| 650 | const auto& metric = mAllMetricProducers[metricIndex]; |
| 651 | const uint64_t metricToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 652 | FIELD_ID_ACTIVE_CONFIG_METRIC); |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 653 | metric->writeActiveMetricToProtoOutputStream(currentTimeNs, reason, proto); |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 654 | proto->end(metricToken); |
| 655 | } |
| 656 | } |
| 657 | |
Jeffrey Huang | b8f5403 | 2020-03-23 13:42:42 -0700 | [diff] [blame] | 658 | bool MetricsManager::writeMetadataToProto(int64_t currentWallClockTimeNs, |
| 659 | int64_t systemElapsedTimeNs, |
| 660 | metadata::StatsMetadata* statsMetadata) { |
| 661 | bool metadataWritten = false; |
| 662 | metadata::ConfigKey* configKey = statsMetadata->mutable_config_key(); |
| 663 | configKey->set_config_id(mConfigKey.GetId()); |
| 664 | configKey->set_uid(mConfigKey.GetUid()); |
| 665 | for (const auto& anomalyTracker : mAllAnomalyTrackers) { |
| 666 | metadata::AlertMetadata* alertMetadata = statsMetadata->add_alert_metadata(); |
| 667 | bool alertWritten = anomalyTracker->writeAlertMetadataToProto(currentWallClockTimeNs, |
| 668 | systemElapsedTimeNs, alertMetadata); |
| 669 | if (!alertWritten) { |
| 670 | statsMetadata->mutable_alert_metadata()->RemoveLast(); |
| 671 | } |
| 672 | metadataWritten |= alertWritten; |
| 673 | } |
| 674 | return metadataWritten; |
| 675 | } |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 676 | |
Jeffrey Huang | 475677e | 2020-03-30 19:52:07 -0700 | [diff] [blame] | 677 | void MetricsManager::loadMetadata(const metadata::StatsMetadata& metadata, |
| 678 | int64_t currentWallClockTimeNs, |
| 679 | int64_t systemElapsedTimeNs) { |
| 680 | for (const metadata::AlertMetadata& alertMetadata : metadata.alert_metadata()) { |
| 681 | int64_t alertId = alertMetadata.alert_id(); |
| 682 | auto it = mAlertTrackerMap.find(alertId); |
| 683 | if (it == mAlertTrackerMap.end()) { |
| 684 | ALOGE("No anomalyTracker found for alertId %lld", (long long) alertId); |
| 685 | continue; |
| 686 | } |
| 687 | mAllAnomalyTrackers[it->second]->loadAlertMetadata(alertMetadata, |
| 688 | currentWallClockTimeNs, |
| 689 | systemElapsedTimeNs); |
| 690 | } |
| 691 | } |
Muhammad Qureshi | 844694b | 2019-04-05 10:10:40 -0700 | [diff] [blame] | 692 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 693 | } // namespace statsd |
| 694 | } // namespace os |
| 695 | } // namespace android |