Skip to content

Commit 498c3f3

Browse files
committed
Reduce lock contention in cgroup cache rebuild and simplify BPF map update
Walk the cgroup filesystem outside any lock and swap in the fresh cache under a brief write lock, eliminating the upgrade-to-write-lock pattern that blocked all readers during the walk. Extract the inline map lookup/update logic in process_eth() into the existing update_val() helper to reduce code duplication across hook types.
1 parent cb72a11 commit 498c3f3

12 files changed

+13
-24
lines changed

bpf/counter_common.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,7 @@ process_eth(void *data, void *data_end, __u64 pkt_len) {
350350
return;
351351
}
352352

353-
// lookup value in hash
354-
statvalue *val = (statvalue *)bpf_map_lookup_elem(&pkt_count, &key);
355-
if (val) {
356-
// atomic XADD, doesn't need bpf_spin_lock()
357-
__sync_fetch_and_add(&val->packets, 1);
358-
__sync_fetch_and_add(&val->bytes, pkt_len);
359-
} else {
360-
statvalue initval = {.packets = 1, .bytes = pkt_len};
361-
362-
bpf_map_update_elem(&pkt_count, &key, &initval, BPF_NOEXIST);
363-
}
353+
update_val(&key, pkt_len);
364354
}
365355

366356
/**

cgroup.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,22 @@ func cGroupToPath(id uint64) string {
7474
return p
7575
}
7676

77-
// Upgrade to write lock; re-check to avoid redundant WalkDir from concurrent misses.
78-
cGroupCacheLock.Lock()
79-
defer cGroupCacheLock.Unlock()
80-
81-
if p, ok = cGroupCache[id]; ok {
82-
return p
83-
}
84-
85-
// ID genuinely absent: rebuild cache from filesystem.
86-
_ = cGroupWalk(CGroupRootPath, cGroupCache)
77+
// Build a fresh cache outside any lock to avoid blocking readers during
78+
// the filesystem walk, then swap it in under a brief write lock.
79+
fresh := make(map[uint64]string)
80+
_ = cGroupWalk(CGroupRootPath, fresh)
8781

88-
// Create negative cache entry if still missing after walk.
89-
if p, ok = cGroupCache[id]; !ok {
82+
// Synthesise a negative cache entry if still missing after the walk.
83+
p, ok = fresh[id]
84+
if !ok {
9085
p = "cgroup-id: " + strconv.FormatUint(id, 10)
91-
cGroupCache[id] = p
86+
fresh[id] = p
9287
}
9388

89+
cGroupCacheLock.Lock()
90+
cGroupCache = fresh
91+
cGroupCacheLock.Unlock()
92+
9493
return p
9594
}
9695

cgroup_arm64_bpfel.o

0 Bytes
Binary file not shown.

cgroup_x86_bpfel.o

0 Bytes
Binary file not shown.

cgroupskb_arm64_bpfel.o

0 Bytes
Binary file not shown.

cgroupskb_x86_bpfel.o

0 Bytes
Binary file not shown.

kprobe_arm64_bpfel.o

0 Bytes
Binary file not shown.

kprobe_x86_bpfel.o

0 Bytes
Binary file not shown.

tc_arm64_bpfel.o

192 Bytes
Binary file not shown.

tc_x86_bpfel.o

192 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)