David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 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 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 15 | #include <android/binder_ibinder.h> |
| 16 | #include <android/binder_interface_utils.h> |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 21 | #include "src/StatsLogProcessor.h" |
| 22 | #include "src/StatsService.h" |
| 23 | #include "src/stats_log_util.h" |
| 24 | #include "tests/statsd_test_util.h" |
| 25 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 26 | using::ndk::SharedRefBase; |
| 27 | using std::shared_ptr; |
| 28 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | #ifdef __ANDROID__ |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 34 | namespace { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 35 | const string kApp1 = "app1.sharing.1"; |
| 36 | const int kConfigKey = 789130123; // Randomly chosen to avoid collisions with existing configs. |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 37 | const int kCallingUid = 0; // Randomly chosen |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 38 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 39 | void SendConfig(shared_ptr<StatsService>& service, const StatsdConfig& config) { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 40 | string str; |
| 41 | config.SerializeToString(&str); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 42 | std::vector<int8_t> configAsVec(str.begin(), str.end()); |
| 43 | service->addConfiguration(kConfigKey, configAsVec, kCallingUid); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 44 | } |
| 45 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 46 | ConfigMetricsReport GetReports(sp<StatsLogProcessor> processor, int64_t timestamp, |
| 47 | bool include_current = false) { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 48 | vector<uint8_t> output; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 49 | ConfigKey configKey(AIBinder_getCallingUid(), kConfigKey); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 50 | processor->onDumpReport(configKey, timestamp, include_current /* include_current_bucket*/, |
Olivier Gaillard | e3c4b93 | 2019-03-15 17:58:04 +0000 | [diff] [blame] | 51 | true /* erase_data */, ADB_DUMP, NO_TIME_CONSTRAINTS, &output); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 52 | ConfigMetricsReportList reports; |
| 53 | reports.ParseFromArray(output.data(), output.size()); |
| 54 | EXPECT_EQ(1, reports.reports_size()); |
Jeffrey Huang | 94eafe7 | 2020-01-07 15:18:43 -0800 | [diff] [blame] | 55 | return reports.reports(kCallingUid); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | StatsdConfig MakeConfig() { |
| 59 | StatsdConfig config; |
| 60 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 61 | |
| 62 | auto appCrashMatcher = CreateProcessCrashAtomMatcher(); |
| 63 | *config.add_atom_matcher() = appCrashMatcher; |
| 64 | auto countMetric = config.add_count_metric(); |
| 65 | countMetric->set_id(StringToId("AppCrashes")); |
| 66 | countMetric->set_what(appCrashMatcher.id()); |
| 67 | countMetric->set_bucket(FIVE_MINUTES); |
| 68 | return config; |
| 69 | } |
| 70 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 71 | StatsdConfig MakeValueMetricConfig(int64_t minTime) { |
| 72 | StatsdConfig config; |
| 73 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 74 | config.add_default_pull_packages("AID_ROOT"); // Fake puller is registered with root. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 75 | |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 76 | auto pulledAtomMatcher = |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 77 | CreateSimpleAtomMatcher("TestMatcher", util::SUBSYSTEM_SLEEP_STATE); |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 78 | *config.add_atom_matcher() = pulledAtomMatcher; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 79 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 80 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 81 | |
| 82 | auto valueMetric = config.add_value_metric(); |
| 83 | valueMetric->set_id(123456); |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 84 | valueMetric->set_what(pulledAtomMatcher.id()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 85 | *valueMetric->mutable_value_field() = |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 86 | CreateDimensions(util::SUBSYSTEM_SLEEP_STATE, {4 /* time sleeping field */}); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 87 | *valueMetric->mutable_dimensions_in_what() = |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 88 | CreateDimensions(util::SUBSYSTEM_SLEEP_STATE, {1 /* subsystem name */}); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 89 | valueMetric->set_bucket(FIVE_MINUTES); |
| 90 | valueMetric->set_min_bucket_size_nanos(minTime); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 91 | valueMetric->set_use_absolute_value_on_reset(true); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 92 | valueMetric->set_skip_zero_diff_output(false); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 93 | return config; |
| 94 | } |
| 95 | |
| 96 | StatsdConfig MakeGaugeMetricConfig(int64_t minTime) { |
| 97 | StatsdConfig config; |
| 98 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 99 | config.add_default_pull_packages("AID_ROOT"); // Fake puller is registered with root. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 100 | |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 101 | auto pulledAtomMatcher = |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 102 | CreateSimpleAtomMatcher("TestMatcher", util::SUBSYSTEM_SLEEP_STATE); |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 103 | *config.add_atom_matcher() = pulledAtomMatcher; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 104 | *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); |
| 105 | *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); |
| 106 | |
| 107 | auto gaugeMetric = config.add_gauge_metric(); |
| 108 | gaugeMetric->set_id(123456); |
Tej Singh | d7b5118 | 2019-01-29 17:02:12 -0800 | [diff] [blame] | 109 | gaugeMetric->set_what(pulledAtomMatcher.id()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 110 | gaugeMetric->mutable_gauge_fields_filter()->set_include_all(true); |
| 111 | *gaugeMetric->mutable_dimensions_in_what() = |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 112 | CreateDimensions(util::SUBSYSTEM_SLEEP_STATE, {1 /* subsystem name */}); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 113 | gaugeMetric->set_bucket(FIVE_MINUTES); |
| 114 | gaugeMetric->set_min_bucket_size_nanos(minTime); |
| 115 | return config; |
| 116 | } |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 117 | } // anonymous namespace |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 118 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 119 | TEST(PartialBucketE2eTest, TestCountMetricWithoutSplit) { |
| 120 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 121 | SendConfig(service, MakeConfig()); |
| 122 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 123 | // initialized with. |
| 124 | |
| 125 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 1, 100).get()); |
| 126 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 2, 100).get()); |
| 127 | |
| 128 | ConfigMetricsReport report = GetReports(service->mProcessor, start + 3); |
| 129 | // Expect no metrics since the bucket has not finished yet. |
| 130 | EXPECT_EQ(1, report.metrics_size()); |
| 131 | EXPECT_EQ(0, report.metrics(0).count_metrics().data_size()); |
| 132 | } |
| 133 | |
| 134 | TEST(PartialBucketE2eTest, TestCountMetricNoSplitOnNewApp) { |
| 135 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 136 | SendConfig(service, MakeConfig()); |
| 137 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 138 | // initialized with. |
| 139 | |
| 140 | // Force the uidmap to update at timestamp 2. |
| 141 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 1, 100).get()); |
| 142 | // This is a new installation, so there shouldn't be a split (should be same as the without |
| 143 | // split case). |
| 144 | service->mUidMap->updateApp(start + 2, String16(kApp1.c_str()), 1, 2, String16("v2"), |
| 145 | String16("")); |
| 146 | // Goes into the second bucket. |
| 147 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 3, 100).get()); |
| 148 | |
| 149 | ConfigMetricsReport report = GetReports(service->mProcessor, start + 4); |
| 150 | EXPECT_EQ(1, report.metrics_size()); |
| 151 | EXPECT_EQ(0, report.metrics(0).count_metrics().data_size()); |
| 152 | } |
| 153 | |
| 154 | TEST(PartialBucketE2eTest, TestCountMetricSplitOnUpgrade) { |
| 155 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 156 | SendConfig(service, MakeConfig()); |
| 157 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 158 | // initialized with. |
| 159 | service->mUidMap->updateMap(start, {1}, {1}, {String16("v1")}, {String16(kApp1.c_str())}, |
| 160 | {String16("")}); |
| 161 | |
| 162 | // Force the uidmap to update at timestamp 2. |
| 163 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 1, 100).get()); |
| 164 | service->mUidMap->updateApp(start + 2, String16(kApp1.c_str()), 1, 2, String16("v2"), |
| 165 | String16("")); |
| 166 | // Goes into the second bucket. |
| 167 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 3, 100).get()); |
| 168 | |
| 169 | ConfigMetricsReport report = GetReports(service->mProcessor, start + 4); |
| 170 | backfillStartEndTimestamp(&report); |
| 171 | |
| 172 | ASSERT_EQ(1, report.metrics_size()); |
| 173 | ASSERT_EQ(1, report.metrics(0).count_metrics().data_size()); |
| 174 | ASSERT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info_size()); |
| 175 | EXPECT_TRUE(report.metrics(0) |
| 176 | .count_metrics() |
| 177 | .data(0) |
| 178 | .bucket_info(0) |
| 179 | .has_start_bucket_elapsed_nanos()); |
| 180 | EXPECT_TRUE(report.metrics(0) |
| 181 | .count_metrics() |
| 182 | .data(0) |
| 183 | .bucket_info(0) |
| 184 | .has_end_bucket_elapsed_nanos()); |
| 185 | EXPECT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info(0).count()); |
| 186 | } |
| 187 | |
| 188 | TEST(PartialBucketE2eTest, TestCountMetricSplitOnRemoval) { |
| 189 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 190 | SendConfig(service, MakeConfig()); |
| 191 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 192 | // initialized with. |
| 193 | service->mUidMap->updateMap(start, {1}, {1}, {String16("v1")}, {String16(kApp1.c_str())}, |
| 194 | {String16("")}); |
| 195 | |
| 196 | // Force the uidmap to update at timestamp 2. |
| 197 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 1, 100).get()); |
| 198 | service->mUidMap->removeApp(start + 2, String16(kApp1.c_str()), 1); |
| 199 | // Goes into the second bucket. |
| 200 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 3, 100).get()); |
| 201 | |
| 202 | ConfigMetricsReport report = GetReports(service->mProcessor, start + 4); |
| 203 | backfillStartEndTimestamp(&report); |
| 204 | |
| 205 | ASSERT_EQ(1, report.metrics_size()); |
| 206 | ASSERT_EQ(1, report.metrics(0).count_metrics().data_size()); |
| 207 | ASSERT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info_size()); |
| 208 | EXPECT_TRUE(report.metrics(0) |
| 209 | .count_metrics() |
| 210 | .data(0) |
| 211 | .bucket_info(0) |
| 212 | .has_start_bucket_elapsed_nanos()); |
| 213 | EXPECT_TRUE(report.metrics(0) |
| 214 | .count_metrics() |
| 215 | .data(0) |
| 216 | .bucket_info(0) |
| 217 | .has_end_bucket_elapsed_nanos()); |
| 218 | EXPECT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info(0).count()); |
| 219 | } |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 220 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 221 | TEST(PartialBucketE2eTest, TestCountMetricSplitOnBoot) { |
| 222 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 223 | SendConfig(service, MakeConfig()); |
| 224 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 225 | // initialized with. |
| 226 | |
| 227 | // Goes into the first bucket |
| 228 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + NS_PER_SEC, 100).get()); |
| 229 | int64_t bootCompleteTimeNs = start + 2 * NS_PER_SEC; |
| 230 | service->mProcessor->onStatsdInitCompleted(bootCompleteTimeNs); |
| 231 | // Goes into the second bucket. |
| 232 | service->mProcessor->OnLogEvent(CreateAppCrashEvent(start + 3 * NS_PER_SEC, 100).get()); |
| 233 | |
| 234 | ConfigMetricsReport report = GetReports(service->mProcessor, start + 4 * NS_PER_SEC); |
| 235 | backfillStartEndTimestamp(&report); |
| 236 | |
| 237 | ASSERT_EQ(1, report.metrics_size()); |
| 238 | ASSERT_EQ(1, report.metrics(0).count_metrics().data_size()); |
| 239 | ASSERT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info_size()); |
| 240 | EXPECT_TRUE(report.metrics(0) |
| 241 | .count_metrics() |
| 242 | .data(0) |
| 243 | .bucket_info(0) |
| 244 | .has_start_bucket_elapsed_nanos()); |
| 245 | EXPECT_EQ(MillisToNano(NanoToMillis(bootCompleteTimeNs)), |
| 246 | report.metrics(0).count_metrics().data(0).bucket_info(0).end_bucket_elapsed_nanos()); |
| 247 | EXPECT_EQ(1, report.metrics(0).count_metrics().data(0).bucket_info(0).count()); |
| 248 | } |
| 249 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 250 | TEST(PartialBucketE2eTest, TestValueMetricWithoutMinPartialBucket) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 251 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 252 | service->mPullerManager->RegisterPullAtomCallback( |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 253 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 254 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 255 | // Partial buckets don't occur when app is first installed. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 256 | service->mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 257 | SendConfig(service, MakeValueMetricConfig(0)); |
Yao Chen | cc884df | 2018-05-07 10:34:31 -0700 | [diff] [blame] | 258 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 259 | // initialized with. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 260 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 261 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 262 | int64_t appUpgradeTimeNs = 5 * 60 * NS_PER_SEC + start + 2 * NS_PER_SEC; |
| 263 | service->mUidMap->updateApp(appUpgradeTimeNs, String16(kApp1.c_str()), 1, 2, String16("v2"), |
| 264 | String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 265 | |
| 266 | ConfigMetricsReport report = |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 267 | GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100 * NS_PER_SEC); |
| 268 | backfillStartEndTimestamp(&report); |
| 269 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 270 | EXPECT_EQ(1, report.metrics_size()); |
| 271 | EXPECT_EQ(0, report.metrics(0).value_metrics().skipped_size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 272 | |
| 273 | // The fake subsystem state sleep puller returns two atoms. |
| 274 | ASSERT_EQ(2, report.metrics(0).value_metrics().data_size()); |
| 275 | ASSERT_EQ(2, report.metrics(0).value_metrics().data(0).bucket_info_size()); |
| 276 | EXPECT_EQ(MillisToNano(NanoToMillis(appUpgradeTimeNs)), |
| 277 | report.metrics(0).value_metrics().data(0).bucket_info(1).end_bucket_elapsed_nanos()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | TEST(PartialBucketE2eTest, TestValueMetricWithMinPartialBucket) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 281 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 282 | service->mPullerManager->RegisterPullAtomCallback( |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 283 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 284 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 285 | // Partial buckets don't occur when app is first installed. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 286 | service->mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 287 | SendConfig(service, MakeValueMetricConfig(60 * NS_PER_SEC /* One minute */)); |
Yao Chen | cc884df | 2018-05-07 10:34:31 -0700 | [diff] [blame] | 288 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 289 | // initialized with. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 290 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 291 | const int64_t endSkipped = 5 * 60 * NS_PER_SEC + start + 2 * NS_PER_SEC; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 292 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
| 293 | service->mUidMap->updateApp(endSkipped, String16(kApp1.c_str()), 1, 2, String16("v2"), |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 294 | String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 295 | |
| 296 | ConfigMetricsReport report = |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 297 | GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100 * NS_PER_SEC); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 298 | backfillStartEndTimestamp(&report); |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 299 | |
| 300 | ASSERT_EQ(1, report.metrics_size()); |
| 301 | ASSERT_EQ(1, report.metrics(0).value_metrics().skipped_size()); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 302 | EXPECT_TRUE(report.metrics(0).value_metrics().skipped(0).has_start_bucket_elapsed_nanos()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 303 | // Can't test the start time since it will be based on the actual time when the pulling occurs. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 304 | EXPECT_EQ(MillisToNano(NanoToMillis(endSkipped)), |
| 305 | report.metrics(0).value_metrics().skipped(0).end_bucket_elapsed_nanos()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 306 | |
| 307 | ASSERT_EQ(2, report.metrics(0).value_metrics().data_size()); |
| 308 | EXPECT_EQ(1, report.metrics(0).value_metrics().data(0).bucket_info_size()); |
| 309 | } |
| 310 | |
| 311 | TEST(PartialBucketE2eTest, TestValueMetricOnBootWithoutMinPartialBucket) { |
| 312 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 313 | // Initial pull will fail since puller is not registered. |
| 314 | SendConfig(service, MakeValueMetricConfig(0)); |
| 315 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 316 | // initialized with. |
| 317 | |
| 318 | service->mPullerManager->RegisterPullAtomCallback( |
| 319 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
| 320 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
| 321 | |
| 322 | int64_t bootCompleteTimeNs = start + NS_PER_SEC; |
| 323 | service->mProcessor->onStatsdInitCompleted(bootCompleteTimeNs); |
| 324 | |
| 325 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
| 326 | |
| 327 | ConfigMetricsReport report = GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100); |
| 328 | backfillStartEndTimestamp(&report); |
| 329 | |
| 330 | // First bucket is dropped due to the initial pull failing |
| 331 | ASSERT_EQ(1, report.metrics_size()); |
| 332 | EXPECT_EQ(1, report.metrics(0).value_metrics().skipped_size()); |
| 333 | EXPECT_EQ(MillisToNano(NanoToMillis(bootCompleteTimeNs)), |
| 334 | report.metrics(0).value_metrics().skipped(0).end_bucket_elapsed_nanos()); |
| 335 | |
| 336 | // The fake subsystem state sleep puller returns two atoms. |
| 337 | ASSERT_EQ(2, report.metrics(0).value_metrics().data_size()); |
| 338 | ASSERT_EQ(1, report.metrics(0).value_metrics().data(0).bucket_info_size()); |
| 339 | EXPECT_EQ( |
| 340 | MillisToNano(NanoToMillis(bootCompleteTimeNs)), |
| 341 | report.metrics(0).value_metrics().data(0).bucket_info(0).start_bucket_elapsed_nanos()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | TEST(PartialBucketE2eTest, TestGaugeMetricWithoutMinPartialBucket) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 345 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 346 | service->mPullerManager->RegisterPullAtomCallback( |
| 347 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
| 348 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 349 | // Partial buckets don't occur when app is first installed. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 350 | service->mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 351 | SendConfig(service, MakeGaugeMetricConfig(0)); |
Yao Chen | cc884df | 2018-05-07 10:34:31 -0700 | [diff] [blame] | 352 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 353 | // initialized with. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 354 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 355 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
| 356 | service->mUidMap->updateApp(5 * 60 * NS_PER_SEC + start + 2, String16(kApp1.c_str()), 1, 2, |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 357 | String16("v2"), String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 358 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 359 | ConfigMetricsReport report = GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100); |
| 360 | backfillStartEndTimestamp(&report); |
| 361 | ASSERT_EQ(1, report.metrics_size()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 362 | EXPECT_EQ(0, report.metrics(0).gauge_metrics().skipped_size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 363 | // The fake subsystem state sleep puller returns two atoms. |
| 364 | ASSERT_EQ(2, report.metrics(0).gauge_metrics().data_size()); |
| 365 | EXPECT_EQ(2, report.metrics(0).gauge_metrics().data(0).bucket_info_size()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | TEST(PartialBucketE2eTest, TestGaugeMetricWithMinPartialBucket) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 369 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 370 | // Partial buckets don't occur when app is first installed. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 371 | service->mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 372 | service->mPullerManager->RegisterPullAtomCallback( |
| 373 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
| 374 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 375 | SendConfig(service, MakeGaugeMetricConfig(60 * NS_PER_SEC /* One minute */)); |
Yao Chen | cc884df | 2018-05-07 10:34:31 -0700 | [diff] [blame] | 376 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 377 | // initialized with. |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 378 | |
| 379 | const int64_t endSkipped = 5 * 60 * NS_PER_SEC + start + 2; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 380 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
| 381 | service->mUidMap->updateApp(endSkipped, String16(kApp1.c_str()), 1, 2, String16("v2"), |
| 382 | String16("")); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 383 | |
| 384 | ConfigMetricsReport report = |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 385 | GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100 * NS_PER_SEC); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 386 | backfillStartEndTimestamp(&report); |
Tej Singh | 9ec159a | 2019-11-14 11:59:48 -0800 | [diff] [blame] | 387 | ASSERT_EQ(1, report.metrics_size()); |
| 388 | ASSERT_EQ(1, report.metrics(0).gauge_metrics().skipped_size()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 389 | // Can't test the start time since it will be based on the actual time when the pulling occurs. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 390 | EXPECT_TRUE(report.metrics(0).gauge_metrics().skipped(0).has_start_bucket_elapsed_nanos()); |
| 391 | EXPECT_EQ(MillisToNano(NanoToMillis(endSkipped)), |
| 392 | report.metrics(0).gauge_metrics().skipped(0).end_bucket_elapsed_nanos()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 393 | ASSERT_EQ(2, report.metrics(0).gauge_metrics().data_size()); |
| 394 | EXPECT_EQ(1, report.metrics(0).gauge_metrics().data(0).bucket_info_size()); |
| 395 | } |
| 396 | |
| 397 | TEST(PartialBucketE2eTest, TestGaugeMetricOnBootWithoutMinPartialBucket) { |
| 398 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 399 | // Initial pull will fail since puller hasn't been registered. |
| 400 | SendConfig(service, MakeGaugeMetricConfig(0)); |
| 401 | int64_t start = getElapsedRealtimeNs(); // This is the start-time the metrics producers are |
| 402 | // initialized with. |
| 403 | |
| 404 | service->mPullerManager->RegisterPullAtomCallback( |
| 405 | /*uid=*/0, util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, |
| 406 | SharedRefBase::make<FakeSubsystemSleepCallback>()); |
| 407 | |
| 408 | int64_t bootCompleteTimeNs = start + NS_PER_SEC; |
| 409 | service->mProcessor->onStatsdInitCompleted(bootCompleteTimeNs); |
| 410 | |
| 411 | service->mProcessor->informPullAlarmFired(5 * 60 * NS_PER_SEC + start); |
| 412 | |
| 413 | ConfigMetricsReport report = GetReports(service->mProcessor, 5 * 60 * NS_PER_SEC + start + 100); |
| 414 | backfillStartEndTimestamp(&report); |
| 415 | |
| 416 | ASSERT_EQ(1, report.metrics_size()); |
| 417 | EXPECT_EQ(0, report.metrics(0).gauge_metrics().skipped_size()); |
| 418 | // The fake subsystem state sleep puller returns two atoms. |
| 419 | ASSERT_EQ(2, report.metrics(0).gauge_metrics().data_size()); |
| 420 | // No data in the first bucket, so nothing is reported |
| 421 | ASSERT_EQ(1, report.metrics(0).gauge_metrics().data(0).bucket_info_size()); |
| 422 | EXPECT_EQ( |
| 423 | MillisToNano(NanoToMillis(bootCompleteTimeNs)), |
| 424 | report.metrics(0).gauge_metrics().data(0).bucket_info(0).start_bucket_elapsed_nanos()); |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 425 | } |
| 426 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 427 | #else |
| 428 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 429 | #endif |
| 430 | |
| 431 | } // namespace statsd |
| 432 | } // namespace os |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 433 | } // namespace android |