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