blob: 8864252bcf4bd02b615494e13dfdf1be10a645fa [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;
Bookatzff71cad2018-09-20 17:17:49 -0700171 p.onDumpReport(key, 1, false, true, 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;
Bookatzff71cad2018-09-20 17:17:49 -0700200 p.onDumpReport(key, 1, false, true, 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;
Bookatzff71cad2018-09-20 17:17:49 -0700230 p.onDumpReport(key, 1, false, true, 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
David Chend9269e22017-12-05 13:43:51 -0800241#else
242GTEST_LOG_(INFO) << "This test does nothing.\n";
243#endif
244
245} // namespace statsd
246} // namespace os
Yao Chen288c6002017-12-12 13:43:18 -0800247} // namespace android