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/anomaly/AnomalyTracker.cpp b/bin/src/anomaly/AnomalyTracker.cpp
index e8b4083..4777dcd 100644
--- a/bin/src/anomaly/AnomalyTracker.cpp
+++ b/bin/src/anomaly/AnomalyTracker.cpp
@@ -317,7 +317,7 @@
     for (const auto& kv : matchedAlarms) {
         declareAnomaly(timestampNs /* TODO: , kv.first */);
         mAlarms.erase(kv.first);
-        firedAlarms.erase(kv.second); // No one else can also own it, so we're done with it.
+        firedAlarms.erase(kv.second);  // No one else can also own it, so we're done with it.
     }
 }
 
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
diff --git a/bin/src/metrics/CountMetricProducer.cpp b/bin/src/metrics/CountMetricProducer.cpp
index ae297d9..7b865c2 100644
--- a/bin/src/metrics/CountMetricProducer.cpp
+++ b/bin/src/metrics/CountMetricProducer.cpp
@@ -66,7 +66,7 @@
                                          const int conditionIndex,
                                          const sp<ConditionWizard>& wizard,
                                          const uint64_t startTimeNs)
-    : MetricProducer(key, startTimeNs, conditionIndex, wizard), mMetric(metric) {
+    : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard) {
     // TODO: evaluate initial conditions. and set mConditionMet.
     if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
         mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
@@ -92,18 +92,18 @@
 }
 
 void CountMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
-    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
+    VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
 }
 
 void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
                                              ProtoOutputStream* protoOutput) {
     flushIfNeededLocked(dumpTimeNs);
 
-    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
     long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
 
-    VLOG("metric %s dump report now...", mMetric.name().c_str());
+    VLOG("metric %s dump report now...", mName.c_str());
 
     for (const auto& counter : mPastBuckets) {
         const HashableDimensionKey& hashableKey = counter.first;
@@ -160,7 +160,7 @@
 
 void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
                                                    const uint64_t eventTime) {
-    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
+    VLOG("Metric %s onConditionChanged", mName.c_str());
     mCondition = conditionMet;
 }
 
@@ -172,11 +172,10 @@
     // 1. Report the tuple count if the tuple count > soft limit
     if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
         size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
-        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
-                                                           newTupleCount);
+        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
         // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
         if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
-            ALOGE("CountMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
+            ALOGE("CountMetric %s dropping data for dimension key %s", mName.c_str(),
                   newKey.c_str());
             return true;
         }
@@ -218,7 +217,7 @@
                                          mCurrentSlicedCounter->find(eventKey)->second);
     }
 
-    VLOG("metric %s %s->%lld", mMetric.name().c_str(), eventKey.c_str(),
+    VLOG("metric %s %s->%lld", mName.c_str(), eventKey.c_str(),
          (long long)(*mCurrentSlicedCounter)[eventKey]);
 }
 
@@ -237,7 +236,7 @@
         info.mCount = counter.second;
         auto& bucketList = mPastBuckets[counter.first];
         bucketList.push_back(info);
-        VLOG("metric %s, dump key value: %s -> %lld", mMetric.name().c_str(), counter.first.c_str(),
+        VLOG("metric %s, dump key value: %s -> %lld", mName.c_str(), counter.first.c_str(),
              (long long)counter.second);
     }
 
@@ -250,7 +249,7 @@
     uint64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
     mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
     mCurrentBucketNum += numBucketsForward;
-    VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
+    VLOG("metric %s: new bucket start time: %lld", mName.c_str(),
          (long long)mCurrentBucketStartTimeNs);
 }
 
diff --git a/bin/src/metrics/CountMetricProducer.h b/bin/src/metrics/CountMetricProducer.h
index 8a17169..59995d2 100644
--- a/bin/src/metrics/CountMetricProducer.h
+++ b/bin/src/metrics/CountMetricProducer.h
@@ -76,8 +76,6 @@
     // Util function to flush the old packet.
     void flushIfNeededLocked(const uint64_t& newEventTime);
 
-    const CountMetric mMetric;
-
     // TODO: Add a lock to mPastBuckets.
     std::unordered_map<HashableDimensionKey, std::vector<CountBucket>> mPastBuckets;
 
diff --git a/bin/src/metrics/DurationMetricProducer.cpp b/bin/src/metrics/DurationMetricProducer.cpp
index c268798..6afbe45 100644
--- a/bin/src/metrics/DurationMetricProducer.cpp
+++ b/bin/src/metrics/DurationMetricProducer.cpp
@@ -68,8 +68,8 @@
                                                const sp<ConditionWizard>& wizard,
                                                const vector<KeyMatcher>& internalDimension,
                                                const uint64_t startTimeNs)
