trace_processor: introduce API for computing metrics on trace processor

This will be used by shell as well as internal targets to compute a
metric and write that metric into a merged proto.

Bug: 129747127
Change-Id: I82eb7b0c6fbbfb0f7626c021283f42ebfa8815f9
diff --git a/BUILD b/BUILD
index b13063c..b10d621 100644
--- a/BUILD
+++ b/BUILD
@@ -464,8 +464,6 @@
         "//third_party/perfetto/protos:common_cc_proto",
         "//third_party/perfetto/protos:common_zero_cc_proto",
         "//third_party/perfetto/protos:config_zero_cc_proto",
-        "//third_party/perfetto/protos:metrics_android_cc_proto",
-        "//third_party/perfetto/protos:metrics_cc_proto",
         "//third_party/perfetto/protos:trace_android_zero_cc_proto",
         "//third_party/perfetto/protos:trace_chrome_zero_cc_proto",
         "//third_party/perfetto/protos:trace_filesystem_zero_cc_proto",
diff --git a/include/perfetto/trace_processor/trace_processor.h b/include/perfetto/trace_processor/trace_processor.h
index 9cdf728..b511f46 100644
--- a/include/perfetto/trace_processor/trace_processor.h
+++ b/include/perfetto/trace_processor/trace_processor.h
@@ -19,6 +19,7 @@
 
 #include <functional>
 #include <memory>
+#include <vector>
 
 #include "perfetto/base/optional.h"
 #include "perfetto/base/string_view.h"
@@ -108,6 +109,14 @@
   // iterator can be used to load rows from the result.
   virtual Iterator ExecuteQuery(base::StringView sql) = 0;
 
+  // Computes the given metrics on the loded portion of the trace. If
+  // successful, the output argument |metrics_proto| will be filled with the
+  // proto-encoded bytes for the message TraceMetrics in
+  // perfetto/metrics/metrics.proto. The return value will be 0 if no error
+  // occured or non-zero otherwise.
+  virtual int ComputeMetric(const std::vector<std::string>& metric_names,
+                            std::vector<uint8_t>* metrics_proto) = 0;
+
   // Interrupts the current query. Typically used by Ctrl-C handler.
   virtual void InterruptQuery() = 0;
 };
diff --git a/protos/BUILD b/protos/BUILD
index 9af53a8..d8ff1d2 100644
--- a/protos/BUILD
+++ b/protos/BUILD
@@ -145,59 +145,6 @@
     ],
 )
 
-# GN target: //protos/perfetto/metrics:lite_gen
-proto_library(
-    name = "metrics",
-    srcs = [
-        "perfetto/metrics/metrics.proto",
-    ],
-    has_services = 1,
-    cc_api_version = 2,
-    cc_generic_services = 1,
-    visibility = [
-        "//visibility:public",
-    ],
-    deps = [
-        "//third_party/perfetto/protos:metrics_android",
-    ],
-)
-
-# GN target: //protos/perfetto/metrics/android:lite_gen
-proto_library(
-    name = "metrics_android",
-    srcs = [
-        "perfetto/metrics/android/mem_metric.proto",
-    ],
-    has_services = 1,
-    cc_api_version = 2,
-    cc_generic_services = 1,
-    visibility = [
-        "//visibility:public",
-    ],
-)
-
-# GN target: //protos/perfetto/metrics/android:lite_gen
-cc_proto_library(
-    name = "metrics_android_cc_proto",
-    visibility = [
-        "//visibility:public",
-    ],
-    deps = [
-        "//third_party/perfetto/protos:metrics_android",
-    ],
-)
-
-# GN target: //protos/perfetto/metrics:lite_gen
-cc_proto_library(
-    name = "metrics_cc_proto",
-    visibility = [
-        "//visibility:public",
-    ],
-    deps = [
-        "//third_party/perfetto/protos:metrics",
-    ],
-)
-
 # GN target: //protos/third_party/pprof:lite_gen
 proto_library(
     name = "protos_third_party_pprof",
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index 8ee7edb..b60f291 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -170,7 +170,6 @@
     deps = [
       ":lib",
       "../../gn:default_deps",
-      "../../protos/perfetto/metrics:lite",
       "../../protos/perfetto/trace_processor:lite",
       "../base",
     ]
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index 136a8ab..36cf6b6 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -387,6 +387,13 @@
   sqlite3_interrupt(db_.get());
 }
 
+int TraceProcessorImpl::ComputeMetric(
+    const std::vector<std::string>& metric_names,
+    std::vector<uint8_t>* metrics_proto) {
+  perfetto::base::ignore_result(metric_names, metrics_proto);
+  return 0;
+}
+
 TraceProcessor::IteratorImpl::IteratorImpl(TraceProcessorImpl* trace_processor,
                                            sqlite3* db,
                                            ScopedStmt stmt,
diff --git a/src/trace_processor/trace_processor_impl.h b/src/trace_processor/trace_processor_impl.h
index aa57364..4fcb919 100644
--- a/src/trace_processor/trace_processor_impl.h
+++ b/src/trace_processor/trace_processor_impl.h
@@ -64,6 +64,9 @@
 
   Iterator ExecuteQuery(base::StringView sql) override;
 
+  int ComputeMetric(const std::vector<std::string>& metric_names,
+                    std::vector<uint8_t>* metrics) override;
+
   void InterruptQuery() override;
 
  private:
diff --git a/src/trace_processor/trace_processor_shell.cc b/src/trace_processor/trace_processor_shell.cc
index 2e3d5c9..68ee4e6 100644
--- a/src/trace_processor/trace_processor_shell.cc
+++ b/src/trace_processor/trace_processor_shell.cc
@@ -31,7 +31,6 @@
 #include "perfetto/base/time.h"
 #include "perfetto/trace_processor/trace_processor.h"
 
-#include "perfetto/metrics/metrics.pb.h"
 #include "perfetto/trace_processor/raw_query.pb.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
@@ -254,11 +253,12 @@
 }
 
 int RunMetrics(const std::vector<std::string>& metric_names) {
-  protos::TraceMetrics metrics_proto;
-  for (const auto& metric : metric_names) {
-    perfetto::base::ignore_result(metric);
+  std::vector<uint8_t> metric_result;
+  int res = g_tp->ComputeMetric(metric_names, &metric_result);
+  if (res) {
+    PERFETTO_ELOG("Error when computing metrics");
+    return 1;
   }
-  perfetto::base::ignore_result(metrics_proto);
   return 0;
 }