stdlib: Add sched.latency module
Fixes:371047772
Change-Id: I52cf87b4933dc14f2269de04634eaf6ca7549100
diff --git a/Android.bp b/Android.bp
index 621eedc..fa82c12 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13654,6 +13654,7 @@
"src/trace_processor/perfetto_sql/stdlib/prelude/after_eof/views.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/before_eof/tables.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/before_eof/trace_bounds.sql",
+ "src/trace_processor/perfetto_sql/stdlib/sched/latency.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/states.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql",
diff --git a/BUILD b/BUILD
index d4d3aff..64c1e49 100644
--- a/BUILD
+++ b/BUILD
@@ -3167,6 +3167,7 @@
perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_sched_sched",
srcs = [
+ "src/trace_processor/perfetto_sql/stdlib/sched/latency.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/states.sql",
"src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
index d7e9608..3a2ccbe 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
@@ -16,6 +16,7 @@
perfetto_sql_source_set("sched") {
sources = [
+ "latency.sql",
"runnable.sql",
"states.sql",
"thread_executing_span.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/latency.sql b/src/trace_processor/perfetto_sql/stdlib/sched/latency.sql
new file mode 100644
index 0000000..50dd7e7
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/latency.sql
@@ -0,0 +1,51 @@
+--
+-- 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 sched.runnable;
+
+CREATE PERFETTO VIEW _sched_with_thread_state_join AS
+SELECT
+ thread_state.id AS thread_state_id,
+ sched.id AS sched_id
+FROM sched
+JOIN thread_state USING (utid, ts, dur);
+
+-- Scheduling latency of running thread states.
+-- For each time the thread was running, returns the duration of the runnable
+-- state directly before.
+CREATE PERFETTO TABLE sched_latency_for_running_interval(
+ -- Running state of the thread. Alias of `thread_state.id`.
+ thread_state_id INT,
+ -- Id of a corresponding slice in a `sched` table. Alias of `sched.id`.
+ sched_id INT,
+ -- Thread with running state. Alias of `thread.id`.
+ utid INT,
+ -- Runnable state before thread is "running". Duration of this thread state
+ -- is `latency_dur`. One of `thread_state.id`.
+ runnable_latency_id INT,
+ -- Scheduling latency of thread state. Duration of thread state with
+ -- `runnable_latency_id`.
+ latency_dur INT
+) AS
+SELECT
+ r.id AS thread_state_id,
+ sched_id,
+ utid,
+ prev_runnable_id AS runnable_latency_id,
+ dur AS latency_dur
+FROM sched_previous_runnable_on_thread r
+JOIN thread_state prev_ts ON prev_runnable_id = prev_ts.id
+JOIN _sched_with_thread_state_join ON thread_state_id = r.id
+
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql b/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql
index 38956b1..ffe8b55 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql
@@ -19,12 +19,12 @@
-- - previous "Runnable" (or runnable preempted) state.
-- - previous uninterrupted "Runnable" state with a valid waker thread.
CREATE PERFETTO TABLE sched_previous_runnable_on_thread(
- -- `thread_state.id` id.
+ -- Alias of `thread_state.id`.
id INT,
- -- Previous runnable `thread_state.id`.
+ -- Previous runnable thread state. Alias of `thread_state.id`.
prev_runnable_id INT,
- -- Previous runnable `thread_state.id` with valid waker
- -- thread.
+ -- Previous runnable thread state with valid waker thread. Alias of
+ -- `thread_state.id`.
prev_wakeup_runnable_id INT
) AS
WITH running_and_runnable AS (
diff --git a/test/trace_processor/diff_tests/stdlib/sched/tests.py b/test/trace_processor/diff_tests/stdlib/sched/tests.py
index 1de62b5..a327125 100644
--- a/test/trace_processor/diff_tests/stdlib/sched/tests.py
+++ b/test/trace_processor/diff_tests/stdlib/sched/tests.py
@@ -187,3 +187,33 @@
538177,537492,537492
538175,538174,524613
"""))
+
+ def test_sched_latency(self):
+ return DiffTestBlueprint(
+ trace=DataPath('android_boot.pftrace'),
+ query="""
+ INCLUDE PERFETTO MODULE sched.latency;
+
+ SELECT
+ thread_state_id,
+ sched_id,
+ utid,
+ runnable_latency_id,
+ latency_dur
+ FROM sched_latency_for_running_interval
+ ORDER BY thread_state_id DESC
+ LIMIT 10;
+ """,
+ out=Csv("""
+ "thread_state_id","sched_id","utid","runnable_latency_id","latency_dur"
+ 538199,269427,2,538191,91919
+ 538197,269425,2,538191,91919
+ 538195,269423,2,538191,91919
+ 538190,269422,1330,538136,1437215
+ 538188,269420,2,538088,826823
+ 538184,269419,91,538176,131388
+ 538181,269418,319,538178,4883
+ 538179,269417,1022,524619,469849
+ 538177,269416,319,537492,670736
+ 538175,269415,91,538174,12532
+ """))