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 16INCLUDE PERFETTO MODULE intervals.intersect; 17 18INCLUDE PERFETTO MODULE slices.with_context; 19 20-- For each thread slice, returns the sum of the time it spent in various 21-- scheduling states. 22-- 23-- Requires scheduling data to be available in the trace. 24CREATE PERFETTO TABLE thread_slice_time_in_state ( 25 -- Thread slice. 26 id JOINID(slice.id), 27 -- Name of the slice. 28 name STRING, 29 -- Thread the slice is running on. 30 utid JOINID(thread.id), 31 -- Name of the thread. 32 thread_name STRING, 33 -- Id of the process the slice is running on. 34 upid JOINID(process.id), 35 -- Name of the process. 36 process_name STRING, 37 -- The scheduling state (from the `thread_state` table). 38 -- 39 -- Use the `sched_state_to_human_readable_string` function in the `sched` 40 -- package to get full name. 41 state STRING, 42 -- If the `state` is uninterruptible sleep, `io_wait` indicates if it was 43 -- an IO sleep. Will be null if `state` is *not* uninterruptible sleep or if 44 -- we cannot tell if it was an IO sleep or not. 45 -- 46 -- Only available on Android when 47 -- `sched/sched_blocked_reason` ftrace tracepoint is enabled. 48 io_wait BOOL, 49 -- If in uninterruptible sleep (D), the kernel function on which was blocked. 50 -- Only available on userdebug Android builds when 51 -- `sched/sched_blocked_reason` ftrace tracepoint is enabled. 52 blocked_function LONG, 53 -- The duration of time the threads slice spent for each 54 -- (state, io_wait, blocked_function) tuple. 55 dur DURATION 56) AS 57SELECT 58 ii.id_0 AS id, 59 ts.name, 60 ts.utid, 61 ts.thread_name, 62 ts.upid, 63 ts.process_name, 64 tstate.state, 65 tstate.io_wait, 66 tstate.blocked_function, 67 sum(ii.dur) AS dur 68FROM _interval_intersect!( 69 ( 70 (SELECT * FROM thread_slice WHERE utid > 0 AND dur > 0), 71 (SELECT * FROM thread_state WHERE dur > 0) 72 ), 73 (utid) 74) AS ii 75JOIN thread_slice AS ts 76 ON ts.id = ii.id_0 77JOIN thread_state AS tstate 78 ON tstate.id = ii.id_1 79GROUP BY 80 ii.id_0, 81 tstate.state, 82 tstate.io_wait, 83 tstate.blocked_function 84ORDER BY 85 ii.id_0; 86