1-- 2-- Copyright 2021 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 16DROP VIEW IF EXISTS freq_slice; 17 18CREATE PERFETTO VIEW freq_slice AS 19SELECT 20 counter.track_id AS track_id, 21 track.name AS freq_name, 22 ts, 23 value AS freq_value, 24 LEAD(ts, 1, trace_end()) OVER (PARTITION BY track.id ORDER BY ts) - ts AS duration 25FROM counter 26LEFT JOIN track ON counter.track_id = track.id 27WHERE track.name GLOB "* Frequency" 28ORDER BY ts; 29 30DROP VIEW IF EXISTS freq_total_duration; 31 32CREATE PERFETTO VIEW freq_total_duration AS 33SELECT 34 track_id, 35 freq_name, 36 SUM(duration) AS total_duration 37FROM freq_slice 38WHERE duration > 0 39GROUP BY track_id, freq_name; 40 41DROP VIEW IF EXISTS dvfs_per_band_view; 42 43CREATE PERFETTO VIEW dvfs_per_band_view AS 44WITH 45freq_duration AS ( 46 SELECT 47 track_id, 48 freq_name, 49 CAST(freq_value AS int) AS freq_value, 50 SUM(duration) AS duration_ns 51 FROM freq_slice 52 WHERE duration > 0 53 GROUP BY track_id, freq_name, freq_value 54) 55SELECT 56 freq_duration.track_id, 57 freq_duration.freq_name, 58 AndroidDvfsMetric_BandStat( 59 'freq_value', freq_value, 60 'percentage', duration_ns / (total_duration / 1e2), 61 'duration_ns', duration_ns 62 ) AS proto 63FROM freq_duration 64LEFT JOIN freq_total_duration 65 USING(track_id) 66ORDER BY freq_duration.freq_name, freq_duration.freq_value; 67 68DROP VIEW IF EXISTS dvfs_per_freq_view; 69CREATE PERFETTO VIEW dvfs_per_freq_view AS 70SELECT 71 AndroidDvfsMetric_FrequencyResidency( 72 'freq_name', freq_total_duration.freq_name, 73 'band_stat', ( 74 SELECT 75 RepeatedField(proto) 76 FROM dvfs_per_band_view 77 WHERE dvfs_per_band_view.track_id = freq_total_duration.track_id 78 ) 79 ) AS proto 80FROM freq_total_duration 81GROUP BY track_id, freq_name 82ORDER BY freq_name; 83 84DROP VIEW IF EXISTS android_dvfs_output; 85CREATE PERFETTO VIEW android_dvfs_output AS 86SELECT AndroidDVFSMetric( 87 'freq_residencies', ( 88 SELECT 89 RepeatedField(proto) 90 FROM dvfs_per_freq_view 91 ) 92 ); 93