DeltaPerformer: Don't destroy previously allocated space am: 55c2bfe0f7 am: fe29715ec3 am: 7103c34e04

Change-Id: I27440d3ac66f334891395f0f7734f91f93f270da
diff --git a/Android.bp b/Android.bp
index e457cd1..693abf8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -266,6 +266,7 @@
         "liblog",
         "libmetricslogger",
         "libssl",
+        "libstatslog",
         "libutils",
     ],
 }
diff --git a/metrics_reporter_android.cc b/metrics_reporter_android.cc
index 9165f0d..454649c 100644
--- a/metrics_reporter_android.cc
+++ b/metrics_reporter_android.cc
@@ -16,10 +16,14 @@
 
 #include "update_engine/metrics_reporter_android.h"
 
+#include <stdint.h>
+
 #include <memory>
 #include <string>
 
+#include <android-base/properties.h>
 #include <metricslogger/metrics_logger.h>
+#include <statslog.h>
 
 #include "update_engine/common/constants.h"
 
@@ -28,6 +32,16 @@
   android::metricslogger::LogHistogram(metrics, value);
   LOG(INFO) << "uploading " << value << " to histogram for metric " << metrics;
 }
+
+// A number offset adds on top of the enum value. e.g. ErrorCode::SUCCESS will
+// be reported as 10000, and AttemptResult::UPDATE_CANCELED will be reported as
+// 10011. The keeps the ordering of update engine's enum definition when statsd
+// atoms reserve the value 0 for unknown state.
+constexpr auto kMetricsReporterEnumOffset = 10000;
+
+int32_t GetStatsdEnumValue(int32_t value) {
+  return kMetricsReporterEnumOffset + value;
+}
 }  // namespace
 
 namespace chromeos_update_engine {
@@ -100,6 +114,17 @@
                static_cast<int>(attempt_result));
   LogHistogram(metrics::kMetricsUpdateEngineAttemptErrorCode,
                static_cast<int>(error_code));
+
+  android::util::stats_write(
+      android::util::UPDATE_ENGINE_UPDATE_ATTEMPT_REPORTED,
+      attempt_number,
+      GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
+      duration.InMinutes(),
+      duration_uptime.InMinutes(),
+      payload_size_mib,
+      GetStatsdEnumValue(static_cast<int32_t>(attempt_result)),
+      GetStatsdEnumValue(static_cast<int32_t>(error_code)),
+      android::base::GetProperty("ro.build.fingerprint", "").c_str());
 }
 
 void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics(
@@ -148,6 +173,16 @@
       total_duration.InMinutes());
   LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateRebootCount,
                reboot_count);
+
+  android::util::stats_write(
+      android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED,
+      static_cast<int32_t>(attempt_count),
+      GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
+      static_cast<int32_t>(payload_size_mib),
+      static_cast<int32_t>(total_bytes_downloaded),
+      static_cast<int32_t>(download_overhead_percentage),
+      static_cast<int32_t>(total_duration.InMinutes()),
+      static_cast<int32_t>(reboot_count));
 }
 
 void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() {