tools/testing/selftests/bpf/prog_tests/ringbuf.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/ringbuf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/ringbuf.c- Extension
.c- Size
- 16963 bytes
- Lines
- 576
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/compiler.hasm/barrier.htest_progs.hsys/mman.hsys/epoll.htime.hsched.hsignal.hpthread.hsys/sysinfo.hlinux/perf_event.hlinux/ring_buffer.htest_ringbuf.lskel.htest_ringbuf_n.lskel.htest_ringbuf_map_key.lskel.htest_ringbuf_write.lskel.htest_ringbuf_overwrite.lskel.h
Detected Declarations
struct samplefunction atomic_incfunction atomic_xchgfunction process_samplefunction trigger_samplesfunction ringbuf_write_subtestfunction ringbuf_subtestfunction ring_buffer__consume_nfunction ringbuf_n_subtestfunction process_map_key_samplefunction ringbuf_map_key_subtestfunction ringbuf_overwrite_mode_subtestfunction test_ringbuf
Annotated Snippet
struct sample {
int pid;
int seq;
long value;
char comm[16];
};
static int sample_cnt;
static void atomic_inc(int *cnt)
{
__atomic_add_fetch(cnt, 1, __ATOMIC_SEQ_CST);
}
static int atomic_xchg(int *cnt, int val)
{
return __atomic_exchange_n(cnt, val, __ATOMIC_SEQ_CST);
}
static int process_sample(void *ctx, void *data, size_t len)
{
struct sample *s = data;
atomic_inc(&sample_cnt);
switch (s->seq) {
case 0:
CHECK(s->value != 333, "sample1_value", "exp %ld, got %ld\n",
333L, s->value);
return 0;
case 1:
CHECK(s->value != 777, "sample2_value", "exp %ld, got %ld\n",
777L, s->value);
return -EDONE;
default:
/* we don't care about the rest */
return 0;
}
}
static struct test_ringbuf_map_key_lskel *skel_map_key;
static struct test_ringbuf_lskel *skel;
static struct ring_buffer *ringbuf;
static void trigger_samples()
{
skel->bss->dropped = 0;
skel->bss->total = 0;
skel->bss->discarded = 0;
/* trigger exactly two samples */
skel->bss->value = 333;
syscall(__NR_getpgid);
skel->bss->value = 777;
syscall(__NR_getpgid);
}
static void *poll_thread(void *input)
{
long timeout = (long)input;
return (void *)(long)ring_buffer__poll(ringbuf, timeout);
}
static void ringbuf_write_subtest(void)
{
struct test_ringbuf_write_lskel *skel;
int page_size = getpagesize();
size_t *mmap_ptr;
int err, rb_fd;
skel = test_ringbuf_write_lskel__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;
skel->maps.ringbuf.max_entries = 0x40000;
err = test_ringbuf_write_lskel__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;
rb_fd = skel->maps.ringbuf.map_fd;
mmap_ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, rb_fd, 0);
if (!ASSERT_OK_PTR(mmap_ptr, "rw_cons_pos"))
goto cleanup;
*mmap_ptr = 0x30000;
ASSERT_OK(munmap(mmap_ptr, page_size), "unmap_rw");
skel->bss->pid = getpid();
Annotation
- Immediate include surface: `linux/compiler.h`, `asm/barrier.h`, `test_progs.h`, `sys/mman.h`, `sys/epoll.h`, `time.h`, `sched.h`, `signal.h`.
- Detected declarations: `struct sample`, `function atomic_inc`, `function atomic_xchg`, `function process_sample`, `function trigger_samples`, `function ringbuf_write_subtest`, `function ringbuf_subtest`, `function ring_buffer__consume_n`, `function ringbuf_n_subtest`, `function process_map_key_sample`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.