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;
21
22-- The count of runnable threads over time.
23CREATE PERFETTO TABLE sched_runnable_thread_count(
24  -- Timestamp when the runnable thread count changed to the current value.
25  ts TIMESTAMP,
26  -- Number of runnable threads, covering the range from this timestamp to the
27  -- next row's timestamp.
28  runnable_thread_count LONG
29) AS
30WITH
31runnable AS (
32  SELECT ts, dur FROM thread_state
33  where state = 'R'
34)
35SELECT
36  ts, value as runnable_thread_count
37FROM intervals_overlap_count!(runnable, ts, dur)
38ORDER BY ts;
39
40-- The count of threads in uninterruptible sleep over time.
41CREATE PERFETTO TABLE sched_uninterruptible_sleep_thread_count(
42  -- Timestamp when the thread count changed to the current value.
43  ts TIMESTAMP,
44  -- Number of threads in uninterrutible sleep, covering the range from this timestamp to the
45  -- next row's timestamp.
46  uninterruptible_sleep_thread_count LONG
47) AS
48WITH
49uninterruptible_sleep AS (
50  SELECT ts, dur FROM thread_state
51  where state = 'D'
52)
53SELECT
54  ts, value as uninterruptible_sleep_thread_count
55FROM intervals_overlap_count!(uninterruptible_sleep, ts, dur)
56ORDER BY ts;
57
58-- The count of active CPUs over time.
59CREATE PERFETTO TABLE sched_active_cpu_count(
60  -- Timestamp when the number of active CPU changed.
61  ts TIMESTAMP,
62  -- Number of active CPUs, covering the range from this timestamp to the next
63  -- row's timestamp.
64  active_cpu_count LONG
65) AS
66WITH
67-- Filter sched events corresponding to running tasks.
68-- utid=0 is the swapper thread / idle task.
69tasks AS (
70  SELECT ts, dur
71  FROM sched
72  WHERE utid != 0
73)
74SELECT
75  ts, value as active_cpu_count
76FROM intervals_overlap_count!(tasks, ts, dur)
77ORDER BY ts;
78