1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4 * 5 * Parts came from builtin-{top,stat,record}.c, see those files for further 6 * copyright notes. 7 */ 8 #include <api/fs/fs.h> 9 #include <errno.h> 10 #include <inttypes.h> 11 #include <poll.h> 12 #include "cpumap.h" 13 #include "util/mmap.h" 14 #include "thread_map.h" 15 #include "target.h" 16 #include "evlist.h" 17 #include "evsel.h" 18 #include "debug.h" 19 #include "units.h" 20 #include <internal/lib.h> // page_size 21 #include "affinity.h" 22 #include "../perf.h" 23 #include "asm/bug.h" 24 #include "bpf-event.h" 25 #include "util/string2.h" 26 #include "util/perf_api_probe.h" 27 #include "util/evsel_fprintf.h" 28 #include <signal.h> 29 #include <unistd.h> 30 #include <sched.h> 31 #include <stdlib.h> 32 33 #include "parse-events.h" 34 #include <subcmd/parse-options.h> 35 36 #include <fcntl.h> 37 #include <sys/ioctl.h> 38 #include <sys/mman.h> 39 40 #include <linux/bitops.h> 41 #include <linux/hash.h> 42 #include <linux/log2.h> 43 #include <linux/err.h> 44 #include <linux/string.h> 45 #include <linux/zalloc.h> 46 #include <perf/evlist.h> 47 #include <perf/evsel.h> 48 #include <perf/cpumap.h> 49 #include <perf/mmap.h> 50 51 #include <internal/xyarray.h> 52 53 #ifdef LACKS_SIGQUEUE_PROTOTYPE 54 int sigqueue(pid_t pid, int sig, const union sigval value); 55 #endif 56 57 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 58 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 59 60 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 61 struct perf_thread_map *threads) 62 { 63 perf_evlist__init(&evlist->core); 64 perf_evlist__set_maps(&evlist->core, cpus, threads); 65 evlist->workload.pid = -1; 66 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 67 evlist->ctl_fd.fd = -1; 68 evlist->ctl_fd.ack = -1; 69 evlist->ctl_fd.pos = -1; 70 } 71 72 struct evlist *evlist__new(void) 73 { 74 struct evlist *evlist = zalloc(sizeof(*evlist)); 75 76 if (evlist != NULL) 77 evlist__init(evlist, NULL, NULL); 78 79 return evlist; 80 } 81 82 struct evlist *evlist__new_default(void) 83 { 84 struct evlist *evlist = evlist__new(); 85 86 if (evlist && evlist__add_default(evlist)) { 87 evlist__delete(evlist); 88 evlist = NULL; 89 } 90 91 return evlist; 92 } 93 94 struct evlist *evlist__new_dummy(void) 95 { 96 struct evlist *evlist = evlist__new(); 97 98 if (evlist && evlist__add_dummy(evlist)) { 99 evlist__delete(evlist); 100 evlist = NULL; 101 } 102 103 return evlist; 104 } 105 106 /** 107 * evlist__set_id_pos - set the positions of event ids. 108 * @evlist: selected event list 109 * 110 * Events with compatible sample types all have the same id_pos 111 * and is_pos. For convenience, put a copy on evlist. 112 */ 113 void evlist__set_id_pos(struct evlist *evlist) 114 { 115 struct evsel *first = evlist__first(evlist); 116 117 evlist->id_pos = first->id_pos; 118 evlist->is_pos = first->is_pos; 119 } 120 121 static void evlist__update_id_pos(struct evlist *evlist) 122 { 123 struct evsel *evsel; 124 125 evlist__for_each_entry(evlist, evsel) 126 evsel__calc_id_pos(evsel); 127 128 evlist__set_id_pos(evlist); 129 } 130 131 static void evlist__purge(struct evlist *evlist) 132 { 133 struct evsel *pos, *n; 134 135 evlist__for_each_entry_safe(evlist, n, pos) { 136 list_del_init(&pos->core.node); 137 pos->evlist = NULL; 138 evsel__delete(pos); 139 } 140 141 evlist->core.nr_entries = 0; 142 } 143 144 void evlist__exit(struct evlist *evlist) 145 { 146 zfree(&evlist->mmap); 147 zfree(&evlist->overwrite_mmap); 148 perf_evlist__exit(&evlist->core); 149 } 150 151 void evlist__delete(struct evlist *evlist) 152 { 153 if (evlist == NULL) 154 return; 155 156 evlist__munmap(evlist); 157 evlist__close(evlist); 158 evlist__purge(evlist); 159 evlist__exit(evlist); 160 free(evlist); 161 } 162 163 void evlist__add(struct evlist *evlist, struct evsel *entry) 164 { 165 entry->evlist = evlist; 166 entry->idx = evlist->core.nr_entries; 167 entry->tracking = !entry->idx; 168 169 perf_evlist__add(&evlist->core, &entry->core); 170 171 if (evlist->core.nr_entries == 1) 172 evlist__set_id_pos(evlist); 173 } 174 175 void evlist__remove(struct evlist *evlist, struct evsel *evsel) 176 { 177 evsel->evlist = NULL; 178 perf_evlist__remove(&evlist->core, &evsel->core); 179 } 180 181 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) 182 { 183 while (!list_empty(list)) { 184 struct evsel *evsel, *temp, *leader = NULL; 185 186 __evlist__for_each_entry_safe(list, temp, evsel) { 187 list_del_init(&evsel->core.node); 188 evlist__add(evlist, evsel); 189 leader = evsel; 190 break; 191 } 192 193 __evlist__for_each_entry_safe(list, temp, evsel) { 194 if (evsel->leader == leader) { 195 list_del_init(&evsel->core.node); 196 evlist__add(evlist, evsel); 197 } 198 } 199 } 200 } 201 202 int __evlist__set_tracepoints_handlers(struct evlist *evlist, 203 const struct evsel_str_handler *assocs, size_t nr_assocs) 204 { 205 size_t i; 206 int err; 207 208 for (i = 0; i < nr_assocs; i++) { 209 // Adding a handler for an event not in this evlist, just ignore it. 210 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name); 211 if (evsel == NULL) 212 continue; 213 214 err = -EEXIST; 215 if (evsel->handler != NULL) 216 goto out; 217 evsel->handler = assocs[i].handler; 218 } 219 220 err = 0; 221 out: 222 return err; 223 } 224 225 void __evlist__set_leader(struct list_head *list) 226 { 227 struct evsel *evsel, *leader; 228 229 leader = list_entry(list->next, struct evsel, core.node); 230 evsel = list_entry(list->prev, struct evsel, core.node); 231 232 leader->core.nr_members = evsel->idx - leader->idx + 1; 233 234 __evlist__for_each_entry(list, evsel) { 235 evsel->leader = leader; 236 } 237 } 238 239 void evlist__set_leader(struct evlist *evlist) 240 { 241 if (evlist->core.nr_entries) { 242 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 243 __evlist__set_leader(&evlist->core.entries); 244 } 245 } 246 247 int __evlist__add_default(struct evlist *evlist, bool precise) 248 { 249 struct evsel *evsel = evsel__new_cycles(precise); 250 251 if (evsel == NULL) 252 return -ENOMEM; 253 254 evlist__add(evlist, evsel); 255 return 0; 256 } 257 258 int evlist__add_dummy(struct evlist *evlist) 259 { 260 struct perf_event_attr attr = { 261 .type = PERF_TYPE_SOFTWARE, 262 .config = PERF_COUNT_SW_DUMMY, 263 .size = sizeof(attr), /* to capture ABI version */ 264 }; 265 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 266 267 if (evsel == NULL) 268 return -ENOMEM; 269 270 evlist__add(evlist, evsel); 271 return 0; 272 } 273 274 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 275 { 276 struct evsel *evsel, *n; 277 LIST_HEAD(head); 278 size_t i; 279 280 for (i = 0; i < nr_attrs; i++) { 281 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 282 if (evsel == NULL) 283 goto out_delete_partial_list; 284 list_add_tail(&evsel->core.node, &head); 285 } 286 287 evlist__splice_list_tail(evlist, &head); 288 289 return 0; 290 291 out_delete_partial_list: 292 __evlist__for_each_entry_safe(&head, n, evsel) 293 evsel__delete(evsel); 294 return -1; 295 } 296 297 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 298 { 299 size_t i; 300 301 for (i = 0; i < nr_attrs; i++) 302 event_attr_init(attrs + i); 303 304 return evlist__add_attrs(evlist, attrs, nr_attrs); 305 } 306 307 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused) 308 { 309 return 0; 310 } 311 312 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 313 { 314 struct evsel *evsel; 315 316 evlist__for_each_entry(evlist, evsel) { 317 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 318 (int)evsel->core.attr.config == id) 319 return evsel; 320 } 321 322 return NULL; 323 } 324 325 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 326 { 327 struct evsel *evsel; 328 329 evlist__for_each_entry(evlist, evsel) { 330 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 331 (strcmp(evsel->name, name) == 0)) 332 return evsel; 333 } 334 335 return NULL; 336 } 337 338 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 339 { 340 struct evsel *evsel = evsel__newtp(sys, name); 341 342 if (IS_ERR(evsel)) 343 return -1; 344 345 evsel->handler = handler; 346 evlist__add(evlist, evsel); 347 return 0; 348 } 349 350 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel) 351 { 352 if (evsel->core.system_wide) 353 return 1; 354 else 355 return perf_thread_map__nr(evlist->core.threads); 356 } 357 358 void evlist__cpu_iter_start(struct evlist *evlist) 359 { 360 struct evsel *pos; 361 362 /* 363 * Reset the per evsel cpu_iter. This is needed because 364 * each evsel's cpumap may have a different index space, 365 * and some operations need the index to modify 366 * the FD xyarray (e.g. open, close) 367 */ 368 evlist__for_each_entry(evlist, pos) 369 pos->cpu_iter = 0; 370 } 371 372 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 373 { 374 if (ev->cpu_iter >= ev->core.cpus->nr) 375 return true; 376 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 377 return true; 378 return false; 379 } 380 381 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 382 { 383 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 384 ev->cpu_iter++; 385 return false; 386 } 387 return true; 388 } 389 390 static int evsel__strcmp(struct evsel *pos, char *evsel_name) 391 { 392 if (!evsel_name) 393 return 0; 394 if (evsel__is_dummy_event(pos)) 395 return 1; 396 return strcmp(pos->name, evsel_name); 397 } 398 399 static int evlist__is_enabled(struct evlist *evlist) 400 { 401 struct evsel *pos; 402 403 evlist__for_each_entry(evlist, pos) { 404 if (!evsel__is_group_leader(pos) || !pos->core.fd) 405 continue; 406 /* If at least one event is enabled, evlist is enabled. */ 407 if (!pos->disabled) 408 return true; 409 } 410 return false; 411 } 412 413 static void __evlist__disable(struct evlist *evlist, char *evsel_name) 414 { 415 struct evsel *pos; 416 struct affinity affinity; 417 int cpu, i, imm = 0; 418 bool has_imm = false; 419 420 if (affinity__setup(&affinity) < 0) 421 return; 422 423 /* Disable 'immediate' events last */ 424 for (imm = 0; imm <= 1; imm++) { 425 evlist__for_each_cpu(evlist, i, cpu) { 426 affinity__set(&affinity, cpu); 427 428 evlist__for_each_entry(evlist, pos) { 429 if (evsel__strcmp(pos, evsel_name)) 430 continue; 431 if (evsel__cpu_iter_skip(pos, cpu)) 432 continue; 433 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 434 continue; 435 if (pos->immediate) 436 has_imm = true; 437 if (pos->immediate != imm) 438 continue; 439 evsel__disable_cpu(pos, pos->cpu_iter - 1); 440 } 441 } 442 if (!has_imm) 443 break; 444 } 445 446 affinity__cleanup(&affinity); 447 evlist__for_each_entry(evlist, pos) { 448 if (evsel__strcmp(pos, evsel_name)) 449 continue; 450 if (!evsel__is_group_leader(pos) || !pos->core.fd) 451 continue; 452 pos->disabled = true; 453 } 454 455 /* 456 * If we disabled only single event, we need to check 457 * the enabled state of the evlist manually. 458 */ 459 if (evsel_name) 460 evlist->enabled = evlist__is_enabled(evlist); 461 else 462 evlist->enabled = false; 463 } 464 465 void evlist__disable(struct evlist *evlist) 466 { 467 __evlist__disable(evlist, NULL); 468 } 469 470 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name) 471 { 472 __evlist__disable(evlist, evsel_name); 473 } 474 475 static void __evlist__enable(struct evlist *evlist, char *evsel_name) 476 { 477 struct evsel *pos; 478 struct affinity affinity; 479 int cpu, i; 480 481 if (affinity__setup(&affinity) < 0) 482 return; 483 484 evlist__for_each_cpu(evlist, i, cpu) { 485 affinity__set(&affinity, cpu); 486 487 evlist__for_each_entry(evlist, pos) { 488 if (evsel__strcmp(pos, evsel_name)) 489 continue; 490 if (evsel__cpu_iter_skip(pos, cpu)) 491 continue; 492 if (!evsel__is_group_leader(pos) || !pos->core.fd) 493 continue; 494 evsel__enable_cpu(pos, pos->cpu_iter - 1); 495 } 496 } 497 affinity__cleanup(&affinity); 498 evlist__for_each_entry(evlist, pos) { 499 if (evsel__strcmp(pos, evsel_name)) 500 continue; 501 if (!evsel__is_group_leader(pos) || !pos->core.fd) 502 continue; 503 pos->disabled = false; 504 } 505 506 /* 507 * Even single event sets the 'enabled' for evlist, 508 * so the toggle can work properly and toggle to 509 * 'disabled' state. 510 */ 511 evlist->enabled = true; 512 } 513 514 void evlist__enable(struct evlist *evlist) 515 { 516 __evlist__enable(evlist, NULL); 517 } 518 519 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name) 520 { 521 __evlist__enable(evlist, evsel_name); 522 } 523 524 void evlist__toggle_enable(struct evlist *evlist) 525 { 526 (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 527 } 528 529 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu) 530 { 531 int thread; 532 int nr_threads = evlist__nr_threads(evlist, evsel); 533 534 if (!evsel->core.fd) 535 return -EINVAL; 536 537 for (thread = 0; thread < nr_threads; thread++) { 538 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 539 if (err) 540 return err; 541 } 542 return 0; 543 } 544 545 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread) 546 { 547 int cpu; 548 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 549 550 if (!evsel->core.fd) 551 return -EINVAL; 552 553 for (cpu = 0; cpu < nr_cpus; cpu++) { 554 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 555 if (err) 556 return err; 557 } 558 return 0; 559 } 560 561 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx) 562 { 563 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 564 565 if (per_cpu_mmaps) 566 return evlist__enable_event_cpu(evlist, evsel, idx); 567 568 return evlist__enable_event_thread(evlist, evsel, idx); 569 } 570 571 int evlist__add_pollfd(struct evlist *evlist, int fd) 572 { 573 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 574 } 575 576 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 577 { 578 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 579 } 580 581 #ifdef HAVE_EVENTFD_SUPPORT 582 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd) 583 { 584 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 585 fdarray_flag__nonfilterable); 586 } 587 #endif 588 589 int evlist__poll(struct evlist *evlist, int timeout) 590 { 591 return perf_evlist__poll(&evlist->core, timeout); 592 } 593 594 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 595 { 596 struct hlist_head *head; 597 struct perf_sample_id *sid; 598 int hash; 599 600 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 601 head = &evlist->core.heads[hash]; 602 603 hlist_for_each_entry(sid, head, node) 604 if (sid->id == id) 605 return sid; 606 607 return NULL; 608 } 609 610 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 611 { 612 struct perf_sample_id *sid; 613 614 if (evlist->core.nr_entries == 1 || !id) 615 return evlist__first(evlist); 616 617 sid = evlist__id2sid(evlist, id); 618 if (sid) 619 return container_of(sid->evsel, struct evsel, core); 620 621 if (!evlist__sample_id_all(evlist)) 622 return evlist__first(evlist); 623 624 return NULL; 625 } 626 627 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 628 { 629 struct perf_sample_id *sid; 630 631 if (!id) 632 return NULL; 633 634 sid = evlist__id2sid(evlist, id); 635 if (sid) 636 return container_of(sid->evsel, struct evsel, core); 637 638 return NULL; 639 } 640 641 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 642 { 643 const __u64 *array = event->sample.array; 644 ssize_t n; 645 646 n = (event->header.size - sizeof(event->header)) >> 3; 647 648 if (event->header.type == PERF_RECORD_SAMPLE) { 649 if (evlist->id_pos >= n) 650 return -1; 651 *id = array[evlist->id_pos]; 652 } else { 653 if (evlist->is_pos > n) 654 return -1; 655 n -= evlist->is_pos; 656 *id = array[n]; 657 } 658 return 0; 659 } 660 661 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 662 { 663 struct evsel *first = evlist__first(evlist); 664 struct hlist_head *head; 665 struct perf_sample_id *sid; 666 int hash; 667 u64 id; 668 669 if (evlist->core.nr_entries == 1) 670 return first; 671 672 if (!first->core.attr.sample_id_all && 673 event->header.type != PERF_RECORD_SAMPLE) 674 return first; 675 676 if (evlist__event2id(evlist, event, &id)) 677 return NULL; 678 679 /* Synthesized events have an id of zero */ 680 if (!id) 681 return first; 682 683 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 684 head = &evlist->core.heads[hash]; 685 686 hlist_for_each_entry(sid, head, node) { 687 if (sid->id == id) 688 return container_of(sid->evsel, struct evsel, core); 689 } 690 return NULL; 691 } 692 693 static int evlist__set_paused(struct evlist *evlist, bool value) 694 { 695 int i; 696 697 if (!evlist->overwrite_mmap) 698 return 0; 699 700 for (i = 0; i < evlist->core.nr_mmaps; i++) { 701 int fd = evlist->overwrite_mmap[i].core.fd; 702 int err; 703 704 if (fd < 0) 705 continue; 706 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 707 if (err) 708 return err; 709 } 710 return 0; 711 } 712 713 static int evlist__pause(struct evlist *evlist) 714 { 715 return evlist__set_paused(evlist, true); 716 } 717 718 static int evlist__resume(struct evlist *evlist) 719 { 720 return evlist__set_paused(evlist, false); 721 } 722 723 static void evlist__munmap_nofree(struct evlist *evlist) 724 { 725 int i; 726 727 if (evlist->mmap) 728 for (i = 0; i < evlist->core.nr_mmaps; i++) 729 perf_mmap__munmap(&evlist->mmap[i].core); 730 731 if (evlist->overwrite_mmap) 732 for (i = 0; i < evlist->core.nr_mmaps; i++) 733 perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 734 } 735 736 void evlist__munmap(struct evlist *evlist) 737 { 738 evlist__munmap_nofree(evlist); 739 zfree(&evlist->mmap); 740 zfree(&evlist->overwrite_mmap); 741 } 742 743 static void perf_mmap__unmap_cb(struct perf_mmap *map) 744 { 745 struct mmap *m = container_of(map, struct mmap, core); 746 747 mmap__munmap(m); 748 } 749 750 static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 751 bool overwrite) 752 { 753 int i; 754 struct mmap *map; 755 756 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 757 if (!map) 758 return NULL; 759 760 for (i = 0; i < evlist->core.nr_mmaps; i++) { 761 struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 762 763 /* 764 * When the perf_mmap() call is made we grab one refcount, plus 765 * one extra to let perf_mmap__consume() get the last 766 * events after all real references (perf_mmap__get()) are 767 * dropped. 768 * 769 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 770 * thus does perf_mmap__get() on it. 771 */ 772 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 773 } 774 775 return map; 776 } 777 778 static void 779 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 780 struct perf_mmap_param *_mp, 781 int idx, bool per_cpu) 782 { 783 struct evlist *evlist = container_of(_evlist, struct evlist, core); 784 struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 785 786 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 787 } 788 789 static struct perf_mmap* 790 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 791 { 792 struct evlist *evlist = container_of(_evlist, struct evlist, core); 793 struct mmap *maps; 794 795 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 796 797 if (!maps) { 798 maps = evlist__alloc_mmap(evlist, overwrite); 799 if (!maps) 800 return NULL; 801 802 if (overwrite) { 803 evlist->overwrite_mmap = maps; 804 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 805 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 806 } else { 807 evlist->mmap = maps; 808 } 809 } 810 811 return &maps[idx].core; 812 } 813 814 static int 815 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 816 int output, int cpu) 817 { 818 struct mmap *map = container_of(_map, struct mmap, core); 819 struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 820 821 return mmap__mmap(map, mp, output, cpu); 822 } 823 824 unsigned long perf_event_mlock_kb_in_pages(void) 825 { 826 unsigned long pages; 827 int max; 828 829 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 830 /* 831 * Pick a once upon a time good value, i.e. things look 832 * strange since we can't read a sysctl value, but lets not 833 * die yet... 834 */ 835 max = 512; 836 } else { 837 max -= (page_size / 1024); 838 } 839 840 pages = (max * 1024) / page_size; 841 if (!is_power_of_2(pages)) 842 pages = rounddown_pow_of_two(pages); 843 844 return pages; 845 } 846 847 size_t evlist__mmap_size(unsigned long pages) 848 { 849 if (pages == UINT_MAX) 850 pages = perf_event_mlock_kb_in_pages(); 851 else if (!is_power_of_2(pages)) 852 return 0; 853 854 return (pages + 1) * page_size; 855 } 856 857 static long parse_pages_arg(const char *str, unsigned long min, 858 unsigned long max) 859 { 860 unsigned long pages, val; 861 static struct parse_tag tags[] = { 862 { .tag = 'B', .mult = 1 }, 863 { .tag = 'K', .mult = 1 << 10 }, 864 { .tag = 'M', .mult = 1 << 20 }, 865 { .tag = 'G', .mult = 1 << 30 }, 866 { .tag = 0 }, 867 }; 868 869 if (str == NULL) 870 return -EINVAL; 871 872 val = parse_tag_value(str, tags); 873 if (val != (unsigned long) -1) { 874 /* we got file size value */ 875 pages = PERF_ALIGN(val, page_size) / page_size; 876 } else { 877 /* we got pages count value */ 878 char *eptr; 879 pages = strtoul(str, &eptr, 10); 880 if (*eptr != '\0') 881 return -EINVAL; 882 } 883 884 if (pages == 0 && min == 0) { 885 /* leave number of pages at 0 */ 886 } else if (!is_power_of_2(pages)) { 887 char buf[100]; 888 889 /* round pages up to next power of 2 */ 890 pages = roundup_pow_of_two(pages); 891 if (!pages) 892 return -EINVAL; 893 894 unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 895 pr_info("rounding mmap pages size to %s (%lu pages)\n", 896 buf, pages); 897 } 898 899 if (pages > max) 900 return -EINVAL; 901 902 return pages; 903 } 904 905 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 906 { 907 unsigned long max = UINT_MAX; 908 long pages; 909 910 if (max > SIZE_MAX / page_size) 911 max = SIZE_MAX / page_size; 912 913 pages = parse_pages_arg(str, 1, max); 914 if (pages < 0) { 915 pr_err("Invalid argument for --mmap_pages/-m\n"); 916 return -1; 917 } 918 919 *mmap_pages = pages; 920 return 0; 921 } 922 923 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused) 924 { 925 return __evlist__parse_mmap_pages(opt->value, str); 926 } 927 928 /** 929 * evlist__mmap_ex - Create mmaps to receive events. 930 * @evlist: list of events 931 * @pages: map length in pages 932 * @overwrite: overwrite older events? 933 * @auxtrace_pages - auxtrace map length in pages 934 * @auxtrace_overwrite - overwrite older auxtrace data? 935 * 936 * If @overwrite is %false the user needs to signal event consumption using 937 * perf_mmap__write_tail(). Using evlist__mmap_read() does this 938 * automatically. 939 * 940 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 941 * consumption using auxtrace_mmap__write_tail(). 942 * 943 * Return: %0 on success, negative error code otherwise. 944 */ 945 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 946 unsigned int auxtrace_pages, 947 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 948 int comp_level) 949 { 950 /* 951 * Delay setting mp.prot: set it before calling perf_mmap__mmap. 952 * Its value is decided by evsel's write_backward. 953 * So &mp should not be passed through const pointer. 954 */ 955 struct mmap_params mp = { 956 .nr_cblocks = nr_cblocks, 957 .affinity = affinity, 958 .flush = flush, 959 .comp_level = comp_level 960 }; 961 struct perf_evlist_mmap_ops ops = { 962 .idx = perf_evlist__mmap_cb_idx, 963 .get = perf_evlist__mmap_cb_get, 964 .mmap = perf_evlist__mmap_cb_mmap, 965 }; 966 967 evlist->core.mmap_len = evlist__mmap_size(pages); 968 pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 969 970 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 971 auxtrace_pages, auxtrace_overwrite); 972 973 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 974 } 975 976 int evlist__mmap(struct evlist *evlist, unsigned int pages) 977 { 978 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 979 } 980 981 int evlist__create_maps(struct evlist *evlist, struct target *target) 982 { 983 bool all_threads = (target->per_thread && target->system_wide); 984 struct perf_cpu_map *cpus; 985 struct perf_thread_map *threads; 986 987 /* 988 * If specify '-a' and '--per-thread' to perf record, perf record 989 * will override '--per-thread'. target->per_thread = false and 990 * target->system_wide = true. 991 * 992 * If specify '--per-thread' only to perf record, 993 * target->per_thread = true and target->system_wide = false. 994 * 995 * So target->per_thread && target->system_wide is false. 996 * For perf record, thread_map__new_str doesn't call 997 * thread_map__new_all_cpus. That will keep perf record's 998 * current behavior. 999 * 1000 * For perf stat, it allows the case that target->per_thread and 1001 * target->system_wide are all true. It means to collect system-wide 1002 * per-thread data. thread_map__new_str will call 1003 * thread_map__new_all_cpus to enumerate all threads. 1004 */ 1005 threads = thread_map__new_str(target->pid, target->tid, target->uid, 1006 all_threads); 1007 1008 if (!threads) 1009 return -1; 1010 1011 if (target__uses_dummy_map(target)) 1012 cpus = perf_cpu_map__dummy_new(); 1013 else 1014 cpus = perf_cpu_map__new(target->cpu_list); 1015 1016 if (!cpus) 1017 goto out_delete_threads; 1018 1019 evlist->core.has_user_cpus = !!target->cpu_list; 1020 1021 perf_evlist__set_maps(&evlist->core, cpus, threads); 1022 1023 /* as evlist now has references, put count here */ 1024 perf_cpu_map__put(cpus); 1025 perf_thread_map__put(threads); 1026 1027 return 0; 1028 1029 out_delete_threads: 1030 perf_thread_map__put(threads); 1031 return -1; 1032 } 1033 1034 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 1035 { 1036 struct evsel *evsel; 1037 int err = 0; 1038 1039 evlist__for_each_entry(evlist, evsel) { 1040 if (evsel->filter == NULL) 1041 continue; 1042 1043 /* 1044 * filters only work for tracepoint event, which doesn't have cpu limit. 1045 * So evlist and evsel should always be same. 1046 */ 1047 err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 1048 if (err) { 1049 *err_evsel = evsel; 1050 break; 1051 } 1052 } 1053 1054 return err; 1055 } 1056 1057 int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1058 { 1059 struct evsel *evsel; 1060 int err = 0; 1061 1062 if (filter == NULL) 1063 return -1; 1064 1065 evlist__for_each_entry(evlist, evsel) { 1066 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1067 continue; 1068 1069 err = evsel__set_filter(evsel, filter); 1070 if (err) 1071 break; 1072 } 1073 1074 return err; 1075 } 1076 1077 int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 1078 { 1079 struct evsel *evsel; 1080 int err = 0; 1081 1082 if (filter == NULL) 1083 return -1; 1084 1085 evlist__for_each_entry(evlist, evsel) { 1086 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1087 continue; 1088 1089 err = evsel__append_tp_filter(evsel, filter); 1090 if (err) 1091 break; 1092 } 1093 1094 return err; 1095 } 1096 1097 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1098 { 1099 char *filter; 1100 size_t i; 1101 1102 for (i = 0; i < npids; ++i) { 1103 if (i == 0) { 1104 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1105 return NULL; 1106 } else { 1107 char *tmp; 1108 1109 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1110 goto out_free; 1111 1112 free(filter); 1113 filter = tmp; 1114 } 1115 } 1116 1117 return filter; 1118 out_free: 1119 free(filter); 1120 return NULL; 1121 } 1122 1123 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1124 { 1125 char *filter = asprintf__tp_filter_pids(npids, pids); 1126 int ret = evlist__set_tp_filter(evlist, filter); 1127 1128 free(filter); 1129 return ret; 1130 } 1131 1132 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1133 { 1134 return evlist__set_tp_filter_pids(evlist, 1, &pid); 1135 } 1136 1137 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1138 { 1139 char *filter = asprintf__tp_filter_pids(npids, pids); 1140 int ret = evlist__append_tp_filter(evlist, filter); 1141 1142 free(filter); 1143 return ret; 1144 } 1145 1146 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 1147 { 1148 return evlist__append_tp_filter_pids(evlist, 1, &pid); 1149 } 1150 1151 bool evlist__valid_sample_type(struct evlist *evlist) 1152 { 1153 struct evsel *pos; 1154 1155 if (evlist->core.nr_entries == 1) 1156 return true; 1157 1158 if (evlist->id_pos < 0 || evlist->is_pos < 0) 1159 return false; 1160 1161 evlist__for_each_entry(evlist, pos) { 1162 if (pos->id_pos != evlist->id_pos || 1163 pos->is_pos != evlist->is_pos) 1164 return false; 1165 } 1166 1167 return true; 1168 } 1169 1170 u64 __evlist__combined_sample_type(struct evlist *evlist) 1171 { 1172 struct evsel *evsel; 1173 1174 if (evlist->combined_sample_type) 1175 return evlist->combined_sample_type; 1176 1177 evlist__for_each_entry(evlist, evsel) 1178 evlist->combined_sample_type |= evsel->core.attr.sample_type; 1179 1180 return evlist->combined_sample_type; 1181 } 1182 1183 u64 evlist__combined_sample_type(struct evlist *evlist) 1184 { 1185 evlist->combined_sample_type = 0; 1186 return __evlist__combined_sample_type(evlist); 1187 } 1188 1189 u64 evlist__combined_branch_type(struct evlist *evlist) 1190 { 1191 struct evsel *evsel; 1192 u64 branch_type = 0; 1193 1194 evlist__for_each_entry(evlist, evsel) 1195 branch_type |= evsel->core.attr.branch_sample_type; 1196 return branch_type; 1197 } 1198 1199 bool evlist__valid_read_format(struct evlist *evlist) 1200 { 1201 struct evsel *first = evlist__first(evlist), *pos = first; 1202 u64 read_format = first->core.attr.read_format; 1203 u64 sample_type = first->core.attr.sample_type; 1204 1205 evlist__for_each_entry(evlist, pos) { 1206 if (read_format != pos->core.attr.read_format) { 1207 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 1208 read_format, (u64)pos->core.attr.read_format); 1209 } 1210 } 1211 1212 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 1213 if ((sample_type & PERF_SAMPLE_READ) && 1214 !(read_format & PERF_FORMAT_ID)) { 1215 return false; 1216 } 1217 1218 return true; 1219 } 1220 1221 u16 evlist__id_hdr_size(struct evlist *evlist) 1222 { 1223 struct evsel *first = evlist__first(evlist); 1224 struct perf_sample *data; 1225 u64 sample_type; 1226 u16 size = 0; 1227 1228 if (!first->core.attr.sample_id_all) 1229 goto out; 1230 1231 sample_type = first->core.attr.sample_type; 1232 1233 if (sample_type & PERF_SAMPLE_TID) 1234 size += sizeof(data->tid) * 2; 1235 1236 if (sample_type & PERF_SAMPLE_TIME) 1237 size += sizeof(data->time); 1238 1239 if (sample_type & PERF_SAMPLE_ID) 1240 size += sizeof(data->id); 1241 1242 if (sample_type & PERF_SAMPLE_STREAM_ID) 1243 size += sizeof(data->stream_id); 1244 1245 if (sample_type & PERF_SAMPLE_CPU) 1246 size += sizeof(data->cpu) * 2; 1247 1248 if (sample_type & PERF_SAMPLE_IDENTIFIER) 1249 size += sizeof(data->id); 1250 out: 1251 return size; 1252 } 1253 1254 bool evlist__valid_sample_id_all(struct evlist *evlist) 1255 { 1256 struct evsel *first = evlist__first(evlist), *pos = first; 1257 1258 evlist__for_each_entry_continue(evlist, pos) { 1259 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1260 return false; 1261 } 1262 1263 return true; 1264 } 1265 1266 bool evlist__sample_id_all(struct evlist *evlist) 1267 { 1268 struct evsel *first = evlist__first(evlist); 1269 return first->core.attr.sample_id_all; 1270 } 1271 1272 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel) 1273 { 1274 evlist->selected = evsel; 1275 } 1276 1277 void evlist__close(struct evlist *evlist) 1278 { 1279 struct evsel *evsel; 1280 struct affinity affinity; 1281 int cpu, i; 1282 1283 /* 1284 * With perf record core.cpus is usually NULL. 1285 * Use the old method to handle this for now. 1286 */ 1287 if (!evlist->core.cpus) { 1288 evlist__for_each_entry_reverse(evlist, evsel) 1289 evsel__close(evsel); 1290 return; 1291 } 1292 1293 if (affinity__setup(&affinity) < 0) 1294 return; 1295 evlist__for_each_cpu(evlist, i, cpu) { 1296 affinity__set(&affinity, cpu); 1297 1298 evlist__for_each_entry_reverse(evlist, evsel) { 1299 if (evsel__cpu_iter_skip(evsel, cpu)) 1300 continue; 1301 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 1302 } 1303 } 1304 affinity__cleanup(&affinity); 1305 evlist__for_each_entry_reverse(evlist, evsel) { 1306 perf_evsel__free_fd(&evsel->core); 1307 perf_evsel__free_id(&evsel->core); 1308 } 1309 } 1310 1311 static int evlist__create_syswide_maps(struct evlist *evlist) 1312 { 1313 struct perf_cpu_map *cpus; 1314 struct perf_thread_map *threads; 1315 int err = -ENOMEM; 1316 1317 /* 1318 * Try reading /sys/devices/system/cpu/online to get 1319 * an all cpus map. 1320 * 1321 * FIXME: -ENOMEM is the best we can do here, the cpu_map 1322 * code needs an overhaul to properly forward the 1323 * error, and we may not want to do that fallback to a 1324 * default cpu identity map :-\ 1325 */ 1326 cpus = perf_cpu_map__new(NULL); 1327 if (!cpus) 1328 goto out; 1329 1330 threads = perf_thread_map__new_dummy(); 1331 if (!threads) 1332 goto out_put; 1333 1334 perf_evlist__set_maps(&evlist->core, cpus, threads); 1335 1336 perf_thread_map__put(threads); 1337 out_put: 1338 perf_cpu_map__put(cpus); 1339 out: 1340 return err; 1341 } 1342 1343 int evlist__open(struct evlist *evlist) 1344 { 1345 struct evsel *evsel; 1346 int err; 1347 1348 /* 1349 * Default: one fd per CPU, all threads, aka systemwide 1350 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 1351 */ 1352 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 1353 err = evlist__create_syswide_maps(evlist); 1354 if (err < 0) 1355 goto out_err; 1356 } 1357 1358 evlist__update_id_pos(evlist); 1359 1360 evlist__for_each_entry(evlist, evsel) { 1361 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1362 if (err < 0) 1363 goto out_err; 1364 } 1365 1366 return 0; 1367 out_err: 1368 evlist__close(evlist); 1369 errno = -err; 1370 return err; 1371 } 1372 1373 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 1374 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 1375 { 1376 int child_ready_pipe[2], go_pipe[2]; 1377 char bf; 1378 1379 if (pipe(child_ready_pipe) < 0) { 1380 perror("failed to create 'ready' pipe"); 1381 return -1; 1382 } 1383 1384 if (pipe(go_pipe) < 0) { 1385 perror("failed to create 'go' pipe"); 1386 goto out_close_ready_pipe; 1387 } 1388 1389 evlist->workload.pid = fork(); 1390 if (evlist->workload.pid < 0) { 1391 perror("failed to fork"); 1392 goto out_close_pipes; 1393 } 1394 1395 if (!evlist->workload.pid) { 1396 int ret; 1397 1398 if (pipe_output) 1399 dup2(2, 1); 1400 1401 signal(SIGTERM, SIG_DFL); 1402 1403 close(child_ready_pipe[0]); 1404 close(go_pipe[1]); 1405 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 1406 1407 /* 1408 * Tell the parent we're ready to go 1409 */ 1410 close(child_ready_pipe[1]); 1411 1412 /* 1413 * Wait until the parent tells us to go. 1414 */ 1415 ret = read(go_pipe[0], &bf, 1); 1416 /* 1417 * The parent will ask for the execvp() to be performed by 1418 * writing exactly one byte, in workload.cork_fd, usually via 1419 * evlist__start_workload(). 1420 * 1421 * For cancelling the workload without actually running it, 1422 * the parent will just close workload.cork_fd, without writing 1423 * anything, i.e. read will return zero and we just exit() 1424 * here. 1425 */ 1426 if (ret != 1) { 1427 if (ret == -1) 1428 perror("unable to read pipe"); 1429 exit(ret); 1430 } 1431 1432 execvp(argv[0], (char **)argv); 1433 1434 if (exec_error) { 1435 union sigval val; 1436 1437 val.sival_int = errno; 1438 if (sigqueue(getppid(), SIGUSR1, val)) 1439 perror(argv[0]); 1440 } else 1441 perror(argv[0]); 1442 exit(-1); 1443 } 1444 1445 if (exec_error) { 1446 struct sigaction act = { 1447 .sa_flags = SA_SIGINFO, 1448 .sa_sigaction = exec_error, 1449 }; 1450 sigaction(SIGUSR1, &act, NULL); 1451 } 1452 1453 if (target__none(target)) { 1454 if (evlist->core.threads == NULL) { 1455 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 1456 __func__, __LINE__); 1457 goto out_close_pipes; 1458 } 1459 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 1460 } 1461 1462 close(child_ready_pipe[1]); 1463 close(go_pipe[0]); 1464 /* 1465 * wait for child to settle 1466 */ 1467 if (read(child_ready_pipe[0], &bf, 1) == -1) { 1468 perror("unable to read pipe"); 1469 goto out_close_pipes; 1470 } 1471 1472 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 1473 evlist->workload.cork_fd = go_pipe[1]; 1474 close(child_ready_pipe[0]); 1475 return 0; 1476 1477 out_close_pipes: 1478 close(go_pipe[0]); 1479 close(go_pipe[1]); 1480 out_close_ready_pipe: 1481 close(child_ready_pipe[0]); 1482 close(child_ready_pipe[1]); 1483 return -1; 1484 } 1485 1486 int evlist__start_workload(struct evlist *evlist) 1487 { 1488 if (evlist->workload.cork_fd > 0) { 1489 char bf = 0; 1490 int ret; 1491 /* 1492 * Remove the cork, let it rip! 1493 */ 1494 ret = write(evlist->workload.cork_fd, &bf, 1); 1495 if (ret < 0) 1496 perror("unable to write to pipe"); 1497 1498 close(evlist->workload.cork_fd); 1499 return ret; 1500 } 1501 1502 return 0; 1503 } 1504 1505 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1506 { 1507 struct evsel *evsel = evlist__event2evsel(evlist, event); 1508 1509 if (!evsel) 1510 return -EFAULT; 1511 return evsel__parse_sample(evsel, event, sample); 1512 } 1513 1514 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 1515 { 1516 struct evsel *evsel = evlist__event2evsel(evlist, event); 1517 1518 if (!evsel) 1519 return -EFAULT; 1520 return evsel__parse_sample_timestamp(evsel, event, timestamp); 1521 } 1522 1523 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1524 { 1525 int printed, value; 1526 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1527 1528 switch (err) { 1529 case EACCES: 1530 case EPERM: 1531 printed = scnprintf(buf, size, 1532 "Error:\t%s.\n" 1533 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1534 1535 value = perf_event_paranoid(); 1536 1537 printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1538 1539 if (value >= 2) { 1540 printed += scnprintf(buf + printed, size - printed, 1541 "For your workloads it needs to be <= 1\nHint:\t"); 1542 } 1543 printed += scnprintf(buf + printed, size - printed, 1544 "For system wide tracing it needs to be set to -1.\n"); 1545 1546 printed += scnprintf(buf + printed, size - printed, 1547 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 1548 "Hint:\tThe current value is %d.", value); 1549 break; 1550 case EINVAL: { 1551 struct evsel *first = evlist__first(evlist); 1552 int max_freq; 1553 1554 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1555 goto out_default; 1556 1557 if (first->core.attr.sample_freq < (u64)max_freq) 1558 goto out_default; 1559 1560 printed = scnprintf(buf, size, 1561 "Error:\t%s.\n" 1562 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1563 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1564 emsg, max_freq, first->core.attr.sample_freq); 1565 break; 1566 } 1567 default: 1568 out_default: 1569 scnprintf(buf, size, "%s", emsg); 1570 break; 1571 } 1572 1573 return 0; 1574 } 1575 1576 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1577 { 1578 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1579 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1580 1581 switch (err) { 1582 case EPERM: 1583 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1584 printed += scnprintf(buf + printed, size - printed, 1585 "Error:\t%s.\n" 1586 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1587 "Hint:\tTried using %zd kB.\n", 1588 emsg, pages_max_per_user, pages_attempted); 1589 1590 if (pages_attempted >= pages_max_per_user) { 1591 printed += scnprintf(buf + printed, size - printed, 1592 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1593 pages_max_per_user + pages_attempted); 1594 } 1595 1596 printed += scnprintf(buf + printed, size - printed, 1597 "Hint:\tTry using a smaller -m/--mmap-pages value."); 1598 break; 1599 default: 1600 scnprintf(buf, size, "%s", emsg); 1601 break; 1602 } 1603 1604 return 0; 1605 } 1606 1607 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel) 1608 { 1609 struct evsel *evsel, *n; 1610 LIST_HEAD(move); 1611 1612 if (move_evsel == evlist__first(evlist)) 1613 return; 1614 1615 evlist__for_each_entry_safe(evlist, n, evsel) { 1616 if (evsel->leader == move_evsel->leader) 1617 list_move_tail(&evsel->core.node, &move); 1618 } 1619 1620 list_splice(&move, &evlist->core.entries); 1621 } 1622 1623 struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1624 { 1625 struct evsel *evsel; 1626 1627 evlist__for_each_entry(evlist, evsel) { 1628 if (evsel->tracking) 1629 return evsel; 1630 } 1631 1632 return evlist__first(evlist); 1633 } 1634 1635 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 1636 { 1637 struct evsel *evsel; 1638 1639 if (tracking_evsel->tracking) 1640 return; 1641 1642 evlist__for_each_entry(evlist, evsel) { 1643 if (evsel != tracking_evsel) 1644 evsel->tracking = false; 1645 } 1646 1647 tracking_evsel->tracking = true; 1648 } 1649 1650 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 1651 { 1652 struct evsel *evsel; 1653 1654 evlist__for_each_entry(evlist, evsel) { 1655 if (!evsel->name) 1656 continue; 1657 if (strcmp(str, evsel->name) == 0) 1658 return evsel; 1659 } 1660 1661 return NULL; 1662 } 1663 1664 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 1665 { 1666 enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 1667 enum action { 1668 NONE, 1669 PAUSE, 1670 RESUME, 1671 } action = NONE; 1672 1673 if (!evlist->overwrite_mmap) 1674 return; 1675 1676 switch (old_state) { 1677 case BKW_MMAP_NOTREADY: { 1678 if (state != BKW_MMAP_RUNNING) 1679 goto state_err; 1680 break; 1681 } 1682 case BKW_MMAP_RUNNING: { 1683 if (state != BKW_MMAP_DATA_PENDING) 1684 goto state_err; 1685 action = PAUSE; 1686 break; 1687 } 1688 case BKW_MMAP_DATA_PENDING: { 1689 if (state != BKW_MMAP_EMPTY) 1690 goto state_err; 1691 break; 1692 } 1693 case BKW_MMAP_EMPTY: { 1694 if (state != BKW_MMAP_RUNNING) 1695 goto state_err; 1696 action = RESUME; 1697 break; 1698 } 1699 default: 1700 WARN_ONCE(1, "Shouldn't get there\n"); 1701 } 1702 1703 evlist->bkw_mmap_state = state; 1704 1705 switch (action) { 1706 case PAUSE: 1707 evlist__pause(evlist); 1708 break; 1709 case RESUME: 1710 evlist__resume(evlist); 1711 break; 1712 case NONE: 1713 default: 1714 break; 1715 } 1716 1717 state_err: 1718 return; 1719 } 1720 1721 bool evlist__exclude_kernel(struct evlist *evlist) 1722 { 1723 struct evsel *evsel; 1724 1725 evlist__for_each_entry(evlist, evsel) { 1726 if (!evsel->core.attr.exclude_kernel) 1727 return false; 1728 } 1729 1730 return true; 1731 } 1732 1733 /* 1734 * Events in data file are not collect in groups, but we still want 1735 * the group display. Set the artificial group and set the leader's 1736 * forced_leader flag to notify the display code. 1737 */ 1738 void evlist__force_leader(struct evlist *evlist) 1739 { 1740 if (!evlist->nr_groups) { 1741 struct evsel *leader = evlist__first(evlist); 1742 1743 evlist__set_leader(evlist); 1744 leader->forced_leader = true; 1745 } 1746 } 1747 1748 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close) 1749 { 1750 struct evsel *c2, *leader; 1751 bool is_open = true; 1752 1753 leader = evsel->leader; 1754 pr_debug("Weak group for %s/%d failed\n", 1755 leader->name, leader->core.nr_members); 1756 1757 /* 1758 * for_each_group_member doesn't work here because it doesn't 1759 * include the first entry. 1760 */ 1761 evlist__for_each_entry(evsel_list, c2) { 1762 if (c2 == evsel) 1763 is_open = false; 1764 if (c2->leader == leader) { 1765 if (is_open && close) 1766 perf_evsel__close(&c2->core); 1767 c2->leader = c2; 1768 c2->core.nr_members = 0; 1769 /* 1770 * Set this for all former members of the group 1771 * to indicate they get reopened. 1772 */ 1773 c2->reset_group = true; 1774 } 1775 } 1776 return leader; 1777 } 1778 1779 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1780 { 1781 char *s, *p; 1782 int ret = 0, fd; 1783 1784 if (strncmp(str, "fifo:", 5)) 1785 return -EINVAL; 1786 1787 str += 5; 1788 if (!*str || *str == ',') 1789 return -EINVAL; 1790 1791 s = strdup(str); 1792 if (!s) 1793 return -ENOMEM; 1794 1795 p = strchr(s, ','); 1796 if (p) 1797 *p = '\0'; 1798 1799 /* 1800 * O_RDWR avoids POLLHUPs which is necessary to allow the other 1801 * end of a FIFO to be repeatedly opened and closed. 1802 */ 1803 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1804 if (fd < 0) { 1805 pr_err("Failed to open '%s'\n", s); 1806 ret = -errno; 1807 goto out_free; 1808 } 1809 *ctl_fd = fd; 1810 *ctl_fd_close = true; 1811 1812 if (p && *++p) { 1813 /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1814 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1815 if (fd < 0) { 1816 pr_err("Failed to open '%s'\n", p); 1817 ret = -errno; 1818 goto out_free; 1819 } 1820 *ctl_fd_ack = fd; 1821 } 1822 1823 out_free: 1824 free(s); 1825 return ret; 1826 } 1827 1828 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1829 { 1830 char *comma = NULL, *endptr = NULL; 1831 1832 *ctl_fd_close = false; 1833 1834 if (strncmp(str, "fd:", 3)) 1835 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 1836 1837 *ctl_fd = strtoul(&str[3], &endptr, 0); 1838 if (endptr == &str[3]) 1839 return -EINVAL; 1840 1841 comma = strchr(str, ','); 1842 if (comma) { 1843 if (endptr != comma) 1844 return -EINVAL; 1845 1846 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 1847 if (endptr == comma + 1 || *endptr != '\0') 1848 return -EINVAL; 1849 } 1850 1851 return 0; 1852 } 1853 1854 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1855 { 1856 if (*ctl_fd_close) { 1857 *ctl_fd_close = false; 1858 close(ctl_fd); 1859 if (ctl_fd_ack >= 0) 1860 close(ctl_fd_ack); 1861 } 1862 } 1863 1864 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1865 { 1866 if (fd == -1) { 1867 pr_debug("Control descriptor is not initialized\n"); 1868 return 0; 1869 } 1870 1871 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1872 fdarray_flag__nonfilterable); 1873 if (evlist->ctl_fd.pos < 0) { 1874 evlist->ctl_fd.pos = -1; 1875 pr_err("Failed to add ctl fd entry: %m\n"); 1876 return -1; 1877 } 1878 1879 evlist->ctl_fd.fd = fd; 1880 evlist->ctl_fd.ack = ack; 1881 1882 return 0; 1883 } 1884 1885 bool evlist__ctlfd_initialized(struct evlist *evlist) 1886 { 1887 return evlist->ctl_fd.pos >= 0; 1888 } 1889 1890 int evlist__finalize_ctlfd(struct evlist *evlist) 1891 { 1892 struct pollfd *entries = evlist->core.pollfd.entries; 1893 1894 if (!evlist__ctlfd_initialized(evlist)) 1895 return 0; 1896 1897 entries[evlist->ctl_fd.pos].fd = -1; 1898 entries[evlist->ctl_fd.pos].events = 0; 1899 entries[evlist->ctl_fd.pos].revents = 0; 1900 1901 evlist->ctl_fd.pos = -1; 1902 evlist->ctl_fd.ack = -1; 1903 evlist->ctl_fd.fd = -1; 1904 1905 return 0; 1906 } 1907 1908 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1909 char *cmd_data, size_t data_size) 1910 { 1911 int err; 1912 char c; 1913 size_t bytes_read = 0; 1914 1915 *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1916 memset(cmd_data, 0, data_size); 1917 data_size--; 1918 1919 do { 1920 err = read(evlist->ctl_fd.fd, &c, 1); 1921 if (err > 0) { 1922 if (c == '\n' || c == '\0') 1923 break; 1924 cmd_data[bytes_read++] = c; 1925 if (bytes_read == data_size) 1926 break; 1927 continue; 1928 } else if (err == -1) { 1929 if (errno == EINTR) 1930 continue; 1931 if (errno == EAGAIN || errno == EWOULDBLOCK) 1932 err = 0; 1933 else 1934 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1935 } 1936 break; 1937 } while (1); 1938 1939 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1940 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1941 1942 if (bytes_read > 0) { 1943 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1944 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1945 *cmd = EVLIST_CTL_CMD_ENABLE; 1946 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1947 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1948 *cmd = EVLIST_CTL_CMD_DISABLE; 1949 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1950 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1951 *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1952 pr_debug("is snapshot\n"); 1953 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG, 1954 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) { 1955 *cmd = EVLIST_CTL_CMD_EVLIST; 1956 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG, 1957 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) { 1958 *cmd = EVLIST_CTL_CMD_STOP; 1959 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG, 1960 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) { 1961 *cmd = EVLIST_CTL_CMD_PING; 1962 } 1963 } 1964 1965 return bytes_read ? (int)bytes_read : err; 1966 } 1967 1968 int evlist__ctlfd_ack(struct evlist *evlist) 1969 { 1970 int err; 1971 1972 if (evlist->ctl_fd.ack == -1) 1973 return 0; 1974 1975 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1976 sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1977 if (err == -1) 1978 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1979 1980 return err; 1981 } 1982 1983 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg) 1984 { 1985 char *data = cmd_data + cmd_size; 1986 1987 /* no argument */ 1988 if (!*data) 1989 return 0; 1990 1991 /* there's argument */ 1992 if (*data == ' ') { 1993 *arg = data + 1; 1994 return 1; 1995 } 1996 1997 /* malformed */ 1998 return -1; 1999 } 2000 2001 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable) 2002 { 2003 struct evsel *evsel; 2004 char *name; 2005 int err; 2006 2007 err = get_cmd_arg(cmd_data, 2008 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 : 2009 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1, 2010 &name); 2011 if (err < 0) { 2012 pr_info("failed: wrong command\n"); 2013 return -1; 2014 } 2015 2016 if (err) { 2017 evsel = evlist__find_evsel_by_str(evlist, name); 2018 if (evsel) { 2019 if (enable) 2020 evlist__enable_evsel(evlist, name); 2021 else 2022 evlist__disable_evsel(evlist, name); 2023 pr_info("Event %s %s\n", evsel->name, 2024 enable ? "enabled" : "disabled"); 2025 } else { 2026 pr_info("failed: can't find '%s' event\n", name); 2027 } 2028 } else { 2029 if (enable) { 2030 evlist__enable(evlist); 2031 pr_info(EVLIST_ENABLED_MSG); 2032 } else { 2033 evlist__disable(evlist); 2034 pr_info(EVLIST_DISABLED_MSG); 2035 } 2036 } 2037 2038 return 0; 2039 } 2040 2041 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data) 2042 { 2043 struct perf_attr_details details = { .verbose = false, }; 2044 struct evsel *evsel; 2045 char *arg; 2046 int err; 2047 2048 err = get_cmd_arg(cmd_data, 2049 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1, 2050 &arg); 2051 if (err < 0) { 2052 pr_info("failed: wrong command\n"); 2053 return -1; 2054 } 2055 2056 if (err) { 2057 if (!strcmp(arg, "-v")) { 2058 details.verbose = true; 2059 } else if (!strcmp(arg, "-g")) { 2060 details.event_group = true; 2061 } else if (!strcmp(arg, "-F")) { 2062 details.freq = true; 2063 } else { 2064 pr_info("failed: wrong command\n"); 2065 return -1; 2066 } 2067 } 2068 2069 evlist__for_each_entry(evlist, evsel) 2070 evsel__fprintf(evsel, &details, stderr); 2071 2072 return 0; 2073 } 2074 2075 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 2076 { 2077 int err = 0; 2078 char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 2079 int ctlfd_pos = evlist->ctl_fd.pos; 2080 struct pollfd *entries = evlist->core.pollfd.entries; 2081 2082 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 2083 return 0; 2084 2085 if (entries[ctlfd_pos].revents & POLLIN) { 2086 err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 2087 EVLIST_CTL_CMD_MAX_LEN); 2088 if (err > 0) { 2089 switch (*cmd) { 2090 case EVLIST_CTL_CMD_ENABLE: 2091 case EVLIST_CTL_CMD_DISABLE: 2092 err = evlist__ctlfd_enable(evlist, cmd_data, 2093 *cmd == EVLIST_CTL_CMD_ENABLE); 2094 break; 2095 case EVLIST_CTL_CMD_EVLIST: 2096 err = evlist__ctlfd_list(evlist, cmd_data); 2097 break; 2098 case EVLIST_CTL_CMD_SNAPSHOT: 2099 case EVLIST_CTL_CMD_STOP: 2100 case EVLIST_CTL_CMD_PING: 2101 break; 2102 case EVLIST_CTL_CMD_ACK: 2103 case EVLIST_CTL_CMD_UNSUPPORTED: 2104 default: 2105 pr_debug("ctlfd: unsupported %d\n", *cmd); 2106 break; 2107 } 2108 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 2109 *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 2110 evlist__ctlfd_ack(evlist); 2111 } 2112 } 2113 2114 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 2115 evlist__finalize_ctlfd(evlist); 2116 else 2117 entries[ctlfd_pos].revents = 0; 2118 2119 return err; 2120 } 2121 2122 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 2123 { 2124 struct evsel *evsel; 2125 2126 evlist__for_each_entry(evlist, evsel) { 2127 if (evsel->idx == idx) 2128 return evsel; 2129 } 2130 return NULL; 2131 } 2132