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 process 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_process_utilization_per_period( 24 -- Length of the period on which utilization should be averaged. 25 interval INT, 26 -- Upid of the process. 27 upid 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_upid AS ( 42 SELECT 43 ts, 44 ts_end, 45 utid 46 FROM sched 47 JOIN thread USING (utid) 48 JOIN process USING (upid) 49 WHERE upid = $upid AND utid != 0) 50SELECT * FROM _cpu_avg_utilization_per_period!($interval, sched_for_upid); 51 52-- Returns a table of process utilization per second. 53-- Utilization is calculated as sum of average utilization of each CPU in each 54-- period, which is defined as a multiply of |interval|. For this reason 55-- first and last period might have lower then real utilization. 56CREATE PERFETTO FUNCTION cpu_process_utilization_per_second( 57 -- Upid of the process. 58 upid INT 59) 60RETURNS TABLE ( 61 -- Timestamp of start of a second. 62 ts INT, 63 -- Sum of average utilization over period. 64 -- Note: as the data is normalized, the values will be in the 65 -- [0, 1] range. 66 utilization DOUBLE, 67 -- Sum of average utilization over all CPUs over period. 68 -- Note: as the data is unnormalized, the values will be in the 69 -- [0, cpu_count] range. 70 unnormalized_utilization DOUBLE 71) AS 72SELECT * FROM cpu_process_utilization_per_period(time_from_s(1), $upid); 73 74-- Aggregated CPU statistics for each process. 75CREATE PERFETTO TABLE cpu_cycles_per_process( 76 -- Unique process id 77 upid INT, 78 -- Sum of CPU millicycles 79 millicycles INT, 80 -- Sum of CPU megacycles 81 megacycles INT, 82 -- Total runtime duration 83 runtime INT, 84 -- Minimum CPU frequency in kHz 85 min_freq INT, 86 -- Maximum CPU frequency in kHz 87 max_freq INT, 88 -- Average CPU frequency in kHz 89 avg_freq INT 90) AS 91WITH threads AS ( 92 SELECT upid, utid FROM thread 93) 94SELECT 95 upid, 96 cast_int!(SUM(dur * freq) / 1000) AS millicycles, 97 cast_int!(SUM(dur * freq) / 1000 / 1e9) AS megacycles, 98 SUM(dur) AS runtime, 99 MIN(freq) AS min_freq, 100 MAX(freq) AS max_freq, 101 cast_int!(SUM((dur * freq) / 1000) / SUM(dur / 1000)) AS avg_freq 102FROM _cpu_freq_per_thread 103JOIN threads USING (utid) 104GROUP BY upid;