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  */
16 
17 #pragma once
18 
19 #include <gtest/gtest_prod.h>
20 #include "config/ConfigListener.h"
21 #include "metrics/MetricsManager.h"
22 #include "packages/UidMap.h"
23 #include "external/StatsPullerManager.h"
24 
25 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
26 
27 #include <stdio.h>
28 #include <unordered_map>
29 
30 namespace android {
31 namespace os {
32 namespace statsd {
33 
34 
35 class StatsLogProcessor : public ConfigListener, public virtual PackageInfoListener {
36 public:
37     StatsLogProcessor(const sp<UidMap>& uidMap, const sp<StatsPullerManager>& pullerManager,
38                       const sp<AlarmMonitor>& anomalyAlarmMonitor,
39                       const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
40                       const int64_t timeBaseNs,
41                       const std::function<bool(const ConfigKey&)>& sendBroadcast,
42                       const std::function<bool(const int&,
43                                                const vector<int64_t>&)>& sendActivationBroadcast);
44     virtual ~StatsLogProcessor();
45 
46     void OnLogEvent(LogEvent* event);
47 
48     void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
49                          const StatsdConfig& config);
50     void OnConfigRemoved(const ConfigKey& key);
51 
52     size_t GetMetricsSize(const ConfigKey& key) const;
53 
54     void GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs);
55 
56     void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
57                       const bool include_current_partial_bucket, const bool erase_data,
58                       const DumpReportReason dumpReportReason,
59                       const DumpLatency dumpLatency,
60                       vector<uint8_t>* outData);
61     void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
62                       const bool include_current_partial_bucket, const bool erase_data,
63                       const DumpReportReason dumpReportReason,
64                       const DumpLatency dumpLatency,
65                       ProtoOutputStream* proto);
66 
67     /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
68     void onAnomalyAlarmFired(
69             const int64_t& timestampNs,
70             unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
71 
72     /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
73     void onPeriodicAlarmFired(
74             const int64_t& timestampNs,
75             unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
76 
77     /* Flushes data to disk. Data on memory will be gone after written to disk. */
78     void WriteDataToDisk(const DumpReportReason dumpReportReason,
79                          const DumpLatency dumpLatency);
80 
81     /* Persist configs containing metrics with active activations to disk. */
82     void SaveActiveConfigsToDisk(int64_t currentTimeNs);
83 
84     /* Writes the current active status/ttl for all configs and metrics to ProtoOutputStream. */
85     void WriteActiveConfigsToProtoOutputStream(
86             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
87 
88     /* Load configs containing metrics with active activations from disk. */
89     void LoadActiveConfigsFromDisk();
90 
91     /* Sets the active status/ttl for all configs and metrics to the status in ActiveConfigList. */
92     void SetConfigsActiveState(const ActiveConfigList& activeConfigList, int64_t currentTimeNs);
93 
94     /* Notify all MetricsManagers of app upgrades */
95     void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
96                           const int64_t version) override;
97 
98     /* Notify all MetricsManagers of app removals */
99     void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override;
100 
101     /* Notify all MetricsManagers of uid map snapshots received */
102     void onUidMapReceived(const int64_t& eventTimeNs) override;
103 
104     // Reset all configs.
105     void resetConfigs();
106 
getUidMap()107     inline sp<UidMap> getUidMap() {
108         return mUidMap;
109     }
110 
111     void dumpStates(int outFd, bool verbose);
112 
113     void informPullAlarmFired(const int64_t timestampNs);
114 
115     int64_t getLastReportTimeNs(const ConfigKey& key);
116 
setPrintLogs(bool enabled)117     inline void setPrintLogs(bool enabled) {
118 #ifdef VERY_VERBOSE_PRINTING
119         std::lock_guard<std::mutex> lock(mMetricsMutex);
120         mPrintAllLogs = enabled;
121 #endif
122     }
123 
124     // Add a specific config key to the possible configs to dump ASAP.
125     void noteOnDiskData(const ConfigKey& key);
126 
127 private:
128     // For testing only.
getAnomalyAlarmMonitor()129     inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
130         return mAnomalyAlarmMonitor;
131     }
132 
getPeriodicAlarmMonitor()133     inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
134         return mPeriodicAlarmMonitor;
135     }
136 
137     mutable mutex mMetricsMutex;
138 
139     std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
140 
141     std::unordered_map<ConfigKey, int64_t> mLastBroadcastTimes;
142 
143     // Last time we sent a broadcast to this uid that the active configs had changed.
144     std::unordered_map<int, int64_t> mLastActivationBroadcastTimes;
145 
146     // Tracks when we last checked the bytes consumed for each config key.
147     std::unordered_map<ConfigKey, int64_t> mLastByteSizeTimes;
148 
149     // Tracks which config keys has metric reports on disk
150     std::set<ConfigKey> mOnDiskDataConfigs;
151 
152     sp<UidMap> mUidMap;  // Reference to the UidMap to lookup app name and version for each uid.
153 
154     sp<StatsPullerManager> mPullerManager;  // Reference to StatsPullerManager
155 
156     sp<AlarmMonitor> mAnomalyAlarmMonitor;
157 
158     sp<AlarmMonitor> mPeriodicAlarmMonitor;
159 
160     void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
161 
162     void OnConfigUpdatedLocked(
163         const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
164 
165     void GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs);
166 
167     void WriteActiveConfigsToProtoOutputStreamLocked(
168             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
169 
170     void SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
171                                      int64_t currentTimeNs);
172 
173     void WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
174                                const DumpLatency dumpLatency);
175     void WriteDataToDiskLocked(const ConfigKey& key, const int64_t timestampNs,
176                                const DumpReportReason dumpReportReason,
177                                const DumpLatency dumpLatency);
178 
179     void onConfigMetricsReportLocked(
180             const ConfigKey& key, const int64_t dumpTimeStampNs,
181             const bool include_current_partial_bucket, const bool erase_data,
182             const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
183             /*if dataSavedToDisk is true, it indicates the caller will write the data to disk
184              (e.g., before reboot). So no need to further persist local history.*/
185             const bool dataSavedToDisk, vector<uint8_t>* proto);
186 
187     /* Check if we should send a broadcast if approaching memory limits and if we're over, we
188      * actually delete the data. */
189     void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key,
190                                 MetricsManager& metricsManager);
191 
192     // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
193     void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
194 
195     // Handler over the isolated uid change event.
196     void onIsolatedUidChangedEventLocked(const LogEvent& event);
197 
198     // Reset all configs.
199     void resetConfigsLocked(const int64_t timestampNs);
200     // Reset the specified configs.
201     void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);
202 
203     // Function used to send a broadcast so that receiver for the config key can call getData
204     // to retrieve the stored data.
205     std::function<bool(const ConfigKey& key)> mSendBroadcast;
206 
207     // Function used to send a broadcast so that receiver can be notified of which configs
208     // are currently active.
209     std::function<bool(const int& uid, const vector<int64_t>& configIds)> mSendActivationBroadcast;
210 
211     const int64_t mTimeBaseNs;
212 
213     // Largest timestamp of the events that we have processed.
214     int64_t mLargestTimestampSeen = 0;
215 
216     int64_t mLastTimestampSeen = 0;
217 
218     int64_t mLastPullerCacheClearTimeSec = 0;
219 
220     // Last time we wrote data to disk.
221     int64_t mLastWriteTimeNs = 0;
222 
223     // Last time we wrote active metrics to disk.
224     int64_t mLastActiveMetricsWriteNs = 0;
225 
226 #ifdef VERY_VERBOSE_PRINTING
227     bool mPrintAllLogs = false;
228 #endif
229 
230     FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
231     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
232     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
233     FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
234     FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
235     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
236     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
237     FRIEND_TEST(StatsLogProcessorTest,
238             TestActivationOnBootMultipleActivationsDifferentActivationTypes);
239     FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
240 
241     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
242     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
243     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
244     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
245     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
246     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
247     FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
248     FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
249     FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
250     FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
251     FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
252     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
253     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
254     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation);
255     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition);
256     FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
257     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
258     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
259     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
260 
261     FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
262     FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
263     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
264     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
265 
266     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
267     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
268     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
269 
270     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
271     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
272     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
273 
274     FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
275     FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
276     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
277     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
278     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
279 
280     FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
281     FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
282     FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
283     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
284     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
285     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
286     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
287 
288     FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
289     FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
290     FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
291     FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
292     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
293     FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
294 };
295 
296 }  // namespace statsd
297 }  // namespace os
298 }  // namespace android
299