Use server flag to control partial config update

Hooks up statsd's partial config update feature to use server-side
flags.

Test: atest statsd_test
Bug: 172842175
Change-Id: Iaf76172286d7259dc81b847fa0e6b7830482b014
This commit is contained in:
Tej Singh
2020-12-01 12:15:23 -08:00
parent b5e682ef3d
commit 750684cc76
10 changed files with 453 additions and 289 deletions

View File

@@ -40,6 +40,7 @@ cc_defaults {
"src/external/StatsPullerManager.cpp",
"src/external/TrainInfoPuller.cpp",
"src/FieldValue.cpp",
"src/flags/flags.cpp",
"src/guardrail/StatsdStats.cpp",
"src/hash.cpp",
"src/HashableDimensionKey.cpp",
@@ -92,6 +93,7 @@ cc_defaults {
"libstatslog_statsd",
"libsysutils",
"libutils",
"server_configurable_flags",
"statsd-aidl-ndk_platform",
],
shared_libs: [
@@ -265,6 +267,7 @@ cc_test {
"tests/e2e/Anomaly_duration_sum_e2e_test.cpp",
"tests/e2e/Attribution_e2e_test.cpp",
"tests/e2e/ConfigTtl_e2e_test.cpp",
"tests/e2e/ConfigUpdate_e2e_ab_test.cpp",
"tests/e2e/ConfigUpdate_e2e_test.cpp",
"tests/e2e/CountMetric_e2e_test.cpp",
"tests/e2e/DurationMetric_e2e_test.cpp",

View File

@@ -24,12 +24,13 @@
#include <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
#include <frameworks/base/cmds/statsd/src/experiment_ids.pb.h>
#include "StatsService.h"
#include "android-base/stringprintf.h"
#include "external/StatsPullerManager.h"
#include "flags/flags.h"
#include "guardrail/StatsdStats.h"
#include "logd/LogEvent.h"
#include "metrics/CountMetricProducer.h"
#include "StatsService.h"
#include "state/StateManager.h"
#include "stats_log_util.h"
#include "stats_util.h"
@@ -520,9 +521,10 @@ void StatsLogProcessor::GetActiveConfigsLocked(const int uid, vector<int64_t>& o
}
void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
const StatsdConfig& config, bool modularUpdate) {
const StatsdConfig& config) {
std::lock_guard<std::mutex> lock(mMetricsMutex);
WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED, NO_TIME_CONSTRAINTS);
bool modularUpdate = getFlagBool(PARTIAL_CONFIG_UPDATE_FLAG, "false");
OnConfigUpdatedLocked(timestampNs, key, config, modularUpdate);
}

View File

@@ -48,7 +48,7 @@ public:
void OnLogEvent(LogEvent* event);
void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
const StatsdConfig& config, bool modularUpdate = false);
const StatsdConfig& config);
void OnConfigRemoved(const ConfigKey& key);
size_t GetMetricsSize(const ConfigKey& key) const;
@@ -338,9 +338,9 @@ private:
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
FRIEND_TEST(ConfigUpdateE2eTest, TestHashStrings);
FRIEND_TEST(ConfigUpdateE2eTest, TestUidMapVersionStringInstaller);
FRIEND_TEST(ConfigUpdateE2eTest, TestConfigTtl);
FRIEND_TEST(ConfigUpdateE2eAbTest, TestHashStrings);
FRIEND_TEST(ConfigUpdateE2eAbTest, TestUidMapVersionStringInstaller);
FRIEND_TEST(ConfigUpdateE2eAbTest, TestConfigTtl);
FRIEND_TEST(CountMetricE2eTest, TestInitialConditionChanges);
FRIEND_TEST(CountMetricE2eTest, TestSlicedState);

View File

