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-- TODO(mayzner): Replace with good implementation of interval intersect. 17CREATE PERFETTO MACRO _interval_intersect_partition_utid( 18 left_table TableOrSubquery, 19 right_table TableOrSubquery 20) 21RETURNS TableOrSubquery AS 22( 23 WITH on_left AS ( 24 SELECT 25 B.ts, 26 IIF( 27 A.ts + A.dur <= B.ts + B.dur, 28 A.ts + A.dur - B.ts, B.dur) AS dur, 29 A.id AS left_id, 30 B.id as right_id 31 FROM $left_table A 32 JOIN $right_table B ON (A.ts <= B.ts AND A.ts + A.dur > B.ts AND A.utid = B.utid) 33 ), on_right AS ( 34 SELECT 35 B.ts, 36 IIF( 37 A.ts + A.dur <= B.ts + B.dur, 38 A.ts + A.dur - B.ts, B.dur) AS dur, 39 B.id as left_id, 40 A.id AS right_id 41 FROM $right_table A 42 -- The difference between this table and on_left is the lack of equality on 43 -- A.ts <= B.ts. This is to remove the issue of double accounting 44 -- timestamps that start at the same time. 45 JOIN $left_table B ON (A.ts < B.ts AND A.ts + A.dur > B.ts AND A.utid = B.utid) 46 ) 47 SELECT * FROM on_left 48 UNION ALL 49 SELECT * FROM on_right 50); 51 52-- Time each thread slice spent running on CPU. 53-- Requires scheduling data to be available in the trace. 54CREATE PERFETTO TABLE thread_slice_cpu_time( 55 -- Slice id. 56 id INT, 57 -- Duration of the time the slice was running. 58 cpu_time INT) AS 59WITH slice_with_utid AS ( 60 SELECT 61 slice.id, 62 slice.ts, 63 slice.dur, 64 utid 65 FROM slice 66 JOIN thread_track ON slice.track_id = thread_track.id 67 JOIN thread USING (utid) 68 WHERE utid != 0) 69SELECT left_id AS id, SUM(dur) AS cpu_time 70FROM _interval_intersect_partition_utid!(slice_with_utid, sched) 71GROUP BY 1 72ORDER BY 1;