Skip to content

Commit 5525aeb

Browse files
rtla/tests: Test BPF action program
Add a test that implements a BPF program writing to a test map, which is attached to RTLA via --bpf-action to be executed on theshold overflow. A combination of --on-threshold shell with bpftool (which is always present if BPF support is enabled) is used to check whether the BPF program has executed successfully. Suggested-by: Crystal Wood <crwood@redhat.com> Link: https://lore.kernel.org/r/20251126144205.331954-5-tglozar@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 0304a3b commit 5525aeb

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

tools/tracing/rtla/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ src/timerlat.skel.h: src/timerlat.bpf.o
7676

7777
example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
7878
$(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@
79+
80+
tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c
81+
$(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@
7982
else
8083
src/timerlat.skel.h:
8184
$(Q)echo '/* BPF skeleton is disabled */' > src/timerlat.skel.h
8285

8386
example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
8487
$(Q)echo "BPF skeleton support is disabled, skipping example/timerlat_bpf_action.o"
88+
89+
tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c
90+
$(Q)echo "BPF skeleton support is disabled, skipping tests/bpf/bpf_action_map.o"
8591
endif
8692

8793
$(RTLA): $(RTLA_IN)
@@ -103,7 +109,7 @@ clean: doc_clean fixdep-clean
103109
$(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-*
104110
$(Q)rm -rf feature
105111
$(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h example/timerlat_bpf_action.o
106-
check: $(RTLA)
107-
RTLA=$(RTLA) prove -o -f tests/
112+
check: $(RTLA) tests/bpf/bpf_action_map.o
113+
RTLA=$(RTLA) BPFTOOL=$(SYSTEM_BPFTOOL) prove -o -f tests/
108114
examples: example/timerlat_bpf_action.o
109115
.PHONY: FORCE clean check
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_tracing.h>
4+
5+
char LICENSE[] SEC("license") = "GPL";
6+
7+
struct {
8+
__uint(type, BPF_MAP_TYPE_ARRAY);
9+
__uint(max_entries, 1);
10+
__type(key, unsigned int);
11+
__type(value, unsigned long long);
12+
} rtla_test_map SEC(".maps");
13+
14+
struct trace_event_raw_timerlat_sample;
15+
16+
SEC("tp/timerlat_action")
17+
int action_handler(struct trace_event_raw_timerlat_sample *tp_args)
18+
{
19+
unsigned int key = 0;
20+
unsigned long long value = 42;
21+
22+
bpf_map_update_elem(&rtla_test_map, &key, &value, BPF_ANY);
23+
24+
return 0;
25+
}

tools/tracing/rtla/tests/timerlat.t

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ check "hist with trace output at end" \
6767
"timerlat hist -d 1s --on-end trace" 0 "^ Saving trace to timerlat_trace.txt$"
6868
check "top with trace output at end" \
6969
"timerlat top -d 1s --on-end trace" 0 "^ Saving trace to timerlat_trace.txt$"
70+
71+
# BPF action program tests
72+
if [ "$option" -eq 0 ]
73+
then
74+
# Test BPF action program properly in BPF mode
75+
[ -z "$BPFTOOL" ] && BPFTOOL=bpftool
76+
check "hist with BPF action program (BPF mode)" \
77+
"timerlat hist -T 2 --bpf-action tests/bpf/bpf_action_map.o --on-threshold shell,command='$BPFTOOL map dump name rtla_test_map'" \
78+
2 '"value": 42'
79+
else
80+
# Test BPF action program failure in non-BPF mode
81+
check "hist with BPF action program (non-BPF mode)" \
82+
"timerlat hist -T 2 --bpf-action tests/bpf/bpf_action_map.o" \
83+
1 "BPF actions are not supported in tracefs-only mode"
84+
fi
7085
done
7186

7287
test_end

0 commit comments

Comments
 (0)