1-- 2-- Copyright 2023 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-- Standardizes an Android thread name by extracting its core identifier to make it 17-- possible to aggregate by name. 18-- 19-- Removes extra parts of a thread name, like identifiers, leaving only the main prefix. 20-- Splits the name at ('-', '[', ':' , ' '). 21-- 22-- Some Examples: 23-- Given thread_name = "RenderThread-1[123]", 24-- returns "RenderThread". 25-- 26-- Given thread_name = "binder:5543_E" 27-- returns "binder". 28-- 29-- Given thread_name = "pool-3-thread-5", 30-- returns "pool". 31-- 32-- Given thread_name = "MainThread", 33-- returns "MainThread". 34CREATE PERFETTO FUNCTION android_standardize_thread_name( 35 -- The full android thread name to be processed. 36 thread_name STRING 37) 38-- Simplified name 39RETURNS STRING AS 40SELECT 41 CASE 42 WHEN $thread_name GLOB 'kworker/*' 43 THEN 'kworker' 44 ELSE __intrinsic_strip_hex( 45 str_split(str_split(str_split(str_split($thread_name, "-", 0), "[", 0), ":", 0), " ", 0), 46 1 47 ) 48 END; 49 50-- Per process stats of threads created in a process 51CREATE PERFETTO FUNCTION _android_thread_creation_spam( 52 -- Minimum duration between creating and destroying a thread before their the 53 -- thread creation event is considered. If NULL, considers all thread creations. 54 min_thread_dur DOUBLE, 55 -- Sliding window duration for counting the thread creations. Each window 56 -- starts at the first thread creation per <process, thread_name_prefix>. 57 sliding_window_dur DOUBLE 58) 59RETURNS TABLE ( 60 -- Process name creating threads. 61 process_name STRING, 62 -- Process pid creating threads. 63 pid LONG, 64 -- String prefix of thread names created. 65 thread_name_prefix STRING, 66 -- Max number of threads created within a time window. 67 max_count_per_sec LONG 68) AS 69WITH 70 x AS ( 71 SELECT 72 pid, 73 upid, 74 android_standardize_thread_name(thread.name) AS thread_name_prefix, 75 process.name AS process_name, 76 count(thread.start_ts) OVER (PARTITION BY upid, thread.name ORDER BY thread.start_ts RANGE BETWEEN CURRENT ROW AND cast_int!($sliding_window_dur) FOLLOWING) AS count 77 FROM thread 78 JOIN process 79 USING (upid) 80 WHERE 81 ( 82 $min_thread_dur AND ( 83 thread.end_ts - thread.start_ts 84 ) <= $min_thread_dur 85 ) 86 OR $min_thread_dur IS NULL 87 ) 88SELECT 89 process_name, 90 pid, 91 thread_name_prefix, 92 max(count) AS max_count_per_sec 93FROM x 94GROUP BY 95 upid, 96 thread_name_prefix 97HAVING 98 max_count_per_sec > 0 99ORDER BY 100 count DESC; 101