[PATCH v4 3/7] rtla/timerlat: Add example for BPF action program

Tomas Glozar posted 7 patches 2 months, 2 weeks ago
[PATCH v4 3/7] rtla/timerlat: Add example for BPF action program
Posted by Tomas Glozar 2 months, 2 weeks ago
Add an example BPF action program that prints the measured latency to
the tracefs buffer via bpf_printk().

A new Makefile target, "examples", is added to build the example. In
addition, "sample/" subfolder is renamed to "example".

If BPF skeleton support is unavailable or disabled, a warning will be
displayed when building the BPF action program example.

Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
 tools/tracing/rtla/Makefile                      |  9 ++++++++-
 tools/tracing/rtla/example/timerlat_bpf_action.c | 16 ++++++++++++++++
 .../rtla/{sample => example}/timerlat_load.py    |  0
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 tools/tracing/rtla/example/timerlat_bpf_action.c
 rename tools/tracing/rtla/{sample => example}/timerlat_load.py (100%)

diff --git a/tools/tracing/rtla/Makefile b/tools/tracing/rtla/Makefile
index 746ccf2f5808..5f1529ce3693 100644
--- a/tools/tracing/rtla/Makefile
+++ b/tools/tracing/rtla/Makefile
@@ -73,9 +73,15 @@ src/timerlat.bpf.o: src/timerlat.bpf.c
 
 src/timerlat.skel.h: src/timerlat.bpf.o
 	$(QUIET_GENSKEL)$(SYSTEM_BPFTOOL) gen skeleton $< > $@
