1-- Copyright 2022 The Android Open Source Project 2-- 3-- Licensed under the Apache License, Version 2.0 (the "License"); 4-- you may not use this file except in compliance with the License. 5-- You may obtain a copy of the License at 6-- 7-- https://www.apache.org/licenses/LICENSE-2.0 8-- 9-- Unless required by applicable law or agreed to in writing, software 10-- distributed under the License is distributed on an "AS IS" BASIS, 11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12-- See the License for the specific language governing permissions and 13-- limitations under the License. 14 15-- The "reliable range" is defined as follows: 16-- 1. If a thread_track has a first_packet_on_sequence flag, the thread data is reliable for the 17-- entire duration of the trace. 18-- 2. Otherwise, the thread data is reliable from the first thread event till the end of the trace. 19-- 3. The "reliable range" is an intersection of reliable thread ranges for all threads such that: 20-- a. The number of events on the thread is at or above 25p. 21-- b. The event rate for the thread is at or above 75p. 22-- Note: this metric considers only chrome processes and their threads, i.e. the ones coming 23-- from track_event's. 24 25-- Extracts an int value with the given name from the metadata table. 26CREATE OR REPLACE PERFETTO FUNCTION _extract_int_metadata( 27 -- The name of the metadata entry. 28 name STRING) 29-- int_value for the given name. NULL if there's no such entry. 30RETURNS LONG AS 31SELECT int_value FROM metadata WHERE name = ($name); 32 33DROP VIEW IF EXISTS chrome_event_stats_per_thread; 34 35CREATE PERFETTO VIEW chrome_event_stats_per_thread 36AS 37SELECT 38 COUNT(*) AS cnt, CAST(COUNT(*) AS DOUBLE) / (MAX(ts + dur) - MIN(ts)) AS rate, utid 39FROM thread_track 40JOIN slice 41 ON thread_track.id = slice.track_id 42WHERE EXTRACT_ARG(source_arg_set_id, 'source') = 'descriptor' 43GROUP BY utid; 44 45DROP VIEW IF EXISTS chrome_event_cnt_cutoff; 46 47-- Ignore the bottom 25% of threads by event count. 25% is a somewhat arbitrary number. It creates a 48-- cutoff at around 10 events for a typical trace, and threads with fewer events are usually: 49-- 1. Not particularly interesting for the reliable range definition. 50-- 2. Create a lot of noise for other metrics, such as event rate. 51CREATE PERFETTO VIEW chrome_event_cnt_cutoff 52AS 53SELECT cnt 54FROM 55 chrome_event_stats_per_thread 56ORDER BY 57 cnt 58LIMIT 59 1 60 OFFSET( 61 (SELECT COUNT(*) FROM chrome_event_stats_per_thread) / 4); 62 63DROP VIEW IF EXISTS chrome_event_rate_cutoff; 64 65-- Choose the top 25% event rate. 25% is a somewhat arbitrary number. The goal is to strike 66-- balance between not cropping too many events and making sure that the chance of data loss in the 67-- range declared "reliable" is low. 68CREATE PERFETTO VIEW chrome_event_rate_cutoff 69AS 70SELECT rate 71FROM 72 chrome_event_stats_per_thread 73ORDER BY 74 rate 75LIMIT 76 1 77 OFFSET( 78 (SELECT COUNT(*) FROM chrome_event_stats_per_thread) * 3 / 4); 79 80DROP VIEW IF EXISTS chrome_reliable_range_per_thread; 81 82-- This view includes only threads with event count and rate above the cutoff points defined 83-- above. 84-- See b/239830951 for the analysis showing why we don't want to include all threads here 85-- (TL;DR - it makes the "reliable range" too short for a typical trace). 86CREATE PERFETTO VIEW chrome_reliable_range_per_thread 87AS 88SELECT 89 utid, 90 MIN(ts) AS start, 91 MAX(IFNULL(EXTRACT_ARG(source_arg_set_id, 'has_first_packet_on_sequence'), 0)) 92 AS has_first_packet_on_sequence 93FROM thread_track 94JOIN slice 95 ON thread_track.id = slice.track_id 96WHERE 97 utid IN ( 98 SELECT utid 99 FROM chrome_event_stats_per_thread 100 LEFT JOIN chrome_event_cnt_cutoff 101 ON 1 102 LEFT JOIN chrome_event_rate_cutoff 103 ON 1 104 WHERE 105 chrome_event_stats_per_thread.cnt >= chrome_event_cnt_cutoff.cnt 106 AND chrome_event_stats_per_thread.rate >= chrome_event_rate_cutoff.rate 107 ) 108GROUP BY utid; 109 110-- Finds Browser and Renderer processes with a missing main thread. If there 111-- is such a process, the trace definitely has thread data loss, and no part 112-- of the trace is trustworthy/reliable. 113-- As of Jan 2023, all tracing scenarios emit some data from the Browser and 114-- Renderer main thread (assuming that the corresponding process is present). 115DROP VIEW IF EXISTS chrome_processes_with_missing_main; 116 117CREATE PERFETTO VIEW chrome_processes_with_missing_main 118AS 119SELECT 120 upid 121FROM ( 122 SELECT upid, utid 123 FROM process 124 LEFT JOIN 125 -- We can't use is_main_thread column for Chrome traces - Renderer 126 -- processes have is_main_thread = 0 for the logical main thread. 127 (SELECT utid, upid FROM thread WHERE thread.name GLOB '*[Mm]ain*') 128 USING (upid) 129 WHERE 130 EXTRACT_ARG(process.arg_set_id, 'chrome.process_type') 131 IN ('Browser', 'Renderer', 'Gpu') 132) 133WHERE utid is NULL; 134 135DROP VIEW IF EXISTS chrome_processes_data_loss_free_period; 136 137CREATE PERFETTO VIEW chrome_processes_data_loss_free_period 138AS 139SELECT 140 upid AS limiting_upid, 141 -- If reliable_from is NULL, the process has data loss until the end of the trace. 142 IFNULL(reliable_from, (SELECT MAX(ts + dur) FROM slice)) AS start 143FROM 144 ( 145 SELECT upid, reliable_from 146 FROM experimental_missing_chrome_processes 147 UNION ALL 148 -- A missing main thread means that the process data is unreliable for the 149 -- entire duration of the trace. 150 SELECT upid, NULL AS reliable_from 151 FROM chrome_processes_with_missing_main 152 ) 153ORDER BY start DESC 154LIMIT 1; 155 156DROP VIEW IF EXISTS chrome_reliable_range; 157 158CREATE PERFETTO VIEW chrome_reliable_range 159AS 160SELECT 161 -- If the trace has a cropping packet, we don't want to recompute the reliable 162 -- based on cropped track events - the result might be incorrect. 163 IFNULL(_extract_int_metadata('range_of_interest_start_us') * 1000, 164 MAX(thread_start, data_loss_free_start)) AS start, 165 IIF(_extract_int_metadata('range_of_interest_start_us') IS NOT NULL, 166 'Range of interest packet', 167 IIF(limiting_upid IN (SELECT upid FROM chrome_processes_with_missing_main), 168 'Missing main thread for upid=' || limiting_upid, 169 IIF(thread_start >= data_loss_free_start, 170 'First slice for utid=' || limiting_utid, 171 'Missing process data for upid=' || limiting_upid))) AS reason, 172 limiting_upid AS debug_limiting_upid, 173 limiting_utid AS debug_limiting_utid 174FROM 175 (SELECT 176 COALESCE(MAX(start), 0) AS thread_start, 177 utid AS limiting_utid, 178 COALESCE((SELECT start FROM chrome_processes_data_loss_free_period), 0) AS data_loss_free_start, 179 (SELECT limiting_upid FROM chrome_processes_data_loss_free_period) AS limiting_upid 180 FROM chrome_reliable_range_per_thread 181 WHERE has_first_packet_on_sequence = 0); 182