Merge "metric: Add network performance metric" am: 8aecaa34dc

Original change: https://android-review.googlesource.com/c/platform/external/perfetto/+/1913152

Change-Id: I02a49fd9a9dee3c1090f05ad4a1067000c69588f
diff --git a/Android.bp b/Android.bp
index c7f2447..57ce922 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3683,6 +3683,7 @@
         "protos/perfetto/metrics/android/mem_metric.proto",
         "protos/perfetto/metrics/android/mem_unagg_metric.proto",
         "protos/perfetto/metrics/android/multiuser_metric.proto",
+        "protos/perfetto/metrics/android/network_metric.proto",
         "protos/perfetto/metrics/android/package_list.proto",
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
@@ -3740,6 +3741,7 @@
         "protos/perfetto/metrics/android/mem_metric.proto",
         "protos/perfetto/metrics/android/mem_unagg_metric.proto",
         "protos/perfetto/metrics/android/multiuser_metric.proto",
+        "protos/perfetto/metrics/android/network_metric.proto",
         "protos/perfetto/metrics/android/package_list.proto",
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
@@ -8082,6 +8084,7 @@
         "src/trace_processor/metrics/sql/android/android_mem_unagg.sql",
         "src/trace_processor/metrics/sql/android/android_multiuser.sql",
         "src/trace_processor/metrics/sql/android/android_multiuser_populator.sql",
+        "src/trace_processor/metrics/sql/android/android_netperf.sql",
         "src/trace_processor/metrics/sql/android/android_package_list.sql",
         "src/trace_processor/metrics/sql/android/android_powrails.sql",
         "src/trace_processor/metrics/sql/android/android_proxy_power.sql",
diff --git a/BUILD b/BUILD
index ccc51d8..0fbed3c 100644
--- a/BUILD
+++ b/BUILD
@@ -1054,6 +1054,7 @@
         "src/trace_processor/metrics/sql/android/android_mem_unagg.sql",
         "src/trace_processor/metrics/sql/android/android_multiuser.sql",
         "src/trace_processor/metrics/sql/android/android_multiuser_populator.sql",
+        "src/trace_processor/metrics/sql/android/android_netperf.sql",
         "src/trace_processor/metrics/sql/android/android_package_list.sql",
         "src/trace_processor/metrics/sql/android/android_powrails.sql",
         "src/trace_processor/metrics/sql/android/android_proxy_power.sql",
@@ -2618,6 +2619,7 @@
         "protos/perfetto/metrics/android/mem_metric.proto",
         "protos/perfetto/metrics/android/mem_unagg_metric.proto",
         "protos/perfetto/metrics/android/multiuser_metric.proto",
+        "protos/perfetto/metrics/android/network_metric.proto",
         "protos/perfetto/metrics/android/package_list.proto",
         "protos/perfetto/metrics/android/powrails_metric.proto",
         "protos/perfetto/metrics/android/process_metadata.proto",
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index c217438..b4cc80b 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -40,6 +40,7 @@
     "mem_metric.proto",
     "mem_unagg_metric.proto",
     "multiuser_metric.proto",
+    "network_metric.proto",
     "package_list.proto",
     "powrails_metric.proto",
     "process_metadata.proto",
