BpfNetworkStats: optimize bpfGetIfaceStatsInternal() NULL/wildcard iface case

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 <maze@google.com>
Change-Id: Id30fa1d2b4f30c5b526b5eba80e401d1a201d95d
This commit is contained in:
Maciej Żenczykowski 2025-08-09 18:34:03 -07:00
commit af5e799120

View file

@ -89,6 +89,17 @@ int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
const BpfMapRO<uint32_t, StatsValue>& 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<void> {
@ -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<StatsValue> statsEntry = ifaceStatsMap.readValue(key);
if (!statsEntry.ok()) return statsEntry.error();
*stats += statsEntry.value();