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 17SELECT IMPORT('android.startup.startups'); 18 19DROP VIEW IF EXISTS thread_state_extended; 20CREATE VIEW thread_state_extended AS 21SELECT 22 ts, 23 IIF(dur = -1, (SELECT end_ts FROM trace_bounds), 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 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 41WHERE 42 is_main_thread 43 -- Allowlist specific threads which need this. Do not add to this list 44 -- without careful consideration as every thread added here can cause 45 -- memory usage to balloon. 46 OR thread_name IN ( 47 'Jit thread pool' 48 ) 49GROUP BY 1, 2, 3, 4, 5; 50 51DROP VIEW IF EXISTS launch_thread_state_dur_sum; 52CREATE VIEW launch_thread_state_dur_sum AS 53SELECT startup_id, state, is_main_thread, thread_name, SUM(dur) AS dur 54FROM launch_thread_state_io_wait_dur_sum 55GROUP BY 1, 2, 3, 4; 56 57-- Given a launch id and thread state value, returns the aggregate sum 58-- of time spent in that state by the main thread of the process being started up. 59SELECT CREATE_FUNCTION( 60 'MAIN_THREAD_TIME_FOR_LAUNCH_AND_STATE(startup_id INT, state STRING)', 61 'INT', 62 ' 63 SELECT SUM(dur) 64 FROM launch_thread_state_dur_sum l 65 WHERE l.startup_id = $startup_id AND state GLOB $state AND is_main_thread; 66 ' 67); 68 69-- Given a launch id, returns the aggregate sum of time spent in runnable state 70-- by the main thread of the process being started up. 71SELECT CREATE_FUNCTION( 72 'MAIN_THREAD_TIME_FOR_LAUNCH_IN_RUNNABLE_STATE(startup_id INT)', 73 'INT', 74 ' 75 SELECT IFNULL(MAIN_THREAD_TIME_FOR_LAUNCH_AND_STATE($startup_id, "R"), 0) 76 + IFNULL(MAIN_THREAD_TIME_FOR_LAUNCH_AND_STATE($startup_id, "R+"), 0); 77 ' 78); 79 80-- Given a launch id, thread state and io_wait value, returns the aggregate sum 81-- of time spent in that state by the main thread of the process being started up. 82SELECT CREATE_FUNCTION( 83 'MAIN_THREAD_TIME_FOR_LAUNCH_STATE_AND_IO_WAIT(startup_id INT, state STRING, io_wait BOOL)', 84 'INT', 85 ' 86 SELECT SUM(dur) 87 FROM launch_thread_state_io_wait_dur_sum l 88 WHERE l.startup_id = $startup_id AND state GLOB $state 89 AND is_main_thread AND l.io_wait = $io_wait; 90 ' 91); 92 93 94-- Given a launch id, thread state value and name of a thread, returns the aggregate sum 95-- of time spent in that state by that thread. Note: only threads of the processes 96-- being started are considered by this function - if a thread from a different name 97-- happens to match the name passed, it will *not* be included. 98SELECT CREATE_FUNCTION( 99 'THREAD_TIME_FOR_LAUNCH_STATE_AND_THREAD(startup_id INT, state STRING, thread_name STRING)', 100 'INT', 101 ' 102 SELECT SUM(dur) 103 FROM launch_thread_state_dur_sum l 104 WHERE l.startup_id = $startup_id AND state GLOB $state AND thread_name = $thread_name; 105 ' 106); 107