lib: cgen: replace counters map with per-CPU array#547
Merged
Conversation
Contributor
Author
|
@claude review |
Claude review of PR #547 (f0720d2)Suggestions
CLAUDE.md improvements
|
Switch BF_MAP_TYPE_COUNTERS from BPF_MAP_TYPE_ARRAY to BPF_MAP_TYPE_PERCPU_ARRAY so each CPU updates its own counter slot without atomic contention. The userspace read path in bf_handle_get_counter() now allocates a per-CPU buffer and sums all CPU slots into the returned bf_counter. A matching bf_handle_set_counter() does the reverse for counter preservation: it places the aggregated value in CPU 0's slot and zeroes the rest, replacing the old bf_map_set_elem() call that was passing a single-value buffer to the syscall (which reads num_cpus * value_size bytes and dragged garbage off the stack). E2E tests that read the counter map via bpftool are updated to use the per-CPU dump format: .[N].values | map(.value.count) | add instead of .[N].value.count.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Switch BF_MAP_TYPE_COUNTERS from BPF_MAP_TYPE_ARRAY to
BPF_MAP_TYPE_PERCPU_ARRAY. Each CPU updates its own counter slot
without contention, which eliminates the need for atomic operations
in the BPF fast path (bpf_map_lookup_elem on a per-CPU map returns
a direct pointer to the current CPU's value).
Changes
lib:
BF_BPF_MAP_TYPE_PERCPU_ARRAYtobf_bpf_map_typeBF_BPF_MAP_TYPE_PERCPU_ARRAYbf_handle_get_counter(): allocate a per-CPU buffer, do one lookup,then sum all CPU slots into the returned
bf_counterbf_handle_set_counter(): builds a per-CPU buffer with the valuein CPU 0's slot and zeroes elsewhere, then calls
bf_bpf_map_update_elem()with the full buffer_bf_cgen_transfer_counters(): replacebf_map_set_elem()withbf_handle_set_counter()— the old call passed a 16-byte buffer toa syscall that reads
num_cpus × 16bytes, writing garbage from thestack into the new map
tests:
bpftool map dumpjq queries in 8 e2e tests: per-CPU dumpsuse
values(array per CPU) instead ofvalue, so counter readschange from
.[N].value.countto.[N].values | map(.value.count) | add