Reset statsd and correctly record the dump reason when system
server restarts/crashes.

Test: statsd test
BUG: b/79161505
Change-Id: I0646c764964f6eafde91f9ae0179a1c837af320d
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp
index 986f2ef..d557913 100644
--- a/cmds/statsd/src/StatsLogProcessor.cpp
+++ b/cmds/statsd/src/StatsLogProcessor.cpp
@@ -162,6 +162,19 @@
     OnLogEvent(event, false);
 }
 
+void StatsLogProcessor::resetConfigs() {
+    std::lock_guard<std::mutex> lock(mMetricsMutex);
+    resetConfigsLocked(getElapsedRealtimeNs());
+}
+
+void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) {
+    std::vector<ConfigKey> configKeys;
+    for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
+        configKeys.push_back(it->first);
+    }
+    resetConfigsLocked(timestampNs, configKeys);
+}
+
 void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) {
     std::lock_guard<std::mutex> lock(mMetricsMutex);
     const int64_t currentTimestampNs = event->GetElapsedTimestampNs();
@@ -188,11 +201,7 @@
             WriteDataToDiskLocked(CONFIG_RESET);
             // We see fresher event before we see the checkpoint. We might have lost data.
             // The best we can do is to reset.
-            std::vector<ConfigKey> configKeys;
-            for (auto it = mMetricsManagers.begin(); it != mMetricsManagers.end(); it++) {
-                configKeys.push_back(it->first);
-            }
-            resetConfigsLocked(currentTimestampNs, configKeys);
+            resetConfigsLocked(currentTimestampNs);
         } else {
             // Still in search of the CP. Keep going.
             return;
@@ -242,6 +251,7 @@
 void StatsLogProcessor::OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
                                         const StatsdConfig& config) {
     std::lock_guard<std::mutex> lock(mMetricsMutex);
+    WriteDataToDiskLocked(key, timestampNs, CONFIG_UPDATED);
     OnConfigUpdatedLocked(timestampNs, key, config);
 }
 
@@ -251,10 +261,6 @@
     sp<MetricsManager> newMetricsManager =
         new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap,
                            mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
-    auto it = mMetricsManagers.find(key);
-    if (it != mMetricsManagers.end()) {
-        WriteDataToDiskLocked(it->first, CONFIG_UPDATED);
-    }
     if (newMetricsManager->isConfigValid()) {
         mUidMap->OnConfigUpdated(key);
         if (newMetricsManager->shouldAddUidMapListener()) {
@@ -419,6 +425,7 @@
         }
     }
     if (configKeysTtlExpired.size() > 0) {
+        WriteDataToDiskLocked(CONFIG_RESET);
         resetConfigsLocked(timestampNs, configKeysTtlExpired);
     }
 }
@@ -427,7 +434,7 @@
     std::lock_guard<std::mutex> lock(mMetricsMutex);
     auto it = mMetricsManagers.find(key);
     if (it != mMetricsManagers.end()) {
-        WriteDataToDiskLocked(key, CONFIG_REMOVED);
+        WriteDataToDiskLocked(key, getElapsedRealtimeNs(), CONFIG_REMOVED);
         mMetricsManagers.erase(it);
         mUidMap->OnConfigRemoved(key);
     }
@@ -474,9 +481,13 @@
 }
 
 void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
+                                              const int64_t timestampNs,
                                               const DumpReportReason dumpReportReason) {
+    if (mMetricsManagers.find(key) == mMetricsManagers.end()) {
+        return;
+    }
     ProtoOutputStream proto;
-    onConfigMetricsReportLocked(key, getElapsedRealtimeNs(),
+    onConfigMetricsReportLocked(key, timestampNs,
                                 true /* include_current_partial_bucket*/,
                                 false /* include strings */, dumpReportReason, &proto);
     string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
@@ -491,14 +502,15 @@
 }
 
 void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason) {
+    const int64_t timeNs = getElapsedRealtimeNs();
     for (auto& pair : mMetricsManagers) {
-        WriteDataToDiskLocked(pair.first, dumpReportReason);
+        WriteDataToDiskLocked(pair.first, timeNs, dumpReportReason);
     }
 }
 
-void StatsLogProcessor::WriteDataToDisk(bool isShutdown) {
+void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason) {
     std::lock_guard<std::mutex> lock(mMetricsMutex);
-    WriteDataToDiskLocked(DEVICE_SHUTDOWN);
+    WriteDataToDiskLocked(dumpReportReason);
 }
 
 void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {