1-- 2-- Copyright 2022 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 slices.with_context; 17 18-- Checks if slice has an ancestor with provided name. 19CREATE PERFETTO FUNCTION has_parent_slice_with_name( 20 -- Id of the slice to check parents of. 21 id INT, 22 -- Name of potential ancestor slice. 23 parent_name STRING) 24-- Whether `parent_name` is a name of an ancestor slice. 25RETURNS BOOL AS 26SELECT EXISTS( 27 SELECT 1 28 FROM ancestor_slice($id) 29 WHERE name = $parent_name 30 LIMIT 1 31); 32 33-- Checks if slice has a descendant with provided name. 34CREATE PERFETTO FUNCTION has_descendant_slice_with_name( 35 -- Id of the slice to check descendants of. 36 id INT, 37 -- Name of potential descendant slice. 38 descendant_name STRING 39) 40-- Whether `descendant_name` is a name of an descendant slice. 41RETURNS BOOL AS 42SELECT EXISTS( 43 SELECT 1 44 FROM descendant_slice($id) 45 WHERE name = $descendant_name 46 LIMIT 1 47); 48 49-- Finds the end timestamp for a given slice's descendant with a given name. 50-- If there are multiple descendants with a given name, the function will return the 51-- first one, so it's most useful when working with a timeline broken down into phases, 52-- where each subphase can happen only once. 53CREATE PERFETTO FUNCTION descendant_slice_end( 54 -- Id of the parent slice. 55 parent_id INT, 56 -- Name of the child with the desired end TS. 57 child_name STRING 58) 59-- End timestamp of the child or NULL if it doesn't exist. 60RETURNS INT AS 61SELECT 62 CASE WHEN s.dur 63 IS NOT -1 THEN s.ts + s.dur 64 ELSE NULL 65 END 66FROM descendant_slice($parent_id) s 67WHERE s.name = $child_name 68LIMIT 1; 69 70-- Finds all slices with a direct parent with the given parent_id. 71CREATE PERFETTO FUNCTION direct_children_slice( 72 -- Id of the parent slice. 73 parent_id LONG) 74RETURNS TABLE( 75 -- Alias for `slice.id`. 76 id LONG, 77 -- Alias for `slice.type`. 78 type STRING, 79 -- Alias for `slice.ts`. 80 ts LONG, 81 -- Alias for `slice.dur`. 82 dur LONG, 83 -- Alias for `slice.category`. 84 category LONG, 85 -- Alias for `slice.name`. 86 name STRING, 87 -- Alias for `slice.track_id`. 88 track_id LONG, 89 -- Alias for `slice.depth`. 90 depth LONG, 91 -- Alias for `slice.parent_id`. 92 parent_id LONG, 93 -- Alias for `slice.arg_set_id`. 94 arg_set_id LONG, 95 -- Alias for `slice.thread_ts`. 96 thread_ts LONG, 97 -- Alias for `slice.thread_dur`. 98 thread_dur LONG 99) AS 100SELECT 101 slice.id, 102 slice.type, 103 slice.ts, 104 slice.dur, 105 slice.category, 106 slice.name, 107 slice.track_id, 108 slice.depth, 109 slice.parent_id, 110 slice.arg_set_id, 111 slice.thread_ts, 112 slice.thread_dur 113FROM slice 114WHERE parent_id = $parent_id; 115 116-- Given a slice id, returns the name of the slice. 117CREATE PERFETTO FUNCTION slice_name_from_id( 118 -- The slice id which we need the name for. 119 id LONG 120) 121-- The name of slice with the given id. 122RETURNS STRING AS 123SELECT 124 name 125FROM slice 126WHERE $id = id; 127 128CREATE PERFETTO FUNCTION slice_count( 129 -- Name of the slices to counted. 130 slice_glob STRING) 131-- Number of slices with the name. 132RETURNS INT AS 133SELECT COUNT(1) FROM slice WHERE name GLOB $slice_glob; 134