• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
15INCLUDE PERFETTO MODULE time.conversion;
16
17-- Percentage counter information for sysfs cpuidle states.
18-- For each state per cpu, report the incremental time spent in one state,
19-- divided by time spent in all states, between two timestamps.
20CREATE PERFETTO TABLE linux_per_cpu_idle_time_in_state_counters (
21  -- Timestamp.
22  ts TIMESTAMP,
23  -- The machine this residency is calculated for.
24  machine_id LONG,
25  -- State name.
26  state STRING,
27  -- CPU.
28  cpu LONG,
29  -- Percentage of time this cpu spent in this state.
30  idle_percentage DOUBLE,
31  -- Incremental time spent in this state (residency), in microseconds.
32  total_residency DOUBLE,
33  -- Time this cpu spent in any state, in microseconds.
34  time_slice LONG
35) AS
36WITH
37  cpu_counts_per_machine AS (
38    SELECT
39      machine_id,
40      count(1) AS cpu_count
41    FROM cpu
42    GROUP BY
43      machine_id
44  ),
45  idle_states AS (
46    SELECT
47      c.ts,
48      c.value,
49      c.track_id,
50      t.machine_id,
51      extract_arg(t.dimension_arg_set_id, 'state') AS state,
52      extract_arg(t.dimension_arg_set_id, 'cpu') AS cpu
53    FROM counter AS c
54    JOIN track AS t
55      ON c.track_id = t.id
56    WHERE
57      t.type = 'cpu_idle_state'
58  ),
59  residency_deltas AS (
60    SELECT
61      ts,
62      state,
63      cpu,
64      idle_states.machine_id,
65      track_id,
66      value - (
67        lag(value) OVER (PARTITION BY track_id ORDER BY ts)
68      ) AS total_residency,
69      (
70        time_to_us(ts - lag(ts, 1) OVER (PARTITION BY track_id ORDER BY ts))
71      ) AS time_slice
72    FROM idle_states
73  )
74SELECT
75  ts,
76  machine_id,
77  state,
78  cpu,
79  min(100, (
80    total_residency / time_slice
81  ) * 100) AS idle_percentage,
82  total_residency,
83  time_slice
84FROM residency_deltas
85WHERE
86  time_slice IS NOT NULL
87UNION ALL
88-- Calculate c0 state by subtracting all other states from total time.
89SELECT
90  ts,
91  machine_id,
92  'C0' AS state,
93  cpu,
94  max(0, (
95    (
96      time_slice - sum(total_residency)
97    ) / time_slice
98  ) * 100) AS idle_percentage,
99  max(0, time_slice - sum(total_residency)) AS total_residency,
100  time_slice
101FROM residency_deltas
102WHERE
103  time_slice IS NOT NULL
104GROUP BY
105  ts,
106  cpu,
107  machine_id;
108
109-- Percentage counter information for sysfs cpuidle states.
110-- For each state across all CPUs, report the incremental time spent in one
111-- state, divided by time spent in all states, between two timestamps.
112CREATE PERFETTO TABLE linux_cpu_idle_time_in_state_counters (
113  -- Timestamp.
114  ts TIMESTAMP,
115  -- The machine this residency is calculated for.
116  machine_id LONG,
117  -- State name.
118  state STRING,
119  -- Percentage of time all CPUS spent in this state.
120  idle_percentage DOUBLE,
121  -- Incremental time spent in this state (residency), in microseconds.
122  total_residency DOUBLE,
123  -- Time all CPUS spent in any state, in microseconds.
124  time_slice LONG
125) AS
126SELECT
127  ts,
128  machine_id,
129  state,
130  min(100, (
131    sum(total_residency) / sum(time_slice)
132  ) * 100) AS idle_percentage,
133  sum(total_residency) AS total_residency,
134  sum(time_slice) AS time_slice
135FROM linux_per_cpu_idle_time_in_state_counters
136GROUP BY
137  ts,
138  state,
139  machine_id;
140