• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-- Similar to `ancestor_slice`, but returns the slice itself in addition to strict ancestors.
17CREATE PERFETTO FUNCTION _slice_ancestor_and_self(
18    -- Id of the slice.
19    slice_id JOINID(slice.id)
20)
21RETURNS TABLE (
22  -- Slice
23  id JOINID(slice.id),
24  -- Alias of `slice.ts`.
25  ts TIMESTAMP,
26  -- Alias of `slice.dur`.
27  dur DURATION,
28  -- Alias of `slice.track_id`.
29  track_id JOINID(track.id),
30  -- Alias of `slice.category`.
31  category STRING,
32  -- Alias of `slice.name`.
33  name STRING,
34  -- Alias of `slice.depth`.
35  depth LONG,
36  -- Alias of `slice.parent_id`.
37  parent_id JOINID(slice.id),
38  -- Alias of `slice.arg_set_id`.
39  arg_set_id ARGSETID,
40  -- Alias of `slice.thread_ts`.
41  thread_ts TIMESTAMP,
42  -- Alias of `slice.thread_dur`.
43  thread_dur LONG
44) AS
45SELECT
46  id,
47  ts,
48  dur,
49  track_id,
50  category,
51  name,
52  depth,
53  parent_id,
54  arg_set_id,
55  thread_ts,
56  thread_dur
57FROM slice
58WHERE
59  id = $slice_id
60UNION ALL
61SELECT
62  id,
63  ts,
64  dur,
65  track_id,
66  category,
67  name,
68  depth,
69  parent_id,
70  arg_set_id,
71  thread_ts,
72  thread_dur
73FROM ancestor_slice($slice_id);
74
75-- Similar to `descendant_slice`, but returns the slice itself in addition to strict descendants.
76CREATE PERFETTO FUNCTION _slice_descendant_and_self(
77    -- Id of the slice.
78    slice_id JOINID(slice.id)
79)
80RETURNS TABLE (
81  -- Slice
82  id JOINID(slice.id),
83  -- Alias of `slice.ts`.
84  ts TIMESTAMP,
85  -- Alias of `slice.dur`.
86  dur DURATION,
87  -- Track.
88  track_id JOINID(track.id),
89  -- Alias of `slice.category`.
90  category STRING,
91  -- Alias of `slice.name`.
92  name STRING,
93  -- Alias of `slice.depth`.
94  depth LONG,
95  -- Alias of `slice.parent_id`.
96  parent_id JOINID(slice.id),
97  -- Alias of `slice.arg_set_id`.
98  arg_set_id ARGSETID,
99  -- Alias of `slice.thread_ts`.
100  thread_ts TIMESTAMP,
101  -- Alias of `slice.thread_dur`.
102  thread_dur LONG
103) AS
104SELECT
105  id,
106  ts,
107  dur,
108  track_id,
109  category,
110  name,
111  depth,
112  parent_id,
113  arg_set_id,
114  thread_ts,
115  thread_dur
116FROM slice
117WHERE
118  id = $slice_id
119UNION ALL
120SELECT
121  id,
122  ts,
123  dur,
124  track_id,
125  category,
126  name,
127  depth,
128  parent_id,
129  arg_set_id,
130  thread_ts,
131  thread_dur
132FROM descendant_slice($slice_id);
133
134-- Delete rows from |slice_table| where the |column_name| value is NULL.
135--
136-- The |parent_id| of the remaining rows are adjusted to point to the closest
137-- ancestor remaining. This keeps the trees as connected as possible,
138-- allowing further graph analysis.
139CREATE PERFETTO MACRO _slice_remove_nulls_and_reparent(
140    -- Table or subquery containing a subset of the slice table. Required columns are
141    -- (id LONG, parent_id LONG, depth LONG, <column_name>).
142    slice_table TableOrSubQuery,
143    -- Column name for which a NULL value indicates the row will be deleted.
144    column_name ColumnName
145)
146-- The returned table has the schema (id LONG, parent_id LONG, depth LONG, <column_name>).
147RETURNS TableOrSubQuery AS
148(
149  WITH
150    _slice AS (
151      SELECT
152        *
153      FROM $slice_table
154      WHERE
155        $column_name IS NOT NULL
156    )
157  SELECT
158    id,
159    parent_id,
160    depth,
161    $column_name
162  FROM _slice
163  WHERE
164    depth = 0
165  UNION ALL
166  SELECT
167    child.id,
168    anc.id AS parent_id,
169    max(iif(parent.$column_name IS NULL, 0, anc.depth)) AS depth,
170    child.$column_name
171  FROM _slice AS child, ancestor_slice(child.id) AS anc
172  LEFT JOIN _slice AS parent
173    ON parent.id = anc.id
174  GROUP BY
175    child.id
176);
177