tools/perf/util/bpf_off_cpu.c
Source file repositories/reference/linux-study-clean/tools/perf/util/bpf_off_cpu.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/bpf_off_cpu.c- Extension
.c- Size
- 10193 bytes
- Lines
- 429
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
util/bpf_counter.hutil/debug.hutil/evsel.hutil/evlist.hutil/off_cpu.hutil/perf-hooks.hutil/record.hutil/session.hutil/target.hutil/cpumap.hutil/thread_map.hutil/cgroup.hutil/strlist.hbpf/bpf.hbpf/btf.hinternal/xyarray.hlinux/time64.hbpf_skel/off_cpu.skel.h
Detected Declarations
struct off_cpu_keyfunction off_cpu_configfunction evlist__for_each_entryfunction off_cpu_startfunction perf_thread_map__pidfunction perf_cpu_map__for_each_cpufunction off_cpu_finishfunction check_sched_switch_argsfunction off_cpu_preparefunction strlist__for_each_entryfunction strlist__for_each_entryfunction evlist__for_each_entryfunction off_cpu_write
Annotated Snippet
struct off_cpu_key {
u32 pid;
u32 tgid;
u32 stack_id;
u32 state;
u64 cgroup_id;
};
union off_cpu_data {
struct perf_event_header hdr;
u64 array[1024 / sizeof(u64)];
};
static u64 off_cpu_raw[MAX_STACKS + 5];
static int off_cpu_config(struct evlist *evlist)
{
char off_cpu_event[64];
struct evsel *evsel;
scnprintf(off_cpu_event, sizeof(off_cpu_event), "bpf-output/name=%s/", OFFCPU_EVENT);
if (parse_event(evlist, off_cpu_event)) {
pr_err("Failed to open off-cpu event\n");
return -1;
}
evlist__for_each_entry(evlist, evsel) {
if (evsel__is_offcpu_event(evsel)) {
evsel->core.system_wide = true;
break;
}
}
return 0;
}
static void off_cpu_start(void *arg)
{
struct evlist *evlist = arg;
struct evsel *evsel;
struct perf_cpu pcpu;
unsigned int i;
/* update task filter for the given workload */
if (skel->rodata->has_task && skel->rodata->uses_tgid &&
perf_thread_map__pid(evlist->core.threads, 0) != -1) {
int fd;
u32 pid;
u8 val = 1;
fd = bpf_map__fd(skel->maps.task_filter);
pid = perf_thread_map__pid(evlist->core.threads, 0);
bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
}
/* update BPF perf_event map */
evsel = evlist__find_evsel_by_str(evlist, OFFCPU_EVENT);
if (evsel == NULL) {
pr_err("%s evsel not found\n", OFFCPU_EVENT);
return;
}
perf_cpu_map__for_each_cpu(pcpu, i, evsel->core.cpus) {
int err;
int cpu_nr = pcpu.cpu;
err = bpf_map__update_elem(skel->maps.offcpu_output, &cpu_nr, sizeof(int),
xyarray__entry(evsel->core.fd, cpu_nr, 0),
sizeof(int), BPF_ANY);
if (err) {
pr_err("Failed to update perf event map for direct off-cpu dumping\n");
return;
}
}
skel->bss->enabled = 1;
}
static void off_cpu_finish(void *arg __maybe_unused)
{
skel->bss->enabled = 0;
off_cpu_bpf__destroy(skel);
}
/* v5.18 kernel added prev_state arg, so it needs to check the signature */
static void check_sched_switch_args(void)
{
struct btf *btf = btf__load_vmlinux_btf();
const struct btf_type *t1, *t2, *t3;
u32 type_id;
Annotation
- Immediate include surface: `util/bpf_counter.h`, `util/debug.h`, `util/evsel.h`, `util/evlist.h`, `util/off_cpu.h`, `util/perf-hooks.h`, `util/record.h`, `util/session.h`.
- Detected declarations: `struct off_cpu_key`, `function off_cpu_config`, `function evlist__for_each_entry`, `function off_cpu_start`, `function perf_thread_map__pid`, `function perf_cpu_map__for_each_cpu`, `function off_cpu_finish`, `function check_sched_switch_args`, `function off_cpu_prepare`, `function strlist__for_each_entry`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.