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 cpu.utilization.general; 17INCLUDE PERFETTO MODULE time.conversion; 18 19-- Returns a table of thread utilization per given period. 20-- Utilization is calculated as sum of average utilization of each CPU in each 21-- period, which is defined as a multiply of |interval|. For this reason 22-- first and last period might have lower then real utilization. 23CREATE PERFETTO FUNCTION cpu_thread_utilization_per_period( 24 -- Length of the period on which utilization should be averaged. 25 interval INT, 26 -- Utid of the thread. 27 utid INT 28) 29RETURNS TABLE( 30 -- Timestamp of start of a second. 31 ts INT, 32 -- Sum of average utilization over period. 33 -- Note: as the data is normalized, the values will be in the 34 -- [0, 1] range. 35 utilization DOUBLE, 36 -- Sum of average utilization over all CPUs over period. 37 -- Note: as the data is unnormalized, the values will be in the 38 -- [0, cpu_count] range. 39 unnormalized_utilization DOUBLE 40) AS 41WITH sched_for_utid AS ( 42 SELECT 43 ts, 44 ts_end, 45 utid 46 FROM sched 47 WHERE utid = $utid 48) SELECT * FROM _cpu_avg_utilization_per_period!($interval, sched_for_utid); 49 50-- Returns a table of thread utilization per second. 51-- Utilization is calculated as sum of average utilization of each CPU in each 52-- period, which is defined as a multiply of |interval|. For this reason 53-- first and last period might have lower then real utilization. 54CREATE PERFETTO FUNCTION cpu_thread_utilization_per_second( 55 -- Utid of the thread. 56 utid INT 57) 58RETURNS TABLE ( 59 -- Timestamp of start of a second. 60 ts INT, 61 -- Sum of average utilization over period. 62 -- Note: as the data is normalized, the values will be in the 63 -- [0, 1] range. 64 utilization DOUBLE, 65 -- Sum of average utilization over all CPUs over period. 66 -- Note: as the data is unnormalized, the values will be in the 67 -- [0, cpu_count] range. 68 unnormalized_utilization DOUBLE 69) AS 70SELECT * FROM cpu_thread_utilization_per_period(time_from_s(1), $utid); 71 72-- Aggregated CPU statistics for each thread. 73CREATE PERFETTO TABLE cpu_cycles_per_thread( 74 -- Unique thread id 75 utid INT, 76 -- Sum of CPU millicycles 77 millicycles INT, 78 -- Sum of CPU megacycles 79 megacycles INT, 80 -- Total runtime duration 81 runtime INT, 82 -- Minimum CPU frequency in kHz 83 min_freq INT, 84 -- Maximum CPU frequency in kHz 85 max_freq INT, 86 -- Average CPU frequency in kHz 87 avg_freq INT 88) AS 89SELECT 90 utid, 91 cast_int!(SUM(dur * freq) / 1000) AS millicycles, 92 cast_int!(SUM(dur * freq) / 1000 / 1e9) AS megacycles, 93 SUM(dur) AS runtime, 94 MIN(freq) AS min_freq, 95 MAX(freq) AS max_freq, 96 cast_int!(SUM((dur * freq) / 1000) / SUM(dur / 1000)) AS avg_freq 97FROM _cpu_freq_per_thread 98GROUP BY utid;