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