diff --git a/cmds/statsd/src/external/StatsPullerManager.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp index fef213dad60e9..46d44e3ada1b9 100644 --- a/cmds/statsd/src/external/StatsPullerManager.cpp +++ b/cmds/statsd/src/external/StatsPullerManager.cpp @@ -84,7 +84,7 @@ bool StatsPullerManager::PullLocked(int tagId, vector>* dat } return ret; } else { - VLOG("Unknown tagId %d", tagId); + ALOGW("StatsPullerManager: Unknown tagId %d", tagId); return false; // Return early since we don't know what to pull. } } diff --git a/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp b/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp index c7ba9be3ca5a6..5e60abaf77923 100644 --- a/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp +++ b/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp @@ -14,12 +14,12 @@ #include +#include + #include "src/StatsLogProcessor.h" #include "src/stats_log_util.h" #include "tests/statsd_test_util.h" -#include - namespace android { namespace os { namespace statsd { @@ -29,12 +29,13 @@ namespace statsd { namespace { const int64_t metricId = 123456; +const int32_t ATOM_TAG = android::util::SUBSYSTEM_SLEEP_STATE; StatsdConfig CreateStatsdConfig(const GaugeMetric::SamplingType sampling_type, bool useCondition = true) { StatsdConfig config; config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. - auto atomMatcher = CreateSimpleAtomMatcher("TestMatcher", android::util::SUBSYSTEM_SLEEP_STATE); + auto atomMatcher = CreateSimpleAtomMatcher("TestMatcher", ATOM_TAG); *config.add_atom_matcher() = atomMatcher; *config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher(); *config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher(); @@ -51,7 +52,7 @@ StatsdConfig CreateStatsdConfig(const GaugeMetric::SamplingType sampling_type, gaugeMetric->set_sampling_type(sampling_type); gaugeMetric->mutable_gauge_fields_filter()->set_include_all(true); *gaugeMetric->mutable_dimensions_in_what() = - CreateDimensions(android::util::SUBSYSTEM_SLEEP_STATE, {1 /* subsystem name */}); + CreateDimensions(ATOM_TAG, {1 /* subsystem name */}); gaugeMetric->set_bucket(FIVE_MINUTES); gaugeMetric->set_max_pull_delay_sec(INT_MAX); config.set_hash_strings_in_metric_report(false); @@ -69,8 +70,8 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents) { TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), ATOM_TAG); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -144,7 +145,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents) { EXPECT_GT((int)gaugeMetrics.data_size(), 1); auto data = gaugeMetrics.data(0); - EXPECT_EQ(android::util::SUBSYSTEM_SLEEP_STATE, data.dimensions_in_what().field()); + EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field()); EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size()); EXPECT_EQ(1 /* subsystem name field */, data.dimensions_in_what().value_tuple().dimensions_value(0).field()); @@ -214,8 +215,8 @@ TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents) { TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), ATOM_TAG); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -267,7 +268,7 @@ TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents) { EXPECT_GT((int)gaugeMetrics.data_size(), 1); auto data = gaugeMetrics.data(0); - EXPECT_EQ(android::util::SUBSYSTEM_SLEEP_STATE, data.dimensions_in_what().field()); + EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field()); EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size()); EXPECT_EQ(1 /* subsystem name field */, data.dimensions_in_what().value_tuple().dimensions_value(0).field()); @@ -315,8 +316,8 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm) { TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback, ATOM_TAG); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -371,7 +372,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm) { EXPECT_GT((int)gaugeMetrics.data_size(), 1); auto data = gaugeMetrics.data(0); - EXPECT_EQ(android::util::SUBSYSTEM_SLEEP_STATE, data.dimensions_in_what().field()); + EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field()); EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size()); EXPECT_EQ(1 /* subsystem name field */, data.dimensions_in_what().value_tuple().dimensions_value(0).field()); @@ -424,8 +425,8 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation) { event_activation->set_ttl_seconds(ttlNs / 1000000000); ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), ATOM_TAG); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -493,7 +494,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation) { EXPECT_GT((int)gaugeMetrics.data_size(), 0); auto data = gaugeMetrics.data(0); - EXPECT_EQ(android::util::SUBSYSTEM_SLEEP_STATE, data.dimensions_in_what().field()); + EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field()); EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size()); EXPECT_EQ(1 /* subsystem name field */, data.dimensions_in_what().value_tuple().dimensions_value(0).field()); @@ -542,8 +543,8 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition) { TimeUnitToBucketSizeInMillis(config.gauge_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), ATOM_TAG); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -586,7 +587,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition) { EXPECT_GT((int)gaugeMetrics.data_size(), 0); auto data = gaugeMetrics.data(0); - EXPECT_EQ(android::util::SUBSYSTEM_SLEEP_STATE, data.dimensions_in_what().field()); + EXPECT_EQ(ATOM_TAG, data.dimensions_in_what().field()); EXPECT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size()); EXPECT_EQ(1 /* subsystem name field */, data.dimensions_in_what().value_tuple().dimensions_value(0).field()); diff --git a/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp b/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp index 16b51d99535b7..9d58867f09db4 100644 --- a/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp @@ -12,22 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include +#include #include -#include +#include + #include "src/StatsLogProcessor.h" #include "src/StatsService.h" #include "src/stats_log_util.h" #include "tests/statsd_test_util.h" -#include - namespace android { namespace os { namespace statsd { #ifdef __ANDROID__ - +namespace { const string kApp1 = "app1.sharing.1"; const int kConfigKey = 789130123; // Randomly chosen to avoid collisions with existing configs. const int kCallingUid = 0; // Randomly chosen @@ -109,6 +111,7 @@ StatsdConfig MakeGaugeMetricConfig(int64_t minTime) { gaugeMetric->set_min_bucket_size_nanos(minTime); return config; } +} // anonymous namespace TEST(PartialBucketE2eTest, TestCountMetricWithoutSplit) { StatsService service(nullptr, nullptr); @@ -202,6 +205,9 @@ TEST(PartialBucketE2eTest, TestCountMetricSplitOnRemoval) { TEST(PartialBucketE2eTest, TestValueMetricWithoutMinPartialBucket) { StatsService service(nullptr, nullptr); + service.mPullerManager->RegisterPullAtomCallback( + /*uid=*/0, android::util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, + new FakeSubsystemSleepCallback()); // Partial buckets don't occur when app is first installed. service.mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); SendConfig(service, MakeValueMetricConfig(0)); @@ -220,6 +226,9 @@ TEST(PartialBucketE2eTest, TestValueMetricWithoutMinPartialBucket) { TEST(PartialBucketE2eTest, TestValueMetricWithMinPartialBucket) { StatsService service(nullptr, nullptr); + service.mPullerManager->RegisterPullAtomCallback( + /*uid=*/0, android::util::SUBSYSTEM_SLEEP_STATE, NS_PER_SEC, NS_PER_SEC * 10, {}, + new FakeSubsystemSleepCallback()); // Partial buckets don't occur when app is first installed. service.mUidMap->updateApp(1, String16(kApp1.c_str()), 1, 1, String16("v1"), String16("")); SendConfig(service, MakeValueMetricConfig(60 * NS_PER_SEC /* One minute */)); diff --git a/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp b/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp index e8d2ec514cad5..a140af8764748 100644 --- a/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp @@ -69,8 +69,9 @@ TEST(ValueMetricE2eTest, TestPulledEvents) { TimeUnitToBucketSizeInMillis(config.value_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), + android::util::SUBSYSTEM_SLEEP_STATE); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -173,8 +174,9 @@ TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm) { TimeUnitToBucketSizeInMillis(config.value_metric(0).bucket()) * 1000000; ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), + android::util::SUBSYSTEM_SLEEP_STATE); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); @@ -285,8 +287,9 @@ TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation) { event_activation->set_ttl_seconds(ttlNs / 1000000000); ConfigKey cfgKey; - auto processor = CreateStatsLogProcessor( - baseTimeNs, configAddedTimeNs, config, cfgKey); + auto processor = CreateStatsLogProcessor(baseTimeNs, configAddedTimeNs, config, cfgKey, + new FakeSubsystemSleepCallback(), + android::util::SUBSYSTEM_SLEEP_STATE); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); processor->mPullerManager->ForceClearPullerCache(); diff --git a/cmds/statsd/tests/statsd_test_util.cpp b/cmds/statsd/tests/statsd_test_util.cpp index e0aecceac4e3a..6e1890ad63a90 100644 --- a/cmds/statsd/tests/statsd_test_util.cpp +++ b/cmds/statsd/tests/statsd_test_util.cpp @@ -580,9 +580,15 @@ std::unique_ptr CreateUidProcessStateChangedEvent( } sp CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, - const StatsdConfig& config, const ConfigKey& key) { + const StatsdConfig& config, const ConfigKey& key, + const sp& puller, + const int32_t atomTag) { sp uidMap = new UidMap(); sp pullerManager = new StatsPullerManager(); + if (puller != nullptr) { + pullerManager->RegisterPullAtomCallback(/*uid=*/0, atomTag, NS_PER_SEC, NS_PER_SEC * 10, {}, + puller); + } sp anomalyAlarmMonitor = new AlarmMonitor(1, [](const sp&, int64_t){}, [](const sp&){}); @@ -942,6 +948,34 @@ void backfillStartEndTimestamp(ConfigMetricsReportList *config_report_list) { } } +binder::Status FakeSubsystemSleepCallback::onPullAtom( + int atomTag, const sp& resultReceiver) { + // Convert stats_events into StatsEventParcels. + std::vector parcels; + for (int i = 1; i < 3; i++) { + stats_event* event = stats_event_obtain(); + stats_event_set_atom_id(event, atomTag); + std::string subsystemName = "subsystem_name_"; + subsystemName = subsystemName + std::to_string(i); + stats_event_write_string8(event, subsystemName.c_str()); + stats_event_write_string8(event, "subsystem_subname foo"); + stats_event_write_int64(event, /*count= */ i); + stats_event_write_int64(event, /*time_millis= */ i * 100); + stats_event_build(event); + size_t size; + uint8_t* buffer = stats_event_get_buffer(event, &size); + + android::util::StatsEventParcel p; + // vector.assign() creates a copy, but this is inevitable unless + // stats_event.h/c uses a vector as opposed to a buffer. + p.buffer.assign(buffer, buffer + size); + parcels.push_back(std::move(p)); + stats_event_release(event); + } + resultReceiver->pullFinished(atomTag, /*success=*/true, parcels); + return binder::Status::ok(); +} + } // namespace statsd } // namespace os } // namespace android diff --git a/cmds/statsd/tests/statsd_test_util.h b/cmds/statsd/tests/statsd_test_util.h index 9bdfeebe561f9..576a4916d60b2 100644 --- a/cmds/statsd/tests/statsd_test_util.h +++ b/cmds/statsd/tests/statsd_test_util.h @@ -14,12 +14,16 @@ #pragma once +#include +#include +#include #include + #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "src/StatsLogProcessor.h" -#include "src/logd/LogEvent.h" #include "src/hash.h" +#include "src/logd/LogEvent.h" #include "src/stats_log_util.h" #include "statslog.h" @@ -224,9 +228,10 @@ std::unique_ptr CreateUidProcessStateChangedEvent( AttributionNodeInternal CreateAttribution(const int& uid, const string& tag); // Create a statsd log event processor upon the start time in seconds, config and key. -sp CreateStatsLogProcessor(const int64_t timeBaseNs, - const int64_t currentTimeNs, - const StatsdConfig& config, const ConfigKey& key); +sp CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, + const StatsdConfig& config, const ConfigKey& key, + const sp& puller = nullptr, + const int32_t atomTag = 0 /*for puller only*/); // Util function to sort the log events by timestamp. void sortLogEventsByTimestamp(std::vector> *events); @@ -278,6 +283,12 @@ bool backfillDimensionPath(const DimensionsValue& path, const google::protobuf::RepeatedPtrField& leafValues, DimensionsValue* dimension); +class FakeSubsystemSleepCallback : public BnPullAtomCallback { +public: + binder::Status onPullAtom(int atomTag, + const sp& resultReceiver) override; +}; + template void backfillDimensionPath(const DimensionsValue& whatPath, const DimensionsValue& conditionPath,