blob: 170d6a7eacae351c1ea5a2430bcdd23257a26a53 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -07001/*
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 Onorato9fc9edf2017-10-15 20:08:52 -070017#pragma once
Yao Chen44cf27c2017-09-14 22:32:50 -070018
Yangster-mac932ecec2018-02-01 10:23:52 -080019#include "anomaly/AlarmMonitor.h"
20#include "anomaly/AlarmTracker.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080021#include "anomaly/AnomalyTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "condition/ConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070025#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include "matchers/LogMatchingTracker.h"
27#include "metrics/MetricProducer.h"
Yao Chend10f7b12017-12-18 12:53:50 -080028#include "packages/UidMap.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070029
Yao Chen44cf27c2017-09-14 22:32:50 -070030#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
36// A MetricsManager is responsible for managing metrics from one single config source.
Yao Chend10f7b12017-12-18 12:53:50 -080037class MetricsManager : public PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070038public:
Yangster-macc04feba2018-04-02 14:37:33 -070039 MetricsManager(const ConfigKey& configKey, const StatsdConfig& config,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070040 const int64_t timeBaseNs, const int64_t currentTimeNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080041 const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor,
42 const sp<AlarmMonitor>& periodicAlarmMonitor);
Yao Chen44cf27c2017-09-14 22:32:50 -070043
David Chend9269e22017-12-05 13:43:51 -080044 virtual ~MetricsManager();
Yao Chen44cf27c2017-09-14 22:32:50 -070045
Yao Chencaf339d2017-10-06 16:01:10 -070046 // Return whether the configuration is valid.
47 bool isConfigValid() const;
48
Joe Onoratoc4dfae52017-10-17 23:38:21 -070049 void onLogEvent(const LogEvent& event);
Yao Chen44cf27c2017-09-14 22:32:50 -070050
Yangster-mac20877162017-12-22 17:19:39 -080051 void onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070052 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080053 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080054
Yangster-mac932ecec2018-02-01 10:23:52 -080055 void onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070056 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080057 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080058
Yangster-macb142cc82018-03-30 15:22:08 -070059 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080060 const int64_t version) override;
Yao Chend10f7b12017-12-18 12:53:50 -080061
Yangster-macb142cc82018-03-30 15:22:08 -070062 void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override;
Yao Chend10f7b12017-12-18 12:53:50 -080063
Yangster-macb142cc82018-03-30 15:22:08 -070064 void onUidMapReceived(const int64_t& eventTimeNs) override;
Yao Chend10f7b12017-12-18 12:53:50 -080065
Yao Chen147ce602017-12-22 14:35:34 -080066 bool shouldAddUidMapListener() const {
67 return !mAllowedPkg.empty();
68 }
69
Yao Chen884c8c12018-01-26 10:36:25 -080070 void dumpStates(FILE* out, bool verbose);
71
Yangster-macb142cc82018-03-30 15:22:08 -070072 inline bool isInTtl(const int64_t timestampNs) const {
73 return mTtlNs <= 0 || timestampNs < mTtlEndNs;
74 };
75
76 void refreshTtl(const int64_t currentTimestampNs) {
77 if (mTtlNs > 0) {
78 mTtlEndNs = currentTimestampNs + mTtlNs;
79 }
80 };
81
David Chen16049572018-02-01 18:27:51 -080082 // Returns the elapsed realtime when this metric manager last reported metrics.
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080083 inline int64_t getLastReportTimeNs() const {
David Chen16049572018-02-01 18:27:51 -080084 return mLastReportTimeNs;
85 };
86
Yangster-mac3fa5d7f2018-03-10 21:50:27 -080087 inline int64_t getLastReportWallClockNs() const {
88 return mLastReportWallClockNs;
89 };
90
Yangster-macb142cc82018-03-30 15:22:08 -070091 virtual void dropData(const int64_t dropTimeNs);
Yao Chen06dba5d2018-01-26 13:38:16 -080092
Yangster-macb142cc82018-03-30 15:22:08 -070093 virtual void onDumpReport(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -070094 const bool include_current_partial_bucket,
Yangster-mac9def8e32018-04-17 13:55:51 -070095 std::set<string> *str_set,
Yao Chen8a8d16c2018-02-08 14:50:40 -080096 android::util::ProtoOutputStream* protoOutput);
Yao Chen729093d2017-10-16 10:33:26 -070097
David Chen1d7b0cd2017-11-15 14:20:04 -080098 // Computes the total byte size of all metrics managed by a single config source.
99 // Does not change the state.
David Chend9269e22017-12-05 13:43:51 -0800100 virtual size_t byteSize();
Yangster-macb142cc82018-03-30 15:22:08 -0700101
Yao Chen44cf27c2017-09-14 22:32:50 -0700102private:
Yangster-macb142cc82018-03-30 15:22:08 -0700103 // For test only.
104 inline int64_t getTtlEndNs() const { return mTtlEndNs; }
105
Yao Chenb3561512017-11-21 18:07:17 -0800106 const ConfigKey mConfigKey;
107
Yao Chend10f7b12017-12-18 12:53:50 -0800108 sp<UidMap> mUidMap;
109
110 bool mConfigValid = false;
111
Yangster-macb142cc82018-03-30 15:22:08 -0700112 const int64_t mTtlNs;
113 int64_t mTtlEndNs;
114
Yangster-mac3fa5d7f2018-03-10 21:50:27 -0800115 int64_t mLastReportTimeNs;
116 int64_t mLastReportWallClockNs;
David Chen16049572018-02-01 18:27:51 -0800117
Yao Chend10f7b12017-12-18 12:53:50 -0800118 // The uid log sources from StatsdConfig.
119 std::vector<int32_t> mAllowedUid;
120
121 // The pkg log sources from StatsdConfig.
122 std::vector<std::string> mAllowedPkg;
123
124 // The combined uid sources (after translating pkg name to uid).
125 // Logs from uids that are not in the list will be ignored to avoid spamming.
126 std::set<int32_t> mAllowedLogSources;
127
David Chenfaa1af52018-03-30 15:14:04 -0700128 // Contains the annotations passed in with StatsdConfig.
129 std::list<std::pair<const int64_t, const int32_t>> mAnnotations;
130
Yao Chend10f7b12017-12-18 12:53:50 -0800131 // To guard access to mAllowedLogSources
132 mutable std::mutex mAllowedLogSourcesMutex;
133
Yao Chen44cf27c2017-09-14 22:32:50 -0700134 // All event tags that are interesting to my metrics.
135 std::set<int> mTagIds;
136
Yao Chencaf339d2017-10-06 16:01:10 -0700137 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
Bookatzd3606c72017-10-19 10:13:49 -0700138 // MetricsManager. There are relationships between them, and the relationships are denoted by
139 // index instead of pointers. The reasons for this are: (1) the relationship between them are
140 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B
141 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get
142 // the related results from a cache using the index.
Yao Chen44cf27c2017-09-14 22:32:50 -0700143
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800144 // Hold all the atom matchers from the config.
145 std::vector<sp<LogMatchingTracker>> mAllAtomMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -0700146
Yao Chencaf339d2017-10-06 16:01:10 -0700147 // Hold all the conditions from the config.
148 std::vector<sp<ConditionTracker>> mAllConditionTrackers;
149
150 // Hold all metrics from the config.
151 std::vector<sp<MetricProducer>> mAllMetricProducers;
152
Yangster-mace2cd6d52017-11-09 20:38:30 -0800153 // Hold all alert trackers.
154 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
155
Yangster-mac932ecec2018-02-01 10:23:52 -0800156 // Hold all periodic alarm trackers.
157 std::vector<sp<AlarmTracker>> mAllPeriodicAlarmTrackers;
158
Yao Chencaf339d2017-10-06 16:01:10 -0700159 // To make the log processing more efficient, we want to do as much filtering as possible
160 // before we go into individual trackers and conditions to match.
161
162 // 1st filter: check if the event tag id is in mTagIds.
163 // 2nd filter: if it is, we parse the event because there is at least one member is interested.
164 // then pass to all LogMatchingTrackers (itself also filter events by ids).
165 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
166 // ConditionTrackers and MetricProducers that use this matcher.
167 // 4th filter: for ConditionTrackers that changed value due to this event, we pass
168 // new conditions to metrics that use this condition.
169
170 // The following map is initialized from the statsd_config.
171
172 // maps from the index of the LogMatchingTracker to index of MetricProducer.
173 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
174
175 // maps from LogMatchingTracker to ConditionTracker
176 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
177
178 // maps from ConditionTracker to MetricProducer
179 std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
180
Yao Chend10f7b12017-12-18 12:53:50 -0800181 void initLogSourceWhiteList();
Yangster-mac20877162017-12-22 17:19:39 -0800182
Yangster-mac94e197c2018-01-02 16:03:03 -0800183 // The metrics that don't need to be uploaded or even reported.
184 std::set<int64_t> mNoReportMetricIds;
185
Yangster-mac20877162017-12-22 17:19:39 -0800186 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
187 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
Yangster-mace06cfd72018-03-10 23:22:59 -0800188 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
189 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
Yangster-mac87718e22018-01-11 16:16:26 -0800190 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700191 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
192 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
193 FRIEND_TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents);
194 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
195 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
Yangster13fb7e42018-03-07 17:30:49 -0800196 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
197 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
198 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
199 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
200
201 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
202 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
203 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
204 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
205 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
206 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
207
Yangster-macbe10ddf2018-03-13 15:39:51 -0700208 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
209 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
210 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
211 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
212 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
213
Yangster-mac684d1952018-03-24 16:47:16 -0700214 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
Yangster-macb142cc82018-03-30 15:22:08 -0700215 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
Yao Chen44cf27c2017-09-14 22:32:50 -0700216};
217
218} // namespace statsd
219} // namespace os
220} // namespace android