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. 15 16INCLUDE PERFETTO MODULE linux.cpu.idle; 17 18-- Aggregates cpu idle statistics per core. 19CREATE PERFETTO TABLE cpu_idle_stats ( 20 -- CPU core number. 21 cpu LONG, 22 -- CPU idle state (C-states). 23 state LONG, 24 -- The count of entering idle state. 25 count LONG, 26 -- Total CPU core idle state duration. 27 dur DURATION, 28 -- Average CPU core idle state duration. 29 avg_dur DURATION, 30 -- Idle state percentage of non suspend time (C-states + P-states). 31 idle_percent DOUBLE 32) AS 33WITH 34 grouped AS ( 35 SELECT 36 cpu, 37 ( 38 idle + 1 39 ) AS state, 40 count(idle) AS count, 41 sum(dur) AS dur, 42 sum(dur) / count(idle) AS avg_dur 43 FROM cpu_idle_counters AS c 44 WHERE 45 c.idle >= 0 46 GROUP BY 47 c.cpu, 48 c.idle 49 ), 50 total AS ( 51 SELECT 52 cpu, 53 sum(dur) AS dur 54 FROM cpu_idle_counters 55 GROUP BY 56 cpu 57 ) 58SELECT 59 g.*, 60 g.dur * 100.0 / t.dur AS idle_percent 61FROM grouped AS g 62JOIN total AS t 63 USING (cpu); 64