1-- 2-- Copyright 2022 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 16SELECT RUN_METRIC("android/android_cpu_agg.sql"); 17 18DROP VIEW IF EXISTS p_state_cpu_idle_counter; 19CREATE PERFETTO VIEW p_state_cpu_idle_counter AS 20SELECT 21 ts, 22 ts - LAG(ts) OVER ( 23 PARTITION BY track_id 24 ORDER BY 25 ts 26 ) AS dur, 27 cpu, 28 iif(value = 4294967295, -1, cast(value AS int)) AS idle_value 29FROM 30 counter c 31JOIN cpu_counter_track t ON c.track_id = t.id 32WHERE 33 t.name = "cpuidle"; 34 35DROP TABLE IF EXISTS p_state_sched_freq_idle; 36CREATE VIRTUAL TABLE p_state_sched_freq_idle USING span_join( 37 cpu_freq_sched_per_thread PARTITIONED cpu, 38 p_state_cpu_idle_counter PARTITIONED cpu 39); 40 41CREATE OR REPLACE PERFETTO FUNCTION p_state_over_interval( 42 start_ns LONG, end_ns LONG) 43RETURNS TABLE(cpu INT, freq_khz INT, idle_value INT, dur_ns INT) 44AS 45WITH sched_freq_idle_windowed AS ( 46 SELECT 47 freq_khz, 48 idle_value, 49 cpu, 50 IIF(ts + dur <= $end_ns, ts + dur, $end_ns) - IIF(ts >= $start_ns, ts, $start_ns) AS dur 51 FROM 52 p_state_sched_freq_idle 53 WHERE 54 ts + dur > $start_ns 55 AND ts < $end_ns 56) 57SELECT 58 cast(cpu AS int) AS cpu, 59 cast(freq_khz AS int) AS freq_khz, 60 cast(idle_value AS int) AS idle_value, 61 cast(sum(dur) AS int) AS dur_ns 62FROM 63 sched_freq_idle_windowed 64WHERE 65 freq_khz > 0 66GROUP BY 67 cpu, 68 freq_khz, 69 idle_value; 70