-    : MetricProducer(key, startTimeNs, conditionIndex, wizard),
-      mMetric(metric),
+    : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard),
+      mAggregationType(metric.aggregation_type()),
       mStartIndex(startIndex),
       mStopIndex(stopIndex),
       mStopAllIndex(stopAllIndex),
@@ -114,20 +114,20 @@
 
 unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
         const HashableDimensionKey& eventKey) const {
-    switch (mMetric.aggregation_type()) {
+    switch (mAggregationType) {
         case DurationMetric_AggregationType_SUM:
             return make_unique<OringDurationTracker>(
-                    mConfigKey, mMetric.name(), eventKey, mWizard, mConditionTrackerIndex, mNested,
+                    mConfigKey, mName, eventKey, mWizard, mConditionTrackerIndex, mNested,
                     mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers);
         case DurationMetric_AggregationType_MAX_SPARSE:
             return make_unique<MaxDurationTracker>(
-                    mConfigKey, mMetric.name(), eventKey, mWizard, mConditionTrackerIndex, mNested,
+                    mConfigKey, mName, eventKey, mWizard, mConditionTrackerIndex, mNested,
                     mCurrentBucketStartTimeNs, mBucketSizeNs, mAnomalyTrackers);
     }
 }
 
 void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
-    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
+    VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
     flushIfNeededLocked(eventTime);
     // Now for each of the on-going event, check if the condition has changed for them.
     for (auto& pair : mCurrentSlicedDuration) {
@@ -137,7 +137,7 @@
 
 void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet,
                                                       const uint64_t eventTime) {
-    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
+    VLOG("Metric %s onConditionChanged", mName.c_str());
     mCondition = conditionMet;
     flushIfNeededLocked(eventTime);
     // TODO: need to populate the condition change time from the event which triggers the condition
@@ -151,11 +151,11 @@
                                                 ProtoOutputStream* protoOutput) {
     flushIfNeededLocked(dumpTimeNs);
 
-    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
     long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS);
 
-    VLOG("metric %s dump report now...", mMetric.name().c_str());
+    VLOG("metric %s dump report now...", mName.c_str());
 
     for (const auto& pair : mPastBuckets) {
         const HashableDimensionKey& hashableKey = pair.first;
@@ -236,11 +236,10 @@
     // 1. Report the tuple count if the tuple count > soft limit
     if (mCurrentSlicedDuration.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
         size_t newTupleCount = mCurrentSlicedDuration.size() + 1;
-        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
-                                                           newTupleCount);
+        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
         // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
         if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
-            ALOGE("DurationMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
+            ALOGE("DurationMetric %s dropping data for dimension key %s", mName.c_str(),
                   newKey.c_str());
             return true;
         }
diff --git a/bin/src/metrics/DurationMetricProducer.h b/bin/src/metrics/DurationMetricProducer.h
index 14504c1..e7aca7f 100644
--- a/bin/src/metrics/DurationMetricProducer.h
+++ b/bin/src/metrics/DurationMetricProducer.h
@@ -75,7 +75,7 @@
     // Util function to flush the old packet.
     void flushIfNeededLocked(const uint64_t& eventTime);
 
-    const DurationMetric mMetric;
+    const DurationMetric_AggregationType mAggregationType;
 
     // Index of the SimpleAtomMatcher which defines the start.
     const size_t mStartIndex;
diff --git a/bin/src/metrics/EventMetricProducer.cpp b/bin/src/metrics/EventMetricProducer.cpp
index bcecf16..4752997 100644
--- a/bin/src/metrics/EventMetricProducer.cpp
+++ b/bin/src/metrics/EventMetricProducer.cpp
@@ -55,7 +55,7 @@
                                          const int conditionIndex,
                                          const sp<ConditionWizard>& wizard,
                                          const uint64_t startTimeNs)
-    : MetricProducer(key, startTimeNs, conditionIndex, wizard), mMetric(metric) {
+    : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard) {
     if (metric.links().size() > 0) {
         mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
                                metric.links().end());
@@ -98,12 +98,12 @@
 
 void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
                                              ProtoOutputStream* protoOutput) {
-    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
 
     size_t bufferSize = mProto->size();
-    VLOG("metric %s dump report now... proto size: %zu ", mMetric.name().c_str(), bufferSize);
+    VLOG("metric %s dump report now... proto size: %zu ", mName.c_str(), bufferSize);
     std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto);
 
     protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS,
@@ -115,7 +115,7 @@
 
 void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
                                                    const uint64_t eventTime) {
-    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
+    VLOG("Metric %s onConditionChanged", mName.c_str());
     mCondition = conditionMet;
 }
 
