From af5e7991201b3435b3c497e76cfaa152f5ea2e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Sat, 9 Aug 2025 18:34:03 -0700 Subject: [PATCH] BpfNetworkStats: optimize bpfGetIfaceStatsInternal() NULL/wildcard iface case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: this is not quite a no-op, as we no longer do the ifname lookup, which could (but shouldn't) fail and cause us to ignore the stats (for that interface). But since it shouldn't ever fail, the value lookup should basically always happen. Switching to forAll() will allow bulk api optimization... Test: TreeHugger, atest libbpf_android_test libnetworkstats_test netd_updatable_unit_test Flag: EXEMPT mainline Signed-off-by: Maciej Żenczykowski Change-Id: Id30fa1d2b4f30c5b526b5eba80e401d1a201d95d --- .../libs/libnetworkstats/BpfNetworkStats.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp index 640f89389d..951baf7ba4 100644 --- a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp +++ b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp @@ -89,6 +89,17 @@ int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats, const BpfMapRO& ifaceStatsMap, const IfIndexToNameFunc ifindex2name) { *stats = {}; + + if (!iface) { // wildcard iface + auto res = ifaceStatsMap.forAll( + [stats](const uint32_t &, const StatsValue &value) { + *stats += value; + } + ); + return res.ok() ? 0 : -res.error().code(); + } + + // specific iface int64_t unknownIfaceBytesTotal = 0; auto res = ifaceStatsMap.iterate( [iface, stats, ifindex2name, &unknownIfaceBytesTotal, &ifaceStatsMap](const uint32_t& key) -> Result { @@ -97,7 +108,8 @@ int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats, maybeLogUnknownIface(key, ifaceStatsMap, key, &unknownIfaceBytesTotal); return {}; } - if (!iface || !strcmp(iface, ifname.value().name)) { + if (!strcmp(iface, ifname.value().name)) { + // TODO: bulk api makes this readValue() inefficient - use forAll(K, V) Result statsEntry = ifaceStatsMap.readValue(key); if (!statsEntry.ok()) return statsEntry.error(); *stats += statsEntry.value();