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