Jiri Olsa | 285a30c | 2019-07-21 13:24:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | b8eca4d | 2019-07-21 13:24:48 +0200 | [diff] [blame] | 2 | #include <errno.h> |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include <sys/syscall.h> |
Jiri Olsa | 285a30c | 2019-07-21 13:24:21 +0200 | [diff] [blame] | 5 | #include <perf/evsel.h> |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 6 | #include <perf/cpumap.h> |
| 7 | #include <perf/threadmap.h> |
Jiri Olsa | 285a30c | 2019-07-21 13:24:21 +0200 | [diff] [blame] | 8 | #include <linux/list.h> |
| 9 | #include <internal/evsel.h> |
Jiri Olsa | 63bd5df | 2019-07-21 13:24:33 +0200 | [diff] [blame] | 10 | #include <linux/zalloc.h> |
Jiri Olsa | b9358ee | 2019-07-21 13:24:36 +0200 | [diff] [blame] | 11 | #include <stdlib.h> |
Jiri Olsa | b8eca4d | 2019-07-21 13:24:48 +0200 | [diff] [blame] | 12 | #include <internal/xyarray.h> |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 13 | #include <internal/cpumap.h> |
Rob Herring | 6cd7075 | 2021-04-14 11:07:37 -0500 | [diff] [blame] | 14 | #include <internal/mmap.h> |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 15 | #include <internal/threadmap.h> |
Jiri Olsa | 5c30af9 | 2019-07-21 13:24:51 +0200 | [diff] [blame] | 16 | #include <internal/lib.h> |
Jiri Olsa | b8eca4d | 2019-07-21 13:24:48 +0200 | [diff] [blame] | 17 | #include <linux/string.h> |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 18 | #include <sys/ioctl.h> |
Rob Herring | 6cd7075 | 2021-04-14 11:07:37 -0500 | [diff] [blame] | 19 | #include <sys/mman.h> |
Jiri Olsa | b04c597 | 2019-07-21 13:24:24 +0200 | [diff] [blame] | 20 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 21 | void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr) |
Jiri Olsa | b04c597 | 2019-07-21 13:24:24 +0200 | [diff] [blame] | 22 | { |
| 23 | INIT_LIST_HEAD(&evsel->node); |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 24 | evsel->attr = *attr; |
Jiri Olsa | b04c597 | 2019-07-21 13:24:24 +0200 | [diff] [blame] | 25 | } |
Jiri Olsa | 63bd5df | 2019-07-21 13:24:33 +0200 | [diff] [blame] | 26 | |
| 27 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr) |
| 28 | { |
| 29 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); |
| 30 | |
| 31 | if (evsel != NULL) |
| 32 | perf_evsel__init(evsel, attr); |
| 33 | |
| 34 | return evsel; |
| 35 | } |
Jiri Olsa | b9358ee | 2019-07-21 13:24:36 +0200 | [diff] [blame] | 36 | |
| 37 | void perf_evsel__delete(struct perf_evsel *evsel) |
| 38 | { |
| 39 | free(evsel); |
| 40 | } |
Jiri Olsa | b8eca4d | 2019-07-21 13:24:48 +0200 | [diff] [blame] | 41 | |
| 42 | #define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y)) |
Rob Herring | 6cd7075 | 2021-04-14 11:07:37 -0500 | [diff] [blame] | 43 | #define MMAP(e, x, y) (e->mmap ? ((struct perf_mmap *) xyarray__entry(e->mmap, x, y)) : NULL) |
Jiri Olsa | b8eca4d | 2019-07-21 13:24:48 +0200 | [diff] [blame] | 44 | |
| 45 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 46 | { |
| 47 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
| 48 | |
| 49 | if (evsel->fd) { |
| 50 | int cpu, thread; |
| 51 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 52 | for (thread = 0; thread < nthreads; thread++) { |
| 53 | FD(evsel, cpu, thread) = -1; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return evsel->fd != NULL ? 0 : -ENOMEM; |
| 59 | } |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 60 | |
Rob Herring | 6cd7075 | 2021-04-14 11:07:37 -0500 | [diff] [blame] | 61 | static int perf_evsel__alloc_mmap(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 62 | { |
| 63 | evsel->mmap = xyarray__new(ncpus, nthreads, sizeof(struct perf_mmap)); |
| 64 | |
| 65 | return evsel->mmap != NULL ? 0 : -ENOMEM; |
| 66 | } |
| 67 | |
Jiri Olsa | 50a4e6f | 2019-07-21 13:24:49 +0200 | [diff] [blame] | 68 | static int |
| 69 | sys_perf_event_open(struct perf_event_attr *attr, |
| 70 | pid_t pid, int cpu, int group_fd, |
| 71 | unsigned long flags) |
| 72 | { |
| 73 | return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); |
| 74 | } |
| 75 | |
| 76 | int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus, |
| 77 | struct perf_thread_map *threads) |
| 78 | { |
| 79 | int cpu, thread, err = 0; |
| 80 | |
| 81 | if (cpus == NULL) { |
| 82 | static struct perf_cpu_map *empty_cpu_map; |
| 83 | |
| 84 | if (empty_cpu_map == NULL) { |
| 85 | empty_cpu_map = perf_cpu_map__dummy_new(); |
| 86 | if (empty_cpu_map == NULL) |
| 87 | return -ENOMEM; |
| 88 | } |
| 89 | |
| 90 | cpus = empty_cpu_map; |
| 91 | } |
| 92 | |
| 93 | if (threads == NULL) { |
| 94 | static struct perf_thread_map *empty_thread_map; |
| 95 | |
| 96 | if (empty_thread_map == NULL) { |
| 97 | empty_thread_map = perf_thread_map__new_dummy(); |
| 98 | if (empty_thread_map == NULL) |
| 99 | return -ENOMEM; |
| 100 | } |
| 101 | |
| 102 | threads = empty_thread_map; |
| 103 | } |
| 104 | |
| 105 | if (evsel->fd == NULL && |
| 106 | perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 110 | for (thread = 0; thread < threads->nr; thread++) { |
| 111 | int fd; |
| 112 | |
| 113 | fd = sys_perf_event_open(&evsel->attr, |
| 114 | threads->map[thread].pid, |
| 115 | cpus->map[cpu], -1, 0); |
| 116 | |
| 117 | if (fd < 0) |
| 118 | return -errno; |
| 119 | |
| 120 | FD(evsel, cpu, thread) = fd; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return err; |
| 125 | } |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 126 | |
Andi Kleen | 99d6141 | 2019-11-20 16:15:16 -0800 | [diff] [blame] | 127 | static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu) |
| 128 | { |
| 129 | int thread; |
| 130 | |
| 131 | for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) { |
| 132 | if (FD(evsel, cpu, thread) >= 0) |
| 133 | close(FD(evsel, cpu, thread)); |
| 134 | FD(evsel, cpu, thread) = -1; |
| 135 | } |
| 136 | } |
| 137 | |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 138 | void perf_evsel__close_fd(struct perf_evsel *evsel) |
| 139 | { |
Andi Kleen | 99d6141 | 2019-11-20 16:15:16 -0800 | [diff] [blame] | 140 | int cpu; |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 141 | |
| 142 | for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) |
Andi Kleen | 99d6141 | 2019-11-20 16:15:16 -0800 | [diff] [blame] | 143 | perf_evsel__close_fd_cpu(evsel, cpu); |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
| 147 | { |
| 148 | xyarray__delete(evsel->fd); |
| 149 | evsel->fd = NULL; |
| 150 | } |
| 151 | |
| 152 | void perf_evsel__close(struct perf_evsel *evsel) |
| 153 | { |
| 154 | if (evsel->fd == NULL) |
| 155 | return; |
| 156 | |
| 157 | perf_evsel__close_fd(evsel); |
| 158 | perf_evsel__free_fd(evsel); |
| 159 | } |
Jiri Olsa | 5c30af9 | 2019-07-21 13:24:51 +0200 | [diff] [blame] | 160 | |
Andi Kleen | 99d6141 | 2019-11-20 16:15:16 -0800 | [diff] [blame] | 161 | void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu) |
| 162 | { |
| 163 | if (evsel->fd == NULL) |
| 164 | return; |
| 165 | |
| 166 | perf_evsel__close_fd_cpu(evsel, cpu); |
| 167 | } |
| 168 | |
Rob Herring | 6cd7075 | 2021-04-14 11:07:37 -0500 | [diff] [blame] | 169 | void perf_evsel__munmap(struct perf_evsel *evsel) |
| 170 | { |
| 171 | int cpu, thread; |
| 172 | |
| 173 | if (evsel->fd == NULL || evsel->mmap == NULL) |
| 174 | return; |
| 175 | |
| 176 | for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) { |
| 177 | for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { |
| 178 | int fd = FD(evsel, cpu, thread); |
| 179 | struct perf_mmap *map = MMAP(evsel, cpu, thread); |
| 180 | |
| 181 | if (fd < 0) |
| 182 | continue; |
| 183 | |
| 184 | perf_mmap__munmap(map); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | xyarray__delete(evsel->mmap); |
| 189 | evsel->mmap = NULL; |
| 190 | } |
| 191 | |
| 192 | int perf_evsel__mmap(struct perf_evsel *evsel, int pages) |
| 193 | { |
| 194 | int ret, cpu, thread; |
| 195 | struct perf_mmap_param mp = { |
| 196 | .prot = PROT_READ | PROT_WRITE, |
| 197 | .mask = (pages * page_size) - 1, |
| 198 | }; |
| 199 | |
| 200 | if (evsel->fd == NULL || evsel->mmap) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | if (perf_evsel__alloc_mmap(evsel, xyarray__max_x(evsel->fd), xyarray__max_y(evsel->fd)) < 0) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) { |
| 207 | for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { |
| 208 | int fd = FD(evsel, cpu, thread); |
| 209 | struct perf_mmap *map = MMAP(evsel, cpu, thread); |
| 210 | |
| 211 | if (fd < 0) |
| 212 | continue; |
| 213 | |
| 214 | perf_mmap__init(map, NULL, false, NULL); |
| 215 | |
| 216 | ret = perf_mmap__mmap(map, &mp, fd, cpu); |
| 217 | if (ret) { |
| 218 | perf_evsel__munmap(evsel); |
| 219 | return ret; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) |
| 228 | { |
| 229 | if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) |
| 230 | return NULL; |
| 231 | |
| 232 | return MMAP(evsel, cpu, thread)->base; |
| 233 | } |
| 234 | |
Jiri Olsa | 5c30af9 | 2019-07-21 13:24:51 +0200 | [diff] [blame] | 235 | int perf_evsel__read_size(struct perf_evsel *evsel) |
| 236 | { |
| 237 | u64 read_format = evsel->attr.read_format; |
| 238 | int entry = sizeof(u64); /* value */ |
| 239 | int size = 0; |
| 240 | int nr = 1; |
| 241 | |
| 242 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 243 | size += sizeof(u64); |
| 244 | |
| 245 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 246 | size += sizeof(u64); |
| 247 | |
| 248 | if (read_format & PERF_FORMAT_ID) |
| 249 | entry += sizeof(u64); |
| 250 | |
| 251 | if (read_format & PERF_FORMAT_GROUP) { |
| 252 | nr = evsel->nr_members; |
| 253 | size += sizeof(u64); |
| 254 | } |
| 255 | |
| 256 | size += entry * nr; |
| 257 | return size; |
| 258 | } |
| 259 | |
| 260 | int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, |
| 261 | struct perf_counts_values *count) |
| 262 | { |
| 263 | size_t size = perf_evsel__read_size(evsel); |
| 264 | |
| 265 | memset(count, 0, sizeof(*count)); |
| 266 | |
| 267 | if (FD(evsel, cpu, thread) < 0) |
| 268 | return -EINVAL; |
| 269 | |
Rob Herring | 47d01e7 | 2021-04-14 11:07:39 -0500 | [diff] [blame] | 270 | if (MMAP(evsel, cpu, thread) && |
| 271 | !perf_mmap__read_self(MMAP(evsel, cpu, thread), count)) |
| 272 | return 0; |
| 273 | |
Jiri Olsa | 5c30af9 | 2019-07-21 13:24:51 +0200 | [diff] [blame] | 274 | if (readn(FD(evsel, cpu, thread), count->values, size) <= 0) |
| 275 | return -errno; |
| 276 | |
| 277 | return 0; |
| 278 | } |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 279 | |
| 280 | static int perf_evsel__run_ioctl(struct perf_evsel *evsel, |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 281 | int ioc, void *arg, |
| 282 | int cpu) |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 283 | { |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 284 | int thread; |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 285 | |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 286 | for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { |
| 287 | int fd = FD(evsel, cpu, thread), |
| 288 | err = ioctl(fd, ioc, arg); |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 289 | |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 290 | if (err) |
| 291 | return err; |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 297 | int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu) |
| 298 | { |
| 299 | return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu); |
| 300 | } |
| 301 | |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 302 | int perf_evsel__enable(struct perf_evsel *evsel) |
| 303 | { |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 304 | int i; |
| 305 | int err = 0; |
| 306 | |
| 307 | for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++) |
| 308 | err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i); |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu) |
| 313 | { |
| 314 | return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu); |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int perf_evsel__disable(struct perf_evsel *evsel) |
| 318 | { |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 319 | int i; |
| 320 | int err = 0; |
| 321 | |
| 322 | for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++) |
| 323 | err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i); |
| 324 | return err; |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter) |
| 328 | { |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 329 | int err = 0, i; |
| 330 | |
| 331 | for (i = 0; i < evsel->cpus->nr && !err; i++) |
| 332 | err = perf_evsel__run_ioctl(evsel, |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 333 | PERF_EVENT_IOC_SET_FILTER, |
Andi Kleen | 363fb12 | 2019-11-20 16:15:21 -0800 | [diff] [blame] | 334 | (void *)filter, i); |
| 335 | return err; |
Jiri Olsa | a00571f | 2019-07-21 13:24:52 +0200 | [diff] [blame] | 336 | } |
Jiri Olsa | 0ff1a0f | 2019-07-21 13:24:54 +0200 | [diff] [blame] | 337 | |
| 338 | struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel) |
| 339 | { |
| 340 | return evsel->cpus; |
| 341 | } |
| 342 | |
| 343 | struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel) |
| 344 | { |
| 345 | return evsel->threads; |
| 346 | } |
Jiri Olsa | 384c4ad1 | 2019-07-21 13:24:57 +0200 | [diff] [blame] | 347 | |
| 348 | struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel) |
| 349 | { |
| 350 | return &evsel->attr; |
| 351 | } |
Jiri Olsa | 70c2036 | 2019-09-03 10:34:29 +0200 | [diff] [blame] | 352 | |
| 353 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 354 | { |
| 355 | if (ncpus == 0 || nthreads == 0) |
| 356 | return 0; |
| 357 | |
| 358 | if (evsel->system_wide) |
| 359 | nthreads = 1; |
| 360 | |
| 361 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
| 362 | if (evsel->sample_id == NULL) |
| 363 | return -ENOMEM; |
| 364 | |
| 365 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); |
| 366 | if (evsel->id == NULL) { |
| 367 | xyarray__delete(evsel->sample_id); |
| 368 | evsel->sample_id = NULL; |
| 369 | return -ENOMEM; |
| 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | void perf_evsel__free_id(struct perf_evsel *evsel) |
| 376 | { |
| 377 | xyarray__delete(evsel->sample_id); |
| 378 | evsel->sample_id = NULL; |
| 379 | zfree(&evsel->id); |
| 380 | evsel->ids = 0; |
| 381 | } |