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