1-- 2-- Copyright 2024 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-- 16INCLUDE PERFETTO MODULE slices.with_context; 17 18-- All process starts. 19CREATE PERFETTO VIEW _proc_start AS 20SELECT 21 ts, 22 dur, 23 trim(substr(name, 12)) AS process_name 24FROM thread_slice 25WHERE 26 name GLOB 'Start proc:*' AND process_name = 'system_server'; 27 28-- Broadcast, service and activity cold starts. 29CREATE PERFETTO TABLE _cold_start AS 30WITH 31 lifecycle_slice AS ( 32 SELECT 33 id, 34 ts AS intent_ts, 35 dur AS intent_dur, 36 track_id, 37 name, 38 upid, 39 process_name, 40 pid, 41 utid, 42 CASE 43 WHEN name GLOB 'performCreate:*' 44 THEN 'activity' 45 WHEN name GLOB 'serviceCreate:*' 46 THEN 'service' 47 WHEN name GLOB 'broadcastReceiveComp:*' 48 THEN 'broadcast' 49 END AS reason, 50 CASE 51 WHEN name GLOB 'performCreate:*' 52 THEN str_split(name, 'performCreate:', 1) 53 WHEN name GLOB 'serviceCreate:*' 54 THEN str_split(str_split(name, '=', 2), ' ', 0) 55 WHEN name GLOB 'broadcastReceive*' 56 THEN str_split(name, 'broadcastReceiveComp:', 1) 57 END AS intent 58 FROM thread_slice AS slice 59 WHERE 60 name GLOB 'bindApplication' 61 OR name GLOB 'performCreate:*' 62 OR name GLOB 'serviceCreate:*' 63 OR name GLOB 'broadcastReceiveComp:*' 64 ORDER BY 65 ts 66 ), 67 cold_start AS ( 68 SELECT 69 *, 70 lag(name) OVER (PARTITION BY track_id ORDER BY intent_ts) AS bind_app_name, 71 lag(intent_ts) OVER (PARTITION BY track_id ORDER BY intent_ts) AS bind_app_ts, 72 lag(intent_dur) OVER (PARTITION BY track_id ORDER BY intent_ts) AS bind_app_dur, 73 lag(id) OVER (PARTITION BY track_id ORDER BY intent_ts) AS bind_app_id 74 FROM lifecycle_slice 75 ) 76SELECT 77 * 78FROM cold_start 79WHERE 80 bind_app_name = 'bindApplication'; 81 82-- Join Broadcast, service and activity cold starts with process starts. 83CREATE PERFETTO VIEW _cold_proc_start AS 84SELECT 85 cold_start.*, 86 max(proc_start.ts) AS proc_start_ts, 87 proc_start.dur AS proc_start_dur, 88 cold_start.intent_ts - max(proc_start.ts) + cold_start.intent_dur AS total_dur 89FROM _cold_start AS cold_start 90JOIN _proc_start AS proc_start 91 ON proc_start.process_name = cold_start.process_name 92 AND cold_start.intent_ts > proc_start.ts 93GROUP BY 94 cold_start.upid; 95 96-- Provider cold starts. 97CREATE PERFETTO TABLE _provider_start AS 98WITH 99 provider_start AS ( 100 SELECT 101 id AS bind_app_id 102 FROM slice 103 WHERE 104 name = 'bindApplication' 105 EXCEPT 106 SELECT 107 bind_app_id 108 FROM _cold_start 109 ) 110SELECT 111 * 112FROM provider_start 113JOIN thread_slice 114 ON id = bind_app_id; 115 116-- Join Provider cold starts with process starts. 117CREATE PERFETTO VIEW _provider_proc_start AS 118SELECT 119 cold_start.*, 120 max(proc_start.ts) AS proc_start_ts, 121 proc_start.dur AS proc_start_dur, 122 cold_start.ts - max(proc_start.ts) + cold_start.dur AS total_dur 123FROM _provider_start AS cold_start 124JOIN _proc_start AS proc_start 125 ON proc_start.process_name = cold_start.process_name 126 AND cold_start.ts > proc_start.ts 127GROUP BY 128 cold_start.upid; 129 130-- All app cold starts with information about their cold start reason: 131-- broadcast, service, activity or provider. 132CREATE PERFETTO TABLE android_app_process_starts ( 133 -- Slice id of the bindApplication slice in the app. Uniquely identifies a process start. 134 start_id LONG, 135 -- Slice id of intent received in the app. 136 id LONG, 137 -- Track id of the intent received in the app. 138 track_id JOINID(track.id), 139 -- Name of the process receiving the intent. 140 process_name STRING, 141 -- Pid of the process receiving the intent. 142 pid LONG, 143 -- Upid of the process receiving the intent. 144 upid JOINID(process.id), 145 -- Intent action or component responsible for the cold start. 146 intent STRING, 147 -- Process start reason: activity, broadcast, service or provider. 148 reason STRING, 149 -- Timestamp the process start was dispatched from system_server. 150 proc_start_ts TIMESTAMP, 151 -- Duration to dispatch the process start from system_server. 152 proc_start_dur DURATION, 153 -- Timestamp the bindApplication started in the app. 154 bind_app_ts TIMESTAMP, 155 -- Duration to complete bindApplication in the app. 156 bind_app_dur DURATION, 157 -- Timestamp the Intent was received in the app. 158 intent_ts TIMESTAMP, 159 -- Duration to handle intent in the app. 160 intent_dur DURATION, 161 -- Total duration from proc_start dispatched to intent completed. 162 total_dur LONG 163) AS 164SELECT 165 bind_app_id AS start_id, 166 id, 167 track_id, 168 process_name, 169 pid, 170 upid, 171 intent, 172 reason, 173 proc_start_ts, 174 proc_start_dur, 175 bind_app_ts, 176 bind_app_dur, 177 intent_ts, 178 intent_dur, 179 total_dur 180FROM _cold_proc_start 181UNION ALL 182SELECT 183 bind_app_id AS start_id, 184 NULL AS id, 185 NULL AS track_id, 186 process_name, 187 pid, 188 upid, 189 NULL AS intent, 190 'provider' AS reason, 191 proc_start_ts, 192 proc_start_dur, 193 ts AS bind_app_ts, 194 dur AS bind_app_dur, 195 NULL AS intent_ts, 196 NULL AS intent_dur, 197 total_dur 198FROM _provider_proc_start; 199