blob: 8db82006d082f80a73ae02e0153a1798c801fdf8 [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-mac20877162017-12-22 17:19:39 -080039 const long timeBaseSec,
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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070045 void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config);
46 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070047
Yangster7c334a12017-11-22 14:24:24 -080048 size_t GetMetricsSize(const ConfigKey& key) const;
49
Yao Chen8a8d16c2018-02-08 14:50:40 -080050 void onDumpReport(const ConfigKey& key, const uint64_t dumpTimeNs, vector<uint8_t>* outData);
Bookatzcc5adef2017-11-21 14:36:23 -080051
Yangster-mac932ecec2018-02-01 10:23:52 -080052 /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080053 void onAnomalyAlarmFired(
54 const uint64_t timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080055 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);
yro31eb67b2017-10-24 13:33:21 -070061
yro947fbce2017-11-15 22:50:23 -080062 /* Flushes data to disk. Data on memory will be gone after written to disk. */
63 void WriteDataToDisk();
64
Yangster-macb5bc7412018-01-06 23:17:45 -080065 inline sp<UidMap> getUidMap() {
66 return mUidMap;
67 }
68
Yao Chen884c8c12018-01-26 10:36:25 -080069 void dumpStates(FILE* out, bool verbose);
70
Yao Chenab273e22017-09-06 12:53:50 -070071private:
Yangster-macb0d06282018-01-05 15:44:07 -080072 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -080073
Yao Chend10f7b12017-12-18 12:53:50 -080074 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070075
David Chen1d7b0cd2017-11-15 14:20:04 -080076 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -070077
David Chend9269e22017-12-05 13:43:51 -080078 // Tracks when we last checked the bytes consumed for each config key.
79 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
80
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070082
Chenjie Yufa22d652018-02-05 14:37:48 -080083 StatsPullerManager mStatsPullerManager;
84
Yangster-mac932ecec2018-02-01 10:23:52 -080085 sp<AlarmMonitor> mAnomalyAlarmMonitor;
86
87 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Yangster-mace2cd6d52017-11-09 20:38:30 -080088
Yao Chen8a8d16c2018-02-08 14:50:40 -080089 void onDumpReportLocked(const ConfigKey& key, const uint64_t dumpTimeNs,
90 vector<uint8_t>* outData);
Yangster-mac86179502018-01-23 15:47:15 -080091
David Chen1d7b0cd2017-11-15 14:20:04 -080092 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
93 * actually delete the data. */
Yangster-macb0d06282018-01-05 15:44:07 -080094 void flushIfNecessaryLocked(uint64_t timestampNs, const ConfigKey& key,
95 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -070096
Yangster-macd40053e2018-01-09 16:29:22 -080097 // 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 Chen1d7b0cd2017-11-15 14:20:04 -0800103 // 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;
yro69007c82017-10-26 20:42:57 -0700106
Chenjie Yu85ed8382017-12-14 16:48:54 -0800107 const long mTimeBaseSec;
108
Yao Chen8f42ba02018-02-27 15:17:07 -0800109 int64_t mLastLogTimestamp;
110
Chenjie Yufa22d652018-02-05 14:37:48 -0800111 long mLastPullerCacheClearTimeSec = 0;
112
David Chend9269e22017-12-05 13:43:51 -0800113 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
114 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
115 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800116 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-macb5bc7412018-01-06 23:17:45 -0800124 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
Yangster-mac87718e22018-01-11 16:16:26 -0800125 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster13fb7e42018-03-07 17:30:49 -0800126 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 Chenab273e22017-09-06 12:53:50 -0700141};
Bookatz906a35c2017-09-20 15:26:44 -0700142
Yao Chenef99c4f2017-09-22 16:26:54 -0700143} // namespace statsd
144} // namespace os
145} // namespace android