Skip to content

Commit 957569f

Browse files
woodpenkeryonghong-song
authored andcommitted
libbpf-tools/cpudist: Allow cpudist to run on old kernels
1 parent ff28fbb commit 957569f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

libbpf-tools/cpudist.bpf.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ static __always_inline void update_hist(struct task_struct *task,
7171
histp = bpf_map_lookup_elem(&hists, &id);
7272
if (!histp)
7373
return;
74-
bpf_probe_read_kernel_str(&histp->comm, sizeof(task->comm),
75-
task->comm);
74+
BPF_CORE_READ_STR_INTO(&histp->comm, task, comm);
7675
}
7776
delta = ts - *tsp;
7877
if (targ_ms)
@@ -85,12 +84,10 @@ static __always_inline void update_hist(struct task_struct *task,
8584
__sync_fetch_and_add(&histp->slots[slot], 1);
8685
}
8786

88-
SEC("tp_btf/sched_switch")
89-
int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
90-
struct task_struct *next)
87+
static int handle_switch(struct task_struct *prev, struct task_struct *next)
9188
{
92-
u32 prev_tgid = prev->tgid, prev_pid = prev->pid;
93-
u32 tgid = next->tgid, pid = next->pid;
89+
u32 prev_tgid = BPF_CORE_READ(prev, tgid), prev_pid = BPF_CORE_READ(prev, pid);
90+
u32 tgid = BPF_CORE_READ(next, tgid), pid = BPF_CORE_READ(next, pid);
9491
u64 ts = bpf_ktime_get_ns();
9592

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

107+
SEC("tp_btf/sched_switch")
108+
int BPF_PROG(sched_switch_btf, bool preempt, struct task_struct *prev,
109+
struct task_struct *next)
110+
{
111+
return handle_switch(prev, next);
112+
}
113+
114+
SEC("raw_tp/sched_switch")
115+
int BPF_PROG(sched_switch_tp, bool preempt, struct task_struct *prev,
116+
struct task_struct *next)
117+
{
118+
return handle_switch(prev, next);
119+
}
120+
110121
char LICENSE[] SEC("license") = "GPL";

libbpf-tools/cpudist.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ int main(int argc, char **argv)
217217
return 1;
218218
}
219219

220+
if (probe_tp_btf("sched_switch"))
221+
bpf_program__set_autoload(obj->progs.sched_switch_tp, false);
222+
else
223+
bpf_program__set_autoload(obj->progs.sched_switch_btf, false);
224+
220225
/* initialize global data (filtering options) */
221226
obj->rodata->filter_cg = env.cg;
222227
obj->rodata->targ_per_process = env.per_process;

0 commit comments

Comments
 (0)