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