1-- 2-- Copyright 2023 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 android.network_packets; 17 18-- Creates a view of aggregated network activity. It is common among networking 19-- to have the interface active for some time after network use. For example, in 20-- mobile networking, it is common to have the cellular interface active for 10 21-- or more seconds after the last packet was sent or received. This view takes 22-- raw packet timing and aggregates it into something that approximates the 23-- activity of the underlying interface. 24-- 25-- @arg view_name The name of the output view. 26-- @arg group_by Expression to group by (set to 'null' for no grouping). 27-- @arg filter Expression on `android_network_packets` to filter by. 28-- @arg idle_ns The amount of time before considering the network idle. 29-- @arg quant_ns Quantization value, to group rows before the heavy 30-- part of the query. This should be smaller than idle_ns. 31-- 32-- @column group_by The group_by columns are all present in the output. 33-- @column ts The timestamp indicating the start of the segment. 34-- @column dur The duration of the current segment. 35-- @column packet_count The total number of packets in this segment. 36-- @column packet_length The total number of bytes for packets in this segment. 37DROP VIEW IF EXISTS {{view_name}}; 38CREATE PERFETTO VIEW {{view_name}} AS 39WITH quantized AS ( 40 SELECT 41 {{group_by}}, 42 MIN(ts) AS ts, 43 MAX(ts+dur)-MIN(ts) AS dur, 44 SUM(packet_count) AS packet_count, 45 SUM(packet_length) AS packet_length 46 FROM android_network_packets 47 WHERE {{filter}} 48 GROUP BY CAST(ts / {{quant_ns}} AS INT64), {{group_by}} 49), 50with_last AS ( 51 SELECT 52 *, 53 MAX(ts+dur) OVER ( 54 PARTITION BY {{group_by}} 55 ORDER BY ts 56 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING 57 ) AS max_end_so_far 58 FROM quantized 59), 60with_group AS ( 61 SELECT 62 *, 63 COUNT(IIF(ts-max_end_so_far>{{idle_ns}}, 1, null)) OVER ( 64 PARTITION BY {{group_by}} 65 ORDER BY ts 66 ) AS group_id 67 FROM with_last 68) 69SELECT 70 {{group_by}}, 71 MIN(ts) AS ts, 72 MAX(ts+dur)-MIN(ts)+{{idle_ns}} AS dur, 73 SUM(packet_count) AS packet_count, 74 SUM(packet_length) AS packet_length 75FROM with_group 76GROUP BY group_id, {{group_by}} 77