Write data to file when StatsCompanionSerivice (system_server) crashes

Bug: 73352867
Change-Id: Iecbb1ae3e29264975771155a878b368cfc2f50f0
Test: statsd_test
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp
index 3127e94..02d4dc9 100644
--- a/cmds/statsd/src/StatsLogProcessor.cpp
+++ b/cmds/statsd/src/StatsLogProcessor.cpp
@@ -355,7 +355,6 @@
         const ConfigKey& key = pair.first;
         vector<uint8_t> data;
         onDumpReportLocked(key, getElapsedRealtimeNs(), &data);
-        // TODO: Add a guardrail to prevent accumulation of file on disk.
         string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
              (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
         StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index 280081e..c5dfc44 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -55,18 +55,22 @@
 class CompanionDeathRecipient : public IBinder::DeathRecipient {
 public:
     CompanionDeathRecipient(const sp<AlarmMonitor>& anomalyAlarmMonitor,
-                            const sp<AlarmMonitor>& periodicAlarmMonitor) :
-                                mAnomalyAlarmMonitor(anomalyAlarmMonitor),
-                                mPeriodicAlarmMonitor(periodicAlarmMonitor)  {}
+                            const sp<AlarmMonitor>& periodicAlarmMonitor,
+                            const sp<StatsLogProcessor>& processor)
+        : mAnomalyAlarmMonitor(anomalyAlarmMonitor),
+          mPeriodicAlarmMonitor(periodicAlarmMonitor),
+          mProcessor(processor) {}
     virtual void binderDied(const wp<IBinder>& who);
 
 private:
-   sp<AlarmMonitor> mAnomalyAlarmMonitor;
-   sp<AlarmMonitor> mPeriodicAlarmMonitor;
+    sp<AlarmMonitor> mAnomalyAlarmMonitor;
+    sp<AlarmMonitor> mPeriodicAlarmMonitor;
+    sp<StatsLogProcessor> mProcessor;
 };
 
 void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
     ALOGW("statscompanion service died");
+    mProcessor->WriteDataToDisk();
     mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
     mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
     SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
@@ -817,8 +821,9 @@
                 "statscompanion unavailable despite it contacting statsd!");
     }
     VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
-    IInterface::asBinder(statsCompanion)->linkToDeath(
-            new CompanionDeathRecipient(mAnomalyAlarmMonitor, mPeriodicAlarmMonitor));
+    IInterface::asBinder(statsCompanion)
+        ->linkToDeath(new CompanionDeathRecipient(
+            mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, mProcessor));
     mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
     mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
     SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h
index e0a1299..1daec36 100644
--- a/cmds/statsd/src/StatsService.h
+++ b/cmds/statsd/src/StatsService.h
@@ -215,10 +215,10 @@
      */
     status_t cmd_dump_memory_info(FILE* out);
 
-  /*
+    /*
      * Clear all puller cached data
      */
-  status_t cmd_clear_puller_cache(FILE* out);
+    status_t cmd_clear_puller_cache(FILE* out);
 
     /**
      * Update a configuration.