Add Chrome histogram hashes metric to Trace Processor

Bug: 225826610
Change-Id: I6795b3891905a2bfdccada6b83ec2b511b1bb27a
diff --git a/Android.bp b/Android.bp
index 6a66249..103b088 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3730,6 +3730,7 @@
         "protos/perfetto/metrics/chrome/blink_gc_metric.proto",
         "protos/perfetto/metrics/chrome/dropped_frames.proto",
         "protos/perfetto/metrics/chrome/frame_times.proto",
+        "protos/perfetto/metrics/chrome/histogram_hashes.proto",
         "protos/perfetto/metrics/chrome/long_latency.proto",
         "protos/perfetto/metrics/chrome/media_metric.proto",
         "protos/perfetto/metrics/chrome/reported_by_page.proto",
@@ -8367,6 +8368,7 @@
         "src/trace_processor/metrics/sql/chrome/actual_power_by_category.sql",
         "src/trace_processor/metrics/sql/chrome/actual_power_by_rail_mode.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_event_metadata.sql",
+        "src/trace_processor/metrics/sql/chrome/chrome_histogram_hashes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_processes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_thread_slice.sql",
         "src/trace_processor/metrics/sql/chrome/cpu_time_by_category.sql",
diff --git a/BUILD b/BUILD
index 3260a6e..cd517bb 100644
--- a/BUILD
+++ b/BUILD
@@ -1106,6 +1106,7 @@
         "src/trace_processor/metrics/sql/chrome/actual_power_by_category.sql",
         "src/trace_processor/metrics/sql/chrome/actual_power_by_rail_mode.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_event_metadata.sql",
+        "src/trace_processor/metrics/sql/chrome/chrome_histogram_hashes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_processes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_thread_slice.sql",
         "src/trace_processor/metrics/sql/chrome/cpu_time_by_category.sql",
@@ -2721,6 +2722,7 @@
         "protos/perfetto/metrics/chrome/blink_gc_metric.proto",
         "protos/perfetto/metrics/chrome/dropped_frames.proto",
         "protos/perfetto/metrics/chrome/frame_times.proto",
+        "protos/perfetto/metrics/chrome/histogram_hashes.proto",
         "protos/perfetto/metrics/chrome/long_latency.proto",
         "protos/perfetto/metrics/chrome/media_metric.proto",
         "protos/perfetto/metrics/chrome/reported_by_page.proto",
diff --git a/protos/perfetto/metrics/chrome/BUILD.gn b/protos/perfetto/metrics/chrome/BUILD.gn
index 9e88247..a170ace 100644
--- a/protos/perfetto/metrics/chrome/BUILD.gn
+++ b/protos/perfetto/metrics/chrome/BUILD.gn
@@ -25,6 +25,7 @@
     "blink_gc_metric.proto",
     "dropped_frames.proto",
     "frame_times.proto",
+    "histogram_hashes.proto",
     "long_latency.proto",
     "media_metric.proto",
     "reported_by_page.proto",
diff --git a/protos/perfetto/metrics/chrome/all_chrome_metrics.proto b/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
index 4a24580..03c2bb2 100644
--- a/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
+++ b/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
@@ -22,6 +22,7 @@
 import "protos/perfetto/metrics/chrome/blink_gc_metric.proto";
 import "protos/perfetto/metrics/chrome/dropped_frames.proto";
 import "protos/perfetto/metrics/chrome/frame_times.proto";
+import "protos/perfetto/metrics/chrome/histogram_hashes.proto";
 import "protos/perfetto/metrics/chrome/long_latency.proto";
 import "protos/perfetto/metrics/chrome/media_metric.proto";
 import "protos/perfetto/metrics/chrome/reported_by_page.proto";
@@ -40,4 +41,5 @@
   optional TouchJank touch_jank = 1007;
   optional ChromeDroppedFrames chrome_dropped_frames = 1008;
   optional ChromeLongLatency chrome_long_latency = 1009;
+  optional ChromeHistogramHashes chrome_histogram_hashes = 1010;
 }
