Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools/cpudist: Allow cpudist to run on old kernels #4338

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and below BPF_CORE_READ will actually call kernel helpers instead of direct memory access. But I understand this is necessary for raw_tp. I think it is okay for portability perspective. We have quite a few tools still using kprobe which uses BPF_CORE_READ as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, do you mean this will have a latency cost to use BPF_CORE_READ instead of direct mem access? Maybe another way is to separate each other with independent functions containing the same logic?

}
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