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 /* certificateHash */ {{}, {}, {}, {}});
115
116 std::vector<std::unique_ptr<LogEvent>> events;
117 // Events 1~4 are in the 1st bucket.
118 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
119 attributionTags1, "wl1"));
120 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 200, attributionUids2,
121 attributionTags2, "wl1"));
122 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs - 1,
123 attributionUids3, attributionTags3, "wl1"));
124 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs, attributionUids4,
125 attributionTags4, "wl1"));
126
127 // Events 5~8 are in the 3rd bucket.
128 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 1,
129 attributionUids5, attributionTags5, "wl2"));
130 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
131 attributionUids6, attributionTags6, "wl2"));
132 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs - 2,
133 attributionUids7, attributionTags7, "wl2"));
134 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs,
135 attributionUids8, attributionTags8, "wl2"));
136 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 1,
137 attributionUids9, attributionTags9, "wl2"));
138 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 100,
139 attributionUids9, attributionTags9, "wl2"));
140 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs - 1, 222,
141 isolatedUid, true /*is_create*/));
142 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs + 10, 222,
143 isolatedUid, false /*is_create*/));
144
145 sortLogEventsByTimestamp(&events);
146
147 for (const auto& event : events) {
148 processor->OnLogEvent(event.get());
149 }
150 ConfigMetricsReportList reports;
151 vector<uint8_t> buffer;
152 processor->onDumpReport(cfgKey, bucketStartTimeNs + 4 * bucketSizeNs + 1, false, true, ADB_DUMP,
153 FAST, &buffer);
154 EXPECT_TRUE(buffer.size() > 0);
155 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
156 backfillDimensionPath(&reports);
157 backfillStringInReport(&reports);
158 backfillStartEndTimestamp(&reports);
159 ASSERT_EQ(reports.reports_size(), 1);
160 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
161
162 StatsLogReport::CountMetricDataWrapper countMetrics;
163 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
164 ASSERT_EQ(countMetrics.data_size(), 4);
165
166 auto data = countMetrics.data(0);
167 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
168 util::WAKELOCK_STATE_CHANGED, 111, "App1");
169 ASSERT_EQ(data.bucket_info_size(), 2);
170 EXPECT_EQ(data.bucket_info(0).count(), 2);
171 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
172 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
173 EXPECT_EQ(data.bucket_info(1).count(), 1);
174 EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(),
175 bucketStartTimeNs + 2 * bucketSizeNs);
176 EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
177
178 data = countMetrics.data(1);
179 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
180 util::WAKELOCK_STATE_CHANGED, 222,
181 "GMSCoreModule1");
182 ASSERT_EQ(data.bucket_info_size(), 2);
183 EXPECT_EQ(data.bucket_info(0).count(), 1);
184 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
185 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
186 EXPECT_EQ(data.bucket_info(1).count(), 1);
187 EXPECT_EQ(data.bucket_info(1).start_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
188 EXPECT_EQ(data.bucket_info(1).end_bucket_elapsed_nanos(), bucketStartTimeNs + 2 * bucketSizeNs);
189
190 data = countMetrics.data(2);
191 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
192 util::WAKELOCK_STATE_CHANGED, 222,
193 "GMSCoreModule3");
194 ASSERT_EQ(data.bucket_info_size(), 1);
195 EXPECT_EQ(data.bucket_info(0).count(), 1);
196 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(),
197 bucketStartTimeNs + 3 * bucketSizeNs);
198 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 4 * bucketSizeNs);
199
200 data = countMetrics.data(3);
201 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
202 util::WAKELOCK_STATE_CHANGED, 444,
203 "GMSCoreModule2");
204 ASSERT_EQ(data.bucket_info_size(), 1);
205 EXPECT_EQ(data.bucket_info(0).count(), 1);
206 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(),
207 bucketStartTimeNs + 2 * bucketSizeNs);
208 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + 3 * bucketSizeNs);
209 }
210
TEST(AttributionE2eTest,TestAttributionMatchAndSliceByChain)211 TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain) {
212 auto config = CreateStatsdConfig(Position::ALL);
213 int64_t bucketStartTimeNs = 10000000000;
214 int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000;
215
216 ConfigKey cfgKey;
217 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
218 ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
219 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
220
221 // Here it assumes that GMS core has two uids.
222 processor->getUidMap()->updateMap(
223 1, {222, 444, 111, 333}, {1, 1, 2, 2},
224 {String16("v1"), String16("v1"), String16("v2"), String16("v2")},
225 {String16("com.android.gmscore"), String16("com.android.gmscore"), String16("app1"),
226 String16("APP3")},
227 {String16(""), String16(""), String16(""), String16("")},
228 /* certificateHash */ {{}, {}, {}, {}});
229
230 std::vector<std::unique_ptr<LogEvent>> events;
231 // Events 1~4 are in the 1st bucket.
232 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
233 attributionTags1, "wl1"));
234 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 200, attributionUids2,
235 attributionTags2, "wl1"));
236 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs - 1,
237 attributionUids3, attributionTags3, "wl1"));
238 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs, attributionUids4,
239 attributionTags4, "wl1"));
240
241 // Events 5~8 are in the 3rd bucket.
242 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 1,
243 attributionUids5, attributionTags5, "wl2"));
244 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 2 * bucketSizeNs + 100,
245 attributionUids6, attributionTags6, "wl2"));
246 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs - 2,
247 attributionUids7, attributionTags7, "wl2"));
248 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs,
249 attributionUids8, attributionTags8, "wl2"));
250 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 1,
251 attributionUids10, attributionTags10, "wl2"));
252 events.push_back(CreateAcquireWakelockEvent(bucketStartTimeNs + 3 * bucketSizeNs + 100,
253 attributionUids10, attributionTags10, "wl2"));
254 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs - 1, 222,
255 isolatedUid, true /*is_create*/));
256 events.push_back(CreateIsolatedUidChangedEvent(bucketStartTimeNs + 3 * bucketSizeNs + 10, 222,
257 isolatedUid, false /*is_create*/));
258
259 sortLogEventsByTimestamp(&events);
260
261 for (const auto& event : events) {
262 processor->OnLogEvent(event.get());
263 }
264 ConfigMetricsReportList reports;
265 vector<uint8_t> buffer;
266 processor->onDumpReport(cfgKey, bucketStartTimeNs + 4 * bucketSizeNs + 1, false, true, ADB_DUMP,
267 FAST, &buffer);
268 EXPECT_TRUE(buffer.size() > 0);
269 EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
270 backfillDimensionPath(&reports);
271 backfillStringInReport(&reports);
272 backfillStartEndTimestamp(&reports);
273 ASSERT_EQ(reports.reports_size(), 1);
274 ASSERT_EQ(reports.reports(0).metrics_size(), 1);
275
276 StatsLogReport::CountMetricDataWrapper countMetrics;
277 sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
278 ASSERT_EQ(countMetrics.data_size(), 6);
279
280 auto data = countMetrics.data(0);
281 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(),
282 util::WAKELOCK_STATE_CHANGED, 222,
283 "GMSCoreModule1");
284 ASSERT_EQ(2, data.bucket_info_size());
285 EXPECT_EQ(1, data.bucket_info(0).count());
286 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
287 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
288 EXPECT_EQ(1, data.bucket_info(1).count());
289 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs,
290 data.bucket_info(1).start_bucket_elapsed_nanos());
291 EXPECT_EQ(bucketStartTimeNs + 4 * bucketSizeNs, data.bucket_info(1).end_bucket_elapsed_nanos());
292
293 data = countMetrics.data(1);
294 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 222);
295 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
296 util::WAKELOCK_STATE_CHANGED, 222,
297 "GMSCoreModule1");
298 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 333);
299 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
300 util::WAKELOCK_STATE_CHANGED, 333, "App3");
301 ASSERT_EQ(data.bucket_info_size(), 1);
302 EXPECT_EQ(data.bucket_info(0).count(), 1);
303 EXPECT_EQ(data.bucket_info(0).start_bucket_elapsed_nanos(), bucketStartTimeNs);
304 EXPECT_EQ(data.bucket_info(0).end_bucket_elapsed_nanos(), bucketStartTimeNs + bucketSizeNs);
305
306 data = countMetrics.data(2);
307 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 444);
308 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
309 util::WAKELOCK_STATE_CHANGED, 444,
310 "GMSCoreModule2");
311 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 222);
312 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
313 util::WAKELOCK_STATE_CHANGED, 222,
314 "GMSCoreModule1");
315 ASSERT_EQ(data.bucket_info_size(), 1);
316 EXPECT_EQ(data.bucket_info(0).count(), 1);
317 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
318 data.bucket_info(0).start_bucket_elapsed_nanos());
319 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
320
321 data = countMetrics.data(3);
322 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
323 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
324 util::WAKELOCK_STATE_CHANGED, 111, "App1");
325 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 222);
326 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
327 util::WAKELOCK_STATE_CHANGED, 222,
328 "GMSCoreModule1");
329 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 333);
330 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
331 util::WAKELOCK_STATE_CHANGED, 333, "App3");
332 ASSERT_EQ(data.bucket_info_size(), 1);
333 EXPECT_EQ(data.bucket_info(0).count(), 1);
334 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
335 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
336
337 data = countMetrics.data(4);
338 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
339 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
340 util::WAKELOCK_STATE_CHANGED, 111, "App1");
341 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 333);
342 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
343 util::WAKELOCK_STATE_CHANGED, 333, "App3");
344 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 222);
345 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
346 util::WAKELOCK_STATE_CHANGED, 222,
347 "GMSCoreModule1");
348 ASSERT_EQ(data.bucket_info_size(), 1);
349 EXPECT_EQ(data.bucket_info(0).count(), 1);
350 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
351 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
352
353 data = countMetrics.data(5);
354 ValidateUidDimension(data.dimensions_in_what(), 0, util::WAKELOCK_STATE_CHANGED, 111);
355 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 0,
356 util::WAKELOCK_STATE_CHANGED, 111, "App1");
357 ValidateUidDimension(data.dimensions_in_what(), 1, util::WAKELOCK_STATE_CHANGED, 444);
358 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 1,
359 util::WAKELOCK_STATE_CHANGED, 444,
360 "GMSCoreModule2");
361 ValidateUidDimension(data.dimensions_in_what(), 2, util::WAKELOCK_STATE_CHANGED, 333);
362 ValidateAttributionUidAndTagDimension(data.dimensions_in_what(), 2,
363 util::WAKELOCK_STATE_CHANGED, 333, "App3");
364 ASSERT_EQ(data.bucket_info_size(), 1);
365 EXPECT_EQ(data.bucket_info(0).count(), 1);
366 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
367 data.bucket_info(0).start_bucket_elapsed_nanos());
368 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
369 }
370
371 #else
372 GTEST_LOG_(INFO) << "This test does nothing.\n";
373 #endif
374
375 } // namespace statsd
376 } // namespace os
377 } // namespace android
378