blob: 68b12189aa0ebbbfc6c298980035fb07de7352c8 [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53: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 */
Yao Chenab273e22017-09-06 12:53:50 -070016
Yangster-mac20877162017-12-22 17:19:39 -080017#pragma once
18
19#include <gtest/gtest_prod.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020#include "config/ConfigListener.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070021#include "metrics/MetricsManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "packages/UidMap.h"
Chenjie Yufa22d652018-02-05 14:37:48 -080023#include "external/StatsPullerManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024
25#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chenab273e22017-09-06 12:53:50 -070026
Yao Chen44cf27c2017-09-14 22:32:50 -070027#include <stdio.h>
David Chen0656b7a2017-09-13 15:53:39 -070028#include <unordered_map>
29
Bookatz906a35c2017-09-20 15:26:44 -070030namespace android {
31namespace os {
32namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070033
Chenjie Yue36018b2018-04-16 15:18:30 -070034
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070036public:
Chenjie Yue2219202018-06-08 10:07:51 -070037 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<StatsPullerManager>& pullerManager,
38 const sp<AlarmMonitor>& anomalyAlarmMonitor,
Yangster-mac932ecec2018-02-01 10:23:52 -080039 const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070040 const int64_t timeBaseNs,
Tej Singh6ede28b2019-01-29 17:06:54 -080041 const std::function<bool(const ConfigKey&)>& sendBroadcast,
42 const std::function<bool(const int&,
43 const vector<int64_t>&)>& sendActivationBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070044 virtual ~StatsLogProcessor();
45
Yangster-macd40053e2018-01-09 16:29:22 -080046 void OnLogEvent(LogEvent* event);
Yao Chenab273e22017-09-06 12:53:50 -070047
Yangster-macc04feba2018-04-02 14:37:33 -070048 void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
49 const StatsdConfig& config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070050 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070051
Yangster7c334a12017-11-22 14:24:24 -080052 size_t GetMetricsSize(const ConfigKey& key) const;
53
Tej Singh6ede28b2019-01-29 17:06:54 -080054 void GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs);
55
Yangster-mace68f3a52018-04-04 00:01:43 -070056 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
Bookatzff71cad2018-09-20 17:17:49 -070057 const bool include_current_partial_bucket, const bool erase_data,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -070058 const DumpReportReason dumpReportReason,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000059 const DumpLatency dumpLatency,
60 vector<uint8_t>* outData);
Bookatzff71cad2018-09-20 17:17:49 -070061 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
62 const bool include_current_partial_bucket, const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000063 const DumpReportReason dumpReportReason,
64 const DumpLatency dumpLatency,
65 ProtoOutputStream* proto);
Bookatzcc5adef22017-11-21 14:36:23 -080066
Yangster-mac932ecec2018-02-01 10:23:52 -080067 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080068 void onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070069 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080070 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
71
72 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
73 void onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070074 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080075 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
yro31eb67b2017-10-24 13:33:21 -070076
yro947fbce2017-11-15 22:50:23 -080077 /* Flushes data to disk. Data on memory will be gone after written to disk. */
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000078 void WriteDataToDisk(const DumpReportReason dumpReportReason,
79 const DumpLatency dumpLatency);
Yangster-mac892f3d32018-05-02 14:16:48 -070080
Muhammad Qureshi844694b2019-04-05 10:10:40 -070081 /* Persist configs containing metrics with active activations to disk. */
82 void SaveActiveConfigsToDisk(int64_t currentTimeNs);
Chenjie Yuc7939cb2019-02-04 17:25:45 -080083
Tej Singhf53d4452019-05-09 18:17:59 -070084 /* Writes the current active status/ttl for all configs and metrics to ProtoOutputStream. */
85 void WriteActiveConfigsToProtoOutputStream(
86 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
87
Muhammad Qureshi844694b2019-04-05 10:10:40 -070088 /* Load configs containing metrics with active activations from disk. */
89 void LoadActiveConfigsFromDisk();
Chenjie Yuc7939cb2019-02-04 17:25:45 -080090
Tej Singhf53d4452019-05-09 18:17:59 -070091 /* Sets the active status/ttl for all configs and metrics to the status in ActiveConfigList. */
92 void SetConfigsActiveState(const ActiveConfigList& activeConfigList, int64_t currentTimeNs);
93
Yangster-mac892f3d32018-05-02 14:16:48 -070094 // Reset all configs.
95 void resetConfigs();
yro947fbce2017-11-15 22:50:23 -080096
Yangster-macb5bc7412018-01-06 23:17:45 -080097 inline sp<UidMap> getUidMap() {
98 return mUidMap;
99 }
100
Yao Chena80e5c02018-09-04 13:55:29 -0700101 void dumpStates(int outFd, bool verbose);
Yao Chen884c8c12018-01-26 10:36:25 -0800102
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700103 void informPullAlarmFired(const int64_t timestampNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700104
David Chend37bc232018-04-12 18:05:11 -0700105 int64_t getLastReportTimeNs(const ConfigKey& key);
106
Yao Chen876889c2018-05-02 11:16:16 -0700107 inline void setPrintLogs(bool enabled) {
108#ifdef VERY_VERBOSE_PRINTING
109 std::lock_guard<std::mutex> lock(mMetricsMutex);
110 mPrintAllLogs = enabled;
111#endif
112 }
113
David Chen48944902018-05-03 10:29:11 -0700114 // Add a specific config key to the possible configs to dump ASAP.
115 void noteOnDiskData(const ConfigKey& key);
116
Yao Chenab273e22017-09-06 12:53:50 -0700117private:
Yangster-macbe10ddf2018-03-13 15:39:51 -0700118 // For testing only.
119 inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
120 return mAnomalyAlarmMonitor;
121 }
122
Yangster-mac684d1952018-03-24 16:47:16 -0700123 inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
124 return mPeriodicAlarmMonitor;
125 }
126
Yangster-macb0d06282018-01-05 15:44:07 -0800127 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -0800128
Yao Chend10f7b12017-12-18 12:53:50 -0800129 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -0700130
David Chen1d7b0cd2017-11-15 14:20:04 -0800131 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -0700132
Tej Singh6ede28b2019-01-29 17:06:54 -0800133 // Last time we sent a broadcast to this uid that the active configs had changed.
134 std::unordered_map<int, long> mLastActivationBroadcastTimes;
135
David Chend9269e22017-12-05 13:43:51 -0800136 // Tracks when we last checked the bytes consumed for each config key.
137 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
138
David Chen48944902018-05-03 10:29:11 -0700139 // Tracks which config keys has metric reports on disk
140 std::set<ConfigKey> mOnDiskDataConfigs;
141
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700142 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -0700143
Chenjie Yue2219202018-06-08 10:07:51 -0700144 sp<StatsPullerManager> mPullerManager; // Reference to StatsPullerManager
Chenjie Yufa22d652018-02-05 14:37:48 -0800145
Yangster-mac932ecec2018-02-01 10:23:52 -0800146 sp<AlarmMonitor> mAnomalyAlarmMonitor;
147
148 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800149
Tej Singh480392f2019-10-23 15:53:46 -0700150 void OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs);
151
Yangster-macb142cc82018-03-30 15:22:08 -0700152 void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
153
154 void OnConfigUpdatedLocked(
155 const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
156
Tej Singh6ede28b2019-01-29 17:06:54 -0800157 void GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs);
158
Tej Singhf53d4452019-05-09 18:17:59 -0700159 void WriteActiveConfigsToProtoOutputStreamLocked(
160 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
161
162 void SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
163 int64_t currentTimeNs);
164
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000165 void WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
166 const DumpLatency dumpLatency);
Yangster-mac892f3d32018-05-02 14:16:48 -0700167 void WriteDataToDiskLocked(const ConfigKey& key, const int64_t timestampNs,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000168 const DumpReportReason dumpReportReason,
169 const DumpLatency dumpLatency);
Yangster-mace68f3a52018-04-04 00:01:43 -0700170
Yao Chen9a43b4f2019-04-10 10:43:20 -0700171 void onConfigMetricsReportLocked(
172 const ConfigKey& key, const int64_t dumpTimeStampNs,
173 const bool include_current_partial_bucket, const bool erase_data,
174 const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
175 /*if dataSavedToDisk is true, it indicates the caller will write the data to disk
176 (e.g., before reboot). So no need to further persist local history.*/
177 const bool dataSavedToDisk, vector<uint8_t>* proto);
Yangster-mac86179502018-01-23 15:47:15 -0800178
David Chen1d7b0cd2017-11-15 14:20:04 -0800179 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
180 * actually delete the data. */
Tej Singh480392f2019-10-23 15:53:46 -0700181 void flushIfNecessaryLocked(const ConfigKey& key, MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -0700182
Yangster-macd40053e2018-01-09 16:29:22 -0800183 // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
184 void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
185
186 // Handler over the isolated uid change event.
187 void onIsolatedUidChangedEventLocked(const LogEvent& event);
188
Yangster-mac892f3d32018-05-02 14:16:48 -0700189 // Reset all configs.
190 void resetConfigsLocked(const int64_t timestampNs);
191 // Reset the specified configs.
Yao Chen163d2602018-04-10 10:39:53 -0700192 void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);
193
David Chen1d7b0cd2017-11-15 14:20:04 -0800194 // Function used to send a broadcast so that receiver for the config key can call getData
195 // to retrieve the stored data.
David Chen48944902018-05-03 10:29:11 -0700196 std::function<bool(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -0700197
Tej Singh6ede28b2019-01-29 17:06:54 -0800198 // Function used to send a broadcast so that receiver can be notified of which configs
199 // are currently active.
200 std::function<bool(const int& uid, const vector<int64_t>& configIds)> mSendActivationBroadcast;
201
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700202 const int64_t mTimeBaseNs;
Chenjie Yu85ed8382017-12-14 16:48:54 -0800203
Yao Chen163d2602018-04-10 10:39:53 -0700204 // Largest timestamp of the events that we have processed.
205 int64_t mLargestTimestampSeen = 0;
206
207 int64_t mLastTimestampSeen = 0;
208
Chenjie Yufa22d652018-02-05 14:37:48 -0800209 long mLastPullerCacheClearTimeSec = 0;
210
Tej Singh42f9e062018-11-09 10:01:00 -0800211 // Last time we wrote data to disk.
212 int64_t mLastWriteTimeNs = 0;
213
jianjin996f40d2019-03-29 13:53:48 -0700214 // Last time we wrote active metrics to disk.
215 int64_t mLastActiveMetricsWriteNs = 0;
216
Yao Chen876889c2018-05-02 11:16:16 -0700217#ifdef VERY_VERBOSE_PRINTING
218 bool mPrintAllLogs = false;
219#endif
220
Yao Chen163d2602018-04-10 10:39:53 -0700221 FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
David Chend9269e22017-12-05 13:43:51 -0800222 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
223 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
224 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800225 FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
Chenjie Yua9a310e2019-02-06 13:40:10 -0800226 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700227 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
Muhammad Qureshi15f8da92019-04-05 10:10:40 -0700228 FRIEND_TEST(StatsLogProcessorTest,
229 TestActivationOnBootMultipleActivationsDifferentActivationTypes);
Tej Singhf53d4452019-05-09 18:17:59 -0700230 FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800231
Yao Chen8a8d16c2018-02-08 14:50:40 -0800232 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
233 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
234 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
235 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
236 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
237 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
238 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
239 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
Yangster-mace06cfd72018-03-10 23:22:59 -0800240 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
241 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
Yangster-mac87718e22018-01-11 16:16:26 -0800242 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700243 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
244 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
Tej Singh597c7162019-04-17 16:41:45 -0700245 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation);
246 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition);
Yangster-mac32f07af2018-10-13 17:08:11 -0700247 FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700248 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
249 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
Tej Singh597c7162019-04-17 16:41:45 -0700250 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700251
Yangster-macbe10ddf2018-03-13 15:39:51 -0700252 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
253 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
254 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
255 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
256 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
Yangster-macb142cc82018-03-30 15:22:08 -0700257
Yangster-mac684d1952018-03-24 16:47:16 -0700258 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
Yangster-macb142cc82018-03-30 15:22:08 -0700259 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700260 FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700261 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
262 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
Tej Singhee4495e2019-06-03 18:37:35 -0700263 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700264 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700265
tsaichristined21aacf2019-10-07 14:47:38 -0700266 FRIEND_TEST(CountMetricE2eTest, TestWithSimpleState);
267 FRIEND_TEST(CountMetricE2eTest, TestWithMappedState);
268 FRIEND_TEST(CountMetricE2eTest, TestWithMultipleStates);
269
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700270 FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
271 FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
272 FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
273 FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
274 FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
275 FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
Yao Chenab273e22017-09-06 12:53:50 -0700276};
Bookatz906a35c2017-09-20 15:26:44 -0700277
Yao Chenef99c4f2017-09-22 16:26:54 -0700278} // namespace statsd
279} // namespace os
280} // namespace android