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