• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16CREATE PERFETTO FUNCTION _thread_prefix(thread_name STRING)
17RETURNS STRING AS
18SELECT STR_SPLIT(STR_SPLIT(STR_SPLIT(STR_SPLIT($thread_name, "-", 0), "[", 0), ":", 0), " ", 0);
19
20-- Per process stats of threads created in a process
21CREATE PERFETTO FUNCTION _android_thread_creation_spam(
22  -- Minimum duration between creating and destroying a thread before their the
23  -- thread creation event is considered. If NULL, considers all thread creations.
24  min_thread_dur FLOAT,
25  -- Sliding window duration for counting the thread creations. Each window
26  -- starts at the first thread creation per <process, thread_name_prefix>.
27  sliding_window_dur FLOAT)
28RETURNS TABLE(
29  -- Process name creating threads.
30  process_name STRING,
31  -- Process pid creating threads.
32  pid INT,
33  -- String prefix of thread names created.
34  thread_name_prefix STRING,
35  -- Max number of threads created within a time window.
36  max_count_per_sec INT
37) AS
38WITH
39x AS (
40  SELECT
41    pid,
42    upid,
43    _THREAD_PREFIX(thread.name) AS thread_name_prefix,
44    process.name AS process_name,
45    COUNT(thread.start_ts)
46      OVER (
47        PARTITION BY upid, thread.name
48        ORDER BY thread.start_ts
49        RANGE BETWEEN CURRENT ROW AND CAST($sliding_window_dur AS INT64) FOLLOWING
50      ) AS count
51  FROM thread
52  JOIN process
53    USING (upid)
54  WHERE
55    ($min_thread_dur AND (thread.end_ts - thread.start_ts) <= $min_thread_dur)
56    OR $min_thread_dur IS NULL
57)
58SELECT process_name, pid, thread_name_prefix, MAX(count) AS max_count_per_sec
59FROM x
60GROUP BY upid, thread_name_prefix
61HAVING max_count_per_sec > 0
62ORDER BY count DESC;
63