blob: 0e1d4ba55189ff73f9e25bbdc3d46ec4328e0893 [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070036public:
Yangster-mac932ecec2018-02-01 10:23:52 -080037 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor,
38 const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070039 const int64_t timeBaseNs,
David Chen1d7b0cd2017-11-15 14:20:04 -080040 const std::function<void(const ConfigKey&)>& sendBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070041 virtual ~StatsLogProcessor();
42
Yangster-macd40053e2018-01-09 16:29:22 -080043 void OnLogEvent(LogEvent* event);
Yao Chenab273e22017-09-06 12:53:50 -070044
Yangster-macc04feba2018-04-02 14:37:33 -070045 void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
46 const StatsdConfig& config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070047 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070048
Yangster7c334a12017-11-22 14:24:24 -080049 size_t GetMetricsSize(const ConfigKey& key) const;
50
Yangster-mace68f3a52018-04-04 00:01:43 -070051 void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
52 const bool include_current_partial_bucket, vector<uint8_t>* outData);
Bookatzcc5adef22017-11-21 14:36:23 -080053
Yangster-mac932ecec2018-02-01 10:23:52 -080054 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080055 void onAnomalyAlarmFired(
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);
58
59 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
60 void onPeriodicAlarmFired(
Yangster-macb142cc82018-03-30 15:22:08 -070061 const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080062 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
yro31eb67b2017-10-24 13:33:21 -070063
yro947fbce2017-11-15 22:50:23 -080064 /* Flushes data to disk. Data on memory will be gone after written to disk. */
65 void WriteDataToDisk();
66
Yangster-macb5bc7412018-01-06 23:17:45 -080067 inline sp<UidMap> getUidMap() {
68 return mUidMap;
69 }
70
Yao Chen884c8c12018-01-26 10:36:25 -080071 void dumpStates(FILE* out, bool verbose);
72
Yangster-mac15f6bbc2018-04-08 11:52:26 -070073 void informPullAlarmFired(const int64_t timestampNs);
Yangster-macb142cc82018-03-30 15:22:08 -070074
Yao Chenab273e22017-09-06 12:53:50 -070075private:
Yangster-macbe10ddf2018-03-13 15:39:51 -070076 // For testing only.
77 inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
78 return mAnomalyAlarmMonitor;
79 }
80
Yangster-mac684d1952018-03-24 16:47:16 -070081 inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
82 return mPeriodicAlarmMonitor;
83 }
84
Yangster-macb0d06282018-01-05 15:44:07 -080085 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -080086
Yao Chend10f7b12017-12-18 12:53:50 -080087 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070088
David Chen1d7b0cd2017-11-15 14:20:04 -080089 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -070090
David Chend9269e22017-12-05 13:43:51 -080091 // Tracks when we last checked the bytes consumed for each config key.
92 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
93
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070095
Chenjie Yufa22d652018-02-05 14:37:48 -080096 StatsPullerManager mStatsPullerManager;
97
Yangster-mac932ecec2018-02-01 10:23:52 -080098 sp<AlarmMonitor> mAnomalyAlarmMonitor;
99
100 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101
Yangster-macb142cc82018-03-30 15:22:08 -0700102 void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
103
104 void OnConfigUpdatedLocked(
105 const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
106
Yangster-mace68f3a52018-04-04 00:01:43 -0700107 void WriteDataToDiskLocked();
108 void WriteDataToDiskLocked(const ConfigKey& key);
109
Yangster-macb142cc82018-03-30 15:22:08 -0700110 void onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700111 const bool include_current_partial_bucket,
yro4beccbe2018-03-15 19:42:05 -0700112 util::ProtoOutputStream* proto);
Yangster-mac86179502018-01-23 15:47:15 -0800113
David Chen1d7b0cd2017-11-15 14:20:04 -0800114 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
115 * actually delete the data. */
Yangster-macb142cc82018-03-30 15:22:08 -0700116 void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key,
Yangster-macb0d06282018-01-05 15:44:07 -0800117 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -0700118
Yangster-macd40053e2018-01-09 16:29:22 -0800119 // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
120 void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
121
122 // Handler over the isolated uid change event.
123 void onIsolatedUidChangedEventLocked(const LogEvent& event);
124
David Chen1d7b0cd2017-11-15 14:20:04 -0800125 // Function used to send a broadcast so that receiver for the config key can call getData
126 // to retrieve the stored data.
127 std::function<void(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -0700128
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700129 const int64_t mTimeBaseNs;
Chenjie Yu85ed8382017-12-14 16:48:54 -0800130
Yao Chen8f42ba02018-02-27 15:17:07 -0800131 int64_t mLastLogTimestamp;
132
Chenjie Yufa22d652018-02-05 14:37:48 -0800133 long mLastPullerCacheClearTimeSec = 0;
134
David Chend9269e22017-12-05 13:43:51 -0800135 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
136 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
137 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800138 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
139 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
140 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
141 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
142 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
143 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
144 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
145 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
Yangster-mace06cfd72018-03-10 23:22:59 -0800146 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
147 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
Yangster-mac87718e22018-01-11 16:16:26 -0800148 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700149 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
150 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
151 FRIEND_TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents);
152 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
153 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
154
Yangster13fb7e42018-03-07 17:30:49 -0800155 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
156 FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
157 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
158 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
159
160 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
161 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
162 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
163
Yangster13fb7e42018-03-07 17:30:49 -0800164 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
165 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
166 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
167
Yangster-macbe10ddf2018-03-13 15:39:51 -0700168 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
169 FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
170 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
171 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
172 FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
Yangster-macb142cc82018-03-30 15:22:08 -0700173
Yangster-mac684d1952018-03-24 16:47:16 -0700174 FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
Yangster-macb142cc82018-03-30 15:22:08 -0700175 FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
Yao Chenab273e22017-09-06 12:53:50 -0700176};
Bookatz906a35c2017-09-20 15:26:44 -0700177
Yao Chenef99c4f2017-09-22 16:26:54 -0700178} // namespace statsd
179} // namespace os
180} // namespace android