-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add ring_buffer__consume_n test.
Add a testcase for the ring_buffer__consume_n() API. The test produces multiple samples in a ring buffer, using a sys_getpid() fentry prog, and consumes them from user-space in batches, rather than consuming all of them greedily, like ring_buffer__consume() does. Signed-off-by: Andrea Righi <andrea.righi@canonical.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/lkml/CAEf4BzaR4zqUpDmj44KNLdpJ=Tpa97GrvzuzVNO5nM6b7oWd1w@mail.gmail.com Link: https://lore.kernel.org/bpf/20240425140627.112728-1-andrea.righi@canonical.com
- Loading branch information
Andrea Righi
authored and
Andrii Nakryiko
committed
Apr 25, 2024
1 parent
8ec3bf5
commit 638a485
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2024 Andrea Righi <andrea.righi@canonical.com> | ||
|
||
#include <linux/bpf.h> | ||
#include <sched.h> | ||
#include <unistd.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_misc.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
#define TASK_COMM_LEN 16 | ||
|
||
struct sample { | ||
int pid; | ||
long value; | ||
char comm[16]; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
} ringbuf SEC(".maps"); | ||
|
||
int pid = 0; | ||
long value = 0; | ||
|
||
SEC("fentry/" SYS_PREFIX "sys_getpgid") | ||
int test_ringbuf_n(void *ctx) | ||
{ | ||
int cur_pid = bpf_get_current_pid_tgid() >> 32; | ||
struct sample *sample; | ||
|
||
if (cur_pid != pid) | ||
return 0; | ||
|
||
sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0); | ||
if (!sample) | ||
return 0; | ||
|
||
sample->pid = pid; | ||
sample->value = value; | ||
bpf_get_current_comm(sample->comm, sizeof(sample->comm)); | ||
|
||
bpf_ringbuf_submit(sample, 0); | ||
|
||
return 0; | ||
} |