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 "src/metrics/DurationMetricProducer.h"
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <stdio.h>
20
21 #include <set>
22 #include <unordered_map>
23 #include <vector>
24
25 #include "metrics_test_helper.h"
26 #include "src/condition/ConditionWizard.h"
27 #include "src/stats_log_util.h"
28 #include "stats_event.h"
29 #include "tests/statsd_test_util.h"
30
31 using namespace android::os::statsd;
32 using namespace testing;
33 using android::sp;
34 using std::set;
35 using std::unordered_map;
36 using std::vector;
37
38 #ifdef __ANDROID__
39
40 namespace android {
41 namespace os {
42 namespace statsd {
43
44
45 namespace {
46
47 const ConfigKey kConfigKey(0, 12345);
48 const uint64_t protoHash = 0x1234567890;
makeLogEvent(LogEvent * logEvent,int64_t timestampNs,int atomId)49 void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId) {
50 AStatsEvent* statsEvent = AStatsEvent_obtain();
51 AStatsEvent_setAtomId(statsEvent, atomId);
52 AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
53
54 parseStatsEventToLogEvent(statsEvent, logEvent);
55 }
56
57 } // namespace
58
59 // Setup for parameterized tests.
60 class DurationMetricProducerTest_PartialBucket : public TestWithParam<BucketSplitEvent> {};
61
62 INSTANTIATE_TEST_SUITE_P(DurationMetricProducerTest_PartialBucket,
63 DurationMetricProducerTest_PartialBucket,
64 testing::Values(APP_UPGRADE, BOOT_COMPLETE));
65
TEST(DurationMetricTrackerTest,TestFirstBucket)66 TEST(DurationMetricTrackerTest, TestFirstBucket) {
67 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
68 DurationMetric metric;
69 metric.set_id(1);
70 metric.set_bucket(ONE_MINUTE);
71 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
72
73 FieldMatcher dimensions;
74 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
75
76 DurationMetricProducer durationProducer(
77 kConfigKey, metric, -1 /*no condition*/, {}, -1 /*what index not needed*/,
78 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
79 wizard, protoHash, dimensions, 5, 600 * NS_PER_SEC + NS_PER_SEC / 2, provider);
80
81 EXPECT_EQ(600500000000, durationProducer.mCurrentBucketStartTimeNs);
82 EXPECT_EQ(10, durationProducer.mCurrentBucketNum);
83 EXPECT_EQ(660000000005, durationProducer.getCurrentBucketEndTimeNs());
84 }
85
TEST(DurationMetricTrackerTest,TestNoCondition)86 TEST(DurationMetricTrackerTest, TestNoCondition) {
87 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
88 int64_t bucketStartTimeNs = 10000000000;
89 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
90
91 DurationMetric metric;
92 metric.set_id(1);
93 metric.set_bucket(ONE_MINUTE);
94 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
95
96 int tagId = 1;
97 LogEvent event1(/*uid=*/0, /*pid=*/0);
98 makeLogEvent(&event1, bucketStartTimeNs + 1, tagId);
99 LogEvent event2(/*uid=*/0, /*pid=*/0);
100 makeLogEvent(&event2, bucketStartTimeNs + bucketSizeNs + 2, tagId);
101
102 FieldMatcher dimensions;
103 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
104
105 DurationMetricProducer durationProducer(
106 kConfigKey, metric, -1 /*no condition*/, {}, -1 /*what index not needed*/,
107 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
108 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
109
110 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
111 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
112 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1);
113 ASSERT_EQ(1UL, durationProducer.mPastBuckets.size());
114 EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) !=
115 durationProducer.mPastBuckets.end());
116 const auto& buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
117 ASSERT_EQ(2UL, buckets.size());
118 EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs);
119 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs);
120 EXPECT_EQ(bucketSizeNs - 1LL, buckets[0].mDuration);
121 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketStartNs);
122 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[1].mBucketEndNs);
123 EXPECT_EQ(2LL, buckets[1].mDuration);
124 }
125
TEST(DurationMetricTrackerTest,TestNonSlicedCondition)126 TEST(DurationMetricTrackerTest, TestNonSlicedCondition) {
127 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
128 int64_t bucketStartTimeNs = 10000000000;
129 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
130
131 DurationMetric metric;
132 metric.set_id(1);
133 metric.set_bucket(ONE_MINUTE);
134 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
135
136 int tagId = 1;
137 LogEvent event1(/*uid=*/0, /*pid=*/0);
138 makeLogEvent(&event1, bucketStartTimeNs + 1, tagId);
139 LogEvent event2(/*uid=*/0, /*pid=*/0);
140 makeLogEvent(&event2, bucketStartTimeNs + 2, tagId);
141 LogEvent event3(/*uid=*/0, /*pid=*/0);
142 makeLogEvent(&event3, bucketStartTimeNs + bucketSizeNs + 1, tagId);
143 LogEvent event4(/*uid=*/0, /*pid=*/0);
144 makeLogEvent(&event4, bucketStartTimeNs + bucketSizeNs + 3, tagId);
145
146 FieldMatcher dimensions;
147 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
148
149 DurationMetricProducer durationProducer(
150 kConfigKey, metric, 0 /* condition index */, {ConditionState::kUnknown},
151 -1 /*what index not needed*/, 1 /* start index */, 2 /* stop index */,
152 3 /* stop_all index */, false /*nesting*/, wizard, protoHash, dimensions,
153 bucketStartTimeNs, bucketStartTimeNs, provider);
154 durationProducer.mCondition = ConditionState::kFalse;
155
156 assertConditionTimer(durationProducer.mConditionTimer, false, 0, 0);
157 EXPECT_FALSE(durationProducer.mCondition);
158 EXPECT_FALSE(durationProducer.isConditionSliced());
159
160 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
161 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
162 durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1);
163 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
164
165 int64_t conditionStartTimeNs = bucketStartTimeNs + bucketSizeNs + 2;
166 int64_t bucket2EndTimeNs = bucketStartTimeNs + 2 * bucketSizeNs;
167 durationProducer.onMatchedLogEvent(1 /* start index*/, event3);
168 durationProducer.onConditionChanged(true /* condition */, conditionStartTimeNs);
169 assertConditionTimer(durationProducer.mConditionTimer, true, 0, conditionStartTimeNs);
170 durationProducer.onMatchedLogEvent(2 /* stop index*/, event4);
171 durationProducer.flushIfNeededLocked(bucket2EndTimeNs + 1);
172 assertConditionTimer(durationProducer.mConditionTimer, true, 0, bucket2EndTimeNs,
173 /*currentBucketStartDelayNs=*/1);
174 ASSERT_EQ(1UL, durationProducer.mPastBuckets.size());
175 EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) !=
176 durationProducer.mPastBuckets.end());
177 const auto& buckets2 = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
178 ASSERT_EQ(1UL, buckets2.size());
179 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets2[0].mBucketStartNs);
180 EXPECT_EQ(bucket2EndTimeNs, buckets2[0].mBucketEndNs);
181 EXPECT_EQ(1LL, buckets2[0].mDuration);
182 EXPECT_EQ(bucket2EndTimeNs - conditionStartTimeNs, buckets2[0].mConditionTrueNs);
183 }
184
TEST(DurationMetricTrackerTest,TestNonSlicedConditionUnknownState)185 TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState) {
186 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
187 int64_t bucketStartTimeNs = 10000000000;
188 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
189
190 DurationMetric metric;
191 metric.set_id(1);
192 metric.set_bucket(ONE_MINUTE);
193 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
194
195 int tagId = 1;
196 LogEvent event1(/*uid=*/0, /*pid=*/0);
197 makeLogEvent(&event1, bucketStartTimeNs + 1, tagId);
198 LogEvent event2(/*uid=*/0, /*pid=*/0);
199 makeLogEvent(&event2, bucketStartTimeNs + 2, tagId);
200 LogEvent event3(/*uid=*/0, /*pid=*/0);
201 makeLogEvent(&event3, bucketStartTimeNs + bucketSizeNs + 1, tagId);
202 LogEvent event4(/*uid=*/0, /*pid=*/0);
203 makeLogEvent(&event4, bucketStartTimeNs + bucketSizeNs + 3, tagId);
204
205 FieldMatcher dimensions;
206 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
207
208 DurationMetricProducer durationProducer(
209 kConfigKey, metric, 0 /* condition index */, {ConditionState::kUnknown},
210 -1 /*what index not needed*/, 1 /* start index */, 2 /* stop index */,
211 3 /* stop_all index */, false /*nesting*/, wizard, protoHash, dimensions,
212 bucketStartTimeNs, bucketStartTimeNs, provider);
213
214 EXPECT_EQ(ConditionState::kUnknown, durationProducer.mCondition);
215 EXPECT_FALSE(durationProducer.isConditionSliced());
216
217 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
218 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
219 durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1);
220 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
221
222 durationProducer.onMatchedLogEvent(1 /* start index*/, event3);
223 durationProducer.onConditionChanged(true /* condition */, bucketStartTimeNs + bucketSizeNs + 2);
224 durationProducer.onMatchedLogEvent(2 /* stop index*/, event4);
225 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1);
226 ASSERT_EQ(1UL, durationProducer.mPastBuckets.size());
227 const auto& buckets2 = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
228 ASSERT_EQ(1UL, buckets2.size());
229 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets2[0].mBucketStartNs);
230 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets2[0].mBucketEndNs);
231 EXPECT_EQ(1LL, buckets2[0].mDuration);
232 }
233
TEST_P(DurationMetricProducerTest_PartialBucket,TestSumDuration)234 TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDuration) {
235 /**
236 * The duration starts from the first bucket, through the two partial buckets (10-70sec),
237 * another bucket, and ends at the beginning of the next full bucket.
238 * Expected buckets:
239 * - [10,25]: 14 secs
240 * - [25,70]: All 45 secs
241 * - [70,130]: All 60 secs
242 * - [130, 210]: Only 5 secs (event ended at 135sec)
243 */
244 int64_t bucketStartTimeNs = 10000000000;
245 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
246 int tagId = 1;
247
248 DurationMetric metric;
249 metric.set_id(1);
250 metric.set_bucket(ONE_MINUTE);
251 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
252 metric.set_split_bucket_for_app_upgrade(true);
253 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
254 FieldMatcher dimensions;
255 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
256
257 DurationMetricProducer durationProducer(
258 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
259 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
260 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
261
262 int64_t startTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC;
263 LogEvent event1(/*uid=*/0, /*pid=*/0);
264 makeLogEvent(&event1, startTimeNs, tagId);
265 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
266 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
267 EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs);
268
269 int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC;
270 switch (GetParam()) {
271 case APP_UPGRADE:
272 durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs);
273 break;
274 case BOOT_COMPLETE:
275 durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs);
276 break;
277 }
278 ASSERT_EQ(1UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
279 std::vector<DurationBucket> buckets =
280 durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
281 EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs);
282 EXPECT_EQ(partialBucketSplitTimeNs, buckets[0].mBucketEndNs);
283 EXPECT_EQ(partialBucketSplitTimeNs - startTimeNs, buckets[0].mDuration);
284 EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs);
285 EXPECT_EQ(0, durationProducer.getCurrentBucketNum());
286
287 // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket.
288 int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC;
289 LogEvent event2(/*uid=*/0, /*pid=*/0);
290 makeLogEvent(&event2, endTimeNs, tagId);
291 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
292 buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
293 ASSERT_EQ(3UL, buckets.size());
294 EXPECT_EQ(partialBucketSplitTimeNs, buckets[1].mBucketStartNs);
295 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketEndNs);
296 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - partialBucketSplitTimeNs, buckets[1].mDuration);
297 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[2].mBucketStartNs);
298 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[2].mBucketEndNs);
299 EXPECT_EQ(bucketSizeNs, buckets[2].mDuration);
300 }
301
TEST_P(DurationMetricProducerTest_PartialBucket,TestSumDurationWithSplitInFollowingBucket)302 TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDurationWithSplitInFollowingBucket) {
303 /**
304 * Expected buckets (start at 11s, upgrade at 75s, end at 135s):
305 * - [10,70]: 59 secs
306 * - [70,75]: 5 sec
307 * - [75,130]: 55 secs
308 */
309 int64_t bucketStartTimeNs = 10000000000;
310 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
311 int tagId = 1;
312
313 DurationMetric metric;
314 metric.set_id(1);
315 metric.set_bucket(ONE_MINUTE);
316 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
317 metric.set_split_bucket_for_app_upgrade(true);
318 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
319 FieldMatcher dimensions;
320 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
321
322 DurationMetricProducer durationProducer(
323 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
324 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
325 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
326
327 int64_t startTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC;
328 LogEvent event1(/*uid=*/0, /*pid=*/0);
329 makeLogEvent(&event1, startTimeNs, tagId);
330 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
331 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
332 EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs);
333
334 int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 65 * NS_PER_SEC;
335 switch (GetParam()) {
336 case APP_UPGRADE:
337 durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs);
338 break;
339 case BOOT_COMPLETE:
340 durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs);
341 break;
342 }
343 ASSERT_EQ(2UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
344 std::vector<DurationBucket> buckets =
345 durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
346 EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs);
347 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs);
348 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - startTimeNs, buckets[0].mDuration);
349 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketStartNs);
350 EXPECT_EQ(partialBucketSplitTimeNs, buckets[1].mBucketEndNs);
351 EXPECT_EQ(partialBucketSplitTimeNs - (bucketStartTimeNs + bucketSizeNs), buckets[1].mDuration);
352 EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs);
353 EXPECT_EQ(1, durationProducer.getCurrentBucketNum());
354
355 // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket.
356 int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC;
357 LogEvent event2(/*uid=*/0, /*pid=*/0);
358 makeLogEvent(&event2, endTimeNs, tagId);
359 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
360 buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
361 ASSERT_EQ(3UL, buckets.size());
362 EXPECT_EQ(partialBucketSplitTimeNs, buckets[2].mBucketStartNs);
363 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[2].mBucketEndNs);
364 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs - partialBucketSplitTimeNs,
365 buckets[2].mDuration);
366 }
367
TEST_P(DurationMetricProducerTest_PartialBucket,TestSumDurationAnomaly)368 TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDurationAnomaly) {
369 sp<AlarmMonitor> alarmMonitor;
370 int64_t bucketStartTimeNs = 10000000000;
371 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
372 int tagId = 1;
373
374 // Setup metric with alert.
375 DurationMetric metric;
376 metric.set_id(1);
377 metric.set_bucket(ONE_MINUTE);
378 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
379 metric.set_split_bucket_for_app_upgrade(true);
380 Alert alert;
381 alert.set_num_buckets(3);
382 alert.set_trigger_if_sum_gt(2);
383
384 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
385 FieldMatcher dimensions;
386 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
387
388 DurationMetricProducer durationProducer(
389 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
390 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
391 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
392
393 sp<AnomalyTracker> anomalyTracker =
394 durationProducer.addAnomalyTracker(alert, alarmMonitor, UPDATE_NEW, bucketStartTimeNs);
395 EXPECT_TRUE(anomalyTracker != nullptr);
396
397 int64_t startTimeNs = bucketStartTimeNs + 1;
398 LogEvent event1(/*uid=*/0, /*pid=*/0);
399 makeLogEvent(&event1, startTimeNs, tagId);
400 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
401
402 int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC;
403 switch (GetParam()) {
404 case APP_UPGRADE:
405 durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs);
406 break;
407 case BOOT_COMPLETE:
408 durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs);
409 break;
410 }
411
412 // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket.
413 int64_t endTimeNs = startTimeNs + 65 * NS_PER_SEC;
414 LogEvent event2(/*uid=*/0, /*pid=*/0);
415 makeLogEvent(&event2, endTimeNs, tagId);
416 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
417
418 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - startTimeNs,
419 anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY));
420 }
421
TEST_P(DurationMetricProducerTest_PartialBucket,TestMaxDuration)422 TEST_P(DurationMetricProducerTest_PartialBucket, TestMaxDuration) {
423 int64_t bucketStartTimeNs = 10000000000;
424 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
425 int tagId = 1;
426
427 DurationMetric metric;
428 metric.set_id(1);
429 metric.set_bucket(ONE_MINUTE);
430 metric.set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
431 metric.set_split_bucket_for_app_upgrade(true);
432
433 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
434 FieldMatcher dimensions;
435 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
436
437 DurationMetricProducer durationProducer(
438 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
439 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
440 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
441
442 int64_t startTimeNs = bucketStartTimeNs + 1;
443 LogEvent event1(/*uid=*/0, /*pid=*/0);
444 makeLogEvent(&event1, startTimeNs, tagId);
445 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
446 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
447 EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs);
448
449 int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC;
450 switch (GetParam()) {
451 case APP_UPGRADE:
452 durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs);
453 break;
454 case BOOT_COMPLETE:
455 durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs);
456 break;
457 }
458 ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
459 EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs);
460 EXPECT_EQ(0, durationProducer.getCurrentBucketNum());
461
462 // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket.
463 int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC;
464 LogEvent event2(/*uid=*/0, /*pid=*/0);
465 makeLogEvent(&event2, endTimeNs, tagId);
466 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
467 ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
468
469 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 3 * bucketSizeNs + 1);
470 std::vector<DurationBucket> buckets =
471 durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
472 ASSERT_EQ(1UL, buckets.size());
473 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[0].mBucketStartNs);
474 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, buckets[0].mBucketEndNs);
475 EXPECT_EQ(endTimeNs - startTimeNs, buckets[0].mDuration);
476 }
477
TEST_P(DurationMetricProducerTest_PartialBucket,TestMaxDurationWithSplitInNextBucket)478 TEST_P(DurationMetricProducerTest_PartialBucket, TestMaxDurationWithSplitInNextBucket) {
479 int64_t bucketStartTimeNs = 10000000000;
480 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
481 int tagId = 1;
482
483 DurationMetric metric;
484 metric.set_id(1);
485 metric.set_bucket(ONE_MINUTE);
486 metric.set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
487 metric.set_split_bucket_for_app_upgrade(true);
488
489 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
490 FieldMatcher dimensions;
491 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
492
493 DurationMetricProducer durationProducer(
494 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
495 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
496 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
497
498 int64_t startTimeNs = bucketStartTimeNs + 1;
499 LogEvent event1(/*uid=*/0, /*pid=*/0);
500 makeLogEvent(&event1, startTimeNs, tagId);
501 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
502 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
503 EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs);
504
505 int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 65 * NS_PER_SEC;
506 switch (GetParam()) {
507 case APP_UPGRADE:
508 durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs);
509 break;
510 case BOOT_COMPLETE:
511 durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs);
512 break;
513 }
514 ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
515 EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs);
516 EXPECT_EQ(1, durationProducer.getCurrentBucketNum());
517
518 // Stop occurs in the same partial bucket as created for the app upgrade.
519 int64_t endTimeNs = startTimeNs + 115 * NS_PER_SEC;
520 LogEvent event2(/*uid=*/0, /*pid=*/0);
521 makeLogEvent(&event2, endTimeNs, tagId);
522 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
523 ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
524 EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs);
525
526 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1);
527 std::vector<DurationBucket> buckets =
528 durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY];
529 ASSERT_EQ(1UL, buckets.size());
530 EXPECT_EQ(partialBucketSplitTimeNs, buckets[0].mBucketStartNs);
531 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[0].mBucketEndNs);
532 EXPECT_EQ(endTimeNs - startTimeNs, buckets[0].mDuration);
533 }
534
TEST(DurationMetricProducerTest,TestSumDurationAppUpgradeSplitDisabled)535 TEST(DurationMetricProducerTest, TestSumDurationAppUpgradeSplitDisabled) {
536 /**
537 * The duration starts from the first bucket, through one full bucket (10-70sec).
538 * The app upgrade should not split a partial bucket.
539 * Expected buckets:
540 * - [10,70]: All 60 secs
541 * - [70, 75]: Only 5 secs (event ended at 75sec)
542 */
543 int64_t bucketStartTimeNs = 10000000000;
544 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
545 int tagId = 1;
546
547 DurationMetric metric;
548 metric.set_id(1);
549 metric.set_bucket(ONE_MINUTE);
550 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
551 metric.set_split_bucket_for_app_upgrade(false);
552 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
553 FieldMatcher dimensions;
554 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
555
556 DurationMetricProducer durationProducer(
557 kConfigKey, metric, -1 /* no condition */, {}, -1 /*what index not needed*/,
558 1 /* start index */, 2 /* stop index */, 3 /* stop_all index */, false /*nesting*/,
559 wizard, protoHash, dimensions, bucketStartTimeNs, bucketStartTimeNs, provider);
560
561 int64_t startTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC;
562 LogEvent event1(/*uid=*/0, /*pid=*/0);
563 makeLogEvent(&event1, startTimeNs, tagId);
564 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
565 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
566 EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs);
567
568 int64_t appUpgradeTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC;
569 durationProducer.notifyAppUpgrade(appUpgradeTimeNs);
570
571 ASSERT_EQ(0UL, durationProducer.mPastBuckets.size());
572 EXPECT_EQ(0, durationProducer.getCurrentBucketNum());
573
574 // We skip ahead one bucket, so we fill in one full bucket and expect 0 partial buckets.
575 int64_t endTimeNs = startTimeNs + 65 * NS_PER_SEC;
576 LogEvent event2(/*uid=*/0, /*pid=*/0);
577 makeLogEvent(&event2, endTimeNs, tagId);
578 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
579 ASSERT_EQ(1UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
580 DurationBucket bucket = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0];
581 EXPECT_EQ(bucketStartTimeNs, bucket.mBucketStartNs);
582 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, bucket.mBucketEndNs);
583 EXPECT_EQ(bucketSizeNs - 1 * NS_PER_SEC, bucket.mDuration);
584 EXPECT_EQ(1, durationProducer.getCurrentBucketNum());
585 }
586
TEST(DurationMetricProducerTest,TestClearCurrentSlicedTrackerMapWhenStop)587 TEST(DurationMetricProducerTest, TestClearCurrentSlicedTrackerMapWhenStop) {
588 int64_t bucketStartTimeNs = 10000000000;
589 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL;
590 int tagId = 1;
591
592 DurationMetric metric;
593 metric.set_id(1);
594 metric.set_bucket(ONE_MINUTE);
595 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
596 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
597 FieldMatcher dimensions;
598 sp<MockConfigMetadataProvider> provider = makeMockConfigMetadataProvider(/*enabled=*/false);
599
600 LogEvent event1(/*uid=*/0, /*pid=*/0);
601 makeLogEvent(&event1, bucketStartTimeNs + 50, tagId);
602 LogEvent event2(/*uid=*/0, /*pid=*/0);
603 makeLogEvent(&event2, bucketStartTimeNs + 100, tagId);
604 LogEvent event3(/*uid=*/0, /*pid=*/0);
605 makeLogEvent(&event3, bucketStartTimeNs + 150, tagId);
606 LogEvent event4(/*uid=*/0, /*pid=*/0);
607 makeLogEvent(&event4, bucketStartTimeNs + bucketSizeNs + 5, tagId);
608
609 DurationMetricProducer durationProducer(
610 kConfigKey, metric, 0 /* condition index */, {ConditionState::kUnknown},
611 -1 /*what index not needed*/, 1 /* start index */, 2 /* stop index */,
612 3 /* stop_all index */, false /*nesting*/, wizard, protoHash, dimensions,
613 bucketStartTimeNs, bucketStartTimeNs, provider);
614
615 durationProducer.onConditionChanged(true /* condition */, bucketStartTimeNs + 5);
616 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
617 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
618 durationProducer.onMatchedLogEvent(1 /* start index*/, event3);
619 durationProducer.onConditionChanged(false /* condition */, bucketStartTimeNs + 200);
620 durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1);
621 durationProducer.onMatchedLogEvent(2 /* stop index*/, event4);
622
623 ASSERT_TRUE(durationProducer.mCurrentSlicedDurationTrackerMap.empty());
624 EXPECT_EQ(1UL, durationProducer.mPastBuckets.size());
625 EXPECT_EQ(1, durationProducer.getCurrentBucketNum());
626 }
627
628 } // namespace statsd
629 } // namespace os
630 } // namespace android
631 #else
632 GTEST_LOG_(INFO) << "This test does nothing.\n";
633 #endif
634