1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <android/binder_interface_utils.h>
16 #include <gtest/gtest.h>
17
18 #include <vector>
19
20 #include "src/StatsLogProcessor.h"
21 #include "src/stats_log_util.h"
22 #include "tests/statsd_test_util.h"
23
24 using ::ndk::SharedRefBase;
25
26 namespace android {
27 namespace os {
28 namespace statsd {
29
30 #ifdef __ANDROID__
31
32 namespace {
33
34 const int64_t metricId = 123456;
35 const int32_t ATOM_TAG = util::SUBSYSTEM_SLEEP_STATE;
36
CreateStatsdConfig(const GaugeMetric::SamplingType sampling_type,bool useCondition=true)37 StatsdConfig CreateStatsdConfig(const GaugeMetric::SamplingType sampling_type,
38 bool useCondition = true) {
39 StatsdConfig config;
40 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
41 config.add_default_pull_packages("AID_ROOT"); // Fake puller is registered with root.
42 auto atomMatcher = CreateSimpleAtomMatcher("TestMatcher", ATOM_TAG);
43 *config.add_atom_matcher() = atomMatcher;
44 *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher();
45 *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher();
46
47 auto screenIsOffPredicate = CreateScreenIsOffPredicate();
48 *config.add_predicate() = screenIsOffPredicate;
49
50 auto gaugeMetric = config.add_gauge_metric();
51 gaugeMetric->set_id(metricId);
52 gaugeMetric->set_what(atomMatcher.id());
53 if (useCondition) {
54 gaugeMetric->set_condition(screenIsOffPredicate.id());
55 }
56 gaugeMetric->set_sampling_type(sampling_type);
57 gaugeMetric->mutable_gauge_fields_filter()->set_include_all(true);
58 *gaugeMetric->mutable_dimensions_in_what() =
59 CreateDimensions(ATOM_TAG, {1 /* subsystem name */});
60 gaugeMetric->set_bucket(FIVE_MINUTES);
61 gaugeMetric->set_max_pull_delay_sec(INT_MAX);
62 config.set_hash_strings_in_metric_report(false);
63
64 return config;
65 }
66
67 } // namespaces
68
TEST(GaugeMetricE2eTest,TestRandomSamplePulledEvents)69 TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents) {
70 auto config = CreateStatsdConfig(GaugeMetric::RANDOM_ONE_SAMPLE);
71 int64_t baseTimeNs = getElapsedRealtimeNs();
72 int64_t configAddedTimeNs = 10 * 60 * NS_PER_SEC + baseTimeNs;
73 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
74
75 ConfigKey cfgKey;
76 auto processor =
77 CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey,
78 SharedRefBase::make<FakeSubsystemSleepCallback>(), ATOM_TAG);
79 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
80 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
81 processor->mPullerManager->ForceClearPullerCache();
82
83 int startBucketNum = processor->mMetricsManagers.begin()
84 ->second->mAllMetricProducers[0]
85 ->getCurrentBucketNum();
86 EXPECT_GT(startBucketNum, (int64_t)0);
87
88 // When creating the config, the gauge metric producer should register the alarm at the
89 // end of the current bucket.
90 ASSERT_EQ((size_t)1, processor->mPullerManager->mReceivers.size());
91 EXPECT_EQ(bucketSizeNs,
92 processor->mPullerManager->mReceivers.begin()->second.front().intervalNs);
93 int64_t& nextPullTimeNs =
94 processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs;
95 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs);
96
97 auto screenOffEvent =
98 CreateScreenStateChangedEvent(configAddedTimeNs + 55, android::view::DISPLAY_STATE_OFF);
99 processor->OnLogEvent(screenOffEvent.get());
100
101 // Pulling alarm arrives on time and reset the sequential pulling alarm.
102 processor->informPullAlarmFired(nextPullTimeNs + 1);
103 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 2 * bucketSizeNs, nextPullTimeNs);
104
105 auto screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + bucketSizeNs + 10,
106 android::view::DISPLAY_STATE_ON);
107 processor->OnLogEvent(screenOnEvent.get());
108
109 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + bucketSizeNs + 100,
110 android::view::DISPLAY_STATE_OFF);
111 processor->OnLogEvent(screenOffEvent.get());
112
113 processor->informPullAlarmFired(nextPullTimeNs + 1);
114 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 3 * bucketSizeNs, nextPullTimeNs);
115
116 processor->informPullAlarmFired(nextPullTimeNs + 1);
117 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 4 * bucketSizeNs, nextPullTimeNs);
118
119 screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 3 * bucketSizeNs + 2,
120 android::view::DISPLAY_STATE_ON);
121 processor->OnLogEvent(screenOnEvent.get());
122
123 processor->informPullAlarmFired(nextPullTimeNs + 3);
124 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 5 * bucketSizeNs, nextPullTimeNs);
125
126 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 5 * bucketSizeNs + 1,
127 android::view::DISPLAY_STATE_OFF);
128 processor->OnLogEvent(screenOffEvent.get());
129
130 processor->informPullAlarmFired(nextPullTimeNs + 2);
131 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 6 * bucketSizeNs, nextPullTimeNs);
132
133 processor->informPullAlarmFired(nextPullTimeNs + 2);
134
135 ConfigMetricsReportList reports;
136 vector<uint8_t> buffer;
137 processor->onDumpReport(cfgKey, configAddedTimeNs + 7 * bucketSizeNs + 10, false, true,
138 ADB_DUMP, FAST, &buffer);
139 EXPECT_TRUE(buffer.size() > 0);
140 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
141 backfillDimensionPath(&reports);
142 backfillStringInReport(&reports);
143 backfillStartEndTimestamp(&reports);
144 ASSERT_EQ(1, reports.reports_size());
145 ASSERT_EQ(1, reports.reports(0).metrics_size());
146 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
147 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
148 ASSERT_GT((int)gaugeMetrics.data_size(), 1);
149
150 auto data = gaugeMetrics.data(0);
151 EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field());
152 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
153 EXPECT_EQ(1 /* subsystem name field */,
154 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
155 EXPECT_FALSE(data.dimensions_in_what().value_tuple().dimensions_value(0).value_str().empty());
156 ASSERT_EQ(6, data.bucket_info_size());
157
158 ASSERT_EQ(1, data.bucket_info(0).atom_size());
159 ASSERT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
160 EXPECT_EQ(configAddedTimeNs + 55, data.bucket_info(0).elapsed_timestamp_nanos(0));
161 ASSERT_EQ(0, data.bucket_info(0).wall_clock_timestamp_nanos_size());
162 EXPECT_EQ(baseTimeNs + 2 * bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
163 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
164 EXPECT_TRUE(data.bucket_info(0).atom(0).subsystem_sleep_state().subsystem_name().empty());
165 EXPECT_GT(data.bucket_info(0).atom(0).subsystem_sleep_state().time_millis(), 0);
166
167 ASSERT_EQ(1, data.bucket_info(1).atom_size());
168 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs + 1, data.bucket_info(1).elapsed_timestamp_nanos(0));
169 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs + 1, data.bucket_info(1).elapsed_timestamp_nanos(0));
170 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(1).start_bucket_elapsed_nanos());
171 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
172 EXPECT_TRUE(data.bucket_info(1).atom(0).subsystem_sleep_state().subsystem_name().empty());
173 EXPECT_GT(data.bucket_info(1).atom(0).subsystem_sleep_state().time_millis(), 0);
174
175 ASSERT_EQ(1, data.bucket_info(2).atom_size());
176 ASSERT_EQ(1, data.bucket_info(2).elapsed_timestamp_nanos_size());
177 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs + 1, data.bucket_info(2).elapsed_timestamp_nanos(0));
178 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, data.bucket_info(2).start_bucket_elapsed_nanos());
179 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs, data.bucket_info(2).end_bucket_elapsed_nanos());
180 EXPECT_TRUE(data.bucket_info(2).atom(0).subsystem_sleep_state().subsystem_name().empty());
181 EXPECT_GT(data.bucket_info(2).atom(0).subsystem_sleep_state().time_millis(), 0);
182
183 ASSERT_EQ(1, data.bucket_info(3).atom_size());
184 ASSERT_EQ(1, data.bucket_info(3).elapsed_timestamp_nanos_size());
185 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs + 1, data.bucket_info(3).elapsed_timestamp_nanos(0));
186 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs, data.bucket_info(3).start_bucket_elapsed_nanos());
187 EXPECT_EQ(baseTimeNs + 6 * bucketSizeNs, data.bucket_info(3).end_bucket_elapsed_nanos());
188 EXPECT_TRUE(data.bucket_info(3).atom(0).subsystem_sleep_state().subsystem_name().empty());
189 EXPECT_GT(data.bucket_info(3).atom(0).subsystem_sleep_state().time_millis(), 0);
190
191 ASSERT_EQ(1, data.bucket_info(4).atom_size());
192 ASSERT_EQ(1, data.bucket_info(4).elapsed_timestamp_nanos_size());
193 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs + 1, data.bucket_info(4).elapsed_timestamp_nanos(0));
194 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs, data.bucket_info(4).start_bucket_elapsed_nanos());
195 EXPECT_EQ(baseTimeNs + 8 * bucketSizeNs, data.bucket_info(4).end_bucket_elapsed_nanos());
196 EXPECT_TRUE(data.bucket_info(4).atom(0).subsystem_sleep_state().subsystem_name().empty());
197 EXPECT_GT(data.bucket_info(4).atom(0).subsystem_sleep_state().time_millis(), 0);
198
199 ASSERT_EQ(1, data.bucket_info(5).atom_size());
200 ASSERT_EQ(1, data.bucket_info(5).elapsed_timestamp_nanos_size());
201 EXPECT_EQ(baseTimeNs + 8 * bucketSizeNs + 2, data.bucket_info(5).elapsed_timestamp_nanos(0));
202 EXPECT_EQ(baseTimeNs + 8 * bucketSizeNs, data.bucket_info(5).start_bucket_elapsed_nanos());
203 EXPECT_EQ(baseTimeNs + 9 * bucketSizeNs, data.bucket_info(5).end_bucket_elapsed_nanos());
204 EXPECT_TRUE(data.bucket_info(5).atom(0).subsystem_sleep_state().subsystem_name().empty());
205 EXPECT_GT(data.bucket_info(5).atom(0).subsystem_sleep_state().time_millis(), 0);
206 }
207
TEST(GaugeMetricE2eTest,TestConditionChangeToTrueSamplePulledEvents)208 TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents) {
209 auto config = CreateStatsdConfig(GaugeMetric::CONDITION_CHANGE_TO_TRUE);
210 int64_t baseTimeNs = getElapsedRealtimeNs();
211 int64_t configAddedTimeNs = 10 * 60 * NS_PER_SEC + baseTimeNs;
212 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
213
214 ConfigKey cfgKey;
215 auto processor =
216 CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey,
217 SharedRefBase::make<FakeSubsystemSleepCallback>(), ATOM_TAG);
218 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
219 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
220 processor->mPullerManager->ForceClearPullerCache();
221
222 int startBucketNum = processor->mMetricsManagers.begin()
223 ->second->mAllMetricProducers[0]
224 ->getCurrentBucketNum();
225 EXPECT_GT(startBucketNum, (int64_t)0);
226
227 auto screenOffEvent =
228 CreateScreenStateChangedEvent(configAddedTimeNs + 55, android::view::DISPLAY_STATE_OFF);
229 processor->OnLogEvent(screenOffEvent.get());
230
231 auto screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + bucketSizeNs + 10,
232 android::view::DISPLAY_STATE_ON);
233 processor->OnLogEvent(screenOnEvent.get());
234
235 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + bucketSizeNs + 100,
236 android::view::DISPLAY_STATE_OFF);
237 processor->OnLogEvent(screenOffEvent.get());
238
239 screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 3 * bucketSizeNs + 2,
240 android::view::DISPLAY_STATE_ON);
241 processor->OnLogEvent(screenOnEvent.get());
242
243 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 5 * bucketSizeNs + 1,
244 android::view::DISPLAY_STATE_OFF);
245 processor->OnLogEvent(screenOffEvent.get());
246 screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 5 * bucketSizeNs + 3,
247 android::view::DISPLAY_STATE_ON);
248 processor->OnLogEvent(screenOnEvent.get());
249 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 5 * bucketSizeNs + 10,
250 android::view::DISPLAY_STATE_OFF);
251 processor->OnLogEvent(screenOffEvent.get());
252
253 ConfigMetricsReportList reports;
254 vector<uint8_t> buffer;
255 processor->onDumpReport(cfgKey, configAddedTimeNs + 8 * bucketSizeNs + 10, false, true,
256 ADB_DUMP, FAST, &buffer);
257 EXPECT_TRUE(buffer.size() > 0);
258 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
259 backfillDimensionPath(&reports);
260 backfillStringInReport(&reports);
261 backfillStartEndTimestamp(&reports);
262 ASSERT_EQ(1, reports.reports_size());
263 ASSERT_EQ(1, reports.reports(0).metrics_size());
264 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
265 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
266 ASSERT_GT((int)gaugeMetrics.data_size(), 1);
267
268 auto data = gaugeMetrics.data(0);
269 EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field());
270 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
271 EXPECT_EQ(1 /* subsystem name field */,
272 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
273 EXPECT_FALSE(data.dimensions_in_what().value_tuple().dimensions_value(0).value_str().empty());
274 ASSERT_EQ(3, data.bucket_info_size());
275
276 ASSERT_EQ(1, data.bucket_info(0).atom_size());
277 ASSERT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
278 EXPECT_EQ(configAddedTimeNs + 55, data.bucket_info(0).elapsed_timestamp_nanos(0));
279 ASSERT_EQ(0, data.bucket_info(0).wall_clock_timestamp_nanos_size());
280 EXPECT_EQ(baseTimeNs + 2 * bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
281 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
282 EXPECT_TRUE(data.bucket_info(0).atom(0).subsystem_sleep_state().subsystem_name().empty());
283 EXPECT_GT(data.bucket_info(0).atom(0).subsystem_sleep_state().time_millis(), 0);
284
285 ASSERT_EQ(1, data.bucket_info(1).atom_size());
286 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs + 100, data.bucket_info(1).elapsed_timestamp_nanos(0));
287 EXPECT_EQ(configAddedTimeNs + 55, data.bucket_info(0).elapsed_timestamp_nanos(0));
288 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(1).start_bucket_elapsed_nanos());
289 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
290 EXPECT_TRUE(data.bucket_info(1).atom(0).subsystem_sleep_state().subsystem_name().empty());
291 EXPECT_GT(data.bucket_info(1).atom(0).subsystem_sleep_state().time_millis(), 0);
292
293 ASSERT_EQ(2, data.bucket_info(2).atom_size());
294 ASSERT_EQ(2, data.bucket_info(2).elapsed_timestamp_nanos_size());
295 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs + 1, data.bucket_info(2).elapsed_timestamp_nanos(0));
296 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs + 10, data.bucket_info(2).elapsed_timestamp_nanos(1));
297 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs, data.bucket_info(2).start_bucket_elapsed_nanos());
298 EXPECT_EQ(baseTimeNs + 8 * bucketSizeNs, data.bucket_info(2).end_bucket_elapsed_nanos());
299 EXPECT_TRUE(data.bucket_info(2).atom(0).subsystem_sleep_state().subsystem_name().empty());
300 EXPECT_GT(data.bucket_info(2).atom(0).subsystem_sleep_state().time_millis(), 0);
301 EXPECT_TRUE(data.bucket_info(2).atom(1).subsystem_sleep_state().subsystem_name().empty());
302 EXPECT_GT(data.bucket_info(2).atom(1).subsystem_sleep_state().time_millis(), 0);
303 }
304
TEST(GaugeMetricE2eTest,TestRandomSamplePulledEvent_LateAlarm)305 TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm) {
306 auto config = CreateStatsdConfig(GaugeMetric::RANDOM_ONE_SAMPLE);
307 int64_t baseTimeNs = getElapsedRealtimeNs();
308 int64_t configAddedTimeNs = 10 * 60 * NS_PER_SEC + baseTimeNs;
309 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
310
311 ConfigKey cfgKey;
312 auto processor =
313 CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey,
314 SharedRefBase::make<FakeSubsystemSleepCallback>(), ATOM_TAG);
315 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
316 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
317 processor->mPullerManager->ForceClearPullerCache();
318
319 int startBucketNum = processor->mMetricsManagers.begin()
320 ->second->mAllMetricProducers[0]
321 ->getCurrentBucketNum();
322 EXPECT_GT(startBucketNum, (int64_t)0);
323
324 // When creating the config, the gauge metric producer should register the alarm at the
325 // end of the current bucket.
326 ASSERT_EQ((size_t)1, processor->mPullerManager->mReceivers.size());
327 EXPECT_EQ(bucketSizeNs,
328 processor->mPullerManager->mReceivers.begin()->second.front().intervalNs);
329 int64_t& nextPullTimeNs =
330 processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs;
331 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs);
332
333 auto screenOffEvent =
334 CreateScreenStateChangedEvent(configAddedTimeNs + 55, android::view::DISPLAY_STATE_OFF);
335 processor->OnLogEvent(screenOffEvent.get());
336
337 auto screenOnEvent = CreateScreenStateChangedEvent(configAddedTimeNs + bucketSizeNs + 10,
338 android::view::DISPLAY_STATE_ON);
339 processor->OnLogEvent(screenOnEvent.get());
340
341 // Pulling alarm arrives one bucket size late.
342 processor->informPullAlarmFired(nextPullTimeNs + bucketSizeNs);
343 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 3 * bucketSizeNs, nextPullTimeNs);
344
345 screenOffEvent = CreateScreenStateChangedEvent(configAddedTimeNs + 3 * bucketSizeNs + 11,
346 android::view::DISPLAY_STATE_OFF);
347 processor->OnLogEvent(screenOffEvent.get());
348
349 // Pulling alarm arrives more than one bucket size late.
350 processor->informPullAlarmFired(nextPullTimeNs + bucketSizeNs + 12);
351 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 5 * bucketSizeNs, nextPullTimeNs);
352
353 ConfigMetricsReportList reports;
354 vector<uint8_t> buffer;
355 processor->onDumpReport(cfgKey, configAddedTimeNs + 7 * bucketSizeNs + 10, false, true,
356 ADB_DUMP, FAST, &buffer);
357 EXPECT_TRUE(buffer.size() > 0);
358 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
359 backfillDimensionPath(&reports);
360 backfillStringInReport(&reports);
361 backfillStartEndTimestamp(&reports);
362 ASSERT_EQ(1, reports.reports_size());
363 ASSERT_EQ(1, reports.reports(0).metrics_size());
364 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
365 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
366 ASSERT_GT((int)gaugeMetrics.data_size(), 1);
367
368 auto data = gaugeMetrics.data(0);
369 EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field());
370 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
371 EXPECT_EQ(1 /* subsystem name field */,
372 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
373 EXPECT_FALSE(data.dimensions_in_what().value_tuple().dimensions_value(0).value_str().empty());
374 ASSERT_EQ(3, data.bucket_info_size());
375
376 ASSERT_EQ(1, data.bucket_info(0).atom_size());
377 ASSERT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
378 EXPECT_EQ(configAddedTimeNs + 55, data.bucket_info(0).elapsed_timestamp_nanos(0));
379 EXPECT_EQ(baseTimeNs + 2 * bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
380 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
381 EXPECT_TRUE(data.bucket_info(0).atom(0).subsystem_sleep_state().subsystem_name().empty());
382 EXPECT_GT(data.bucket_info(0).atom(0).subsystem_sleep_state().time_millis(), 0);
383
384 ASSERT_EQ(1, data.bucket_info(1).atom_size());
385 EXPECT_EQ(configAddedTimeNs + 3 * bucketSizeNs + 11,
386 data.bucket_info(1).elapsed_timestamp_nanos(0));
387 EXPECT_EQ(configAddedTimeNs + 55, data.bucket_info(0).elapsed_timestamp_nanos(0));
388 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs, data.bucket_info(1).start_bucket_elapsed_nanos());
389 EXPECT_EQ(baseTimeNs + 6 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
390 EXPECT_TRUE(data.bucket_info(1).atom(0).subsystem_sleep_state().subsystem_name().empty());
391 EXPECT_GT(data.bucket_info(1).atom(0).subsystem_sleep_state().time_millis(), 0);
392
393 ASSERT_EQ(1, data.bucket_info(2).atom_size());
394 ASSERT_EQ(1, data.bucket_info(2).elapsed_timestamp_nanos_size());
395 EXPECT_EQ(baseTimeNs + 6 * bucketSizeNs + 12, data.bucket_info(2).elapsed_timestamp_nanos(0));
396 EXPECT_EQ(baseTimeNs + 6 * bucketSizeNs, data.bucket_info(2).start_bucket_elapsed_nanos());
397 EXPECT_EQ(baseTimeNs + 7 * bucketSizeNs, data.bucket_info(2).end_bucket_elapsed_nanos());
398 EXPECT_TRUE(data.bucket_info(2).atom(0).subsystem_sleep_state().subsystem_name().empty());
399 EXPECT_GT(data.bucket_info(2).atom(0).subsystem_sleep_state().time_millis(), 0);
400 }
401
TEST(GaugeMetricE2eTest,TestRandomSamplePulledEventsWithActivation)402 TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation) {
403 auto config = CreateStatsdConfig(GaugeMetric::RANDOM_ONE_SAMPLE, /*useCondition=*/false);
404
405 int64_t baseTimeNs = getElapsedRealtimeNs();
406 int64_t configAddedTimeNs = 10 * 60 * NS_PER_SEC + baseTimeNs;
407 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
408
409 auto batterySaverStartMatcher = CreateBatterySaverModeStartAtomMatcher();
410 *config.add_atom_matcher() = batterySaverStartMatcher;
411 const int64_t ttlNs = 2 * bucketSizeNs; // Two buckets.
412 auto metric_activation = config.add_metric_activation();
413 metric_activation->set_metric_id(metricId);
414 metric_activation->set_activation_type(ACTIVATE_IMMEDIATELY);
415 auto event_activation = metric_activation->add_event_activation();
416 event_activation->set_atom_matcher_id(batterySaverStartMatcher.id());
417 event_activation->set_ttl_seconds(ttlNs / 1000000000);
418
419 ConfigKey cfgKey;
420 auto processor =
421 CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey,
422 SharedRefBase::make<FakeSubsystemSleepCallback>(), ATOM_TAG);
423 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
424 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
425 processor->mPullerManager->ForceClearPullerCache();
426
427 int startBucketNum = processor->mMetricsManagers.begin()
428 ->second->mAllMetricProducers[0]
429 ->getCurrentBucketNum();
430 EXPECT_GT(startBucketNum, (int64_t)0);
431 EXPECT_FALSE(processor->mMetricsManagers.begin()->second->mAllMetricProducers[0]->isActive());
432
433 // When creating the config, the gauge metric producer should register the alarm at the
434 // end of the current bucket.
435 ASSERT_EQ((size_t)1, processor->mPullerManager->mReceivers.size());
436 EXPECT_EQ(bucketSizeNs,
437 processor->mPullerManager->mReceivers.begin()->second.front().intervalNs);
438 int64_t& nextPullTimeNs =
439 processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs;
440 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs);
441
442 // Pulling alarm arrives on time and reset the sequential pulling alarm.
443 // Event should not be kept.
444 processor->informPullAlarmFired(nextPullTimeNs + 1); // 15 mins + 1 ns.
445 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 2 * bucketSizeNs, nextPullTimeNs);
446 EXPECT_FALSE(processor->mMetricsManagers.begin()->second->mAllMetricProducers[0]->isActive());
447
448 // Activate the metric. A pull occurs upon activation.
449 const int64_t activationNs = configAddedTimeNs + bucketSizeNs + (2 * 1000 * 1000); // 2 millis.
450 auto batterySaverOnEvent = CreateBatterySaverOnEvent(activationNs);
451 processor->OnLogEvent(batterySaverOnEvent.get()); // 15 mins + 2 ms.
452 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->mAllMetricProducers[0]->isActive());
453
454 // This event should be kept. 2 total.
455 processor->informPullAlarmFired(nextPullTimeNs + 1); // 20 mins + 1 ns.
456 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 3 * bucketSizeNs, nextPullTimeNs);
457
458 // This event should be kept. 3 total.
459 processor->informPullAlarmFired(nextPullTimeNs + 2); // 25 mins + 2 ns.
460 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 4 * bucketSizeNs, nextPullTimeNs);
461
462 // Create random event to deactivate metric.
463 auto deactivationEvent = CreateScreenBrightnessChangedEvent(activationNs + ttlNs + 1, 50);
464 processor->OnLogEvent(deactivationEvent.get());
465 EXPECT_FALSE(processor->mMetricsManagers.begin()->second->mAllMetricProducers[0]->isActive());
466
467 // Event should not be kept. 3 total.
468 processor->informPullAlarmFired(nextPullTimeNs + 3);
469 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 5 * bucketSizeNs, nextPullTimeNs);
470
471 processor->informPullAlarmFired(nextPullTimeNs + 2);
472
473 ConfigMetricsReportList reports;
474 vector<uint8_t> buffer;
475 processor->onDumpReport(cfgKey, configAddedTimeNs + 7 * bucketSizeNs + 10, false, true,
476 ADB_DUMP, FAST, &buffer);
477 EXPECT_TRUE(buffer.size() > 0);
478 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
479 backfillDimensionPath(&reports);
480 backfillStringInReport(&reports);
481 backfillStartEndTimestamp(&reports);
482 ASSERT_EQ(1, reports.reports_size());
483 ASSERT_EQ(1, reports.reports(0).metrics_size());
484 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
485 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
486 ASSERT_GT((int)gaugeMetrics.data_size(), 0);
487
488 auto data = gaugeMetrics.data(0);
489 EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field());
490 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
491 EXPECT_EQ(1 /* subsystem name field */,
492 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
493 EXPECT_FALSE(data.dimensions_in_what().value_tuple().dimensions_value(0).value_str().empty());
494 ASSERT_EQ(3, data.bucket_info_size());
495
496 auto bucketInfo = data.bucket_info(0);
497 ASSERT_EQ(1, bucketInfo.atom_size());
498 ASSERT_EQ(1, bucketInfo.elapsed_timestamp_nanos_size());
499 EXPECT_EQ(activationNs, bucketInfo.elapsed_timestamp_nanos(0));
500 ASSERT_EQ(0, bucketInfo.wall_clock_timestamp_nanos_size());
501 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, bucketInfo.start_bucket_elapsed_nanos());
502 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, bucketInfo.end_bucket_elapsed_nanos());
503 EXPECT_TRUE(bucketInfo.atom(0).subsystem_sleep_state().subsystem_name().empty());
504 EXPECT_GT(bucketInfo.atom(0).subsystem_sleep_state().time_millis(), 0);
505
506 bucketInfo = data.bucket_info(1);
507 ASSERT_EQ(1, bucketInfo.atom_size());
508 ASSERT_EQ(1, bucketInfo.elapsed_timestamp_nanos_size());
509 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs + 1, bucketInfo.elapsed_timestamp_nanos(0));
510 ASSERT_EQ(0, bucketInfo.wall_clock_timestamp_nanos_size());
511 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, bucketInfo.start_bucket_elapsed_nanos());
512 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs, bucketInfo.end_bucket_elapsed_nanos());
513 EXPECT_TRUE(bucketInfo.atom(0).subsystem_sleep_state().subsystem_name().empty());
514 EXPECT_GT(bucketInfo.atom(0).subsystem_sleep_state().time_millis(), 0);
515
516 bucketInfo = data.bucket_info(2);
517 ASSERT_EQ(1, bucketInfo.atom_size());
518 ASSERT_EQ(1, bucketInfo.elapsed_timestamp_nanos_size());
519 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs + 2, bucketInfo.elapsed_timestamp_nanos(0));
520 ASSERT_EQ(0, bucketInfo.wall_clock_timestamp_nanos_size());
521 EXPECT_EQ(MillisToNano(NanoToMillis(baseTimeNs + 5 * bucketSizeNs)),
522 bucketInfo.start_bucket_elapsed_nanos());
523 EXPECT_EQ(MillisToNano(NanoToMillis(activationNs + ttlNs + 1)),
524 bucketInfo.end_bucket_elapsed_nanos());
525 EXPECT_TRUE(bucketInfo.atom(0).subsystem_sleep_state().subsystem_name().empty());
526 EXPECT_GT(bucketInfo.atom(0).subsystem_sleep_state().time_millis(), 0);
527 }
528
TEST(GaugeMetricE2eTest,TestRandomSamplePulledEventsNoCondition)529 TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition) {
530 auto config = CreateStatsdConfig(GaugeMetric::RANDOM_ONE_SAMPLE, /*useCondition=*/false);
531
532 int64_t baseTimeNs = getElapsedRealtimeNs();
533 int64_t configAddedTimeNs = 10 * 60 * NS_PER_SEC + baseTimeNs;
534 int64_t bucketSizeNs =
535 TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
536
537 ConfigKey cfgKey;
538 auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey,
539 SharedRefBase::make<FakeSubsystemSleepCallback>(),
540 ATOM_TAG);
541 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
542 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
543 processor->mPullerManager->ForceClearPullerCache();
544
545 int startBucketNum = processor->mMetricsManagers.begin()->second->
546 mAllMetricProducers[0]->getCurrentBucketNum();
547 EXPECT_GT(startBucketNum, (int64_t)0);
548
549 // When creating the config, the gauge metric producer should register the alarm at the
550 // end of the current bucket.
551 ASSERT_EQ((size_t)1, processor->mPullerManager->mReceivers.size());
552 EXPECT_EQ(bucketSizeNs,
553 processor->mPullerManager->mReceivers.begin()->second.front().intervalNs);
554 int64_t& nextPullTimeNs =
555 processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs;
556 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs);
557
558 // Pulling alarm arrives on time and reset the sequential pulling alarm.
559 processor->informPullAlarmFired(nextPullTimeNs + 1);
560 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 2 * bucketSizeNs, nextPullTimeNs);
561
562 processor->informPullAlarmFired(nextPullTimeNs + 4);
563 EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + 3 * bucketSizeNs,
564 nextPullTimeNs);
565
566 ConfigMetricsReportList reports;
567 vector<uint8_t> buffer;
568 processor->onDumpReport(cfgKey, configAddedTimeNs + 7 * bucketSizeNs + 10, false, true,
569 ADB_DUMP, FAST, &buffer);
570 EXPECT_TRUE(buffer.size() > 0);
571 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
572 backfillDimensionPath(&reports);
573 backfillStringInReport(&reports);
574 backfillStartEndTimestamp(&reports);
575 ASSERT_EQ(1, reports.reports_size());
576 ASSERT_EQ(1, reports.reports(0).metrics_size());
577 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
578 sortMetricDataByDimensionsValue(
579 reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
580 ASSERT_GT((int)gaugeMetrics.data_size(), 0);
581
582 auto data = gaugeMetrics.data(0);
583 EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field());
584 ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
585 EXPECT_EQ(1 /* subsystem name field */,
586 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
587 EXPECT_FALSE(data.dimensions_in_what().value_tuple().dimensions_value(0).value_str().empty());
588 ASSERT_EQ(3, data.bucket_info_size());
589
590 ASSERT_EQ(1, data.bucket_info(0).atom_size());
591 ASSERT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
592 EXPECT_EQ(configAddedTimeNs, data.bucket_info(0).elapsed_timestamp_nanos(0));
593 ASSERT_EQ(0, data.bucket_info(0).wall_clock_timestamp_nanos_size());
594 EXPECT_EQ(baseTimeNs + 2 * bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
595 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
596 EXPECT_TRUE(data.bucket_info(0).atom(0).subsystem_sleep_state().subsystem_name().empty());
597 EXPECT_GT(data.bucket_info(0).atom(0).subsystem_sleep_state().time_millis(), 0);
598
599 ASSERT_EQ(1, data.bucket_info(1).atom_size());
600 ASSERT_EQ(1, data.bucket_info(1).elapsed_timestamp_nanos_size());
601 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs + 1, data.bucket_info(1).elapsed_timestamp_nanos(0));
602 ASSERT_EQ(0, data.bucket_info(1).wall_clock_timestamp_nanos_size());
603 EXPECT_EQ(baseTimeNs + 3 * bucketSizeNs, data.bucket_info(1).start_bucket_elapsed_nanos());
604 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
605 EXPECT_TRUE(data.bucket_info(1).atom(0).subsystem_sleep_state().subsystem_name().empty());
606 EXPECT_GT(data.bucket_info(1).atom(0).subsystem_sleep_state().time_millis(), 0);
607
608 ASSERT_EQ(1, data.bucket_info(2).atom_size());
609 ASSERT_EQ(1, data.bucket_info(2).elapsed_timestamp_nanos_size());
610 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs + 4, data.bucket_info(2).elapsed_timestamp_nanos(0));
611 ASSERT_EQ(0, data.bucket_info(2).wall_clock_timestamp_nanos_size());
612 EXPECT_EQ(baseTimeNs + 4 * bucketSizeNs, data.bucket_info(2).start_bucket_elapsed_nanos());
613 EXPECT_EQ(baseTimeNs + 5 * bucketSizeNs, data.bucket_info(2).end_bucket_elapsed_nanos());
614 EXPECT_TRUE(data.bucket_info(2).atom(0).subsystem_sleep_state().subsystem_name().empty());
615 EXPECT_GT(data.bucket_info(2).atom(0).subsystem_sleep_state().time_millis(), 0);
616 }
617
618 #else
619 GTEST_LOG_(INFO) << "This test does nothing.\n";
620 #endif
621
622 } // namespace statsd
623 } // namespace os
624 } // namespace android
625