1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gtest/gtest.h>
16
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20
21 #include <iostream>
22 #include <vector>
23
24 namespace android {
25 namespace os {
26 namespace statsd {
27
28 #ifdef __ANDROID__
29
30 namespace {
31
CreateStatsdConfig(const Position position)32 StatsdConfig CreateStatsdConfig(const Position position) {
33 StatsdConfig config;
34 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
35 auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher();
36 auto attributionNodeMatcher =
37 wakelockAcquireMatcher.mutable_simple_atom_matcher()->add_field_value_matcher();
38 attributionNodeMatcher->set_field(1);
39 attributionNodeMatcher->set_position(Position::ANY);
40 auto uidMatcher = attributionNodeMatcher->mutable_matches_tuple()->add_field_value_matcher();
41 uidMatcher->set_field(1); // uid field.
42 uidMatcher->set_eq_string("com.android.gmscore");
43
44 *config.add_atom_matcher() = wakelockAcquireMatcher;
45
46 auto countMetric = config.add_count_metric();
47 countMetric->set_id(123456);
48 countMetric->set_what(wakelockAcquireMatcher.id());
49 *countMetric->mutable_dimensions_in_what() =
50 CreateAttributionUidAndTagDimensions(
51 util::WAKELOCK_STATE_CHANGED, {position});
52 countMetric->set_bucket(FIVE_MINUTES);
53 return config;
54 }
55
56 // GMS core node is in the middle.
57 std::vector<int> attributionUids1 = {111, 222, 333};
58 std::vector<string> attributionTags1 = {"App1", "GMSCoreModule1", "App3"};
59
60 // GMS core node is the last one.
61 std::vector<int> attributionUids2 = {111, 333, 222};
62 std::vector<string> attributionTags2 = {"App1", "App3", "GMSCoreModule1"};
63
64 // GMS core node is the first one.
65 std::vector<int> attributionUids3 = {222, 333};
66 std::vector<string> attributionTags3 = {"GMSCoreModule1", "App3"};
67
68 // Single GMS core node.
69 std::vector<int> attributionUids4 = {222};
70 std::vector<string> attributionTags4 = {"GMSCoreModule1"};
71
72 // GMS core has another uid.
73 std::vector<int> attributionUids5 = {111, 444, 333};
74 std::vector<string> attributionTags5 = {"App1", "GMSCoreModule2", "App3"};
75
76 // Multiple GMS core nodes.
77 std::vector<int> attributionUids6 = {444, 222};
78 std::vector<string> attributionTags6 = {"GMSCoreModule2", "GMSCoreModule1"};
79
80 // No GMS core nodes
81 std::vector<int> attributionUids7 = {111, 333};
82 std::vector<string> attributionTags7 = {"App1", "App3"};
83
84 std::vector<int> attributionUids8 = {111};
85 std::vector<string> attributionTags8 = {"App1"};
86
87 // GMS core node with isolated uid.
88 const int isolatedUid = 666;
89 std::vector<int> attributionUids9 = {isolatedUid};
90 std::vector<string> attributionTags9 = {"GMSCoreModule3"};
91
92 std::vector<int> attributionUids10 = {isolatedUid};
93 std::vector<string> attributionTags10 = {"GMSCoreModule1"};
94
95 } // namespace
96
TEST(AttributionE2eTest,TestAttributionMatchAndSliceByFirstUid)97 TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid) {
98 auto config = CreateStatsdConfig(Position::FIRST);
99 int64_t bucketStartTimeNs = 10000000000;
100 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000;
101
102 ConfigKey cfgKey;
103 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
104 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
105 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
106
107 // Here it assumes that GMS core has two uids.
108 processor->getUidMap()->updateMap(
109 1, {222, 444, 111, 333}, {1, 1, 2, 2},
110 {String16("v1"), String16("v1"), String16("v2"), String16("v2")},
111 {String16("com.android.gmscore"), String16("com.android.gmscore"), String16("app1"),
112 String16("APP3")},
113 {String16(""), String16(""), String16(""), String16("")});
114
115 std::vector<std::unique_ptr<LogEvent>> events;
116 // Events 1~4 are in the 1st bucket.
117 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
118 attributionTags1, "wl1"));
119 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 200, attributionUids2,
120 attributionTags2, "wl1"));
121 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs - 1,
122 attributionUids3, attributionTags3, "wl1"));
123 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs, attributionUids4,
124 attributionTags4, "wl1"));
125
126 // Events 5~8 are in the 3rd bucket.
127 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 1,
128 attributionUids5, attributionTags5, "wl2"));
129 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
130 attributionUids6, attributionTags6, "wl2"));
131 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs - 2,
132 attributionUids7, attributionTags7, "wl2"));
133 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs,
134 attributionUids8, attributionTags8, "wl2"));
135 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 1,
136 attributionUids9, attributionTags9, "wl2"));
137 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 100,
138 attributionUids9, attributionTags9, "wl2"));
139 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs - 1, 222,
140 isolatedUid, true /*is_create*/));
141 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs + 10, 222,
142 isolatedUid, false /*is_create*/));
143
144 sortLogEventsByTimestamp(&events);
145
146 for (const auto& event : events) {
147 processor->OnLogEvent(event.get());
148 }
149 ConfigMetricsReportList reports;
150 vector<uint8_t> buffer;
151 processor->onDumpReport(cfgKey, bucketStartTimeNs + 4 * bucketSizeNs + 1, false, true, ADB_DUMP,
152 FAST, &buffer);
153 EXPECT_TRUE(buffer.size() > 0);
154 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
155 backfillDimensionPath(&reports);
156 backfillStringInReport(&reports);
157 backfillStartEndTimestamp(&reports);
158 ASSERT_EQ(reports.reports_size(), 1);
159 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
160
161 StatsLogReport::CountMetricDataWrapper countMetrics;
162 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
163 ASSERT_EQ(countMetrics.data_size(), 4);
164
165 auto data = countMetrics.data(0);
166 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
167 util::WAKELOCK_STATE_CHANGED, 111, "App1");
168 ASSERT_EQ(data.bucket_info_size(), 2);
169 EXPECT_EQ(data.bucket_info(0).count(), 2);
170 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
171 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
172 EXPECT_EQ(data.bucket_info(1).count(), 1);
173 EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(),
174 bucketStartTimeNs + 2 * bucketSizeNs);
175 EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
176
177 data = countMetrics.data(1);
178 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
179 util::WAKELOCK_STATE_CHANGED, 222,
180 "GMSCoreModule1");
181 ASSERT_EQ(data.bucket_info_size(), 2);
182 EXPECT_EQ(data.bucket_info(0).count(), 1);
183 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
184 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
185 EXPECT_EQ(data.bucket_info(1).count(), 1);
186 EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
187 EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs);
188
189 data = countMetrics.data(2);
190 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
191 util::WAKELOCK_STATE_CHANGED, 222,
192 "GMSCoreModule3");
193 ASSERT_EQ(data.bucket_info_size(), 1);
194 EXPECT_EQ(data.bucket_info(0).count(), 1);
195 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(),
196 bucketStartTimeNs + 3 * bucketSizeNs);
197 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 4 * bucketSizeNs);
198
199 data = countMetrics.data(3);
200 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
201 util::WAKELOCK_STATE_CHANGED, 444,
202 "GMSCoreModule2");
203 ASSERT_EQ(data.bucket_info_size(), 1);
204 EXPECT_EQ(data.bucket_info(0).count(), 1);
205 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(),
206 bucketStartTimeNs + 2 * bucketSizeNs);
207 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
208 }
209
TEST(AttributionE2eTest,TestAttributionMatchAndSliceByChain)210 TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain) {
211 auto config = CreateStatsdConfig(Position::ALL);
212 int64_t bucketStartTimeNs = 10000000000;
213 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000;
214
215 ConfigKey cfgKey;
216 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
217 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
218 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
219
220 // Here it assumes that GMS core has two uids.
221 processor->getUidMap()->updateMap(
222 1, {222, 444, 111, 333}, {1, 1, 2, 2},
223 {String16("v1"), String16("v1"), String16("v2"), String16("v2")},
224 {String16("com.android.gmscore"), String16("com.android.gmscore"), String16("app1"),
225 String16("APP3")},
226 {String16(""), String16(""), String16(""), String16("")});
227
228 std::vector<std::unique_ptr<LogEvent>> events;
229 // Events 1~4 are in the 1st bucket.
230 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
231 attributionTags1, "wl1"));
232 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 200, attributionUids2,
233 attributionTags2, "wl1"));
234 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs - 1,
235 attributionUids3, attributionTags3, "wl1"));
236 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs, attributionUids4,
237 attributionTags4, "wl1"));
238
239 // Events 5~8 are in the 3rd bucket.
240 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 1,
241 attributionUids5, attributionTags5, "wl2"));
242 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
243 attributionUids6, attributionTags6, "wl2"));
244 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs - 2,
245 attributionUids7, attributionTags7, "wl2"));
246 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs,
247 attributionUids8, attributionTags8, "wl2"));
248 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 1,
249 attributionUids10, attributionTags10, "wl2"));
250 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 100,
251 attributionUids10, attributionTags10, "wl2"));
252 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs - 1, 222,
253 isolatedUid, true /*is_create*/));
254 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs + 10, 222,
255 isolatedUid, false /*is_create*/));
256
257 sortLogEventsByTimestamp(&events);
258
259 for (const auto& event : events) {
260 processor->OnLogEvent(event.get());
261 }
262 ConfigMetricsReportList reports;
263 vector<uint8_t> buffer;
264 processor->onDumpReport(cfgKey, bucketStartTimeNs + 4 * bucketSizeNs + 1, false, true, ADB_DUMP,
265 FAST, &buffer);
266 EXPECT_TRUE(buffer.size() > 0);
267 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
268 backfillDimensionPath(&reports);
269 backfillStringInReport(&reports);
270 backfillStartEndTimestamp(&reports);
271 ASSERT_EQ(reports.reports_size(), 1);
272 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
273
274 StatsLogReport::CountMetricDataWrapper countMetrics;
275 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
276 ASSERT_EQ(countMetrics.data_size(), 6);
277
278 auto data = countMetrics.data(0);
279 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
280 util::WAKELOCK_STATE_CHANGED, 222,
281 "GMSCoreModule1");
282 ASSERT_EQ(2, data.bucket_info_size());
283 EXPECT_EQ(1, data.bucket_info(0).count());
284 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
285 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
286 EXPECT_EQ(1, data.bucket_info(1).count());
287 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs,
288 data.bucket_info(1).start_bucket_elapsed_nanos());
289 EXPECT_EQ(bucketStartTimeNs + 4 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
290
291 data = countMetrics.data(1);
292 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 222);
293 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
294 util::WAKELOCK_STATE_CHANGED, 222,
295 "GMSCoreModule1");
296 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 333);
297 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
298 util::WAKELOCK_STATE_CHANGED, 333, "App3");
299 ASSERT_EQ(data.bucket_info_size(), 1);
300 EXPECT_EQ(data.bucket_info(0).count(), 1);
301 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
302 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
303
304 data = countMetrics.data(2);
305 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 444);
306 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
307 util::WAKELOCK_STATE_CHANGED, 444,
308 "GMSCoreModule2");
309 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 222);
310 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
311 util::WAKELOCK_STATE_CHANGED, 222,
312 "GMSCoreModule1");
313 ASSERT_EQ(data.bucket_info_size(), 1);
314 EXPECT_EQ(data.bucket_info(0).count(), 1);
315 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
316 data.bucket_info(0).start_bucket_elapsed_nanos());
317 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
318
319 data = countMetrics.data(3);
320 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
321 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
322 util::WAKELOCK_STATE_CHANGED, 111, "App1");
323 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 222);
324 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
325 util::WAKELOCK_STATE_CHANGED, 222,
326 "GMSCoreModule1");
327 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 333);
328 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
329 util::WAKELOCK_STATE_CHANGED, 333, "App3");
330 ASSERT_EQ(data.bucket_info_size(), 1);
331 EXPECT_EQ(data.bucket_info(0).count(), 1);
332 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
333 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
334
335 data = countMetrics.data(4);
336 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
337 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
338 util::WAKELOCK_STATE_CHANGED, 111, "App1");
339 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 333);
340 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
341 util::WAKELOCK_STATE_CHANGED, 333, "App3");
342 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 222);
343 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
344 util::WAKELOCK_STATE_CHANGED, 222,
345 "GMSCoreModule1");
346 ASSERT_EQ(data.bucket_info_size(), 1);
347 EXPECT_EQ(data.bucket_info(0).count(), 1);
348 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
349 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
350
351 data = countMetrics.data(5);
352 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
353 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
354 util::WAKELOCK_STATE_CHANGED, 111, "App1");
355 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 444);
356 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
357 util::WAKELOCK_STATE_CHANGED, 444,
358 "GMSCoreModule2");
359 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 333);
360 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
361 util::WAKELOCK_STATE_CHANGED, 333, "App3");
362 ASSERT_EQ(data.bucket_info_size(), 1);
363 EXPECT_EQ(data.bucket_info(0).count(), 1);
364 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
365 data.bucket_info(0).start_bucket_elapsed_nanos());
366 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
367 }
368
369 #else
370 GTEST_LOG_(INFO) << "This test does nothing.\n";
371 #endif
372
373 } // namespace statsd
374 } // namespace os
375 } // namespace android
376