Revert^4 "[metrics] Add background reporting thread"

This adds a background thread that reports metrics every N seconds,
where N is specified by the -Xmetrics-reporting-period command line
option. Periodic reporting is disabled by default.

This reverts commit 1060838894e34785139b5e3583fbc9edad7fa7f9.

Reason for revert: Remove problematic test

Test: test/run-test --host 2233-metrics-background-thread
Test: adb shell stop && \
      adb shell setprop dalvik.vm.extra-opts \
          -Xmetrics-reporting-period=30\\\ -Xwrite-metrics-to-log && \
      adb shell start && \
      adb logcat  # observe metrics in log
Bug: 170149255
Change-Id: I3d72043bbb1e652728253585aae5486598658d2b
diff --git a/runtime/metrics/metrics.h b/runtime/metrics/metrics.h
index a6e3955..36948dd 100644
--- a/runtime/metrics/metrics.h
+++ b/runtime/metrics/metrics.h
@@ -24,9 +24,11 @@
 #include <optional>
 #include <ostream>
 #include <string_view>
+#include <thread>
 #include <vector>
 
 #include "android-base/logging.h"
+#include "base/message_queue.h"
 #include "base/time_utils.h"
 
 #pragma clang diagnostic push
@@ -344,6 +346,7 @@
 // Returns a human readable name for the given DatumId.
 std::string DatumName(DatumId datum);
 
+// Defines the set of options for how metrics reporting happens.
 struct ReportingConfig {
   static ReportingConfig FromRuntimeArguments(const RuntimeArgumentMap& args);
 
@@ -353,23 +356,60 @@
   // If set, provides a file name to enable metrics logging to a file.
   std::optional<std::string> dump_to_file;
 
+  // Indicates whether to report the final state of metrics on shutdown.
+  //
+  // Note that reporting only happens if some output, such as logcat, is enabled.
+  bool report_metrics_on_shutdown{true};
+
+  // If set, metrics will be reported every time this many seconds elapses.
+  std::optional<unsigned int> periodic_report_seconds;
+
   // Returns whether any options are set that enables metrics reporting.
   constexpr bool ReportingEnabled() const { return dump_to_logcat || dump_to_file.has_value(); }
+
+  // Returns whether any options are set that requires a background reporting thread.
+  constexpr bool BackgroundReportingEnabled() const {
+    return ReportingEnabled() && periodic_report_seconds.has_value();
+  }
 };
 
 // MetricsReporter handles periodically reporting ART metrics.
 class MetricsReporter {
  public:
   // Creates a MetricsReporter instance that matches the options selected in ReportingConfig.
-  static std::unique_ptr<MetricsReporter> Create(ReportingConfig config, const ArtMetrics* metrics);
+  static std::unique_ptr<MetricsReporter> Create(ReportingConfig config, Runtime* runtime);
 
   ~MetricsReporter();
 
- private:
-  explicit MetricsReporter(ReportingConfig config, const ArtMetrics* metrics);
+  // Creates and runs the background reporting thread.
+  void MaybeStartBackgroundThread();
 
-  ReportingConfig config_;
-  const ArtMetrics* metrics_;
+  // Sends a request to the background thread to shutdown.
+  void MaybeStopBackgroundThread();
+
+  static constexpr const char* kBackgroundThreadName = "Metrics Background Reporting Thread";
+
+ private:
+  MetricsReporter(ReportingConfig config, Runtime* runtime);
+
+  // The background reporting thread main loop.
+  void BackgroundThreadRun();
+
+  // Calls messages_.SetTimeout if needed.
+  void MaybeResetTimeout();
+
+  // Outputs the current state of the metrics to the destination set by config_.
+  void ReportMetrics() const;
+
+  const ReportingConfig config_;
+  Runtime* runtime_;
+
+  std::optional<std::thread> thread_;
+
+  // A message indicating that the reporting thread should shut down.
+  struct ShutdownRequestedMessage {};
+
+  MessageQueue<ShutdownRequestedMessage> messages_;
 };