diff --git a/protos/perfetto/metrics/android/network_metric.proto b/protos/perfetto/metrics/android/network_metric.proto
new file mode 100644
index 0000000..61fac5c
--- /dev/null
+++ b/protos/perfetto/metrics/android/network_metric.proto
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2020 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
+ *
+ *      http://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.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+message AndroidNetworkMetric {
+  message PacketStatistic {
+    // Packet count
+    optional int64 packets = 1;
+
+    // Packet Bytes
+    optional int64 bytes = 2;
+
+    // Timestamp when first packet received or transmitted
+    optional int64 first_packet_timestamp_ns = 3;
+
+    // Timestamp when last packet received or transmitted
+    optional int64 last_packet_timestamp_ns = 4;
+
+    // Interval between first & last packet. The minimum interval is 10ms.
+    optional int64 interval_ns = 5;
+
+    // Data Speed
+    optional double data_rate_kbps = 6;
+  }
+
+  message CorePacketStatistic {
+    optional uint32 id = 1;
+    optional PacketStatistic packet_statistic = 2;
+  }
+
+  message Rx {
+    // Total packets statistic
+    optional PacketStatistic total = 1;
+    // Per core packets statistic
+    repeated CorePacketStatistic core = 2;
+  }
+
+  message NetDevice {
+    // Network device name
+    optional string name = 1;
+
+    // Ingress traffic statistic
+    optional Rx rx = 2;
+  }
+
+  // Network device metrics
+  repeated NetDevice net_devices = 1;
+}
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index c948145..8bdc51e 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -38,6 +38,7 @@
 import "protos/perfetto/metrics/android/mem_metric.proto";
 import "protos/perfetto/metrics/android/mem_unagg_metric.proto";
 import "protos/perfetto/metrics/android/multiuser_metric.proto";
+import "protos/perfetto/metrics/android/network_metric.proto";
 import "protos/perfetto/metrics/android/package_list.proto";
 import "protos/perfetto/metrics/android/powrails_metric.proto";
 import "protos/perfetto/metrics/android/profiler_smaps.proto";
@@ -94,7 +95,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 40
+// Next id: 41
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -196,6 +197,9 @@
   // Metrics for dynamic voltage and frequency scaling.
   optional AndroidDvfsMetric android_dvfs = 39;
 
+  // Metrics for network performance.
+  optional AndroidNetworkMetric android_netperf = 40;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/protos/perfetto/metrics/perfetto_merged_metrics.proto b/protos/perfetto/metrics/perfetto_merged_metrics.proto
index 15a8d87..9bd0298 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -796,6 +796,55 @@
 }
 // End of protos/perfetto/metrics/android/multiuser_metric.proto
 
