Add a map to the rtla-timerlat BPF program that holds a file descriptor
of another BPF program, to be executed on threshold overflow.

timerlat_bpf_set_action() is added as an interface to set the program.

Signed-off-by: Tomas Glozar <[email protected]>
---
 tools/tracing/rtla/src/timerlat.bpf.c | 25 +++++++++++++++++++++----
 tools/tracing/rtla/src/timerlat_bpf.c | 13 +++++++++++++
 tools/tracing/rtla/src/timerlat_bpf.h |  1 +
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/tools/tracing/rtla/src/timerlat.bpf.c 
b/tools/tracing/rtla/src/timerlat.bpf.c
index e2265b5d6491..549d2d2191d2 100644
--- a/tools/tracing/rtla/src/timerlat.bpf.c
+++ b/tools/tracing/rtla/src/timerlat.bpf.c
@@ -40,6 +40,17 @@ struct {
        __uint(max_entries, 1);
 } signal_stop_tracing SEC(".maps");
 
+struct {
+       __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+       __uint(key_size, sizeof(unsigned int));
+       __uint(max_entries, 1);
+       __array(values, unsigned int (void *));
+} bpf_action SEC(".maps") = {
+       .values = {
+               [0] = 0
+       },
+};
+
 /* Params to be set by rtla */
 const volatile int bucket_size = 1;
 const volatile int output_divisor = 1000;
@@ -109,7 +120,7 @@ nosubprog void update_summary(void *map,
        map_set(map, SUMMARY_SUM, map_get(map, SUMMARY_SUM) + latency);
 }
 
-nosubprog void set_stop_tracing(void)
+nosubprog void set_stop_tracing(struct trace_event_raw_timerlat_sample 
*tp_args)
 {
        int value = 0;
 
@@ -118,6 +129,12 @@ nosubprog void set_stop_tracing(void)
 
        /* Signal to userspace */
        bpf_ringbuf_output(&signal_stop_tracing, &value, sizeof(value), 0);
+
+       /*
+        * Call into BPF action program, if attached.
+        * Otherwise, just silently fail.
+        */
+       bpf_tail_call(tp_args, &bpf_action, 0);
 }
 
 SEC("tp/osnoise/timerlat_sample")
@@ -138,19 +155,19 @@ int handle_timerlat_sample(struct 
trace_event_raw_timerlat_sample *tp_args)
                update_summary(&summary_irq, latency, bucket);
 
                if (irq_threshold != 0 && latency_us >= irq_threshold)
-                       set_stop_tracing();
+                       set_stop_tracing(tp_args);
        } else if (tp_args->context == 1) {
                update_main_hist(&hist_thread, bucket);
                update_summary(&summary_thread, latency, bucket);
 
                if (thread_threshold != 0 && latency_us >= thread_threshold)
-                       set_stop_tracing();
+                       set_stop_tracing(tp_args);
        } else {
                update_main_hist(&hist_user, bucket);
                update_summary(&summary_user, latency, bucket);
 
                if (thread_threshold != 0 && latency_us >= thread_threshold)
-                       set_stop_tracing();
+                       set_stop_tracing(tp_args);
        }
 
        return 0;
diff --git a/tools/tracing/rtla/src/timerlat_bpf.c 
b/tools/tracing/rtla/src/timerlat_bpf.c
index e97d16646bcd..1d619e502c65 100644
--- a/tools/tracing/rtla/src/timerlat_bpf.c
+++ b/tools/tracing/rtla/src/timerlat_bpf.c
@@ -59,6 +59,19 @@ int timerlat_bpf_init(struct timerlat_params *params)
        return 0;
 }
 
+/*
+ * timerlat_bpf_set_action - set action on threshold executed on BPF side
+ */
+static int timerlat_bpf_set_action(struct bpf_program *prog)
+{
+       unsigned int key = 0, value = bpf_program__fd(prog);
+
+       return bpf_map__update_elem(bpf->maps.bpf_action,
+                                   &key, sizeof(key),
+                                   &value, sizeof(value),
+                                   BPF_ANY);
+}
+
 /*
  * timerlat_bpf_attach - attach BPF program to collect timerlat data
  */
diff --git a/tools/tracing/rtla/src/timerlat_bpf.h 
b/tools/tracing/rtla/src/timerlat_bpf.h
index 118487436d30..b5009092c7a3 100644
--- a/tools/tracing/rtla/src/timerlat_bpf.h
+++ b/tools/tracing/rtla/src/timerlat_bpf.h
@@ -12,6 +12,7 @@ enum summary_field {
 };
 
 #ifndef __bpf__
+#include <bpf/libbpf.h>
 #ifdef HAVE_BPF_SKEL
 int timerlat_bpf_init(struct timerlat_params *params);
 int timerlat_bpf_attach(void);
-- 
2.51.1


Reply via email to