1 // SPDX-License-Identifier: GPL-2.0 2 #include <perf/evlist.h> 3 #include <perf/evsel.h> 4 #include <linux/bitops.h> 5 #include <linux/list.h> 6 #include <linux/hash.h> 7 #include <sys/ioctl.h> 8 #include <internal/evlist.h> 9 #include <internal/evsel.h> 10 #include <internal/xyarray.h> 11 #include <internal/mmap.h> 12 #include <internal/cpumap.h> 13 #include <internal/threadmap.h> 14 #include <internal/lib.h> 15 #include <linux/zalloc.h> 16 #include <stdlib.h> 17 #include <errno.h> 18 #include <unistd.h> 19 #include <fcntl.h> 20 #include <signal.h> 21 #include <poll.h> 22 #include <sys/mman.h> 23 #include <perf/cpumap.h> 24 #include <perf/threadmap.h> 25 #include <api/fd/array.h> 26 #include "internal.h" 27 28 void perf_evlist__init(struct perf_evlist *evlist) 29 { 30 INIT_LIST_HEAD(&evlist->entries); 31 evlist->nr_entries = 0; 32 fdarray__init(&evlist->pollfd, 64); 33 perf_evlist__reset_id_hash(evlist); 34 } 35 36 static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 37 struct perf_evsel *evsel) 38 { 39 /* 40 * We already have cpus for evsel (via PMU sysfs) so 41 * keep it, if there's no target cpu list defined. 42 */ 43 if (!evsel->own_cpus || 44 (!evsel->system_wide && evlist->has_user_cpus) || 45 (!evsel->system_wide && 46 !evsel->requires_cpu && 47 perf_cpu_map__empty(evlist->user_requested_cpus))) { 48 perf_cpu_map__put(evsel->cpus); 49 evsel->cpus = perf_cpu_map__get(evlist->user_requested_cpus); 50 } else if (evsel->cpus != evsel->own_cpus) { 51 perf_cpu_map__put(evsel->cpus); 52 evsel->cpus = perf_cpu_map__get(evsel->own_cpus); 53 } 54 55 if (!evsel->system_wide) { 56 perf_thread_map__put(evsel->threads); 57 evsel->threads = perf_thread_map__get(evlist->threads); 58 } 59 60 evlist->all_cpus = perf_cpu_map__merge(evlist->all_cpus, evsel->cpus); 61 } 62 63 static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 64 { 65 struct perf_evsel *evsel; 66 67 /* Recomputing all_cpus, so start with a blank slate. */ 68 perf_cpu_map__put(evlist->all_cpus); 69 evlist->all_cpus = NULL; 70 71 perf_evlist__for_each_evsel(evlist, evsel) 72 __perf_evlist__propagate_maps(evlist, evsel); 73 } 74 75 void perf_evlist__add(struct perf_evlist *evlist, 76 struct perf_evsel *evsel) 77 { 78 evsel->idx = evlist->nr_entries; 79 list_add_tail(&evsel->node, &evlist->entries); 80 evlist->nr_entries += 1; 81 __perf_evlist__propagate_maps(evlist, evsel); 82 } 83 84 void perf_evlist__remove(struct perf_evlist *evlist, 85 struct perf_evsel *evsel) 86 { 87 list_del_init(&evsel->node); 88 evlist->nr_entries -= 1; 89 } 90 91 struct perf_evlist *perf_evlist__new(void) 92 { 93 struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 94 95 if (evlist != NULL) 96 perf_evlist__init(evlist); 97 98 return evlist; 99 } 100 101 struct perf_evsel * 102 perf_evlist__next(struct perf_evlist *evlist, struct perf_evsel *prev) 103 { 104 struct perf_evsel *next; 105 106 if (!prev) { 107 next = list_first_entry(&evlist->entries, 108 struct perf_evsel, 109 node); 110 } else { 111 next = list_next_entry(prev, node); 112 } 113 114 /* Empty list is noticed here so don't need checking on entry. */ 115 if (&next->node == &evlist->entries) 116 return NULL; 117 118 return next; 119 } 120 121 static void perf_evlist__purge(struct perf_evlist *evlist) 122 { 123 struct perf_evsel *pos, *n; 124 125 perf_evlist__for_each_entry_safe(evlist, n, pos) { 126 list_del_init(&pos->node); 127 perf_evsel__delete(pos); 128 } 129 130 evlist->nr_entries = 0; 131 } 132 133 void perf_evlist__exit(struct perf_evlist *evlist) 134 { 135 perf_cpu_map__put(evlist->user_requested_cpus); 136 perf_cpu_map__put(evlist->all_cpus); 137 perf_thread_map__put(evlist->threads); 138 evlist->user_requested_cpus = NULL; 139 evlist->all_cpus = NULL; 140 evlist->threads = NULL; 141 fdarray__exit(&evlist->pollfd); 142 } 143 144 void perf_evlist__delete(struct perf_evlist *evlist) 145 { 146 if (evlist == NULL) 147 return; 148 149 perf_evlist__munmap(evlist); 150 perf_evlist__close(evlist); 151 perf_evlist__purge(evlist); 152 perf_evlist__exit(evlist); 153 free(evlist); 154 } 155 156 void perf_evlist__set_maps(struct perf_evlist *evlist, 157 struct perf_cpu_map *cpus, 158 struct perf_thread_map *threads) 159 { 160 /* 161 * Allow for the possibility that one or another of the maps isn't being 162 * changed i.e. don't put it. Note we are assuming the maps that are 163 * being applied are brand new and evlist is taking ownership of the 164 * original reference count of 1. If that is not the case it is up to 165 * the caller to increase the reference count. 166 */ 167 if (cpus != evlist->user_requested_cpus) { 168 perf_cpu_map__put(evlist->user_requested_cpus); 169 evlist->user_requested_cpus = perf_cpu_map__get(cpus); 170 } 171 172 if (threads != evlist->threads) { 173 perf_thread_map__put(evlist->threads); 174 evlist->threads = perf_thread_map__get(threads); 175 } 176 177 if (!evlist->all_cpus && cpus) 178 evlist->all_cpus = perf_cpu_map__get(cpus); 179 180 perf_evlist__propagate_maps(evlist); 181 } 182 183 int perf_evlist__open(struct perf_evlist *evlist) 184 { 185 struct perf_evsel *evsel; 186 int err; 187 188 perf_evlist__for_each_entry(evlist, evsel) { 189 err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 190 if (err < 0) 191 goto out_err; 192 } 193 194 return 0; 195 196 out_err: 197 perf_evlist__close(evlist); 198 return err; 199 } 200 201 void perf_evlist__close(struct perf_evlist *evlist) 202 { 203 struct perf_evsel *evsel; 204 205 perf_evlist__for_each_entry_reverse(evlist, evsel) 206 perf_evsel__close(evsel); 207 } 208 209 void perf_evlist__enable(struct perf_evlist *evlist) 210 { 211 struct perf_evsel *evsel; 212 213 perf_evlist__for_each_entry(evlist, evsel) 214 perf_evsel__enable(evsel); 215 } 216 217 void perf_evlist__disable(struct perf_evlist *evlist) 218 { 219 struct perf_evsel *evsel; 220 221 perf_evlist__for_each_entry(evlist, evsel) 222 perf_evsel__disable(evsel); 223 } 224 225 u64 perf_evlist__read_format(struct perf_evlist *evlist) 226 { 227 struct perf_evsel *first = perf_evlist__first(evlist); 228 229 return first->attr.read_format; 230 } 231 232 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 233 234 static void perf_evlist__id_hash(struct perf_evlist *evlist, 235 struct perf_evsel *evsel, 236 int cpu, int thread, u64 id) 237 { 238 int hash; 239 struct perf_sample_id *sid = SID(evsel, cpu, thread); 240 241 sid->id = id; 242 sid->evsel = evsel; 243 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 244 hlist_add_head(&sid->node, &evlist->heads[hash]); 245 } 246 247 void perf_evlist__reset_id_hash(struct perf_evlist *evlist) 248 { 249 int i; 250 251 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 252 INIT_HLIST_HEAD(&evlist->heads[i]); 253 } 254 255 void perf_evlist__id_add(struct perf_evlist *evlist, 256 struct perf_evsel *evsel, 257 int cpu, int thread, u64 id) 258 { 259 perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 260 evsel->id[evsel->ids++] = id; 261 } 262 263 int perf_evlist__id_add_fd(struct perf_evlist *evlist, 264 struct perf_evsel *evsel, 265 int cpu, int thread, int fd) 266 { 267 u64 read_data[4] = { 0, }; 268 int id_idx = 1; /* The first entry is the counter value */ 269 u64 id; 270 int ret; 271 272 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 273 if (!ret) 274 goto add; 275 276 if (errno != ENOTTY) 277 return -1; 278 279 /* Legacy way to get event id.. All hail to old kernels! */ 280 281 /* 282 * This way does not work with group format read, so bail 283 * out in that case. 284 */ 285 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 286 return -1; 287 288 if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 289 read(fd, &read_data, sizeof(read_data)) == -1) 290 return -1; 291 292 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 293 ++id_idx; 294 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 295 ++id_idx; 296 297 id = read_data[id_idx]; 298 299 add: 300 perf_evlist__id_add(evlist, evsel, cpu, thread, id); 301 return 0; 302 } 303 304 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 305 { 306 int nr_cpus = perf_cpu_map__nr(evlist->all_cpus); 307 int nr_threads = perf_thread_map__nr(evlist->threads); 308 int nfds = 0; 309 struct perf_evsel *evsel; 310 311 perf_evlist__for_each_entry(evlist, evsel) { 312 if (evsel->system_wide) 313 nfds += nr_cpus; 314 else 315 nfds += nr_cpus * nr_threads; 316 } 317 318 if (fdarray__available_entries(&evlist->pollfd) < nfds && 319 fdarray__grow(&evlist->pollfd, nfds) < 0) 320 return -ENOMEM; 321 322 return 0; 323 } 324 325 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, 326 void *ptr, short revent, enum fdarray_flags flags) 327 { 328 int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP, flags); 329 330 if (pos >= 0) { 331 evlist->pollfd.priv[pos].ptr = ptr; 332 fcntl(fd, F_SETFL, O_NONBLOCK); 333 } 334 335 return pos; 336 } 337 338 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 339 void *arg __maybe_unused) 340 { 341 struct perf_mmap *map = fda->priv[fd].ptr; 342 343 if (map) 344 perf_mmap__put(map); 345 } 346 347 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 348 { 349 return fdarray__filter(&evlist->pollfd, revents_and_mask, 350 perf_evlist__munmap_filtered, NULL); 351 } 352 353 int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 354 { 355 return fdarray__poll(&evlist->pollfd, timeout); 356 } 357 358 static struct perf_mmap* perf_evlist__alloc_mmap(struct perf_evlist *evlist, bool overwrite) 359 { 360 int i; 361 struct perf_mmap *map; 362 363 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 364 if (!map) 365 return NULL; 366 367 for (i = 0; i < evlist->nr_mmaps; i++) { 368 struct perf_mmap *prev = i ? &map[i - 1] : NULL; 369 370 /* 371 * When the perf_mmap() call is made we grab one refcount, plus 372 * one extra to let perf_mmap__consume() get the last 373 * events after all real references (perf_mmap__get()) are 374 * dropped. 375 * 376 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 377 * thus does perf_mmap__get() on it. 378 */ 379 perf_mmap__init(&map[i], prev, overwrite, NULL); 380 } 381 382 return map; 383 } 384 385 static void perf_evsel__set_sid_idx(struct perf_evsel *evsel, int idx, int cpu, int thread) 386 { 387 struct perf_sample_id *sid = SID(evsel, cpu, thread); 388 389 sid->idx = idx; 390 sid->cpu = perf_cpu_map__cpu(evsel->cpus, cpu); 391 sid->tid = perf_thread_map__pid(evsel->threads, thread); 392 } 393 394 static struct perf_mmap* 395 perf_evlist__mmap_cb_get(struct perf_evlist *evlist, bool overwrite, int idx) 396 { 397 struct perf_mmap *maps; 398 399 maps = overwrite ? evlist->mmap_ovw : evlist->mmap; 400 401 if (!maps) { 402 maps = perf_evlist__alloc_mmap(evlist, overwrite); 403 if (!maps) 404 return NULL; 405 406 if (overwrite) 407 evlist->mmap_ovw = maps; 408 else 409 evlist->mmap = maps; 410 } 411 412 return &maps[idx]; 413 } 414 415 #define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y)) 416 417 static int 418 perf_evlist__mmap_cb_mmap(struct perf_mmap *map, struct perf_mmap_param *mp, 419 int output, struct perf_cpu cpu) 420 { 421 return perf_mmap__mmap(map, mp, output, cpu); 422 } 423 424 static void perf_evlist__set_mmap_first(struct perf_evlist *evlist, struct perf_mmap *map, 425 bool overwrite) 426 { 427 if (overwrite) 428 evlist->mmap_ovw_first = map; 429 else 430 evlist->mmap_first = map; 431 } 432 433 static int 434 mmap_per_evsel(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops, 435 int idx, struct perf_mmap_param *mp, int cpu_idx, 436 int thread, int *_output, int *_output_overwrite, int *nr_mmaps) 437 { 438 struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->all_cpus, cpu_idx); 439 struct perf_evsel *evsel; 440 int revent; 441 442 perf_evlist__for_each_entry(evlist, evsel) { 443 bool overwrite = evsel->attr.write_backward; 444 enum fdarray_flags flgs; 445 struct perf_mmap *map; 446 int *output, fd, cpu; 447 448 if (evsel->system_wide && thread) 449 continue; 450 451 cpu = perf_cpu_map__idx(evsel->cpus, evlist_cpu); 452 if (cpu == -1) 453 continue; 454 455 map = ops->get(evlist, overwrite, idx); 456 if (map == NULL) 457 return -ENOMEM; 458 459 if (overwrite) { 460 mp->prot = PROT_READ; 461 output = _output_overwrite; 462 } else { 463 mp->prot = PROT_READ | PROT_WRITE; 464 output = _output; 465 } 466 467 fd = FD(evsel, cpu, thread); 468 469 if (*output == -1) { 470 *output = fd; 471 472 /* 473 * The last one will be done at perf_mmap__consume(), so that we 474 * make sure we don't prevent tools from consuming every last event in 475 * the ring buffer. 476 * 477 * I.e. we can get the POLLHUP meaning that the fd doesn't exist 478 * anymore, but the last events for it are still in the ring buffer, 479 * waiting to be consumed. 480 * 481 * Tools can chose to ignore this at their own discretion, but the 482 * evlist layer can't just drop it when filtering events in 483 * perf_evlist__filter_pollfd(). 484 */ 485 refcount_set(&map->refcnt, 2); 486 487 if (ops->idx) 488 ops->idx(evlist, evsel, mp, idx); 489 490 pr_debug("idx %d: mmapping fd %d\n", idx, *output); 491 if (ops->mmap(map, mp, *output, evlist_cpu) < 0) 492 return -1; 493 494 *nr_mmaps += 1; 495 496 if (!idx) 497 perf_evlist__set_mmap_first(evlist, map, overwrite); 498 } else { 499 pr_debug("idx %d: set output fd %d -> %d\n", idx, fd, *output); 500 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 501 return -1; 502 503 perf_mmap__get(map); 504 } 505 506 revent = !overwrite ? POLLIN : 0; 507 508 flgs = evsel->system_wide ? fdarray_flag__nonfilterable : fdarray_flag__default; 509 if (perf_evlist__add_pollfd(evlist, fd, map, revent, flgs) < 0) { 510 perf_mmap__put(map); 511 return -1; 512 } 513 514 if (evsel->attr.read_format & PERF_FORMAT_ID) { 515 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 516 fd) < 0) 517 return -1; 518 perf_evsel__set_sid_idx(evsel, idx, cpu, thread); 519 } 520 } 521 522 return 0; 523 } 524 525 static int 526 mmap_per_thread(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops, 527 struct perf_mmap_param *mp) 528 { 529 int nr_threads = perf_thread_map__nr(evlist->threads); 530 int nr_cpus = perf_cpu_map__nr(evlist->all_cpus); 531 int cpu, thread, idx = 0; 532 int nr_mmaps = 0; 533 534 pr_debug("%s: nr cpu values (may include -1) %d nr threads %d\n", 535 __func__, nr_cpus, nr_threads); 536 537 /* per-thread mmaps */ 538 for (thread = 0; thread < nr_threads; thread++, idx++) { 539 int output = -1; 540 int output_overwrite = -1; 541 542 if (mmap_per_evsel(evlist, ops, idx, mp, 0, thread, &output, 543 &output_overwrite, &nr_mmaps)) 544 goto out_unmap; 545 } 546 547 /* system-wide mmaps i.e. per-cpu */ 548 for (cpu = 1; cpu < nr_cpus; cpu++, idx++) { 549 int output = -1; 550 int output_overwrite = -1; 551 552 if (mmap_per_evsel(evlist, ops, idx, mp, cpu, 0, &output, 553 &output_overwrite, &nr_mmaps)) 554 goto out_unmap; 555 } 556 557 if (nr_mmaps != evlist->nr_mmaps) 558 pr_err("Miscounted nr_mmaps %d vs %d\n", nr_mmaps, evlist->nr_mmaps); 559 560 return 0; 561 562 out_unmap: 563 perf_evlist__munmap(evlist); 564 return -1; 565 } 566 567 static int 568 mmap_per_cpu(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops, 569 struct perf_mmap_param *mp) 570 { 571 int nr_threads = perf_thread_map__nr(evlist->threads); 572 int nr_cpus = perf_cpu_map__nr(evlist->all_cpus); 573 int nr_mmaps = 0; 574 int cpu, thread; 575 576 pr_debug("%s: nr cpu values %d nr threads %d\n", __func__, nr_cpus, nr_threads); 577 578 for (cpu = 0; cpu < nr_cpus; cpu++) { 579 int output = -1; 580 int output_overwrite = -1; 581 582 for (thread = 0; thread < nr_threads; thread++) { 583 if (mmap_per_evsel(evlist, ops, cpu, mp, cpu, 584 thread, &output, &output_overwrite, &nr_mmaps)) 585 goto out_unmap; 586 } 587 } 588 589 if (nr_mmaps != evlist->nr_mmaps) 590 pr_err("Miscounted nr_mmaps %d vs %d\n", nr_mmaps, evlist->nr_mmaps); 591 592 return 0; 593 594 out_unmap: 595 perf_evlist__munmap(evlist); 596 return -1; 597 } 598 599 static int perf_evlist__nr_mmaps(struct perf_evlist *evlist) 600 { 601 int nr_mmaps; 602 603 /* One for each CPU */ 604 nr_mmaps = perf_cpu_map__nr(evlist->all_cpus); 605 if (perf_cpu_map__empty(evlist->all_cpus)) { 606 /* Plus one for each thread */ 607 nr_mmaps += perf_thread_map__nr(evlist->threads); 608 /* Minus the per-thread CPU (-1) */ 609 nr_mmaps -= 1; 610 } 611 612 return nr_mmaps; 613 } 614 615 int perf_evlist__mmap_ops(struct perf_evlist *evlist, 616 struct perf_evlist_mmap_ops *ops, 617 struct perf_mmap_param *mp) 618 { 619 const struct perf_cpu_map *cpus = evlist->all_cpus; 620 struct perf_evsel *evsel; 621 622 if (!ops || !ops->get || !ops->mmap) 623 return -EINVAL; 624 625 mp->mask = evlist->mmap_len - page_size - 1; 626 627 evlist->nr_mmaps = perf_evlist__nr_mmaps(evlist); 628 629 perf_evlist__for_each_entry(evlist, evsel) { 630 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 631 evsel->sample_id == NULL && 632 perf_evsel__alloc_id(evsel, evsel->fd->max_x, evsel->fd->max_y) < 0) 633 return -ENOMEM; 634 } 635 636 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 637 return -ENOMEM; 638 639 if (perf_cpu_map__empty(cpus)) 640 return mmap_per_thread(evlist, ops, mp); 641 642 return mmap_per_cpu(evlist, ops, mp); 643 } 644 645 int perf_evlist__mmap(struct perf_evlist *evlist, int pages) 646 { 647 struct perf_mmap_param mp; 648 struct perf_evlist_mmap_ops ops = { 649 .get = perf_evlist__mmap_cb_get, 650 .mmap = perf_evlist__mmap_cb_mmap, 651 }; 652 653 evlist->mmap_len = (pages + 1) * page_size; 654 655 return perf_evlist__mmap_ops(evlist, &ops, &mp); 656 } 657 658 void perf_evlist__munmap(struct perf_evlist *evlist) 659 { 660 int i; 661 662 if (evlist->mmap) { 663 for (i = 0; i < evlist->nr_mmaps; i++) 664 perf_mmap__munmap(&evlist->mmap[i]); 665 } 666 667 if (evlist->mmap_ovw) { 668 for (i = 0; i < evlist->nr_mmaps; i++) 669 perf_mmap__munmap(&evlist->mmap_ovw[i]); 670 } 671 672 zfree(&evlist->mmap); 673 zfree(&evlist->mmap_ovw); 674 } 675 676 struct perf_mmap* 677 perf_evlist__next_mmap(struct perf_evlist *evlist, struct perf_mmap *map, 678 bool overwrite) 679 { 680 if (map) 681 return map->next; 682 683 return overwrite ? evlist->mmap_ovw_first : evlist->mmap_first; 684 } 685 686 void __perf_evlist__set_leader(struct list_head *list, struct perf_evsel *leader) 687 { 688 struct perf_evsel *first, *last, *evsel; 689 690 first = list_first_entry(list, struct perf_evsel, node); 691 last = list_last_entry(list, struct perf_evsel, node); 692 693 leader->nr_members = last->idx - first->idx + 1; 694 695 __perf_evlist__for_each_entry(list, evsel) 696 evsel->leader = leader; 697 } 698 699 void perf_evlist__set_leader(struct perf_evlist *evlist) 700 { 701 if (evlist->nr_entries) { 702 struct perf_evsel *first = list_entry(evlist->entries.next, 703 struct perf_evsel, node); 704 705 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 706 __perf_evlist__set_leader(&evlist->entries, first); 707 } 708 } 709