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 17INCLUDE PERFETTO MODULE android.startup.startups; 18 19DROP VIEW IF EXISTS thread_state_extended; 20CREATE PERFETTO VIEW thread_state_extended AS 21SELECT 22 ts, 23 IIF(dur = -1, trace_end(), dur) AS dur, 24 utid, 25 state, 26 io_wait 27FROM thread_state; 28 29DROP TABLE IF EXISTS launch_threads_by_thread_state; 30CREATE VIRTUAL TABLE launch_threads_by_thread_state 31USING SPAN_JOIN( 32 android_startup_threads PARTITIONED utid, 33 thread_state_extended PARTITIONED utid 34); 35 36-- Materialized to avoid repeatedly span joining per each thread state. 37DROP TABLE IF EXISTS launch_thread_state_io_wait_dur_sum; 38CREATE PERFETTO TABLE launch_thread_state_io_wait_dur_sum AS 39SELECT startup_id, state, is_main_thread, thread_name, io_wait, SUM(dur) AS dur 40FROM launch_threads_by_thread_state l 41JOIN android_startup_processes p USING (startup_id) 42WHERE 43 -- If it is a main thread, only add it if it is the lauching thread. 44 (is_main_thread AND p.startup_type NOT NULL) 45 -- Allowlist specific threads which need this. Do not add to this list 46 -- without careful consideration as every thread added here can cause 47 -- memory usage to balloon. 48 OR thread_name IN ( 49 'Jit thread pool' 50 ) 51GROUP BY 1, 2, 3, 4, 5; 52 53DROP VIEW IF EXISTS launch_thread_state_dur_sum; 54CREATE PERFETTO VIEW launch_thread_state_dur_sum AS 55SELECT startup_id, state, is_main_thread, thread_name, SUM(dur) AS dur 56FROM launch_thread_state_io_wait_dur_sum 57GROUP BY 1, 2, 3, 4; 58 59-- Given a launch id and thread state value, returns the aggregate sum 60-- of time spent in that state by the main thread of the process being started up. 61CREATE OR REPLACE PERFETTO FUNCTION main_thread_time_for_launch_and_state(startup_id INT, state STRING) 62RETURNS INT AS 63SELECT SUM(dur) 64FROM launch_thread_state_dur_sum l 65WHERE l.startup_id = $startup_id AND state GLOB $state AND is_main_thread; 66 67-- Given a launch id, returns the aggregate sum of time spent in runnable state 68-- by the main thread of the process being started up. 69CREATE OR REPLACE PERFETTO FUNCTION main_thread_time_for_launch_in_runnable_state(startup_id INT) 70RETURNS INT AS 71SELECT IFNULL(main_thread_time_for_launch_and_state($startup_id, "R"), 0) 72 + IFNULL(main_thread_time_for_launch_and_state($startup_id, "R+"), 0); 73 74-- Given a launch id, thread state and io_wait value, returns the aggregate sum 75-- of time spent in that state by the main thread of the process being started up. 76CREATE OR REPLACE PERFETTO FUNCTION main_thread_time_for_launch_state_and_io_wait(startup_id INT, state STRING, io_wait BOOL) 77RETURNS INT AS 78SELECT SUM(dur) 79FROM launch_thread_state_io_wait_dur_sum l 80WHERE l.startup_id = $startup_id AND state GLOB $state 81 AND is_main_thread AND l.io_wait = $io_wait; 82 83 84-- Given a launch id, thread state value and name of a thread, returns the aggregate sum 85-- of time spent in that state by that thread. Note: only threads of the processes 86-- being started are considered by this function - if a thread from a different name 87-- happens to match the name passed, it will *not* be included. 88CREATE OR REPLACE PERFETTO FUNCTION thread_time_for_launch_state_and_thread(startup_id INT, state STRING, thread_name STRING) 89RETURNS INT AS 90SELECT SUM(dur) 91FROM launch_thread_state_dur_sum l 92WHERE l.startup_id = $startup_id AND state GLOB $state AND thread_name = $thread_name; 93 94 95-- Given a launch id, returns the duration between the launch and a running state thread of 96-- startup process. 97CREATE OR REPLACE PERFETTO FUNCTION time_to_running_state_for_launch(startup_id LONG) 98RETURNS PROTO AS 99 SELECT NULL_IF_EMPTY( 100 STARTUP_SLICE_PROTO( 101 IIF(MIN(l.ts) > launches.ts, MIN(l.ts) - launches.ts, NULL))) 102 FROM launch_threads_by_thread_state l 103 JOIN android_startups launches USING(startup_id) 104 WHERE l.startup_id = $startup_id AND l.state = "Running"; 105