@@ -39,7 +39,7 @@ public:
* A configuration was added or updated.
*/
virtual void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
const StatsdConfig& config, bool modularUpdate = false) = 0;
const StatsdConfig& config) = 0;
/**
* A configuration was removed.

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "flags.h"
#include <server_configurable_flags/get_flags.h>
using server_configurable_flags::GetServerConfigurableFlag;
using std::string;
namespace android {
namespace os {
namespace statsd {
string getFlagString(const string& flagName, const string& defaultValue) {
return GetServerConfigurableFlag(STATSD_NATIVE_NAMESPACE, flagName, defaultValue);
}
bool getFlagBool(const string& flagName, const string& defaultValue) {
return getFlagString(flagName, defaultValue) == "true";
}
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <string>
namespace android {
namespace os {
namespace statsd {
const std::string STATSD_NATIVE_NAMESPACE = "statsd_native";
const std::string PARTIAL_CONFIG_UPDATE_FLAG = "partial_config_update";
std::string getFlagString(const std::string& flagName, const std::string& defaultValue);
// Returns true IFF flagName has a value of "true".
bool getFlagBool(const std::string& flagName, const std::string& defaultValue);
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -334,7 +334,7 @@ private:
FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
FRIEND_TEST(ConfigUpdateE2eTest, TestConfigTtl);
FRIEND_TEST(ConfigUpdateE2eAbTest, TestConfigTtl);
FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);

View File

@@ -44,8 +44,8 @@ static ostream& operator<<(ostream& os, const StatsdConfig& config) {
*/
class MockListener : public ConfigListener {
public:
MOCK_METHOD4(OnConfigUpdated, void(const int64_t timestampNs, const ConfigKey& key,
const StatsdConfig& config, bool modularUpdate));
MOCK_METHOD3(OnConfigUpdated,
void(const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config));
MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
};
@@ -90,25 +90,25 @@ TEST(ConfigManagerTest, TestAddUpdateRemove) {
// Add another one
EXPECT_CALL(*(listener.get()),
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(91), _))
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(91)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
// Update It
EXPECT_CALL(*(listener.get()),
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(92), _))
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(92)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
// Add one with the same uid but a different name
EXPECT_CALL(*(listener.get()),
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("yyy")), StatsdConfigEq(93), _))
OnConfigUpdated(_, ConfigKeyEq(1, StringToId("yyy")), StatsdConfigEq(93)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
// Add one with the same name but a different uid
EXPECT_CALL(*(listener.get()),
OnConfigUpdated(_, ConfigKeyEq(2, StringToId("zzz")), StatsdConfigEq(94), _))
OnConfigUpdated(_, ConfigKeyEq(2, StringToId("zzz")), StatsdConfigEq(94)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
@@ -143,7 +143,7 @@ TEST(ConfigManagerTest, TestRemoveUid) {
StatsdConfig config;
EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _, _, _)).Times(5);
EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _, _)).Times(5);
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("xxx"))));
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("yyy"))));
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))));

View File

