blob: 17fa5cdb1fea5922383a19577063f50713a3f8ec [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"
21#include "logd/LogReader.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070022#include "metrics/MetricsManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "packages/UidMap.h"
Chenjie Yufa22d652018-02-05 14:37:48 -080024#include "external/StatsPullerManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025
26#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chenab273e22017-09-06 12:53:50 -070027
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include <stdio.h>
David Chen0656b7a2017-09-13 15:53:39 -070029#include <unordered_map>
30
Bookatz906a35c2017-09-20 15:26:44 -070031namespace android {
32namespace os {
33namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070034
Chenjie Yue36018b2018-04-16 15:18:30 -070035// Keep this in sync with DumpReportReason enum in stats_log.proto
36enum DumpReportReason {
37 DEVICE_SHUTDOWN = 1,
38 CONFIG_UPDATED = 2,
39 CONFIG_REMOVED = 3,
40 GET_DATA_CALLED = 4,
41 ADB_DUMP = 5,
42 CONFIG_RESET = 6,
43 STATSCOMPANION_DIED = 7
44};
45
Joe Onorato9fc9edf2017-10-15 20:08:52 -070046class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070047public:
Yangster-mac932ecec2018-02-01 10:23:52 -080048 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor,
49 const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070050 const int64_t timeBaseNs,
David Chen1d7b0cd2017-11-15 14:20:04 -080051 const std::function<void(const ConfigKey&)>& sendBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070052 virtual ~StatsLogProcessor();
53
Yao Chen163d2602018-04-10 10:39:53 -070054 void OnLogEvent(LogEvent* event, bool reconnectionStarts);
55
56 // for testing only.
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
Yangster-mace68f3a52018-04-04 00:01:43 -070065 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
Yangster-mac9def8e32018-04-17 13:55:51 -070066 const bool include_current_partial_bucket, const bool include_string,
Chenjie Yue36018b2018-04-16 15:18:30 -070067 const DumpReportReason dumpReportReason, vector<uint8_t>* outData);
Bookatzcc5adef22017-11-21 14:36:23 -080068
Yangster-mac932ecec2018-02-01 10:23:52 -080069 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080070 void onAnomalyAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070071 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080072 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
73
74 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
75 void onPeriodicAlarmFired(
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);
yro31eb67b2017-10-24 13:33:21 -070078
yro947fbce2017-11-15 22:50:23 -080079 /* Flushes data to disk. Data on memory will be gone after written to disk. */
Chenjie Yue36018b2018-04-16 15:18:30 -070080 void WriteDataToDisk(bool shutdown);
yro947fbce2017-11-15 22:50:23 -080081
Yangster-macb5bc7412018-01-06 23:17:45 -080082 inline sp<UidMap> getUidMap() {
83 return mUidMap;
84 }
85
Yao Chen884c8c12018-01-26 10:36:25 -080086 void dumpStates(FILE* out, bool verbose);
87
Yangster-mac15f6bbc2018-04-08 11:52:26 -070088 void informPullAlarmFired(const int64_t timestampNs);
Yangster-macb142cc82018-03-30 15:22:08 -070089
David Chend37bc232018-04-12 18:05:11 -070090 int64_t getLastReportTimeNs(const ConfigKey& key);
91
Yao Chen876889c2018-05-02 11:16:16 -070092 inline void setPrintLogs(bool enabled) {
93#ifdef VERY_VERBOSE_PRINTING
94 std::lock_guard<std::mutex> lock(mMetricsMutex);
95 mPrintAllLogs = enabled;
96#endif
97 }
98
Yao Chenab273e22017-09-06 12:53:50 -070099private:
Yangster-macbe10ddf2018-03-13 15:39:51 -0700100 // For testing only.
101 inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
102 return mAnomalyAlarmMonitor;
103 }
104
Yangster-mac684d1952018-03-24 16:47:16 -0700105 inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
106 return mPeriodicAlarmMonitor;
107 }
108
Yangster-macb0d06282018-01-05 15:44:07 -0800109 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -0800110
Yao Chend10f7b12017-12-18 12:53:50 -0800111 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -0700112
David Chen1d7b0cd2017-11-15 14:20:04 -0800113 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -0700114
David Chend9269e22017-12-05 13:43:51 -0800115 // Tracks when we last checked the bytes consumed for each config key.
116 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
117
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700118 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -0700119
Chenjie Yufa22d652018-02-05 14:37:48 -0800120 StatsPullerManager mStatsPullerManager;
121
Yangster-mac932ecec2018-02-01 10:23:52 -0800122 sp<AlarmMonitor> mAnomalyAlarmMonitor;
123
124 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800125
Yangster-macb142cc82018-03-30 15:22:08 -0700126 void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
127
128 void OnConfigUpdatedLocked(
129 const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
130
Chenjie Yue36018b2018-04-16 15:18:30 -0700131 void WriteDataToDiskLocked(DumpReportReason dumpReportReason);
132 void WriteDataToDiskLocked(const ConfigKey& key, DumpReportReason dumpReportReason);
Yangster-mace68f3a52018-04-04 00:01:43 -0700133
Yangster-macb142cc82018-03-30 15:22:08 -0700134 void onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700135 const bool include_current_partial_bucket,
Yangster-mac9def8e32018-04-17 13:55:51 -0700136 const bool include_string,
Chenjie Yue36018b2018-04-16 15:18:30 -0700137 const DumpReportReason dumpReportReason,
yro4beccbe2018-03-15 19:42:05 -0700138 util::ProtoOutputStream* proto);
Yangster-mac86179502018-01-23 15:47:15 -0800139
David Chen1d7b0cd2017-11-15 14:20:04 -0800140 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
141 * actually delete the data. */
Yangster-macb142cc82018-03-30 15:22:08 -0700142 void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key,
Yangster-macb0d06282018-01-05 15:44:07 -0800143 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -0700144
Yangster-macd40053e2018-01-09 16:29:22 -0800145 // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
146 void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
147
148 // Handler over the isolated uid change event.
149 void onIsolatedUidChangedEventLocked(const LogEvent& event);
150
Yao Chen163d2602018-04-10 10:39:53 -0700151 void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);
152
David Chen1d7b0cd2017-11-15 14:20:04 -0800153 // Function used to send a broadcast so that receiver for the config key can call getData
154 // to retrieve the stored data.
155 std::function<void(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -0700156
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700157 const int64_t mTimeBaseNs;
Chenjie Yu85ed8382017-12-14 16:48:54 -0800158
Yao Chen163d2602018-04-10 10:39:53 -0700159 // Largest timestamp of the events that we have processed.
160 int64_t mLargestTimestampSeen = 0;
161
162 int64_t mLastTimestampSeen = 0;
163
164 bool mInReconnection = false;
165
166 // Processed log count
167 uint64_t mLogCount = 0;
168
169 // Log loss detected count
170 int mLogLossCount = 0;
Yao Chen8f42ba02018-02-27 15:17:07 -0800171
Chenjie Yufa22d652018-02-05 14:37:48 -0800172 long mLastPullerCacheClearTimeSec = 0;
173
Yao Chen876889c2018-05-02 11:16:16 -0700174#ifdef VERY_VERBOSE_PRINTING
175 bool mPrintAllLogs = false;
176#endif
177
Yao Chen163d2602018-04-10 10:39:53 -0700178 FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
David Chend9269e22017-12-05 13:43:51 -0800179 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
180 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
181 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800182 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
183 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
184 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
185 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
186 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
187 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
188 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
189 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
Yangster-mace06cfd72018-03-10 23:22:59 -0800190 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
191 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
Yangster-mac87718e22018-01-11 16:16:26 -0800192 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700193 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
194 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
195 FRIEND_TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents);
196 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
197 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
198
Yangster13fb7e42018-03-07 17:30:49 -0800199 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
200 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
201 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
202 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
203
204 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
205 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
206 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
207
Yangster13fb7e42018-03-07 17:30:49 -0800208 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
209 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
210 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
211
Yangster-macbe10ddf2018-03-13 15:39:51 -0700212 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
213 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
214 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
215 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
216 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
Yangster-macb142cc82018-03-30 15:22:08 -0700217
Yangster-mac684d1952018-03-24 16:47:16 -0700218 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
Yangster-macb142cc82018-03-30 15:22:08 -0700219 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
Yao Chenab273e22017-09-06 12:53:50 -0700220};
Bookatz906a35c2017-09-20 15:26:44 -0700221
Yao Chenef99c4f2017-09-22 16:26:54 -0700222} // namespace statsd
223} // namespace os
224} // namespace android