Add AdServices metric to perfetto.

Test: tools/diff_test_trace_processor.py out/linux_clang_release/trace_processor_shell
Bug: b/304358902
Change-Id: Idbc5f5f1d3056fd214130aa76a10652ef6df87ec
diff --git a/Android.bp b/Android.bp
index d668c2b..78fb15d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -4587,6 +4587,7 @@
 genrule {
     name: "perfetto_protos_perfetto_metrics_chrome_descriptor",
     srcs: [
+        "protos/perfetto/metrics/android/ad_services_metric.proto",
         "protos/perfetto/metrics/android/android_blocking_call.proto",
         "protos/perfetto/metrics/android/android_blocking_calls_cuj_metric.proto",
         "protos/perfetto/metrics/android/android_boot.proto",
@@ -4668,6 +4669,7 @@
 genrule {
     name: "perfetto_protos_perfetto_metrics_descriptor",
     srcs: [
+        "protos/perfetto/metrics/android/ad_services_metric.proto",
         "protos/perfetto/metrics/android/android_blocking_call.proto",
         "protos/perfetto/metrics/android/android_blocking_calls_cuj_metric.proto",
         "protos/perfetto/metrics/android/android_boot.proto",
@@ -4732,6 +4734,7 @@
 genrule {
     name: "perfetto_protos_perfetto_metrics_webview_descriptor",
     srcs: [
+        "protos/perfetto/metrics/android/ad_services_metric.proto",
         "protos/perfetto/metrics/android/android_blocking_call.proto",
         "protos/perfetto/metrics/android/android_blocking_calls_cuj_metric.proto",
         "protos/perfetto/metrics/android/android_boot.proto",
@@ -10430,6 +10433,7 @@
 genrule {
     name: "perfetto_src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
     srcs: [
+        "src/trace_processor/metrics/sql/android/ad_services_metric.sql",
         "src/trace_processor/metrics/sql/android/android_anr.sql",
         "src/trace_processor/metrics/sql/android/android_batt.sql",
         "src/trace_processor/metrics/sql/android/android_binder.sql",
diff --git a/BUILD b/BUILD
index 42122bd..135ce8e 100644
--- a/BUILD
+++ b/BUILD
@@ -1787,6 +1787,7 @@
 perfetto_filegroup(
     name = "src_trace_processor_metrics_sql_android_android",
     srcs = [
+        "src/trace_processor/metrics/sql/android/ad_services_metric.sql",
         "src/trace_processor/metrics/sql/android/android_anr.sql",
         "src/trace_processor/metrics/sql/android/android_batt.sql",
         "src/trace_processor/metrics/sql/android/android_binder.sql",
@@ -4030,6 +4031,7 @@
 perfetto_proto_library(
     name = "protos_perfetto_metrics_android_protos",
     srcs = [
+        "protos/perfetto/metrics/android/ad_services_metric.proto",
         "protos/perfetto/metrics/android/android_blocking_call.proto",
         "protos/perfetto/metrics/android/android_blocking_calls_cuj_metric.proto",
         "protos/perfetto/metrics/android/android_boot.proto",
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index a2ae264..2504f4c 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -20,6 +20,7 @@
     "source_set",
   ]
   sources = [
+    "ad_services_metric.proto",
     "android_blocking_call.proto",
     "android_blocking_calls_cuj_metric.proto",
     "android_boot.proto",
diff --git a/protos/perfetto/metrics/android/ad_services_metric.proto b/protos/perfetto/metrics/android/ad_services_metric.proto
new file mode 100644
index 0000000..c2ddcfd
--- /dev/null
+++ b/protos/perfetto/metrics/android/ad_services_metric.proto
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2023 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;
+
+// Next: 2
+message AdServicesUiMetric {
+  optional double latency = 1;
+}
+
+// Next: 2
+message AdServicesAdIdMetric {
+  optional double latency = 1;
+}
+
+// Next: 2
+message AdServicesAppSetIdMetric {
+  optional double latency = 1;
+}
+
+// Next: 4
+message AdServicesMetric {
+  repeated AdServicesUiMetric ui_metric = 1;
+  repeated AdServicesAdIdMetric ad_id_metric = 2;
+  repeated AdServicesAppSetIdMetric app_set_id_metric = 3;
+}
\ No newline at end of file
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index 95fd3b4..8330a3f 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -18,6 +18,7 @@
 
 package perfetto.protos;
 
+import "protos/perfetto/metrics/android/ad_services_metric.proto";
 import "protos/perfetto/metrics/android/android_boot.proto";
 import "protos/perfetto/metrics/android/android_frame_timeline_metric.proto";
 import "protos/perfetto/metrics/android/anr_metric.proto";
@@ -109,7 +110,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 58
+// Next id: 59
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -261,11 +262,15 @@
 
   // Metrics for App Not Responding (ANR) errors.
   optional AndroidAnrMetric android_anr = 55;
+
   // Aggregated Android Monitor Contention metrics
   optional AndroidMonitorContentionAggMetric android_monitor_contention_agg = 56;
 
   optional AndroidBootMetric android_boot = 57;
 
+  // Metric for AdServices module.
+  optional AdServicesMetric ad_services_metric = 58;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/protos/perfetto/metrics/perfetto_merged_metrics.proto b/protos/perfetto/metrics/perfetto_merged_metrics.proto
index e940682..3f270f6 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -13,6 +13,31 @@
 
 option go_package = "github.com/google/perfetto/perfetto_proto";
 
+// Begin of protos/perfetto/metrics/android/ad_services_metric.proto
+
+// Next: 2
+message AdServicesUiMetric {
+  optional double latency = 1;
+}
+
+// Next: 2
+message AdServicesAdIdMetric {
+  optional double latency = 1;
+}
+
+// Next: 2
+message AdServicesAppSetIdMetric {
+  optional double latency = 1;
+}
+
+// Next: 4
+message AdServicesMetric {
+  repeated AdServicesUiMetric ui_metric = 1;
+  repeated AdServicesAdIdMetric ad_id_metric = 2;
+  repeated AdServicesAppSetIdMetric app_set_id_metric = 3;
+}
+// End of protos/perfetto/metrics/android/ad_services_metric.proto
+
 // Begin of protos/perfetto/metrics/android/android_blocking_call.proto
 
 // Blocking call on the main thread.
@@ -2283,7 +2308,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 58
+// Next id: 59
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -2435,11 +2460,15 @@
 
   // Metrics for App Not Responding (ANR) errors.
   optional AndroidAnrMetric android_anr = 55;
+
   // Aggregated Android Monitor Contention metrics
   optional AndroidMonitorContentionAggMetric android_monitor_contention_agg = 56;
 
   optional AndroidBootMetric android_boot = 57;
 
+  // Metric for AdServices module.
+  optional AdServicesMetric ad_services_metric = 58;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/python/perfetto/trace_processor/metrics.descriptor b/python/perfetto/trace_processor/metrics.descriptor
index 926dee9..7af1fde 100644
--- a/python/perfetto/trace_processor/metrics.descriptor
+++ b/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/metrics/sql/android/BUILD.gn b/src/trace_processor/metrics/sql/android/BUILD.gn
index 6c31943..a2e64da 100644
--- a/src/trace_processor/metrics/sql/android/BUILD.gn
+++ b/src/trace_processor/metrics/sql/android/BUILD.gn
@@ -20,6 +20,7 @@
 perfetto_sql_source_set("android") {
   sources = [
     "android_anr.sql",
+    "ad_services_metric.sql",
     "android_batt.sql",
     "android_binder.sql",
     "android_blocking_calls_cuj_metric.sql",
diff --git a/src/trace_processor/metrics/sql/android/ad_services_metric.sql b/src/trace_processor/metrics/sql/android/ad_services_metric.sql
new file mode 100644
index 0000000..36885fd
--- /dev/null
+++ b/src/trace_processor/metrics/sql/android/ad_services_metric.sql
@@ -0,0 +1,59 @@
+--
+-- Copyright 2023 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.
+--
+
+CREATE OR REPLACE PERFETTO FUNCTION GET_EVENT_LATENCY_TABLE(event_name STRING)
+RETURNS TABLE (latency LONG) AS
+SELECT
+  dur / 1e6 as latency
+FROM
+  slices
+WHERE
+  name = $event_name;
+
+DROP VIEW IF EXISTS ad_services_metric_output;
+CREATE VIEW ad_services_metric_output AS
+SELECT
+  AdServicesMetric(
+    'ui_metric',
+    (
+      SELECT
+        RepeatedField(
+          AdServicesUiMetric('latency', latency)
+        )
+      FROM
+        GET_EVENT_LATENCY_TABLE("NotificationTriggerEvent")
+    ),
+    'app_set_id_metric',
+    (
+      SELECT
+        RepeatedField(
+          AdServicesAppSetIdMetric(
+            'latency', latency
+          )
+        )
+      FROM
+        GET_EVENT_LATENCY_TABLE("AdIdCacheEvent")
+    ),
+    'ad_id_metric',
+    (
+      SELECT
+        RepeatedField(
+          AdServicesAdIdMetric('latency', latency)
+        )
+      FROM
+        GET_EVENT_LATENCY_TABLE("AppSetIdEvent")
+    )
+);
diff --git a/test/trace_processor/diff_tests/android/ad_services_metric.py b/test/trace_processor/diff_tests/android/ad_services_metric.py
new file mode 100644
index 0000000..efa827f
--- /dev/null
+++ b/test/trace_processor/diff_tests/android/ad_services_metric.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+# Copyright (C) 2023 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.
+
+from os import sys, path
+
+import synth_common
+
+UI_NOTIFICATION_TRIGGER_EVENT = "NotificationTriggerEvent"
+AD_ID_CACHE_EVENT = "AdIdCacheEvent"
+APP_SET_ID_EVENT = "AppSetIdEvent"
+
+trace = synth_common.create_trace()
+
+trace.add_ftrace_packet(cpu=0)
+
+trace.add_sys_enter(ts=100, tid=42, id=64)
+trace.add_sys_exit(ts=200, tid=42, id=64, ret=0)
+
+trace.add_atrace_begin(ts=350, tid=42, pid=42, buf=UI_NOTIFICATION_TRIGGER_EVENT)
+trace.add_atrace_end(ts=650, tid=42, pid=42)
+
+trace.add_atrace_begin(ts=750, tid=42, pid=42, buf=AD_ID_CACHE_EVENT)
+trace.add_atrace_end(ts=850, tid=42, pid=42)
+
+trace.add_atrace_begin(ts=900, tid=42, pid=42, buf=APP_SET_ID_EVENT)
+trace.add_atrace_end(ts=1200, tid=42, pid=42)
+
+sys.stdout.buffer.write(trace.trace.SerializeToString())
\ No newline at end of file
diff --git a/test/trace_processor/diff_tests/android/tests.py b/test/trace_processor/diff_tests/android/tests.py
index 8bbbeb7..58410ce 100644
--- a/test/trace_processor/diff_tests/android/tests.py
+++ b/test/trace_processor/diff_tests/android/tests.py
@@ -1186,3 +1186,22 @@
           }
         }
         """))
+
+  def test_ad_services_metric(self):
+    return DiffTestBlueprint(
+        trace=Path('ad_services_metric.py'),
+        query=Metric('ad_services_metric'),
+        out=TextProto(r"""
+         ad_services_metric {
+           ui_metric {
+             latency: 0.0003
+           }
+           app_set_id_metric {
+             latency: 0.0001
+           }
+           ad_id_metric {
+             latency:0.0003
+           }
+         }
+        """)
+    )