+// Begin of protos/perfetto/metrics/android/network_metric.proto
+
+message AndroidNetworkMetric {
+  message PacketStatistic {
+    // Packet count
+    optional int64 packets = 1;
+
+    // Packet Bytes
+    optional int64 bytes = 2;
+
+    // Timestamp when first packet received or transmitted
+    optional int64 first_packet_timestamp_ns = 3;
+
+    // Timestamp when last packet received or transmitted
+    optional int64 last_packet_timestamp_ns = 4;
+
+    // Interval between first & last packet. The minimum interval is 10ms.
+    optional int64 interval_ns = 5;
+
+    // Data Speed
+    optional double data_rate_kbps = 6;
+  }
+
+  message CorePacketStatistic {
+    optional uint32 id = 1;
+    optional PacketStatistic packet_statistic = 2;
+  }
+
+  message Rx {
+    // Total packets statistic
+    optional PacketStatistic total = 1;
+    // Per core packets statistic
+    repeated CorePacketStatistic core = 2;
+  }
+
+  message NetDevice {
+    // Network device name
+    optional string name = 1;
+
+    // Ingress traffic statistic
+    optional Rx rx = 2;
+  }
+
+  // Network device metrics
+  repeated NetDevice net_devices = 1;
+}
+
+// End of protos/perfetto/metrics/android/network_metric.proto
+
 // Begin of protos/perfetto/metrics/android/package_list.proto
 
 message AndroidPackageList {
@@ -1272,7 +1321,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 40
+// Next id: 41
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -1374,6 +1423,9 @@
   // Metrics for dynamic voltage and frequency scaling.
   optional AndroidDvfsMetric android_dvfs = 39;
 
+  // Metrics for network performance.
+  optional AndroidNetworkMetric android_netperf = 40;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/src/trace_processor/metrics/sql/BUILD.gn b/src/trace_processor/metrics/sql/BUILD.gn
index 7ddbf2a..8be8f90 100644
--- a/src/trace_processor/metrics/sql/BUILD.gn
+++ b/src/trace_processor/metrics/sql/BUILD.gn
@@ -30,6 +30,7 @@
   "android/android_hwui_threads.sql",
   "android/android_mem.sql",
   "android/android_mem_unagg.sql",
+  "android/android_netperf.sql",
   "android/android_ion.sql",
   "android/composer_execution.sql",
   "android/composition_layers.sql",
diff --git a/src/trace_processor/metrics/sql/android/android_netperf.sql b/src/trace_processor/metrics/sql/android/android_netperf.sql
new file mode 100644
index 0000000..f7d8ae5
--- /dev/null
+++ b/src/trace_processor/metrics/sql/android/android_netperf.sql
@@ -0,0 +1,93 @@
+--
+-- Copyright 2021 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.
+
+DROP VIEW IF EXISTS rx_packets;
+CREATE VIEW rx_packets AS
+  SELECT
+    ts,
+    RTRIM(name, " Received KB") AS dev,
+    EXTRACT_ARG(arg_set_id, 'cpu') AS cpu,
+    EXTRACT_ARG(arg_set_id, 'len') AS len
+  FROM counter c
+  LEFT JOIN counter_track t
+    ON c.track_id = t.id
+  WHERE name GLOB "* Received KB"
+  ORDER BY ts DESC;
+
+DROP VIEW IF EXISTS device_total_ingress_traffic;
+CREATE VIEW device_total_ingress_traffic AS
+  SELECT
+    dev,
+    MIN(ts) AS start_ts,
+    MAX(ts) AS end_ts,
+    IIF((MAX(ts) - MIN(ts)) > 10000000, MAX(ts)-MIN(ts), 10000000) AS interval,
+    count(1) AS packets,
+    SUM(len) AS bytes
+  FROM rx_packets
+  GROUP BY dev;
+
+DROP VIEW IF EXISTS device_per_core_ingress_traffic;
+CREATE VIEW device_per_core_ingress_traffic AS
+  SELECT
+    dev,
+    AndroidNetworkMetric_CorePacketStatistic(
+      'id', cpu,
+      'packet_statistic', AndroidNetworkMetric_PacketStatistic(
+        'packets', count(1),
+        'bytes', SUM(len),
+        'first_packet_timestamp_ns', MIN(ts),
+        'last_packet_timestamp_ns', MAX(ts),
+        'interval_ns', IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000),
+        'data_rate_kbps', (SUM(len)*8)/(IIF((MAX(ts)-MIN(ts))>10000000, MAX(ts)-MIN(ts), 10000000)/1e9)/1024
+      )
+    ) AS proto
+  FROM rx_packets
+  GROUP BY dev, cpu;
+
+DROP VIEW IF EXISTS device_ingress_traffic_statistic;
+CREATE VIEW device_ingress_traffic_statistic AS
+  SELECT
+    AndroidNetworkMetric_NetDevice(
+      'name', dev,
+      'rx', AndroidNetworkMetric_Rx(
+        'total', AndroidNetworkMetric_PacketStatistic(
+          'packets', packets,
+          'bytes', bytes,
+          'first_packet_timestamp_ns', start_ts,
+          'last_packet_timestamp_ns', end_ts,
+          'interval_ns', interval,
+          'data_rate_kbps', (bytes*8)/(interval/1e9)/1024
+        ),
+        'core', (
+          SELECT
+            RepeatedField(proto)
+          FROM device_per_core_ingress_traffic
+          WHERE device_per_core_ingress_traffic.dev = device_total_ingress_traffic.dev
+        )
+      )
+    ) AS proto
+  FROM device_total_ingress_traffic
+  ORDER BY dev;
+
+DROP VIEW IF EXISTS android_netperf_output;
+CREATE VIEW android_netperf_output AS
+  SELECT AndroidNetworkMetric(
+    'net_devices', (
+      SELECT
+        RepeatedField(proto)
+      FROM device_ingress_traffic_statistic
+    )
+  );
+
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
index 167645f..9ade464 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1 b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
index 5d91f78..1a51f76 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
@@ -2,5 +2,5 @@
 // SHA1(tools/gen_binary_descriptors)
 // 9fc6d77de57ec76a80b76aa282f4c7cf5ce55eec
 // SHA1(protos/perfetto/metrics/metrics.proto)
