blob: ecc57f5c4b26f313a83b54e53206b3a04dfa37e5 [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();
David Chenbd125272018-04-04 19:02:50 -0700156 m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")});
David Chen35045cb2018-03-23 22:21:47 -0700157 sp<AlarmMonitor> anomalyAlarmMonitor;
158 sp<AlarmMonitor> subscriberAlarmMonitor;
159 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700160 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
161 [&broadcastCount](const ConfigKey& key) {
162 broadcastCount++;
163 return true;
164 });
David Chen35045cb2018-03-23 22:21:47 -0700165 ConfigKey key(3, 4);
David Chen9e6dbbd2018-05-07 17:52:29 -0700166 StatsdConfig config = MakeConfig(true);
Yangster-macc04feba2018-04-02 14:37:33 -0700167 p.OnConfigUpdated(0, key, config);
David Chen35045cb2018-03-23 22:21:47 -0700168
169 // Expect to get no metrics, but snapshot specified above in uidmap.
170 vector<uint8_t> bytes;
David Chen56ae0d92018-05-11 16:00:22 -0700171 p.onDumpReport(key, 1, false, ADB_DUMP, &bytes);
David Chen35045cb2018-03-23 22:21:47 -0700172
173 ConfigMetricsReportList output;
174 output.ParseFromArray(bytes.data(), bytes.size());
175 EXPECT_TRUE(output.reports_size() > 0);
176 auto uidmap = output.reports(0).uid_map();
177 EXPECT_TRUE(uidmap.snapshots_size() > 0);
178 EXPECT_EQ(2, uidmap.snapshots(0).package_info_size());
179}
180
David Chen9e6dbbd2018-05-07 17:52:29 -0700181TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) {
182 // Setup simple config key corresponding to empty config.
183 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700184 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
David Chen9e6dbbd2018-05-07 17:52:29 -0700185 m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")});
186 sp<AlarmMonitor> anomalyAlarmMonitor;
187 sp<AlarmMonitor> subscriberAlarmMonitor;
188 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700189 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
190 [&broadcastCount](const ConfigKey& key) {
191 broadcastCount++;
192 return true;
193 });
David Chen9e6dbbd2018-05-07 17:52:29 -0700194 ConfigKey key(3, 4);
195 StatsdConfig config = MakeConfig(false);
196 p.OnConfigUpdated(0, key, config);
197
198 // Expect to get no metrics, but snapshot specified above in uidmap.
199 vector<uint8_t> bytes;
David Chen56ae0d92018-05-11 16:00:22 -0700200 p.onDumpReport(key, 1, false, ADB_DUMP, &bytes);
David Chen9e6dbbd2018-05-07 17:52:29 -0700201
202 ConfigMetricsReportList output;
203 output.ParseFromArray(bytes.data(), bytes.size());
204 EXPECT_TRUE(output.reports_size() > 0);
205 EXPECT_FALSE(output.reports(0).has_uid_map());
206}
207
David Chenfaa1af52018-03-30 15:14:04 -0700208TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) {
209 // Setup simple config key corresponding to empty config.
210 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700211 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
David Chenfaa1af52018-03-30 15:14:04 -0700212 sp<AlarmMonitor> anomalyAlarmMonitor;
213 sp<AlarmMonitor> subscriberAlarmMonitor;
214 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700215 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
216 [&broadcastCount](const ConfigKey& key) {
217 broadcastCount++;
218 return true;
219 });
David Chenfaa1af52018-03-30 15:14:04 -0700220 ConfigKey key(3, 4);
221 StatsdConfig config;
222 auto annotation = config.add_annotation();
223 annotation->set_field_int64(1);
224 annotation->set_field_int32(2);
225 config.add_allowed_log_source("AID_ROOT");
226 p.OnConfigUpdated(1, key, config);
227
228 // Expect to get no metrics, but snapshot specified above in uidmap.
229 vector<uint8_t> bytes;
David Chen56ae0d92018-05-11 16:00:22 -0700230 p.onDumpReport(key, 1, false, ADB_DUMP, &bytes);
David Chenfaa1af52018-03-30 15:14:04 -0700231
232 ConfigMetricsReportList output;
233 output.ParseFromArray(bytes.data(), bytes.size());
234 EXPECT_TRUE(output.reports_size() > 0);
235 auto report = output.reports(0);
236 EXPECT_EQ(1, report.annotation_size());
237 EXPECT_EQ(1, report.annotation(0).field_int64());
238 EXPECT_EQ(2, report.annotation(0).field_int32());
239}
240
Yao Chen163d2602018-04-10 10:39:53 -0700241TEST(StatsLogProcessorTest, TestOutOfOrderLogs) {
242 // Setup simple config key corresponding to empty config.
243 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -0700244 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yao Chen163d2602018-04-10 10:39:53 -0700245 sp<AlarmMonitor> anomalyAlarmMonitor;
246 sp<AlarmMonitor> subscriberAlarmMonitor;
247 int broadcastCount = 0;
Chenjie Yue2219202018-06-08 10:07:51 -0700248 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
249 [&broadcastCount](const ConfigKey& key) {
250 broadcastCount++;
251 return true;
252 });
Yao Chen163d2602018-04-10 10:39:53 -0700253
254 LogEvent event1(0, 1 /*logd timestamp*/, 1001 /*elapsedRealtime*/);
255 event1.init();
256
257 LogEvent event2(0, 2, 1002);
258 event2.init();
259
260 LogEvent event3(0, 3, 1005);
261 event3.init();
262
263 LogEvent event4(0, 4, 1004);
264 event4.init();
265
266 // <----- Reconnection happens
267
268 LogEvent event5(0, 5, 999);
269 event5.init();
270
271 LogEvent event6(0, 6, 2000);
272 event6.init();
273
274 // <----- Reconnection happens
275
276 LogEvent event7(0, 7, 3000);
277 event7.init();
278
279 // first event ever
280 p.OnLogEvent(&event1, true);
281 EXPECT_EQ(1UL, p.mLogCount);
282 EXPECT_EQ(1001LL, p.mLargestTimestampSeen);
283 EXPECT_EQ(1001LL, p.mLastTimestampSeen);
284
285 p.OnLogEvent(&event2, false);
286 EXPECT_EQ(2UL, p.mLogCount);
287 EXPECT_EQ(1002LL, p.mLargestTimestampSeen);
288 EXPECT_EQ(1002LL, p.mLastTimestampSeen);
289
290 p.OnLogEvent(&event3, false);
291 EXPECT_EQ(3UL, p.mLogCount);
292 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
293 EXPECT_EQ(1005LL, p.mLastTimestampSeen);
294
295 p.OnLogEvent(&event4, false);
296 EXPECT_EQ(4UL, p.mLogCount);
297 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
298 EXPECT_EQ(1004LL, p.mLastTimestampSeen);
299 EXPECT_FALSE(p.mInReconnection);
300
301 // Reconnect happens, event1 out of buffer. Read event2
302 p.OnLogEvent(&event2, true);
303 EXPECT_EQ(4UL, p.mLogCount);
304 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
305 EXPECT_EQ(1004LL, p.mLastTimestampSeen);
306 EXPECT_TRUE(p.mInReconnection);
307
308 p.OnLogEvent(&event3, false);
309 EXPECT_EQ(4UL, p.mLogCount);
310 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
311 EXPECT_EQ(1004LL, p.mLastTimestampSeen);
312 EXPECT_TRUE(p.mInReconnection);
313
314 p.OnLogEvent(&event4, false);
315 EXPECT_EQ(4UL, p.mLogCount);
316 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
317 EXPECT_EQ(1004LL, p.mLastTimestampSeen);
318 EXPECT_FALSE(p.mInReconnection);
319
320 // Fresh event comes.
321 p.OnLogEvent(&event5, false);
322 EXPECT_EQ(5UL, p.mLogCount);
323 EXPECT_EQ(1005LL, p.mLargestTimestampSeen);
324 EXPECT_EQ(999LL, p.mLastTimestampSeen);
325
326 p.OnLogEvent(&event6, false);
327 EXPECT_EQ(6UL, p.mLogCount);
328 EXPECT_EQ(2000LL, p.mLargestTimestampSeen);
329 EXPECT_EQ(2000LL, p.mLastTimestampSeen);
330
331 // Reconnect happens, read from event4
332 p.OnLogEvent(&event4, true);
333 EXPECT_EQ(6UL, p.mLogCount);
334 EXPECT_EQ(2000LL, p.mLargestTimestampSeen);
335 EXPECT_EQ(2000LL, p.mLastTimestampSeen);
336 EXPECT_TRUE(p.mInReconnection);
337
338 p.OnLogEvent(&event5, false);
339 EXPECT_EQ(6UL, p.mLogCount);
340 EXPECT_EQ(2000LL, p.mLargestTimestampSeen);
341 EXPECT_EQ(2000LL, p.mLastTimestampSeen);
342 EXPECT_TRUE(p.mInReconnection);
343
344 // Before we get out of reconnection state, it reconnects again.
345 p.OnLogEvent(&event5, true);
346 EXPECT_EQ(6UL, p.mLogCount);
347 EXPECT_EQ(2000LL, p.mLargestTimestampSeen);
348 EXPECT_EQ(2000LL, p.mLastTimestampSeen);
349 EXPECT_TRUE(p.mInReconnection);
350
351 p.OnLogEvent(&event6, false);
352 EXPECT_EQ(6UL, p.mLogCount);
353 EXPECT_EQ(2000LL, p.mLargestTimestampSeen);
354 EXPECT_EQ(2000LL, p.mLastTimestampSeen);
355 EXPECT_FALSE(p.mInReconnection);
356 EXPECT_EQ(0, p.mLogLossCount);
357
358 // it reconnects again. All old events are gone. We lose CP.
359 p.OnLogEvent(&event7, true);
360 EXPECT_EQ(7UL, p.mLogCount);
361 EXPECT_EQ(3000LL, p.mLargestTimestampSeen);
362 EXPECT_EQ(3000LL, p.mLastTimestampSeen);
363 EXPECT_EQ(1, p.mLogLossCount);
364 EXPECT_FALSE(p.mInReconnection);
365}
366
David Chend9269e22017-12-05 13:43:51 -0800367#else
368GTEST_LOG_(INFO) << "This test does nothing.\n";
369#endif
370
371} // namespace statsd
372} // namespace os
Yao Chen288c6002017-12-12 13:43:18 -0800373} // namespace android