diff --git a/protos/perfetto/metrics/chrome/histogram_hashes.proto b/protos/perfetto/metrics/chrome/histogram_hashes.proto
new file mode 100644
index 0000000..8f7323e
--- /dev/null
+++ b/protos/perfetto/metrics/chrome/histogram_hashes.proto
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+// The list of Chrome Histogram hashes in trace track events.
+// Use cases include translating histogram hashes to histogram
+// names by getting this list, and prepending a translation table to the trace.
+message ChromeHistogramHashes {
+  repeated int64 hash = 1;
+}
diff --git a/src/trace_processor/metrics/sql/BUILD.gn b/src/trace_processor/metrics/sql/BUILD.gn
index fbbb41c..27facfb 100644
--- a/src/trace_processor/metrics/sql/BUILD.gn
+++ b/src/trace_processor/metrics/sql/BUILD.gn
@@ -84,6 +84,7 @@
   "chrome/actual_power_by_category.sql",
   "chrome/actual_power_by_rail_mode.sql",
   "chrome/chrome_event_metadata.sql",
+  "chrome/chrome_histogram_hashes.sql",
   "chrome/chrome_processes.sql",
   "chrome/chrome_thread_slice.sql",
   "chrome/cpu_time_by_category.sql",
diff --git a/src/trace_processor/metrics/sql/chrome/chrome_histogram_hashes.sql b/src/trace_processor/metrics/sql/chrome/chrome_histogram_hashes.sql
new file mode 100644
index 0000000..ba48b5d
--- /dev/null
+++ b/src/trace_processor/metrics/sql/chrome/chrome_histogram_hashes.sql
@@ -0,0 +1,26 @@
+--
+-- Copyright 2022 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+DROP VIEW IF EXISTS chrome_histogram_hashes_output;
+
+CREATE VIEW chrome_histogram_hashes_output AS
+SELECT ChromeHistogramHashes(
+  'hash', (
+    SELECT RepeatedField(int_value)
+    FROM args
+    WHERE key = 'chrome_histogram_sample.name_hash'
+    ORDER BY arg_set_id
+  )
+);
diff --git a/test/trace_processor/chrome/chrome_histogram_hashes.out b/test/trace_processor/chrome/chrome_histogram_hashes.out
new file mode 100644
index 0000000..4cd61a5
--- /dev/null
+++ b/test/trace_processor/chrome/chrome_histogram_hashes.out
@@ -0,0 +1,4 @@
+[perfetto.protos.chrome_histogram_hashes]: {
+  hash: 10
+  hash: 20
+}
diff --git a/test/trace_processor/chrome/chrome_histogram_hashes.textproto b/test/trace_processor/chrome/chrome_histogram_hashes.textproto
new file mode 100644
index 0000000..dfc1260
--- /dev/null
+++ b/test/trace_processor/chrome/chrome_histogram_hashes.textproto
@@ -0,0 +1,27 @@
+packet {
+  trusted_packet_sequence_id: 1
+  timestamp: 0
+  incremental_state_cleared: true
+  track_event {
+    categories: "cat1"
+    type: 3
+    name_iid: 1
+    chrome_histogram_sample {
+      name_hash: 10
+      sample: 100
+    }
+  }
+}
+packet {
+  trusted_packet_sequence_id: 1
+  timestamp: 0
+  incremental_state_cleared: true
+  track_event {
+    categories: "cat2"
+    type: 3
+    name_iid: 2
+    chrome_histogram_sample {
+      name_hash: 20
+    }
+  }
+}
diff --git a/test/trace_processor/chrome/index b/test/trace_processor/chrome/index
index 7e6856c..1f727f4 100644
--- a/test/trace_processor/chrome/index
+++ b/test/trace_processor/chrome/index
@@ -54,3 +54,6 @@
 ../../data/chrome_android_systrace.pftrace chrome_processes.sql chrome_processes_android_systrace.out
 ../../data/chrome_scroll_without_vsync.pftrace chrome_threads.sql chrome_threads.out
 ../../data/chrome_android_systrace.pftrace chrome_threads.sql chrome_threads_android_systrace.out
+
+# Chrome histogram hashes
+chrome_histogram_hashes.textproto chrome_histogram_hashes chrome_histogram_hashes.out