blob: ea9c6e70401711592b1df7598f8aa68e26c547c5 [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// Keep this in sync with DumpReportReason enum in stats_log.proto
35enum DumpReportReason {
36 DEVICE_SHUTDOWN = 1,
37 CONFIG_UPDATED = 2,
38 CONFIG_REMOVED = 3,
39 GET_DATA_CALLED = 4,
40 ADB_DUMP = 5,
41 CONFIG_RESET = 6,
Yangster-mac97e7d202018-10-09 11:05:39 -070042 STATSCOMPANION_DIED = 7,
43 TERMINATION_SIGNAL_RECEIVED = 8
Chenjie Yue36018b2018-04-16 15:18:30 -070044};
45
Joe Onorato9fc9edf2017-10-15 20:08:52 -070046class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070047public:
Chenjie Yue2219202018-06-08 10:07:51 -070048 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<StatsPullerManager>& pullerManager,
49 const sp<AlarmMonitor>& anomalyAlarmMonitor,
Yangster-mac932ecec2018-02-01 10:23:52 -080050 const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070051 const int64_t timeBaseNs,
Tej Singh6ede28b2019-01-29 17:06:54 -080052 const std::function<bool(const ConfigKey&)>& sendBroadcast,
53 const std::function<bool(const int&,
54 const vector<int64_t>&)>& sendActivationBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070055 virtual ~StatsLogProcessor();
56
Yangster-macd40053e2018-01-09 16:29:22 -080057 void OnLogEvent(LogEvent* event);
Yao Chenab273e22017-09-06 12:53:50 -070058
Yangster-macc04feba2018-04-02 14:37:33 -070059 void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
60 const StatsdConfig& config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070062
Yangster7c334a12017-11-22 14:24:24 -080063 size_t GetMetricsSize(const ConfigKey& key) const;
64
Tej Singh6ede28b2019-01-29 17:06:54 -080065 void GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs);
66
Yangster-mace68f3a52018-04-04 00:01:43 -070067 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
Bookatzff71cad2018-09-20 17:17:49 -070068 const bool include_current_partial_bucket, const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -070069 const DumpReportReason dumpReportReason, vector<uint8_t>* outData);
Bookatzff71cad2018-09-20 17:17:49 -070070 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
71 const bool include_current_partial_bucket, const bool erase_data,
72 const DumpReportReason dumpReportReason, ProtoOutputStream* proto);
Bookatzcc5adef22017-11-21 14:36:23 -080073
Yangster-mac932ecec2018-02-01 10:23:52 -080074 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080075 void onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070076 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080077 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
78
79 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
80 void onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070081 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080082 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
yro31eb67b2017-10-24 13:33:21 -070083
yro947fbce2017-11-15 22:50:23 -080084 /* Flushes data to disk. Data on memory will be gone after written to disk. */
Yangster-mac892f3d32018-05-02 14:16:48 -070085 void WriteDataToDisk(const DumpReportReason dumpReportReason);
86
Chenjie Yuc7939cb2019-02-04 17:25:45 -080087 /* Persist metric activation status onto disk. */
88 void WriteMetricsActivationToDisk(int64_t currentTimeNs);
89
90 /* Load metric activation status from disk. */
91 void LoadMetricsActivationFromDisk();
92
Yangster-mac892f3d32018-05-02 14:16:48 -070093 // Reset all configs.
94 void resetConfigs();
yro947fbce2017-11-15 22:50:23 -080095
Yangster-macb5bc7412018-01-06 23:17:45 -080096 inline sp<UidMap> getUidMap() {
97 return mUidMap;
98 }
99
Yao Chena80e5c02018-09-04 13:55:29 -0700100 void dumpStates(int outFd, bool verbose);
Yao Chen884c8c12018-01-26 10:36:25 -0800101
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700102 void informPullAlarmFired(const int64_t timestampNs);
Yangster-macb142cc82018-03-30 15:22:08 -0700103
David Chend37bc232018-04-12 18:05:11 -0700104 int64_t getLastReportTimeNs(const ConfigKey& key);
105
Yao Chen876889c2018-05-02 11:16:16 -0700106 inline void setPrintLogs(bool enabled) {
107#ifdef VERY_VERBOSE_PRINTING
108 std::lock_guard<std::mutex> lock(mMetricsMutex);
109 mPrintAllLogs = enabled;
110#endif
111 }
112
David Chen48944902018-05-03 10:29:11 -0700113 // Add a specific config key to the possible configs to dump ASAP.
114 void noteOnDiskData(const ConfigKey& key);
115
Yao Chenab273e22017-09-06 12:53:50 -0700116private:
Yangster-macbe10ddf2018-03-13 15:39:51 -0700117 // For testing only.
118 inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
119 return mAnomalyAlarmMonitor;
120 }
121
Yangster-mac684d1952018-03-24 16:47:16 -0700122 inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
123 return mPeriodicAlarmMonitor;
124 }
125
Yangster-macb0d06282018-01-05 15:44:07 -0800126 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -0800127
Yao Chend10f7b12017-12-18 12:53:50 -0800128 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -0700129
David Chen1d7b0cd2017-11-15 14:20:04 -0800130 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -0700131
Tej Singh6ede28b2019-01-29 17:06:54 -0800132 // Last time we sent a broadcast to this uid that the active configs had changed.
133 std::unordered_map<int, long> mLastActivationBroadcastTimes;
134
David Chend9269e22017-12-05 13:43:51 -0800135 // Tracks when we last checked the bytes consumed for each config key.
136 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
137
David Chen48944902018-05-03 10:29:11 -0700138 // Tracks which config keys has metric reports on disk
139 std::set<ConfigKey> mOnDiskDataConfigs;
140
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700141 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -0700142
Chenjie Yue2219202018-06-08 10:07:51 -0700143 sp<StatsPullerManager> mPullerManager; // Reference to StatsPullerManager
Chenjie Yufa22d652018-02-05 14:37:48 -0800144
Yangster-mac932ecec2018-02-01 10:23:52 -0800145 sp<AlarmMonitor> mAnomalyAlarmMonitor;
146
147 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800148
Yangster-macb142cc82018-03-30 15:22:08 -0700149 void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
150
151 void OnConfigUpdatedLocked(
152 const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
153
Tej Singh6ede28b2019-01-29 17:06:54 -0800154 void GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs);
155
Yangster-mac892f3d32018-05-02 14:16:48 -0700156 void WriteDataToDiskLocked(const DumpReportReason dumpReportReason);
157 void WriteDataToDiskLocked(const ConfigKey& key, const int64_t timestampNs,
158 const DumpReportReason dumpReportReason);
Yangster-mace68f3a52018-04-04 00:01:43 -0700159
Yangster-macb142cc82018-03-30 15:22:08 -0700160 void onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700161 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700162 const bool erase_data,
Chenjie Yue36018b2018-04-16 15:18:30 -0700163 const DumpReportReason dumpReportReason,
yro4beccbe2018-03-15 19:42:05 -0700164 util::ProtoOutputStream* proto);
Yangster-mac86179502018-01-23 15:47:15 -0800165
David Chen1d7b0cd2017-11-15 14:20:04 -0800166 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
167 * actually delete the data. */
Yangster-macb142cc82018-03-30 15:22:08 -0700168 void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key,
Yangster-macb0d06282018-01-05 15:44:07 -0800169 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -0700170
Yangster-macd40053e2018-01-09 16:29:22 -0800171 // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
172 void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
173
174 // Handler over the isolated uid change event.
175 void onIsolatedUidChangedEventLocked(const LogEvent& event);
176
Yangster-mac892f3d32018-05-02 14:16:48 -0700177 // Reset all configs.
178 void resetConfigsLocked(const int64_t timestampNs);
179 // Reset the specified configs.
Yao Chen163d2602018-04-10 10:39:53 -0700180 void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);
181
David Chen1d7b0cd2017-11-15 14:20:04 -0800182 // Function used to send a broadcast so that receiver for the config key can call getData
183 // to retrieve the stored data.
David Chen48944902018-05-03 10:29:11 -0700184 std::function<bool(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -0700185
Tej Singh6ede28b2019-01-29 17:06:54 -0800186 // Function used to send a broadcast so that receiver can be notified of which configs
187 // are currently active.
188 std::function<bool(const int& uid, const vector<int64_t>& configIds)> mSendActivationBroadcast;
189
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700190 const int64_t mTimeBaseNs;
Chenjie Yu85ed8382017-12-14 16:48:54 -0800191
Yao Chen163d2602018-04-10 10:39:53 -0700192 // Largest timestamp of the events that we have processed.
193 int64_t mLargestTimestampSeen = 0;
194
195 int64_t mLastTimestampSeen = 0;
196
Chenjie Yufa22d652018-02-05 14:37:48 -0800197 long mLastPullerCacheClearTimeSec = 0;
198
Tej Singh42f9e062018-11-09 10:01:00 -0800199 // Last time we wrote data to disk.
200 int64_t mLastWriteTimeNs = 0;
201
Yao Chen876889c2018-05-02 11:16:16 -0700202#ifdef VERY_VERBOSE_PRINTING
203 bool mPrintAllLogs = false;
204#endif
205
Yao Chen163d2602018-04-10 10:39:53 -0700206 FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
David Chend9269e22017-12-05 13:43:51 -0800207 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
208 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
209 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800210 FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
Chenjie Yua9a310e2019-02-06 13:40:10 -0800211 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800212
Yao Chen8a8d16c2018-02-08 14:50:40 -0800213 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
214 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
215 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
216 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
217 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
218 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
219 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
220 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
Yangster-mace06cfd72018-03-10 23:22:59 -0800221 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
222 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
Yangster-mac87718e22018-01-11 16:16:26 -0800223 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700224 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
225 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
Yangster-mac32f07af2018-10-13 17:08:11 -0700226 FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700227 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
228 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
229
Yangster13fb7e42018-03-07 17:30:49 -0800230 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
231 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
232 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
233 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
234
235 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
236 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
237 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
238
Yangster13fb7e42018-03-07 17:30:49 -0800239 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
240 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
241 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
242
Yangster-macbe10ddf2018-03-13 15:39:51 -0700243 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
244 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
245 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
246 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
247 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
Yangster-macb142cc82018-03-30 15:22:08 -0700248
Yangster-mac684d1952018-03-24 16:47:16 -0700249 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
Yangster-macb142cc82018-03-30 15:22:08 -0700250 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700251 FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
Yao Chenab273e22017-09-06 12:53:50 -0700252};
Bookatz906a35c2017-09-20 15:26:44 -0700253
Yao Chenef99c4f2017-09-22 16:26:54 -0700254} // namespace statsd
255} // namespace os
256} // namespace android