tp: Find previous runnable slice on thread.
Change-Id: I221972530cb1923e3772c35e9133c93d1b982ee0
diff --git a/Android.bp b/Android.bp
index 722da60..9734948 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12369,6 +12369,7 @@
"src/trace_processor/perfetto_sql/stdlib/prelude/casts.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/slices.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/trace_bounds.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",
"src/trace_processor/perfetto_sql/stdlib/sched/thread_level_parallelism.sql",
diff --git a/BUILD b/BUILD
index d678720..626bceb 100644
--- a/BUILD
+++ b/BUILD
@@ -2528,6 +2528,7 @@
perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_sched_sched",
srcs = [
+ "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",
"src/trace_processor/perfetto_sql/stdlib/sched/thread_level_parallelism.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
index bd2dc94..d9b8f42 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
@@ -17,6 +17,7 @@
perfetto_sql_source_set("sched") {
deps = [ "utilization" ]
sources = [
+ "runnable.sql",
"states.sql",
"thread_executing_span.sql",
"thread_level_parallelism.sql",
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql b/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql
new file mode 100644
index 0000000..38956b1
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/runnable.sql
@@ -0,0 +1,53 @@
+--
+-- 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.
+--
+
+-- Previous runnable slice on the same thread.
+-- For each "Running" thread state finds:
+-- - 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.
+ id INT,
+ -- Previous runnable `thread_state.id`.
+ prev_runnable_id INT,
+ -- Previous runnable `thread_state.id` with valid waker
+ -- thread.
+ prev_wakeup_runnable_id INT
+) AS
+WITH running_and_runnable AS (
+ SELECT
+ id,
+ state,
+ MAX(id)
+ FILTER (WHERE state != 'Running')
+ OVER utid_part AS prev_runnable_id,
+ MAX(id)
+ FILTER (WHERE
+ waker_utid IS NOT NULL
+ AND (irq_context IS NULL OR irq_context != 1))
+ OVER utid_part AS prev_wakeup_runnable_id
+ FROM thread_state
+ -- Optimal operation for state IN (R, R+, Running)
+ WHERE state GLOB 'R*' AND dur != -1
+ WINDOW utid_part AS (PARTITION BY utid ORDER BY id)
+)
+SELECT
+ id,
+ prev_runnable_id,
+ prev_wakeup_runnable_id
+FROM running_and_runnable
+WHERE state = 'Running'
+ORDER BY id;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql b/src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql
index 348d261..ffafd6d 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql
@@ -180,6 +180,7 @@
USING (utid);
-- Mapping from running thread state to runnable
+-- TODO(zezeozue): Switch to use `sched_previous_runnable_on_thread`.
CREATE PERFETTO TABLE _waker_map
AS
WITH x AS (
diff --git a/test/trace_processor/diff_tests/stdlib/sched/tests.py b/test/trace_processor/diff_tests/stdlib/sched/tests.py
index bb24f1b..3527185 100644
--- a/test/trace_processor/diff_tests/stdlib/sched/tests.py
+++ b/test/trace_processor/diff_tests/stdlib/sched/tests.py
@@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from python.generators.diff_tests.testing import Path, DataPath, Metric
-from python.generators.diff_tests.testing import Csv, Json, TextProto
+from python.generators.diff_tests.testing import Path, DataPath
+from python.generators.diff_tests.testing import Csv
from python.generators.diff_tests.testing import DiffTestBlueprint
from python.generators.diff_tests.testing import TestSuite
@@ -263,3 +263,29 @@
"S",3868233011
"x",35240577
"""))
+
+ def test_sched_previous_runnable_on_thread(self):
+ return DiffTestBlueprint(
+ trace=DataPath('android_boot.pftrace'),
+ query="""
+ INCLUDE PERFETTO MODULE sched.runnable;
+
+ SELECT *
+ FROM sched_previous_runnable_on_thread
+ WHERE prev_wakeup_runnable_id IS NOT NULL
+ ORDER BY id DESC
+ LIMIT 10;
+ """,
+ out=Csv("""
+ "id","prev_runnable_id","prev_wakeup_runnable_id"
+ 538199,538191,538191
+ 538197,538191,538191
+ 538195,538191,538191
+ 538190,538136,538136
+ 538188,538088,533235
+ 538184,538176,524613
+ 538181,538178,537492
+ 538179,524619,524619
+ 538177,537492,537492
+ 538175,538174,524613
+ """))