1-- 2-- Copyright 2019 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-- 16 17-- Create all the views used to generate the Android Cpu metrics proto. 18SELECT RUN_METRIC('android/android_cpu_agg.sql'); 19SELECT RUN_METRIC('android/android_cpu_raw_metrics_per_core.sql', 20 'input_table', 'cpu_freq_sched_per_thread', 21 'output_table', 'raw_metrics_per_core'); 22SELECT RUN_METRIC('android/process_metadata.sql'); 23 24DROP VIEW IF EXISTS metrics_per_core_type; 25CREATE PERFETTO VIEW metrics_per_core_type AS 26SELECT 27 utid, 28 core_type, 29 AndroidCpuMetric_Metrics( 30 'mcycles', SUM(mcycles), 31 'runtime_ns', SUM(runtime_ns), 32 'min_freq_khz', MIN(min_freq_khz), 33 'max_freq_khz', MAX(max_freq_khz), 34 -- In total here, we need to divide the denominator by 1e9 (to convert 35 -- ns to s) and divide the numerator by 1e6 (to convert millicycles to 36 -- kcycles). In total, this means we need to multiply the expression as 37 -- a whole by 1e3. 38 'avg_freq_khz', CAST((SUM(millicycles) / SUM(runtime_ns)) * 1000 AS INT) 39 ) AS proto 40FROM raw_metrics_per_core 41GROUP BY utid, core_type; 42 43-- Aggregate everything per thread. 44DROP VIEW IF EXISTS core_proto_per_thread; 45CREATE PERFETTO VIEW core_proto_per_thread AS 46SELECT 47 utid, 48 RepeatedField( 49 AndroidCpuMetric_CoreData( 50 'id', cpu, 51 'metrics', AndroidCpuMetric_Metrics( 52 'mcycles', mcycles, 53 'runtime_ns', runtime_ns, 54 'min_freq_khz', min_freq_khz, 55 'max_freq_khz', max_freq_khz, 56 'avg_freq_khz', avg_freq_khz 57 ) 58 ) 59 ) AS proto 60FROM raw_metrics_per_core 61GROUP BY utid; 62 63DROP VIEW IF EXISTS core_type_proto_per_thread; 64CREATE PERFETTO VIEW core_type_proto_per_thread AS 65SELECT 66 utid, 67 RepeatedField( 68 AndroidCpuMetric_CoreTypeData( 69 'type', core_type, 70 'metrics', metrics_per_core_type.proto 71 ) 72 ) AS proto 73FROM metrics_per_core_type 74GROUP BY utid; 75 76DROP VIEW IF EXISTS metrics_proto_per_thread; 77CREATE PERFETTO VIEW metrics_proto_per_thread AS 78SELECT 79 utid, 80 AndroidCpuMetric_Metrics( 81 'mcycles', SUM(mcycles), 82 'runtime_ns', SUM(runtime_ns), 83 'min_freq_khz', MIN(min_freq_khz), 84 'max_freq_khz', MAX(max_freq_khz), 85 -- See above for a breakdown of the maths used to compute the 86 -- multiplicative factor. 87 'avg_freq_khz', CAST((SUM(millicycles) / SUM(runtime_ns)) * 1000 AS INT) 88 ) AS proto 89FROM raw_metrics_per_core 90GROUP BY utid; 91 92-- Aggregate everything per perocess 93DROP VIEW IF EXISTS thread_proto_per_process; 94CREATE PERFETTO VIEW thread_proto_per_process AS 95SELECT 96 upid, 97 RepeatedField( 98 AndroidCpuMetric_Thread( 99 'name', thread.name, 100 'metrics', metrics_proto_per_thread.proto, 101 'core', core_proto_per_thread.proto, 102 'core_type', core_type_proto_per_thread.proto 103 ) 104 ) AS proto 105FROM thread 106LEFT JOIN core_proto_per_thread USING (utid) 107LEFT JOIN core_type_proto_per_thread USING (utid) 108LEFT JOIN metrics_proto_per_thread USING(utid) 109GROUP BY upid; 110 111DROP VIEW IF EXISTS core_metrics_per_process; 112CREATE PERFETTO VIEW core_metrics_per_process AS 113SELECT 114 upid, 115 cpu, 116 AndroidCpuMetric_Metrics( 117 'mcycles', SUM(mcycles), 118 'runtime_ns', SUM(runtime_ns), 119 'min_freq_khz', MIN(min_freq_khz), 120 'max_freq_khz', MAX(max_freq_khz), 121 -- In total here, we need to divide the denominator by 1e9 (to convert 122 -- ns to s) and divide the numerator by 1e6 (to convert millicycles to 123 -- kcycles). In total, this means we need to multiply the expression as 124 -- a whole by 1e3. 125 'avg_freq_khz', CAST((SUM(millicycles) / SUM(runtime_ns)) * 1000 AS INT) 126 ) AS proto 127FROM raw_metrics_per_core 128JOIN thread USING (utid) 129GROUP BY upid, cpu; 130 131DROP VIEW IF EXISTS core_proto_per_process; 132CREATE PERFETTO VIEW core_proto_per_process AS 133SELECT 134 upid, 135 RepeatedField( 136 AndroidCpuMetric_CoreData( 137 'id', cpu, 138 'metrics', core_metrics_per_process.proto 139 ) 140 ) AS proto 141FROM core_metrics_per_process 142GROUP BY upid; 143 144DROP VIEW IF EXISTS core_type_metrics_per_process; 145CREATE PERFETTO VIEW core_type_metrics_per_process AS 146SELECT 147 upid, 148 core_type, 149 AndroidCpuMetric_Metrics( 150 'mcycles', SUM(mcycles), 151 'runtime_ns', SUM(runtime_ns), 152 'min_freq_khz', MIN(min_freq_khz), 153 'max_freq_khz', MAX(max_freq_khz), 154 -- In total here, we need to divide the denominator by 1e9 (to convert 155 -- ns to s) and divide the numerator by 1e6 (to convert millicycles to 156 -- kcycles). In total, this means we need to multiply the expression as 157 -- a whole by 1e3. 158 'avg_freq_khz', CAST((SUM(millicycles) / SUM(runtime_ns)) * 1000 AS INT) 159 ) AS proto 160FROM raw_metrics_per_core 161JOIN thread USING (utid) 162GROUP BY upid, core_type; 163 164DROP VIEW IF EXISTS core_type_proto_per_process; 165CREATE PERFETTO VIEW core_type_proto_per_process AS 166SELECT 167 upid, 168 RepeatedField( 169 AndroidCpuMetric_CoreTypeData( 170 'type', core_type, 171 'metrics', core_type_metrics_per_process.proto 172 ) 173 ) AS proto 174FROM core_type_metrics_per_process 175GROUP BY upid; 176 177DROP VIEW IF EXISTS metrics_proto_per_process; 178CREATE PERFETTO VIEW metrics_proto_per_process AS 179SELECT 180 upid, 181 AndroidCpuMetric_Metrics( 182 'mcycles', SUM(mcycles), 183 'runtime_ns', SUM(runtime_ns), 184 'min_freq_khz', MIN(min_freq_khz), 185 'max_freq_khz', MAX(max_freq_khz), 186 -- See above for a breakdown of the maths used to compute the 187 -- multiplicative factor. 188 'avg_freq_khz', CAST((SUM(millicycles) / SUM(runtime_ns)) * 1000 AS INT) 189 ) AS proto 190FROM raw_metrics_per_core 191JOIN thread USING (utid) 192GROUP BY upid; 193 194DROP VIEW IF EXISTS android_cpu_output; 195CREATE PERFETTO VIEW android_cpu_output AS 196SELECT AndroidCpuMetric( 197 'process_info', ( 198 SELECT RepeatedField( 199 AndroidCpuMetric_Process( 200 'name', process.name, 201 'process', process_metadata.metadata, 202 'metrics', metrics_proto_per_process.proto, 203 'threads', thread_proto_per_process.proto, 204 'core', core_proto_per_process.proto, 205 'core_type', core_type_proto_per_process.proto 206 ) 207 ) 208 FROM process 209 JOIN metrics_proto_per_process USING(upid) 210 JOIN thread_proto_per_process USING (upid) 211 JOIN core_proto_per_process USING (upid) 212 JOIN core_type_proto_per_process USING (upid) 213 JOIN process_metadata USING (upid) 214 ) 215); 216