Add table for main thread blocking calls in standard library.
This table represents blocking calls that run on the main thread for all
processes in a trace.
Test: perfetto diff tests
Bug: 330479146
Change-Id: I8d09f3e3bf23fe6d768861fc9ef9788847604ec2
diff --git a/Android.bp b/Android.bp
index d5da1dd..27ab5f9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12314,6 +12314,7 @@
"src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
"src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
"src/trace_processor/perfetto_sql/stdlib/android/broadcasts.sql",
+ "src/trace_processor/perfetto_sql/stdlib/android/critical_blocking_calls.sql",
"src/trace_processor/perfetto_sql/stdlib/android/dvfs.sql",
"src/trace_processor/perfetto_sql/stdlib/android/freezer.sql",
"src/trace_processor/perfetto_sql/stdlib/android/garbage_collection.sql",
diff --git a/BUILD b/BUILD
index 8eedd8d..6767763 100644
--- a/BUILD
+++ b/BUILD
@@ -2384,6 +2384,7 @@
"src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
"src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
"src/trace_processor/perfetto_sql/stdlib/android/broadcasts.sql",
+ "src/trace_processor/perfetto_sql/stdlib/android/critical_blocking_calls.sql",
"src/trace_processor/perfetto_sql/stdlib/android/dvfs.sql",
"src/trace_processor/perfetto_sql/stdlib/android/freezer.sql",
"src/trace_processor/perfetto_sql/stdlib/android/garbage_collection.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
index 29c4b89..87f8dec 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
@@ -23,6 +23,7 @@
"battery_stats.sql",
"binder.sql",
"broadcasts.sql",
+ "critical_blocking_calls.sql",
"dvfs.sql",
"freezer.sql",
"garbage_collection.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/critical_blocking_calls.sql b/src/trace_processor/perfetto_sql/stdlib/android/critical_blocking_calls.sql
new file mode 100644
index 0000000..ba5a3f6
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/android/critical_blocking_calls.sql
@@ -0,0 +1,84 @@
+--
+-- Copyright 2024 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.
+--
+INCLUDE PERFETTO MODULE android.slices;
+INCLUDE PERFETTO MODULE android.binder;
+INCLUDE PERFETTO MODULE slices.with_context;
+
+CREATE PERFETTO FUNCTION _is_relevant_blocking_call(name STRING, depth INT)
+RETURNS BOOL AS SELECT
+ $name = 'measure'
+ OR $name = 'layout'
+ OR $name = 'configChanged'
+ OR $name = 'animation'
+ OR $name = 'input'
+ OR $name = 'traversal'
+ OR $name = 'Contending for pthread mutex'
+ OR $name = 'postAndWait'
+ OR $name GLOB 'monitor contention with*'
+ OR $name GLOB 'SuspendThreadByThreadId*'
+ OR $name GLOB 'LoadApkAssetsFd*'
+ OR $name GLOB '*binder transaction*'
+ OR $name GLOB 'inflate*'
+ OR $name GLOB 'Lock contention on*'
+ OR $name GLOB 'android.os.Handler: kotlinx.coroutines*'
+ OR $name GLOB 'relayoutWindow*'
+ OR $name GLOB 'ImageDecoder#decode*'
+ OR $name GLOB 'NotificationStackScrollLayout#onMeasure'
+ OR $name GLOB 'ExpNotRow#*'
+ OR $name GLOB 'GC: Wait For*'
+ OR (
+ -- Some top level handler slices
+ $depth = 0
+ AND $name NOT GLOB '*Choreographer*'
+ AND $name NOT GLOB '*Input*'
+ AND $name NOT GLOB '*input*'
+ AND $name NOT GLOB 'android.os.Handler: #*'
+ AND (
+ -- Handler pattern heuristics
+ $name GLOB '*Handler: *$*'
+ OR $name GLOB '*.*.*: *$*'
+ OR $name GLOB '*.*$*: #*'
+ )
+ );
+
+
+--Extract all slice data on main thread for all processes.
+CREATE PERFETTO TABLE _android_critical_blocking_calls AS
+SELECT
+ android_standardize_slice_name(s.name) AS name,
+ s.ts,
+ s.dur,
+ s.id,
+ s.process_name,
+ thread.utid,
+ s.upid
+FROM thread_slice s JOIN
+thread USING (utid)
+WHERE
+ thread.is_main_thread AND _is_relevant_blocking_call(s.name, s.depth)
+UNION ALL
+-- As binder names are not included in slice table, extract these directly from the
+-- android_binder_txns table.
+SELECT
+ tx.aidl_name AS name,
+ tx.client_ts AS ts,
+ tx.client_dur AS dur,
+ tx.binder_txn_id AS id,
+ tx.client_process as process_name,
+ tx.client_utid as utid,
+ tx.client_upid as upid
+FROM android_binder_txns AS tx
+WHERE is_main_thread AND aidl_name IS NOT NULL AND is_sync = 1;