blob: d52be441f6b69c4e25a5634764e18809c07c7fef [file] [log] [blame]
David Chend9269e22017-12-05 13:43:51 -08001// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "StatsLogProcessor.h"
16#include "config/ConfigKey.h"
David Chen35045cb2018-03-23 22:21:47 -070017#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
David Chend9269e22017-12-05 13:43:51 -080018#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
19#include "guardrail/StatsdStats.h"
20#include "logd/LogEvent.h"
21#include "packages/UidMap.h"
22#include "statslog.h"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
David Chen9e6dbbd2018-05-07 17:52:29 -070027#include "tests/statsd_test_util.h"
28
David Chend9269e22017-12-05 13:43:51 -080029#include <stdio.h>
30
31using namespace android;
32using namespace testing;
33
34namespace android {
35namespace os {
36namespace statsd {
37
Yao Chen288c6002017-12-12 13:43:18 -080038using android::util::ProtoOutputStream;
39
David Chend9269e22017-12-05 13:43:51 -080040#ifdef __ANDROID__
41
42/**
43 * Mock MetricsManager (ByteSize() is called).
44 */
45class MockMetricsManager : public MetricsManager {
46public:
Chenjie Yue2219202018-06-08 10:07:51 -070047 MockMetricsManager()
48 : MetricsManager(ConfigKey(1, 12345), StatsdConfig(), 1000, 1000, new UidMap(),
49 new StatsPullerManager(),
50 new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t) {},
51 [](const sp<IStatsCompanionService>&) {}),
52 new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t) {},
53 [](const sp<IStatsCompanionService>&) {})) {
David Chend9269e22017-12-05 13:43:51 -080054 }
55
56 MOCK_METHOD0(byteSize, size_t());
Yao Chen06dba5d2018-01-26 13:38:16 -080057
Yangster-macb142cc82018-03-30 15:22:08 -070058 MOCK_METHOD1(dropData, void(const int64_t dropTimeNs));
David Chend9269e22017-12-05 13:43:51 -080059};
60
61TEST(StatsLogProcessorTest, TestRateLimitByteSize) {
62 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -070063 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yangster-mac932ecec2018-02-01 10:23:52 -080064 sp<AlarmMonitor> anomalyAlarmMonitor;
65 sp<AlarmMonitor> periodicAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -080066 // Construct the processor with a dummy sendBroadcast function that does nothing.
Chenjie Yue2219202018-06-08 10:07:51 -070067 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, 0,
68 [](const ConfigKey& key) { return true; });
David Chend9269e22017-12-05 13:43:51 -080069
70 MockMetricsManager mockMetricsManager;
71
Yangster-mac94e197c2018-01-02 16:03:03 -080072 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -080073 // Expect only the first flush to trigger a check for byte size since the last two are
74 // rate-limited.
75 EXPECT_CALL(mockMetricsManager, byteSize()).Times(1);
Yangster-macb0d06282018-01-05 15:44:07 -080076 p.flushIfNecessaryLocked(99, key, mockMetricsManager);
77 p.flushIfNecessaryLocked(100, key, mockMetricsManager);
78 p.flushIfNecessaryLocked(101, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -080079}
80
81TEST(StatsLogProcessorTest, TestRateLimitBroadcast) {
82 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -070083 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yangster-mac932ecec2018-02-01 10:23:52 -080084 sp<AlarmMonitor> anomalyAlarmMonitor;
85 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -080086 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -070087 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
88 [&broadcastCount](const ConfigKey& key) {
89 broadcastCount++;
90 return true;
91 });
David Chend9269e22017-12-05 13:43:51 -080092
93 MockMetricsManager mockMetricsManager;
94
Yangster-mac94e197c2018-01-02 16:03:03 -080095 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -080096 EXPECT_CALL(mockMetricsManager, byteSize())
Yao Chen8a8d16c2018-02-08 14:50:40 -080097 .Times(1)
David Chend9269e22017-12-05 13:43:51 -080098 .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * .95)));
99
100 // Expect only one broadcast despite always returning a size that should trigger broadcast.
Yangster-macb0d06282018-01-05 15:44:07 -0800101 p.flushIfNecessaryLocked(1, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -0800102 EXPECT_EQ(1, broadcastCount);
103
Yao Chen8a8d16c2018-02-08 14:50:40 -0800104 // b/73089712
David Chend9269e22017-12-05 13:43:51 -0800105 // This next call to flush should not trigger a broadcast.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800106 // p.mLastByteSizeTimes.clear(); // Force another check for byte size.
107 // p.flushIfNecessaryLocked(2, key, mockMetricsManager);
108 // EXPECT_EQ(1, broadcastCount);
David Chend9269e22017-12-05 13:43:51 -0800109}
110
111TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) {
112 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700113 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yangster-mac932ecec2018-02-01 10:23:52 -0800114 sp<AlarmMonitor> anomalyAlarmMonitor;
115 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -0800116 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700117 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
118 [&broadcastCount](const ConfigKey& key) {
119 broadcastCount++;
120 return true;
121 });
David Chend9269e22017-12-05 13:43:51 -0800122
123 MockMetricsManager mockMetricsManager;
124
Yangster-mac94e197c2018-01-02 16:03:03 -0800125 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -0800126 EXPECT_CALL(mockMetricsManager, byteSize())
127 .Times(1)
128 .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2)));
129
Yao Chen06dba5d2018-01-26 13:38:16 -0800130 EXPECT_CALL(mockMetricsManager, dropData(_)).Times(1);
David Chend9269e22017-12-05 13:43:51 -0800131
132 // Expect to call the onDumpReport and skip the broadcast.
Yangster-macb0d06282018-01-05 15:44:07 -0800133 p.flushIfNecessaryLocked(1, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -0800134 EXPECT_EQ(0, broadcastCount);
135}
136
David Chen9e6dbbd2018-05-07 17:52:29 -0700137StatsdConfig MakeConfig(bool includeMetric) {
138 StatsdConfig config;
139 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
140
141 if (includeMetric) {
142 auto appCrashMatcher = CreateProcessCrashAtomMatcher();
143 *config.add_atom_matcher() = appCrashMatcher;
144 auto countMetric = config.add_count_metric();
145 countMetric->set_id(StringToId("AppCrashes"));
146 countMetric->set_what(appCrashMatcher.id());
147 countMetric->set_bucket(FIVE_MINUTES);
148 }
149 return config;
150}
151
David Chen35045cb2018-03-23 22:21:47 -0700152TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) {
153 // Setup simple config key corresponding to empty config.
154 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700155 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
dwchen730403e2018-10-29 11:41:56 -0700156 m->updateMap(1, {1, 2}, {1, 2}, {String16("v1"), String16("v2")},
157 {String16("p1"), String16("p2")}, {String16(""), String16("")});
David Chen35045cb2018-03-23 22:21:47 -0700158 sp<AlarmMonitor> anomalyAlarmMonitor;
159 sp<AlarmMonitor> subscriberAlarmMonitor;
160 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700161 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
162 [&broadcastCount](const ConfigKey& key) {
163 broadcastCount++;
164 return true;
165 });
David Chen35045cb2018-03-23 22:21:47 -0700166 ConfigKey key(3, 4);
David Chen9e6dbbd2018-05-07 17:52:29 -0700167 StatsdConfig config = MakeConfig(true);
Yangster-macc04feba2018-04-02 14:37:33 -0700168 p.OnConfigUpdated(0, key, config);
David Chen35045cb2018-03-23 22:21:47 -0700169
170 // Expect to get no metrics, but snapshot specified above in uidmap.
171 vector<uint8_t> bytes;
Bookatzff71cad2018-09-20 17:17:49 -0700172 p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes);
David Chen35045cb2018-03-23 22:21:47 -0700173
174 ConfigMetricsReportList output;
175 output.ParseFromArray(bytes.data(), bytes.size());
176 EXPECT_TRUE(output.reports_size() > 0);
177 auto uidmap = output.reports(0).uid_map();
178 EXPECT_TRUE(uidmap.snapshots_size() > 0);
179 EXPECT_EQ(2, uidmap.snapshots(0).package_info_size());
180}
181
David Chen9e6dbbd2018-05-07 17:52:29 -0700182TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) {
183 // Setup simple config key corresponding to empty config.
184 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700185 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
dwchen730403e2018-10-29 11:41:56 -0700186 m->updateMap(1, {1, 2}, {1, 2}, {String16("v1"), String16("v2")},
187 {String16("p1"), String16("p2")}, {String16(""), String16("")});
David Chen9e6dbbd2018-05-07 17:52:29 -0700188 sp<AlarmMonitor> anomalyAlarmMonitor;
189 sp<AlarmMonitor> subscriberAlarmMonitor;
190 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700191 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
192 [&broadcastCount](const ConfigKey& key) {
193 broadcastCount++;
194 return true;
195 });
David Chen9e6dbbd2018-05-07 17:52:29 -0700196 ConfigKey key(3, 4);
197 StatsdConfig config = MakeConfig(false);
198 p.OnConfigUpdated(0, key, config);
199
200 // Expect to get no metrics, but snapshot specified above in uidmap.
201 vector<uint8_t> bytes;
Bookatzff71cad2018-09-20 17:17:49 -0700202 p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes);
David Chen9e6dbbd2018-05-07 17:52:29 -0700203
204 ConfigMetricsReportList output;
205 output.ParseFromArray(bytes.data(), bytes.size());
206 EXPECT_TRUE(output.reports_size() > 0);
207 EXPECT_FALSE(output.reports(0).has_uid_map());
208}
209
David Chenfaa1af52018-03-30 15:14:04 -0700210TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) {
211 // Setup simple config key corresponding to empty config.
212 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700213 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
David Chenfaa1af52018-03-30 15:14:04 -0700214 sp<AlarmMonitor> anomalyAlarmMonitor;
215 sp<AlarmMonitor> subscriberAlarmMonitor;
216 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700217 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
218 [&broadcastCount](const ConfigKey& key) {
219 broadcastCount++;
220 return true;
221 });
David Chenfaa1af52018-03-30 15:14:04 -0700222 ConfigKey key(3, 4);
223 StatsdConfig config;
224 auto annotation = config.add_annotation();
225 annotation->set_field_int64(1);
226 annotation->set_field_int32(2);
227 config.add_allowed_log_source("AID_ROOT");
228 p.OnConfigUpdated(1, key, config);
229
230 // Expect to get no metrics, but snapshot specified above in uidmap.
231 vector<uint8_t> bytes;
Bookatzff71cad2018-09-20 17:17:49 -0700232 p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes);
David Chenfaa1af52018-03-30 15:14:04 -0700233
234 ConfigMetricsReportList output;
235 output.ParseFromArray(bytes.data(), bytes.size());
236 EXPECT_TRUE(output.reports_size() > 0);
237 auto report = output.reports(0);
238 EXPECT_EQ(1, report.annotation_size());
239 EXPECT_EQ(1, report.annotation(0).field_int64());
240 EXPECT_EQ(2, report.annotation(0).field_int32());
241}
242
Bookatz3e906582018-12-10 17:26:58 -0800243TEST(StatsLogProcessorTest, TestOnDumpReportEraseData) {
244 // Setup a simple config.
245 StatsdConfig config;
246 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
247 auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher();
248 *config.add_atom_matcher() = wakelockAcquireMatcher;
249
250 auto countMetric = config.add_count_metric();
251 countMetric->set_id(123456);
252 countMetric->set_what(wakelockAcquireMatcher.id());
253 countMetric->set_bucket(FIVE_MINUTES);
254
255 ConfigKey cfgKey;
256 sp<StatsLogProcessor> processor = CreateStatsLogProcessor(1, 1, config, cfgKey);
257
258 std::vector<AttributionNodeInternal> attributions1 = {CreateAttribution(111, "App1")};
259 auto event = CreateAcquireWakelockEvent(attributions1, "wl1", 2);
260 processor->OnLogEvent(event.get());
261
262 vector<uint8_t> bytes;
263 ConfigMetricsReportList output;
264
265 // Dump report WITHOUT erasing data.
266 processor->onDumpReport(cfgKey, 3, true, false /* Do NOT erase data. */, ADB_DUMP, &bytes);
267 output.ParseFromArray(bytes.data(), bytes.size());
268 EXPECT_EQ(output.reports_size(), 1);
269 EXPECT_EQ(output.reports(0).metrics_size(), 1);
270 EXPECT_EQ(output.reports(0).metrics(0).count_metrics().data_size(), 1);
271
272 // Dump report WITH erasing data. There should be data since we didn't previously erase it.
273 processor->onDumpReport(cfgKey, 4, true, true /* DO erase data. */, ADB_DUMP, &bytes);
274 output.ParseFromArray(bytes.data(), bytes.size());
275 EXPECT_EQ(output.reports_size(), 1);
276 EXPECT_EQ(output.reports(0).metrics_size(), 1);
277 EXPECT_EQ(output.reports(0).metrics(0).count_metrics().data_size(), 1);
278
279 // Dump report again. There should be no data since we erased it.
280 processor->onDumpReport(cfgKey, 5, true, true /* DO erase data. */, ADB_DUMP, &bytes);
281 output.ParseFromArray(bytes.data(), bytes.size());
Bookatzea20bff2018-12-18 10:07:56 -0800282 // We don't care whether statsd has a report, as long as it has no count metrics in it.
283 bool noData = output.reports_size() == 0
284 || output.reports(0).metrics_size() == 0
285 || output.reports(0).metrics(0).count_metrics().data_size() == 0;
Bookatz3e906582018-12-10 17:26:58 -0800286 EXPECT_TRUE(noData);
287}
288
David Chend9269e22017-12-05 13:43:51 -0800289#else
290GTEST_LOG_(INFO) << "This test does nothing.\n";
291#endif
292
293} // namespace statsd
294} // namespace os
Yao Chen288c6002017-12-12 13:43:18 -0800295} // namespace android