• 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
16-- This module contains helpers for computing the thread-level parallelism counters,
17-- including how many threads were runnable at a given time and how many threads
18-- where running at a given point in time.
19
20INCLUDE PERFETTO MODULE intervals.overlap;
21INCLUDE PERFETTO MODULE cpu.cpus;
22
23-- The count of runnable threads over time.
24CREATE PERFETTO TABLE sched_runnable_thread_count(
25  -- Timestamp when the runnable thread count changed to the current value.
26  ts INT,
27  -- Number of runnable threads, covering the range from this timestamp to the
28  -- next row's timestamp.
29  runnable_thread_count INT
30) AS
31WITH
32runnable AS (
33  SELECT ts, dur FROM thread_state
34  where state = 'R'
35)
36SELECT
37  ts, value as runnable_thread_count
38FROM intervals_overlap_count!(runnable, ts, dur)
39ORDER BY ts;
40
41-- The count of active CPUs over time.
42CREATE PERFETTO TABLE sched_active_cpu_count(
43  -- Timestamp when the number of active CPU changed.
44  ts INT,
45  -- Number of active CPUs, covering the range from this timestamp to the next
46  -- row's timestamp.
47  active_cpu_count INT
48) AS
49WITH
50-- Filter sched events corresponding to running tasks.
51-- utid=0 is the swapper thread / idle task.
52tasks AS (
53  SELECT ts, dur
54  FROM sched
55  WHERE utid != 0
56)
57SELECT
58  ts, value as active_cpu_count
59FROM intervals_overlap_count!(tasks, ts, dur)
60ORDER BY ts;
61
62-- The count of active CPUs with a given core type over time.
63CREATE PERFETTO FUNCTION sched_active_cpu_count_for_core_type(
64  -- Type of the CPU core as reported by GUESS_CPU_SIZE. Usually 'big', 'mid' or 'little'.
65  core_type STRING
66) RETURNS TABLE(
67  -- Timestamp when the number of active CPU changed.
68  ts LONG,
69  -- Number of active CPUs, covering the range from this timestamp to the next
70  -- row's timestamp.
71  active_cpu_count LONG
72) AS
73WITH
74-- Materialise the relevant cores to avoid calling a function for each row of the sched table.
75cores AS MATERIALIZED (
76  SELECT cpu_index
77  FROM cpu_core_types
78  WHERE size = $core_type
79),
80-- Filter sched events corresponding to running tasks.
81-- utid=0 is the swapper thread / idle task.
82tasks AS (
83  SELECT ts, dur
84  FROM sched
85  WHERE
86    cpu IN (SELECT cpu_index FROM cores)
87    AND utid != 0
88)
89SELECT
90  ts, value as active_cpu_count
91FROM intervals_overlap_count!(tasks, ts, dur)
92ORDER BY ts;
93