tools/lib/perf/evsel.c
Source file repositories/reference/linux-study-clean/tools/lib/perf/evsel.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/perf/evsel.c- Extension
.c- Size
- 13852 bytes
- Lines
- 619
- 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
errno.hunistd.hsys/syscall.hperf/evsel.hperf/cpumap.hperf/threadmap.hlinux/hash.hlinux/list.hinternal/evsel.hlinux/zalloc.hstdlib.hinternal/xyarray.hinternal/cpumap.hinternal/mmap.hinternal/threadmap.hinternal/lib.hlinux/string.hsys/ioctl.hsys/mman.hasm/bug.h
Detected Declarations
function perf_evsel__initfunction perf_evsel__exitfunction perf_evsel__deletefunction perf_evsel__alloc_fdfunction perf_evsel__alloc_mmapfunction sys_perf_event_openfunction get_group_fdfunction perf_evsel__openfunction perf_cpu_map__for_each_cpufunction perf_evsel__close_fd_cpufunction perf_evsel__close_fdfunction perf_evsel__free_fdfunction perf_evsel__closefunction perf_evsel__close_cpufunction perf_evsel__munmapfunction perf_evsel__mmapfunction perf_evsel__read_sizefunction perf_evsel__read_groupfunction perf_evsel__adjust_valuesfunction perf_evsel__readfunction perf_evsel__ioctlfunction perf_evsel__run_ioctlfunction perf_evsel__enable_cpufunction perf_evsel__enable_threadfunction perf_cpu_map__for_each_cpufunction perf_evsel__enablefunction perf_evsel__disable_cpufunction perf_evsel__disablefunction perf_evsel__apply_filterfunction perf_evsel__alloc_idfunction perf_evsel__free_idfunction perf_evsel_for_each_per_thread_period_safefunction perf_evsel__attr_has_per_thread_sample_periodfunction perf_counts_values__scale
Annotated Snippet
if (empty_cpu_map == NULL) {
empty_cpu_map = perf_cpu_map__new_any_cpu();
if (empty_cpu_map == NULL)
return -ENOMEM;
}
cpus = empty_cpu_map;
}
if (threads == NULL) {
static struct perf_thread_map *empty_thread_map;
if (empty_thread_map == NULL) {
empty_thread_map = perf_thread_map__new_dummy();
if (empty_thread_map == NULL)
return -ENOMEM;
}
threads = empty_thread_map;
}
if (evsel->fd == NULL &&
perf_evsel__alloc_fd(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
return -ENOMEM;
perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
for (thread = 0; thread < threads->nr; thread++) {
int fd, group_fd, *evsel_fd;
evsel_fd = FD(evsel, idx, thread);
if (evsel_fd == NULL) {
err = -EINVAL;
goto out;
}
err = get_group_fd(evsel, idx, thread, &group_fd);
if (err < 0)
goto out;
fd = sys_perf_event_open(&evsel->attr,
threads->map[thread].pid,
cpu, group_fd, 0);
if (fd < 0) {
err = -errno;
goto out;
}
*evsel_fd = fd;
}
}
out:
if (err)
perf_evsel__close(evsel);
return err;
}
static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx)
{
int thread;
for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
int *fd = FD(evsel, cpu_map_idx, thread);
if (fd && *fd >= 0) {
close(*fd);
*fd = -1;
}
}
}
void perf_evsel__close_fd(struct perf_evsel *evsel)
{
for (int idx = 0; idx < xyarray__max_x(evsel->fd); idx++)
perf_evsel__close_fd_cpu(evsel, idx);
}
void perf_evsel__free_fd(struct perf_evsel *evsel)
{
xyarray__delete(evsel->fd);
evsel->fd = NULL;
}
void perf_evsel__close(struct perf_evsel *evsel)
{
if (evsel->fd == NULL)
return;
perf_evsel__close_fd(evsel);
Annotation
- Immediate include surface: `errno.h`, `unistd.h`, `sys/syscall.h`, `perf/evsel.h`, `perf/cpumap.h`, `perf/threadmap.h`, `linux/hash.h`, `linux/list.h`.
- Detected declarations: `function perf_evsel__init`, `function perf_evsel__exit`, `function perf_evsel__delete`, `function perf_evsel__alloc_fd`, `function perf_evsel__alloc_mmap`, `function sys_perf_event_open`, `function get_group_fd`, `function perf_evsel__open`, `function perf_cpu_map__for_each_cpu`, `function perf_evsel__close_fd_cpu`.
- 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.