+
+example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
+	$(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@
 else
 src/timerlat.skel.h:
 	$(Q)echo '/* BPF skeleton is disabled */' > src/timerlat.skel.h
+
+example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
+	$(Q)echo "BPF skeleton support is disabled, skipping example/timerlat_bpf_action.o"
 endif
 
 $(RTLA): $(RTLA_IN)
@@ -96,7 +102,8 @@ clean: doc_clean fixdep-clean
 	$(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
 	$(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-*
 	$(Q)rm -rf feature
-	$(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h
+	$(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h example/timerlat_bpf_action.o
 check: $(RTLA)
 	RTLA=$(RTLA) prove -o -f tests/
+examples: example/timerlat_bpf_action.o
 .PHONY: FORCE clean check
diff --git a/tools/tracing/rtla/example/timerlat_bpf_action.c b/tools/tracing/rtla/example/timerlat_bpf_action.c
new file mode 100644
index 000000000000..ac1be049a848
--- /dev/null
+++ b/tools/tracing/rtla/example/timerlat_bpf_action.c
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_tracing.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+struct trace_event_raw_timerlat_sample {
+	unsigned long long timer_latency;
+} __attribute__((preserve_access_index));
+
+SEC("tp/timerlat_action")
+int action_handler(struct trace_event_raw_timerlat_sample *tp_args)
+{
+	bpf_printk("Latency: %lld\n", tp_args->timer_latency);
+	return 0;
+}
diff --git a/tools/tracing/rtla/sample/timerlat_load.py b/tools/tracing/rtla/example/timerlat_load.py
similarity index 100%
rename from tools/tracing/rtla/sample/timerlat_load.py
rename to tools/tracing/rtla/example/timerlat_load.py
-- 
2.51.1
Re: [PATCH v4 3/7] rtla/timerlat: Add example for BPF action program
Posted by Wander Lairson Costa 1 month, 3 weeks ago
On Wed, Nov 26, 2025 at 03:42:01PM +0100, Tomas Glozar wrote:
> Add an example BPF action program that prints the measured latency to
> the tracefs buffer via bpf_printk().
> 
> A new Makefile target, "examples", is added to build the example. In
> addition, "sample/" subfolder is renamed to "example".
> 
> If BPF skeleton support is unavailable or disabled, a warning will be
> displayed when building the BPF action program example.
> 
> Signed-off-by: Tomas Glozar <tglozar@redhat.com>
> ---
>  tools/tracing/rtla/Makefile                      |  9 ++++++++-
>  tools/tracing/rtla/example/timerlat_bpf_action.c | 16 ++++++++++++++++
>  .../rtla/{sample => example}/timerlat_load.py    |  0
>  3 files changed, 24 insertions(+), 1 deletion(-)
>  create mode 100644 tools/tracing/rtla/example/timerlat_bpf_action.c
>  rename tools/tracing/rtla/{sample => example}/timerlat_load.py (100%)
> 
> diff --git a/tools/tracing/rtla/Makefile b/tools/tracing/rtla/Makefile
> index 746ccf2f5808..5f1529ce3693 100644
> --- a/tools/tracing/rtla/Makefile
> +++ b/tools/tracing/rtla/Makefile
> @@ -73,9 +73,15 @@ src/timerlat.bpf.o: src/timerlat.bpf.c
>  
>  src/timerlat.skel.h: src/timerlat.bpf.o
>  	$(QUIET_GENSKEL)$(SYSTEM_BPFTOOL) gen skeleton $< > $@
> +
> +example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
> +	$(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@

I couldn't understand the need for the `filter` function.

>  else
>  src/timerlat.skel.h:
>  	$(Q)echo '/* BPF skeleton is disabled */' > src/timerlat.skel.h
> +
> +example/timerlat_bpf_action.o: example/timerlat_bpf_action.c
> +	$(Q)echo "BPF skeleton support is disabled, skipping example/timerlat_bpf_action.o"
>  endif
>  
>  $(RTLA): $(RTLA_IN)
> @@ -96,7 +102,8 @@ clean: doc_clean fixdep-clean
>  	$(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
>  	$(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-*
>  	$(Q)rm -rf feature
> -	$(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h
> +	$(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h example/timerlat_bpf_action.o
>  check: $(RTLA)
>  	RTLA=$(RTLA) prove -o -f tests/
> +examples: example/timerlat_bpf_action.o
>  .PHONY: FORCE clean check
> diff --git a/tools/tracing/rtla/example/timerlat_bpf_action.c b/tools/tracing/rtla/example/timerlat_bpf_action.c
> new file mode 100644
> index 000000000000..ac1be049a848
> --- /dev/null
> +++ b/tools/tracing/rtla/example/timerlat_bpf_action.c
> @@ -0,0 +1,16 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <linux/bpf.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char LICENSE[] SEC("license") = "GPL";
> +
> +struct trace_event_raw_timerlat_sample {
> +	unsigned long long timer_latency;
> +} __attribute__((preserve_access_index));
> +
> +SEC("tp/timerlat_action")
> +int action_handler(struct trace_event_raw_timerlat_sample *tp_args)
> +{
> +	bpf_printk("Latency: %lld\n", tp_args->timer_latency);
> +	return 0;
> +}
> diff --git a/tools/tracing/rtla/sample/timerlat_load.py b/tools/tracing/rtla/example/timerlat_load.py
> similarity index 100%
> rename from tools/tracing/rtla/sample/timerlat_load.py
> rename to tools/tracing/rtla/example/timerlat_load.py
> -- 
> 2.51.1
>
Re: [PATCH v4 3/7] rtla/timerlat: Add example for BPF action program
Posted by Tomas Glozar 1 month, 3 weeks ago
út 16. 12. 2025 v 16:22 odesílatel Wander Lairson Costa
<wander@redhat.com> napsal:
>
> I couldn't understand the need for the `filter` function.
>

Oh that is copied from stalld, where the dependency list includes .h
header files. See our discussion from before [1], I promised to remove
it but forgot about it while doing the rebase.

[1] https://lore.kernel.org/linux-trace-kernel/CAP4=nvSJq1kOUvBRwy_5Fh_FX=Qx-=xiRfngSyky1eVdZsGrrg@mail.gmail.com/

Tomas