-// fe8af2b2dce211830488daf2357aea79b4b2a539
+// 1653b1ab1e7701e7a30c606e357ab1957fa2fd48
   
\ No newline at end of file
diff --git a/test/trace_processor/network/index b/test/trace_processor/network/index
index a252836..73f9898 100644
--- a/test/trace_processor/network/index
+++ b/test/trace_processor/network/index
@@ -1,2 +1,3 @@
 # Network performance
 netif_receive_skb.textproto netif_receive_skb.sql netif_receive_skb.out
+netperf_metric.textproto android_netperf netperf_metric.out
diff --git a/test/trace_processor/network/netperf_metric.out b/test/trace_processor/network/netperf_metric.out
new file mode 100644
index 0000000..89ca722
--- /dev/null
+++ b/test/trace_processor/network/netperf_metric.out
@@ -0,0 +1,61 @@
+android_netperf{
+  net_devices {
+    name: "rmnet0"
+    rx {
+      total {
+        packets: 4
+        bytes: 4096
+        first_packet_timestamp_ns: 100000000
+        last_packet_timestamp_ns: 140000000
+        interval_ns: 40000000
+        data_rate_kbps: 800.0
+      }
+      core {
+        id: 0
+        packet_statistic {
+          packets: 2
+          bytes: 2048
+          first_packet_timestamp_ns: 100000000
+          last_packet_timestamp_ns: 140000000
+          interval_ns: 40000000
+          data_rate_kbps: 400.0
+        }
+      }
+      core {
+        id: 1
+        packet_statistic {
+          packets: 2
+          bytes: 2048
+          first_packet_timestamp_ns: 120000000
+          last_packet_timestamp_ns: 140000000
+          interval_ns: 20000000
+          data_rate_kbps: 800.0
+        }
+      }
+    }
+  }
+  net_devices {
+    name: "wlan"
+    rx {
+      total {
+        packets: 1
+        bytes: 512
+        first_packet_timestamp_ns: 100000000
+        last_packet_timestamp_ns: 100000000
+        interval_ns: 10000000
+        data_rate_kbps: 400.0
+      }
+      core {
+        id: 4
+        packet_statistic {
+          packets: 1
+          bytes: 512
+          first_packet_timestamp_ns: 100000000
+          last_packet_timestamp_ns: 100000000
+          interval_ns: 10000000
+          data_rate_kbps: 400.0
+        }
+      }
+    }
+  }
+}
diff --git a/test/trace_processor/network/netperf_metric.textproto b/test/trace_processor/network/netperf_metric.textproto
new file mode 100644
index 0000000..ffa3ad1
--- /dev/null
+++ b/test/trace_processor/network/netperf_metric.textproto
@@ -0,0 +1,65 @@
+ packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 100000000
+      pid: 200
+      netif_receive_skb {
+        name: "rmnet0"
+        len: 1024
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 1
+    event {
+      timestamp: 120000000
+      pid: 300
+      netif_receive_skb {
+        name: "rmnet0"
+        len: 1024
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 140000000
+      pid: 400
+      netif_receive_skb {
+        name: "rmnet0"
+        len: 1024
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 1
+    event {
+      timestamp: 140000000
+      pid: 500
+      netif_receive_skb {
+        name: "rmnet0"
+        len: 1024
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 4
+    event {
+      timestamp: 100000000
+      pid: 600
+      netif_receive_skb {
+        name: "wlan"
+        len: 512
+      }
+    }
+  }
+}