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 | */ |
| 16 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #pragma once |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 18 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 19 | #include "anomaly/AnomalyMonitor.h" |
| 20 | #include "anomaly/AnomalyTracker.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "condition/ConditionTracker.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 24 | #include "logd/LogEvent.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "matchers/LogMatchingTracker.h" |
| 26 | #include "metrics/MetricProducer.h" |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 27 | #include "packages/UidMap.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
| 35 | // A MetricsManager is responsible for managing metrics from one single config source. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 36 | class MetricsManager : public PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 37 | public: |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 38 | MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, const long timeBaseSec, |
| 39 | sp<UidMap> uidMap); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 40 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 41 | virtual ~MetricsManager(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | // Return whether the configuration is valid. |
| 44 | bool isConfigValid() const; |
| 45 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 46 | void onLogEvent(const LogEvent& event); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 48 | void onAnomalyAlarmFired( |
| 49 | const uint64_t timestampNs, |
| 50 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 51 | |
| 52 | void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor); |
| 53 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 54 | void notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid, |
| 55 | const int64_t version) override; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 56 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 57 | void notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, const int uid) override; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 58 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 59 | void onUidMapReceived(const uint64_t& eventTimeNs) override; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 60 | |
Yao Chen | 147ce60 | 2017-12-22 14:35:34 -0800 | [diff] [blame] | 61 | bool shouldAddUidMapListener() const { |
| 62 | return !mAllowedPkg.empty(); |
| 63 | } |
| 64 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 65 | void dumpStates(FILE* out, bool verbose); |
| 66 | |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 67 | // Returns the elapsed realtime when this metric manager last reported metrics. |
| 68 | uint64_t getLastReportTimeNs() { |
| 69 | return mLastReportTimeNs; |
| 70 | }; |
| 71 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 72 | // Config source owner can call onDumpReport() to get all the metrics collected. |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 73 | virtual void onDumpReport(android::util::ProtoOutputStream* protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 74 | virtual void onDumpReport(const uint64_t& dumpTimeStampNs, ConfigMetricsReport* report); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 75 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 76 | // Computes the total byte size of all metrics managed by a single config source. |
| 77 | // Does not change the state. |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 78 | virtual size_t byteSize(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 79 | private: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 80 | const ConfigKey mConfigKey; |
| 81 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 82 | sp<UidMap> mUidMap; |
| 83 | |
| 84 | bool mConfigValid = false; |
| 85 | |
David Chen | 1604957 | 2018-02-01 18:27:51 -0800 | [diff] [blame] | 86 | uint64_t mLastReportTimeNs; |
| 87 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 88 | // The uid log sources from StatsdConfig. |
| 89 | std::vector<int32_t> mAllowedUid; |
| 90 | |
| 91 | // The pkg log sources from StatsdConfig. |
| 92 | std::vector<std::string> mAllowedPkg; |
| 93 | |
| 94 | // The combined uid sources (after translating pkg name to uid). |
| 95 | // Logs from uids that are not in the list will be ignored to avoid spamming. |
| 96 | std::set<int32_t> mAllowedLogSources; |
| 97 | |
| 98 | // To guard access to mAllowedLogSources |
| 99 | mutable std::mutex mAllowedLogSourcesMutex; |
| 100 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 101 | // All event tags that are interesting to my metrics. |
| 102 | std::set<int> mTagIds; |
| 103 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 104 | // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 105 | // MetricsManager. There are relationships between them, and the relationships are denoted by |
| 106 | // index instead of pointers. The reasons for this are: (1) the relationship between them are |
| 107 | // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B |
| 108 | // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get |
| 109 | // the related results from a cache using the index. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 110 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 111 | // Hold all the atom matchers from the config. |
| 112 | std::vector<sp<LogMatchingTracker>> mAllAtomMatchers; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 113 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 114 | // Hold all the conditions from the config. |
| 115 | std::vector<sp<ConditionTracker>> mAllConditionTrackers; |
| 116 | |
| 117 | // Hold all metrics from the config. |
| 118 | std::vector<sp<MetricProducer>> mAllMetricProducers; |
| 119 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 120 | // Hold all alert trackers. |
| 121 | std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers; |
| 122 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 123 | // To make the log processing more efficient, we want to do as much filtering as possible |
| 124 | // before we go into individual trackers and conditions to match. |
| 125 | |
| 126 | // 1st filter: check if the event tag id is in mTagIds. |
| 127 | // 2nd filter: if it is, we parse the event because there is at least one member is interested. |
| 128 | // then pass to all LogMatchingTrackers (itself also filter events by ids). |
| 129 | // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the |
| 130 | // ConditionTrackers and MetricProducers that use this matcher. |
| 131 | // 4th filter: for ConditionTrackers that changed value due to this event, we pass |
| 132 | // new conditions to metrics that use this condition. |
| 133 | |
| 134 | // The following map is initialized from the statsd_config. |
| 135 | |
| 136 | // maps from the index of the LogMatchingTracker to index of MetricProducer. |
| 137 | std::unordered_map<int, std::vector<int>> mTrackerToMetricMap; |
| 138 | |
| 139 | // maps from LogMatchingTracker to ConditionTracker |
| 140 | std::unordered_map<int, std::vector<int>> mTrackerToConditionMap; |
| 141 | |
| 142 | // maps from ConditionTracker to MetricProducer |
| 143 | std::unordered_map<int, std::vector<int>> mConditionToMetricMap; |
| 144 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 145 | void initLogSourceWhiteList(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 146 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 147 | // The metrics that don't need to be uploaded or even reported. |
| 148 | std::set<int64_t> mNoReportMetricIds; |
| 149 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 150 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions); |
| 151 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 152 | FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 153 | FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 154 | FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricNoLink); |
| 155 | FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricWithLink); |
| 156 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricNoLink); |
| 157 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricWithLink); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace statsd |
| 161 | } // namespace os |
| 162 | } // namespace android |