BpfNetworkStats: use atomic for ifindex cache when possible
Test: TreeHugger Flag: EXEMPT BUGFIX Signed-off-by: Maciej Żenczykowski <maze@google.com> Change-Id: Ibc24f079711c36a8bc6ff850fc5b07fb6546b3ae
This commit is contained in:
parent
8095d6f081
commit
a22d4e1061
1 changed files with 22 additions and 7 deletions
|
|
@ -50,23 +50,38 @@ const BpfMapRO<uint32_t, StatsValue>& getIfaceStatsMap() {
|
|||
return ifaceStatsMap;
|
||||
}
|
||||
|
||||
#ifndef __riscv
|
||||
#ifndef __ILP32__
|
||||
static constexpr unsigned cacheSize = 1024; // see 'iface_index_name_map' bpf map size
|
||||
|
||||
#if !defined(__ILP32__) && !defined(__riscv)
|
||||
static_assert(std::atomic<IfaceValue>::is_always_lock_free,
|
||||
"Error: 16-byte IfaceValue is not lock-free on this platform!");
|
||||
#endif
|
||||
#define CACHE_IS_ATOMIC
|
||||
static std::atomic<IfaceValue> cache[cacheSize];
|
||||
#else
|
||||
static IfaceValue cache[cacheSize];
|
||||
#endif
|
||||
|
||||
static constexpr unsigned cacheSize = 1024; // see 'iface_index_name_map' bpf map size
|
||||
static IfaceValue cache[cacheSize];
|
||||
|
||||
static inline const IfaceValue& updateCache(unsigned i, const IfaceValue &v) {
|
||||
if (i < cacheSize) cache[i] = v;
|
||||
if (i < cacheSize) {
|
||||
#ifdef CACHE_IS_ATOMIC
|
||||
cache[i].store(v, std::memory_order_relaxed);
|
||||
#else
|
||||
cache[i] = v;
|
||||
#endif
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
Result<IfaceValue> ifindex2name(const uint32_t ifindex) {
|
||||
if (ifindex < cacheSize && cache[ifindex].name[0]) return cache[ifindex];
|
||||
if (ifindex < cacheSize) {
|
||||
#ifdef CACHE_IS_ATOMIC
|
||||
IfaceValue c = cache[ifindex].load(std::memory_order_relaxed);
|
||||
#else
|
||||
IfaceValue c = cache[ifindex];
|
||||
#endif
|
||||
if (c.name[0]) return c;
|
||||
}
|
||||
|
||||
Result<IfaceValue> v = getIfaceIndexNameMap().readValue(ifindex);
|
||||
if (v.ok()) return updateCache(ifindex, v.value());
|
||||
|
|
|
|||
Loading…
Reference in a new issue