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-- CPU frequency counter per core. 17CREATE PERFETTO VIEW _cpu_freq_counters 18AS 19SELECT 20 ts, 21 dur, 22 value AS freq_value, 23 cct.cpu 24FROM experimental_counter_dur ecd 25LEFT JOIN cpu_counter_track cct 26 ON ecd.track_id = cct.id 27WHERE cct.name = 'cpufreq'; 28 29-- CPU idle counter per core. 30CREATE PERFETTO VIEW _cpu_idle_counters 31AS 32SELECT 33 ts, 34 dur, 35 -- Correct 4294967295 to -1 (both of them means an exit from the current state). 36 iif(value = 4294967295, -1, CAST(value AS int)) AS idle_value, 37 cct.cpu 38FROM experimental_counter_dur ecd 39LEFT JOIN cpu_counter_track cct 40 ON ecd.track_id = cct.id 41WHERE cct.name = 'cpuidle'; 42 43-- Combined cpu freq & idle counter 44CREATE VIRTUAL TABLE _freq_idle_counters 45USING 46 span_join(_cpu_freq_counters PARTITIONED cpu, _cpu_idle_counters PARTITIONED cpu); 47 48-- Aggregates cpu idle statistics per core. 49CREATE PERFETTO TABLE linux_cpu_idle_stats( 50 -- CPU core number. 51 cpu INT, 52 -- CPU idle state (C-states). 53 state INT, 54 -- The count of entering idle state. 55 count INT, 56 -- Total CPU core idle state duration in nanoseconds. 57 dur INT, 58 -- Average CPU core idle state duration in nanoseconds. 59 avg_dur INT, 60 -- Idle state percentage of non suspend time (C-states + P-states). 61 idle_percent FLOAT 62) 63AS 64WITH 65total AS ( 66 SELECT 67 cpu, 68 sum(dur) AS dur 69 FROM _freq_idle_counters 70 GROUP BY cpu 71) 72SELECT 73 cpu, 74 (idle_value + 1) AS state, 75 COUNT(idle_value) AS count, 76 SUM(dur) AS dur, 77 SUM(dur) / COUNT(idle_value) AS avg_dur, 78 SUM(dur) * 100.0 / (SELECT dur FROM total t WHERE t.cpu = ific.cpu) AS idle_percent 79FROM _freq_idle_counters ific 80WHERE idle_value >=0 81GROUP BY cpu, idle_value; 82