tools/perf/util/bpf_counter.c
Source file repositories/reference/linux-study-clean/tools/perf/util/bpf_counter.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/bpf_counter.c- Extension
.c- Size
- 24943 bytes
- Lines
- 903
- 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
assert.hlimits.hunistd.hsys/file.hsys/resource.hsys/time.hlinux/err.hlinux/list.hlinux/zalloc.hapi/fs/fs.hbpf/bpf.hbpf/btf.hperf/bpf_perf.hbpf_counter.hbpf-utils.hcounts.hdebug.hevsel.hevlist.htarget.hcgroup.hcpumap.hthread_map.hbpf_skel/bpf_prog_profiler.skel.hbpf_skel/bperf_u.hbpf_skel/bperf_leader.skel.hbpf_skel/bperf_follower.skel.h
Detected Declarations
struct bpf_counterfunction set_max_rlimitfunction bpf_link_get_idfunction bpf_link_get_prog_idfunction bpf_map_get_idfunction bperf_trigger_readingfunction bpf_program_profiler__destroyfunction list_for_each_entry_safefunction bpf_program_profiler_load_onefunction bpf_object__for_each_programfunction bpf_program_profiler__loadfunction bpf_program_profiler__enablefunction list_for_each_entryfunction bpf_program_profiler__disablefunction list_for_each_entryfunction bpf_program_profiler__readfunction perf_cpu_map__for_each_idxfunction bpf_program_profiler__install_pefunction list_for_each_entryfunction bperf_attr_map_compatiblefunction bperf_lock_attr_mapfunction bperf_check_targetfunction bperf_reload_leader_programfunction bperf_attach_follower_programfunction bperf__loadfunction bperf_reload_leader_programfunction bperf__install_pefunction bperf_sync_countersfunction bperf__enablefunction bperf__disablefunction bperf__readfunction perf_cpu_map__for_each_cpufunction bperf__destroyfunction bpf_counter_skipfunction bpf_counter__install_pefunction bpf_counter__loadfunction bpf_counter__enablefunction bpf_counter__disablefunction bpf_counter__readfunction bpf_counter__destroy
Annotated Snippet
struct bpf_counter {
void *skel;
struct list_head list;
};
#define ATTR_MAP_SIZE 16
static void *u64_to_ptr(__u64 ptr)
{
return (void *)(unsigned long)ptr;
}
void set_max_rlimit(void)
{
struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
setrlimit(RLIMIT_MEMLOCK, &rinf);
}
static __u32 bpf_link_get_id(int fd)
{
struct bpf_link_info link_info = { .id = 0, };
__u32 link_info_len = sizeof(link_info);
bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
return link_info.id;
}
static __u32 bpf_link_get_prog_id(int fd)
{
struct bpf_link_info link_info = { .id = 0, };
__u32 link_info_len = sizeof(link_info);
bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
return link_info.prog_id;
}
static __u32 bpf_map_get_id(int fd)
{
struct bpf_map_info map_info = { .id = 0, };
__u32 map_info_len = sizeof(map_info);
bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
return map_info.id;
}
/* trigger the leader program on a cpu */
int bperf_trigger_reading(int prog_fd, int cpu)
{
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
.ctx_in = NULL,
.ctx_size_in = 0,
.flags = BPF_F_TEST_RUN_ON_CPU,
.cpu = cpu,
.retval = 0,
);
return bpf_prog_test_run_opts(prog_fd, &opts);
}
static struct bpf_counter *bpf_counter_alloc(void)
{
struct bpf_counter *counter;
counter = zalloc(sizeof(*counter));
if (counter)
INIT_LIST_HEAD(&counter->list);
return counter;
}
static int bpf_program_profiler__destroy(struct evsel *evsel)
{
struct bpf_counter *counter, *tmp;
list_for_each_entry_safe(counter, tmp,
&evsel->bpf_counter_list, list) {
list_del_init(&counter->list);
bpf_prog_profiler_bpf__destroy(counter->skel);
free(counter);
}
assert(list_empty(&evsel->bpf_counter_list));
return 0;
}
static char *bpf_target_prog_name(int tgt_fd)
{
struct bpf_func_info *func_info;
struct perf_bpil *info_linear;
Annotation
- Immediate include surface: `assert.h`, `limits.h`, `unistd.h`, `sys/file.h`, `sys/resource.h`, `sys/time.h`, `linux/err.h`, `linux/list.h`.
- Detected declarations: `struct bpf_counter`, `function set_max_rlimit`, `function bpf_link_get_id`, `function bpf_link_get_prog_id`, `function bpf_map_get_id`, `function bperf_trigger_reading`, `function bpf_program_profiler__destroy`, `function list_for_each_entry_safe`, `function bpf_program_profiler_load_one`, `function bpf_object__for_each_program`.
- 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.