diff --git a/bin/src/metrics/EventMetricProducer.h b/bin/src/metrics/EventMetricProducer.h
index 49ba9d8..d720ead 100644
--- a/bin/src/metrics/EventMetricProducer.h
+++ b/bin/src/metrics/EventMetricProducer.h
@@ -67,8 +67,6 @@
     // Internal function to calculate the current used bytes.
     size_t byteSizeLocked() const override;
 
-    const EventMetric mMetric;
-
     // Maps to a EventMetricDataWrapper. Storing atom events in ProtoOutputStream
     // is more space efficient than storing LogEvent.
     std::unique_ptr<android::util::ProtoOutputStream> mProto;
diff --git a/bin/src/metrics/GaugeMetricProducer.cpp b/bin/src/metrics/GaugeMetricProducer.cpp
index fffb2bf..ae9b86f 100644
--- a/bin/src/metrics/GaugeMetricProducer.cpp
+++ b/bin/src/metrics/GaugeMetricProducer.cpp
@@ -67,8 +67,8 @@
                                          const int conditionIndex,
                                          const sp<ConditionWizard>& wizard, const int pullTagId,
                                          const int64_t startTimeNs)
-    : MetricProducer(key, startTimeNs, conditionIndex, wizard),
-      mMetric(metric),
+    : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard),
+      mGaugeField(metric.gauge_field()),
       mPullTagId(pullTagId) {
     if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
         mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
@@ -104,11 +104,11 @@
 
 void GaugeMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
                                              ProtoOutputStream* protoOutput) {
-    VLOG("gauge metric %s dump report now...", mMetric.name().c_str());
+    VLOG("gauge metric %s dump report now...", mName.c_str());
 
     flushIfNeededLocked(dumpTimeNs);
 
-    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
     long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_GAUGE_METRICS);
 
@@ -166,7 +166,7 @@
 
 void GaugeMetricProducer::onConditionChangedLocked(const bool conditionMet,
                                                    const uint64_t eventTime) {
-    VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
+    VLOG("Metric %s onConditionChanged", mName.c_str());
     flushIfNeededLocked(eventTime);
     mCondition = conditionMet;
 
@@ -193,12 +193,12 @@
 }
 
 void GaugeMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
-    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
+    VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
 }
 
 int64_t GaugeMetricProducer::getGauge(const LogEvent& event) {
     status_t err = NO_ERROR;
-    int64_t val = event.GetLong(mMetric.gauge_field(), &err);
+    int64_t val = event.GetLong(mGaugeField, &err);
     if (err == NO_ERROR) {
         return val;
     } else {
@@ -222,11 +222,10 @@
     // 1. Report the tuple count if the tuple count > soft limit
     if (mCurrentSlicedBucket->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
         size_t newTupleCount = mCurrentSlicedBucket->size() + 1;
-        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
-                                                           newTupleCount);
+        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
         // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
         if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
-            ALOGE("GaugeMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
+            ALOGE("GaugeMetric %s dropping data for dimension key %s", mName.c_str(),
                   newKey.c_str());
             return true;
         }
@@ -289,8 +288,8 @@
         info.mGauge = slice.second;
         auto& bucketList = mPastBuckets[slice.first];
         bucketList.push_back(info);
-        VLOG("gauge metric %s, dump key value: %s -> %lld", mMetric.name().c_str(),
-             slice.first.c_str(), (long long)slice.second);
+        VLOG("gauge metric %s, dump key value: %s -> %lld", mName.c_str(), slice.first.c_str(),
+             (long long)slice.second);
     }
 
     // Reset counters
@@ -304,7 +303,7 @@
     int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
     mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
     mCurrentBucketNum += numBucketsForward;
-    VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
+    VLOG("metric %s: new bucket start time: %lld", mName.c_str(),
          (long long)mCurrentBucketStartTimeNs);
 }
 
