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/threadmap.h>
15 #include <internal/lib.h>
16 #include <linux/string.h>
17 #include <sys/ioctl.h>
19 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
21 INIT_LIST_HEAD(&evsel->node);
25 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
27 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
30 perf_evsel__init(evsel, attr);
35 void perf_evsel__delete(struct perf_evsel *evsel)
40 #define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y))
42 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
44 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
48 for (cpu = 0; cpu < ncpus; cpu++) {
49 for (thread = 0; thread < nthreads; thread++) {
50 FD(evsel, cpu, thread) = -1;
55 return evsel->fd != NULL ? 0 : -ENOMEM;
59 sys_perf_event_open(struct perf_event_attr *attr,
60 pid_t pid, int cpu, int group_fd,
63 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
66 int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
67 struct perf_thread_map *threads)
69 int cpu, thread, err = 0;
72 static struct perf_cpu_map *empty_cpu_map;
74 if (empty_cpu_map == NULL) {
75 empty_cpu_map = perf_cpu_map__dummy_new();
76 if (empty_cpu_map == NULL)
83 if (threads == NULL) {
84 static struct perf_thread_map *empty_thread_map;
86 if (empty_thread_map == NULL) {
87 empty_thread_map = perf_thread_map__new_dummy();
88 if (empty_thread_map == NULL)
92 threads = empty_thread_map;
95 if (evsel->fd == NULL &&
96 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
99 for (cpu = 0; cpu < cpus->nr; cpu++) {
100 for (thread = 0; thread < threads->nr; thread++) {
103 fd = sys_perf_event_open(&evsel->attr,
104 threads->map[thread].pid,
105 cpus->map[cpu], -1, 0);
110 FD(evsel, cpu, thread) = fd;
117 void perf_evsel__close_fd(struct perf_evsel *evsel)
121 for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++)
122 for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
123 close(FD(evsel, cpu, thread));
124 FD(evsel, cpu, thread) = -1;
128 void perf_evsel__free_fd(struct perf_evsel *evsel)
130 xyarray__delete(evsel->fd);
134 void perf_evsel__close(struct perf_evsel *evsel)
136 if (evsel->fd == NULL)
139 perf_evsel__close_fd(evsel);
140 perf_evsel__free_fd(evsel);
143 int perf_evsel__read_size(struct perf_evsel *evsel)
145 u64 read_format = evsel->attr.read_format;
146 int entry = sizeof(u64); /* value */
150 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
153 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
156 if (read_format & PERF_FORMAT_ID)
157 entry += sizeof(u64);
159 if (read_format & PERF_FORMAT_GROUP) {
160 nr = evsel->nr_members;
168 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
169 struct perf_counts_values *count)
171 size_t size = perf_evsel__read_size(evsel);
173 memset(count, 0, sizeof(*count));
175 if (FD(evsel, cpu, thread) < 0)
178 if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
184 static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
189 for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) {
190 for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
191 int fd = FD(evsel, cpu, thread),
192 err = ioctl(fd, ioc, arg);
202 int perf_evsel__enable(struct perf_evsel *evsel)
204 return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0);
207 int perf_evsel__disable(struct perf_evsel *evsel)
209 return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0);
212 int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
214 return perf_evsel__run_ioctl(evsel,
215 PERF_EVENT_IOC_SET_FILTER,
219 struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
224 struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel)
226 return evsel->threads;
229 struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel)
234 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
236 if (ncpus == 0 || nthreads == 0)
239 if (evsel->system_wide)
242 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
243 if (evsel->sample_id == NULL)
246 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
247 if (evsel->id == NULL) {
248 xyarray__delete(evsel->sample_id);
249 evsel->sample_id = NULL;
256 void perf_evsel__free_id(struct perf_evsel *evsel)
258 xyarray__delete(evsel->sample_id);
259 evsel->sample_id = NULL;