Extract ANR sql into perfetto module

Bug: 297193790
Test: tools/diff_test_trace_processor.py
Change-Id: I331344206979e12ffc129d4a4ca7e722301a1f57
diff --git a/Android.bp b/Android.bp
index aaff342..2533f4d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -10449,7 +10449,6 @@
         "src/trace_processor/metrics/sql/android/android_task_names.sql",
         "src/trace_processor/metrics/sql/android/android_trace_quality.sql",
         "src/trace_processor/metrics/sql/android/android_trusty_workqueues.sql",
-        "src/trace_processor/metrics/sql/android/anr/anrs.sql",
         "src/trace_processor/metrics/sql/android/codec_metrics.sql",
         "src/trace_processor/metrics/sql/android/composer_execution.sql",
         "src/trace_processor/metrics/sql/android/composition_layers.sql",
@@ -10757,6 +10756,7 @@
 genrule {
     name: "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
     srcs: [
+        "src/trace_processor/perfetto_sql/stdlib/android/anrs.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
diff --git a/BUILD b/BUILD
index dbd0bd1..914b521 100644
--- a/BUILD
+++ b/BUILD
@@ -1877,7 +1877,6 @@
         "src/trace_processor/metrics/sql/android/android_task_names.sql",
         "src/trace_processor/metrics/sql/android/android_trace_quality.sql",
         "src/trace_processor/metrics/sql/android/android_trusty_workqueues.sql",
-        "src/trace_processor/metrics/sql/android/anr/anrs.sql",
         "src/trace_processor/metrics/sql/android/codec_metrics.sql",
         "src/trace_processor/metrics/sql/android/composer_execution.sql",
         "src/trace_processor/metrics/sql/android/composition_layers.sql",
@@ -2249,6 +2248,7 @@
 perfetto_filegroup(
     name = "src_trace_processor_perfetto_sql_stdlib_android_android",
     srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/android/anrs.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
diff --git a/src/trace_processor/metrics/sql/android/BUILD.gn b/src/trace_processor/metrics/sql/android/BUILD.gn
index f8c5303..99223c0 100644
--- a/src/trace_processor/metrics/sql/android/BUILD.gn
+++ b/src/trace_processor/metrics/sql/android/BUILD.gn
@@ -45,8 +45,8 @@
     "android_lmk_reason.sql",
     "android_mem.sql",
     "android_mem_unagg.sql",
-    "android_monitor_contention_agg.sql",
     "android_monitor_contention.sql",
+    "android_monitor_contention_agg.sql",
     "android_multiuser.sql",
     "android_multiuser_populator.sql",
     "android_netperf.sql",
@@ -62,7 +62,6 @@
     "android_task_names.sql",
     "android_trace_quality.sql",
     "android_trusty_workqueues.sql",
-    "anr/anrs.sql",
     "codec_metrics.sql",
     "composer_execution.sql",
     "composition_layers.sql",
diff --git a/src/trace_processor/metrics/sql/android/android_anr.sql b/src/trace_processor/metrics/sql/android/android_anr.sql
index 17d6f95..f97a49d 100644
--- a/src/trace_processor/metrics/sql/android/android_anr.sql
+++ b/src/trace_processor/metrics/sql/android/android_anr.sql
@@ -13,9 +13,7 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
--- Create the base table (`android_anr_anrs`) containing all ANRs found
--- in the trace.
-SELECT RUN_METRIC('android/anr/anrs.sql');
+INCLUDE PERFETTO MODULE android.anrs;
 
 DROP VIEW IF EXISTS android_anr_output;
 CREATE VIEW android_anr_output AS
@@ -29,6 +27,6 @@
           'subject', subject,
           'error_id', error_id,
           'ts', ts))
-      FROM android_anr_anrs
+      FROM android_anrs
     )
   );
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
index b6f8cd6..8a70e98 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
@@ -17,6 +17,7 @@
 perfetto_sql_source_set("android") {
   deps = [ "startup" ]
   sources = [
+    "anrs.sql",
     "battery.sql",
     "battery_stats.sql",
     "binder.sql",
diff --git a/src/trace_processor/metrics/sql/android/anr/anrs.sql b/src/trace_processor/perfetto_sql/stdlib/android/anrs.sql
similarity index 68%
rename from src/trace_processor/metrics/sql/android/anr/anrs.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/anrs.sql
index cba5d84..86c9fdc 100644
--- a/src/trace_processor/metrics/sql/android/anr/anrs.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/android/anrs.sql
@@ -1,5 +1,5 @@
 --
--- Copyright 2022 The Android Open Source Project
+-- 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.
@@ -12,12 +12,19 @@
 -- 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.
+--
 
--- One row per ANR that occurred in the trace.
-DROP TABLE IF EXISTS android_anr_anrs;
-CREATE PERFETTO TABLE android_anr_anrs AS
+-- List of all ANRs that occurred in the trace (one row per ANR).
+--
+-- @column process_name  Name of the process that triggered the ANR.
+-- @column pid           PID of the process that triggered the ANR.
+-- @column upid          UPID of the process that triggered the ANR.
+-- @column error_id      UUID of the ANR (generated on the platform).
+-- @column ts            Timestamp of the ANR.
+-- @column subject       Subject line of the ANR.
+CREATE VIEW android_anrs AS
 -- Process and PID that ANRed.
-WITH anr_process AS (
+WITH anr AS (
   SELECT
     -- Counter formats:
     -- v1: "ErrorId:<process_name>#<UUID>"
@@ -33,7 +40,7 @@
     AND process.name = 'system_server'
 ),
 -- ANR subject line.
-anr_subject AS (
+subject AS (
   --- Counter format:
   --- "Subject(for ErrorId <UUID>):<subject>"
   SELECT
@@ -45,6 +52,13 @@
   WHERE process_counter_track.name GLOB 'Subject(for ErrorId *'
   AND process.name = 'system_server'
 )
-SELECT *
-FROM anr_process
-LEFT JOIN anr_subject USING (error_id);
\ No newline at end of file
+SELECT
+    anr.process_name,
+    anr.pid,
+    process.upid,
+    anr.error_id,
+    anr.ts,
+    subject
+FROM anr
+LEFT JOIN subject USING (error_id)
+LEFT JOIN process ON (process.pid = anr.pid);
\ 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 791346f..71778ea 100644
--- a/test/trace_processor/diff_tests/android/tests.py
+++ b/test/trace_processor/diff_tests/android/tests.py
@@ -277,6 +277,21 @@
         """,
         out=Path('android_network_activity.out'))
 
+  def test_anrs(self):
+    return DiffTestBlueprint(
+        trace=Path('android_anr_metric.py'),
+        query="""
+        INCLUDE PERFETTO MODULE android.anrs;
+        SELECT *
+        FROM android_anrs;
+      """,
+        out=Csv("""
+        "process_name","pid","upid","error_id","ts","subject"
+        "com.google.android.app1",11167,"[NULL]","da24554c-452a-4ae1-b74a-fb898f6e0982",1000,"Test ANR subject 1"
+        "com.google.android.app2","[NULL]","[NULL]","8612fece-c2f1-4aeb-9d45-8e6d9d0201cf",2000,"Test ANR subject 2"
+        "com.google.android.app3","[NULL]","[NULL]","c25916a0-a8f0-41f3-87df-319e06471a0f",3000,"[NULL]"
+      """))
+
   def test_anr_metric(self):
     return DiffTestBlueprint(
         trace=Path('android_anr_metric.py'),