Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 16 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <gtest/gtest_prod.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigListener.h" |
| 21 | #include "logd/LogReader.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 22 | #include "metrics/MetricsManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "packages/UidMap.h" |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 24 | #include "external/StatsPullerManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | |
| 26 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 27 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 34 | |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 35 | // Keep this in sync with DumpReportReason enum in stats_log.proto |
| 36 | enum 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, |
Yang Lu | 658bc0e | 2018-10-19 09:03:51 -0700 | [diff] [blame] | 43 | STATSCOMPANION_DIED = 7, |
| 44 | TERMINATION_SIGNAL_RECEIVED = 8 |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 47 | class StatsLogProcessor : public ConfigListener { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 48 | public: |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 49 | StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 50 | const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 51 | const int64_t timeBaseNs, |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 52 | const std::function<bool(const ConfigKey&)>& sendBroadcast); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 53 | virtual ~StatsLogProcessor(); |
| 54 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 55 | void OnLogEvent(LogEvent* event, bool reconnectionStarts); |
| 56 | |
| 57 | // for testing only. |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 58 | void OnLogEvent(LogEvent* event); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 59 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 60 | void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key, |
| 61 | const StatsdConfig& config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 62 | void OnConfigRemoved(const ConfigKey& key); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 63 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 64 | size_t GetMetricsSize(const ConfigKey& key) const; |
| 65 | |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 66 | void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs, |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 67 | const bool include_current_partial_bucket, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 68 | const DumpReportReason dumpReportReason, vector<uint8_t>* outData); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 69 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 70 | /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */ |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | void onAnomalyAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 72 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 73 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet); |
| 74 | |
| 75 | /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */ |
| 76 | void onPeriodicAlarmFired( |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 77 | const int64_t& timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 78 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 79 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 80 | /* Flushes data to disk. Data on memory will be gone after written to disk. */ |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 81 | void WriteDataToDisk(const DumpReportReason dumpReportReason); |
| 82 | |
| 83 | // Reset all configs. |
| 84 | void resetConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 85 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 86 | inline sp<UidMap> getUidMap() { |
| 87 | return mUidMap; |
| 88 | } |
| 89 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 90 | void dumpStates(FILE* out, bool verbose); |
| 91 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 92 | void informPullAlarmFired(const int64_t timestampNs); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 93 | |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 94 | int64_t getLastReportTimeNs(const ConfigKey& key); |
| 95 | |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 96 | inline void setPrintLogs(bool enabled) { |
| 97 | #ifdef VERY_VERBOSE_PRINTING |
| 98 | std::lock_guard<std::mutex> lock(mMetricsMutex); |
| 99 | mPrintAllLogs = enabled; |
| 100 | #endif |
| 101 | } |
| 102 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 103 | // Add a specific config key to the possible configs to dump ASAP. |
| 104 | void noteOnDiskData(const ConfigKey& key); |
| 105 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 106 | private: |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 107 | // For testing only. |
| 108 | inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const { |
| 109 | return mAnomalyAlarmMonitor; |
| 110 | } |
| 111 | |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 112 | inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const { |
| 113 | return mPeriodicAlarmMonitor; |
| 114 | } |
| 115 | |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 116 | mutable mutex mMetricsMutex; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 117 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 118 | std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 119 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 120 | std::unordered_map<ConfigKey, long> mLastBroadcastTimes; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 121 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 122 | // Tracks when we last checked the bytes consumed for each config key. |
| 123 | std::unordered_map<ConfigKey, long> mLastByteSizeTimes; |
| 124 | |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 125 | // Tracks which config keys has metric reports on disk |
| 126 | std::set<ConfigKey> mOnDiskDataConfigs; |
| 127 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 128 | sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid. |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 129 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 130 | StatsPullerManager mStatsPullerManager; |
| 131 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 132 | sp<AlarmMonitor> mAnomalyAlarmMonitor; |
| 133 | |
| 134 | sp<AlarmMonitor> mPeriodicAlarmMonitor; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 135 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 136 | void resetIfConfigTtlExpiredLocked(const int64_t timestampNs); |
| 137 | |
| 138 | void OnConfigUpdatedLocked( |
| 139 | const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config); |
| 140 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 141 | void WriteDataToDiskLocked(const DumpReportReason dumpReportReason); |
| 142 | void WriteDataToDiskLocked(const ConfigKey& key, const int64_t timestampNs, |
| 143 | const DumpReportReason dumpReportReason); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 144 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 145 | void onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 146 | const bool include_current_partial_bucket, |
Chenjie Yu | e36018b | 2018-04-16 15:18:30 -0700 | [diff] [blame] | 147 | const DumpReportReason dumpReportReason, |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 148 | util::ProtoOutputStream* proto); |
Yangster-mac | 8617950 | 2018-01-23 15:47:15 -0800 | [diff] [blame] | 149 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 150 | /* Check if we should send a broadcast if approaching memory limits and if we're over, we |
| 151 | * actually delete the data. */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 152 | void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key, |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 153 | MetricsManager& metricsManager); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 154 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 155 | // Maps the isolated uid in the log event to host uid if the log event contains uid fields. |
| 156 | void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const; |
| 157 | |
| 158 | // Handler over the isolated uid change event. |
| 159 | void onIsolatedUidChangedEventLocked(const LogEvent& event); |
| 160 | |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 161 | // Reset all configs. |
| 162 | void resetConfigsLocked(const int64_t timestampNs); |
| 163 | // Reset the specified configs. |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 164 | void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs); |
| 165 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 166 | // Function used to send a broadcast so that receiver for the config key can call getData |
| 167 | // to retrieve the stored data. |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 168 | std::function<bool(const ConfigKey& key)> mSendBroadcast; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 169 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 170 | const int64_t mTimeBaseNs; |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 171 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 172 | // Largest timestamp of the events that we have processed. |
| 173 | int64_t mLargestTimestampSeen = 0; |
| 174 | |
| 175 | int64_t mLastTimestampSeen = 0; |
| 176 | |
| 177 | bool mInReconnection = false; |
| 178 | |
| 179 | // Processed log count |
| 180 | uint64_t mLogCount = 0; |
| 181 | |
| 182 | // Log loss detected count |
| 183 | int mLogLossCount = 0; |
Yao Chen | 8f42ba0 | 2018-02-27 15:17:07 -0800 | [diff] [blame] | 184 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 185 | long mLastPullerCacheClearTimeSec = 0; |
| 186 | |
Tej Singh | 42f9e06 | 2018-11-09 10:01:00 -0800 | [diff] [blame] | 187 | // Last time we wrote data to disk. |
| 188 | int64_t mLastWriteTimeNs = 0; |
| 189 | |
Yao Chen | 876889c | 2018-05-02 11:16:16 -0700 | [diff] [blame] | 190 | #ifdef VERY_VERBOSE_PRINTING |
| 191 | bool mPrintAllLogs = false; |
| 192 | #endif |
| 193 | |
Yao Chen | 163d260 | 2018-04-10 10:39:53 -0700 | [diff] [blame] | 194 | FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 195 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize); |
| 196 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast); |
| 197 | FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 198 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1); |
| 199 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2); |
| 200 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3); |
| 201 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1); |
| 202 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2); |
| 203 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3); |
| 204 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1); |
| 205 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 206 | FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid); |
| 207 | FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 208 | FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 209 | FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); |
| 210 | FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); |
| 211 | FRIEND_TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents); |
| 212 | FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); |
| 213 | FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); |
| 214 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 215 | FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition); |
| 216 | FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition); |
| 217 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition); |
| 218 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition); |
| 219 | |
| 220 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition); |
| 221 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition); |
| 222 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition); |
| 223 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 224 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition); |
| 225 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition); |
| 226 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition); |
| 227 | |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 228 | FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket); |
| 229 | FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets); |
| 230 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket); |
| 231 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets); |
| 232 | FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 233 | |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 234 | FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 235 | FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 236 | }; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 237 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 238 | } // namespace statsd |
| 239 | } // namespace os |
| 240 | } // namespace android |