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