1 // Copyright (C) 2018 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()31 StatsdConfig CreateStatsdConfig() {
32 StatsdConfig config;
33 auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
34 auto crashMatcher = CreateProcessCrashAtomMatcher();
35 auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
36
37 *config.add_atom_matcher() = saverModeMatcher;
38 *config.add_atom_matcher() = crashMatcher;
39 *config.add_atom_matcher() = screenOnMatcher;
40
41 int64_t metricId = 123456;
42 auto countMetric = config.add_count_metric();
43 countMetric->set_id(metricId);
44 countMetric->set_what(crashMatcher.id());
45 countMetric->set_bucket(FIVE_MINUTES);
46 countMetric->mutable_dimensions_in_what()->set_field(
47 util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
48 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
49
50 auto metric_activation1 = config.add_metric_activation();
51 metric_activation1->set_metric_id(metricId);
52 auto event_activation1 = metric_activation1->add_event_activation();
53 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
54 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
55 auto event_activation2 = metric_activation1->add_event_activation();
56 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
57 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
58
59 return config;
60 }
61
CreateStatsdConfigWithOneDeactivation()62 StatsdConfig CreateStatsdConfigWithOneDeactivation() {
63 StatsdConfig config;
64 auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
65 auto crashMatcher = CreateProcessCrashAtomMatcher();
66 auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
67 auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
68
69 *config.add_atom_matcher() = saverModeMatcher;
70 *config.add_atom_matcher() = crashMatcher;
71 *config.add_atom_matcher() = screenOnMatcher;
72 *config.add_atom_matcher() = brightnessChangedMatcher;
73
74 int64_t metricId = 123456;
75 auto countMetric = config.add_count_metric();
76 countMetric->set_id(metricId);
77 countMetric->set_what(crashMatcher.id());
78 countMetric->set_bucket(FIVE_MINUTES);
79 countMetric->mutable_dimensions_in_what()->set_field(
80 util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
81 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
82
83 auto metric_activation1 = config.add_metric_activation();
84 metric_activation1->set_metric_id(metricId);
85 auto event_activation1 = metric_activation1->add_event_activation();
86 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
87 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
88 event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
89 auto event_activation2 = metric_activation1->add_event_activation();
90 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
91 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
92
93 return config;
94 }
95
CreateStatsdConfigWithTwoDeactivations()96 StatsdConfig CreateStatsdConfigWithTwoDeactivations() {
97 StatsdConfig config;
98 auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
99 auto crashMatcher = CreateProcessCrashAtomMatcher();
100 auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
101 auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
102 auto brightnessChangedMatcher2 = CreateScreenBrightnessChangedAtomMatcher();
103 brightnessChangedMatcher2.set_id(StringToId("ScreenBrightnessChanged2"));
104
105 *config.add_atom_matcher() = saverModeMatcher;
106 *config.add_atom_matcher() = crashMatcher;
107 *config.add_atom_matcher() = screenOnMatcher;
108 *config.add_atom_matcher() = brightnessChangedMatcher;
109 *config.add_atom_matcher() = brightnessChangedMatcher2;
110
111 int64_t metricId = 123456;
112 auto countMetric = config.add_count_metric();
113 countMetric->set_id(metricId);
114 countMetric->set_what(crashMatcher.id());
115 countMetric->set_bucket(FIVE_MINUTES);
116 countMetric->mutable_dimensions_in_what()->set_field(
117 util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
118 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
119
120 auto metric_activation1 = config.add_metric_activation();
121 metric_activation1->set_metric_id(metricId);
122 auto event_activation1 = metric_activation1->add_event_activation();
123 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
124 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
125 event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
126 auto event_activation2 = metric_activation1->add_event_activation();
127 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
128 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
129 event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
130
131 return config;
132 }
133
CreateStatsdConfigWithSameDeactivations()134 StatsdConfig CreateStatsdConfigWithSameDeactivations() {
135 StatsdConfig config;
136 auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
137 auto crashMatcher = CreateProcessCrashAtomMatcher();
138 auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
139 auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
140
141 *config.add_atom_matcher() = saverModeMatcher;
142 *config.add_atom_matcher() = crashMatcher;
143 *config.add_atom_matcher() = screenOnMatcher;
144 *config.add_atom_matcher() = brightnessChangedMatcher;
145
146 int64_t metricId = 123456;
147 auto countMetric = config.add_count_metric();
148 countMetric->set_id(metricId);
149 countMetric->set_what(crashMatcher.id());
150 countMetric->set_bucket(FIVE_MINUTES);
151 countMetric->mutable_dimensions_in_what()->set_field(
152 util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
153 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
154
155 auto metric_activation1 = config.add_metric_activation();
156 metric_activation1->set_metric_id(metricId);
157 auto event_activation1 = metric_activation1->add_event_activation();
158 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
159 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
160 event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
161 auto event_activation2 = metric_activation1->add_event_activation();
162 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
163 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
164 event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
165
166 return config;
167 }
168
CreateStatsdConfigWithTwoMetricsTwoDeactivations()169 StatsdConfig CreateStatsdConfigWithTwoMetricsTwoDeactivations() {
170 StatsdConfig config;
171 auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
172 auto crashMatcher = CreateProcessCrashAtomMatcher();
173 auto foregroundMatcher = CreateMoveToForegroundAtomMatcher();
174 auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
175 auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
176 auto brightnessChangedMatcher2 = CreateScreenBrightnessChangedAtomMatcher();
177 brightnessChangedMatcher2.set_id(StringToId("ScreenBrightnessChanged2"));
178
179 *config.add_atom_matcher() = saverModeMatcher;
180 *config.add_atom_matcher() = crashMatcher;
181 *config.add_atom_matcher() = screenOnMatcher;
182 *config.add_atom_matcher() = brightnessChangedMatcher;
183 *config.add_atom_matcher() = brightnessChangedMatcher2;
184 *config.add_atom_matcher() = foregroundMatcher;
185
186 int64_t metricId = 123456;
187 auto countMetric = config.add_count_metric();
188 countMetric->set_id(metricId);
189 countMetric->set_what(crashMatcher.id());
190 countMetric->set_bucket(FIVE_MINUTES);
191 countMetric->mutable_dimensions_in_what()->set_field(
192 util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
193 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
194
195 int64_t metricId2 = 234567;
196 countMetric = config.add_count_metric();
197 countMetric->set_id(metricId2);
198 countMetric->set_what(foregroundMatcher.id());
199 countMetric->set_bucket(FIVE_MINUTES);
200 countMetric->mutable_dimensions_in_what()->set_field(
201 util::ACTIVITY_FOREGROUND_STATE_CHANGED);
202 countMetric->mutable_dimensions_in_what()->add_child()->set_field(1); // uid field
203
204 auto metric_activation1 = config.add_metric_activation();
205 metric_activation1->set_metric_id(metricId);
206 auto event_activation1 = metric_activation1->add_event_activation();
207 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
208 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
209 event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
210 auto event_activation2 = metric_activation1->add_event_activation();
211 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
212 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
213 event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
214
215 metric_activation1 = config.add_metric_activation();
216 metric_activation1->set_metric_id(metricId2);
217 event_activation1 = metric_activation1->add_event_activation();
218 event_activation1->set_atom_matcher_id(saverModeMatcher.id());
219 event_activation1->set_ttl_seconds(60 * 6); // 6 minutes
220 event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
221 event_activation2 = metric_activation1->add_event_activation();
222 event_activation2->set_atom_matcher_id(screenOnMatcher.id());
223 event_activation2->set_ttl_seconds(60 * 2); // 2 minutes
224 event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
225
226 return config;
227 }
228
229 } // namespace
230
TEST(MetricActivationE2eTest,TestCountMetric)231 TEST(MetricActivationE2eTest, TestCountMetric) {
232 auto config = CreateStatsdConfig();
233
234 int64_t bucketStartTimeNs = NS_PER_SEC * 10; // 10 secs
235 int64_t bucketSizeNs =
236 TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
237
238 int uid = 12345;
239 int64_t cfgId = 98765;
240 ConfigKey cfgKey(uid, cfgId);
241
242 sp<UidMap> m = new UidMap();
243 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
244 sp<AlarmMonitor> anomalyAlarmMonitor;
245 sp<AlarmMonitor> subscriberAlarmMonitor;
246 vector<int64_t> activeConfigsBroadcast;
247
248 long timeBase1 = 1;
249 int broadcastCount = 0;
250 std::shared_ptr<MockLogEventFilter> mockLogEventFilter = std::make_shared<MockLogEventFilter>();
251 EXPECT_CALL(*mockLogEventFilter, setAtomIds(StatsLogProcessor::getDefaultAtomIdSet(), _))
252 .Times(1);
253 StatsLogProcessor processor(
254 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
255 [](const ConfigKey& key) { return true; },
256 [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
257 const vector<int64_t>& activeConfigs) {
258 broadcastCount++;
259 EXPECT_EQ(broadcastUid, uid);
260 activeConfigsBroadcast.clear();
261 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
262 activeConfigs.end());
263 return true;
264 },
265 [](const ConfigKey&, const string&, const vector<int64_t>&) {}, mockLogEventFilter);
266
267 const LogEventFilter::AtomIdSet atomIdsList = CreateAtomIdSetFromConfig(config);
268 EXPECT_CALL(*mockLogEventFilter, setAtomIds(atomIdsList, &processor)).Times(1);
269
270 processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
271
272 ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
273 sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
274 EXPECT_TRUE(metricsManager->isConfigValid());
275 ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
276 sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
277 auto& eventActivationMap = metricProducer->mEventActivationMap;
278
279 EXPECT_FALSE(metricsManager->isActive());
280 EXPECT_FALSE(metricProducer->mIsActive);
281 // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
282 // triggered by screen on event (tracker index 2).
283 ASSERT_EQ(eventActivationMap.size(), 2u);
284 EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
285 EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
286 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
287 EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
288 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
289 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
290 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
291 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
292
293 std::unique_ptr<LogEvent> event;
294
295 event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
296 processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
297 EXPECT_FALSE(metricsManager->isActive());
298 EXPECT_FALSE(metricProducer->mIsActive);
299 EXPECT_EQ(broadcastCount, 0);
300
301 // Activated by battery save mode.
302 event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
303 processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
304 EXPECT_TRUE(metricsManager->isActive());
305 EXPECT_TRUE(metricProducer->mIsActive);
306 EXPECT_EQ(broadcastCount, 1);
307 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
308 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
309 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
310 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
311 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
312 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
313 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
314 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
315
316 // First processed event.
317 event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
318 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
319
320 // Activated by screen on event.
321 event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
322 processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
323 EXPECT_TRUE(metricsManager->isActive());
324 EXPECT_TRUE(metricProducer->mIsActive);
325 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
326 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
327 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
328 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
329 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
330 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
331
332 // 2nd processed event.
333 // The activation by screen_on event expires, but the one by battery save mode is still active.
334 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
335 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
336 EXPECT_TRUE(metricsManager->isActive());
337 EXPECT_TRUE(metricProducer->mIsActive);
338 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
339 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
340 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
341 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
342 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
343 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
344 // No new broadcast since the config should still be active.
345 EXPECT_EQ(broadcastCount, 1);
346
347 // 3rd processed event.
348 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
349 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
350
351 // All activations expired.
352 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
353 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
354 EXPECT_FALSE(metricsManager->isActive());
355 EXPECT_FALSE(metricProducer->mIsActive);
356 // New broadcast since the config is no longer active.
357 EXPECT_EQ(broadcastCount, 2);
358 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
359 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
360 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
361 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
362 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
363 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
364 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
365
366 // Re-activate metric via screen on.
367 event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
368 android::view::DISPLAY_STATE_ON);
369 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
370 EXPECT_TRUE(metricsManager->isActive());
371 EXPECT_TRUE(metricProducer->mIsActive);
372 EXPECT_EQ(broadcastCount, 3);
373 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
374 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
375 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
376 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
377 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
378 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
379 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
380 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
381
382 // 4th processed event.
383 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
384 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
385
386 ConfigMetricsReportList reports;
387 vector<uint8_t> buffer;
388 processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
389 ADB_DUMP, FAST, &buffer);
390 EXPECT_TRUE(buffer.size() > 0);
391 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
392 backfillDimensionPath(&reports);
393 backfillStartEndTimestamp(&reports);
394 ASSERT_EQ(1, reports.reports_size());
395 ASSERT_EQ(1, reports.reports(0).metrics_size());
396
397 StatsLogReport::CountMetricDataWrapper countMetrics;
398 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
399 ASSERT_EQ(4, countMetrics.data_size());
400
401 auto data = countMetrics.data(0);
402 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
403 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
404 EXPECT_EQ(1 /* uid field */,
405 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
406 EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
407 ASSERT_EQ(1, data.bucket_info_size());
408 EXPECT_EQ(1, data.bucket_info(0).count());
409 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
410 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
411
412 data = countMetrics.data(1);
413 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
414 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
415 EXPECT_EQ(1 /* uid field */,
416 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
417 EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
418 ASSERT_EQ(1, data.bucket_info_size());
419 EXPECT_EQ(1, data.bucket_info(0).count());
420 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
421 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
422
423 data = countMetrics.data(2);
424 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
425 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
426 EXPECT_EQ(1 /* uid field */,
427 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
428 EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
429 ASSERT_EQ(1, data.bucket_info_size());
430 EXPECT_EQ(1, data.bucket_info(0).count());
431 // Partial bucket as metric is deactivated.
432 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
433 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
434 data.bucket_info(0).end_bucket_elapsed_nanos());
435
436 data = countMetrics.data(3);
437 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
438 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
439 EXPECT_EQ(1 /* uid field */,
440 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
441 EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
442 ASSERT_EQ(1, data.bucket_info_size());
443 EXPECT_EQ(1, data.bucket_info(0).count());
444 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
445 data.bucket_info(0).start_bucket_elapsed_nanos());
446 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
447 }
448
TEST(MetricActivationE2eTest,TestCountMetricWithOneDeactivation)449 TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation) {
450 auto config = CreateStatsdConfigWithOneDeactivation();
451
452 int64_t bucketStartTimeNs = NS_PER_SEC * 10; // 10 secs
453 int64_t bucketSizeNs =
454 TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
455
456 int uid = 12345;
457 int64_t cfgId = 98765;
458 ConfigKey cfgKey(uid, cfgId);
459
460 sp<UidMap> m = new UidMap();
461 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
462 sp<AlarmMonitor> anomalyAlarmMonitor;
463 sp<AlarmMonitor> subscriberAlarmMonitor;
464 vector<int64_t> activeConfigsBroadcast;
465
466 long timeBase1 = 1;
467 int broadcastCount = 0;
468 std::shared_ptr<MockLogEventFilter> mockLogEventFilter = std::make_shared<MockLogEventFilter>();
469 EXPECT_CALL(*mockLogEventFilter, setAtomIds(StatsLogProcessor::getDefaultAtomIdSet(), _))
470 .Times(1);
471 StatsLogProcessor processor(
472 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
473 [](const ConfigKey& key) { return true; },
474 [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
475 const vector<int64_t>& activeConfigs) {
476 broadcastCount++;
477 EXPECT_EQ(broadcastUid, uid);
478 activeConfigsBroadcast.clear();
479 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
480 activeConfigs.end());
481 return true;
482 },
483 [](const ConfigKey&, const string&, const vector<int64_t>&) {}, mockLogEventFilter);
484
485 const LogEventFilter::AtomIdSet atomIdsList = CreateAtomIdSetFromConfig(config);
486 EXPECT_CALL(*mockLogEventFilter, setAtomIds(atomIdsList, &processor)).Times(1);
487
488 processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
489
490 ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
491 sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
492 EXPECT_TRUE(metricsManager->isConfigValid());
493 ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
494 sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
495 auto& eventActivationMap = metricProducer->mEventActivationMap;
496 auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
497
498 EXPECT_FALSE(metricsManager->isActive());
499 EXPECT_FALSE(metricProducer->mIsActive);
500 // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
501 // triggered by screen on event (tracker index 2).
502 ASSERT_EQ(eventActivationMap.size(), 2u);
503 EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
504 EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
505 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
506 EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
507 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
508 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
509 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
510 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
511 ASSERT_EQ(eventDeactivationMap.size(), 1u);
512 EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
513 ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
514 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
515
516 std::unique_ptr<LogEvent> event;
517
518 event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
519 processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
520 EXPECT_FALSE(metricsManager->isActive());
521 EXPECT_FALSE(metricProducer->mIsActive);
522 EXPECT_EQ(broadcastCount, 0);
523
524 // Activated by battery save mode.
525 event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
526 processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
527 EXPECT_TRUE(metricsManager->isActive());
528 EXPECT_TRUE(metricProducer->mIsActive);
529 EXPECT_EQ(broadcastCount, 1);
530 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
531 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
532 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
533 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
534 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
535 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
536 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
537 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
538 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
539
540 // First processed event.
541 event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
542 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
543
544 // Activated by screen on event.
545 event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
546 processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
547 EXPECT_TRUE(metricsManager->isActive());
548 EXPECT_TRUE(metricProducer->mIsActive);
549 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
550 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
551 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
552 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
553 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
554 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
555 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
556
557 // 2nd processed event.
558 // The activation by screen_on event expires, but the one by battery save mode is still active.
559 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
560 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
561 EXPECT_TRUE(metricsManager->isActive());
562 EXPECT_TRUE(metricProducer->mIsActive);
563 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
564 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
565 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
566 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
567 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
568 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
569 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
570 // No new broadcast since the config should still be active.
571 EXPECT_EQ(broadcastCount, 1);
572
573 // 3rd processed event.
574 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
575 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
576
577 // All activations expired.
578 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
579 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
580 EXPECT_FALSE(metricsManager->isActive());
581 EXPECT_FALSE(metricProducer->mIsActive);
582 // New broadcast since the config is no longer active.
583 EXPECT_EQ(broadcastCount, 2);
584 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
585 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
586 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
587 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
588 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
589 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
590 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
591 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
592
593 // Re-activate metric via screen on.
594 event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
595 android::view::DISPLAY_STATE_ON);
596 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
597 EXPECT_TRUE(metricsManager->isActive());
598 EXPECT_TRUE(metricProducer->mIsActive);
599 EXPECT_EQ(broadcastCount, 3);
600 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
601 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
602 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
603 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
604 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
605 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
606 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
607 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
608 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
609
610 // 4th processed event.
611 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
612 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
613
614 // Re-enable battery saver mode activation.
615 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
616 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
617 EXPECT_TRUE(metricsManager->isActive());
618 EXPECT_TRUE(metricProducer->mIsActive);
619 EXPECT_EQ(broadcastCount, 3);
620 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
621 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
622 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
623 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
624 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
625 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
626 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
627 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
628 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
629
630 // 5th processed event.
631 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
632 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
633
634 // Cancel battery saver mode activation.
635 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
636 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
637 EXPECT_TRUE(metricsManager->isActive());
638 EXPECT_TRUE(metricProducer->mIsActive);
639 EXPECT_EQ(broadcastCount, 3);
640 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
641 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
642 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
643 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
644 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
645 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
646 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
647 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
648 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
649
650 // Screen-on activation expired.
651 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
652 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
653 EXPECT_FALSE(metricsManager->isActive());
654 EXPECT_FALSE(metricProducer->mIsActive);
655 // New broadcast since the config is no longer active.
656 EXPECT_EQ(broadcastCount, 4);
657 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
658 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
659 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
660 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
661 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
662 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
663 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
664 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
665
666 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
667 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
668
669 // Re-enable battery saver mode activation.
670 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
671 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
672 EXPECT_TRUE(metricsManager->isActive());
673 EXPECT_TRUE(metricProducer->mIsActive);
674 EXPECT_EQ(broadcastCount, 5);
675 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
676 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
677 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
678 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
679 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
680 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
681 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
682 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
683 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
684
685 // Cancel battery saver mode activation.
686 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
687 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
688 EXPECT_FALSE(metricsManager->isActive());
689 EXPECT_FALSE(metricProducer->mIsActive);
690 EXPECT_EQ(broadcastCount, 6);
691 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
692 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
693 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
694 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
695 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
696 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
697 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
698 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
699
700 ConfigMetricsReportList reports;
701 vector<uint8_t> buffer;
702 processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
703 ADB_DUMP, FAST, &buffer);
704 EXPECT_TRUE(buffer.size() > 0);
705 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
706 backfillDimensionPath(&reports);
707 backfillStartEndTimestamp(&reports);
708 ASSERT_EQ(1, reports.reports_size());
709 ASSERT_EQ(1, reports.reports(0).metrics_size());
710
711 StatsLogReport::CountMetricDataWrapper countMetrics;
712 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
713 ASSERT_EQ(5, countMetrics.data_size());
714
715 auto data = countMetrics.data(0);
716 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
717 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
718 EXPECT_EQ(1 /* uid field */,
719 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
720 EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
721 ASSERT_EQ(1, data.bucket_info_size());
722 EXPECT_EQ(1, data.bucket_info(0).count());
723 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
724 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
725
726 data = countMetrics.data(1);
727 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
728 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
729 EXPECT_EQ(1 /* uid field */,
730 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
731 EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
732 ASSERT_EQ(1, data.bucket_info_size());
733 EXPECT_EQ(1, data.bucket_info(0).count());
734 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
735 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
736
737 data = countMetrics.data(2);
738 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
739 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
740 EXPECT_EQ(1 /* uid field */,
741 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
742 EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
743 ASSERT_EQ(1, data.bucket_info_size());
744 EXPECT_EQ(1, data.bucket_info(0).count());
745 // Partial bucket as metric is deactivated.
746 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
747 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
748 data.bucket_info(0).end_bucket_elapsed_nanos());
749
750 data = countMetrics.data(3);
751 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
752 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
753 EXPECT_EQ(1 /* uid field */,
754 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
755 EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
756 ASSERT_EQ(1, data.bucket_info_size());
757 EXPECT_EQ(1, data.bucket_info(0).count());
758 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
759 data.bucket_info(0).start_bucket_elapsed_nanos());
760 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 13,
761 data.bucket_info(0).end_bucket_elapsed_nanos());
762
763 data = countMetrics.data(4);
764 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
765 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
766 EXPECT_EQ(1 /* uid field */,
767 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
768 EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
769 ASSERT_EQ(1, data.bucket_info_size());
770 EXPECT_EQ(1, data.bucket_info(0).count());
771 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
772 data.bucket_info(0).start_bucket_elapsed_nanos());
773 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 13,
774 data.bucket_info(0).end_bucket_elapsed_nanos());
775 }
776
TEST(MetricActivationE2eTest,TestCountMetricWithTwoDeactivations)777 TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations) {
778 auto config = CreateStatsdConfigWithTwoDeactivations();
779
780 int64_t bucketStartTimeNs = NS_PER_SEC * 10; // 10 secs
781 int64_t bucketSizeNs =
782 TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
783
784 int uid = 12345;
785 int64_t cfgId = 98765;
786 ConfigKey cfgKey(uid, cfgId);
787
788 sp<UidMap> m = new UidMap();
789 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
790 sp<AlarmMonitor> anomalyAlarmMonitor;
791 sp<AlarmMonitor> subscriberAlarmMonitor;
792 vector<int64_t> activeConfigsBroadcast;
793
794 long timeBase1 = 1;
795 int broadcastCount = 0;
796 std::shared_ptr<MockLogEventFilter> mockLogEventFilter = std::make_shared<MockLogEventFilter>();
797 EXPECT_CALL(*mockLogEventFilter, setAtomIds(StatsLogProcessor::getDefaultAtomIdSet(), _))
798 .Times(1);
799 StatsLogProcessor processor(
800 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
801 [](const ConfigKey& key) { return true; },
802 [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
803 const vector<int64_t>& activeConfigs) {
804 broadcastCount++;
805 EXPECT_EQ(broadcastUid, uid);
806 activeConfigsBroadcast.clear();
807 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
808 activeConfigs.end());
809 return true;
810 },
811 [](const ConfigKey&, const string&, const vector<int64_t>&) {}, mockLogEventFilter);
812
813 const LogEventFilter::AtomIdSet atomIdsList = CreateAtomIdSetFromConfig(config);
814 EXPECT_CALL(*mockLogEventFilter, setAtomIds(atomIdsList, &processor)).Times(1);
815
816 processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
817
818 ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
819 sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
820 EXPECT_TRUE(metricsManager->isConfigValid());
821 ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
822 sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
823 auto& eventActivationMap = metricProducer->mEventActivationMap;
824 auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
825
826 EXPECT_FALSE(metricsManager->isActive());
827 EXPECT_FALSE(metricProducer->mIsActive);
828 // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
829 // triggered by screen on event (tracker index 2).
830 ASSERT_EQ(eventActivationMap.size(), 2u);
831 EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
832 EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
833 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
834 EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
835 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
836 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
837 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
838 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
839 ASSERT_EQ(eventDeactivationMap.size(), 2u);
840 EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
841 EXPECT_TRUE(eventDeactivationMap.find(4) != eventDeactivationMap.end());
842 ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
843 ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
844 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
845 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
846
847 std::unique_ptr<LogEvent> event;
848
849 event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
850 processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
851 EXPECT_FALSE(metricsManager->isActive());
852 EXPECT_FALSE(metricProducer->mIsActive);
853 EXPECT_EQ(broadcastCount, 0);
854
855 // Activated by battery save mode.
856 event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
857 processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
858 EXPECT_TRUE(metricsManager->isActive());
859 EXPECT_TRUE(metricProducer->mIsActive);
860 EXPECT_EQ(broadcastCount, 1);
861 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
862 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
863 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
864 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
865 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
866 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
867 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
868 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
869 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
870 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
871
872 // First processed event.
873 event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
874 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
875
876 // Activated by screen on event.
877 event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
878 processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
879 EXPECT_TRUE(metricsManager->isActive());
880 EXPECT_TRUE(metricProducer->mIsActive);
881 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
882 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
883 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
884 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
885 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
886 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
887 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
888 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
889
890 // 2nd processed event.
891 // The activation by screen_on event expires, but the one by battery save mode is still active.
892 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
893 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
894 EXPECT_TRUE(metricsManager->isActive());
895 EXPECT_TRUE(metricProducer->mIsActive);
896 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
897 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
898 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
899 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
900 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
901 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
902 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
903 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
904 // No new broadcast since the config should still be active.
905 EXPECT_EQ(broadcastCount, 1);
906
907 // 3rd processed event.
908 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
909 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
910
911 // All activations expired.
912 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
913 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
914 EXPECT_FALSE(metricsManager->isActive());
915 EXPECT_FALSE(metricProducer->mIsActive);
916 // New broadcast since the config is no longer active.
917 EXPECT_EQ(broadcastCount, 2);
918 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
919 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
920 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
921 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
922 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
923 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
924 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
925 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
926 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
927
928 // Re-activate metric via screen on.
929 event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
930 android::view::DISPLAY_STATE_ON);
931 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
932 EXPECT_TRUE(metricsManager->isActive());
933 EXPECT_TRUE(metricProducer->mIsActive);
934 EXPECT_EQ(broadcastCount, 3);
935 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
936 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
937 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
938 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
939 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
940 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
941 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
942 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
943 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
944 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
945
946 // 4th processed event.
947 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
948 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
949
950 // Re-enable battery saver mode activation.
951 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
952 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
953 EXPECT_TRUE(metricsManager->isActive());
954 EXPECT_TRUE(metricProducer->mIsActive);
955 EXPECT_EQ(broadcastCount, 3);
956 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
957 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
958 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
959 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
960 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
961 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
962 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
963 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
964 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
965 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
966
967 // 5th processed event.
968 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
969 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
970
971 // Cancel battery saver mode and screen on activation.
972 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
973 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
974 EXPECT_FALSE(metricsManager->isActive());
975 EXPECT_FALSE(metricProducer->mIsActive);
976 // New broadcast since the config is no longer active.
977 EXPECT_EQ(broadcastCount, 4);
978 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
979 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
980 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
981 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
982 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
983 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
984 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
985 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
986 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
987
988 // Screen-on activation expired.
989 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
990 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
991 EXPECT_FALSE(metricsManager->isActive());
992 EXPECT_FALSE(metricProducer->mIsActive);
993 EXPECT_EQ(broadcastCount, 4);
994 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
995 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
996 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
997 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
998 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
999 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1000 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1001 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1002 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1003
1004 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
1005 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
1006
1007 // Re-enable battery saver mode activation.
1008 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1009 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1010 EXPECT_TRUE(metricsManager->isActive());
1011 EXPECT_TRUE(metricProducer->mIsActive);
1012 EXPECT_EQ(broadcastCount, 5);
1013 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1014 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1015 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1016 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1017 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1018 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1019 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1020 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1021 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1022 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1023
1024 // Cancel battery saver mode and screen on activation.
1025 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
1026 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
1027 EXPECT_FALSE(metricsManager->isActive());
1028 EXPECT_FALSE(metricProducer->mIsActive);
1029 EXPECT_EQ(broadcastCount, 6);
1030 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1031 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1032 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1033 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1034 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1035 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1036 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1037 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1038 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1039
1040 ConfigMetricsReportList reports;
1041 vector<uint8_t> buffer;
1042 processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1043 ADB_DUMP, FAST, &buffer);
1044 EXPECT_TRUE(buffer.size() > 0);
1045 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1046 backfillDimensionPath(&reports);
1047 backfillStartEndTimestamp(&reports);
1048 ASSERT_EQ(1, reports.reports_size());
1049 ASSERT_EQ(1, reports.reports(0).metrics_size());
1050
1051 StatsLogReport::CountMetricDataWrapper countMetrics;
1052 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1053 ASSERT_EQ(5, countMetrics.data_size());
1054
1055 auto data = countMetrics.data(0);
1056 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1057 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1058 EXPECT_EQ(1 /* uid field */,
1059 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1060 EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1061 ASSERT_EQ(1, data.bucket_info_size());
1062 EXPECT_EQ(1, data.bucket_info(0).count());
1063 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1064 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1065
1066 data = countMetrics.data(1);
1067 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1068 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1069 EXPECT_EQ(1 /* uid field */,
1070 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1071 EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1072 ASSERT_EQ(1, data.bucket_info_size());
1073 EXPECT_EQ(1, data.bucket_info(0).count());
1074 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1075 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1076
1077 data = countMetrics.data(2);
1078 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1079 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1080 EXPECT_EQ(1 /* uid field */,
1081 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1082 EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1083 ASSERT_EQ(1, data.bucket_info_size());
1084 EXPECT_EQ(1, data.bucket_info(0).count());
1085 // Partial bucket as metric is deactivated.
1086 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1087 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1088 data.bucket_info(0).end_bucket_elapsed_nanos());
1089
1090 data = countMetrics.data(3);
1091 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1092 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1093 EXPECT_EQ(1 /* uid field */,
1094 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1095 EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1096 ASSERT_EQ(1, data.bucket_info_size());
1097 EXPECT_EQ(1, data.bucket_info(0).count());
1098 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1099 data.bucket_info(0).start_bucket_elapsed_nanos());
1100 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1101 data.bucket_info(0).end_bucket_elapsed_nanos());
1102
1103 data = countMetrics.data(4);
1104 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1105 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1106 EXPECT_EQ(1 /* uid field */,
1107 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1108 EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1109 ASSERT_EQ(1, data.bucket_info_size());
1110 EXPECT_EQ(1, data.bucket_info(0).count());
1111 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1112 data.bucket_info(0).start_bucket_elapsed_nanos());
1113 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1114 data.bucket_info(0).end_bucket_elapsed_nanos());
1115 }
1116
TEST(MetricActivationE2eTest,TestCountMetricWithSameDeactivation)1117 TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation) {
1118 auto config = CreateStatsdConfigWithSameDeactivations();
1119
1120 int64_t bucketStartTimeNs = NS_PER_SEC * 10; // 10 secs
1121 int64_t bucketSizeNs =
1122 TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
1123
1124 int uid = 12345;
1125 int64_t cfgId = 98765;
1126 ConfigKey cfgKey(uid, cfgId);
1127
1128 sp<UidMap> m = new UidMap();
1129 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
1130 sp<AlarmMonitor> anomalyAlarmMonitor;
1131 sp<AlarmMonitor> subscriberAlarmMonitor;
1132 vector<int64_t> activeConfigsBroadcast;
1133
1134 long timeBase1 = 1;
1135 int broadcastCount = 0;
1136 std::shared_ptr<MockLogEventFilter> mockLogEventFilter = std::make_shared<MockLogEventFilter>();
1137 EXPECT_CALL(*mockLogEventFilter, setAtomIds(StatsLogProcessor::getDefaultAtomIdSet(), _))
1138 .Times(1);
1139 StatsLogProcessor processor(
1140 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
1141 [](const ConfigKey& key) { return true; },
1142 [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
1143 const vector<int64_t>& activeConfigs) {
1144 broadcastCount++;
1145 EXPECT_EQ(broadcastUid, uid);
1146 activeConfigsBroadcast.clear();
1147 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
1148 activeConfigs.end());
1149 return true;
1150 },
1151 [](const ConfigKey&, const string&, const vector<int64_t>&) {}, mockLogEventFilter);
1152
1153 const LogEventFilter::AtomIdSet atomIdsList = CreateAtomIdSetFromConfig(config);
1154 EXPECT_CALL(*mockLogEventFilter, setAtomIds(atomIdsList, &processor)).Times(1);
1155
1156 processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
1157
1158 ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
1159 sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
1160 EXPECT_TRUE(metricsManager->isConfigValid());
1161 ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
1162 sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
1163 auto& eventActivationMap = metricProducer->mEventActivationMap;
1164 auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
1165
1166 EXPECT_FALSE(metricsManager->isActive());
1167 EXPECT_FALSE(metricProducer->mIsActive);
1168 // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
1169 // triggered by screen on event (tracker index 2).
1170 ASSERT_EQ(eventActivationMap.size(), 2u);
1171 EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
1172 EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
1173 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1174 EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
1175 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1176 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1177 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1178 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1179 ASSERT_EQ(eventDeactivationMap.size(), 1u);
1180 EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
1181 ASSERT_EQ(eventDeactivationMap[3].size(), 2u);
1182 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1183 EXPECT_EQ(eventDeactivationMap[3][1], eventActivationMap[2]);
1184 EXPECT_EQ(broadcastCount, 0);
1185
1186 std::unique_ptr<LogEvent> event;
1187
1188 // Event that should be ignored.
1189 event = CreateAppCrashEvent(bucketStartTimeNs + 1, 111);
1190 processor.OnLogEvent(event.get(), bucketStartTimeNs + 1);
1191
1192 // Activate metric via screen on for 2 minutes.
1193 event = CreateScreenStateChangedEvent(bucketStartTimeNs + 10, android::view::DISPLAY_STATE_ON);
1194 processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
1195 EXPECT_TRUE(metricsManager->isActive());
1196 EXPECT_TRUE(metricProducer->mIsActive);
1197 EXPECT_EQ(broadcastCount, 1);
1198 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1199 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1200 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1201 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1202 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 10);
1203
1204 // 1st processed event.
1205 event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
1206 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1207
1208 // Enable battery saver mode activation for 5 minutes.
1209 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1210 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1211 EXPECT_TRUE(metricsManager->isActive());
1212 EXPECT_TRUE(metricProducer->mIsActive);
1213 EXPECT_EQ(broadcastCount, 1);
1214 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1215 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1216 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1217 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 10);
1218
1219 // 2nd processed event.
1220 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 + 40, 333);
1221 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 + 40);
1222
1223 // Cancel battery saver mode and screen on activation.
1224 int64_t firstDeactivation = bucketStartTimeNs + NS_PER_SEC * 61;
1225 event = CreateScreenBrightnessChangedEvent(firstDeactivation, 64);
1226 processor.OnLogEvent(event.get(), firstDeactivation);
1227 EXPECT_FALSE(metricsManager->isActive());
1228 EXPECT_FALSE(metricProducer->mIsActive);
1229 // New broadcast since the config is no longer active.
1230 EXPECT_EQ(broadcastCount, 2);
1231 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1232 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1233 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1234
1235 // Should be ignored
1236 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 61 + 80, 444);
1237 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 61 + 80);
1238
1239 // Re-enable battery saver mode activation.
1240 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1241 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1242 EXPECT_TRUE(metricsManager->isActive());
1243 EXPECT_TRUE(metricProducer->mIsActive);
1244 EXPECT_EQ(broadcastCount, 3);
1245 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1246 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1247 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1248 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1249 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1250
1251 // 3rd processed event.
1252 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 80, 555);
1253 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 80);
1254
1255 // Cancel battery saver mode activation.
1256 int64_t secondDeactivation = bucketStartTimeNs + NS_PER_SEC * 60 * 13;
1257 event = CreateScreenBrightnessChangedEvent(secondDeactivation, 140);
1258 processor.OnLogEvent(event.get(), secondDeactivation);
1259 EXPECT_FALSE(metricsManager->isActive());
1260 EXPECT_FALSE(metricProducer->mIsActive);
1261 EXPECT_EQ(broadcastCount, 4);
1262 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1263 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1264 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1265
1266 // Should be ignored.
1267 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13 + 80, 666);
1268 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13 + 80);
1269
1270 ConfigMetricsReportList reports;
1271 vector<uint8_t> buffer;
1272 processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1273 ADB_DUMP, FAST, &buffer);
1274 EXPECT_TRUE(buffer.size() > 0);
1275 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1276 backfillDimensionPath(&reports);
1277 backfillStartEndTimestamp(&reports);
1278 ASSERT_EQ(1, reports.reports_size());
1279 ASSERT_EQ(1, reports.reports(0).metrics_size());
1280
1281 StatsLogReport::CountMetricDataWrapper countMetrics;
1282 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1283 ASSERT_EQ(3, countMetrics.data_size());
1284
1285 auto data = countMetrics.data(0);
1286 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1287 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1288 EXPECT_EQ(1 /* uid field */,
1289 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1290 EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1291 ASSERT_EQ(1, data.bucket_info_size());
1292 EXPECT_EQ(1, data.bucket_info(0).count());
1293 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1294 EXPECT_EQ(firstDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1295
1296 data = countMetrics.data(1);
1297 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1298 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1299 EXPECT_EQ(1 /* uid field */,
1300 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1301 EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1302 ASSERT_EQ(1, data.bucket_info_size());
1303 EXPECT_EQ(1, data.bucket_info(0).count());
1304 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1305 EXPECT_EQ(firstDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1306
1307 data = countMetrics.data(2);
1308 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1309 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1310 EXPECT_EQ(1 /* uid field */,
1311 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1312 EXPECT_EQ(555, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1313 ASSERT_EQ(1, data.bucket_info_size());
1314 EXPECT_EQ(1, data.bucket_info(0).count());
1315 // Partial bucket as metric is deactivated.
1316 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1317 data.bucket_info(0).start_bucket_elapsed_nanos());
1318 EXPECT_EQ(secondDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1319 }
1320
TEST(MetricActivationE2eTest,TestCountMetricWithTwoMetricsTwoDeactivations)1321 TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations) {
1322 auto config = CreateStatsdConfigWithTwoMetricsTwoDeactivations();
1323
1324 int64_t bucketStartTimeNs = NS_PER_SEC * 10; // 10 secs
1325 int64_t bucketSizeNs =
1326 TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
1327
1328 int uid = 12345;
1329 int64_t cfgId = 98765;
1330 ConfigKey cfgKey(uid, cfgId);
1331
1332 sp<UidMap> m = new UidMap();
1333 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
1334 sp<AlarmMonitor> anomalyAlarmMonitor;
1335 sp<AlarmMonitor> subscriberAlarmMonitor;
1336 vector<int64_t> activeConfigsBroadcast;
1337
1338 long timeBase1 = 1;
1339 int broadcastCount = 0;
1340 std::shared_ptr<MockLogEventFilter> mockLogEventFilter = std::make_shared<MockLogEventFilter>();
1341 EXPECT_CALL(*mockLogEventFilter, setAtomIds(StatsLogProcessor::getDefaultAtomIdSet(), _))
1342 .Times(1);
1343 StatsLogProcessor processor(
1344 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
1345 [](const ConfigKey& key) { return true; },
1346 [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
1347 const vector<int64_t>& activeConfigs) {
1348 broadcastCount++;
1349 EXPECT_EQ(broadcastUid, uid);
1350 activeConfigsBroadcast.clear();
1351 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
1352 activeConfigs.end());
1353 return true;
1354 },
1355 [](const ConfigKey&, const string&, const vector<int64_t>&) {}, mockLogEventFilter);
1356
1357 const LogEventFilter::AtomIdSet atomIdsList = CreateAtomIdSetFromConfig(config);
1358 EXPECT_CALL(*mockLogEventFilter, setAtomIds(atomIdsList, &processor)).Times(1);
1359
1360 processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
1361
1362 ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
1363 sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
1364 EXPECT_TRUE(metricsManager->isConfigValid());
1365 ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 2);
1366 sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
1367 auto& eventActivationMap = metricProducer->mEventActivationMap;
1368 auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
1369 sp<MetricProducer> metricProducer2 = metricsManager->mAllMetricProducers[1];
1370 auto& eventActivationMap2 = metricProducer2->mEventActivationMap;
1371 auto& eventDeactivationMap2 = metricProducer2->mEventDeactivationMap;
1372
1373 EXPECT_FALSE(metricsManager->isActive());
1374 EXPECT_FALSE(metricProducer->mIsActive);
1375 EXPECT_FALSE(metricProducer2->mIsActive);
1376 // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
1377 // triggered by screen on event (tracker index 2).
1378 ASSERT_EQ(eventActivationMap.size(), 2u);
1379 EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
1380 EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
1381 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1382 EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
1383 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1384 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1385 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1386 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1387 ASSERT_EQ(eventDeactivationMap.size(), 2u);
1388 EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
1389 EXPECT_TRUE(eventDeactivationMap.find(4) != eventDeactivationMap.end());
1390 ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
1391 ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
1392 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1393 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1394
1395 ASSERT_EQ(eventActivationMap2.size(), 2u);
1396 EXPECT_TRUE(eventActivationMap2.find(0) != eventActivationMap2.end());
1397 EXPECT_TRUE(eventActivationMap2.find(2) != eventActivationMap2.end());
1398 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1399 EXPECT_EQ(eventActivationMap2[0]->start_ns, 0);
1400 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1401 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1402 EXPECT_EQ(eventActivationMap2[2]->start_ns, 0);
1403 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1404 ASSERT_EQ(eventDeactivationMap2.size(), 2u);
1405 EXPECT_TRUE(eventDeactivationMap2.find(3) != eventDeactivationMap2.end());
1406 EXPECT_TRUE(eventDeactivationMap2.find(4) != eventDeactivationMap2.end());
1407 ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
1408 ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
1409 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1410 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1411
1412 std::unique_ptr<LogEvent> event;
1413
1414 event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
1415 processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
1416 event = CreateMoveToForegroundEvent(bucketStartTimeNs + 5, 1111);
1417 processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
1418 EXPECT_FALSE(metricsManager->isActive());
1419 EXPECT_FALSE(metricProducer->mIsActive);
1420 EXPECT_FALSE(metricProducer2->mIsActive);
1421 EXPECT_EQ(broadcastCount, 0);
1422
1423 // Activated by battery save mode.
1424 event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
1425 processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
1426 EXPECT_TRUE(metricsManager->isActive());
1427 EXPECT_EQ(broadcastCount, 1);
1428 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1429 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1430 EXPECT_TRUE(metricProducer->mIsActive);
1431 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1432 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1433 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1434 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1435 EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1436 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1437 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1438 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1439 EXPECT_TRUE(metricProducer2->mIsActive);
1440 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1441 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1442 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1443 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1444 EXPECT_EQ(eventActivationMap2[2]->start_ns, 0);
1445 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1446 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1447 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1448
1449 // First processed event.
1450 event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
1451 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1452 event = CreateMoveToForegroundEvent(bucketStartTimeNs + 15, 2222);
1453 processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1454
1455 // Activated by screen on event.
1456 event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
1457 processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
1458 EXPECT_TRUE(metricsManager->isActive());
1459 EXPECT_TRUE(metricProducer->mIsActive);
1460 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1461 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1462 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1463 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1464 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1465 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1466 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1467 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1468 EXPECT_TRUE(metricProducer2->mIsActive);
1469 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1470 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1471 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1472 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1473 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1474 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1475 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1476 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1477
1478 // 2nd processed event.
1479 // The activation by screen_on event expires, but the one by battery save mode is still active.
1480 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
1481 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
1482 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 3333);
1483 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
1484 EXPECT_TRUE(metricsManager->isActive());
1485 EXPECT_TRUE(metricProducer->mIsActive);
1486 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1487 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1488 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1489 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1490 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1491 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1492 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1493 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1494 EXPECT_TRUE(metricProducer2->mIsActive);
1495 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1496 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1497 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1498 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1499 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1500 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1501 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1502 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1503 // No new broadcast since the config should still be active.
1504 EXPECT_EQ(broadcastCount, 1);
1505
1506 // 3rd processed event.
1507 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
1508 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
1509 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 4444);
1510 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
1511
1512 // All activations expired.
1513 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
1514 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
1515 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 5555);
1516 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
1517 EXPECT_FALSE(metricsManager->isActive());
1518 // New broadcast since the config is no longer active.
1519 EXPECT_EQ(broadcastCount, 2);
1520 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1521 EXPECT_FALSE(metricProducer->mIsActive);
1522 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1523 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1524 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1525 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1526 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1527 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1528 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1529 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1530 EXPECT_FALSE(metricProducer2->mIsActive);
1531 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1532 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1533 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1534 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1535 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1536 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1537 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1538 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1539
1540 // Re-activate metric via screen on.
1541 event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
1542 android::view::DISPLAY_STATE_ON);
1543 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1544 EXPECT_TRUE(metricsManager->isActive());
1545 EXPECT_EQ(broadcastCount, 3);
1546 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1547 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1548 EXPECT_TRUE(metricProducer->mIsActive);
1549 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1550 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1551 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1552 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1553 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1554 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1555 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1556 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1557 EXPECT_TRUE(metricProducer2->mIsActive);
1558 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1559 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1560 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1561 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1562 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1563 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1564 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1565 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1566
1567 // 4th processed event.
1568 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
1569 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
1570 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 6666);
1571 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
1572
1573 // Re-enable battery saver mode activation.
1574 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1575 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1576 EXPECT_TRUE(metricsManager->isActive());
1577 EXPECT_EQ(broadcastCount, 3);
1578 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1579 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1580 EXPECT_TRUE(metricProducer->mIsActive);
1581 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1582 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1583 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1584 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1585 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1586 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1587 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1588 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1589 EXPECT_TRUE(metricProducer2->mIsActive);
1590 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1591 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1592 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1593 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1594 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1595 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1596 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1597 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1598
1599 // 5th processed event.
1600 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
1601 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
1602 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 7777);
1603 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
1604
1605 // Cancel battery saver mode and screen on activation.
1606 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
1607 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
1608 EXPECT_FALSE(metricsManager->isActive());
1609 // New broadcast since the config is no longer active.
1610 EXPECT_EQ(broadcastCount, 4);
1611 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1612 EXPECT_FALSE(metricProducer->mIsActive);
1613 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1614 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1615 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1616 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1617 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1618 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1619 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1620 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1621 EXPECT_FALSE(metricProducer2->mIsActive);
1622 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1623 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1624 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1625 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1626 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1627 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1628 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1629 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1630
1631 // Screen-on activation expired.
1632 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
1633 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
1634 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 8888);
1635 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
1636 EXPECT_FALSE(metricsManager->isActive());
1637 EXPECT_EQ(broadcastCount, 4);
1638 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1639 EXPECT_FALSE(metricProducer->mIsActive);
1640 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1641 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1642 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1643 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1644 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1645 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1646 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1647 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1648 EXPECT_FALSE(metricProducer2->mIsActive);
1649 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1650 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1651 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1652 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1653 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1654 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1655 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1656 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1657
1658 event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
1659 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
1660 event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 9999);
1661 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
1662
1663 // Re-enable battery saver mode activation.
1664 event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1665 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1666 EXPECT_TRUE(metricsManager->isActive());
1667 EXPECT_EQ(broadcastCount, 5);
1668 ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1669 EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1670 EXPECT_TRUE(metricProducer->mIsActive);
1671 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1672 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1673 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1674 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1675 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1676 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1677 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1678 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1679 EXPECT_TRUE(metricProducer2->mIsActive);
1680 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1681 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1682 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1683 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1684 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1685 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1686 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1687 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1688
1689 // Cancel battery saver mode and screen on activation.
1690 event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
1691 processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
1692 EXPECT_FALSE(metricsManager->isActive());
1693 EXPECT_EQ(broadcastCount, 6);
1694 ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1695 EXPECT_FALSE(metricProducer->mIsActive);
1696 EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1697 EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1698 EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1699 EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1700 EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1701 EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1702 EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1703 EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1704 EXPECT_FALSE(metricProducer2->mIsActive);
1705 EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1706 EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1707 EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1708 EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1709 EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1710 EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1711 EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1712 EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1713
1714 ConfigMetricsReportList reports;
1715 vector<uint8_t> buffer;
1716 processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1717 ADB_DUMP, FAST, &buffer);
1718 EXPECT_TRUE(buffer.size() > 0);
1719 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1720 backfillDimensionPath(&reports);
1721 backfillStartEndTimestamp(&reports);
1722 ASSERT_EQ(1, reports.reports_size());
1723 ASSERT_EQ(2, reports.reports(0).metrics_size());
1724
1725 StatsLogReport::CountMetricDataWrapper countMetrics;
1726
1727 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1728 ASSERT_EQ(5, countMetrics.data_size());
1729
1730 auto data = countMetrics.data(0);
1731 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1732 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1733 EXPECT_EQ(1 /* uid field */,
1734 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1735 EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1736 ASSERT_EQ(1, data.bucket_info_size());
1737 EXPECT_EQ(1, data.bucket_info(0).count());
1738 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1739 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1740
1741 data = countMetrics.data(1);
1742 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1743 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1744 EXPECT_EQ(1 /* uid field */,
1745 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1746 EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1747 ASSERT_EQ(1, data.bucket_info_size());
1748 EXPECT_EQ(1, data.bucket_info(0).count());
1749 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1750 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1751
1752 data = countMetrics.data(2);
1753 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1754 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1755 EXPECT_EQ(1 /* uid field */,
1756 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1757 EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1758 ASSERT_EQ(1, data.bucket_info_size());
1759 EXPECT_EQ(1, data.bucket_info(0).count());
1760 // Partial bucket as metric is deactivated.
1761 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1762 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1763 data.bucket_info(0).end_bucket_elapsed_nanos());
1764
1765 data = countMetrics.data(3);
1766 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1767 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1768 EXPECT_EQ(1 /* uid field */,
1769 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1770 EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1771 ASSERT_EQ(1, data.bucket_info_size());
1772 EXPECT_EQ(1, data.bucket_info(0).count());
1773 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1774 data.bucket_info(0).start_bucket_elapsed_nanos());
1775 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1776 data.bucket_info(0).end_bucket_elapsed_nanos());
1777
1778 data = countMetrics.data(4);
1779 EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1780 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1781 EXPECT_EQ(1 /* uid field */,
1782 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1783 EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1784 ASSERT_EQ(1, data.bucket_info_size());
1785 EXPECT_EQ(1, data.bucket_info(0).count());
1786 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1787 data.bucket_info(0).start_bucket_elapsed_nanos());
1788 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1789 data.bucket_info(0).end_bucket_elapsed_nanos());
1790
1791 countMetrics.clear_data();
1792 sortMetricDataByDimensionsValue(reports.reports(0).metrics(1).count_metrics(), &countMetrics);
1793 ASSERT_EQ(5, countMetrics.data_size());
1794
1795 data = countMetrics.data(0);
1796 EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1797 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1798 EXPECT_EQ(1 /* uid field */,
1799 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1800 EXPECT_EQ(2222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1801 ASSERT_EQ(1, data.bucket_info_size());
1802 EXPECT_EQ(1, data.bucket_info(0).count());
1803 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1804 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1805
1806 data = countMetrics.data(1);
1807 EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1808 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1809 EXPECT_EQ(1 /* uid field */,
1810 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1811 EXPECT_EQ(3333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1812 ASSERT_EQ(1, data.bucket_info_size());
1813 EXPECT_EQ(1, data.bucket_info(0).count());
1814 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1815 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1816
1817 data = countMetrics.data(2);
1818 EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1819 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1820 EXPECT_EQ(1 /* uid field */,
1821 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1822 EXPECT_EQ(4444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1823 ASSERT_EQ(1, data.bucket_info_size());
1824 EXPECT_EQ(1, data.bucket_info(0).count());
1825 // Partial bucket as metric is deactivated.
1826 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1827 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1828 data.bucket_info(0).end_bucket_elapsed_nanos());
1829
1830 data = countMetrics.data(3);
1831 EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1832 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1833 EXPECT_EQ(1 /* uid field */,
1834 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1835 EXPECT_EQ(6666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1836 ASSERT_EQ(1, data.bucket_info_size());
1837 EXPECT_EQ(1, data.bucket_info(0).count());
1838 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1839 data.bucket_info(0).start_bucket_elapsed_nanos());
1840 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1841 data.bucket_info(0).end_bucket_elapsed_nanos());
1842
1843 data = countMetrics.data(4);
1844 EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1845 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1846 EXPECT_EQ(1 /* uid field */,
1847 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1848 EXPECT_EQ(7777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1849 ASSERT_EQ(1, data.bucket_info_size());
1850 EXPECT_EQ(1, data.bucket_info(0).count());
1851 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1852 data.bucket_info(0).start_bucket_elapsed_nanos());
1853 EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1854 data.bucket_info(0).end_bucket_elapsed_nanos());
1855 }
1856
1857 #else
1858 GTEST_LOG_(INFO) << "This test does nothing.\n";
1859 #endif
1860
1861 } // namespace statsd
1862 } // namespace os
1863 } // namespace android
1864