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-- 16-- Calculating how often each breakdown causes a scroll jank. 17-- We define that a breakdown causes a scroll jank in the janky EventLatency event if it increased 18-- more than other breakdowns relatevly to its neigbour EventLatency events. 19-- 20-- WARNING: This metric should not be used as a source of truth. It is under 21-- active development and the values & meaning might change without 22-- notice. 23 24 25SELECT RUN_METRIC('chrome/event_latency_scroll_jank.sql'); 26 27-- Calculating the jank delta for EventLatency events which are janky relatevly to its next EventLatency event. 28-- For breakdowns that exist in the current EventLatency but not the next EventLatency 29-- we use a default of 0 so that the full duration is considered when looking for the maximum increase. 30-- Breakdowns that exist in the next EventLatency event, but not in the current EventLatency event, 31-- are ignored because they do not cause a jank anyway. 32DROP VIEW IF EXISTS event_latency_scroll_breakdowns_next_jank_deltas; 33CREATE VIEW event_latency_scroll_breakdowns_next_jank_deltas 34AS 35SELECT 36 cur_breakdowns.*, 37 next_breakdowns.event_type as next_event_type, 38 next_breakdowns.slice_id as next_breakdown_id, 39 next_breakdowns.dur as next_dur, 40 cur_breakdowns.dur - COALESCE(next_breakdowns.dur, 0) as delta_dur_ns 41FROM event_latency_scroll_breakdowns_jank as cur_breakdowns LEFT JOIN event_latency_scroll_breakdowns_jank as next_breakdowns 42ON cur_breakdowns.next_event_latency_id = next_breakdowns.event_latency_id AND 43 cur_breakdowns.name = next_breakdowns.name 44WHERE cur_breakdowns.next_jank = 1; 45 46-- Calculating the jank delta for EventLatency events which are janky relatevly to its prev EventLatency event. 47-- For breakdowns that exist in the current EventLatency but not the prev EventLatency 48-- we use a default of 0 so that the full duration is considered when looking for the maximum increase. 49-- Breakdowns that exist in the prev EventLatency event, but not in the current EventLatency event, 50-- are ignored because they do not cause a jank anyway. 51DROP VIEW IF EXISTS event_latency_scroll_breakdowns_prev_jank_deltas; 52CREATE VIEW event_latency_scroll_breakdowns_prev_jank_deltas 53AS 54SELECT 55 cur_breakdowns.*, 56 prev_breakdowns.event_type as prev_event_type, 57 prev_breakdowns.slice_id as prev_breakdown_id, 58 prev_breakdowns.dur as prev_dur, 59 cur_breakdowns.dur - COALESCE(prev_breakdowns.dur, 0) as delta_dur_ns 60FROM event_latency_scroll_breakdowns_jank as cur_breakdowns LEFT JOIN event_latency_scroll_breakdowns_jank as prev_breakdowns 61ON cur_breakdowns.prev_event_latency_id = prev_breakdowns.event_latency_id AND 62 cur_breakdowns.name = prev_breakdowns.name 63WHERE cur_breakdowns.prev_jank = 1; 64 65-- Add a jank indicator to each breakdown. Jank indicator is related to an entire EventLatency envent, not only to a breakdown. 66DROP VIEW IF EXISTS event_latency_scroll_breakdowns_jank; 67CREATE VIEW event_latency_scroll_breakdowns_jank 68AS 69SELECT 70 event_latency_breakdowns.*, 71 scroll_event_latency_jank.jank, 72 scroll_event_latency_jank.next_jank, 73 scroll_event_latency_jank.prev_jank, 74 scroll_event_latency_jank.next_id as next_event_latency_id, 75 scroll_event_latency_jank.prev_id as prev_event_latency_id 76FROM event_latency_breakdowns JOIN scroll_event_latency_jank 77ON event_latency_breakdowns.event_latency_id = scroll_event_latency_jank.id 78WHERE event_latency_breakdowns.event_type in ("GESTURE_SCROLL_UPDATE", "FIRST_GESTURE_SCROLL_UPDATE", "INERTIAL_GESTURE_SCROLL_UPDATE"); 79 80-- Merge breakdowns from the |event_latency_scroll_breakdowns_next_jank_deltas| 81-- and |event_latency_scroll_breakdowns_prev_jank_deltas| tables and select the maximum |delta_dur_ns| of them. 82-- This is necessary in order to get a single reason for the jank for the event later. 83DROP VIEW IF EXISTS event_latency_scroll_breakdowns_max_jank_deltas; 84CREATE VIEW event_latency_scroll_breakdowns_max_jank_deltas 85AS 86SELECT 87 COALESCE(next.slice_id, prev.slice_id) as slice_id, 88 COALESCE(next.name, prev.name) as name, 89 COALESCE(next.event_latency_id, prev.event_latency_id) as event_latency_id, 90 COALESCE(next.event_latency_track_id, prev.event_latency_track_id) as track_id, 91 COALESCE(next.event_latency_dur, prev.event_latency_dur) as event_latency_dur, 92 COALESCE(next.event_latency_ts, prev.event_latency_ts) as event_latency_ts, 93 COALESCE(next.event_type, prev.event_type) as event_type, 94 COALESCE(next.event_latency_ts, prev.event_latency_ts) as ts, 95 COALESCE(next.event_latency_dur, prev.event_latency_dur) as dur, 96 COALESCE(next.jank, prev.jank) as jank, 97 COALESCE(next.next_jank, 0) as next_jank, 98 COALESCE(prev.prev_jank, 0) as prev_jank, 99 next.next_event_latency_id as next_event_latency_id, 100 prev.prev_event_latency_id as prev_event_latency_id, 101 next.delta_dur_ns as next_delta_dur_ns, 102 prev.delta_dur_ns as prev_delta_dur_ns, 103 CASE 104 WHEN prev.delta_dur_ns IS NULL OR next.delta_dur_ns > prev.delta_dur_ns 105 THEN next.delta_dur_ns 106 ELSE prev.delta_dur_ns 107 END as delta_dur_ns, 108 CASE 109 WHEN prev.delta_dur_ns IS NULL OR next.delta_dur_ns > prev.delta_dur_ns 110 THEN next.next_breakdown_id 111 ELSE prev.prev_breakdown_id 112 END as max_jank_neigbour_breakdown_id 113FROM event_latency_scroll_breakdowns_next_jank_deltas as next 114FULL JOIN event_latency_scroll_breakdowns_prev_jank_deltas as prev 115ON next.slice_id = prev.slice_id; 116 117-- Selecting breakdowns which have a maximum ns duration delta as a main causes of a jank for this EventLatency event. 118DROP VIEW IF EXISTS event_latency_scroll_jank_cause_top_level; 119CREATE VIEW event_latency_scroll_jank_cause_top_level 120AS 121SELECT 122 event_latency_id as slice_id, 123 track_id, 124 event_latency_dur as dur, 125 event_latency_ts as ts, 126 event_type, 127 next_jank, 128 prev_jank, 129 next_event_latency_id, 130 prev_event_latency_id, 131 next_delta_dur_ns, 132 prev_delta_dur_ns, 133 name as cause_of_jank, 134 slice_id as max_jank_breakdown_id, 135 max_jank_neigbour_breakdown_id, 136 MAX(delta_dur_ns) as max_delta_dur_ns 137FROM event_latency_scroll_breakdowns_max_jank_deltas 138GROUP BY event_latency_id; 139 140-- Selecting sub-breakdowns of the main causes of a jank which have a maximum ns duration delta with a neighbour event's breakdowns. 141DROP VIEW IF EXISTS event_latency_scroll_sub_breakdowns_max_deltas; 142CREATE VIEW event_latency_scroll_sub_breakdowns_max_deltas 143AS 144SELECT 145 cur_event_latency.slice_id as event_latency_id, 146 cur_sub_breakdowns.name as sub_breakdown_name, 147 MAX(cur_sub_breakdowns.dur - neighbour_sub_breakdowns.dur) as max_sub_breakdown_delta_dur_ns 148FROM event_latency_scroll_jank_cause_top_level as cur_event_latency 149LEFT JOIN slices as cur_sub_breakdowns 150 ON cur_event_latency.max_jank_breakdown_id = cur_sub_breakdowns.parent_id 151LEFT JOIN slices as neighbour_sub_breakdowns 152 ON cur_event_latency.max_jank_neigbour_breakdown_id = neighbour_sub_breakdowns.parent_id AND 153 cur_sub_breakdowns.name = neighbour_sub_breakdowns.name 154GROUP BY cur_event_latency.slice_id, cur_event_latency.max_jank_breakdown_id; 155 156-- Selecting the main cause of jank and its sub-cause of jank. 157DROP VIEW IF EXISTS event_latency_scroll_jank_cause; 158CREATE VIEW event_latency_scroll_jank_cause 159AS 160SELECT 161 event_latency_scroll_jank_cause_top_level.*, 162 max_sub_breakdowns.sub_breakdown_name as sub_cause_of_jank 163FROM event_latency_scroll_jank_cause_top_level 164LEFT JOIN event_latency_scroll_sub_breakdowns_max_deltas as max_sub_breakdowns 165ON event_latency_scroll_jank_cause_top_level.slice_id = max_sub_breakdowns.event_latency_id; 166 167-- Calculate how often each breakdown is a main cause of a jank for EventLatency events. 168DROP VIEW IF EXISTS event_latency_scroll_jank_cause_cnt; 169CREATE VIEW event_latency_scroll_jank_cause_cnt 170AS 171SELECT 172 cause_of_jank, 173 sub_cause_of_jank, 174 COUNT(*) as cnt 175FROM event_latency_scroll_jank_cause 176GROUP BY cause_of_jank, sub_cause_of_jank; 177