MetricsManager.h revision 028091cb15f5e8290eed77a222582162d19a3d87
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 */ 16 17#pragma once 18 19#include "anomaly/AlarmMonitor.h" 20#include "anomaly/AlarmTracker.h" 21#include "anomaly/AnomalyTracker.h" 22#include "condition/ConditionTracker.h" 23#include "config/ConfigKey.h" 24#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" 25#include "logd/LogEvent.h" 26#include "matchers/LogMatchingTracker.h" 27#include "metrics/MetricProducer.h" 28#include "packages/UidMap.h" 29 30#include <unordered_map> 31 32namespace android { 33namespace os { 34namespace statsd { 35 36// A MetricsManager is responsible for managing metrics from one single config source. 37class MetricsManager : public PackageInfoListener { 38public: 39 MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, 40 const int64_t timeBaseNs, const int64_t currentTimeNs, 41 const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor, 42 const sp<AlarmMonitor>& periodicAlarmMonitor); 43 44 virtual ~MetricsManager(); 45 46 // Return whether the configuration is valid. 47 bool isConfigValid() const; 48 49 void onLogEvent(const LogEvent& event); 50 51 void onAnomalyAlarmFired( 52 const int64_t& timestampNs, 53 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet); 54 55 void onPeriodicAlarmFired( 56 const int64_t& timestampNs, 57 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet); 58 59 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid, 60 const int64_t version) override; 61 62 void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override; 63 64 void onUidMapReceived(const int64_t& eventTimeNs) override; 65 66 bool shouldAddUidMapListener() const { 67 return !mAllowedPkg.empty(); 68 } 69 70 bool shouldWriteToDisk() const { 71 return mNoReportMetricIds.size() != mAllMetricProducers.size(); 72 } 73 74 void dumpStates(FILE* out, bool verbose); 75 76 inline bool isInTtl(const int64_t timestampNs) const { 77 return mTtlNs <= 0 || timestampNs < mTtlEndNs; 78 }; 79 80 void refreshTtl(const int64_t currentTimestampNs) { 81 if (mTtlNs > 0) { 82 mTtlEndNs = currentTimestampNs + mTtlNs; 83 } 84 }; 85 86 // Returns the elapsed realtime when this metric manager last reported metrics. If this config 87 // has not yet dumped any reports, this is the time the metricsmanager was initialized. 88 inline int64_t getLastReportTimeNs() const { 89 return mLastReportTimeNs; 90 }; 91 92 inline int64_t getLastReportWallClockNs() const { 93 return mLastReportWallClockNs; 94 }; 95 96 virtual void dropData(const int64_t dropTimeNs); 97 98 virtual void onDumpReport(const int64_t dumpTimeNs, 99 const bool include_current_partial_bucket, 100 std::set<string> *str_set, 101 android::util::ProtoOutputStream* protoOutput); 102 103 // Computes the total byte size of all metrics managed by a single config source. 104 // Does not change the state. 105 virtual size_t byteSize(); 106 107private: 108 // For test only. 109 inline int64_t getTtlEndNs() const { return mTtlEndNs; } 110 111 const ConfigKey mConfigKey; 112 113 sp<UidMap> mUidMap; 114 115 bool mConfigValid = false; 116 117 const int64_t mTtlNs; 118 int64_t mTtlEndNs; 119 120 int64_t mLastReportTimeNs; 121 int64_t mLastReportWallClockNs; 122 123 // The uid log sources from StatsdConfig. 124 std::vector<int32_t> mAllowedUid; 125 126 // The pkg log sources from StatsdConfig. 127 std::vector<std::string> mAllowedPkg; 128 129 // The combined uid sources (after translating pkg name to uid). 130 // Logs from uids that are not in the list will be ignored to avoid spamming. 131 std::set<int32_t> mAllowedLogSources; 132 133 // Contains the annotations passed in with StatsdConfig. 134 std::list<std::pair<const int64_t, const int32_t>> mAnnotations; 135 136 // To guard access to mAllowedLogSources 137 mutable std::mutex mAllowedLogSourcesMutex; 138 139 // All event tags that are interesting to my metrics. 140 std::set<int> mTagIds; 141 142 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in 143 // MetricsManager. There are relationships between them, and the relationships are denoted by 144 // index instead of pointers. The reasons for this are: (1) the relationship between them are 145 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B 146 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get 147 // the related results from a cache using the index. 148 149 // Hold all the atom matchers from the config. 150 std::vector<sp<LogMatchingTracker>> mAllAtomMatchers; 151 152 // Hold all the conditions from the config. 153 std::vector<sp<ConditionTracker>> mAllConditionTrackers; 154 155 // Hold all metrics from the config. 156 std::vector<sp<MetricProducer>> mAllMetricProducers; 157 158 // Hold all alert trackers. 159 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers; 160 161 // Hold all periodic alarm trackers. 162 std::vector<sp<AlarmTracker>> mAllPeriodicAlarmTrackers; 163 164 // To make the log processing more efficient, we want to do as much filtering as possible 165 // before we go into individual trackers and conditions to match. 166 167 // 1st filter: check if the event tag id is in mTagIds. 168 // 2nd filter: if it is, we parse the event because there is at least one member is interested. 169 // then pass to all LogMatchingTrackers (itself also filter events by ids). 170 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the 171 // ConditionTrackers and MetricProducers that use this matcher. 172 // 4th filter: for ConditionTrackers that changed value due to this event, we pass 173 // new conditions to metrics that use this condition. 174 175 // The following map is initialized from the statsd_config. 176 177 // maps from the index of the LogMatchingTracker to index of MetricProducer. 178 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap; 179 180 // maps from LogMatchingTracker to ConditionTracker 181 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap; 182 183 // maps from ConditionTracker to MetricProducer 184 std::unordered_map<int, std::vector<int>> mConditionToMetricMap; 185 186 void initLogSourceWhiteList(); 187 188 // The metrics that don't need to be uploaded or even reported. 189 std::set<int64_t> mNoReportMetricIds; 190 191 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions); 192 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks); 193 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid); 194 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain); 195 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent); 196 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); 197 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); 198 FRIEND_TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents); 199 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); 200 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); 201 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition); 202 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition); 203 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition); 204 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition); 205 206 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition); 207 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition); 208 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition); 209 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition); 210 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition); 211 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition); 212 213 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket); 214 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets); 215 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket); 216 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets); 217 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period); 218 219 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms); 220 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric); 221}; 222 223} // namespace statsd 224} // namespace os 225} // namespace android 226