diff --git a/bin/src/metrics/GaugeMetricProducer.h b/bin/src/metrics/GaugeMetricProducer.h
index ee4f40c..6e6f2bb 100644
--- a/bin/src/metrics/GaugeMetricProducer.h
+++ b/bin/src/metrics/GaugeMetricProducer.h
@@ -87,7 +87,7 @@
     // The default bucket size for gauge metric is 1 second.
     static const uint64_t kDefaultGaugemBucketSizeNs = 1000 * 1000 * 1000;
 
-    const GaugeMetric mMetric;
+    const int32_t mGaugeField;
 
     StatsPullerManager mStatsPullerManager;
     // tagId for pulled data. -1 if this is not pulled
diff --git a/bin/src/metrics/MetricProducer.h b/bin/src/metrics/MetricProducer.h
index 269bd43..d4a2195 100644
--- a/bin/src/metrics/MetricProducer.h
+++ b/bin/src/metrics/MetricProducer.h
@@ -38,9 +38,10 @@
 // be a no-op.
 class MetricProducer : public virtual PackageInfoListener {
 public:
-    MetricProducer(const ConfigKey& key, const int64_t startTimeNs, const int conditionIndex,
-                   const sp<ConditionWizard>& wizard)
-        : mConfigKey(key),
+    MetricProducer(const std::string& name, const ConfigKey& key, const int64_t startTimeNs,
+                   const int conditionIndex, const sp<ConditionWizard>& wizard)
+        : mName(name),
+          mConfigKey(key),
           mStartTimeNs(startTimeNs),
           mCurrentBucketStartTimeNs(startTimeNs),
           mCurrentBucketNum(0),
@@ -108,6 +109,8 @@
                                     android::util::ProtoOutputStream* protoOutput) = 0;
     virtual size_t byteSizeLocked() const = 0;
 
+    const std::string mName;
+
     const ConfigKey mConfigKey;
 
     // The start time for the current in memory metrics data.
diff --git a/bin/src/metrics/MetricsManager.cpp b/bin/src/metrics/MetricsManager.cpp
index b0f0135..3d0e20c 100644
--- a/bin/src/metrics/MetricsManager.cpp
+++ b/bin/src/metrics/MetricsManager.cpp
@@ -50,11 +50,6 @@
                              mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
                              mTrackerToMetricMap, mTrackerToConditionMap);
 
-    // TODO: add alert size.
-    // no matter whether this config is valid, log it in the stats.
-    StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
-                                                  mAllConditionTrackers.size(),
-                                                  mAllAtomMatchers.size(), 0, mConfigValid);
     // Guardrail. Reject the config if it's too big.
     if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
         mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
@@ -62,6 +57,12 @@
         ALOGE("This config is too big! Reject!");
         mConfigValid = false;
     }
+
+    // TODO: add alert size.
+    // no matter whether this config is valid, log it in the stats.
+    StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
+                                                  mAllConditionTrackers.size(),
+                                                  mAllAtomMatchers.size(), 0, mConfigValid);
 }
 
 MetricsManager::~MetricsManager() {
diff --git a/bin/src/metrics/ValueMetricProducer.cpp b/bin/src/metrics/ValueMetricProducer.cpp
index aabe5af..9400a1c 100644
--- a/bin/src/metrics/ValueMetricProducer.cpp
+++ b/bin/src/metrics/ValueMetricProducer.cpp
@@ -73,13 +73,13 @@
                                          const sp<ConditionWizard>& wizard, const int pullTagId,
                                          const uint64_t startTimeNs,
                                          shared_ptr<StatsPullerManager> statsPullerManager)
-    : MetricProducer(key, startTimeNs, conditionIndex, wizard),
-      mMetric(metric),
+    : MetricProducer(metric.name(), key, startTimeNs, conditionIndex, wizard),
+      mValueField(metric.value_field()),
       mStatsPullerManager(statsPullerManager),
       mPullTagId(pullTagId) {
     // TODO: valuemetric for pushed events may need unlimited bucket length
     if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
-        mBucketSizeNs = mMetric.bucket().bucket_size_millis() * 1000 * 1000;
+        mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000 * 1000;
     } else {
         mBucketSizeNs = kDefaultBucketSizeMillis * 1000 * 1000;
     }
@@ -118,14 +118,14 @@
 }
 
 void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
-    VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
+    VLOG("Metric %s onSlicedConditionMayChange", mName.c_str());
 }
 
 void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
                                              ProtoOutputStream* protoOutput) {
-    VLOG("metric %s dump report now...", mMetric.name().c_str());
+    VLOG("metric %s dump report now...", mName.c_str());
     flushIfNeededLocked(dumpTimeNs);
-    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
+    protoOutput->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mName);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs);
     long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
 
