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