1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/bpf_counter.h"
3 #include "util/debug.h"
4 #include "util/evsel.h"
5 #include "util/evlist.h"
6 #include "util/off_cpu.h"
7 #include "util/perf-hooks.h"
8 #include "util/record.h"
9 #include "util/session.h"
10 #include "util/target.h"
11 #include "util/cpumap.h"
12 #include "util/thread_map.h"
13 #include "util/cgroup.h"
14 #include "util/strlist.h"
17 #include "bpf_skel/off_cpu.skel.h"
21 /* we don't need actual timestamp, just want to put the samples at last */
22 #define OFF_CPU_TIMESTAMP (~0ull << 32)
24 static struct off_cpu_bpf
*skel
;
35 struct perf_event_header hdr
;
36 u64 array
[1024 / sizeof(u64
)];
39 static int off_cpu_config(struct evlist
*evlist
)
42 struct perf_event_attr attr
= {
43 .type
= PERF_TYPE_SOFTWARE
,
44 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
45 .size
= sizeof(attr
), /* to capture ABI version */
47 char *evname
= strdup(OFFCPU_EVENT
);
52 evsel
= evsel__new(&attr
);
58 evsel
->core
.attr
.freq
= 1;
59 evsel
->core
.attr
.sample_period
= 1;
60 /* off-cpu analysis depends on stack trace */
61 evsel
->core
.attr
.sample_type
= PERF_SAMPLE_CALLCHAIN
;
63 evlist__add(evlist
, evsel
);
71 static void off_cpu_start(void *arg
)
73 struct evlist
*evlist
= arg
;
75 /* update task filter for the given workload */
76 if (skel
->rodata
->has_task
&& skel
->rodata
->uses_tgid
&&
77 perf_thread_map__pid(evlist
->core
.threads
, 0) != -1) {
82 fd
= bpf_map__fd(skel
->maps
.task_filter
);
83 pid
= perf_thread_map__pid(evlist
->core
.threads
, 0);
84 bpf_map_update_elem(fd
, &pid
, &val
, BPF_ANY
);
87 skel
->bss
->enabled
= 1;
90 static void off_cpu_finish(void *arg __maybe_unused
)
92 skel
->bss
->enabled
= 0;
93 off_cpu_bpf__destroy(skel
);
96 /* v5.18 kernel added prev_state arg, so it needs to check the signature */
97 static void check_sched_switch_args(void)
99 struct btf
*btf
= btf__load_vmlinux_btf();
100 const struct btf_type
*t1
, *t2
, *t3
;
103 type_id
= btf__find_by_name_kind(btf
, "btf_trace_sched_switch",
105 if ((s32
)type_id
< 0)
108 t1
= btf__type_by_id(btf
, type_id
);
112 t2
= btf__type_by_id(btf
, t1
->type
);
113 if (t2
== NULL
|| !btf_is_ptr(t2
))
116 t3
= btf__type_by_id(btf
, t2
->type
);
117 /* btf_trace func proto has one more argument for the context */
118 if (t3
&& btf_is_func_proto(t3
) && btf_vlen(t3
) == 5) {
119 /* new format: pass prev_state as 4th arg */
120 skel
->rodata
->has_prev_state
= true;
126 int off_cpu_prepare(struct evlist
*evlist
, struct target
*target
,
127 struct record_opts
*opts
)
130 int ncpus
= 1, ntasks
= 1, ncgrps
= 1;
131 struct strlist
*pid_slist
= NULL
;
132 struct str_node
*pos
;
134 if (off_cpu_config(evlist
) < 0) {
135 pr_err("Failed to config off-cpu BPF event\n");
139 skel
= off_cpu_bpf__open();
141 pr_err("Failed to open off-cpu BPF skeleton\n");
145 /* don't need to set cpu filter for system-wide mode */
146 if (target
->cpu_list
) {
147 ncpus
= perf_cpu_map__nr(evlist
->core
.user_requested_cpus
);
148 bpf_map__set_max_entries(skel
->maps
.cpu_filter
, ncpus
);
149 skel
->rodata
->has_cpu
= 1;
153 pid_slist
= strlist__new(target
->pid
, NULL
);
155 pr_err("Failed to create a strlist for pid\n");
160 strlist__for_each_entry(pos
, pid_slist
) {
162 int pid
= strtol(pos
->s
, &end_ptr
, 10);
164 if (pid
== INT_MIN
|| pid
== INT_MAX
||
165 (*end_ptr
!= '\0' && *end_ptr
!= ','))
171 if (ntasks
< MAX_PROC
)
174 bpf_map__set_max_entries(skel
->maps
.task_filter
, ntasks
);
175 skel
->rodata
->has_task
= 1;
176 skel
->rodata
->uses_tgid
= 1;
177 } else if (target__has_task(target
)) {
178 ntasks
= perf_thread_map__nr(evlist
->core
.threads
);
179 bpf_map__set_max_entries(skel
->maps
.task_filter
, ntasks
);
180 skel
->rodata
->has_task
= 1;
181 } else if (target__none(target
)) {
182 bpf_map__set_max_entries(skel
->maps
.task_filter
, MAX_PROC
);
183 skel
->rodata
->has_task
= 1;
184 skel
->rodata
->uses_tgid
= 1;
187 if (evlist__first(evlist
)->cgrp
) {
188 ncgrps
= evlist
->core
.nr_entries
- 1; /* excluding a dummy */
189 bpf_map__set_max_entries(skel
->maps
.cgroup_filter
, ncgrps
);
191 if (!cgroup_is_v2("perf_event"))
192 skel
->rodata
->uses_cgroup_v1
= true;
193 skel
->rodata
->has_cgroup
= 1;
196 if (opts
->record_cgroup
) {
197 skel
->rodata
->needs_cgroup
= true;
199 if (!cgroup_is_v2("perf_event"))
200 skel
->rodata
->uses_cgroup_v1
= true;
204 check_sched_switch_args();
206 err
= off_cpu_bpf__load(skel
);
208 pr_err("Failed to load off-cpu skeleton\n");
212 if (target
->cpu_list
) {
216 fd
= bpf_map__fd(skel
->maps
.cpu_filter
);
218 for (i
= 0; i
< ncpus
; i
++) {
219 cpu
= perf_cpu_map__cpu(evlist
->core
.user_requested_cpus
, i
).cpu
;
220 bpf_map_update_elem(fd
, &cpu
, &val
, BPF_ANY
);
227 fd
= bpf_map__fd(skel
->maps
.task_filter
);
229 strlist__for_each_entry(pos
, pid_slist
) {
232 int pid
= strtol(pos
->s
, &end_ptr
, 10);
234 if (pid
== INT_MIN
|| pid
== INT_MAX
||
235 (*end_ptr
!= '\0' && *end_ptr
!= ','))
239 bpf_map_update_elem(fd
, &tgid
, &val
, BPF_ANY
);
241 } else if (target__has_task(target
)) {
245 fd
= bpf_map__fd(skel
->maps
.task_filter
);
247 for (i
= 0; i
< ntasks
; i
++) {
248 pid
= perf_thread_map__pid(evlist
->core
.threads
, i
);
249 bpf_map_update_elem(fd
, &pid
, &val
, BPF_ANY
);
253 if (evlist__first(evlist
)->cgrp
) {
257 fd
= bpf_map__fd(skel
->maps
.cgroup_filter
);
259 evlist__for_each_entry(evlist
, evsel
) {
260 struct cgroup
*cgrp
= evsel
->cgrp
;
265 if (!cgrp
->id
&& read_cgroup_id(cgrp
) < 0) {
266 pr_err("Failed to read cgroup id of %s\n",
271 bpf_map_update_elem(fd
, &cgrp
->id
, &val
, BPF_ANY
);
275 err
= off_cpu_bpf__attach(skel
);
277 pr_err("Failed to attach off-cpu BPF skeleton\n");
281 if (perf_hooks__set_hook("record_start", off_cpu_start
, evlist
) ||
282 perf_hooks__set_hook("record_end", off_cpu_finish
, evlist
)) {
283 pr_err("Failed to attach off-cpu skeleton\n");
290 off_cpu_bpf__destroy(skel
);
294 int off_cpu_write(struct perf_session
*session
)
298 u64 sample_type
, val
, sid
= 0;
300 struct perf_data_file
*file
= &session
->data
->file
;
301 struct off_cpu_key prev
, key
;
302 union off_cpu_data data
= {
304 .type
= PERF_RECORD_SAMPLE
,
305 .misc
= PERF_RECORD_MISC_USER
,
308 u64 tstamp
= OFF_CPU_TIMESTAMP
;
310 skel
->bss
->enabled
= 0;
312 evsel
= evlist__find_evsel_by_str(session
->evlist
, OFFCPU_EVENT
);
314 pr_err("%s evsel not found\n", OFFCPU_EVENT
);
318 sample_type
= evsel
->core
.attr
.sample_type
;
320 if (sample_type
& ~OFFCPU_SAMPLE_TYPES
) {
321 pr_err("not supported sample type: %llx\n",
322 (unsigned long long)sample_type
);
326 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
)) {
328 sid
= evsel
->core
.id
[0];
331 fd
= bpf_map__fd(skel
->maps
.off_cpu
);
332 stack
= bpf_map__fd(skel
->maps
.stacks
);
333 memset(&prev
, 0, sizeof(prev
));
335 while (!bpf_map_get_next_key(fd
, &prev
, &key
)) {
336 int n
= 1; /* start from perf_event_header */
339 bpf_map_lookup_elem(fd
, &key
, &val
);
341 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
342 data
.array
[n
++] = sid
;
343 if (sample_type
& PERF_SAMPLE_IP
) {
345 data
.array
[n
++] = 0; /* will be updated */
347 if (sample_type
& PERF_SAMPLE_TID
)
348 data
.array
[n
++] = (u64
)key
.pid
<< 32 | key
.tgid
;
349 if (sample_type
& PERF_SAMPLE_TIME
)
350 data
.array
[n
++] = tstamp
;
351 if (sample_type
& PERF_SAMPLE_ID
)
352 data
.array
[n
++] = sid
;
353 if (sample_type
& PERF_SAMPLE_CPU
)
355 if (sample_type
& PERF_SAMPLE_PERIOD
)
356 data
.array
[n
++] = val
;
357 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
360 /* data.array[n] is callchain->nr (updated later) */
361 data
.array
[n
+ 1] = PERF_CONTEXT_USER
;
362 data
.array
[n
+ 2] = 0;
364 bpf_map_lookup_elem(stack
, &key
.stack_id
, &data
.array
[n
+ 2]);
365 while (data
.array
[n
+ 2 + len
])
368 /* update length of callchain */
369 data
.array
[n
] = len
+ 1;
371 /* update sample ip with the first callchain entry */
373 data
.array
[ip_pos
] = data
.array
[n
+ 2];
375 /* calculate sample callchain data array length */
378 if (sample_type
& PERF_SAMPLE_CGROUP
)
379 data
.array
[n
++] = key
.cgroup_id
;
381 size
= n
* sizeof(u64
);
382 data
.hdr
.size
= size
;
385 if (perf_data_file__write(file
, &data
, size
) < 0) {
386 pr_err("failed to write perf data, error: %m\n");
391 /* increase dummy timestamp to sort later samples */