Further reduce statsd memory usage.
+ Remove the protobuf *Metric object from MetricProducers
-- This saves ~150 bytes per metric.
+ Remove the StatsdConfig from ConfigManager
-- This saves us xKB per config.
+ Also remove alerts from fake config to avoid crash (Bug: 70627390)
+ Other misc fixes too.
Test: statsd_test & manual
Change-Id: Ied4eb3fa31c50599817b3a5e1caf5077c487fad2
diff --git a/bin/src/config/ConfigManager.cpp b/bin/src/config/ConfigManager.cpp
index e52b273..164f88f 100644
--- a/bin/src/config/ConfigManager.cpp
+++ b/bin/src/config/ConfigManager.cpp
@@ -29,6 +29,12 @@
namespace os {
namespace statsd {
+using std::map;
+using std::pair;
+using std::set;
+using std::string;
+using std::vector;
+
#define STATS_SERVICE_DIR "/data/misc/stats-service"
using android::base::StringPrintf;
@@ -41,8 +47,14 @@
}
void ConfigManager::Startup() {
- StorageManager::readConfigFromDisk(mConfigs);
-
+ map<ConfigKey, StatsdConfig> configsFromDisk;
+ StorageManager::readConfigFromDisk(configsFromDisk);
+ // TODO(b/70667694): Make the configs from disk be used. And remove the fake config,
+ // and tests shouldn't call this Startup(), maybe call StartupForTest() so we don't read
+ // configs from disk for tests.
+ // for (const auto& pair : configsFromDisk) {
+ // UpdateConfig(pair.first, pair.second);
+ //}
// this should be called from StatsService when it receives a statsd_config
UpdateConfig(ConfigKey(1000, "fake"), build_fake_config());
}
@@ -52,9 +64,8 @@
}
void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
- // Add to map
- mConfigs[key] = config;
- // Why doesn't this work? mConfigs.insert({key, config});
+ // Add to set
+ mConfigs.insert(key);
// Save to disk
update_saved_configs(key, config);
@@ -74,7 +85,7 @@
}
void ConfigManager::RemoveConfig(const ConfigKey& key) {
- unordered_map<ConfigKey, StatsdConfig>::iterator it = mConfigs.find(key);
+ auto it = mConfigs.find(key);
if (it != mConfigs.end()) {
// Remove from map
mConfigs.erase(it);
@@ -100,9 +111,9 @@
for (auto it = mConfigs.begin(); it != mConfigs.end();) {
// Remove from map
- if (it->first.GetUid() == uid) {
- removed.push_back(it->first);
- mConfigReceivers.erase(it->first);
+ if (it->GetUid() == uid) {
+ removed.push_back(*it);
+ mConfigReceivers.erase(*it);
it = mConfigs.erase(it);
} else {
it++;
@@ -123,10 +134,10 @@
for (auto it = mConfigs.begin(); it != mConfigs.end();) {
// Remove from map
- removed.push_back(it->first);
- auto receiverIt = mConfigReceivers.find(it->first);
+ removed.push_back(*it);
+ auto receiverIt = mConfigReceivers.find(*it);
if (receiverIt != mConfigReceivers.end()) {
- mConfigReceivers.erase(it->first);
+ mConfigReceivers.erase(*it);
}
it = mConfigs.erase(it);
}
@@ -143,7 +154,7 @@
vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
vector<ConfigKey> ret;
for (auto it = mConfigs.cbegin(); it != mConfigs.cend(); ++it) {
- ret.push_back(it->first);
+ ret.push_back(*it);
}
return ret;
}
@@ -160,15 +171,13 @@
void ConfigManager::Dump(FILE* out) {
fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
fprintf(out, " uid name\n");
- for (unordered_map<ConfigKey, StatsdConfig>::const_iterator it = mConfigs.begin();
- it != mConfigs.end(); it++) {
- fprintf(out, " %6d %s\n", it->first.GetUid(), it->first.GetName().c_str());
- auto receiverIt = mConfigReceivers.find(it->first);
+ for (const auto& key : mConfigs) {
+ fprintf(out, " %6d %s\n", key.GetUid(), key.GetName().c_str());
+ auto receiverIt = mConfigReceivers.find(key);
if (receiverIt != mConfigReceivers.end()) {
fprintf(out, " -> received by %s, %s\n", receiverIt->second.first.c_str(),
receiverIt->second.second.c_str());
}
- // TODO: Print the contents of the config too.
}
}
@@ -227,7 +236,8 @@
metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
// Anomaly threshold for screen-on count.
- Alert* alert = config.add_alert();
+ // TODO(b/70627390): Uncomment once the bug is fixed.
+ /*Alert* alert = config.add_alert();
alert->set_name("ALERT_1");
alert->set_metric_name("METRIC_1");
alert->set_number_of_buckets(6);
@@ -235,7 +245,7 @@
alert->set_refractory_period_secs(30);
Alert::IncidentdDetails* details = alert->mutable_incidentd_details();
details->add_section(12);
- details->add_section(13);
+ details->add_section(13);*/
// Count process state changes, slice by uid.
metric = config.add_count_metric();
@@ -246,6 +256,8 @@
keyMatcher->set_key(UID_PROCESS_STATE_UID_KEY);
// Anomaly threshold for background count.
+ // TODO(b/70627390): Uncomment once the bug is fixed.
+ /*
alert = config.add_alert();
alert->set_name("ALERT_2");
alert->set_metric_name("METRIC_2");
@@ -254,7 +266,7 @@
alert->set_refractory_period_secs(20);
details = alert->mutable_incidentd_details();
details->add_section(14);
- details->add_section(15);
+ details->add_section(15);*/
// Count process state changes, slice by uid, while SCREEN_IS_OFF
metric = config.add_count_metric();
@@ -326,6 +338,8 @@
durationMetric->set_what("SCREEN_IS_ON");
// Anomaly threshold for background count.
+ // TODO(b/70627390): Uncomment once the bug is fixed.
+ /*
alert = config.add_alert();
alert->set_name("ALERT_8");
alert->set_metric_name("METRIC_8");
@@ -333,7 +347,7 @@
alert->set_trigger_if_sum_gt(2000000000); // 2 seconds
alert->set_refractory_period_secs(120);
details = alert->mutable_incidentd_details();
- details->add_section(-1);
+ details->add_section(-1);*/
// Value metric to count KERNEL_WAKELOCK when screen turned on
ValueMetric* valueMetric = config.add_value_metric();
diff --git a/bin/src/config/ConfigManager.h b/bin/src/config/ConfigManager.h
index 90ea6c0..ea42a35 100644
--- a/bin/src/config/ConfigManager.h
+++ b/bin/src/config/ConfigManager.h
@@ -19,8 +19,9 @@
#include "config/ConfigKey.h"
#include "config/ConfigListener.h"
+#include <map>
+#include <set>
#include <string>
-#include <unordered_map>
#include <stdio.h>
@@ -28,13 +29,7 @@
namespace os {
namespace statsd {
-using android::RefBase;
-using std::string;
-using std::unordered_map;
-using std::vector;
-using std::pair;
-
-// Util function to Hard code a test metric for counting screen on events.
+// Util function to build a hard coded config with test metrics.
StatsdConfig build_fake_config();
/**
@@ -43,7 +38,7 @@
* TODO: Store the configs persistently too.
* TODO: Dump method for debugging.
*/
-class ConfigManager : public virtual RefBase {
+class ConfigManager : public virtual android::RefBase {
public:
ConfigManager();
virtual ~ConfigManager();
@@ -68,17 +63,17 @@
/**
* Sets the broadcast receiver for a configuration key.
*/
- void SetConfigReceiver(const ConfigKey& key, const string& pkg, const string& cls);
+ void SetConfigReceiver(const ConfigKey& key, const std::string& pkg, const std::string& cls);
/**
* Returns the package name and class name representing the broadcast receiver for this config.
*/
- const pair<string, string> GetConfigReceiver(const ConfigKey& key) const;
+ const std::pair<std::string, std::string> GetConfigReceiver(const ConfigKey& key) const;
/**
* Returns all config keys registered.
*/
- vector<ConfigKey> GetAllConfigKeys() const;
+ std::vector<ConfigKey> GetAllConfigKeys() const;
/**
* Erase any broadcast receiver associated with this config key.
@@ -121,18 +116,18 @@
/**
* The Configs that have been set. Each config should
*/
- unordered_map<ConfigKey, StatsdConfig> mConfigs;
+ std::set<ConfigKey> mConfigs;
/**
* Each config key can be subscribed by up to one receiver, specified as the package name and
* class name.
*/
- unordered_map<ConfigKey, pair<string, string>> mConfigReceivers;
+ std::map<ConfigKey, std::pair<std::string, std::string>> mConfigReceivers;
/**
* The ConfigListeners that will be told about changes.
*/
- vector<sp<ConfigListener>> mListeners;
+ std::vector<sp<ConfigListener>> mListeners;
};
} // namespace statsd