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 rx_packets; 17CREATE VIEW rx_packets AS 18 SELECT 19 ts, 20 REPLACE(name, " Received KB", "") AS dev, 21 EXTRACT_ARG(arg_set_id, 'cpu') AS cpu, 22 EXTRACT_ARG(arg_set_id, 'len') AS len 23 FROM counter c 24 LEFT JOIN counter_track t 25 ON c.track_id = t.id 26 WHERE name GLOB "* Received KB" 27 ORDER BY ts DESC; 28 29DROP VIEW IF EXISTS gro_rx_packet_count; 30CREATE VIEW gro_rx_packet_count AS 31 SELECT 32 s.name AS dev, 33 COUNT(1) AS cnt 34 FROM slice s 35 LEFT JOIN track t 36 ON s.track_id = t.id 37 WHERE t.name GLOB "Napi Gro Cpu *" 38 GROUP BY s.name; 39 40DROP VIEW IF EXISTS tx_packets; 41CREATE VIEW tx_packets AS 42 SELECT 43 ts, 44 REPLACE(name, " Transmitted KB", "") AS dev, 45 EXTRACT_ARG(arg_set_id, 'cpu') AS cpu, 46 EXTRACT_ARG(arg_set_id, 'len') AS len 47 FROM counter c 48 LEFT JOIN counter_track t 49 ON c.track_id = t.id 50 WHERE name GLOB "* Transmitted KB" 51 ORDER BY ts DESC; 52 53DROP VIEW IF EXISTS net_devices; 54CREATE VIEW net_devices AS 55 SELECT DISTINCT dev 56 FROM tx_packets 57 UNION 58 SELECT DISTINCT dev 59 FROM rx_packets; 60 61DROP VIEW IF EXISTS tcp_retransmitted_count; 62CREATE VIEW tcp_retransmitted_count AS 63 SELECT 64 COUNT(1) AS cnt 65 FROM slice s 66 LEFT JOIN track t 67 ON s.track_id = t.id 68 WHERE 69 t.name = "TCP Retransmit Skb"; 70 71DROP VIEW IF EXISTS kfree_skb_count; 72CREATE VIEW kfree_skb_count AS 73 SELECT 74 MAX(value) AS cnt 75 FROM counter c 76 LEFT JOIN track t 77 ON c.track_id = t.id 78 WHERE 79 t.name = "Kfree Skb IP Prot"; 80 81DROP VIEW IF EXISTS device_per_core_ingress_traffic; 82CREATE VIEW device_per_core_ingress_traffic AS 83 SELECT 84 dev, 85 AndroidNetworkMetric_CorePacketStatistic( 86 'id', cpu, 87 'packet_statistic', AndroidNetworkMetric_PacketStatistic( 88 'packets', COUNT(1), 89 'bytes', SUM(len), 90 'first_packet_timestamp_ns', MIN(ts), 91 'last_packet_timestamp_ns', MAX(ts), 92 'interval_ns', IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000), 93 'data_rate_kbps', (SUM(len)*8)/(IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000)/1e9)/1024 94 ) 95 ) AS proto 96 FROM rx_packets 97 GROUP BY dev, cpu; 98 99DROP VIEW IF EXISTS device_per_core_egress_traffic; 100CREATE VIEW device_per_core_egress_traffic AS 101 SELECT 102 dev, 103 AndroidNetworkMetric_CorePacketStatistic( 104 'id', cpu, 105 'packet_statistic', AndroidNetworkMetric_PacketStatistic( 106 'packets', COUNT(1), 107 'bytes', SUM(len), 108 'first_packet_timestamp_ns', MIN(ts), 109 'last_packet_timestamp_ns', MAX(ts), 110 'interval_ns', IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000), 111 'data_rate_kbps', (SUM(len)*8)/(IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000)/1e9)/1024 112 ) 113 ) AS proto 114 FROM tx_packets 115 GROUP BY dev, cpu; 116 117DROP VIEW IF EXISTS device_total_ingress_traffic; 118CREATE VIEW device_total_ingress_traffic AS 119 SELECT 120 dev, 121 MIN(ts) AS start_ts, 122 MAX(ts) AS end_ts, 123 IIF((MAX(ts) - MIN(ts)) > 10000000, MAX(ts)-MIN(ts), 10000000) AS interval, 124 COUNT(1) AS packets, 125 SUM(len) AS bytes 126 FROM rx_packets 127 GROUP BY dev; 128 129DROP VIEW IF EXISTS device_total_egress_traffic; 130CREATE VIEW device_total_egress_traffic AS 131 SELECT 132 dev, 133 MIN(ts) AS start_ts, 134 MAX(ts) AS end_ts, 135 IIF((MAX(ts) - MIN(ts)) > 10000000, MAX(ts)-MIN(ts), 10000000) AS interval, 136 COUNT(1) AS packets, 137 SUM(len) AS bytes 138 FROM tx_packets 139 GROUP BY dev; 140 141DROP VIEW IF EXISTS device_traffic_statistic; 142CREATE VIEW device_traffic_statistic AS 143 SELECT 144 AndroidNetworkMetric_NetDevice( 145 'name', net_devices.dev, 146 'rx', ( 147 SELECT 148 AndroidNetworkMetric_Rx( 149 'total', AndroidNetworkMetric_PacketStatistic( 150 'packets', packets, 151 'bytes', bytes, 152 'first_packet_timestamp_ns', start_ts, 153 'last_packet_timestamp_ns', end_ts, 154 'interval_ns', interval, 155 'data_rate_kbps', (bytes*8)/(interval/1e9)/1024 156 ), 157 'core', ( 158 SELECT 159 RepeatedField(proto) 160 FROM device_per_core_ingress_traffic 161 WHERE device_per_core_ingress_traffic.dev = device_total_ingress_traffic.dev 162 ), 163 'gro_aggregation_ratio', ( 164 SELECT 165 CASE 166 WHEN packets > 0 THEN '1:' || CAST( (cnt*1.0/packets) AS text) 167 ELSE '0:' || cnt 168 END 169 FROM gro_rx_packet_count 170 WHERE gro_rx_packet_count.dev = net_devices.dev 171 ) 172 ) 173 FROM device_total_ingress_traffic 174 WHERE device_total_ingress_traffic.dev = net_devices.dev 175 ), 176 'tx', ( 177 SELECT 178 AndroidNetworkMetric_Tx( 179 'total', AndroidNetworkMetric_PacketStatistic( 180 'packets', packets, 181 'bytes', bytes, 182 'first_packet_timestamp_ns', start_ts, 183 'last_packet_timestamp_ns', end_ts, 184 'interval_ns', interval, 185 'data_rate_kbps', (bytes*8)/(interval/1e9)/1024 186 ), 187 'core', ( 188 SELECT 189 RepeatedField(proto) 190 FROM device_per_core_egress_traffic 191 WHERE device_per_core_egress_traffic.dev = device_total_egress_traffic.dev 192 ) 193 ) 194 FROM device_total_egress_traffic 195 WHERE device_total_egress_traffic.dev = net_devices.dev 196 ) 197 ) AS proto 198 FROM net_devices 199 ORDER BY dev; 200 201DROP VIEW IF EXISTS net_rx_actions; 202CREATE VIEW net_rx_actions AS 203 SELECT 204 s.ts, 205 s.dur, 206 CAST(SUBSTR(t.name, 13, 1) AS int) AS cpu 207 FROM slice s 208 LEFT JOIN track t 209 ON s.track_id = t.id 210 WHERE s.name = "NET_RX"; 211 212DROP VIEW IF EXISTS cpu_freq_view; 213CREATE VIEW cpu_freq_view AS 214SELECT 215 cpu, 216 ts, 217 LEAD(ts, 1, (SELECT end_ts from trace_bounds)) 218 OVER (PARTITION by cpu ORDER BY ts) - ts AS dur, 219 CAST(value AS INT) as freq_khz 220FROM counter 221JOIN cpu_counter_track on counter.track_id = cpu_counter_track.id 222WHERE name = 'cpufreq'; 223 224DROP TABLE IF EXISTS cpu_freq_net_rx_action_per_core; 225CREATE VIRTUAL TABLE cpu_freq_net_rx_action_per_core 226USING SPAN_LEFT_JOIN(net_rx_actions PARTITIONED cpu, cpu_freq_view PARTITIONED cpu); 227 228DROP VIEW IF EXISTS total_net_rx_action_statistic; 229CREATE VIEW total_net_rx_action_statistic AS 230 SELECT 231 COUNT(1) AS times, 232 SUM(dur) AS runtime, 233 AVG(dur) AS avg_runtime, 234 (SELECT COUNT(1) FROM rx_packets) AS total_packet 235 FROM net_rx_actions; 236 237DROP VIEW IF EXISTS activated_cores; 238CREATE VIEW activated_cores AS 239 SELECT 240 DISTINCT cpu 241 FROM net_rx_actions; 242 243DROP VIEW IF EXISTS per_core_net_rx_action_statistic; 244CREATE VIEW per_core_net_rx_action_statistic AS 245 SELECT 246 AndroidNetworkMetric_CoreNetRxActionStatistic( 247 'id', cpu, 248 'net_rx_action_statistic', AndroidNetworkMetric_NetRxActionStatistic( 249 'count', (SELECT COUNT(1) FROM net_rx_actions AS na WHERE na.cpu = ac.cpu), 250 'runtime_ms', (SELECT SUM(dur)/1e6 FROM net_rx_actions AS na WHERE na.cpu = ac.cpu), 251 'avg_runtime_ms', (SELECT AVG(dur)/1e6 FROM net_rx_actions AS na WHERE na.cpu = ac.cpu), 252 'avg_freq_khz', (SELECT SUM(dur * freq_khz) / SUM(dur) FROM cpu_freq_net_rx_action_per_core AS cc WHERE cc.cpu = ac.cpu), 253 'mcycles', (SELECT CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) FROM cpu_freq_net_rx_action_per_core AS cc WHERE cc.cpu = ac.cpu) 254 ) 255 ) AS proto 256 FROM activated_cores AS ac; 257 258DROP VIEW IF EXISTS android_netperf_output; 259CREATE VIEW android_netperf_output AS 260 SELECT AndroidNetworkMetric( 261 'net_devices', ( 262 SELECT 263 RepeatedField(proto) 264 FROM device_traffic_statistic 265 ), 266 'net_rx_action', AndroidNetworkMetric_NetRxAction( 267 'total', AndroidNetworkMetric_NetRxActionStatistic( 268 'count', (SELECT times FROM total_net_rx_action_statistic), 269 'runtime_ms', (SELECT runtime/1e6 FROM total_net_rx_action_statistic), 270 'avg_runtime_ms', (SELECT avg_runtime/1e6 FROM total_net_rx_action_statistic), 271 'avg_freq_khz', (SELECT SUM(dur * freq_khz) / SUM(dur) FROM cpu_freq_net_rx_action_per_core), 272 'mcycles', (SELECT CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) FROM cpu_freq_net_rx_action_per_core) 273 ), 274 'core', ( 275 SELECT 276 RepeatedField(proto) 277 FROM per_core_net_rx_action_statistic 278 ), 279 'avg_interstack_latency_ms', ( 280 SELECT 281 runtime/total_packet/1e6 282 FROM total_net_rx_action_statistic 283 ) 284 ), 285 'retransmission_rate', ( 286 SELECT 287 (SELECT cnt FROM tcp_retransmitted_count) * 100.0 / COUNT(1) 288 FROM tx_packets 289 ), 290 'kfree_skb_rate', ( 291 SELECT 292 cnt * 100.0 / ((SELECT count(1) FROM rx_packets) + (SELECT count(1) FROM tx_packets)) 293 FROM kfree_skb_count 294 ) 295 ); 296 297