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-- 16 17INCLUDE PERFETTO MODULE android.startup.startups; 18 19-- Time elapsed between the latest user start 20-- and the specific end event 21-- like package startup(ex carlauncher) or previous user stop. 22CREATE PERFETTO TABLE android_auto_multiuser_timing ( 23 -- Id of the started android user 24 event_start_user_id STRING, 25 -- Start event time 26 event_start_time LONG, 27 -- End event time 28 event_end_time LONG, 29 -- End event name 30 event_end_name STRING, 31 -- Start event name 32 event_start_name STRING, 33 -- User switch duration from start event 34 -- to end event 35 duration LONG 36) AS 37-- The last ts for user switch event. 38WITH 39 auto_multiuser_user_start AS ( 40 SELECT 41 slice.name AS event_start_name, 42 slice.ts AS user_start_time, 43 -- Ex.: UserController.startUser-11-fg-start-mode-1 44 -- User is enclosed in dashes and will be at most 2 characters(10, 11, etc.) 45 substr(name, instr(name, '-') + 1, 2) AS started_user_id 46 FROM slice 47 WHERE 48 ( 49 slice.name GLOB "UserController.startUser*" 50 AND NOT slice.name GLOB "UserController.startUser-10*" 51 ) 52 ORDER BY 53 ts DESC 54 LIMIT 1 55 ) 56SELECT 57 started_user_id AS event_start_user_id, 58 user_start_time AS event_start_time, 59 event_end_time, 60 event_end_name, 61 event_start_name, 62 ( 63 event_end_time - user_start_time 64 ) AS duration 65FROM ( 66 SELECT 67 ts_end AS event_end_time, 68 package AS event_end_name 69 FROM android_startups 70 UNION 71 SELECT 72 slice.ts + slice.dur AS event_end_time, 73 slice.name AS event_end_name 74 FROM slice 75 WHERE 76 slice.name GLOB "finishUserStopped-10*" 77) AS a 78JOIN auto_multiuser_user_start AS b 79 ON a.event_end_time > b.user_start_time; 80 81-- Previous user(user 10) total CPU time 82CREATE PERFETTO VIEW _android_auto_user_10_total_cpu_time AS 83SELECT 84 sum(dur) AS total_cpu_time, 85 ( 86 uid - android_appid 87 ) / 100000 AS user_id, 88 event_end_name 89FROM sched_slice 90JOIN thread 91 USING (utid) 92JOIN process 93 USING (upid), android_auto_multiuser_timing 94WHERE 95 user_id = 10 96 AND ts >= android_auto_multiuser_timing.event_start_time 97 AND ts <= android_auto_multiuser_timing.event_end_time 98GROUP BY 99 event_end_name; 100 101-- Previous user(user 10) total memory usage 102CREATE PERFETTO VIEW _android_auto_user_10_total_memory AS 103WITH 104 filtered_process AS ( 105 SELECT 106 c.ts, 107 c.value, 108 p.name AS proc_name, 109 ( 110 uid - android_appid 111 ) / 100000 AS user_id, 112 event_end_name 113 FROM counter AS c 114 LEFT JOIN process_counter_track AS t 115 ON c.track_id = t.id 116 LEFT JOIN process AS p 117 USING (upid), android_auto_multiuser_timing 118 WHERE 119 t.name GLOB "mem.rss" 120 AND user_id = 10 121 AND c.ts >= android_auto_multiuser_timing.event_start_time 122 AND c.ts <= android_auto_multiuser_timing.event_end_time 123 ), 124 process_rss AS ( 125 SELECT 126 *, 127 coalesce(lag(value) OVER (PARTITION BY proc_name, event_end_name ORDER BY ts), value) AS prev_value 128 FROM filtered_process 129 ), 130 per_process_allocations AS ( 131 SELECT 132 proc_name, 133 sum(value - prev_value) / 1e3 AS alloc_value_kb, 134 user_id, 135 event_end_name 136 FROM process_rss 137 WHERE 138 value - prev_value > 0 139 GROUP BY 140 proc_name, 141 event_end_name 142 ORDER BY 143 alloc_value_kb DESC 144 ) 145SELECT 146 cast_int!(SUM(alloc_value_kb)) AS total_memory_usage_kb, 147 user_id, 148 event_end_name 149FROM per_process_allocations 150GROUP BY 151 event_end_name; 152 153-- This table extends `android_auto_multiuser_timing` table with previous user resource usage. 154CREATE PERFETTO VIEW android_auto_multiuser_timing_with_previous_user_resource_usage ( 155 -- Start user id 156 event_start_user_id STRING, 157 -- Start event time 158 event_start_time LONG, 159 -- End event time 160 event_end_time LONG, 161 -- End event name 162 event_end_name STRING, 163 -- Start event name 164 event_start_name STRING, 165 -- User switch duration from start event 166 -- to end event 167 duration LONG, 168 -- User id 169 user_id LONG, 170 -- Total CPU time for a user 171 total_cpu_time LONG, 172 -- Total memory user for a user 173 total_memory_usage_kb LONG 174) AS 175SELECT 176 a.event_start_user_id, 177 a.event_start_time, 178 a.event_end_time, 179 a.event_end_name, 180 a.event_start_name, 181 a.duration, 182 b.user_id, 183 b.total_cpu_time, 184 c.total_memory_usage_kb 185FROM android_auto_multiuser_timing AS a 186LEFT JOIN _android_auto_user_10_total_cpu_time AS b 187 ON a.event_end_name = b.event_end_name 188LEFT JOIN _android_auto_user_10_total_memory AS c 189 ON a.event_end_name = c.event_end_name; 190