1-- 2-- Copyright 2024 The Android Open Source Project 3-- 4-- Licensed under the Apache License, Version 2.0 (the "License"); 5-- you may not use this file except in compliance with the License. 6-- You may obtain a copy of the License at 7-- 8-- https://www.apache.org/licenses/LICENSE-2.0 9-- 10-- Unless required by applicable law or agreed to in writing, software 11-- distributed under the License is distributed on an "AS IS" BASIS, 12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13-- See the License for the specific language governing permissions and 14-- limitations under the License. 15-- 16 17-- Previous runnable slice on the same thread. 18-- For each "Running" thread state finds: 19-- - previous "Runnable" (or runnable preempted) state. 20-- - previous uninterrupted "Runnable" state with a valid waker thread. 21CREATE PERFETTO TABLE sched_previous_runnable_on_thread ( 22 -- Running thread state 23 id JOINID(thread_state.id), 24 -- Previous runnable thread state. 25 prev_runnable_id JOINID(thread_state.id), 26 -- Previous runnable thread state with valid waker thread. 27 prev_wakeup_runnable_id JOINID(thread_state.id) 28) AS 29WITH 30 running_and_runnable AS ( 31 SELECT 32 id, 33 state, 34 max(id) FILTER(WHERE 35 state != 'Running') OVER utid_part AS prev_runnable_id, 36 max(id) FILTER(WHERE 37 NOT waker_utid IS NULL AND ( 38 irq_context IS NULL OR irq_context != 1 39 )) OVER utid_part AS prev_wakeup_runnable_id 40 FROM thread_state 41 -- Optimal operation for state IN (R, R+, Running) 42 WHERE 43 state GLOB 'R*' AND dur != -1 44 WINDOW utid_part AS (PARTITION BY utid ORDER BY id) 45 ) 46SELECT 47 id, 48 prev_runnable_id, 49 prev_wakeup_runnable_id 50FROM running_and_runnable 51WHERE 52 state = 'Running' 53ORDER BY 54 id; 55