From a693bac18092ba8fac9b31f85755339af6d239d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Sun, 13 Aug 2023 07:30:15 +0000 Subject: [PATCH] StatsValue - define operator+= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while this is a little bit more code, it seems much better for the accumulation operation to be next to the struct definition itself (in case we ever add more fields) Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: I26022db4566e69c964298d7b3f2cc4fa4a9a5152 --- bpf_progs/netd.h | 10 ++++++++++ .../native/libs/libnetworkstats/BpfNetworkStats.cpp | 5 +---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bpf_progs/netd.h b/bpf_progs/netd.h index 2995adad06..dcf6d6a554 100644 --- a/bpf_progs/netd.h +++ b/bpf_progs/netd.h @@ -55,6 +55,16 @@ typedef struct { } StatsValue; STRUCT_SIZE(StatsValue, 4 * 8); // 32 +#ifdef __cplusplus +static inline StatsValue& operator+=(StatsValue& lhs, const StatsValue& rhs) { + lhs.rxPackets += rhs.rxPackets; + lhs.rxBytes += rhs.rxBytes; + lhs.txPackets += rhs.txPackets; + lhs.txBytes += rhs.txBytes; + return lhs; +} +#endif + typedef struct { char name[IFNAMSIZ]; } IfaceValue; diff --git a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp index 0d04c05c95..fed297919d 100644 --- a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp +++ b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp @@ -75,10 +75,7 @@ int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats, if (!statsEntry.ok()) { return statsEntry.error(); } - stats->rxPackets += statsEntry.value().rxPackets; - stats->txPackets += statsEntry.value().txPackets; - stats->rxBytes += statsEntry.value().rxBytes; - stats->txBytes += statsEntry.value().txBytes; + *stats += statsEntry.value(); } return Result(); };