File: scroll_jank_cause_utils.sql

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (206 lines) | stat: -rw-r--r-- 5,377 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
-- Copyright 2023 The Chromium Authors
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.

INCLUDE PERFETTO MODULE slices.with_context;

-- Function to retrieve the upid for a surfaceflinger, as these are attributed
-- to the GPU but are recorded on a different data source (and track group).
CREATE PERFETTO FUNCTION _get_process_id_for_surfaceflinger()
-- The process id for surfaceflinger.
RETURNS LONG AS
SELECT
  upid
FROM process
WHERE
  name GLOB '*surfaceflinger*'
LIMIT 1;

-- Map a generic process type to a specific name or substring of a name that
-- can be found in the trace process table.
CREATE PERFETTO TABLE _process_type_to_name (
  -- The process type: one of 'Browser' or 'GPU'.
  process_type STRING,
  -- The process name for Chrome traces.
  process_name STRING,
  -- Substring identifying the process for system traces.
  process_glob STRING
) AS
WITH
  process_names(process_type, process_name, process_glob) AS (
    SELECT
      *
    FROM (VALUES
      ('Browser', 'Browser', '*.chrome'),
      ('GPU', 'Gpu', '*.chrome*:privileged_process*')) AS _values
  )
SELECT
  process_type,
  process_name,
  process_glob
FROM process_names;

CREATE PERFETTO FUNCTION _get_process_name(
    -- The process type: one of 'Browser' or 'GPU'.
    type STRING
)
-- The process name
RETURNS STRING AS
SELECT
  process_name
FROM _process_type_to_name
WHERE
  process_type = $type
LIMIT 1;

CREATE PERFETTO FUNCTION _get_process_glob(
    -- The process type: one of 'Browser' or 'GPU'.
    type STRING
)
-- A substring of the process name that can be used in GLOB calculations.
RETURNS STRING AS
SELECT
  process_glob
FROM _process_type_to_name
WHERE
  process_type = $type
LIMIT 1;

-- TODO(b/309937901): Add chrome instance id for multiple chromes/webviews in a
-- trace, as this may result in  multiple browser and GPU processes.
-- Function to retrieve the chrome process ID for a specific process type. Does
-- not retrieve the Renderer process, as this is determined when the
-- EventLatency is known. See function
-- _get_renderer_upid_for_event_latency below.
CREATE PERFETTO FUNCTION _get_process_id_by_type(
    -- The process type: one of 'Browser' or 'GPU'.
    type STRING
)
RETURNS TABLE (
  -- The process id for the process type.
  upid JOINID(process.id)
) AS
SELECT
  upid
FROM process
WHERE
  name = _get_process_name($type) OR name GLOB _get_process_glob($type);

-- Function to retrieve the chrome process ID that a given EventLatency slice
-- occurred on. This is the Renderer process.
CREATE PERFETTO FUNCTION _get_renderer_upid_for_event_latency(
    -- The slice id for an EventLatency slice.
    id LONG
)
-- The process id for an EventLatency slice. This is the Renderer process.
RETURNS LONG AS
SELECT
  upid
FROM process_slice
WHERE
  id = $id;

-- Helper function to retrieve all of the upids for a given process, thread,
-- or EventLatency.
CREATE PERFETTO FUNCTION _processes_by_type_for_event_latency(
    -- The process type that the thread is on: one of 'Browser', 'Renderer' or
    -- 'GPU'.
    type STRING,
    -- The name of the thread.
    thread STRING,
    -- The slice id of an EventLatency slice.
    event_latency_id LONG
)
RETURNS TABLE (
  upid JOINID(process.id)
) AS
WITH
  all_upids AS (
    -- Renderer process upids
    SELECT
      $type AS process,
      $thread AS thread,
      $event_latency_id AS event_latency_id,
      _get_renderer_upid_for_event_latency($event_latency_id) AS upid
    WHERE
      $type = 'Renderer'
    UNION ALL
    -- surfaceflinger upids
    SELECT
      $type AS process,
      $thread AS thread,
      $event_latency_id AS event_latency_id,
      _get_process_id_for_surfaceflinger() AS upid
    WHERE
      $type = 'GPU' AND $thread = 'surfaceflinger'
    UNION ALL
    -- Generic Browser and GPU process upids
    SELECT
      $type AS process,
      $thread AS thread,
      $event_latency_id AS event_latency_id,
      upid
    FROM _get_process_id_by_type($type)
    WHERE
      $type = 'Browser' OR (
        $type = 'GPU' AND $thread != 'surfaceflinger'
      )
  )
SELECT
  upid
FROM all_upids;

-- Function to retrieve the thread id of the thread on a particular process if
-- there are any slices during a particular EventLatency slice duration; this
-- upid/thread combination refers to a cause of Scroll Jank.
CREATE PERFETTO FUNCTION chrome_select_scroll_jank_cause_thread(
    -- The slice id of an EventLatency slice.
    event_latency_id LONG,
    -- The process type that the thread is on: one of 'Browser', 'Renderer' or
    -- 'GPU'.
    process_type STRING,
    -- The name of the thread.
    thread_name STRING
)
RETURNS TABLE (
  -- The utid associated with |thread| on the process with |upid|.
  utid JOINID(thread.id)
) AS
WITH
  threads AS (
    SELECT
      utid
    FROM thread
    WHERE
      upid IN (
        SELECT DISTINCT
          upid
        FROM _processes_by_type_for_event_latency($process_type, $thread_name, $event_latency_id)
      )
      AND name = $thread_name
  )
SELECT DISTINCT
  utid
FROM thread_slice
WHERE
  utid IN (
    SELECT
      utid
    FROM threads
  )
  AND ts >= (
    SELECT
      ts
    FROM slice
    WHERE
      id = $event_latency_id
    LIMIT 1
  )
  AND ts <= (
    SELECT
      ts + dur
    FROM slice
    WHERE
      id = $event_latency_id
    LIMIT 1
  );