Skip to content

Commit

Permalink
libbpf-tools/cpudist: Allow cpudist to run on old kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
woodpenker authored and yonghong-song committed Nov 26, 2022
1 parent ff28fbb commit 957569f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
25 changes: 18 additions & 7 deletions libbpf-tools/cpudist.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static __always_inline void update_hist(struct task_struct *task,
histp = bpf_map_lookup_elem(&hists, &id);
if (!histp)
return;
bpf_probe_read_kernel_str(&histp->comm, sizeof(task->comm),
task->comm);
BPF_CORE_READ_STR_INTO(&histp->comm, task, comm);
}
delta = ts - *tsp;
if (targ_ms)
Expand All @@ -85,12 +84,10 @@ static __always_inline void update_hist(struct task_struct *task,
__sync_fetch_and_add(&histp->slots[slot], 1);
}

SEC("tp_btf/sched_switch")
int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
struct task_struct *next)
static int handle_switch(struct task_struct *prev, struct task_struct *next)
{
u32 prev_tgid = prev->tgid, prev_pid = prev->pid;
u32 tgid = next->tgid, pid = next->pid;
u32 prev_tgid = BPF_CORE_READ(prev, tgid), prev_pid = BPF_CORE_READ(prev, pid);
u32 tgid = BPF_CORE_READ(next, tgid), pid = BPF_CORE_READ(next, pid);
u64 ts = bpf_ktime_get_ns();

if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
Expand All @@ -107,4 +104,18 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
return 0;
}

SEC("tp_btf/sched_switch")
int BPF_PROG(sched_switch_btf, bool preempt, struct task_struct *prev,
struct task_struct *next)
{
return handle_switch(prev, next);
}

SEC("raw_tp/sched_switch")
int BPF_PROG(sched_switch_tp, bool preempt, struct task_struct *prev,
struct task_struct *next)
{
return handle_switch(prev, next);
}

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

if (probe_tp_btf("sched_switch"))
bpf_program__set_autoload(obj->progs.sched_switch_tp, false);
else
bpf_program__set_autoload(obj->progs.sched_switch_btf, false);

/* initialize global data (filtering options) */
obj->rodata->filter_cg = env.cg;
obj->rodata->targ_per_process = env.per_process;
Expand Down

0 comments on commit 957569f

Please sign in to comment.