Skip to content

Commit

Permalink
libbpf-tools: Allow softirqs to run on old kernels
Browse files Browse the repository at this point in the history
This is part of efforts towards #4231. Fallback to raw tracepoints
if tp_btf is not available.

Signed-off-by: Hengqi Chen <[email protected]>
  • Loading branch information
chenhengqi authored and yonghong-song committed Oct 8, 2022
1 parent c2b9f6e commit e85bd8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
39 changes: 29 additions & 10 deletions libbpf-tools/softirqs.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,26 @@ struct {
__u64 counts[NR_SOFTIRQS] = {};
struct hist hists[NR_SOFTIRQS] = {};

SEC("tp_btf/softirq_entry")
int BPF_PROG(softirq_entry, unsigned int vec_nr)
static int handle_entry(unsigned int vec_nr)
{
u64 ts = bpf_ktime_get_ns();
u32 key = 0;

bpf_map_update_elem(&start, &key, &ts, 0);
bpf_map_update_elem(&start, &key, &ts, BPF_ANY);
return 0;
}

SEC("tp_btf/softirq_exit")
int BPF_PROG(softirq_exit, unsigned int vec_nr)
static int handle_exit(unsigned int vec_nr)
{
u64 delta, *tsp;
u32 key = 0;
s64 delta;
u64 *tsp;

if (vec_nr >= NR_SOFTIRQS)
return 0;
tsp = bpf_map_lookup_elem(&start, &key);
if (!tsp || !*tsp)
if (!tsp)
return 0;
delta = bpf_ktime_get_ns() - *tsp;
if (delta < 0)
return 0;
if (!targ_ns)
delta /= 1000U;

Expand All @@ -64,4 +59,28 @@ int BPF_PROG(softirq_exit, unsigned int vec_nr)
return 0;
}

SEC("tp_btf/softirq_entry")
int BPF_PROG(softirq_entry_btf, unsigned int vec_nr)
{
return handle_entry(vec_nr);
}

SEC("tp_btf/softirq_exit")
int BPF_PROG(softirq_exit_btf, unsigned int vec_nr)
{
return handle_exit(vec_nr);
}

SEC("raw_tp/softirq_entry")
int BPF_PROG(softirq_entry, unsigned int vec_nr)
{
return handle_entry(vec_nr);
}

SEC("raw_tp/softirq_exit")
int BPF_PROG(softirq_exit, unsigned int vec_nr)
{
return handle_exit(vec_nr);
}

char LICENSE[] SEC("license") = "GPL";
8 changes: 8 additions & 0 deletions libbpf-tools/softirqs.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ int main(int argc, char **argv)
return 1;
}

if (probe_tp_btf("softirq_entry")) {
bpf_program__set_autoload(obj->progs.softirq_entry, false);
bpf_program__set_autoload(obj->progs.softirq_exit, false);
} else {
bpf_program__set_autoload(obj->progs.softirq_entry_btf, false);
bpf_program__set_autoload(obj->progs.softirq_exit_btf, false);
}

/* initialize global data (filtering options) */
obj->rodata->targ_dist = env.distributed;
obj->rodata->targ_ns = env.nanoseconds;
Expand Down

0 comments on commit e85bd8f

Please sign in to comment.