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