@@ -175,7 +175,7 @@
     protoOutput->end(protoToken);
     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs);
 
-    VLOG("metric %s dump report now...", mMetric.name().c_str());
+    VLOG("metric %s dump report now...", mName.c_str());
     mPastBuckets.clear();
     mStartTimeNs = mCurrentBucketStartTimeNs;
     // TODO: Clear mDimensionKeyMap once the report is dumped.
@@ -194,8 +194,7 @@
 
     if (mPullTagId != -1) {
         if (mCondition == true) {
-            mStatsPullerManager->RegisterReceiver(mPullTagId, this,
-                                                  mMetric.bucket().bucket_size_millis());
+            mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000);
         } else if (mCondition == false) {
             mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
         }
@@ -216,7 +215,7 @@
 void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
     std::lock_guard<std::mutex> lock(mMutex);
 
-    if (mCondition == true || !mMetric.has_condition()) {
+    if (mCondition == true || mConditionTrackerIndex < 0) {
         if (allData.size() == 0) {
             return;
         }
@@ -247,11 +246,10 @@
     }
     if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
         size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
-        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetric.name(),
-                                                           newTupleCount);
+        StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mName, newTupleCount);
         // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
         if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
-            ALOGE("ValueMetric %s dropping data for dimension key %s", mMetric.name().c_str(),
+            ALOGE("ValueMetric %s dropping data for dimension key %s", mName.c_str(),
                   newKey.c_str());
             return true;
         }
@@ -300,7 +298,7 @@
 
 long ValueMetricProducer::get_value(const LogEvent& event) {
     status_t err = NO_ERROR;
-    long val = event.GetLong(mMetric.value_field(), &err);
+    long val = event.GetLong(mValueField, &err);
     if (err == NO_ERROR) {
         return val;
     } else {
@@ -342,7 +340,7 @@
     if (numBucketsForward > 1) {
         VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
     }
-    VLOG("metric %s: new bucket start time: %lld", mMetric.name().c_str(),
+    VLOG("metric %s: new bucket start time: %lld", mName.c_str(),
          (long long)mCurrentBucketStartTimeNs);
 }
 
diff --git a/bin/src/metrics/ValueMetricProducer.h b/bin/src/metrics/ValueMetricProducer.h
index 4c49927..62e5d52 100644
--- a/bin/src/metrics/ValueMetricProducer.h
+++ b/bin/src/metrics/ValueMetricProducer.h
@@ -74,7 +74,7 @@
     // Util function to flush the old packet.
     void flushIfNeededLocked(const uint64_t& eventTime);
 
-    const ValueMetric mMetric;
+    const int32_t mValueField;
 
     std::shared_ptr<StatsPullerManager> mStatsPullerManager;
 
diff --git a/bin/src/storage/StorageManager.cpp b/bin/src/storage/StorageManager.cpp
index 3a4dfda..9919abf 100644
--- a/bin/src/storage/StorageManager.cpp
+++ b/bin/src/storage/StorageManager.cpp
@@ -23,13 +23,14 @@
 #include <android-base/file.h>
 #include <dirent.h>
 
-using android::util::FIELD_COUNT_REPEATED;
-using android::util::FIELD_TYPE_MESSAGE;
-
 namespace android {
 namespace os {
 namespace statsd {
 
+using android::util::FIELD_COUNT_REPEATED;
+using android::util::FIELD_TYPE_MESSAGE;
+using std::map;
+
 #define STATS_SERVICE_DIR "/data/misc/stats-service"
 
 // for ConfigMetricsReportList
@@ -170,7 +171,7 @@
     }
 }
 
-void StorageManager::readConfigFromDisk(unordered_map<ConfigKey, StatsdConfig>& configsMap) {
+void StorageManager::readConfigFromDisk(map<ConfigKey, StatsdConfig>& configsMap) {
     unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
     if (dir == NULL) {
         VLOG("no default config on disk");
diff --git a/bin/src/storage/StorageManager.h b/bin/src/storage/StorageManager.h
index 4c9abe5..caf5b8b 100644
--- a/bin/src/storage/StorageManager.h
+++ b/bin/src/storage/StorageManager.h
@@ -66,7 +66,7 @@
     /**
      * Call to load the saved configs from disk.
      */
-    static void readConfigFromDisk(unordered_map<ConfigKey, StatsdConfig>& configsMap);
+    static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap);
 };
 
 }  // namespace statsd