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 <vector>
22
23 namespace android {
24 namespace os {
25 namespace statsd {
26
27 #ifdef __ANDROID__
28
29 namespace {
30
CreateStatsdConfigForPushedEvent(const GaugeMetric::SamplingType sampling_type)31 StatsdConfig CreateStatsdConfigForPushedEvent(const GaugeMetric::SamplingType sampling_type) {
32 StatsdConfig config;
33 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
34 *config.add_atom_matcher() = CreateMoveToBackgroundAtomMatcher();
35 *config.add_atom_matcher() = CreateMoveToForegroundAtomMatcher();
36
37 auto atomMatcher = CreateSimpleAtomMatcher("", android::util::APP_START_OCCURRED);
38 *config.add_atom_matcher() = atomMatcher;
39
40 auto isInBackgroundPredicate = CreateIsInBackgroundPredicate();
41 *isInBackgroundPredicate.mutable_simple_predicate()->mutable_dimensions() =
42 CreateDimensions(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, {1 /* uid field */ });
43 *config.add_predicate() = isInBackgroundPredicate;
44
45 auto gaugeMetric = config.add_gauge_metric();
46 gaugeMetric->set_id(123456);
47 gaugeMetric->set_what(atomMatcher.id());
48 gaugeMetric->set_condition(isInBackgroundPredicate.id());
49 gaugeMetric->mutable_gauge_fields_filter()->set_include_all(false);
50 gaugeMetric->set_sampling_type(sampling_type);
51 auto fieldMatcher = gaugeMetric->mutable_gauge_fields_filter()->mutable_fields();
52 fieldMatcher->set_field(android::util::APP_START_OCCURRED);
53 fieldMatcher->add_child()->set_field(3); // type (enum)
54 fieldMatcher->add_child()->set_field(4); // activity_name(str)
55 fieldMatcher->add_child()->set_field(7); // activity_start_msec(int64)
56 *gaugeMetric->mutable_dimensions_in_what() =
57 CreateDimensions(android::util::APP_START_OCCURRED, {1 /* uid field */ });
58 gaugeMetric->set_bucket(FIVE_MINUTES);
59
60 auto links = gaugeMetric->add_links();
61 links->set_condition(isInBackgroundPredicate.id());
62 auto dimensionWhat = links->mutable_fields_in_what();
63 dimensionWhat->set_field(android::util::APP_START_OCCURRED);
64 dimensionWhat->add_child()->set_field(1); // uid field.
65 auto dimensionCondition = links->mutable_fields_in_condition();
66 dimensionCondition->set_field(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED);
67 dimensionCondition->add_child()->set_field(1); // uid field.
68 return config;
69 }
70
CreateAppStartOccurredEvent(const int uid,const string & pkg_name,AppStartOccurred::TransitionType type,const string & activity_name,const string & calling_pkg_name,const bool is_instant_app,int64_t activity_start_msec,uint64_t timestampNs)71 std::unique_ptr<LogEvent> CreateAppStartOccurredEvent(
72 const int uid, const string& pkg_name, AppStartOccurred::TransitionType type,
73 const string& activity_name, const string& calling_pkg_name, const bool is_instant_app,
74 int64_t activity_start_msec, uint64_t timestampNs) {
75 auto logEvent = std::make_unique<LogEvent>(
76 android::util::APP_START_OCCURRED, timestampNs);
77 logEvent->write(uid);
78 logEvent->write(pkg_name);
79 logEvent->write(type);
80 logEvent->write(activity_name);
81 logEvent->write(calling_pkg_name);
82 logEvent->write(is_instant_app);
83 logEvent->write(activity_start_msec);
84 logEvent->init();
85 return logEvent;
86 }
87
88 } // namespace
89
TEST(GaugeMetricE2eTest,TestMultipleFieldsForPushedEvent)90 TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent) {
91 for (const auto& sampling_type :
92 { GaugeMetric::FIRST_N_SAMPLES, GaugeMetric:: RANDOM_ONE_SAMPLE }) {
93 auto config = CreateStatsdConfigForPushedEvent(sampling_type);
94 int64_t bucketStartTimeNs = 10000000000;
95 int64_t bucketSizeNs =
96 TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000;
97
98 ConfigKey cfgKey;
99 auto processor = CreateStatsLogProcessor(
100 bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
101 EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
102 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
103
104 int appUid1 = 123;
105 int appUid2 = 456;
106 std::vector<std::unique_ptr<LogEvent>> events;
107 events.push_back(CreateMoveToBackgroundEvent(appUid1, bucketStartTimeNs + 15));
108 events.push_back(CreateMoveToForegroundEvent(
109 appUid1, bucketStartTimeNs + bucketSizeNs + 250));
110 events.push_back(CreateMoveToBackgroundEvent(
111 appUid1, bucketStartTimeNs + bucketSizeNs + 350));
112 events.push_back(CreateMoveToForegroundEvent(
113 appUid1, bucketStartTimeNs + 2 * bucketSizeNs + 100));
114
115
116 events.push_back(CreateAppStartOccurredEvent(
117 appUid1, "app1", AppStartOccurred::WARM, "activity_name1", "calling_pkg_name1",
118 true /*is_instant_app*/, 101 /*activity_start_msec*/, bucketStartTimeNs + 10));
119 events.push_back(CreateAppStartOccurredEvent(
120 appUid1, "app1", AppStartOccurred::HOT, "activity_name2", "calling_pkg_name2",
121 true /*is_instant_app*/, 102 /*activity_start_msec*/, bucketStartTimeNs + 20));
122 events.push_back(CreateAppStartOccurredEvent(
123 appUid1, "app1", AppStartOccurred::COLD, "activity_name3", "calling_pkg_name3",
124 true /*is_instant_app*/, 103 /*activity_start_msec*/, bucketStartTimeNs + 30));
125 events.push_back(CreateAppStartOccurredEvent(
126 appUid1, "app1", AppStartOccurred::WARM, "activity_name4", "calling_pkg_name4",
127 true /*is_instant_app*/, 104 /*activity_start_msec*/,
128 bucketStartTimeNs + bucketSizeNs + 30));
129 events.push_back(CreateAppStartOccurredEvent(
130 appUid1, "app1", AppStartOccurred::COLD, "activity_name5", "calling_pkg_name5",
131 true /*is_instant_app*/, 105 /*activity_start_msec*/,
132 bucketStartTimeNs + 2 * bucketSizeNs));
133 events.push_back(CreateAppStartOccurredEvent(
134 appUid1, "app1", AppStartOccurred::HOT, "activity_name6", "calling_pkg_name6",
135 false /*is_instant_app*/, 106 /*activity_start_msec*/,
136 bucketStartTimeNs + 2 * bucketSizeNs + 10));
137
138 events.push_back(CreateMoveToBackgroundEvent(
139 appUid2, bucketStartTimeNs + bucketSizeNs + 10));
140 events.push_back(CreateAppStartOccurredEvent(
141 appUid2, "app2", AppStartOccurred::COLD, "activity_name7", "calling_pkg_name7",
142 true /*is_instant_app*/, 201 /*activity_start_msec*/,
143 bucketStartTimeNs + 2 * bucketSizeNs + 10));
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 + 3 * bucketSizeNs, false, true,
153 ADB_DUMP, 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 EXPECT_EQ(1, reports.reports_size());
160 EXPECT_EQ(1, reports.reports(0).metrics_size());
161 StatsLogReport::GaugeMetricDataWrapper gaugeMetrics;
162 sortMetricDataByDimensionsValue(
163 reports.reports(0).metrics(0).gauge_metrics(), &gaugeMetrics);
164 EXPECT_EQ(2, gaugeMetrics.data_size());
165
166 auto data = gaugeMetrics.data(0);
167 EXPECT_EQ(android::util::APP_START_OCCURRED, data.dimensions_in_what().field());
168 EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
169 EXPECT_EQ(1 /* uid field */,
170 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
171 EXPECT_EQ(appUid1, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
172 EXPECT_EQ(3, data.bucket_info_size());
173 if (sampling_type == GaugeMetric::FIRST_N_SAMPLES) {
174 EXPECT_EQ(2, data.bucket_info(0).atom_size());
175 EXPECT_EQ(2, data.bucket_info(0).elapsed_timestamp_nanos_size());
176 EXPECT_EQ(0, data.bucket_info(0).wall_clock_timestamp_nanos_size());
177 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
178 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs,
179 data.bucket_info(0).end_bucket_elapsed_nanos());
180 EXPECT_EQ(AppStartOccurred::HOT,
181 data.bucket_info(0).atom(0).app_start_occurred().type());
182 EXPECT_EQ("activity_name2",
183 data.bucket_info(0).atom(0).app_start_occurred().activity_name());
184 EXPECT_EQ(102L,
185 data.bucket_info(0).atom(0).app_start_occurred().activity_start_millis());
186 EXPECT_EQ(AppStartOccurred::COLD,
187 data.bucket_info(0).atom(1).app_start_occurred().type());
188 EXPECT_EQ("activity_name3",
189 data.bucket_info(0).atom(1).app_start_occurred().activity_name());
190 EXPECT_EQ(103L,
191 data.bucket_info(0).atom(1).app_start_occurred().activity_start_millis());
192
193 EXPECT_EQ(1, data.bucket_info(1).atom_size());
194 EXPECT_EQ(1, data.bucket_info(1).elapsed_timestamp_nanos_size());
195 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs,
196 data.bucket_info(1).start_bucket_elapsed_nanos());
197 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
198 data.bucket_info(1).end_bucket_elapsed_nanos());
199 EXPECT_EQ(AppStartOccurred::WARM,
200 data.bucket_info(1).atom(0).app_start_occurred().type());
201 EXPECT_EQ("activity_name4",
202 data.bucket_info(1).atom(0).app_start_occurred().activity_name());
203 EXPECT_EQ(104L,
204 data.bucket_info(1).atom(0).app_start_occurred().activity_start_millis());
205
206 EXPECT_EQ(2, data.bucket_info(2).atom_size());
207 EXPECT_EQ(2, data.bucket_info(2).elapsed_timestamp_nanos_size());
208 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
209 data.bucket_info(2).start_bucket_elapsed_nanos());
210 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs,
211 data.bucket_info(2).end_bucket_elapsed_nanos());
212 EXPECT_EQ(AppStartOccurred::COLD,
213 data.bucket_info(2).atom(0).app_start_occurred().type());
214 EXPECT_EQ("activity_name5",
215 data.bucket_info(2).atom(0).app_start_occurred().activity_name());
216 EXPECT_EQ(105L,
217 data.bucket_info(2).atom(0).app_start_occurred().activity_start_millis());
218 EXPECT_EQ(AppStartOccurred::HOT,
219 data.bucket_info(2).atom(1).app_start_occurred().type());
220 EXPECT_EQ("activity_name6",
221 data.bucket_info(2).atom(1).app_start_occurred().activity_name());
222 EXPECT_EQ(106L,
223 data.bucket_info(2).atom(1).app_start_occurred().activity_start_millis());
224 } else {
225 EXPECT_EQ(1, data.bucket_info(0).atom_size());
226 EXPECT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
227 EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
228 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs,
229 data.bucket_info(0).end_bucket_elapsed_nanos());
230 EXPECT_EQ(AppStartOccurred::HOT,
231 data.bucket_info(0).atom(0).app_start_occurred().type());
232 EXPECT_EQ("activity_name2",
233 data.bucket_info(0).atom(0).app_start_occurred().activity_name());
234 EXPECT_EQ(102L,
235 data.bucket_info(0).atom(0).app_start_occurred().activity_start_millis());
236
237 EXPECT_EQ(1, data.bucket_info(1).atom_size());
238 EXPECT_EQ(1, data.bucket_info(1).elapsed_timestamp_nanos_size());
239 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs,
240 data.bucket_info(1).start_bucket_elapsed_nanos());
241 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
242 data.bucket_info(1).end_bucket_elapsed_nanos());
243 EXPECT_EQ(AppStartOccurred::WARM,
244 data.bucket_info(1).atom(0).app_start_occurred().type());
245 EXPECT_EQ("activity_name4",
246 data.bucket_info(1).atom(0).app_start_occurred().activity_name());
247 EXPECT_EQ(104L,
248 data.bucket_info(1).atom(0).app_start_occurred().activity_start_millis());
249
250 EXPECT_EQ(1, data.bucket_info(2).atom_size());
251 EXPECT_EQ(1, data.bucket_info(2).elapsed_timestamp_nanos_size());
252 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
253 data.bucket_info(2).start_bucket_elapsed_nanos());
254 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs,
255 data.bucket_info(2).end_bucket_elapsed_nanos());
256 EXPECT_EQ(AppStartOccurred::COLD,
257 data.bucket_info(2).atom(0).app_start_occurred().type());
258 EXPECT_EQ("activity_name5",
259 data.bucket_info(2).atom(0).app_start_occurred().activity_name());
260 EXPECT_EQ(105L,
261 data.bucket_info(2).atom(0).app_start_occurred().activity_start_millis());
262 }
263
264 data = gaugeMetrics.data(1);
265
266 EXPECT_EQ(data.dimensions_in_what().field(), android::util::APP_START_OCCURRED);
267 EXPECT_EQ(data.dimensions_in_what().value_tuple().dimensions_value_size(), 1);
268 EXPECT_EQ(1 /* uid field */,
269 data.dimensions_in_what().value_tuple().dimensions_value(0).field());
270 EXPECT_EQ(appUid2, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
271 EXPECT_EQ(1, data.bucket_info_size());
272 EXPECT_EQ(1, data.bucket_info(0).atom_size());
273 EXPECT_EQ(1, data.bucket_info(0).elapsed_timestamp_nanos_size());
274 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
275 data.bucket_info(0).start_bucket_elapsed_nanos());
276 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs,
277 data.bucket_info(0).end_bucket_elapsed_nanos());
278 EXPECT_EQ(AppStartOccurred::COLD, data.bucket_info(0).atom(0).app_start_occurred().type());
279 EXPECT_EQ("activity_name7",
280 data.bucket_info(0).atom(0).app_start_occurred().activity_name());
281 EXPECT_EQ(201L, data.bucket_info(0).atom(0).app_start_occurred().activity_start_millis());
282 }
283 }
284
285 #else
286 GTEST_LOG_(INFO) << "This test does nothing.\n";
287 #endif
288
289 } // namespace statsd
290 } // namespace os
291 } // namespace android
292