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