1 // SPDX-License-Identifier: GPL-2.0
4 #include <sys/syscall.h>
5 #include <perf/evsel.h>
6 #include <perf/cpumap.h>
7 #include <perf/threadmap.h>
8 #include <linux/list.h>
9 #include <internal/evsel.h>
10 #include <linux/zalloc.h>
12 #include <internal/xyarray.h>
13 #include <internal/cpumap.h>
14 #include <internal/mmap.h>
15 #include <internal/threadmap.h>
16 #include <internal/lib.h>
17 #include <linux/string.h>
18 #include <sys/ioctl.h>
22 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr,
25 INIT_LIST_HEAD(&evsel->node);
28 evsel->leader = evsel;
31 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
33 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
36 perf_evsel__init(evsel, attr, 0);
41 void perf_evsel__delete(struct perf_evsel *evsel)
46 #define FD(_evsel, _cpu_map_idx, _thread) \
47 ((int *)xyarray__entry(_evsel->fd, _cpu_map_idx, _thread))
48 #define MMAP(_evsel, _cpu_map_idx, _thread) \
49 (_evsel->mmap ? ((struct perf_mmap *) xyarray__entry(_evsel->mmap, _cpu_map_idx, _thread)) \
52 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
54 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
59 for (idx = 0; idx < ncpus; idx++) {
60 for (thread = 0; thread < nthreads; thread++) {
61 int *fd = FD(evsel, idx, thread);
69 return evsel->fd != NULL ? 0 : -ENOMEM;
72 static int perf_evsel__alloc_mmap(struct perf_evsel *evsel, int ncpus, int nthreads)
74 evsel->mmap = xyarray__new(ncpus, nthreads, sizeof(struct perf_mmap));
76 return evsel->mmap != NULL ? 0 : -ENOMEM;
80 sys_perf_event_open(struct perf_event_attr *attr,
81 pid_t pid, struct perf_cpu cpu, int group_fd,
84 return syscall(__NR_perf_event_open, attr, pid, cpu.cpu, group_fd, flags);
87 static int get_group_fd(struct perf_evsel *evsel, int cpu_map_idx, int thread, int *group_fd)
89 struct perf_evsel *leader = evsel->leader;
92 if (evsel == leader) {
98 * Leader must be already processed/open,
104 fd = FD(leader, cpu_map_idx, thread);
105 if (fd == NULL || *fd == -1)
113 int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
114 struct perf_thread_map *threads)
117 int idx, thread, err = 0;
120 static struct perf_cpu_map *empty_cpu_map;
122 if (empty_cpu_map == NULL) {
123 empty_cpu_map = perf_cpu_map__dummy_new();
124 if (empty_cpu_map == NULL)
128 cpus = empty_cpu_map;
131 if (threads == NULL) {
132 static struct perf_thread_map *empty_thread_map;
134 if (empty_thread_map == NULL) {
135 empty_thread_map = perf_thread_map__new_dummy();
136 if (empty_thread_map == NULL)
140 threads = empty_thread_map;
143 if (evsel->fd == NULL &&
144 perf_evsel__alloc_fd(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
147 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
148 for (thread = 0; thread < threads->nr; thread++) {
149 int fd, group_fd, *evsel_fd;
151 evsel_fd = FD(evsel, idx, thread);
152 if (evsel_fd == NULL) {
157 err = get_group_fd(evsel, idx, thread, &group_fd);
161 fd = sys_perf_event_open(&evsel->attr,
162 threads->map[thread].pid,
175 perf_evsel__close(evsel);
180 static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx)
184 for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
185 int *fd = FD(evsel, cpu_map_idx, thread);
187 if (fd && *fd >= 0) {
194 void perf_evsel__close_fd(struct perf_evsel *evsel)
196 for (int idx = 0; idx < xyarray__max_x(evsel->fd); idx++)
197 perf_evsel__close_fd_cpu(evsel, idx);
200 void perf_evsel__free_fd(struct perf_evsel *evsel)
202 xyarray__delete(evsel->fd);
206 void perf_evsel__close(struct perf_evsel *evsel)
208 if (evsel->fd == NULL)
211 perf_evsel__close_fd(evsel);
212 perf_evsel__free_fd(evsel);
215 void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu_map_idx)
217 if (evsel->fd == NULL)
220 perf_evsel__close_fd_cpu(evsel, cpu_map_idx);
223 void perf_evsel__munmap(struct perf_evsel *evsel)
227 if (evsel->fd == NULL || evsel->mmap == NULL)
230 for (idx = 0; idx < xyarray__max_x(evsel->fd); idx++) {
231 for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
232 int *fd = FD(evsel, idx, thread);
234 if (fd == NULL || *fd < 0)
237 perf_mmap__munmap(MMAP(evsel, idx, thread));
241 xyarray__delete(evsel->mmap);
245 int perf_evsel__mmap(struct perf_evsel *evsel, int pages)
247 int ret, idx, thread;
248 struct perf_mmap_param mp = {
249 .prot = PROT_READ | PROT_WRITE,
250 .mask = (pages * page_size) - 1,
253 if (evsel->fd == NULL || evsel->mmap)
256 if (perf_evsel__alloc_mmap(evsel, xyarray__max_x(evsel->fd), xyarray__max_y(evsel->fd)) < 0)
259 for (idx = 0; idx < xyarray__max_x(evsel->fd); idx++) {
260 for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
261 int *fd = FD(evsel, idx, thread);
262 struct perf_mmap *map;
263 struct perf_cpu cpu = perf_cpu_map__cpu(evsel->cpus, idx);
265 if (fd == NULL || *fd < 0)
268 map = MMAP(evsel, idx, thread);
269 perf_mmap__init(map, NULL, false, NULL);
271 ret = perf_mmap__mmap(map, &mp, *fd, cpu);
273 perf_evsel__munmap(evsel);
282 void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu_map_idx, int thread)
284 int *fd = FD(evsel, cpu_map_idx, thread);
286 if (fd == NULL || *fd < 0 || MMAP(evsel, cpu_map_idx, thread) == NULL)
289 return MMAP(evsel, cpu_map_idx, thread)->base;
292 int perf_evsel__read_size(struct perf_evsel *evsel)
294 u64 read_format = evsel->attr.read_format;
295 int entry = sizeof(u64); /* value */
299 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
302 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
305 if (read_format & PERF_FORMAT_ID)
306 entry += sizeof(u64);
308 if (read_format & PERF_FORMAT_LOST)
309 entry += sizeof(u64);
311 if (read_format & PERF_FORMAT_GROUP) {
312 nr = evsel->nr_members;
320 /* This only reads values for the leader */
321 static int perf_evsel__read_group(struct perf_evsel *evsel, int cpu_map_idx,
322 int thread, struct perf_counts_values *count)
324 size_t size = perf_evsel__read_size(evsel);
325 int *fd = FD(evsel, cpu_map_idx, thread);
326 u64 read_format = evsel->attr.read_format;
330 if (fd == NULL || *fd < 0)
333 data = calloc(1, size);
337 if (readn(*fd, data, size) <= 0) {
343 * This reads only the leader event intentionally since we don't have
344 * perf counts values for sibling events.
346 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
347 count->ena = data[idx++];
348 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
349 count->run = data[idx++];
351 /* value is always available */
352 count->val = data[idx++];
353 if (read_format & PERF_FORMAT_ID)
354 count->id = data[idx++];
355 if (read_format & PERF_FORMAT_LOST)
356 count->lost = data[idx++];
363 * The perf read format is very flexible. It needs to set the proper
364 * values according to the read format.
366 static void perf_evsel__adjust_values(struct perf_evsel *evsel, u64 *buf,
367 struct perf_counts_values *count)
369 u64 read_format = evsel->attr.read_format;
372 count->val = buf[n++];
374 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
375 count->ena = buf[n++];
377 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
378 count->run = buf[n++];
380 if (read_format & PERF_FORMAT_ID)
381 count->id = buf[n++];
383 if (read_format & PERF_FORMAT_LOST)
384 count->lost = buf[n++];
387 int perf_evsel__read(struct perf_evsel *evsel, int cpu_map_idx, int thread,
388 struct perf_counts_values *count)
390 size_t size = perf_evsel__read_size(evsel);
391 int *fd = FD(evsel, cpu_map_idx, thread);
392 u64 read_format = evsel->attr.read_format;
393 struct perf_counts_values buf;
395 memset(count, 0, sizeof(*count));
397 if (fd == NULL || *fd < 0)
400 if (read_format & PERF_FORMAT_GROUP)
401 return perf_evsel__read_group(evsel, cpu_map_idx, thread, count);
403 if (MMAP(evsel, cpu_map_idx, thread) &&
404 !(read_format & (PERF_FORMAT_ID | PERF_FORMAT_LOST)) &&
405 !perf_mmap__read_self(MMAP(evsel, cpu_map_idx, thread), count))
408 if (readn(*fd, buf.values, size) <= 0)
411 perf_evsel__adjust_values(evsel, buf.values, count);
415 static int perf_evsel__ioctl(struct perf_evsel *evsel, int ioc, void *arg,
416 int cpu_map_idx, int thread)
418 int *fd = FD(evsel, cpu_map_idx, thread);
420 if (fd == NULL || *fd < 0)
423 return ioctl(*fd, ioc, arg);
426 static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
432 for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
433 int err = perf_evsel__ioctl(evsel, ioc, arg, cpu_map_idx, thread);
442 int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
444 return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu_map_idx);
447 int perf_evsel__enable_thread(struct perf_evsel *evsel, int thread)
449 struct perf_cpu cpu __maybe_unused;
453 perf_cpu_map__for_each_cpu(cpu, idx, evsel->cpus) {
454 err = perf_evsel__ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, idx, thread);
462 int perf_evsel__enable(struct perf_evsel *evsel)
467 for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
468 err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i);
472 int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
474 return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu_map_idx);
477 int perf_evsel__disable(struct perf_evsel *evsel)
482 for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
483 err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i);
487 int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
491 for (i = 0; i < perf_cpu_map__nr(evsel->cpus) && !err; i++)
492 err = perf_evsel__run_ioctl(evsel,
493 PERF_EVENT_IOC_SET_FILTER,
498 struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
503 struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel)
505 return evsel->threads;
508 struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel)
513 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
515 if (ncpus == 0 || nthreads == 0)
518 if (evsel->system_wide)
521 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
522 if (evsel->sample_id == NULL)
525 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
526 if (evsel->id == NULL) {
527 xyarray__delete(evsel->sample_id);
528 evsel->sample_id = NULL;
535 void perf_evsel__free_id(struct perf_evsel *evsel)
537 xyarray__delete(evsel->sample_id);
538 evsel->sample_id = NULL;
543 void perf_counts_values__scale(struct perf_counts_values *count,
544 bool scale, __s8 *pscaled)
549 if (count->run == 0) {
552 } else if (count->run < count->ena) {
554 count->val = (u64)((double)count->val * count->ena / count->run);