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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 35 | class StatsLogProcessor : public ConfigListener { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 36 | public: |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 37 | StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 38 | const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 39 | const long timeBaseSec, |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 40 | const std::function<void(const ConfigKey&)>& sendBroadcast); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 41 | virtual ~StatsLogProcessor(); |
| 42 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 43 | void OnLogEvent(LogEvent* event); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 44 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 45 | void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config); |
| 46 | void OnConfigRemoved(const ConfigKey& key); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 47 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 48 | size_t GetMetricsSize(const ConfigKey& key) const; |
| 49 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 50 | void onDumpReport(const ConfigKey& key, const uint64_t dumpTimeNs, vector<uint8_t>* outData); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 51 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 52 | /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */ |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 53 | void onAnomalyAlarmFired( |
| 54 | const uint64_t timestampNs, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 55 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet); |
| 56 | |
| 57 | /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */ |
| 58 | void onPeriodicAlarmFired( |
| 59 | const uint64_t timestampNs, |
| 60 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 61 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 62 | /* Flushes data to disk. Data on memory will be gone after written to disk. */ |
| 63 | void WriteDataToDisk(); |
| 64 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 65 | inline sp<UidMap> getUidMap() { |
| 66 | return mUidMap; |
| 67 | } |
| 68 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 69 | void dumpStates(FILE* out, bool verbose); |
| 70 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 71 | private: |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 72 | mutable mutex mMetricsMutex; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 73 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 74 | std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 75 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 76 | std::unordered_map<ConfigKey, long> mLastBroadcastTimes; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 77 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 78 | // Tracks when we last checked the bytes consumed for each config key. |
| 79 | std::unordered_map<ConfigKey, long> mLastByteSizeTimes; |
| 80 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 81 | 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] | 82 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 83 | StatsPullerManager mStatsPullerManager; |
| 84 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 85 | sp<AlarmMonitor> mAnomalyAlarmMonitor; |
| 86 | |
| 87 | sp<AlarmMonitor> mPeriodicAlarmMonitor; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 88 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 89 | void onDumpReportLocked(const ConfigKey& key, const uint64_t dumpTimeNs, |
| 90 | vector<uint8_t>* outData); |
Yangster-mac | 8617950 | 2018-01-23 15:47:15 -0800 | [diff] [blame] | 91 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 92 | /* Check if we should send a broadcast if approaching memory limits and if we're over, we |
| 93 | * actually delete the data. */ |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 94 | void flushIfNecessaryLocked(uint64_t timestampNs, const ConfigKey& key, |
| 95 | MetricsManager& metricsManager); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 96 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 97 | // Maps the isolated uid in the log event to host uid if the log event contains uid fields. |
| 98 | void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const; |
| 99 | |
| 100 | // Handler over the isolated uid change event. |
| 101 | void onIsolatedUidChangedEventLocked(const LogEvent& event); |
| 102 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 103 | // Function used to send a broadcast so that receiver for the config key can call getData |
| 104 | // to retrieve the stored data. |
| 105 | std::function<void(const ConfigKey& key)> mSendBroadcast; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 106 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 107 | const long mTimeBaseSec; |
| 108 | |
Yao Chen | 8f42ba0 | 2018-02-27 15:17:07 -0800 | [diff] [blame] | 109 | int64_t mLastLogTimestamp; |
| 110 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 111 | long mLastPullerCacheClearTimeSec = 0; |
| 112 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 113 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize); |
| 114 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast); |
| 115 | FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 116 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1); |
| 117 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2); |
| 118 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3); |
| 119 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1); |
| 120 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2); |
| 121 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3); |
| 122 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1); |
| 123 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 124 | FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 125 | FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame^] | 126 | FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition); |
| 127 | FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition); |
| 128 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition); |
| 129 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition); |
| 130 | |
| 131 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition); |
| 132 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition); |
| 133 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition); |
| 134 | |
| 135 | |
| 136 | |
| 137 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition); |
| 138 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition); |
| 139 | FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition); |
| 140 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 141 | }; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 142 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 143 | } // namespace statsd |
| 144 | } // namespace os |
| 145 | } // namespace android |