Move metric reporting config to flags

This will enable us to enable periodic reporting according
to the properties set in the device config.

As part of this CL, enable cmdline only flags for thing that
do not make sense to read from system properties.

Test: gtest
Bug: 170149255

Change-Id: I99bae25d89cf3a17906b4d3c671e5c63e9a3c180
diff --git a/runtime/metrics/reporter.cc b/runtime/metrics/reporter.cc
index 4cf1ba5..26b55b8 100644
--- a/runtime/metrics/reporter.cc
+++ b/runtime/metrics/reporter.cc
@@ -16,6 +16,7 @@
 
 #include "reporter.h"
 
+#include "base/flags.h"
 #include "runtime.h"
 #include "runtime_options.h"
 #include "statsd.h"
@@ -121,10 +122,7 @@
           LOG_STREAM(DEBUG) << "Shutdown request received";
           running = false;
 
-          // Do one final metrics report, if enabled.
-          if (config_.report_metrics_on_shutdown) {
-            ReportMetrics();
-          }
+          ReportMetrics();
         },
         [&](RequestMetricsReportMessage message) {
           LOG_STREAM(DEBUG) << "Explicit report request received";
@@ -178,14 +176,12 @@
   }
 }
 
-ReportingConfig ReportingConfig::FromRuntimeArguments(const RuntimeArgumentMap& args) {
-  using M = RuntimeArgumentMap;
+ReportingConfig ReportingConfig::FromFlags() {
   return {
-      .dump_to_logcat = args.Exists(M::WriteMetricsToLog),
-      .dump_to_statsd = args.GetOrDefault(M::WriteMetricsToStatsd),
-      .dump_to_file = args.GetOptional(M::WriteMetricsToFile),
-      .report_metrics_on_shutdown = !args.Exists(M::DisableFinalMetricsReport),
-      .periodic_report_seconds = args.GetOptional(M::MetricsReportingPeriod),
+      .dump_to_logcat = gFlags.WriteMetricsToLogcat(),
+      .dump_to_file = gFlags.WriteMetricsToFile.GetValueOptional(),
+      .dump_to_statsd = gFlags.WriteMetricsToStatsd(),
+      .periodic_report_seconds = gFlags.MetricsReportingPeriod.GetValueOptional(),
   };
 }
 
diff --git a/runtime/metrics/reporter.h b/runtime/metrics/reporter.h
index 3ad55b0..3e8056b 100644
--- a/runtime/metrics/reporter.h
+++ b/runtime/metrics/reporter.h
@@ -28,7 +28,7 @@
 
 // Defines the set of options for how metrics reporting happens.
 struct ReportingConfig {
-  static ReportingConfig FromRuntimeArguments(const RuntimeArgumentMap& args);
+  static ReportingConfig FromFlags();
 
   // Causes metrics to be written to the log, which makes them show up in logcat.
   bool dump_to_logcat{false};
@@ -39,11 +39,6 @@
   // 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;
 };
diff --git a/runtime/parsed_options.cc b/runtime/parsed_options.cc
index cbee356..3acb975 100644
--- a/runtime/parsed_options.cc
+++ b/runtime/parsed_options.cc
@@ -415,25 +415,6 @@
           .IntoKey(M::CorePlatformApiPolicy)
       .Define("-Xuse-stderr-logger")
           .IntoKey(M::UseStderrLogger)
-      .Define("-Xwrite-metrics-to-log")
-          .WithHelp("Enables writing ART metrics to logcat")
-          .IntoKey(M::WriteMetricsToLog)
-      .Define("-Xwrite-metrics-to-statsd=_")
-          .WithType<bool>()
-          .WithValueMap({{"false", false}, {"true", true}})
-          .WithHelp("Enables writing ART metrics to statsd")
-          .IntoKey(M::WriteMetricsToStatsd)
-      .Define("-Xwrite-metrics-to-file=_")
-          .WithHelp("Enables writing ART metrics to the given file")
-          .WithType<std::string>()
-          .IntoKey(M::WriteMetricsToFile)
-      .Define("-Xdisable-final-metrics-report")
-          .WithHelp("Disables reporting metrics when ART shuts down")
-          .IntoKey(M::DisableFinalMetricsReport)
-      .Define("-Xmetrics-reporting-period=_")
-          .WithHelp("The time in seconds between metrics reports")
-          .WithType<unsigned int>()
-          .IntoKey(M::MetricsReportingPeriod)
       .Define("-Xonly-use-system-oat-files")
           .IntoKey(M::OnlyUseTrustedOatFiles)
       .Define("-Xverifier-logging-threshold=_")
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index d54ef6b..f038969 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1847,7 +1847,7 @@
   // Class-roots are setup, we can now finish initializing the JniIdManager.
   GetJniIdManager()->Init(self);
 
-  InitMetrics(runtime_options);
+  InitMetrics();
 
   // Runtime initialization is largely done now.
   // We load plugins first since that can modify the runtime state slightly.
@@ -1947,8 +1947,8 @@
   return true;
 }
 
-void Runtime::InitMetrics(const RuntimeArgumentMap& runtime_options) {
-  auto metrics_config = metrics::ReportingConfig::FromRuntimeArguments(runtime_options);
+void Runtime::InitMetrics() {
+  auto metrics_config = metrics::ReportingConfig::FromFlags();
   metrics_reporter_ = metrics::MetricsReporter::Create(metrics_config, this);
 }
 
diff --git a/runtime/runtime.h b/runtime/runtime.h
index f399849..1413b98 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -1032,7 +1032,7 @@
       SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
   void InitNativeMethods() REQUIRES(!Locks::mutator_lock_);
   void RegisterRuntimeNativeMethods(JNIEnv* env);
-  void InitMetrics(const RuntimeArgumentMap& runtime_options);
+  void InitMetrics();
 
   void StartDaemonThreads();
   void StartSignalCatcher();
diff --git a/runtime/runtime_options.def b/runtime/runtime_options.def
index 3717fbf..11387a2 100644
--- a/runtime/runtime_options.def
+++ b/runtime/runtime_options.def
@@ -192,11 +192,4 @@
 // This is to enable/disable Perfetto Java Heap Stack Profiling
 RUNTIME_OPTIONS_KEY (bool,                PerfettoJavaHeapStackProf,      false)
 
-// Metrics configuration settings
-RUNTIME_OPTIONS_KEY (Unit,                WriteMetricsToLog)
-RUNTIME_OPTIONS_KEY (bool,                WriteMetricsToStatsd,           true)
-RUNTIME_OPTIONS_KEY (std::string,         WriteMetricsToFile)
-RUNTIME_OPTIONS_KEY (Unit,                DisableFinalMetricsReport)
-RUNTIME_OPTIONS_KEY (unsigned int,        MetricsReportingPeriod)
-
 #undef RUNTIME_OPTIONS_KEY