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 "record.h" 19 #include "debug.h" 20 #include "units.h" 21 #include "bpf_counter.h" 22 #include <internal/lib.h> // page_size 23 #include "affinity.h" 24 #include "../perf.h" 25 #include "asm/bug.h" 26 #include "bpf-event.h" 27 #include "util/event.h" 28 #include "util/string2.h" 29 #include "util/perf_api_probe.h" 30 #include "util/evsel_fprintf.h" 31 #include "util/pmu.h" 32 #include "util/sample.h" 33 #include "util/bpf-filter.h" 34 #include "util/stat.h" 35 #include "util/util.h" 36 #include "util/env.h" 37 #include "util/intel-tpebs.h" 38 #include <signal.h> 39 #include <unistd.h> 40 #include <sched.h> 41 #include <stdlib.h> 42 43 #include "parse-events.h" 44 #include <subcmd/parse-options.h> 45 46 #include <fcntl.h> 47 #include <sys/ioctl.h> 48 #include <sys/mman.h> 49 #include <sys/prctl.h> 50 #include <sys/timerfd.h> 51 #include <sys/wait.h> 52 53 #include <linux/bitops.h> 54 #include <linux/hash.h> 55 #include <linux/log2.h> 56 #include <linux/err.h> 57 #include <linux/string.h> 58 #include <linux/time64.h> 59 #include <linux/zalloc.h> 60 #include <perf/evlist.h> 61 #include <perf/evsel.h> 62 #include <perf/cpumap.h> 63 #include <perf/mmap.h> 64 65 #include <internal/xyarray.h> 66 67 #ifdef LACKS_SIGQUEUE_PROTOTYPE 68 int sigqueue(pid_t pid, int sig, const union sigval value); 69 #endif 70 71 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 72 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 73 74 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 75 struct perf_thread_map *threads) 76 { 77 perf_evlist__init(&evlist->core); 78 perf_evlist__set_maps(&evlist->core, cpus, threads); 79 evlist->workload.pid = -1; 80 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 81 evlist->ctl_fd.fd = -1; 82 evlist->ctl_fd.ack = -1; 83 evlist->ctl_fd.pos = -1; 84 evlist->nr_br_cntr = -1; 85 } 86 87 struct evlist *evlist__new(void) 88 { 89 struct evlist *evlist = zalloc(sizeof(*evlist)); 90 91 if (evlist != NULL) 92 evlist__init(evlist, NULL, NULL); 93 94 return evlist; 95 } 96 97 struct evlist *evlist__new_default(void) 98 { 99 struct evlist *evlist = evlist__new(); 100 bool can_profile_kernel; 101 int err; 102 103 if (!evlist) 104 return NULL; 105 106 can_profile_kernel = perf_event_paranoid_check(1); 107 err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu"); 108 if (err) { 109 evlist__delete(evlist); 110 return NULL; 111 } 112 113 if (evlist->core.nr_entries > 1) { 114 struct evsel *evsel; 115 116 evlist__for_each_entry(evlist, evsel) 117 evsel__set_sample_id(evsel, /*can_sample_identifier=*/false); 118 } 119 120 return evlist; 121 } 122 123 struct evlist *evlist__new_dummy(void) 124 { 125 struct evlist *evlist = evlist__new(); 126 127 if (evlist && evlist__add_dummy(evlist)) { 128 evlist__delete(evlist); 129 evlist = NULL; 130 } 131 132 return evlist; 133 } 134 135 /** 136 * evlist__set_id_pos - set the positions of event ids. 137 * @evlist: selected event list 138 * 139 * Events with compatible sample types all have the same id_pos 140 * and is_pos. For convenience, put a copy on evlist. 141 */ 142 void evlist__set_id_pos(struct evlist *evlist) 143 { 144 struct evsel *first = evlist__first(evlist); 145 146 evlist->id_pos = first->id_pos; 147 evlist->is_pos = first->is_pos; 148 } 149 150 static void evlist__update_id_pos(struct evlist *evlist) 151 { 152 struct evsel *evsel; 153 154 evlist__for_each_entry(evlist, evsel) 155 evsel__calc_id_pos(evsel); 156 157 evlist__set_id_pos(evlist); 158 } 159 160 static void evlist__purge(struct evlist *evlist) 161 { 162 struct evsel *pos, *n; 163 164 evlist__for_each_entry_safe(evlist, n, pos) { 165 list_del_init(&pos->core.node); 166 pos->evlist = NULL; 167 evsel__delete(pos); 168 } 169 170 evlist->core.nr_entries = 0; 171 } 172 173 void evlist__exit(struct evlist *evlist) 174 { 175 event_enable_timer__exit(&evlist->eet); 176 zfree(&evlist->mmap); 177 zfree(&evlist->overwrite_mmap); 178 perf_evlist__exit(&evlist->core); 179 } 180 181 void evlist__delete(struct evlist *evlist) 182 { 183 if (evlist == NULL) 184 return; 185 186 tpebs_delete(); 187 evlist__free_stats(evlist); 188 evlist__munmap(evlist); 189 evlist__close(evlist); 190 evlist__purge(evlist); 191 evlist__exit(evlist); 192 free(evlist); 193 } 194 195 void evlist__add(struct evlist *evlist, struct evsel *entry) 196 { 197 perf_evlist__add(&evlist->core, &entry->core); 198 entry->evlist = evlist; 199 entry->tracking = !entry->core.idx; 200 201 if (evlist->core.nr_entries == 1) 202 evlist__set_id_pos(evlist); 203 } 204 205 void evlist__remove(struct evlist *evlist, struct evsel *evsel) 206 { 207 evsel->evlist = NULL; 208 perf_evlist__remove(&evlist->core, &evsel->core); 209 } 210 211 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) 212 { 213 while (!list_empty(list)) { 214 struct evsel *evsel, *temp, *leader = NULL; 215 216 __evlist__for_each_entry_safe(list, temp, evsel) { 217 list_del_init(&evsel->core.node); 218 evlist__add(evlist, evsel); 219 leader = evsel; 220 break; 221 } 222 223 __evlist__for_each_entry_safe(list, temp, evsel) { 224 if (evsel__has_leader(evsel, leader)) { 225 list_del_init(&evsel->core.node); 226 evlist__add(evlist, evsel); 227 } 228 } 229 } 230 } 231 232 int __evlist__set_tracepoints_handlers(struct evlist *evlist, 233 const struct evsel_str_handler *assocs, size_t nr_assocs) 234 { 235 size_t i; 236 int err; 237 238 for (i = 0; i < nr_assocs; i++) { 239 // Adding a handler for an event not in this evlist, just ignore it. 240 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name); 241 if (evsel == NULL) 242 continue; 243 244 err = -EEXIST; 245 if (evsel->handler != NULL) 246 goto out; 247 evsel->handler = assocs[i].handler; 248 } 249 250 err = 0; 251 out: 252 return err; 253 } 254 255 static void evlist__set_leader(struct evlist *evlist) 256 { 257 perf_evlist__set_leader(&evlist->core); 258 } 259 260 static struct evsel *evlist__dummy_event(struct evlist *evlist) 261 { 262 struct perf_event_attr attr = { 263 .type = PERF_TYPE_SOFTWARE, 264 .config = PERF_COUNT_SW_DUMMY, 265 .size = sizeof(attr), /* to capture ABI version */ 266 /* Avoid frequency mode for dummy events to avoid associated timers. */ 267 .freq = 0, 268 .sample_period = 1, 269 }; 270 271 return evsel__new_idx(&attr, evlist->core.nr_entries); 272 } 273 274 int evlist__add_dummy(struct evlist *evlist) 275 { 276 struct evsel *evsel = evlist__dummy_event(evlist); 277 278 if (evsel == NULL) 279 return -ENOMEM; 280 281 evlist__add(evlist, evsel); 282 return 0; 283 } 284 285 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide) 286 { 287 struct evsel *evsel = evlist__dummy_event(evlist); 288 289 if (!evsel) 290 return NULL; 291 292 evsel->core.attr.exclude_kernel = 1; 293 evsel->core.attr.exclude_guest = 1; 294 evsel->core.attr.exclude_hv = 1; 295 evsel->core.system_wide = system_wide; 296 evsel->no_aux_samples = true; 297 evsel->name = strdup("dummy:u"); 298 299 evlist__add(evlist, evsel); 300 return evsel; 301 } 302 303 #ifdef HAVE_LIBTRACEEVENT 304 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide) 305 { 306 struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0, 307 /*format=*/true); 308 309 if (IS_ERR(evsel)) 310 return evsel; 311 312 evsel__set_sample_bit(evsel, CPU); 313 evsel__set_sample_bit(evsel, TIME); 314 315 evsel->core.system_wide = system_wide; 316 evsel->no_aux_samples = true; 317 318 evlist__add(evlist, evsel); 319 return evsel; 320 } 321 #endif 322 323 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 324 { 325 struct evsel *evsel, *n; 326 LIST_HEAD(head); 327 size_t i; 328 329 for (i = 0; i < nr_attrs; i++) { 330 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 331 if (evsel == NULL) 332 goto out_delete_partial_list; 333 list_add_tail(&evsel->core.node, &head); 334 } 335 336 evlist__splice_list_tail(evlist, &head); 337 338 return 0; 339 340 out_delete_partial_list: 341 __evlist__for_each_entry_safe(&head, n, evsel) 342 evsel__delete(evsel); 343 return -1; 344 } 345 346 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 347 { 348 size_t i; 349 350 for (i = 0; i < nr_attrs; i++) 351 event_attr_init(attrs + i); 352 353 return evlist__add_attrs(evlist, attrs, nr_attrs); 354 } 355 356 __weak int arch_evlist__add_default_attrs(struct evlist *evlist, 357 struct perf_event_attr *attrs, 358 size_t nr_attrs) 359 { 360 if (!nr_attrs) 361 return 0; 362 363 return __evlist__add_default_attrs(evlist, attrs, nr_attrs); 364 } 365 366 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 367 { 368 struct evsel *evsel; 369 370 evlist__for_each_entry(evlist, evsel) { 371 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 372 (int)evsel->core.attr.config == id) 373 return evsel; 374 } 375 376 return NULL; 377 } 378 379 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 380 { 381 struct evsel *evsel; 382 383 evlist__for_each_entry(evlist, evsel) { 384 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 385 (strcmp(evsel->name, name) == 0)) 386 return evsel; 387 } 388 389 return NULL; 390 } 391 392 #ifdef HAVE_LIBTRACEEVENT 393 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 394 { 395 struct evsel *evsel = evsel__newtp(sys, name); 396 397 if (IS_ERR(evsel)) 398 return -1; 399 400 evsel->handler = handler; 401 evlist__add(evlist, evsel); 402 return 0; 403 } 404 #endif 405 406 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity) 407 { 408 struct evlist_cpu_iterator itr = { 409 .container = evlist, 410 .evsel = NULL, 411 .cpu_map_idx = 0, 412 .evlist_cpu_map_idx = 0, 413 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus), 414 .cpu = (struct perf_cpu){ .cpu = -1}, 415 .affinity = affinity, 416 }; 417 418 if (evlist__empty(evlist)) { 419 /* Ensure the empty list doesn't iterate. */ 420 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr; 421 } else { 422 itr.evsel = evlist__first(evlist); 423 if (itr.affinity) { 424 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0); 425 affinity__set(itr.affinity, itr.cpu.cpu); 426 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu); 427 /* 428 * If this CPU isn't in the evsel's cpu map then advance 429 * through the list. 430 */ 431 if (itr.cpu_map_idx == -1) 432 evlist_cpu_iterator__next(&itr); 433 } 434 } 435 return itr; 436 } 437 438 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr) 439 { 440 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) { 441 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel); 442 evlist_cpu_itr->cpu_map_idx = 443 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus, 444 evlist_cpu_itr->cpu); 445 if (evlist_cpu_itr->cpu_map_idx != -1) 446 return; 447 } 448 evlist_cpu_itr->evlist_cpu_map_idx++; 449 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) { 450 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container); 451 evlist_cpu_itr->cpu = 452 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus, 453 evlist_cpu_itr->evlist_cpu_map_idx); 454 if (evlist_cpu_itr->affinity) 455 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu); 456 evlist_cpu_itr->cpu_map_idx = 457 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus, 458 evlist_cpu_itr->cpu); 459 /* 460 * If this CPU isn't in the evsel's cpu map then advance through 461 * the list. 462 */ 463 if (evlist_cpu_itr->cpu_map_idx == -1) 464 evlist_cpu_iterator__next(evlist_cpu_itr); 465 } 466 } 467 468 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr) 469 { 470 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr; 471 } 472 473 static int evsel__strcmp(struct evsel *pos, char *evsel_name) 474 { 475 if (!evsel_name) 476 return 0; 477 if (evsel__is_dummy_event(pos)) 478 return 1; 479 return !evsel__name_is(pos, evsel_name); 480 } 481 482 static int evlist__is_enabled(struct evlist *evlist) 483 { 484 struct evsel *pos; 485 486 evlist__for_each_entry(evlist, pos) { 487 if (!evsel__is_group_leader(pos) || !pos->core.fd) 488 continue; 489 /* If at least one event is enabled, evlist is enabled. */ 490 if (!pos->disabled) 491 return true; 492 } 493 return false; 494 } 495 496 static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy) 497 { 498 struct evsel *pos; 499 struct evlist_cpu_iterator evlist_cpu_itr; 500 struct affinity saved_affinity, *affinity = NULL; 501 bool has_imm = false; 502 503 // See explanation in evlist__close() 504 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) { 505 if (affinity__setup(&saved_affinity) < 0) 506 return; 507 affinity = &saved_affinity; 508 } 509 510 /* Disable 'immediate' events last */ 511 for (int imm = 0; imm <= 1; imm++) { 512 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) { 513 pos = evlist_cpu_itr.evsel; 514 if (evsel__strcmp(pos, evsel_name)) 515 continue; 516 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 517 continue; 518 if (excl_dummy && evsel__is_dummy_event(pos)) 519 continue; 520 if (pos->immediate) 521 has_imm = true; 522 if (pos->immediate != imm) 523 continue; 524 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx); 525 } 526 if (!has_imm) 527 break; 528 } 529 530 affinity__cleanup(affinity); 531 evlist__for_each_entry(evlist, pos) { 532 if (evsel__strcmp(pos, evsel_name)) 533 continue; 534 if (!evsel__is_group_leader(pos) || !pos->core.fd) 535 continue; 536 if (excl_dummy && evsel__is_dummy_event(pos)) 537 continue; 538 pos->disabled = true; 539 } 540 541 /* 542 * If we disabled only single event, we need to check 543 * the enabled state of the evlist manually. 544 */ 545 if (evsel_name) 546 evlist->enabled = evlist__is_enabled(evlist); 547 else 548 evlist->enabled = false; 549 } 550 551 void evlist__disable(struct evlist *evlist) 552 { 553 __evlist__disable(evlist, NULL, false); 554 } 555 556 void evlist__disable_non_dummy(struct evlist *evlist) 557 { 558 __evlist__disable(evlist, NULL, true); 559 } 560 561 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name) 562 { 563 __evlist__disable(evlist, evsel_name, false); 564 } 565 566 static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy) 567 { 568 struct evsel *pos; 569 struct evlist_cpu_iterator evlist_cpu_itr; 570 struct affinity saved_affinity, *affinity = NULL; 571 572 // See explanation in evlist__close() 573 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) { 574 if (affinity__setup(&saved_affinity) < 0) 575 return; 576 affinity = &saved_affinity; 577 } 578 579 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) { 580 pos = evlist_cpu_itr.evsel; 581 if (evsel__strcmp(pos, evsel_name)) 582 continue; 583 if (!evsel__is_group_leader(pos) || !pos->core.fd) 584 continue; 585 if (excl_dummy && evsel__is_dummy_event(pos)) 586 continue; 587 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx); 588 } 589 affinity__cleanup(affinity); 590 evlist__for_each_entry(evlist, pos) { 591 if (evsel__strcmp(pos, evsel_name)) 592 continue; 593 if (!evsel__is_group_leader(pos) || !pos->core.fd) 594 continue; 595 if (excl_dummy && evsel__is_dummy_event(pos)) 596 continue; 597 pos->disabled = false; 598 } 599 600 /* 601 * Even single event sets the 'enabled' for evlist, 602 * so the toggle can work properly and toggle to 603 * 'disabled' state. 604 */ 605 evlist->enabled = true; 606 } 607 608 void evlist__enable(struct evlist *evlist) 609 { 610 __evlist__enable(evlist, NULL, false); 611 } 612 613 void evlist__enable_non_dummy(struct evlist *evlist) 614 { 615 __evlist__enable(evlist, NULL, true); 616 } 617 618 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name) 619 { 620 __evlist__enable(evlist, evsel_name, false); 621 } 622 623 void evlist__toggle_enable(struct evlist *evlist) 624 { 625 (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 626 } 627 628 int evlist__add_pollfd(struct evlist *evlist, int fd) 629 { 630 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 631 } 632 633 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 634 { 635 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 636 } 637 638 #ifdef HAVE_EVENTFD_SUPPORT 639 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd) 640 { 641 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 642 fdarray_flag__nonfilterable | 643 fdarray_flag__non_perf_event); 644 } 645 #endif 646 647 int evlist__poll(struct evlist *evlist, int timeout) 648 { 649 return perf_evlist__poll(&evlist->core, timeout); 650 } 651 652 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 653 { 654 struct hlist_head *head; 655 struct perf_sample_id *sid; 656 int hash; 657 658 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 659 head = &evlist->core.heads[hash]; 660 661 hlist_for_each_entry(sid, head, node) 662 if (sid->id == id) 663 return sid; 664 665 return NULL; 666 } 667 668 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 669 { 670 struct perf_sample_id *sid; 671 672 if (evlist->core.nr_entries == 1 || !id) 673 return evlist__first(evlist); 674 675 sid = evlist__id2sid(evlist, id); 676 if (sid) 677 return container_of(sid->evsel, struct evsel, core); 678 679 if (!evlist__sample_id_all(evlist)) 680 return evlist__first(evlist); 681 682 return NULL; 683 } 684 685 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 686 { 687 struct perf_sample_id *sid; 688 689 if (!id) 690 return NULL; 691 692 sid = evlist__id2sid(evlist, id); 693 if (sid) 694 return container_of(sid->evsel, struct evsel, core); 695 696 return NULL; 697 } 698 699 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 700 { 701 const __u64 *array = event->sample.array; 702 ssize_t n; 703 704 n = (event->header.size - sizeof(event->header)) >> 3; 705 706 if (event->header.type == PERF_RECORD_SAMPLE) { 707 if (evlist->id_pos >= n) 708 return -1; 709 *id = array[evlist->id_pos]; 710 } else { 711 if (evlist->is_pos > n) 712 return -1; 713 n -= evlist->is_pos; 714 *id = array[n]; 715 } 716 return 0; 717 } 718 719 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 720 { 721 struct evsel *first = evlist__first(evlist); 722 struct hlist_head *head; 723 struct perf_sample_id *sid; 724 int hash; 725 u64 id; 726 727 if (evlist->core.nr_entries == 1) 728 return first; 729 730 if (!first->core.attr.sample_id_all && 731 event->header.type != PERF_RECORD_SAMPLE) 732 return first; 733 734 if (evlist__event2id(evlist, event, &id)) 735 return NULL; 736 737 /* Synthesized events have an id of zero */ 738 if (!id) 739 return first; 740 741 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 742 head = &evlist->core.heads[hash]; 743 744 hlist_for_each_entry(sid, head, node) { 745 if (sid->id == id) 746 return container_of(sid->evsel, struct evsel, core); 747 } 748 return NULL; 749 } 750 751 static int evlist__set_paused(struct evlist *evlist, bool value) 752 { 753 int i; 754 755 if (!evlist->overwrite_mmap) 756 return 0; 757 758 for (i = 0; i < evlist->core.nr_mmaps; i++) { 759 int fd = evlist->overwrite_mmap[i].core.fd; 760 int err; 761 762 if (fd < 0) 763 continue; 764 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 765 if (err) 766 return err; 767 } 768 return 0; 769 } 770 771 static int evlist__pause(struct evlist *evlist) 772 { 773 return evlist__set_paused(evlist, true); 774 } 775 776 static int evlist__resume(struct evlist *evlist) 777 { 778 return evlist__set_paused(evlist, false); 779 } 780 781 static void evlist__munmap_nofree(struct evlist *evlist) 782 { 783 int i; 784 785 if (evlist->mmap) 786 for (i = 0; i < evlist->core.nr_mmaps; i++) 787 perf_mmap__munmap(&evlist->mmap[i].core); 788 789 if (evlist->overwrite_mmap) 790 for (i = 0; i < evlist->core.nr_mmaps; i++) 791 perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 792 } 793 794 void evlist__munmap(struct evlist *evlist) 795 { 796 evlist__munmap_nofree(evlist); 797 zfree(&evlist->mmap); 798 zfree(&evlist->overwrite_mmap); 799 } 800 801 static void perf_mmap__unmap_cb(struct perf_mmap *map) 802 { 803 struct mmap *m = container_of(map, struct mmap, core); 804 805 mmap__munmap(m); 806 } 807 808 static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 809 bool overwrite) 810 { 811 int i; 812 struct mmap *map; 813 814 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 815 if (!map) 816 return NULL; 817 818 for (i = 0; i < evlist->core.nr_mmaps; i++) { 819 struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 820 821 /* 822 * When the perf_mmap() call is made we grab one refcount, plus 823 * one extra to let perf_mmap__consume() get the last 824 * events after all real references (perf_mmap__get()) are 825 * dropped. 826 * 827 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 828 * thus does perf_mmap__get() on it. 829 */ 830 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 831 } 832 833 return map; 834 } 835 836 static void 837 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 838 struct perf_evsel *_evsel, 839 struct perf_mmap_param *_mp, 840 int idx) 841 { 842 struct evlist *evlist = container_of(_evlist, struct evlist, core); 843 struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 844 struct evsel *evsel = container_of(_evsel, struct evsel, core); 845 846 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx); 847 } 848 849 static struct perf_mmap* 850 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 851 { 852 struct evlist *evlist = container_of(_evlist, struct evlist, core); 853 struct mmap *maps; 854 855 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 856 857 if (!maps) { 858 maps = evlist__alloc_mmap(evlist, overwrite); 859 if (!maps) 860 return NULL; 861 862 if (overwrite) { 863 evlist->overwrite_mmap = maps; 864 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 865 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 866 } else { 867 evlist->mmap = maps; 868 } 869 } 870 871 return &maps[idx].core; 872 } 873 874 static int 875 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 876 int output, struct perf_cpu cpu) 877 { 878 struct mmap *map = container_of(_map, struct mmap, core); 879 struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 880 881 return mmap__mmap(map, mp, output, cpu); 882 } 883 884 unsigned long perf_event_mlock_kb_in_pages(void) 885 { 886 unsigned long pages; 887 int max; 888 889 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 890 /* 891 * Pick a once upon a time good value, i.e. things look 892 * strange since we can't read a sysctl value, but lets not 893 * die yet... 894 */ 895 max = 512; 896 } else { 897 max -= (page_size / 1024); 898 } 899 900 pages = (max * 1024) / page_size; 901 if (!is_power_of_2(pages)) 902 pages = rounddown_pow_of_two(pages); 903 904 return pages; 905 } 906 907 size_t evlist__mmap_size(unsigned long pages) 908 { 909 if (pages == UINT_MAX) 910 pages = perf_event_mlock_kb_in_pages(); 911 else if (!is_power_of_2(pages)) 912 return 0; 913 914 return (pages + 1) * page_size; 915 } 916 917 static long parse_pages_arg(const char *str, unsigned long min, 918 unsigned long max) 919 { 920 unsigned long pages, val; 921 static struct parse_tag tags[] = { 922 { .tag = 'B', .mult = 1 }, 923 { .tag = 'K', .mult = 1 << 10 }, 924 { .tag = 'M', .mult = 1 << 20 }, 925 { .tag = 'G', .mult = 1 << 30 }, 926 { .tag = 0 }, 927 }; 928 929 if (str == NULL) 930 return -EINVAL; 931 932 val = parse_tag_value(str, tags); 933 if (val != (unsigned long) -1) { 934 /* we got file size value */ 935 pages = PERF_ALIGN(val, page_size) / page_size; 936 } else { 937 /* we got pages count value */ 938 char *eptr; 939 pages = strtoul(str, &eptr, 10); 940 if (*eptr != '\0') 941 return -EINVAL; 942 } 943 944 if (pages == 0 && min == 0) { 945 /* leave number of pages at 0 */ 946 } else if (!is_power_of_2(pages)) { 947 char buf[100]; 948 949 /* round pages up to next power of 2 */ 950 pages = roundup_pow_of_two(pages); 951 if (!pages) 952 return -EINVAL; 953 954 unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 955 pr_info("rounding mmap pages size to %s (%lu pages)\n", 956 buf, pages); 957 } 958 959 if (pages > max) 960 return -EINVAL; 961 962 return pages; 963 } 964 965 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 966 { 967 unsigned long max = UINT_MAX; 968 long pages; 969 970 if (max > SIZE_MAX / page_size) 971 max = SIZE_MAX / page_size; 972 973 pages = parse_pages_arg(str, 1, max); 974 if (pages < 0) { 975 pr_err("Invalid argument for --mmap_pages/-m\n"); 976 return -1; 977 } 978 979 *mmap_pages = pages; 980 return 0; 981 } 982 983 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused) 984 { 985 return __evlist__parse_mmap_pages(opt->value, str); 986 } 987 988 /** 989 * evlist__mmap_ex - Create mmaps to receive events. 990 * @evlist: list of events 991 * @pages: map length in pages 992 * @overwrite: overwrite older events? 993 * @auxtrace_pages - auxtrace map length in pages 994 * @auxtrace_overwrite - overwrite older auxtrace data? 995 * 996 * If @overwrite is %false the user needs to signal event consumption using 997 * perf_mmap__write_tail(). Using evlist__mmap_read() does this 998 * automatically. 999 * 1000 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1001 * consumption using auxtrace_mmap__write_tail(). 1002 * 1003 * Return: %0 on success, negative error code otherwise. 1004 */ 1005 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 1006 unsigned int auxtrace_pages, 1007 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 1008 int comp_level) 1009 { 1010 /* 1011 * Delay setting mp.prot: set it before calling perf_mmap__mmap. 1012 * Its value is decided by evsel's write_backward. 1013 * So &mp should not be passed through const pointer. 1014 */ 1015 struct mmap_params mp = { 1016 .nr_cblocks = nr_cblocks, 1017 .affinity = affinity, 1018 .flush = flush, 1019 .comp_level = comp_level 1020 }; 1021 struct perf_evlist_mmap_ops ops = { 1022 .idx = perf_evlist__mmap_cb_idx, 1023 .get = perf_evlist__mmap_cb_get, 1024 .mmap = perf_evlist__mmap_cb_mmap, 1025 }; 1026 1027 evlist->core.mmap_len = evlist__mmap_size(pages); 1028 pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 1029 1030 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 1031 auxtrace_pages, auxtrace_overwrite); 1032 1033 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 1034 } 1035 1036 int evlist__mmap(struct evlist *evlist, unsigned int pages) 1037 { 1038 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 1039 } 1040 1041 int evlist__create_maps(struct evlist *evlist, struct target *target) 1042 { 1043 bool all_threads = (target->per_thread && target->system_wide); 1044 struct perf_cpu_map *cpus; 1045 struct perf_thread_map *threads; 1046 1047 /* 1048 * If specify '-a' and '--per-thread' to perf record, perf record 1049 * will override '--per-thread'. target->per_thread = false and 1050 * target->system_wide = true. 1051 * 1052 * If specify '--per-thread' only to perf record, 1053 * target->per_thread = true and target->system_wide = false. 1054 * 1055 * So target->per_thread && target->system_wide is false. 1056 * For perf record, thread_map__new_str doesn't call 1057 * thread_map__new_all_cpus. That will keep perf record's 1058 * current behavior. 1059 * 1060 * For perf stat, it allows the case that target->per_thread and 1061 * target->system_wide are all true. It means to collect system-wide 1062 * per-thread data. thread_map__new_str will call 1063 * thread_map__new_all_cpus to enumerate all threads. 1064 */ 1065 threads = thread_map__new_str(target->pid, target->tid, target->uid, 1066 all_threads); 1067 1068 if (!threads) 1069 return -1; 1070 1071 if (target__uses_dummy_map(target) && !evlist__has_bpf_output(evlist)) 1072 cpus = perf_cpu_map__new_any_cpu(); 1073 else 1074 cpus = perf_cpu_map__new(target->cpu_list); 1075 1076 if (!cpus) 1077 goto out_delete_threads; 1078 1079 evlist->core.has_user_cpus = !!target->cpu_list; 1080 1081 perf_evlist__set_maps(&evlist->core, cpus, threads); 1082 1083 /* as evlist now has references, put count here */ 1084 perf_cpu_map__put(cpus); 1085 perf_thread_map__put(threads); 1086 1087 return 0; 1088 1089 out_delete_threads: 1090 perf_thread_map__put(threads); 1091 return -1; 1092 } 1093 1094 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel, 1095 struct target *target) 1096 { 1097 struct evsel *evsel; 1098 int err = 0; 1099 1100 evlist__for_each_entry(evlist, evsel) { 1101 /* 1102 * filters only work for tracepoint event, which doesn't have cpu limit. 1103 * So evlist and evsel should always be same. 1104 */ 1105 if (evsel->filter) { 1106 err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 1107 if (err) { 1108 *err_evsel = evsel; 1109 break; 1110 } 1111 } 1112 1113 /* 1114 * non-tracepoint events can have BPF filters. 1115 */ 1116 if (!list_empty(&evsel->bpf_filters)) { 1117 err = perf_bpf_filter__prepare(evsel, target); 1118 if (err) { 1119 *err_evsel = evsel; 1120 break; 1121 } 1122 } 1123 } 1124 1125 return err; 1126 } 1127 1128 int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1129 { 1130 struct evsel *evsel; 1131 int err = 0; 1132 1133 if (filter == NULL) 1134 return -1; 1135 1136 evlist__for_each_entry(evlist, evsel) { 1137 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1138 continue; 1139 1140 err = evsel__set_filter(evsel, filter); 1141 if (err) 1142 break; 1143 } 1144 1145 return err; 1146 } 1147 1148 int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 1149 { 1150 struct evsel *evsel; 1151 int err = 0; 1152 1153 if (filter == NULL) 1154 return -1; 1155 1156 evlist__for_each_entry(evlist, evsel) { 1157 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1158 continue; 1159 1160 err = evsel__append_tp_filter(evsel, filter); 1161 if (err) 1162 break; 1163 } 1164 1165 return err; 1166 } 1167 1168 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1169 { 1170 char *filter; 1171 size_t i; 1172 1173 for (i = 0; i < npids; ++i) { 1174 if (i == 0) { 1175 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1176 return NULL; 1177 } else { 1178 char *tmp; 1179 1180 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1181 goto out_free; 1182 1183 free(filter); 1184 filter = tmp; 1185 } 1186 } 1187 1188 return filter; 1189 out_free: 1190 free(filter); 1191 return NULL; 1192 } 1193 1194 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1195 { 1196 char *filter = asprintf__tp_filter_pids(npids, pids); 1197 int ret = evlist__set_tp_filter(evlist, filter); 1198 1199 free(filter); 1200 return ret; 1201 } 1202 1203 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1204 { 1205 return evlist__set_tp_filter_pids(evlist, 1, &pid); 1206 } 1207 1208 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1209 { 1210 char *filter = asprintf__tp_filter_pids(npids, pids); 1211 int ret = evlist__append_tp_filter(evlist, filter); 1212 1213 free(filter); 1214 return ret; 1215 } 1216 1217 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 1218 { 1219 return evlist__append_tp_filter_pids(evlist, 1, &pid); 1220 } 1221 1222 bool evlist__valid_sample_type(struct evlist *evlist) 1223 { 1224 struct evsel *pos; 1225 1226 if (evlist->core.nr_entries == 1) 1227 return true; 1228 1229 if (evlist->id_pos < 0 || evlist->is_pos < 0) 1230 return false; 1231 1232 evlist__for_each_entry(evlist, pos) { 1233 if (pos->id_pos != evlist->id_pos || 1234 pos->is_pos != evlist->is_pos) 1235 return false; 1236 } 1237 1238 return true; 1239 } 1240 1241 u64 __evlist__combined_sample_type(struct evlist *evlist) 1242 { 1243 struct evsel *evsel; 1244 1245 if (evlist->combined_sample_type) 1246 return evlist->combined_sample_type; 1247 1248 evlist__for_each_entry(evlist, evsel) 1249 evlist->combined_sample_type |= evsel->core.attr.sample_type; 1250 1251 return evlist->combined_sample_type; 1252 } 1253 1254 u64 evlist__combined_sample_type(struct evlist *evlist) 1255 { 1256 evlist->combined_sample_type = 0; 1257 return __evlist__combined_sample_type(evlist); 1258 } 1259 1260 u64 evlist__combined_branch_type(struct evlist *evlist) 1261 { 1262 struct evsel *evsel; 1263 u64 branch_type = 0; 1264 1265 evlist__for_each_entry(evlist, evsel) 1266 branch_type |= evsel->core.attr.branch_sample_type; 1267 return branch_type; 1268 } 1269 1270 static struct evsel * 1271 evlist__find_dup_event_from_prev(struct evlist *evlist, struct evsel *event) 1272 { 1273 struct evsel *pos; 1274 1275 evlist__for_each_entry(evlist, pos) { 1276 if (event == pos) 1277 break; 1278 if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) && 1279 !strcmp(pos->name, event->name)) 1280 return pos; 1281 } 1282 return NULL; 1283 } 1284 1285 #define MAX_NR_ABBR_NAME (26 * 11) 1286 1287 /* 1288 * The abbr name is from A to Z9. If the number of event 1289 * which requires the branch counter > MAX_NR_ABBR_NAME, 1290 * return NA. 1291 */ 1292 static void evlist__new_abbr_name(char *name) 1293 { 1294 static int idx; 1295 int i = idx / 26; 1296 1297 if (idx >= MAX_NR_ABBR_NAME) { 1298 name[0] = 'N'; 1299 name[1] = 'A'; 1300 name[2] = '\0'; 1301 return; 1302 } 1303 1304 name[0] = 'A' + (idx % 26); 1305 1306 if (!i) 1307 name[1] = '\0'; 1308 else { 1309 name[1] = '0' + i - 1; 1310 name[2] = '\0'; 1311 } 1312 1313 idx++; 1314 } 1315 1316 void evlist__update_br_cntr(struct evlist *evlist) 1317 { 1318 struct evsel *evsel, *dup; 1319 int i = 0; 1320 1321 evlist__for_each_entry(evlist, evsel) { 1322 if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) { 1323 evsel->br_cntr_idx = i++; 1324 evsel__leader(evsel)->br_cntr_nr++; 1325 1326 dup = evlist__find_dup_event_from_prev(evlist, evsel); 1327 if (dup) 1328 memcpy(evsel->abbr_name, dup->abbr_name, 3 * sizeof(char)); 1329 else 1330 evlist__new_abbr_name(evsel->abbr_name); 1331 } 1332 } 1333 evlist->nr_br_cntr = i; 1334 } 1335 1336 bool evlist__valid_read_format(struct evlist *evlist) 1337 { 1338 struct evsel *first = evlist__first(evlist), *pos = first; 1339 u64 read_format = first->core.attr.read_format; 1340 u64 sample_type = first->core.attr.sample_type; 1341 1342 evlist__for_each_entry(evlist, pos) { 1343 if (read_format != pos->core.attr.read_format) { 1344 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 1345 read_format, (u64)pos->core.attr.read_format); 1346 } 1347 } 1348 1349 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */ 1350 if ((sample_type & PERF_SAMPLE_READ) && 1351 !(read_format & PERF_FORMAT_ID)) { 1352 return false; 1353 } 1354 1355 return true; 1356 } 1357 1358 u16 evlist__id_hdr_size(struct evlist *evlist) 1359 { 1360 struct evsel *first = evlist__first(evlist); 1361 1362 return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0; 1363 } 1364 1365 bool evlist__valid_sample_id_all(struct evlist *evlist) 1366 { 1367 struct evsel *first = evlist__first(evlist), *pos = first; 1368 1369 evlist__for_each_entry_continue(evlist, pos) { 1370 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1371 return false; 1372 } 1373 1374 return true; 1375 } 1376 1377 bool evlist__sample_id_all(struct evlist *evlist) 1378 { 1379 struct evsel *first = evlist__first(evlist); 1380 return first->core.attr.sample_id_all; 1381 } 1382 1383 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel) 1384 { 1385 evlist->selected = evsel; 1386 } 1387 1388 void evlist__close(struct evlist *evlist) 1389 { 1390 struct evsel *evsel; 1391 struct evlist_cpu_iterator evlist_cpu_itr; 1392 struct affinity affinity; 1393 1394 /* 1395 * With perf record core.user_requested_cpus is usually NULL. 1396 * Use the old method to handle this for now. 1397 */ 1398 if (!evlist->core.user_requested_cpus || 1399 cpu_map__is_dummy(evlist->core.user_requested_cpus)) { 1400 evlist__for_each_entry_reverse(evlist, evsel) 1401 evsel__close(evsel); 1402 return; 1403 } 1404 1405 if (affinity__setup(&affinity) < 0) 1406 return; 1407 1408 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) { 1409 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core, 1410 evlist_cpu_itr.cpu_map_idx); 1411 } 1412 1413 affinity__cleanup(&affinity); 1414 evlist__for_each_entry_reverse(evlist, evsel) { 1415 perf_evsel__free_fd(&evsel->core); 1416 perf_evsel__free_id(&evsel->core); 1417 } 1418 perf_evlist__reset_id_hash(&evlist->core); 1419 } 1420 1421 static int evlist__create_syswide_maps(struct evlist *evlist) 1422 { 1423 struct perf_cpu_map *cpus; 1424 struct perf_thread_map *threads; 1425 1426 /* 1427 * Try reading /sys/devices/system/cpu/online to get 1428 * an all cpus map. 1429 * 1430 * FIXME: -ENOMEM is the best we can do here, the cpu_map 1431 * code needs an overhaul to properly forward the 1432 * error, and we may not want to do that fallback to a 1433 * default cpu identity map :-\ 1434 */ 1435 cpus = perf_cpu_map__new_online_cpus(); 1436 if (!cpus) 1437 goto out; 1438 1439 threads = perf_thread_map__new_dummy(); 1440 if (!threads) 1441 goto out_put; 1442 1443 perf_evlist__set_maps(&evlist->core, cpus, threads); 1444 1445 perf_thread_map__put(threads); 1446 out_put: 1447 perf_cpu_map__put(cpus); 1448 out: 1449 return -ENOMEM; 1450 } 1451 1452 int evlist__open(struct evlist *evlist) 1453 { 1454 struct evsel *evsel; 1455 int err; 1456 1457 /* 1458 * Default: one fd per CPU, all threads, aka systemwide 1459 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 1460 */ 1461 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) { 1462 err = evlist__create_syswide_maps(evlist); 1463 if (err < 0) 1464 goto out_err; 1465 } 1466 1467 evlist__update_id_pos(evlist); 1468 1469 evlist__for_each_entry(evlist, evsel) { 1470 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1471 if (err < 0) 1472 goto out_err; 1473 } 1474 1475 return 0; 1476 out_err: 1477 evlist__close(evlist); 1478 errno = -err; 1479 return err; 1480 } 1481 1482 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 1483 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 1484 { 1485 int child_ready_pipe[2], go_pipe[2]; 1486 char bf; 1487 1488 evlist->workload.cork_fd = -1; 1489 1490 if (pipe(child_ready_pipe) < 0) { 1491 perror("failed to create 'ready' pipe"); 1492 return -1; 1493 } 1494 1495 if (pipe(go_pipe) < 0) { 1496 perror("failed to create 'go' pipe"); 1497 goto out_close_ready_pipe; 1498 } 1499 1500 evlist->workload.pid = fork(); 1501 if (evlist->workload.pid < 0) { 1502 perror("failed to fork"); 1503 goto out_close_pipes; 1504 } 1505 1506 if (!evlist->workload.pid) { 1507 int ret; 1508 1509 if (pipe_output) 1510 dup2(2, 1); 1511 1512 signal(SIGTERM, SIG_DFL); 1513 1514 close(child_ready_pipe[0]); 1515 close(go_pipe[1]); 1516 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 1517 1518 /* 1519 * Change the name of this process not to confuse --exclude-perf users 1520 * that sees 'perf' in the window up to the execvp() and thinks that 1521 * perf samples are not being excluded. 1522 */ 1523 prctl(PR_SET_NAME, "perf-exec"); 1524 1525 /* 1526 * Tell the parent we're ready to go 1527 */ 1528 close(child_ready_pipe[1]); 1529 1530 /* 1531 * Wait until the parent tells us to go. 1532 */ 1533 ret = read(go_pipe[0], &bf, 1); 1534 /* 1535 * The parent will ask for the execvp() to be performed by 1536 * writing exactly one byte, in workload.cork_fd, usually via 1537 * evlist__start_workload(). 1538 * 1539 * For cancelling the workload without actually running it, 1540 * the parent will just close workload.cork_fd, without writing 1541 * anything, i.e. read will return zero and we just exit() 1542 * here (See evlist__cancel_workload()). 1543 */ 1544 if (ret != 1) { 1545 if (ret == -1) 1546 perror("unable to read pipe"); 1547 exit(ret); 1548 } 1549 1550 execvp(argv[0], (char **)argv); 1551 1552 if (exec_error) { 1553 union sigval val; 1554 1555 val.sival_int = errno; 1556 if (sigqueue(getppid(), SIGUSR1, val)) 1557 perror(argv[0]); 1558 } else 1559 perror(argv[0]); 1560 exit(-1); 1561 } 1562 1563 if (exec_error) { 1564 struct sigaction act = { 1565 .sa_flags = SA_SIGINFO, 1566 .sa_sigaction = exec_error, 1567 }; 1568 sigaction(SIGUSR1, &act, NULL); 1569 } 1570 1571 if (target__none(target)) { 1572 if (evlist->core.threads == NULL) { 1573 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 1574 __func__, __LINE__); 1575 goto out_close_pipes; 1576 } 1577 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 1578 } 1579 1580 close(child_ready_pipe[1]); 1581 close(go_pipe[0]); 1582 /* 1583 * wait for child to settle 1584 */ 1585 if (read(child_ready_pipe[0], &bf, 1) == -1) { 1586 perror("unable to read pipe"); 1587 goto out_close_pipes; 1588 } 1589 1590 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 1591 evlist->workload.cork_fd = go_pipe[1]; 1592 close(child_ready_pipe[0]); 1593 return 0; 1594 1595 out_close_pipes: 1596 close(go_pipe[0]); 1597 close(go_pipe[1]); 1598 out_close_ready_pipe: 1599 close(child_ready_pipe[0]); 1600 close(child_ready_pipe[1]); 1601 return -1; 1602 } 1603 1604 int evlist__start_workload(struct evlist *evlist) 1605 { 1606 if (evlist->workload.cork_fd >= 0) { 1607 char bf = 0; 1608 int ret; 1609 /* 1610 * Remove the cork, let it rip! 1611 */ 1612 ret = write(evlist->workload.cork_fd, &bf, 1); 1613 if (ret < 0) 1614 perror("unable to write to pipe"); 1615 1616 close(evlist->workload.cork_fd); 1617 evlist->workload.cork_fd = -1; 1618 return ret; 1619 } 1620 1621 return 0; 1622 } 1623 1624 void evlist__cancel_workload(struct evlist *evlist) 1625 { 1626 int status; 1627 1628 if (evlist->workload.cork_fd >= 0) { 1629 close(evlist->workload.cork_fd); 1630 evlist->workload.cork_fd = -1; 1631 waitpid(evlist->workload.pid, &status, WNOHANG); 1632 } 1633 } 1634 1635 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1636 { 1637 struct evsel *evsel = evlist__event2evsel(evlist, event); 1638 int ret; 1639 1640 if (!evsel) 1641 return -EFAULT; 1642 ret = evsel__parse_sample(evsel, event, sample); 1643 if (ret) 1644 return ret; 1645 if (perf_guest && sample->id) { 1646 struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id); 1647 1648 if (sid) { 1649 sample->machine_pid = sid->machine_pid; 1650 sample->vcpu = sid->vcpu.cpu; 1651 } 1652 } 1653 return 0; 1654 } 1655 1656 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 1657 { 1658 struct evsel *evsel = evlist__event2evsel(evlist, event); 1659 1660 if (!evsel) 1661 return -EFAULT; 1662 return evsel__parse_sample_timestamp(evsel, event, timestamp); 1663 } 1664 1665 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1666 { 1667 int printed, value; 1668 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1669 1670 switch (err) { 1671 case EACCES: 1672 case EPERM: 1673 printed = scnprintf(buf, size, 1674 "Error:\t%s.\n" 1675 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1676 1677 value = perf_event_paranoid(); 1678 1679 printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1680 1681 if (value >= 2) { 1682 printed += scnprintf(buf + printed, size - printed, 1683 "For your workloads it needs to be <= 1\nHint:\t"); 1684 } 1685 printed += scnprintf(buf + printed, size - printed, 1686 "For system wide tracing it needs to be set to -1.\n"); 1687 1688 printed += scnprintf(buf + printed, size - printed, 1689 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 1690 "Hint:\tThe current value is %d.", value); 1691 break; 1692 case EINVAL: { 1693 struct evsel *first = evlist__first(evlist); 1694 int max_freq; 1695 1696 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1697 goto out_default; 1698 1699 if (first->core.attr.sample_freq < (u64)max_freq) 1700 goto out_default; 1701 1702 printed = scnprintf(buf, size, 1703 "Error:\t%s.\n" 1704 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1705 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1706 emsg, max_freq, first->core.attr.sample_freq); 1707 break; 1708 } 1709 default: 1710 out_default: 1711 scnprintf(buf, size, "%s", emsg); 1712 break; 1713 } 1714 1715 return 0; 1716 } 1717 1718 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1719 { 1720 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1721 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1722 1723 switch (err) { 1724 case EPERM: 1725 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1726 printed += scnprintf(buf + printed, size - printed, 1727 "Error:\t%s.\n" 1728 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1729 "Hint:\tTried using %zd kB.\n", 1730 emsg, pages_max_per_user, pages_attempted); 1731 1732 if (pages_attempted >= pages_max_per_user) { 1733 printed += scnprintf(buf + printed, size - printed, 1734 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1735 pages_max_per_user + pages_attempted); 1736 } 1737 1738 printed += scnprintf(buf + printed, size - printed, 1739 "Hint:\tTry using a smaller -m/--mmap-pages value."); 1740 break; 1741 default: 1742 scnprintf(buf, size, "%s", emsg); 1743 break; 1744 } 1745 1746 return 0; 1747 } 1748 1749 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel) 1750 { 1751 struct evsel *evsel, *n; 1752 LIST_HEAD(move); 1753 1754 if (move_evsel == evlist__first(evlist)) 1755 return; 1756 1757 evlist__for_each_entry_safe(evlist, n, evsel) { 1758 if (evsel__leader(evsel) == evsel__leader(move_evsel)) 1759 list_move_tail(&evsel->core.node, &move); 1760 } 1761 1762 list_splice(&move, &evlist->core.entries); 1763 } 1764 1765 struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1766 { 1767 struct evsel *evsel; 1768 1769 evlist__for_each_entry(evlist, evsel) { 1770 if (evsel->tracking) 1771 return evsel; 1772 } 1773 1774 return evlist__first(evlist); 1775 } 1776 1777 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 1778 { 1779 struct evsel *evsel; 1780 1781 if (tracking_evsel->tracking) 1782 return; 1783 1784 evlist__for_each_entry(evlist, evsel) { 1785 if (evsel != tracking_evsel) 1786 evsel->tracking = false; 1787 } 1788 1789 tracking_evsel->tracking = true; 1790 } 1791 1792 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide) 1793 { 1794 struct evsel *evsel; 1795 1796 evsel = evlist__get_tracking_event(evlist); 1797 if (!evsel__is_dummy_event(evsel)) { 1798 evsel = evlist__add_aux_dummy(evlist, system_wide); 1799 if (!evsel) 1800 return NULL; 1801 1802 evlist__set_tracking_event(evlist, evsel); 1803 } else if (system_wide) { 1804 perf_evlist__go_system_wide(&evlist->core, &evsel->core); 1805 } 1806 1807 return evsel; 1808 } 1809 1810 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 1811 { 1812 struct evsel *evsel; 1813 1814 evlist__for_each_entry(evlist, evsel) { 1815 if (!evsel->name) 1816 continue; 1817 if (evsel__name_is(evsel, str)) 1818 return evsel; 1819 } 1820 1821 return NULL; 1822 } 1823 1824 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 1825 { 1826 enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 1827 enum action { 1828 NONE, 1829 PAUSE, 1830 RESUME, 1831 } action = NONE; 1832 1833 if (!evlist->overwrite_mmap) 1834 return; 1835 1836 switch (old_state) { 1837 case BKW_MMAP_NOTREADY: { 1838 if (state != BKW_MMAP_RUNNING) 1839 goto state_err; 1840 break; 1841 } 1842 case BKW_MMAP_RUNNING: { 1843 if (state != BKW_MMAP_DATA_PENDING) 1844 goto state_err; 1845 action = PAUSE; 1846 break; 1847 } 1848 case BKW_MMAP_DATA_PENDING: { 1849 if (state != BKW_MMAP_EMPTY) 1850 goto state_err; 1851 break; 1852 } 1853 case BKW_MMAP_EMPTY: { 1854 if (state != BKW_MMAP_RUNNING) 1855 goto state_err; 1856 action = RESUME; 1857 break; 1858 } 1859 default: 1860 WARN_ONCE(1, "Shouldn't get there\n"); 1861 } 1862 1863 evlist->bkw_mmap_state = state; 1864 1865 switch (action) { 1866 case PAUSE: 1867 evlist__pause(evlist); 1868 break; 1869 case RESUME: 1870 evlist__resume(evlist); 1871 break; 1872 case NONE: 1873 default: 1874 break; 1875 } 1876 1877 state_err: 1878 return; 1879 } 1880 1881 bool evlist__exclude_kernel(struct evlist *evlist) 1882 { 1883 struct evsel *evsel; 1884 1885 evlist__for_each_entry(evlist, evsel) { 1886 if (!evsel->core.attr.exclude_kernel) 1887 return false; 1888 } 1889 1890 return true; 1891 } 1892 1893 /* 1894 * Events in data file are not collect in groups, but we still want 1895 * the group display. Set the artificial group and set the leader's 1896 * forced_leader flag to notify the display code. 1897 */ 1898 void evlist__force_leader(struct evlist *evlist) 1899 { 1900 if (evlist__nr_groups(evlist) == 0) { 1901 struct evsel *leader = evlist__first(evlist); 1902 1903 evlist__set_leader(evlist); 1904 leader->forced_leader = true; 1905 } 1906 } 1907 1908 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close) 1909 { 1910 struct evsel *c2, *leader; 1911 bool is_open = true; 1912 1913 leader = evsel__leader(evsel); 1914 1915 pr_debug("Weak group for %s/%d failed\n", 1916 leader->name, leader->core.nr_members); 1917 1918 /* 1919 * for_each_group_member doesn't work here because it doesn't 1920 * include the first entry. 1921 */ 1922 evlist__for_each_entry(evsel_list, c2) { 1923 if (c2 == evsel) 1924 is_open = false; 1925 if (evsel__has_leader(c2, leader)) { 1926 if (is_open && close) 1927 perf_evsel__close(&c2->core); 1928 /* 1929 * We want to close all members of the group and reopen 1930 * them. Some events, like Intel topdown, require being 1931 * in a group and so keep these in the group. 1932 */ 1933 evsel__remove_from_group(c2, leader); 1934 1935 /* 1936 * Set this for all former members of the group 1937 * to indicate they get reopened. 1938 */ 1939 c2->reset_group = true; 1940 } 1941 } 1942 /* Reset the leader count if all entries were removed. */ 1943 if (leader->core.nr_members == 1) 1944 leader->core.nr_members = 0; 1945 return leader; 1946 } 1947 1948 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1949 { 1950 char *s, *p; 1951 int ret = 0, fd; 1952 1953 if (strncmp(str, "fifo:", 5)) 1954 return -EINVAL; 1955 1956 str += 5; 1957 if (!*str || *str == ',') 1958 return -EINVAL; 1959 1960 s = strdup(str); 1961 if (!s) 1962 return -ENOMEM; 1963 1964 p = strchr(s, ','); 1965 if (p) 1966 *p = '\0'; 1967 1968 /* 1969 * O_RDWR avoids POLLHUPs which is necessary to allow the other 1970 * end of a FIFO to be repeatedly opened and closed. 1971 */ 1972 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1973 if (fd < 0) { 1974 pr_err("Failed to open '%s'\n", s); 1975 ret = -errno; 1976 goto out_free; 1977 } 1978 *ctl_fd = fd; 1979 *ctl_fd_close = true; 1980 1981 if (p && *++p) { 1982 /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1983 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1984 if (fd < 0) { 1985 pr_err("Failed to open '%s'\n", p); 1986 ret = -errno; 1987 goto out_free; 1988 } 1989 *ctl_fd_ack = fd; 1990 } 1991 1992 out_free: 1993 free(s); 1994 return ret; 1995 } 1996 1997 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1998 { 1999 char *comma = NULL, *endptr = NULL; 2000 2001 *ctl_fd_close = false; 2002 2003 if (strncmp(str, "fd:", 3)) 2004 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 2005 2006 *ctl_fd = strtoul(&str[3], &endptr, 0); 2007 if (endptr == &str[3]) 2008 return -EINVAL; 2009 2010 comma = strchr(str, ','); 2011 if (comma) { 2012 if (endptr != comma) 2013 return -EINVAL; 2014 2015 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 2016 if (endptr == comma + 1 || *endptr != '\0') 2017 return -EINVAL; 2018 } 2019 2020 return 0; 2021 } 2022 2023 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 2024 { 2025 if (*ctl_fd_close) { 2026 *ctl_fd_close = false; 2027 close(ctl_fd); 2028 if (ctl_fd_ack >= 0) 2029 close(ctl_fd_ack); 2030 } 2031 } 2032 2033 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 2034 { 2035 if (fd == -1) { 2036 pr_debug("Control descriptor is not initialized\n"); 2037 return 0; 2038 } 2039 2040 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 2041 fdarray_flag__nonfilterable | 2042 fdarray_flag__non_perf_event); 2043 if (evlist->ctl_fd.pos < 0) { 2044 evlist->ctl_fd.pos = -1; 2045 pr_err("Failed to add ctl fd entry: %m\n"); 2046 return -1; 2047 } 2048 2049 evlist->ctl_fd.fd = fd; 2050 evlist->ctl_fd.ack = ack; 2051 2052 return 0; 2053 } 2054 2055 bool evlist__ctlfd_initialized(struct evlist *evlist) 2056 { 2057 return evlist->ctl_fd.pos >= 0; 2058 } 2059 2060 int evlist__finalize_ctlfd(struct evlist *evlist) 2061 { 2062 struct pollfd *entries = evlist->core.pollfd.entries; 2063 2064 if (!evlist__ctlfd_initialized(evlist)) 2065 return 0; 2066 2067 entries[evlist->ctl_fd.pos].fd = -1; 2068 entries[evlist->ctl_fd.pos].events = 0; 2069 entries[evlist->ctl_fd.pos].revents = 0; 2070 2071 evlist->ctl_fd.pos = -1; 2072 evlist->ctl_fd.ack = -1; 2073 evlist->ctl_fd.fd = -1; 2074 2075 return 0; 2076 } 2077 2078 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 2079 char *cmd_data, size_t data_size) 2080 { 2081 int err; 2082 char c; 2083 size_t bytes_read = 0; 2084 2085 *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 2086 memset(cmd_data, 0, data_size); 2087 data_size--; 2088 2089 do { 2090 err = read(evlist->ctl_fd.fd, &c, 1); 2091 if (err > 0) { 2092 if (c == '\n' || c == '\0') 2093 break; 2094 cmd_data[bytes_read++] = c; 2095 if (bytes_read == data_size) 2096 break; 2097 continue; 2098 } else if (err == -1) { 2099 if (errno == EINTR) 2100 continue; 2101 if (errno == EAGAIN || errno == EWOULDBLOCK) 2102 err = 0; 2103 else 2104 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 2105 } 2106 break; 2107 } while (1); 2108 2109 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 2110 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 2111 2112 if (bytes_read > 0) { 2113 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 2114 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 2115 *cmd = EVLIST_CTL_CMD_ENABLE; 2116 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 2117 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 2118 *cmd = EVLIST_CTL_CMD_DISABLE; 2119 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 2120 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 2121 *cmd = EVLIST_CTL_CMD_SNAPSHOT; 2122 pr_debug("is snapshot\n"); 2123 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG, 2124 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) { 2125 *cmd = EVLIST_CTL_CMD_EVLIST; 2126 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG, 2127 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) { 2128 *cmd = EVLIST_CTL_CMD_STOP; 2129 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG, 2130 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) { 2131 *cmd = EVLIST_CTL_CMD_PING; 2132 } 2133 } 2134 2135 return bytes_read ? (int)bytes_read : err; 2136 } 2137 2138 int evlist__ctlfd_ack(struct evlist *evlist) 2139 { 2140 int err; 2141 2142 if (evlist->ctl_fd.ack == -1) 2143 return 0; 2144 2145 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 2146 sizeof(EVLIST_CTL_CMD_ACK_TAG)); 2147 if (err == -1) 2148 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 2149 2150 return err; 2151 } 2152 2153 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg) 2154 { 2155 char *data = cmd_data + cmd_size; 2156 2157 /* no argument */ 2158 if (!*data) 2159 return 0; 2160 2161 /* there's argument */ 2162 if (*data == ' ') { 2163 *arg = data + 1; 2164 return 1; 2165 } 2166 2167 /* malformed */ 2168 return -1; 2169 } 2170 2171 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable) 2172 { 2173 struct evsel *evsel; 2174 char *name; 2175 int err; 2176 2177 err = get_cmd_arg(cmd_data, 2178 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 : 2179 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1, 2180 &name); 2181 if (err < 0) { 2182 pr_info("failed: wrong command\n"); 2183 return -1; 2184 } 2185 2186 if (err) { 2187 evsel = evlist__find_evsel_by_str(evlist, name); 2188 if (evsel) { 2189 if (enable) 2190 evlist__enable_evsel(evlist, name); 2191 else 2192 evlist__disable_evsel(evlist, name); 2193 pr_info("Event %s %s\n", evsel->name, 2194 enable ? "enabled" : "disabled"); 2195 } else { 2196 pr_info("failed: can't find '%s' event\n", name); 2197 } 2198 } else { 2199 if (enable) { 2200 evlist__enable(evlist); 2201 pr_info(EVLIST_ENABLED_MSG); 2202 } else { 2203 evlist__disable(evlist); 2204 pr_info(EVLIST_DISABLED_MSG); 2205 } 2206 } 2207 2208 return 0; 2209 } 2210 2211 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data) 2212 { 2213 struct perf_attr_details details = { .verbose = false, }; 2214 struct evsel *evsel; 2215 char *arg; 2216 int err; 2217 2218 err = get_cmd_arg(cmd_data, 2219 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1, 2220 &arg); 2221 if (err < 0) { 2222 pr_info("failed: wrong command\n"); 2223 return -1; 2224 } 2225 2226 if (err) { 2227 if (!strcmp(arg, "-v")) { 2228 details.verbose = true; 2229 } else if (!strcmp(arg, "-g")) { 2230 details.event_group = true; 2231 } else if (!strcmp(arg, "-F")) { 2232 details.freq = true; 2233 } else { 2234 pr_info("failed: wrong command\n"); 2235 return -1; 2236 } 2237 } 2238 2239 evlist__for_each_entry(evlist, evsel) 2240 evsel__fprintf(evsel, &details, stderr); 2241 2242 return 0; 2243 } 2244 2245 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 2246 { 2247 int err = 0; 2248 char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 2249 int ctlfd_pos = evlist->ctl_fd.pos; 2250 struct pollfd *entries = evlist->core.pollfd.entries; 2251 2252 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 2253 return 0; 2254 2255 if (entries[ctlfd_pos].revents & POLLIN) { 2256 err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 2257 EVLIST_CTL_CMD_MAX_LEN); 2258 if (err > 0) { 2259 switch (*cmd) { 2260 case EVLIST_CTL_CMD_ENABLE: 2261 case EVLIST_CTL_CMD_DISABLE: 2262 err = evlist__ctlfd_enable(evlist, cmd_data, 2263 *cmd == EVLIST_CTL_CMD_ENABLE); 2264 break; 2265 case EVLIST_CTL_CMD_EVLIST: 2266 err = evlist__ctlfd_list(evlist, cmd_data); 2267 break; 2268 case EVLIST_CTL_CMD_SNAPSHOT: 2269 case EVLIST_CTL_CMD_STOP: 2270 case EVLIST_CTL_CMD_PING: 2271 break; 2272 case EVLIST_CTL_CMD_ACK: 2273 case EVLIST_CTL_CMD_UNSUPPORTED: 2274 default: 2275 pr_debug("ctlfd: unsupported %d\n", *cmd); 2276 break; 2277 } 2278 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 2279 *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 2280 evlist__ctlfd_ack(evlist); 2281 } 2282 } 2283 2284 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 2285 evlist__finalize_ctlfd(evlist); 2286 else 2287 entries[ctlfd_pos].revents = 0; 2288 2289 return err; 2290 } 2291 2292 /** 2293 * struct event_enable_time - perf record -D/--delay single time range. 2294 * @start: start of time range to enable events in milliseconds 2295 * @end: end of time range to enable events in milliseconds 2296 * 2297 * N.B. this structure is also accessed as an array of int. 2298 */ 2299 struct event_enable_time { 2300 int start; 2301 int end; 2302 }; 2303 2304 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first) 2305 { 2306 const char *fmt = first ? "%u - %u %n" : " , %u - %u %n"; 2307 int ret, start, end, n; 2308 2309 ret = sscanf(str, fmt, &start, &end, &n); 2310 if (ret != 2 || end <= start) 2311 return -EINVAL; 2312 if (range) { 2313 range->start = start; 2314 range->end = end; 2315 } 2316 return n; 2317 } 2318 2319 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range) 2320 { 2321 int incr = !!range; 2322 bool first = true; 2323 ssize_t ret, cnt; 2324 2325 for (cnt = 0; *str; cnt++) { 2326 ret = parse_event_enable_time(str, range, first); 2327 if (ret < 0) 2328 return ret; 2329 /* Check no overlap */ 2330 if (!first && range && range->start <= range[-1].end) 2331 return -EINVAL; 2332 str += ret; 2333 range += incr; 2334 first = false; 2335 } 2336 return cnt; 2337 } 2338 2339 /** 2340 * struct event_enable_timer - control structure for perf record -D/--delay. 2341 * @evlist: event list 2342 * @times: time ranges that events are enabled (N.B. this is also accessed as an 2343 * array of int) 2344 * @times_cnt: number of time ranges 2345 * @timerfd: timer file descriptor 2346 * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray) 2347 * @times_step: current position in (int *)@times)[], 2348 * refer event_enable_timer__process() 2349 * 2350 * Note, this structure is only used when there are time ranges, not when there 2351 * is only an initial delay. 2352 */ 2353 struct event_enable_timer { 2354 struct evlist *evlist; 2355 struct event_enable_time *times; 2356 size_t times_cnt; 2357 int timerfd; 2358 int pollfd_pos; 2359 size_t times_step; 2360 }; 2361 2362 static int str_to_delay(const char *str) 2363 { 2364 char *endptr; 2365 long d; 2366 2367 d = strtol(str, &endptr, 10); 2368 if (*endptr || d > INT_MAX || d < -1) 2369 return 0; 2370 return d; 2371 } 2372 2373 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts, 2374 const char *str, int unset) 2375 { 2376 enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event; 2377 struct event_enable_timer *eet; 2378 ssize_t times_cnt; 2379 ssize_t ret; 2380 int err; 2381 2382 if (unset) 2383 return 0; 2384 2385 opts->target.initial_delay = str_to_delay(str); 2386 if (opts->target.initial_delay) 2387 return 0; 2388 2389 ret = parse_event_enable_times(str, NULL); 2390 if (ret < 0) 2391 return ret; 2392 2393 times_cnt = ret; 2394 if (times_cnt == 0) 2395 return -EINVAL; 2396 2397 eet = zalloc(sizeof(*eet)); 2398 if (!eet) 2399 return -ENOMEM; 2400 2401 eet->times = calloc(times_cnt, sizeof(*eet->times)); 2402 if (!eet->times) { 2403 err = -ENOMEM; 2404 goto free_eet; 2405 } 2406 2407 if (parse_event_enable_times(str, eet->times) != times_cnt) { 2408 err = -EINVAL; 2409 goto free_eet_times; 2410 } 2411 2412 eet->times_cnt = times_cnt; 2413 2414 eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); 2415 if (eet->timerfd == -1) { 2416 err = -errno; 2417 pr_err("timerfd_create failed: %s\n", strerror(errno)); 2418 goto free_eet_times; 2419 } 2420 2421 eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags); 2422 if (eet->pollfd_pos < 0) { 2423 err = eet->pollfd_pos; 2424 goto close_timerfd; 2425 } 2426 2427 eet->evlist = evlist; 2428 evlist->eet = eet; 2429 opts->target.initial_delay = eet->times[0].start; 2430 2431 return 0; 2432 2433 close_timerfd: 2434 close(eet->timerfd); 2435 free_eet_times: 2436 zfree(&eet->times); 2437 free_eet: 2438 free(eet); 2439 return err; 2440 } 2441 2442 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms) 2443 { 2444 struct itimerspec its = { 2445 .it_value.tv_sec = ms / MSEC_PER_SEC, 2446 .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC, 2447 }; 2448 int err = 0; 2449 2450 if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) { 2451 err = -errno; 2452 pr_err("timerfd_settime failed: %s\n", strerror(errno)); 2453 } 2454 return err; 2455 } 2456 2457 int event_enable_timer__start(struct event_enable_timer *eet) 2458 { 2459 int ms; 2460 2461 if (!eet) 2462 return 0; 2463 2464 ms = eet->times[0].end - eet->times[0].start; 2465 eet->times_step = 1; 2466 2467 return event_enable_timer__set_timer(eet, ms); 2468 } 2469 2470 int event_enable_timer__process(struct event_enable_timer *eet) 2471 { 2472 struct pollfd *entries; 2473 short revents; 2474 2475 if (!eet) 2476 return 0; 2477 2478 entries = eet->evlist->core.pollfd.entries; 2479 revents = entries[eet->pollfd_pos].revents; 2480 entries[eet->pollfd_pos].revents = 0; 2481 2482 if (revents & POLLIN) { 2483 size_t step = eet->times_step; 2484 size_t pos = step / 2; 2485 2486 if (step & 1) { 2487 evlist__disable_non_dummy(eet->evlist); 2488 pr_info(EVLIST_DISABLED_MSG); 2489 if (pos >= eet->times_cnt - 1) { 2490 /* Disarm timer */ 2491 event_enable_timer__set_timer(eet, 0); 2492 return 1; /* Stop */ 2493 } 2494 } else { 2495 evlist__enable_non_dummy(eet->evlist); 2496 pr_info(EVLIST_ENABLED_MSG); 2497 } 2498 2499 step += 1; 2500 pos = step / 2; 2501 2502 if (pos < eet->times_cnt) { 2503 int *times = (int *)eet->times; /* Accessing 'times' as array of int */ 2504 int ms = times[step] - times[step - 1]; 2505 2506 eet->times_step = step; 2507 return event_enable_timer__set_timer(eet, ms); 2508 } 2509 } 2510 2511 return 0; 2512 } 2513 2514 void event_enable_timer__exit(struct event_enable_timer **ep) 2515 { 2516 if (!ep || !*ep) 2517 return; 2518 zfree(&(*ep)->times); 2519 zfree(ep); 2520 } 2521 2522 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 2523 { 2524 struct evsel *evsel; 2525 2526 evlist__for_each_entry(evlist, evsel) { 2527 if (evsel->core.idx == idx) 2528 return evsel; 2529 } 2530 return NULL; 2531 } 2532 2533 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf) 2534 { 2535 struct evsel *evsel; 2536 int printed = 0; 2537 2538 evlist__for_each_entry(evlist, evsel) { 2539 if (evsel__is_dummy_event(evsel)) 2540 continue; 2541 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) { 2542 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel)); 2543 } else { 2544 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : ""); 2545 break; 2546 } 2547 } 2548 2549 return printed; 2550 } 2551 2552 void evlist__check_mem_load_aux(struct evlist *evlist) 2553 { 2554 struct evsel *leader, *evsel, *pos; 2555 2556 /* 2557 * For some platforms, the 'mem-loads' event is required to use 2558 * together with 'mem-loads-aux' within a group and 'mem-loads-aux' 2559 * must be the group leader. Now we disable this group before reporting 2560 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry 2561 * any valid memory load information. 2562 */ 2563 evlist__for_each_entry(evlist, evsel) { 2564 leader = evsel__leader(evsel); 2565 if (leader == evsel) 2566 continue; 2567 2568 if (leader->name && strstr(leader->name, "mem-loads-aux")) { 2569 for_each_group_evsel(pos, leader) { 2570 evsel__set_leader(pos, pos); 2571 pos->core.nr_members = 0; 2572 } 2573 } 2574 } 2575 } 2576 2577 /** 2578 * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs 2579 * and warn if the user CPU list is inapplicable for the event's PMU's 2580 * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a 2581 * user requested CPU and so any online CPU is applicable. Core PMUs handle 2582 * events on the CPUs in their list and otherwise the event isn't supported. 2583 * @evlist: The list of events being checked. 2584 * @cpu_list: The user provided list of CPUs. 2585 */ 2586 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list) 2587 { 2588 struct perf_cpu_map *user_requested_cpus; 2589 struct evsel *pos; 2590 2591 if (!cpu_list) 2592 return; 2593 2594 user_requested_cpus = perf_cpu_map__new(cpu_list); 2595 if (!user_requested_cpus) 2596 return; 2597 2598 evlist__for_each_entry(evlist, pos) { 2599 struct perf_cpu_map *intersect, *to_test; 2600 const struct perf_pmu *pmu = evsel__find_pmu(pos); 2601 2602 to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online(); 2603 intersect = perf_cpu_map__intersect(to_test, user_requested_cpus); 2604 if (!perf_cpu_map__equal(intersect, user_requested_cpus)) { 2605 char buf[128]; 2606 2607 cpu_map__snprint(to_test, buf, sizeof(buf)); 2608 pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n", 2609 cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos)); 2610 } 2611 perf_cpu_map__put(intersect); 2612 } 2613 perf_cpu_map__put(user_requested_cpus); 2614 } 2615 2616 void evlist__uniquify_name(struct evlist *evlist) 2617 { 2618 char *new_name, empty_attributes[2] = ":", *attributes; 2619 struct evsel *pos; 2620 2621 if (perf_pmus__num_core_pmus() == 1) 2622 return; 2623 2624 evlist__for_each_entry(evlist, pos) { 2625 if (!evsel__is_hybrid(pos)) 2626 continue; 2627 2628 if (strchr(pos->name, '/')) 2629 continue; 2630 2631 attributes = strchr(pos->name, ':'); 2632 if (attributes) 2633 *attributes = '\0'; 2634 else 2635 attributes = empty_attributes; 2636 2637 if (asprintf(&new_name, "%s/%s/%s", pos->pmu_name, pos->name, attributes + 1)) { 2638 free(pos->name); 2639 pos->name = new_name; 2640 } else { 2641 *attributes = ':'; 2642 } 2643 } 2644 } 2645 2646 bool evlist__has_bpf_output(struct evlist *evlist) 2647 { 2648 struct evsel *evsel; 2649 2650 evlist__for_each_entry(evlist, evsel) { 2651 if (evsel__is_bpf_output(evsel)) 2652 return true; 2653 } 2654 2655 return false; 2656 } 2657