aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Zuklie <rzuklie@google.com>2023-05-12 17:47:21 -0700
committerRyan Zuklie <rzuklie@google.com>2023-05-12 17:53:14 -0700
commit71ce4ce705b02e608de9298748fa8ee2665ddfc1 (patch)
tree1169a091b2ced48235fb54340653c59a34fa74e4
parentbaaaed8ad8a20e241e85e300c17495ef188c4446 (diff)
downloadperfetto-71ce4ce705b02e608de9298748fa8ee2665ddfc1.tar.gz
Add a template metric for network activity query.
It is often quite useful to see networking aggregated into regions that indicate whether the interface should be on or off. In the simplest case, it can be an easier way to view large quantities of instant network events. In other cases, it can be used to proxy the network for power or similar analysis. Test: diff test and ui-dev-server Change-Id: I0c58b59d082fe8de640f32fa2a65483e9e9a9e0c
-rw-r--r--Android.bp1
-rw-r--r--BUILD1
-rw-r--r--src/trace_processor/metrics/sql/android/BUILD.gn1
-rw-r--r--src/trace_processor/metrics/sql/android/network_activity_template.sql74
-rw-r--r--test/trace_processor/diff_tests/android/android_network_activity.out4
-rw-r--r--test/trace_processor/diff_tests/android/tests.py65
6 files changed, 146 insertions, 0 deletions
diff --git a/Android.bp b/Android.bp
index 344d5967f..19befb29e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -10035,6 +10035,7 @@ genrule {
"src/trace_processor/metrics/sql/android/java_heap_histogram.sql",
"src/trace_processor/metrics/sql/android/java_heap_stats.sql",
"src/trace_processor/metrics/sql/android/mem_stats_priority_breakdown.sql",
+ "src/trace_processor/metrics/sql/android/network_activity_template.sql",
"src/trace_processor/metrics/sql/android/p_state.sql",
"src/trace_processor/metrics/sql/android/power_drain_in_watts.sql",
"src/trace_processor/metrics/sql/android/power_profile_data.sql",
diff --git a/BUILD b/BUILD
index 8015a7c4f..0062d3a41 100644
--- a/BUILD
+++ b/BUILD
@@ -1784,6 +1784,7 @@ perfetto_filegroup(
"src/trace_processor/metrics/sql/android/java_heap_histogram.sql",
"src/trace_processor/metrics/sql/android/java_heap_stats.sql",
"src/trace_processor/metrics/sql/android/mem_stats_priority_breakdown.sql",
+ "src/trace_processor/metrics/sql/android/network_activity_template.sql",
"src/trace_processor/metrics/sql/android/p_state.sql",
"src/trace_processor/metrics/sql/android/power_drain_in_watts.sql",
"src/trace_processor/metrics/sql/android/power_profile_data.sql",
diff --git a/src/trace_processor/metrics/sql/android/BUILD.gn b/src/trace_processor/metrics/sql/android/BUILD.gn
index bf54d4454..a56666136 100644
--- a/src/trace_processor/metrics/sql/android/BUILD.gn
+++ b/src/trace_processor/metrics/sql/android/BUILD.gn
@@ -82,6 +82,7 @@ perfetto_sql_source_set("android") {
"java_heap_histogram.sql",
"java_heap_stats.sql",
"mem_stats_priority_breakdown.sql",
+ "network_activity_template.sql",
"p_state.sql",
"power_drain_in_watts.sql",
"power_profile_data.sql",
diff --git a/src/trace_processor/metrics/sql/android/network_activity_template.sql b/src/trace_processor/metrics/sql/android/network_activity_template.sql
new file mode 100644
index 000000000..2dcfa31a2
--- /dev/null
+++ b/src/trace_processor/metrics/sql/android/network_activity_template.sql
@@ -0,0 +1,74 @@
+--
+-- Copyright 2023 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+SELECT IMPORT('android.network_packets');
+
+-- Creates a view of aggregated network activity. It is common among networking
+-- to have the interface active for some time after network use. For example, in
+-- mobile networking, it is common to have the cellular interface active for 10
+-- or more seconds after the last packet was sent or received. This view takes
+-- raw packet timing and aggregates it into something that approximates the
+-- activity of the underlying interface.
+--
+-- @arg view_name The name of the output view.
+-- @arg group_by Expression to group by (set to 'null' for no grouping).
+-- @arg filter Expression on `android_network_packets` to filter by.
+-- @arg idle_ns The amount of time before considering the network idle.
+-- @arg quant_ns Quantization value, to group rows before the heavy
+-- part of the query. This should be smaller than idle_ns.
+--
+-- @column group_by The group_by columns are all present in the output.
+-- @column ts The timestamp indicating the start of the segment.
+-- @column dur The duration of the current segment.
+-- @column packet_count The total number of packets in this segment.
+-- @column packet_length The total number of bytes for packets in this segment.
+CREATE VIEW {{view_name}} AS
+WITH quantized AS (
+ SELECT
+ {{group_by}},
+ MIN(ts) AS ts,
+ MAX(ts+dur)-MIN(ts) AS dur,
+ SUM(packet_count) AS packet_count,
+ SUM(packet_length) AS packet_length
+ FROM android_network_packets
+ WHERE {{filter}}
+ GROUP BY CAST(ts / {{quant_ns}} AS INT64), {{group_by}}
+),
+with_last AS (
+ SELECT
+ *,
+ LAG(ts) OVER (
+ PARTITION BY {{group_by}}
+ ORDER BY ts
+ ) AS last_ts
+ FROM quantized
+),
+with_group AS (
+ SELECT
+ *,
+ COUNT(IIF(ts-last_ts>{{idle_ns}}, 1, null)) OVER (
+ PARTITION BY {{group_by}}
+ ORDER BY ts
+ ) AS group_id
+ FROM with_last
+)
+SELECT
+ {{group_by}},
+ MIN(ts) AS ts,
+ MAX(ts+dur)-MIN(ts)+{{idle_ns}} AS dur,
+ SUM(packet_count) AS packet_count,
+ SUM(packet_length) AS packet_length
+FROM with_group
+GROUP BY group_id, {{group_by}}
diff --git a/test/trace_processor/diff_tests/android/android_network_activity.out b/test/trace_processor/diff_tests/android/android_network_activity.out
new file mode 100644
index 000000000..14418e4cd
--- /dev/null
+++ b/test/trace_processor/diff_tests/android/android_network_activity.out
@@ -0,0 +1,4 @@
+"package_name","ts","dur","packet_count","packet_length"
+"uid=123",1000,1010,2,100
+"uid=123",3000,2500,4,200
+"uid=456",1005,1010,2,300
diff --git a/test/trace_processor/diff_tests/android/tests.py b/test/trace_processor/diff_tests/android/tests.py
index f03eed8f9..84bafc1d1 100644
--- a/test/trace_processor/diff_tests/android/tests.py
+++ b/test/trace_processor/diff_tests/android/tests.py
@@ -196,6 +196,71 @@ class Android(TestSuite):
""",
out=Path('android_battery_stats_state.out'))
+ def test_android_network_activity(self):
+ # The following should have three activity regions:
+ # * uid=123 from 1000 to 2010 (note: end is max(ts)+idle_ns)
+ # * uid=456 from 1005 to 2015 (note: doesn't group with above due to name)
+ # * uid=123 from 3000 to 5500 (note: gap between 1010 to 3000 > idle_ns)
+ # Note: packet_timestamps are delta encoded from the base timestamp.
+ return DiffTestBlueprint(
+ trace=TextProto(r"""
+ packet {
+ timestamp: 0
+ network_packet_bundle {
+ ctx {
+ direction: DIR_EGRESS
+ interface: "wlan"
+ uid: 123
+ }
+ packet_timestamps: [
+ 1000, 1010,
+ 3000, 3050, 4000, 4500
+ ],
+ packet_lengths: [
+ 50, 50,
+ 50, 50, 50, 50
+ ],
+ }
+ }
+ packet {
+ timestamp: 0
+ network_packet_bundle {
+ ctx {
+ direction: DIR_EGRESS
+ interface: "wlan"
+ uid: 456
+ }
+ packet_timestamps: [1005, 1015]
+ packet_lengths: [100, 200]
+ }
+ }
+ packet {
+ timestamp: 0
+ network_packet_bundle {
+ ctx {
+ direction: DIR_INGRESS
+ interface: "loopback"
+ uid: 123
+ }
+ packet_timestamps: [6000]
+ packet_lengths: [100]
+ }
+ }
+ """),
+ query="""
+ SELECT RUN_METRIC(
+ 'android/network_activity_template.sql',
+ 'view_name', 'android_network_activity',
+ 'group_by', 'package_name',
+ 'filter', 'iface = "wlan"',
+ 'idle_ns', '1000',
+ 'quant_ns', '100'
+ );
+
+ SELECT * FROM android_network_activity
+ ORDER BY package_name, ts;
+ """,
+ out=Path('android_network_activity.out'))
def test_binder_sync_binder_metrics(self):
return DiffTestBlueprint(