@@ -0,0 +1,328 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <gtest/gtest.h>
#include "flags/flags.h"
#include "src/StatsLogProcessor.h"
#include "src/storage/StorageManager.h"
#include "tests/statsd_test_util.h"
namespace android {
namespace os {
namespace statsd {
#ifdef __ANDROID__
#define STATS_DATA_DIR "/data/misc/stats-data"
using android::base::SetProperty;
using android::base::StringPrintf;
using namespace std;
namespace {
StatsdConfig CreateSimpleConfig() {
StatsdConfig config;
config.add_allowed_log_source("AID_STATSD");
config.set_hash_strings_in_metric_report(false);
*config.add_atom_matcher() = CreateBatteryStateUsbMatcher();
// Simple count metric so the config isn't empty.
CountMetric* countMetric1 = config.add_count_metric();
countMetric1->set_id(StringToId("Count1"));
countMetric1->set_what(config.atom_matcher(0).id());
countMetric1->set_bucket(FIVE_MINUTES);
return config;
}
} // namespace
// Setup for parameterized tests.
class ConfigUpdateE2eAbTest : public TestWithParam<bool> {
private:
string originalFlagValue;
public:
void SetUp() override {
originalFlagValue = getFlagBool(PARTIAL_CONFIG_UPDATE_FLAG, "");
string rawFlagName =
StringPrintf("persist.device_config.%s.%s", STATSD_NATIVE_NAMESPACE.c_str(),
PARTIAL_CONFIG_UPDATE_FLAG.c_str());
SetProperty(rawFlagName, GetParam() ? "true" : "false");
}
void TearDown() override {
string rawFlagName =
StringPrintf("persist.device_config.%s.%s", STATSD_NATIVE_NAMESPACE.c_str(),
PARTIAL_CONFIG_UPDATE_FLAG.c_str());
SetProperty(rawFlagName, originalFlagValue);
}
};
INSTANTIATE_TEST_SUITE_P(ConfigUpdateE2eAbTest, ConfigUpdateE2eAbTest, testing::Bool());
TEST_P(ConfigUpdateE2eAbTest, TestUidMapVersionStringInstaller) {
sp<UidMap> uidMap = new UidMap();
vector<int32_t> uids({1000});
vector<int64_t> versions({1});
vector<String16> apps({String16("app1")});
vector<String16> versionStrings({String16("v1")});
vector<String16> installers({String16("installer1")});
uidMap->updateMap(1, uids, versions, versionStrings, apps, installers);
StatsdConfig config = CreateSimpleConfig();
config.set_version_strings_in_metric_report(true);
config.set_installer_in_metric_report(false);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey, nullptr, 0, uidMap);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_TRUE(metricsManager->isConfigValid());
// Now update.
config.set_version_strings_in_metric_report(false);
config.set_installer_in_metric_report(true);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
EXPECT_EQ(metricsManager == processor->mMetricsManagers.begin()->second, GetParam());
EXPECT_TRUE(metricsManager->isConfigValid());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
UidMapping uidMapping = reports.reports(1).uid_map();
ASSERT_EQ(uidMapping.snapshots_size(), 1);
ASSERT_EQ(uidMapping.snapshots(0).package_info_size(), 1);
EXPECT_FALSE(uidMapping.snapshots(0).package_info(0).has_version_string());
EXPECT_EQ(uidMapping.snapshots(0).package_info(0).installer(), "installer1");
}
TEST_P(ConfigUpdateE2eAbTest, TestHashStrings) {
sp<UidMap> uidMap = new UidMap();
vector<int32_t> uids({1000});
vector<int64_t> versions({1});
vector<String16> apps({String16("app1")});
vector<String16> versionStrings({String16("v1")});
vector<String16> installers({String16("installer1")});
uidMap->updateMap(1, uids, versions, versionStrings, apps, installers);
StatsdConfig config = CreateSimpleConfig();
config.set_version_strings_in_metric_report(true);
config.set_hash_strings_in_metric_report(true);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey, nullptr, 0, uidMap);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_TRUE(metricsManager->isConfigValid());
// Now update.
config.set_hash_strings_in_metric_report(false);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
EXPECT_EQ(metricsManager == processor->mMetricsManagers.begin()->second, GetParam());
EXPECT_TRUE(metricsManager->isConfigValid());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
UidMapping uidMapping = reports.reports(1).uid_map();
ASSERT_EQ(uidMapping.snapshots_size(), 1);
ASSERT_EQ(uidMapping.snapshots(0).package_info_size(), 1);
EXPECT_TRUE(uidMapping.snapshots(0).package_info(0).has_version_string());
EXPECT_FALSE(uidMapping.snapshots(0).package_info(0).has_version_string_hash());
}
TEST_P(ConfigUpdateE2eAbTest, TestAnnotations) {
StatsdConfig config = CreateSimpleConfig();
StatsdConfig_Annotation* annotation = config.add_annotation();
annotation->set_field_int64(11);
annotation->set_field_int32(1);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Now update
config.clear_annotation();
annotation = config.add_annotation();
annotation->set_field_int64(22);
annotation->set_field_int32(2);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
ConfigMetricsReport report = reports.reports(1);
EXPECT_EQ(report.annotation_size(), 1);
EXPECT_EQ(report.annotation(0).field_int64(), 22);
EXPECT_EQ(report.annotation(0).field_int32(), 2);
}
TEST_P(ConfigUpdateE2eAbTest, TestPersistLocally) {
StatsdConfig config = CreateSimpleConfig();
config.set_persist_locally(false);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Number of reports should still be 1 since persist_locally is false.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Now update.
config.set_persist_locally(true);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
// Should get 2: 1 in memory + 1 on disk. Both should be saved on disk.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 2);
// Should get 3, 2 on disk + 1 in memory.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 3);
string suffix = StringPrintf("%d_%lld", cfgKey.GetUid(), (long long)cfgKey.GetId());
StorageManager::deleteSuffixedFiles(STATS_DATA_DIR, suffix.c_str());
string historySuffix =
StringPrintf("%d_%lld_history", cfgKey.GetUid(), (long long)cfgKey.GetId());
StorageManager::deleteSuffixedFiles(STATS_DATA_DIR, historySuffix.c_str());
}
TEST_P(ConfigUpdateE2eAbTest, TestNoReportMetrics) {
StatsdConfig config = CreateSimpleConfig();
// Second simple count metric.
CountMetric* countMetric = config.add_count_metric();
countMetric->set_id(StringToId("Count2"));
countMetric->set_what(config.atom_matcher(0).id());
countMetric->set_bucket(FIVE_MINUTES);
config.add_no_report_metric(config.count_metric(0).id());
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Now update.
config.clear_no_report_metric();
config.add_no_report_metric(config.count_metric(1).id());
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
// First report (before update) has the first count metric.
ASSERT_EQ(reports.reports(0).metrics_size(), 1);
EXPECT_EQ(reports.reports(0).metrics(0).metric_id(), config.count_metric(1).id());
// Second report (after update) has the first count metric.
ASSERT_EQ(reports.reports(1).metrics_size(), 1);
EXPECT_EQ(reports.reports(1).metrics(0).metric_id(), config.count_metric(0).id());
}
TEST_P(ConfigUpdateE2eAbTest, TestAtomsAllowedFromAnyUid) {
StatsdConfig config = CreateSimpleConfig();
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Uses AID_ROOT, which isn't in allowed log sources.
unique_ptr<LogEvent> event = CreateBatteryStateChangedEvent(
baseTimeNs + 2, BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
processor->OnLogEvent(event.get());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, true, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Check the metric and make sure it has 0 count.
ASSERT_EQ(reports.reports(0).metrics_size(), 1);
EXPECT_FALSE(reports.reports(0).metrics(0).has_count_metrics());
// Now update. Allow plugged state to be logged from any uid, so the atom will be counted.
config.add_whitelisted_atom_ids(util::PLUGGED_STATE_CHANGED);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config);
unique_ptr<LogEvent> event2 = CreateBatteryStateChangedEvent(
baseTimeNs + 2000, BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
processor->OnLogEvent(event.get());
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 3000, true, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 2);
// Check the metric and make sure it has 0 count.
ASSERT_EQ(reports.reports(1).metrics_size(), 1);
EXPECT_TRUE(reports.reports(1).metrics(0).has_count_metrics());
ASSERT_EQ(reports.reports(1).metrics(0).count_metrics().data_size(), 1);
ASSERT_EQ(reports.reports(1).metrics(0).count_metrics().data(0).bucket_info_size(), 1);
EXPECT_EQ(reports.reports(1).metrics(0).count_metrics().data(0).bucket_info(0).count(), 1);
}
TEST_P(ConfigUpdateE2eAbTest, TestConfigTtl) {
StatsdConfig config = CreateSimpleConfig();
config.set_ttl_in_seconds(1);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_EQ(metricsManager->getTtlEndNs(), baseTimeNs + NS_PER_SEC);
config.set_ttl_in_seconds(5);
processor->OnConfigUpdated(baseTimeNs + 2 * NS_PER_SEC, cfgKey, config);
metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_EQ(metricsManager->getTtlEndNs(), baseTimeNs + 7 * NS_PER_SEC);
// Clear the data stored on disk as a result of the update.
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 3 * NS_PER_SEC, false, true, ADB_DUMP, FAST,
&buffer);
}
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <gtest/gtest.h>
#include "android-base/stringprintf.h"
#include "flags/flags.h"
#include "src/StatsLogProcessor.h"
#include "src/storage/StorageManager.h"
#include "tests/statsd_test_util.h"
@@ -24,280 +26,36 @@ namespace os {
namespace statsd {
#ifdef __ANDROID__
#define STATS_DATA_DIR "/data/misc/stats-data"
using android::base::SetProperty;
using android::base::StringPrintf;
using namespace std;
// Tests that only run with the partial config update feature turned on.
namespace {
// Setup for test fixture.
class ConfigUpdateE2eTest : public ::testing::Test {
private:
string originalFlagValue;
public:
void SetUp() override {
originalFlagValue = getFlagBool(PARTIAL_CONFIG_UPDATE_FLAG, "");
string rawFlagName =
StringPrintf("persist.device_config.%s.%s", STATSD_NATIVE_NAMESPACE.c_str(),
PARTIAL_CONFIG_UPDATE_FLAG.c_str());
SetProperty(rawFlagName, "true");
}
StatsdConfig CreateSimpleConfig() {
StatsdConfig config;
config.add_allowed_log_source("AID_STATSD");
config.set_hash_strings_in_metric_report(false);
void TearDown() override {
string rawFlagName =
StringPrintf("persist.device_config.%s.%s", STATSD_NATIVE_NAMESPACE.c_str(),
PARTIAL_CONFIG_UPDATE_FLAG.c_str());
SetProperty(rawFlagName, originalFlagValue);
}
};
} // Anonymous namespace.
*config.add_atom_matcher() = CreateBatteryStateUsbMatcher();
// Simple count metric so the config isn't empty.
CountMetric* countMetric1 = config.add_count_metric();
countMetric1->set_id(StringToId("Count1"));
countMetric1->set_what(config.atom_matcher(0).id());
countMetric1->set_bucket(FIVE_MINUTES);
return config;
}
} // namespace
// Setup for parameterized tests.
class ConfigUpdateE2eTest : public TestWithParam<bool> {};
INSTANTIATE_TEST_SUITE_P(ConfigUpdateE2eTest, ConfigUpdateE2eTest, testing::Bool());
TEST_P(ConfigUpdateE2eTest, TestUidMapVersionStringInstaller) {
sp<UidMap> uidMap = new UidMap();
vector<int32_t> uids({1000});
vector<int64_t> versions({1});
vector<String16> apps({String16("app1")});
vector<String16> versionStrings({String16("v1")});
vector<String16> installers({String16("installer1")});
uidMap->updateMap(1, uids, versions, versionStrings, apps, installers);
StatsdConfig config = CreateSimpleConfig();
config.set_version_strings_in_metric_report(true);
config.set_installer_in_metric_report(false);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey, nullptr, 0, uidMap);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_TRUE(metricsManager->isConfigValid());
// Now update.
config.set_version_strings_in_metric_report(false);
config.set_installer_in_metric_report(true);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
EXPECT_EQ(metricsManager == processor->mMetricsManagers.begin()->second, GetParam());
EXPECT_TRUE(metricsManager->isConfigValid());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
UidMapping uidMapping = reports.reports(1).uid_map();
ASSERT_EQ(uidMapping.snapshots_size(), 1);
ASSERT_EQ(uidMapping.snapshots(0).package_info_size(), 1);
EXPECT_FALSE(uidMapping.snapshots(0).package_info(0).has_version_string());
EXPECT_EQ(uidMapping.snapshots(0).package_info(0).installer(), "installer1");
}
TEST_P(ConfigUpdateE2eTest, TestHashStrings) {
sp<UidMap> uidMap = new UidMap();
vector<int32_t> uids({1000});
vector<int64_t> versions({1});
vector<String16> apps({String16("app1")});
vector<String16> versionStrings({String16("v1")});
vector<String16> installers({String16("installer1")});
uidMap->updateMap(1, uids, versions, versionStrings, apps, installers);
StatsdConfig config = CreateSimpleConfig();
config.set_version_strings_in_metric_report(true);
config.set_hash_strings_in_metric_report(true);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey, nullptr, 0, uidMap);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_TRUE(metricsManager->isConfigValid());
// Now update.
config.set_hash_strings_in_metric_report(false);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
EXPECT_EQ(metricsManager == processor->mMetricsManagers.begin()->second, GetParam());
EXPECT_TRUE(metricsManager->isConfigValid());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
UidMapping uidMapping = reports.reports(1).uid_map();
ASSERT_EQ(uidMapping.snapshots_size(), 1);
ASSERT_EQ(uidMapping.snapshots(0).package_info_size(), 1);
EXPECT_TRUE(uidMapping.snapshots(0).package_info(0).has_version_string());
EXPECT_FALSE(uidMapping.snapshots(0).package_info(0).has_version_string_hash());
}
TEST_P(ConfigUpdateE2eTest, TestAnnotations) {
StatsdConfig config = CreateSimpleConfig();
StatsdConfig_Annotation* annotation = config.add_annotation();
annotation->set_field_int64(11);
annotation->set_field_int32(1);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Now update
config.clear_annotation();
annotation = config.add_annotation();
annotation->set_field_int64(22);
annotation->set_field_int32(2);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
ConfigMetricsReport report = reports.reports(1);
EXPECT_EQ(report.annotation_size(), 1);
EXPECT_EQ(report.annotation(0).field_int64(), 22);
EXPECT_EQ(report.annotation(0).field_int32(), 2);
}
TEST_P(ConfigUpdateE2eTest, TestPersistLocally) {
StatsdConfig config = CreateSimpleConfig();
config.set_persist_locally(false);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Number of reports should still be 1 since persist_locally is false.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Now update.
config.set_persist_locally(true);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
// Should get 2: 1 in memory + 1 on disk. Both should be saved on disk.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 2);
// Should get 3, 2 on disk + 1 in memory.
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 3);
string suffix = StringPrintf("%d_%lld", cfgKey.GetUid(), (long long)cfgKey.GetId());
StorageManager::deleteSuffixedFiles(STATS_DATA_DIR, suffix.c_str());
string historySuffix =
StringPrintf("%d_%lld_history", cfgKey.GetUid(), (long long)cfgKey.GetId());
StorageManager::deleteSuffixedFiles(STATS_DATA_DIR, historySuffix.c_str());
}
TEST_P(ConfigUpdateE2eTest, TestNoReportMetrics) {
StatsdConfig config = CreateSimpleConfig();
// Second simple count metric.
CountMetric* countMetric = config.add_count_metric();
countMetric->set_id(StringToId("Count2"));
countMetric->set_what(config.atom_matcher(0).id());
countMetric->set_bucket(FIVE_MINUTES);
config.add_no_report_metric(config.count_metric(0).id());
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Now update.
config.clear_no_report_metric();
config.add_no_report_metric(config.count_metric(1).id());
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, false, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
// First report is written to disk when the update happens.
ASSERT_EQ(reports.reports_size(), 2);
// First report (before update) has the first count metric.
ASSERT_EQ(reports.reports(0).metrics_size(), 1);
EXPECT_EQ(reports.reports(0).metrics(0).metric_id(), config.count_metric(1).id());
// Second report (after update) has the first count metric.
ASSERT_EQ(reports.reports(1).metrics_size(), 1);
EXPECT_EQ(reports.reports(1).metrics(0).metric_id(), config.count_metric(0).id());
}
TEST_P(ConfigUpdateE2eTest, TestAtomsAllowedFromAnyUid) {
StatsdConfig config = CreateSimpleConfig();
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
// Uses AID_ROOT, which isn't in allowed log sources.
unique_ptr<LogEvent> event = CreateBatteryStateChangedEvent(
baseTimeNs + 2, BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
processor->OnLogEvent(event.get());
ConfigMetricsReportList reports;
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 1001, true, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 1);
// Check the metric and make sure it has 0 count.
ASSERT_EQ(reports.reports(0).metrics_size(), 1);
EXPECT_FALSE(reports.reports(0).metrics(0).has_count_metrics());
// Now update. Allow plugged state to be logged from any uid, so the atom will be counted.
config.add_whitelisted_atom_ids(util::PLUGGED_STATE_CHANGED);
processor->OnConfigUpdated(baseTimeNs + 1000, cfgKey, config, /*modularUpdate=*/GetParam());
unique_ptr<LogEvent> event2 = CreateBatteryStateChangedEvent(
baseTimeNs + 2000, BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
processor->OnLogEvent(event.get());
reports.Clear();
buffer.clear();
processor->onDumpReport(cfgKey, baseTimeNs + 3000, true, true, ADB_DUMP, FAST, &buffer);
EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
ASSERT_EQ(reports.reports_size(), 2);
// Check the metric and make sure it has 0 count.
ASSERT_EQ(reports.reports(1).metrics_size(), 1);
EXPECT_TRUE(reports.reports(1).metrics(0).has_count_metrics());
ASSERT_EQ(reports.reports(1).metrics(0).count_metrics().data_size(), 1);
ASSERT_EQ(reports.reports(1).metrics(0).count_metrics().data(0).bucket_info_size(), 1);
EXPECT_EQ(reports.reports(1).metrics(0).count_metrics().data(0).bucket_info(0).count(), 1);
}
TEST_P(ConfigUpdateE2eTest, TestConfigTtl) {
StatsdConfig config = CreateSimpleConfig();
config.set_ttl_in_seconds(1);
int64_t baseTimeNs = getElapsedRealtimeNs();
ConfigKey cfgKey(0, 12345);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(baseTimeNs, baseTimeNs, config, cfgKey);
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_EQ(metricsManager->getTtlEndNs(), baseTimeNs + NS_PER_SEC);
config.set_ttl_in_seconds(5);
processor->OnConfigUpdated(baseTimeNs + 2 * NS_PER_SEC, cfgKey, config,
/*modularUpdate=*/GetParam());
metricsManager = processor->mMetricsManagers.begin()->second;
EXPECT_EQ(metricsManager->getTtlEndNs(), baseTimeNs + 7 * NS_PER_SEC);
// Clear the data stored on disk as a result of the update.
vector<uint8_t> buffer;
processor->onDumpReport(cfgKey, baseTimeNs + 3 * NS_PER_SEC, false, true, ADB_DUMP, FAST,
&buffer);
}
TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhat) {
TEST_F(ConfigUpdateE2eTest, TestNewDurationExistingWhat) {
StatsdConfig config;
config.add_allowed_log_source("AID_ROOT");
*config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher();
@@ -329,7 +87,7 @@ TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhat) {
durationMetric->set_bucket(FIVE_MINUTES);
uint64_t updateTimeNs = bucketStartTimeNs + 60 * NS_PER_SEC; // 1:00
processor->OnConfigUpdated(updateTimeNs, key, config, /*modular*/ true);
processor->OnConfigUpdated(updateTimeNs, key, config);
event = CreateReleaseWakelockEvent(bucketStartTimeNs + 80 * NS_PER_SEC, attributionUids1,
attributionTags1,
@@ -359,7 +117,7 @@ TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhat) {
EXPECT_EQ(bucketInfo.duration_nanos(), 20 * NS_PER_SEC);
}
TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedCondition) {
TEST_F(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedCondition) {
StatsdConfig config;
config.add_allowed_log_source("AID_ROOT");
*config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher();
@@ -419,7 +177,7 @@ TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedCondition) {
CreateDimensions(util::ACTIVITY_FOREGROUND_STATE_CHANGED, {1 /*uid*/});
uint64_t updateTimeNs = bucketStartTimeNs + 60 * NS_PER_SEC; // 1:00
processor->OnConfigUpdated(updateTimeNs, key, config, /*modular*/ true);
processor->OnConfigUpdated(updateTimeNs, key, config);
event = CreateMoveToBackgroundEvent(bucketStartTimeNs + 73 * NS_PER_SEC, app2Uid); // 1:13
processor->OnLogEvent(event.get());
@@ -458,7 +216,7 @@ TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedCondition) {
EXPECT_EQ(bucketInfo.duration_nanos(), 17 * NS_PER_SEC);
}
TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedState) {
TEST_F(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedState) {
StatsdConfig config;
config.add_allowed_log_source("AID_ROOT");
*config.add_atom_matcher() = CreateAcquireWakelockAtomMatcher();
@@ -533,7 +291,7 @@ TEST(ConfigUpdateE2eTest, TestNewDurationExistingWhatSlicedState) {
CreateDimensions(util::UID_PROCESS_STATE_CHANGED, {1 /*uid*/});
uint64_t updateTimeNs = bucketStartTimeNs + 60 * NS_PER_SEC; // 1:00
processor->OnConfigUpdated(updateTimeNs, key, config, /*modular*/ true);
processor->OnConfigUpdated(updateTimeNs, key, config);
event = CreateAcquireWakelockEvent(bucketStartTimeNs + 72 * NS_PER_SEC, attributionUids2,
attributionTags2,