1 // 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 <gtest/gtest.h>
16
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20
21 #include <vector>
22
23 namespace android {
24 namespace os {
25 namespace statsd {
26
27 #ifdef __ANDROID__
28
29 namespace {
30
CreateStatsdConfig(DurationMetric::AggregationType aggregationType)31 StatsdConfig CreateStatsdConfig(DurationMetric::AggregationType aggregationType) {
32 StatsdConfig config;
33 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
34 *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher();
35 *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher();
36 *config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher();
37 *config.add_atom_matcher() = CreateReleaseWakelockAtomMatcher();
38
39 auto screenIsOffPredicate = CreateScreenIsOffPredicate();
40 *config.add_predicate() = screenIsOffPredicate;
41
42 auto holdingWakelockPredicate = CreateHoldingWakelockPredicate();
43 // The predicate is dimensioning by any attribution node and both by uid and tag.
44 FieldMatcher dimensions = CreateAttributionUidAndTagDimensions(
45 util::WAKELOCK_STATE_CHANGED, {Position::FIRST, Position::LAST});
46 // Also slice by the wakelock tag
47 dimensions.add_child()->set_field(3); // The wakelock tag is set in field 3 of the wakelock.
48 *holdingWakelockPredicate.mutable_simple_predicate()->mutable_dimensions() = dimensions;
49 *config.add_predicate() = holdingWakelockPredicate;
50
51 auto durationMetric = config.add_duration_metric();
52 durationMetric->set_id(StringToId("WakelockDuration"));
53 durationMetric->set_what(holdingWakelockPredicate.id());
54 durationMetric->set_condition(screenIsOffPredicate.id());
55 durationMetric->set_aggregation_type(aggregationType);
56 // The metric is dimensioning by first attribution node and only by uid.
57 *durationMetric->mutable_dimensions_in_what() =
58 CreateAttributionUidDimensions(
59 util::WAKELOCK_STATE_CHANGED, {Position::FIRST});
60 durationMetric->set_bucket(FIVE_MINUTES);
61 return config;
62 }
63
64 std::vector<int> attributionUids1 = {111, 222, 222};
65 std::vector<string> attributionTags1 = {"App1", "GMSCoreModule1", "GMSCoreModule2"};
66
67 std::vector<int> attributionUids2 = {111, 222, 222};
68 std::vector<string> attributionTags2 = {"App2", "GMSCoreModule1", "GMSCoreModule2"};
69
70 /*
71 Events:
72 Screen off is met from (200ns,1 min+500ns].
73 Acquire event for wl1 from 2ns to 1min+2ns
74 Acquire event for wl2 from 1min-10ns to 2min-15ns
75 */
FeedEvents(StatsdConfig config,sp<StatsLogProcessor> processor)76 void FeedEvents(StatsdConfig config, sp<StatsLogProcessor> processor) {
77 uint64_t bucketStartTimeNs = 10000000000;
78 uint64_t bucketSizeNs =
79 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
80
81 auto screenTurnedOnEvent = CreateScreenStateChangedEvent(
82 bucketStartTimeNs + 1, android::view::DisplayStateEnum::DISPLAY_STATE_ON);
83 auto screenTurnedOffEvent = CreateScreenStateChangedEvent(
84 bucketStartTimeNs + 200, android::view::DisplayStateEnum::DISPLAY_STATE_OFF);
85 auto screenTurnedOnEvent2 =
86 CreateScreenStateChangedEvent(bucketStartTimeNs + bucketSizeNs + 500,
87 android::view::DisplayStateEnum::DISPLAY_STATE_ON);
88
89 auto acquireEvent1 = CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
90 attributionTags1, "wl1");
91 auto releaseEvent1 = CreateReleaseWakelockEvent(bucketStartTimeNs + bucketSizeNs + 2,
92 attributionUids1, attributionTags1, "wl1");
93 auto acquireEvent2 = CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs - 10,
94 attributionUids2, attributionTags2, "wl2");
95 auto releaseEvent2 = CreateReleaseWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs - 15,
96 attributionUids2, attributionTags2, "wl2");
97
98 std::vector<std::unique_ptr<LogEvent>> events;
99
100 events.push_back(std::move(screenTurnedOnEvent));
101 events.push_back(std::move(screenTurnedOffEvent));
102 events.push_back(std::move(screenTurnedOnEvent2));
103 events.push_back(std::move(acquireEvent1));
104 events.push_back(std::move(acquireEvent2));
105 events.push_back(std::move(releaseEvent1));
106 events.push_back(std::move(releaseEvent2));
107
108 sortLogEventsByTimestamp(&events);
109
110 for (const auto& event : events) {
111 processor->OnLogEvent(event.get());
112 }
113 }
114
115 } // namespace
116
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForSumDuration1)117 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1) {
118 ConfigKey cfgKey;
119 auto config = CreateStatsdConfig(DurationMetric::SUM);
120 uint64_t bucketStartTimeNs = 10000000000;
121 uint64_t bucketSizeNs =
122 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
123 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
124 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
125 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
126 FeedEvents(config, processor);
127 vector<uint8_t> buffer;
128 ConfigMetricsReportList reports;
129 processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, false, true, ADB_DUMP,
130 FAST, &buffer);
131 EXPECT_TRUE(buffer.size() > 0);
132 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
133 backfillDimensionPath(&reports);
134 backfillStringInReport(&reports);
135 backfillStartEndTimestamp(&reports);
136
137 ASSERT_EQ(reports.reports_size(), 1);
138 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
139 // Only 1 dimension output. The tag dimension in the predicate has been aggregated.
140 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
141
142 auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
143 // Validate dimension value.
144 ValidateAttributionUidDimension(data.dimensions_in_what(),
145 util::WAKELOCK_STATE_CHANGED, 111);
146 // Validate bucket info.
147 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1);
148 data = reports.reports(0).metrics(0).duration_metrics().data(0);
149 // The wakelock holding interval starts from the screen off event and to the end of the 1st
150 // bucket.
151 EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), bucketSizeNs - 200);
152 }
153
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForSumDuration2)154 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2) {
155 ConfigKey cfgKey;
156 auto config = CreateStatsdConfig(DurationMetric::SUM);
157 uint64_t bucketStartTimeNs = 10000000000;
158 uint64_t bucketSizeNs =
159 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
160 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
161 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
162 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
163 FeedEvents(config, processor);
164 vector<uint8_t> buffer;
165 ConfigMetricsReportList reports;
166 processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, false, true, ADB_DUMP,
167 FAST, &buffer);
168 EXPECT_TRUE(buffer.size() > 0);
169 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
170 backfillDimensionPath(&reports);
171 backfillStringInReport(&reports);
172 backfillStartEndTimestamp(&reports);
173 ASSERT_EQ(reports.reports_size(), 1);
174 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
175 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
176 // Dump the report after the end of 2nd bucket.
177 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 2);
178 auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
179 // Validate dimension value.
180 ValidateAttributionUidDimension(data.dimensions_in_what(),
181 util::WAKELOCK_STATE_CHANGED, 111);
182 // Two output buckets.
183 // The wakelock holding interval in the 1st bucket starts from the screen off event and to
184 // the end of the 1st bucket.
185 EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(),
186 bucketStartTimeNs + bucketSizeNs - (bucketStartTimeNs + 200));
187 // The wakelock holding interval in the 2nd bucket starts at the beginning of the bucket and
188 // ends at the second screen on event.
189 EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 500UL);
190 }
191
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForSumDuration3)192 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3) {
193 ConfigKey cfgKey;
194 auto config = CreateStatsdConfig(DurationMetric::SUM);
195 uint64_t bucketStartTimeNs = 10000000000;
196 uint64_t bucketSizeNs =
197 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
198 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
199 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
200 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
201 FeedEvents(config, processor);
202 vector<uint8_t> buffer;
203 ConfigMetricsReportList reports;
204
205 std::vector<std::unique_ptr<LogEvent>> events;
206 events.push_back(
207 CreateScreenStateChangedEvent(bucketStartTimeNs + 2 * bucketSizeNs + 90,
208 android::view::DisplayStateEnum::DISPLAY_STATE_OFF));
209 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
210 attributionUids1, attributionTags1, "wl3"));
211 events.push_back(CreateReleaseWakelockEvent(bucketStartTimeNs + 5 * bucketSizeNs + 100,
212 attributionUids1, attributionTags1, "wl3"));
213 sortLogEventsByTimestamp(&events);
214 for (const auto& event : events) {
215 processor->OnLogEvent(event.get());
216 }
217
218 processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, false, true, ADB_DUMP,
219 FAST, &buffer);
220 EXPECT_TRUE(buffer.size() > 0);
221 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
222 backfillDimensionPath(&reports);
223 backfillStringInReport(&reports);
224 backfillStartEndTimestamp(&reports);
225 ASSERT_EQ(reports.reports_size(), 1);
226 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
227 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
228 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 6);
229 auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
230 ValidateAttributionUidDimension(data.dimensions_in_what(),
231 util::WAKELOCK_STATE_CHANGED, 111);
232 // The last wakelock holding spans 4 buckets.
233 EXPECT_EQ((unsigned long long)data.bucket_info(2).duration_nanos(), bucketSizeNs - 100);
234 EXPECT_EQ((unsigned long long)data.bucket_info(3).duration_nanos(), bucketSizeNs);
235 EXPECT_EQ((unsigned long long)data.bucket_info(4).duration_nanos(), bucketSizeNs);
236 EXPECT_EQ((unsigned long long)data.bucket_info(5).duration_nanos(), 100UL);
237 }
238
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForMaxDuration1)239 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1) {
240 ConfigKey cfgKey;
241 auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE);
242 uint64_t bucketStartTimeNs = 10000000000;
243 uint64_t bucketSizeNs =
244 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
245 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
246 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
247 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
248 FeedEvents(config, processor);
249 ConfigMetricsReportList reports;
250 vector<uint8_t> buffer;
251 processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs - 1, false, true, ADB_DUMP,
252 FAST, &buffer);
253 EXPECT_TRUE(buffer.size() > 0);
254
255 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
256 backfillDimensionPath(&reports);
257 backfillStringInReport(&reports);
258 backfillStartEndTimestamp(&reports);
259
260 ASSERT_EQ(reports.reports_size(), 1);
261
262 // When using ProtoOutputStream, if nothing written to a sub msg, it won't be treated as
263 // one. It was previsouly 1 because we had a fake onDumpReport which calls add_metric() by
264 // itself.
265 ASSERT_EQ(1, reports.reports(0).metrics_size());
266 ASSERT_EQ(0, reports.reports(0).metrics(0).duration_metrics().data_size());
267 }
268
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForMaxDuration2)269 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2) {
270 ConfigKey cfgKey;
271 auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE);
272 uint64_t bucketStartTimeNs = 10000000000;
273 uint64_t bucketSizeNs =
274 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
275 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
276 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
277 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
278 FeedEvents(config, processor);
279 ConfigMetricsReportList reports;
280 vector<uint8_t> buffer;
281 processor->onDumpReport(cfgKey, bucketStartTimeNs + 2 * bucketSizeNs + 1, false, true, ADB_DUMP,
282 FAST, &buffer);
283 EXPECT_TRUE(buffer.size() > 0);
284 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
285 backfillDimensionPath(&reports);
286 backfillStringInReport(&reports);
287 backfillStartEndTimestamp(&reports);
288 ASSERT_EQ(reports.reports_size(), 1);
289 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
290 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
291 // Dump the report after the end of 2nd bucket. One dimension with one bucket.
292 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 1);
293 auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
294 // Validate dimension value.
295 ValidateAttributionUidDimension(data.dimensions_in_what(),
296 util::WAKELOCK_STATE_CHANGED, 111);
297 // The max is acquire event for wl1 to screen off start.
298 EXPECT_EQ((unsigned long long)data.bucket_info(0).duration_nanos(), bucketSizeNs + 2 - 200);
299 }
300
TEST(WakelockDurationE2eTest,TestAggregatedPredicateDimensionsForMaxDuration3)301 TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3) {
302 ConfigKey cfgKey;
303 auto config = CreateStatsdConfig(DurationMetric::MAX_SPARSE);
304 uint64_t bucketStartTimeNs = 10000000000;
305 uint64_t bucketSizeNs =
306 TimeUnitToBucketSizeInMillis(config.duration_metric(0).bucket()) * 1000000LL;
307 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
308 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
309 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
310 FeedEvents(config, processor);
311 ConfigMetricsReportList reports;
312 vector<uint8_t> buffer;
313
314 std::vector<std::unique_ptr<LogEvent>> events;
315 events.push_back(
316 CreateScreenStateChangedEvent(bucketStartTimeNs + 2 * bucketSizeNs + 90,
317 android::view::DisplayStateEnum::DISPLAY_STATE_OFF));
318 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
319 attributionUids1, attributionTags1, "wl3"));
320 events.push_back(CreateReleaseWakelockEvent(bucketStartTimeNs + 5 * bucketSizeNs + 100,
321 attributionUids1, attributionTags1, "wl3"));
322 sortLogEventsByTimestamp(&events);
323 for (const auto& event : events) {
324 processor->OnLogEvent(event.get());
325 }
326
327 processor->onDumpReport(cfgKey, bucketStartTimeNs + 6 * bucketSizeNs + 1, false, true, ADB_DUMP,
328 FAST, &buffer);
329 EXPECT_TRUE(buffer.size() > 0);
330 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
331 backfillDimensionPath(&reports);
332 backfillStringInReport(&reports);
333 backfillStartEndTimestamp(&reports);
334 ASSERT_EQ(reports.reports_size(), 1);
335 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
336 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data_size(), 1);
337 ASSERT_EQ(reports.reports(0).metrics(0).duration_metrics().data(0).bucket_info_size(), 2);
338 auto data = reports.reports(0).metrics(0).duration_metrics().data(0);
339 ValidateAttributionUidDimension(data.dimensions_in_what(),
340 util::WAKELOCK_STATE_CHANGED, 111);
341 // The last wakelock holding spans 4 buckets.
342 EXPECT_EQ((unsigned long long)data.bucket_info(1).duration_nanos(), 3 * bucketSizeNs);
343 EXPECT_EQ((unsigned long long)data.bucket_info(1).start_bucket_elapsed_nanos(),
344 bucketStartTimeNs + 5 * bucketSizeNs);
345 EXPECT_EQ((unsigned long long)data.bucket_info(1).end_bucket_elapsed_nanos(),
346 bucketStartTimeNs + 6 * bucketSizeNs);
347 }
348
349 #else
350 GTEST_LOG_(INFO) << "This test does nothing.\n";
351 #endif
352
353 } // namespace statsd
354 } // namespace os
355 } // namespace android
356