• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18SELECT
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
23FROM counter c
24LEFT JOIN counter_track t
25  ON c.track_id = t.id
26WHERE name GLOB "* Received KB"
27ORDER BY ts DESC;
28
29DROP VIEW IF EXISTS gro_rx_packet_count;
30CREATE VIEW gro_rx_packet_count AS
31SELECT
32  s.name AS dev,
33  COUNT(1) AS cnt
34FROM slice s
35LEFT JOIN track t
36  ON s.track_id = t.id
37WHERE t.name GLOB "Napi Gro Cpu *"
38GROUP BY s.name;
39
40DROP VIEW IF EXISTS tx_packets;
41CREATE VIEW tx_packets AS
42SELECT
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
47FROM counter c
48LEFT JOIN counter_track t
49  ON c.track_id = t.id
50WHERE name GLOB "* Transmitted KB"
51ORDER BY ts DESC;
52
53DROP VIEW IF EXISTS net_devices;
54CREATE VIEW net_devices AS
55SELECT DISTINCT dev
56FROM tx_packets
57UNION
58SELECT DISTINCT dev
59FROM rx_packets;
60
61DROP VIEW IF EXISTS tcp_retransmitted_count;
62CREATE VIEW tcp_retransmitted_count AS
63SELECT
64  COUNT(1) AS cnt
65FROM slice s
66LEFT JOIN track t
67  ON s.track_id = t.id
68WHERE
69  t.name = "TCP Retransmit Skb";
70
71DROP VIEW IF EXISTS kfree_skb_count;
72CREATE VIEW kfree_skb_count AS
73SELECT
74  MAX(value) AS cnt
75FROM counter c
76LEFT JOIN track t
77  ON c.track_id = t.id
78WHERE
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
83SELECT
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
96FROM rx_packets
97GROUP BY dev, cpu;
98
99DROP VIEW IF EXISTS device_per_core_egress_traffic;
100CREATE VIEW device_per_core_egress_traffic AS
101SELECT
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
114FROM tx_packets
115GROUP BY dev, cpu;
116
117DROP VIEW IF EXISTS device_total_ingress_traffic;
118CREATE VIEW device_total_ingress_traffic AS
119SELECT
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
126FROM rx_packets
127GROUP BY dev;
128
129DROP VIEW IF EXISTS device_total_egress_traffic;
130CREATE VIEW device_total_egress_traffic AS
131SELECT
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
138FROM tx_packets
139GROUP BY dev;
140
141DROP VIEW IF EXISTS device_traffic_statistic;
142CREATE VIEW device_traffic_statistic AS
143SELECT
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
198FROM net_devices
199ORDER BY dev;
200
201DROP VIEW IF EXISTS net_rx_actions;
202CREATE VIEW net_rx_actions AS
203SELECT
204  s.ts,
205  s.dur,
206  CAST(SUBSTR(t.name, 13, 1) AS int) AS cpu
207FROM slice s
208LEFT JOIN track t
209  ON s.track_id = t.id
210WHERE s.name = "NET_RX";
211
212DROP VIEW IF EXISTS net_tx_actions;
213CREATE VIEW net_tx_actions AS
214SELECT
215  s.ts,
216  s.dur,
217  CAST(SUBSTR(t.name, 13, 1) AS int) AS cpu
218FROM slice s
219LEFT JOIN track t
220  ON s.track_id = t.id
221WHERE s.name = "NET_TX";
222
223DROP VIEW IF EXISTS ipi_actions;
224CREATE VIEW ipi_actions AS
225SELECT
226  s.ts,
227  s.dur,
228  CAST(SUBSTR(t.name, 13, 1) AS int) AS cpu
229FROM slice s
230LEFT JOIN track t
231  ON s.track_id = t.id
232WHERE s.name = "IRQ (IPI)";
233
234DROP VIEW IF EXISTS cpu_freq_view;
235CREATE VIEW cpu_freq_view AS
236SELECT
237  cpu,
238  ts,
239  LEAD(ts, 1, (SELECT end_ts FROM trace_bounds))
240  OVER (PARTITION BY cpu ORDER BY ts) - ts AS dur,
241  CAST(value AS INT) AS freq_khz
242FROM counter
243JOIN cpu_counter_track ON counter.track_id = cpu_counter_track.id
244WHERE name = 'cpufreq';
245
246DROP TABLE IF EXISTS cpu_freq_net_rx_action_per_core;
247CREATE VIRTUAL TABLE cpu_freq_net_rx_action_per_core
248USING SPAN_LEFT_JOIN(net_rx_actions PARTITIONED cpu, cpu_freq_view PARTITIONED cpu);
249
250DROP TABLE IF EXISTS cpu_freq_net_tx_action_per_core;
251CREATE VIRTUAL TABLE cpu_freq_net_tx_action_per_core
252USING SPAN_LEFT_JOIN(net_tx_actions PARTITIONED cpu, cpu_freq_view PARTITIONED cpu);
253
254DROP VIEW IF EXISTS total_net_rx_action_statistic;
255CREATE VIEW total_net_rx_action_statistic AS
256SELECT
257  COUNT(1) AS times,
258  SUM(dur) AS runtime,
259  AVG(dur) AS avg_runtime,
260  (SELECT COUNT(1) FROM rx_packets) AS total_packet
261FROM net_rx_actions;
262
263DROP VIEW IF EXISTS total_net_tx_action_statistic;
264CREATE VIEW total_net_tx_action_statistic AS
265SELECT
266  COUNT(1) AS times,
267  SUM(dur) AS runtime,
268  AVG(dur) AS avg_runtime
269FROM net_tx_actions;
270
271DROP VIEW IF EXISTS total_ipi_action_statistic;
272CREATE VIEW total_ipi_action_statistic AS
273SELECT
274  COUNT(1) AS times,
275  SUM(dur) AS runtime,
276  AVG(dur) AS avg_runtime
277FROM ipi_actions;
278
279DROP VIEW IF EXISTS activated_cores_net_rx;
280CREATE VIEW activated_cores_net_rx AS
281SELECT DISTINCT
282  cpu
283FROM net_rx_actions;
284
285DROP VIEW IF EXISTS activated_cores_net_tx;
286CREATE VIEW activated_cores_net_tx AS
287SELECT DISTINCT
288  cpu
289FROM net_tx_actions;
290
291DROP VIEW IF EXISTS per_core_net_rx_action_statistic;
292CREATE VIEW per_core_net_rx_action_statistic AS
293SELECT
294  AndroidNetworkMetric_CoreNetRxActionStatistic(
295    'id', cpu,
296    'net_rx_action_statistic', AndroidNetworkMetric_NetRxActionStatistic(
297      'count', (SELECT COUNT(1) FROM net_rx_actions AS na WHERE na.cpu = ac.cpu),
298      'runtime_ms', (SELECT SUM(dur) / 1e6 FROM net_rx_actions AS na WHERE na.cpu = ac.cpu),
299      'avg_runtime_ms', (SELECT AVG(dur) / 1e6 FROM net_rx_actions AS na WHERE na.cpu = ac.cpu),
300      '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),
301      '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)
302    )
303  ) AS proto
304FROM activated_cores_net_rx AS ac;
305
306DROP VIEW IF EXISTS per_core_net_tx_action_statistic;
307CREATE VIEW per_core_net_tx_action_statistic AS
308SELECT
309  AndroidNetworkMetric_CoreNetTxActionStatistic(
310    'id', cpu,
311    'net_tx_action_statistic', AndroidNetworkMetric_NetTxActionStatistic(
312      'count', (SELECT COUNT(1) FROM net_tx_actions AS na WHERE na.cpu = ac.cpu),
313      'runtime_ms', (SELECT SUM(dur) / 1e6 FROM net_tx_actions AS na WHERE na.cpu = ac.cpu),
314      'avg_runtime_ms', (SELECT AVG(dur) / 1e6 FROM net_tx_actions AS na WHERE na.cpu = ac.cpu),
315      'avg_freq_khz', (SELECT SUM(dur * freq_khz) / SUM(dur) FROM cpu_freq_net_tx_action_per_core AS cc WHERE cc.cpu = ac.cpu),
316      'mcycles', (SELECT CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) FROM cpu_freq_net_tx_action_per_core AS cc WHERE cc.cpu = ac.cpu)
317    )
318  ) AS proto
319FROM activated_cores_net_tx AS ac;
320
321DROP VIEW IF EXISTS android_netperf_output;
322CREATE VIEW android_netperf_output AS
323SELECT AndroidNetworkMetric(
324    'net_devices', (
325      SELECT
326        RepeatedField(proto)
327      FROM device_traffic_statistic
328    ),
329    'net_rx_action', AndroidNetworkMetric_NetRxAction(
330      'total', AndroidNetworkMetric_NetRxActionStatistic(
331        'count', (SELECT times FROM total_net_rx_action_statistic),
332        'runtime_ms', (SELECT runtime / 1e6 FROM total_net_rx_action_statistic),
333        'avg_runtime_ms', (SELECT avg_runtime / 1e6 FROM total_net_rx_action_statistic),
334        'avg_freq_khz', (SELECT SUM(dur * freq_khz) / SUM(dur) FROM cpu_freq_net_rx_action_per_core),
335        'mcycles', (SELECT CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) FROM cpu_freq_net_rx_action_per_core)
336      ),
337      'core', (
338        SELECT
339          RepeatedField(proto)
340        FROM per_core_net_rx_action_statistic
341      ),
342      'avg_interstack_latency_ms', (
343        SELECT
344          runtime / total_packet / 1e6
345        FROM total_net_rx_action_statistic
346      )
347    ),
348    'retransmission_rate', (
349      SELECT
350        (SELECT cnt FROM tcp_retransmitted_count) * 100.0 / COUNT(1)
351      FROM tx_packets
352    ),
353    'kfree_skb_rate', (
354      SELECT
355        cnt * 100.0 / ((SELECT count(1) FROM rx_packets) + (SELECT count(1) FROM tx_packets))
356      FROM kfree_skb_count
357    ),
358    'net_tx_action', AndroidNetworkMetric_NetTxAction(
359      'total', AndroidNetworkMetric_NetTxActionStatistic(
360        'count', (SELECT times FROM total_net_tx_action_statistic),
361        'runtime_ms', (SELECT runtime / 1e6 FROM total_net_tx_action_statistic),
362        'avg_runtime_ms', (SELECT avg_runtime / 1e6 FROM total_net_tx_action_statistic),
363        'avg_freq_khz', (SELECT SUM(dur * freq_khz) / SUM(dur) FROM cpu_freq_net_tx_action_per_core),
364        'mcycles', (SELECT CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) FROM cpu_freq_net_tx_action_per_core)
365      ),
366      'core', (
367        SELECT
368          RepeatedField(proto)
369        FROM per_core_net_tx_action_statistic
370      )
371    ),
372    'ipi_action', AndroidNetworkMetric_IpiAction(
373      'total', AndroidNetworkMetric_IpiActionStatistic(
374        'count', (SELECT times FROM total_ipi_action_statistic),
375        'runtime_ms', (SELECT runtime / 1e6 FROM total_ipi_action_statistic),
376        'avg_runtime_ms', (SELECT avg_runtime / 1e6 FROM total_ipi_action_statistic)
377      )
378    )
379  );
380