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 "thread_map.h" 14 #include "target.h" 15 #include "evlist.h" 16 #include "evsel.h" 17 #include "debug.h" 18 #include "units.h" 19 #include "util.h" 20 #include "../perf.h" 21 #include "asm/bug.h" 22 #include "bpf-event.h" 23 #include <signal.h> 24 #include <unistd.h> 25 #include <sched.h> 26 27 #include "parse-events.h" 28 #include <subcmd/parse-options.h> 29 30 #include <fcntl.h> 31 #include <sys/ioctl.h> 32 #include <sys/mman.h> 33 34 #include <linux/bitops.h> 35 #include <linux/hash.h> 36 #include <linux/log2.h> 37 #include <linux/err.h> 38 #include <linux/zalloc.h> 39 #include <perf/evlist.h> 40 #include <perf/evsel.h> 41 #include <perf/cpumap.h> 42 43 #include <internal/xyarray.h> 44 45 #ifdef LACKS_SIGQUEUE_PROTOTYPE 46 int sigqueue(pid_t pid, int sig, const union sigval value); 47 #endif 48 49 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 50 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 51 52 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 53 struct perf_thread_map *threads) 54 { 55 int i; 56 57 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 58 INIT_HLIST_HEAD(&evlist->heads[i]); 59 perf_evlist__init(&evlist->core); 60 perf_evlist__set_maps(&evlist->core, cpus, threads); 61 fdarray__init(&evlist->pollfd, 64); 62 evlist->workload.pid = -1; 63 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 64 } 65 66 struct evlist *evlist__new(void) 67 { 68 struct evlist *evlist = zalloc(sizeof(*evlist)); 69 70 if (evlist != NULL) 71 evlist__init(evlist, NULL, NULL); 72 73 return evlist; 74 } 75 76 struct evlist *perf_evlist__new_default(void) 77 { 78 struct evlist *evlist = evlist__new(); 79 80 if (evlist && perf_evlist__add_default(evlist)) { 81 evlist__delete(evlist); 82 evlist = NULL; 83 } 84 85 return evlist; 86 } 87 88 struct evlist *perf_evlist__new_dummy(void) 89 { 90 struct evlist *evlist = evlist__new(); 91 92 if (evlist && perf_evlist__add_dummy(evlist)) { 93 evlist__delete(evlist); 94 evlist = NULL; 95 } 96 97 return evlist; 98 } 99 100 /** 101 * perf_evlist__set_id_pos - set the positions of event ids. 102 * @evlist: selected event list 103 * 104 * Events with compatible sample types all have the same id_pos 105 * and is_pos. For convenience, put a copy on evlist. 106 */ 107 void perf_evlist__set_id_pos(struct evlist *evlist) 108 { 109 struct evsel *first = perf_evlist__first(evlist); 110 111 evlist->id_pos = first->id_pos; 112 evlist->is_pos = first->is_pos; 113 } 114 115 static void perf_evlist__update_id_pos(struct evlist *evlist) 116 { 117 struct evsel *evsel; 118 119 evlist__for_each_entry(evlist, evsel) 120 perf_evsel__calc_id_pos(evsel); 121 122 perf_evlist__set_id_pos(evlist); 123 } 124 125 static void perf_evlist__purge(struct evlist *evlist) 126 { 127 struct evsel *pos, *n; 128 129 evlist__for_each_entry_safe(evlist, n, pos) { 130 list_del_init(&pos->core.node); 131 pos->evlist = NULL; 132 evsel__delete(pos); 133 } 134 135 evlist->core.nr_entries = 0; 136 } 137 138 void perf_evlist__exit(struct evlist *evlist) 139 { 140 zfree(&evlist->mmap); 141 zfree(&evlist->overwrite_mmap); 142 fdarray__exit(&evlist->pollfd); 143 } 144 145 void evlist__delete(struct evlist *evlist) 146 { 147 if (evlist == NULL) 148 return; 149 150 perf_evlist__munmap(evlist); 151 evlist__close(evlist); 152 perf_cpu_map__put(evlist->core.cpus); 153 perf_thread_map__put(evlist->core.threads); 154 evlist->core.cpus = NULL; 155 evlist->core.threads = NULL; 156 perf_evlist__purge(evlist); 157 perf_evlist__exit(evlist); 158 free(evlist); 159 } 160 161 void evlist__add(struct evlist *evlist, struct evsel *entry) 162 { 163 entry->evlist = evlist; 164 entry->idx = evlist->core.nr_entries; 165 entry->tracking = !entry->idx; 166 167 perf_evlist__add(&evlist->core, &entry->core); 168 169 if (evlist->core.nr_entries == 1) 170 perf_evlist__set_id_pos(evlist); 171 } 172 173 void evlist__remove(struct evlist *evlist, struct evsel *evsel) 174 { 175 evsel->evlist = NULL; 176 perf_evlist__remove(&evlist->core, &evsel->core); 177 } 178 179 void perf_evlist__splice_list_tail(struct evlist *evlist, 180 struct list_head *list) 181 { 182 struct evsel *evsel, *temp; 183 184 __evlist__for_each_entry_safe(list, temp, evsel) { 185 list_del_init(&evsel->core.node); 186 evlist__add(evlist, evsel); 187 } 188 } 189 190 void __perf_evlist__set_leader(struct list_head *list) 191 { 192 struct evsel *evsel, *leader; 193 194 leader = list_entry(list->next, struct evsel, core.node); 195 evsel = list_entry(list->prev, struct evsel, core.node); 196 197 leader->core.nr_members = evsel->idx - leader->idx + 1; 198 199 __evlist__for_each_entry(list, evsel) { 200 evsel->leader = leader; 201 } 202 } 203 204 void perf_evlist__set_leader(struct evlist *evlist) 205 { 206 if (evlist->core.nr_entries) { 207 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 208 __perf_evlist__set_leader(&evlist->core.entries); 209 } 210 } 211 212 int __perf_evlist__add_default(struct evlist *evlist, bool precise) 213 { 214 struct evsel *evsel = perf_evsel__new_cycles(precise); 215 216 if (evsel == NULL) 217 return -ENOMEM; 218 219 evlist__add(evlist, evsel); 220 return 0; 221 } 222 223 int perf_evlist__add_dummy(struct evlist *evlist) 224 { 225 struct perf_event_attr attr = { 226 .type = PERF_TYPE_SOFTWARE, 227 .config = PERF_COUNT_SW_DUMMY, 228 .size = sizeof(attr), /* to capture ABI version */ 229 }; 230 struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 231 232 if (evsel == NULL) 233 return -ENOMEM; 234 235 evlist__add(evlist, evsel); 236 return 0; 237 } 238 239 static int evlist__add_attrs(struct evlist *evlist, 240 struct perf_event_attr *attrs, size_t nr_attrs) 241 { 242 struct evsel *evsel, *n; 243 LIST_HEAD(head); 244 size_t i; 245 246 for (i = 0; i < nr_attrs; i++) { 247 evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 248 if (evsel == NULL) 249 goto out_delete_partial_list; 250 list_add_tail(&evsel->core.node, &head); 251 } 252 253 perf_evlist__splice_list_tail(evlist, &head); 254 255 return 0; 256 257 out_delete_partial_list: 258 __evlist__for_each_entry_safe(&head, n, evsel) 259 evsel__delete(evsel); 260 return -1; 261 } 262 263 int __perf_evlist__add_default_attrs(struct evlist *evlist, 264 struct perf_event_attr *attrs, size_t nr_attrs) 265 { 266 size_t i; 267 268 for (i = 0; i < nr_attrs; i++) 269 event_attr_init(attrs + i); 270 271 return evlist__add_attrs(evlist, attrs, nr_attrs); 272 } 273 274 struct evsel * 275 perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 276 { 277 struct evsel *evsel; 278 279 evlist__for_each_entry(evlist, evsel) { 280 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 281 (int)evsel->core.attr.config == id) 282 return evsel; 283 } 284 285 return NULL; 286 } 287 288 struct evsel * 289 perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 290 const char *name) 291 { 292 struct evsel *evsel; 293 294 evlist__for_each_entry(evlist, evsel) { 295 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 296 (strcmp(evsel->name, name) == 0)) 297 return evsel; 298 } 299 300 return NULL; 301 } 302 303 int perf_evlist__add_newtp(struct evlist *evlist, 304 const char *sys, const char *name, void *handler) 305 { 306 struct evsel *evsel = perf_evsel__newtp(sys, name); 307 308 if (IS_ERR(evsel)) 309 return -1; 310 311 evsel->handler = handler; 312 evlist__add(evlist, evsel); 313 return 0; 314 } 315 316 static int perf_evlist__nr_threads(struct evlist *evlist, 317 struct evsel *evsel) 318 { 319 if (evsel->system_wide) 320 return 1; 321 else 322 return perf_thread_map__nr(evlist->core.threads); 323 } 324 325 void evlist__disable(struct evlist *evlist) 326 { 327 struct evsel *pos; 328 329 evlist__for_each_entry(evlist, pos) { 330 if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) 331 continue; 332 evsel__disable(pos); 333 } 334 335 evlist->enabled = false; 336 } 337 338 void evlist__enable(struct evlist *evlist) 339 { 340 struct evsel *pos; 341 342 evlist__for_each_entry(evlist, pos) { 343 if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) 344 continue; 345 evsel__enable(pos); 346 } 347 348 evlist->enabled = true; 349 } 350 351 void perf_evlist__toggle_enable(struct evlist *evlist) 352 { 353 (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 354 } 355 356 static int perf_evlist__enable_event_cpu(struct evlist *evlist, 357 struct evsel *evsel, int cpu) 358 { 359 int thread; 360 int nr_threads = perf_evlist__nr_threads(evlist, evsel); 361 362 if (!evsel->core.fd) 363 return -EINVAL; 364 365 for (thread = 0; thread < nr_threads; thread++) { 366 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 367 if (err) 368 return err; 369 } 370 return 0; 371 } 372 373 static int perf_evlist__enable_event_thread(struct evlist *evlist, 374 struct evsel *evsel, 375 int thread) 376 { 377 int cpu; 378 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 379 380 if (!evsel->core.fd) 381 return -EINVAL; 382 383 for (cpu = 0; cpu < nr_cpus; cpu++) { 384 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 385 if (err) 386 return err; 387 } 388 return 0; 389 } 390 391 int perf_evlist__enable_event_idx(struct evlist *evlist, 392 struct evsel *evsel, int idx) 393 { 394 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 395 396 if (per_cpu_mmaps) 397 return perf_evlist__enable_event_cpu(evlist, evsel, idx); 398 else 399 return perf_evlist__enable_event_thread(evlist, evsel, idx); 400 } 401 402 int perf_evlist__alloc_pollfd(struct evlist *evlist) 403 { 404 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 405 int nr_threads = perf_thread_map__nr(evlist->core.threads); 406 int nfds = 0; 407 struct evsel *evsel; 408 409 evlist__for_each_entry(evlist, evsel) { 410 if (evsel->system_wide) 411 nfds += nr_cpus; 412 else 413 nfds += nr_cpus * nr_threads; 414 } 415 416 if (fdarray__available_entries(&evlist->pollfd) < nfds && 417 fdarray__grow(&evlist->pollfd, nfds) < 0) 418 return -ENOMEM; 419 420 return 0; 421 } 422 423 static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd, 424 struct perf_mmap *map, short revent) 425 { 426 int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 427 /* 428 * Save the idx so that when we filter out fds POLLHUP'ed we can 429 * close the associated evlist->mmap[] entry. 430 */ 431 if (pos >= 0) { 432 evlist->pollfd.priv[pos].ptr = map; 433 434 fcntl(fd, F_SETFL, O_NONBLOCK); 435 } 436 437 return pos; 438 } 439 440 int perf_evlist__add_pollfd(struct evlist *evlist, int fd) 441 { 442 return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 443 } 444 445 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 446 void *arg __maybe_unused) 447 { 448 struct perf_mmap *map = fda->priv[fd].ptr; 449 450 if (map) 451 perf_mmap__put(map); 452 } 453 454 int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 455 { 456 return fdarray__filter(&evlist->pollfd, revents_and_mask, 457 perf_evlist__munmap_filtered, NULL); 458 } 459 460 int perf_evlist__poll(struct evlist *evlist, int timeout) 461 { 462 return fdarray__poll(&evlist->pollfd, timeout); 463 } 464 465 static void perf_evlist__id_hash(struct evlist *evlist, 466 struct evsel *evsel, 467 int cpu, int thread, u64 id) 468 { 469 int hash; 470 struct perf_sample_id *sid = SID(evsel, cpu, thread); 471 472 sid->id = id; 473 sid->evsel = evsel; 474 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 475 hlist_add_head(&sid->node, &evlist->heads[hash]); 476 } 477 478 void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel, 479 int cpu, int thread, u64 id) 480 { 481 perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 482 evsel->id[evsel->ids++] = id; 483 } 484 485 int perf_evlist__id_add_fd(struct evlist *evlist, 486 struct evsel *evsel, 487 int cpu, int thread, int fd) 488 { 489 u64 read_data[4] = { 0, }; 490 int id_idx = 1; /* The first entry is the counter value */ 491 u64 id; 492 int ret; 493 494 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 495 if (!ret) 496 goto add; 497 498 if (errno != ENOTTY) 499 return -1; 500 501 /* Legacy way to get event id.. All hail to old kernels! */ 502 503 /* 504 * This way does not work with group format read, so bail 505 * out in that case. 506 */ 507 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 508 return -1; 509 510 if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) || 511 read(fd, &read_data, sizeof(read_data)) == -1) 512 return -1; 513 514 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 515 ++id_idx; 516 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 517 ++id_idx; 518 519 id = read_data[id_idx]; 520 521 add: 522 perf_evlist__id_add(evlist, evsel, cpu, thread, id); 523 return 0; 524 } 525 526 static void perf_evlist__set_sid_idx(struct evlist *evlist, 527 struct evsel *evsel, int idx, int cpu, 528 int thread) 529 { 530 struct perf_sample_id *sid = SID(evsel, cpu, thread); 531 sid->idx = idx; 532 if (evlist->core.cpus && cpu >= 0) 533 sid->cpu = evlist->core.cpus->map[cpu]; 534 else 535 sid->cpu = -1; 536 if (!evsel->system_wide && evlist->core.threads && thread >= 0) 537 sid->tid = perf_thread_map__pid(evlist->core.threads, thread); 538 else 539 sid->tid = -1; 540 } 541 542 struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 543 { 544 struct hlist_head *head; 545 struct perf_sample_id *sid; 546 int hash; 547 548 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 549 head = &evlist->heads[hash]; 550 551 hlist_for_each_entry(sid, head, node) 552 if (sid->id == id) 553 return sid; 554 555 return NULL; 556 } 557 558 struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 559 { 560 struct perf_sample_id *sid; 561 562 if (evlist->core.nr_entries == 1 || !id) 563 return perf_evlist__first(evlist); 564 565 sid = perf_evlist__id2sid(evlist, id); 566 if (sid) 567 return sid->evsel; 568 569 if (!perf_evlist__sample_id_all(evlist)) 570 return perf_evlist__first(evlist); 571 572 return NULL; 573 } 574 575 struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 576 u64 id) 577 { 578 struct perf_sample_id *sid; 579 580 if (!id) 581 return NULL; 582 583 sid = perf_evlist__id2sid(evlist, id); 584 if (sid) 585 return sid->evsel; 586 587 return NULL; 588 } 589 590 static int perf_evlist__event2id(struct evlist *evlist, 591 union perf_event *event, u64 *id) 592 { 593 const __u64 *array = event->sample.array; 594 ssize_t n; 595 596 n = (event->header.size - sizeof(event->header)) >> 3; 597 598 if (event->header.type == PERF_RECORD_SAMPLE) { 599 if (evlist->id_pos >= n) 600 return -1; 601 *id = array[evlist->id_pos]; 602 } else { 603 if (evlist->is_pos > n) 604 return -1; 605 n -= evlist->is_pos; 606 *id = array[n]; 607 } 608 return 0; 609 } 610 611 struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 612 union perf_event *event) 613 { 614 struct evsel *first = perf_evlist__first(evlist); 615 struct hlist_head *head; 616 struct perf_sample_id *sid; 617 int hash; 618 u64 id; 619 620 if (evlist->core.nr_entries == 1) 621 return first; 622 623 if (!first->core.attr.sample_id_all && 624 event->header.type != PERF_RECORD_SAMPLE) 625 return first; 626 627 if (perf_evlist__event2id(evlist, event, &id)) 628 return NULL; 629 630 /* Synthesized events have an id of zero */ 631 if (!id) 632 return first; 633 634 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 635 head = &evlist->heads[hash]; 636 637 hlist_for_each_entry(sid, head, node) { 638 if (sid->id == id) 639 return sid->evsel; 640 } 641 return NULL; 642 } 643 644 static int perf_evlist__set_paused(struct evlist *evlist, bool value) 645 { 646 int i; 647 648 if (!evlist->overwrite_mmap) 649 return 0; 650 651 for (i = 0; i < evlist->nr_mmaps; i++) { 652 int fd = evlist->overwrite_mmap[i].fd; 653 int err; 654 655 if (fd < 0) 656 continue; 657 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 658 if (err) 659 return err; 660 } 661 return 0; 662 } 663 664 static int perf_evlist__pause(struct evlist *evlist) 665 { 666 return perf_evlist__set_paused(evlist, true); 667 } 668 669 static int perf_evlist__resume(struct evlist *evlist) 670 { 671 return perf_evlist__set_paused(evlist, false); 672 } 673 674 static void perf_evlist__munmap_nofree(struct evlist *evlist) 675 { 676 int i; 677 678 if (evlist->mmap) 679 for (i = 0; i < evlist->nr_mmaps; i++) 680 perf_mmap__munmap(&evlist->mmap[i]); 681 682 if (evlist->overwrite_mmap) 683 for (i = 0; i < evlist->nr_mmaps; i++) 684 perf_mmap__munmap(&evlist->overwrite_mmap[i]); 685 } 686 687 void perf_evlist__munmap(struct evlist *evlist) 688 { 689 perf_evlist__munmap_nofree(evlist); 690 zfree(&evlist->mmap); 691 zfree(&evlist->overwrite_mmap); 692 } 693 694 static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist, 695 bool overwrite) 696 { 697 int i; 698 struct perf_mmap *map; 699 700 evlist->nr_mmaps = perf_cpu_map__nr(evlist->core.cpus); 701 if (perf_cpu_map__empty(evlist->core.cpus)) 702 evlist->nr_mmaps = perf_thread_map__nr(evlist->core.threads); 703 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 704 if (!map) 705 return NULL; 706 707 for (i = 0; i < evlist->nr_mmaps; i++) { 708 map[i].fd = -1; 709 map[i].overwrite = overwrite; 710 /* 711 * When the perf_mmap() call is made we grab one refcount, plus 712 * one extra to let perf_mmap__consume() get the last 713 * events after all real references (perf_mmap__get()) are 714 * dropped. 715 * 716 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 717 * thus does perf_mmap__get() on it. 718 */ 719 refcount_set(&map[i].refcnt, 0); 720 } 721 return map; 722 } 723 724 static bool 725 perf_evlist__should_poll(struct evlist *evlist __maybe_unused, 726 struct evsel *evsel) 727 { 728 if (evsel->core.attr.write_backward) 729 return false; 730 return true; 731 } 732 733 static int perf_evlist__mmap_per_evsel(struct evlist *evlist, int idx, 734 struct mmap_params *mp, int cpu_idx, 735 int thread, int *_output, int *_output_overwrite) 736 { 737 struct evsel *evsel; 738 int revent; 739 int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx); 740 741 evlist__for_each_entry(evlist, evsel) { 742 struct perf_mmap *maps = evlist->mmap; 743 int *output = _output; 744 int fd; 745 int cpu; 746 747 mp->prot = PROT_READ | PROT_WRITE; 748 if (evsel->core.attr.write_backward) { 749 output = _output_overwrite; 750 maps = evlist->overwrite_mmap; 751 752 if (!maps) { 753 maps = perf_evlist__alloc_mmap(evlist, true); 754 if (!maps) 755 return -1; 756 evlist->overwrite_mmap = maps; 757 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 758 perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 759 } 760 mp->prot &= ~PROT_WRITE; 761 } 762 763 if (evsel->system_wide && thread) 764 continue; 765 766 cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu); 767 if (cpu == -1) 768 continue; 769 770 fd = FD(evsel, cpu, thread); 771 772 if (*output == -1) { 773 *output = fd; 774 775 if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 776 return -1; 777 } else { 778 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 779 return -1; 780 781 perf_mmap__get(&maps[idx]); 782 } 783 784 revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 785 786 /* 787 * The system_wide flag causes a selected event to be opened 788 * always without a pid. Consequently it will never get a 789 * POLLHUP, but it is used for tracking in combination with 790 * other events, so it should not need to be polled anyway. 791 * Therefore don't add it for polling. 792 */ 793 if (!evsel->system_wide && 794 __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 795 perf_mmap__put(&maps[idx]); 796 return -1; 797 } 798 799 if (evsel->core.attr.read_format & PERF_FORMAT_ID) { 800 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 801 fd) < 0) 802 return -1; 803 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 804 thread); 805 } 806 } 807 808 return 0; 809 } 810 811 static int perf_evlist__mmap_per_cpu(struct evlist *evlist, 812 struct mmap_params *mp) 813 { 814 int cpu, thread; 815 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 816 int nr_threads = perf_thread_map__nr(evlist->core.threads); 817 818 pr_debug2("perf event ring buffer mmapped per cpu\n"); 819 for (cpu = 0; cpu < nr_cpus; cpu++) { 820 int output = -1; 821 int output_overwrite = -1; 822 823 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 824 true); 825 826 for (thread = 0; thread < nr_threads; thread++) { 827 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 828 thread, &output, &output_overwrite)) 829 goto out_unmap; 830 } 831 } 832 833 return 0; 834 835 out_unmap: 836 perf_evlist__munmap_nofree(evlist); 837 return -1; 838 } 839 840 static int perf_evlist__mmap_per_thread(struct evlist *evlist, 841 struct mmap_params *mp) 842 { 843 int thread; 844 int nr_threads = perf_thread_map__nr(evlist->core.threads); 845 846 pr_debug2("perf event ring buffer mmapped per thread\n"); 847 for (thread = 0; thread < nr_threads; thread++) { 848 int output = -1; 849 int output_overwrite = -1; 850 851 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 852 false); 853 854 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 855 &output, &output_overwrite)) 856 goto out_unmap; 857 } 858 859 return 0; 860 861 out_unmap: 862 perf_evlist__munmap_nofree(evlist); 863 return -1; 864 } 865 866 unsigned long perf_event_mlock_kb_in_pages(void) 867 { 868 unsigned long pages; 869 int max; 870 871 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 872 /* 873 * Pick a once upon a time good value, i.e. things look 874 * strange since we can't read a sysctl value, but lets not 875 * die yet... 876 */ 877 max = 512; 878 } else { 879 max -= (page_size / 1024); 880 } 881 882 pages = (max * 1024) / page_size; 883 if (!is_power_of_2(pages)) 884 pages = rounddown_pow_of_two(pages); 885 886 return pages; 887 } 888 889 size_t perf_evlist__mmap_size(unsigned long pages) 890 { 891 if (pages == UINT_MAX) 892 pages = perf_event_mlock_kb_in_pages(); 893 else if (!is_power_of_2(pages)) 894 return 0; 895 896 return (pages + 1) * page_size; 897 } 898 899 static long parse_pages_arg(const char *str, unsigned long min, 900 unsigned long max) 901 { 902 unsigned long pages, val; 903 static struct parse_tag tags[] = { 904 { .tag = 'B', .mult = 1 }, 905 { .tag = 'K', .mult = 1 << 10 }, 906 { .tag = 'M', .mult = 1 << 20 }, 907 { .tag = 'G', .mult = 1 << 30 }, 908 { .tag = 0 }, 909 }; 910 911 if (str == NULL) 912 return -EINVAL; 913 914 val = parse_tag_value(str, tags); 915 if (val != (unsigned long) -1) { 916 /* we got file size value */ 917 pages = PERF_ALIGN(val, page_size) / page_size; 918 } else { 919 /* we got pages count value */ 920 char *eptr; 921 pages = strtoul(str, &eptr, 10); 922 if (*eptr != '\0') 923 return -EINVAL; 924 } 925 926 if (pages == 0 && min == 0) { 927 /* leave number of pages at 0 */ 928 } else if (!is_power_of_2(pages)) { 929 char buf[100]; 930 931 /* round pages up to next power of 2 */ 932 pages = roundup_pow_of_two(pages); 933 if (!pages) 934 return -EINVAL; 935 936 unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 937 pr_info("rounding mmap pages size to %s (%lu pages)\n", 938 buf, pages); 939 } 940 941 if (pages > max) 942 return -EINVAL; 943 944 return pages; 945 } 946 947 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 948 { 949 unsigned long max = UINT_MAX; 950 long pages; 951 952 if (max > SIZE_MAX / page_size) 953 max = SIZE_MAX / page_size; 954 955 pages = parse_pages_arg(str, 1, max); 956 if (pages < 0) { 957 pr_err("Invalid argument for --mmap_pages/-m\n"); 958 return -1; 959 } 960 961 *mmap_pages = pages; 962 return 0; 963 } 964 965 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 966 int unset __maybe_unused) 967 { 968 return __perf_evlist__parse_mmap_pages(opt->value, str); 969 } 970 971 /** 972 * perf_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 perf_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 perf_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 struct evsel *evsel; 994 const struct perf_cpu_map *cpus = evlist->core.cpus; 995 const struct perf_thread_map *threads = evlist->core.threads; 996 /* 997 * Delay setting mp.prot: set it before calling perf_mmap__mmap. 998 * Its value is decided by evsel's write_backward. 999 * So &mp should not be passed through const pointer. 1000 */ 1001 struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush, 1002 .comp_level = comp_level }; 1003 1004 if (!evlist->mmap) 1005 evlist->mmap = perf_evlist__alloc_mmap(evlist, false); 1006 if (!evlist->mmap) 1007 return -ENOMEM; 1008 1009 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1010 return -ENOMEM; 1011 1012 evlist->mmap_len = perf_evlist__mmap_size(pages); 1013 pr_debug("mmap size %zuB\n", evlist->mmap_len); 1014 mp.mask = evlist->mmap_len - page_size - 1; 1015 1016 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1017 auxtrace_pages, auxtrace_overwrite); 1018 1019 evlist__for_each_entry(evlist, evsel) { 1020 if ((evsel->core.attr.read_format & PERF_FORMAT_ID) && 1021 evsel->sample_id == NULL && 1022 perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0) 1023 return -ENOMEM; 1024 } 1025 1026 if (perf_cpu_map__empty(cpus)) 1027 return perf_evlist__mmap_per_thread(evlist, &mp); 1028 1029 return perf_evlist__mmap_per_cpu(evlist, &mp); 1030 } 1031 1032 int perf_evlist__mmap(struct evlist *evlist, unsigned int pages) 1033 { 1034 return perf_evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 1035 } 1036 1037 int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 1038 { 1039 bool all_threads = (target->per_thread && target->system_wide); 1040 struct perf_cpu_map *cpus; 1041 struct perf_thread_map *threads; 1042 1043 /* 1044 * If specify '-a' and '--per-thread' to perf record, perf record 1045 * will override '--per-thread'. target->per_thread = false and 1046 * target->system_wide = true. 1047 * 1048 * If specify '--per-thread' only to perf record, 1049 * target->per_thread = true and target->system_wide = false. 1050 * 1051 * So target->per_thread && target->system_wide is false. 1052 * For perf record, thread_map__new_str doesn't call 1053 * thread_map__new_all_cpus. That will keep perf record's 1054 * current behavior. 1055 * 1056 * For perf stat, it allows the case that target->per_thread and 1057 * target->system_wide are all true. It means to collect system-wide 1058 * per-thread data. thread_map__new_str will call 1059 * thread_map__new_all_cpus to enumerate all threads. 1060 */ 1061 threads = thread_map__new_str(target->pid, target->tid, target->uid, 1062 all_threads); 1063 1064 if (!threads) 1065 return -1; 1066 1067 if (target__uses_dummy_map(target)) 1068 cpus = perf_cpu_map__dummy_new(); 1069 else 1070 cpus = perf_cpu_map__new(target->cpu_list); 1071 1072 if (!cpus) 1073 goto out_delete_threads; 1074 1075 evlist->core.has_user_cpus = !!target->cpu_list; 1076 1077 perf_evlist__set_maps(&evlist->core, cpus, threads); 1078 1079 return 0; 1080 1081 out_delete_threads: 1082 perf_thread_map__put(threads); 1083 return -1; 1084 } 1085 1086 void __perf_evlist__set_sample_bit(struct evlist *evlist, 1087 enum perf_event_sample_format bit) 1088 { 1089 struct evsel *evsel; 1090 1091 evlist__for_each_entry(evlist, evsel) 1092 __perf_evsel__set_sample_bit(evsel, bit); 1093 } 1094 1095 void __perf_evlist__reset_sample_bit(struct evlist *evlist, 1096 enum perf_event_sample_format bit) 1097 { 1098 struct evsel *evsel; 1099 1100 evlist__for_each_entry(evlist, evsel) 1101 __perf_evsel__reset_sample_bit(evsel, bit); 1102 } 1103 1104 int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 1105 { 1106 struct evsel *evsel; 1107 int err = 0; 1108 1109 evlist__for_each_entry(evlist, evsel) { 1110 if (evsel->filter == NULL) 1111 continue; 1112 1113 /* 1114 * filters only work for tracepoint event, which doesn't have cpu limit. 1115 * So evlist and evsel should always be same. 1116 */ 1117 err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 1118 if (err) { 1119 *err_evsel = evsel; 1120 break; 1121 } 1122 } 1123 1124 return err; 1125 } 1126 1127 int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1128 { 1129 struct evsel *evsel; 1130 int err = 0; 1131 1132 evlist__for_each_entry(evlist, evsel) { 1133 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1134 continue; 1135 1136 err = perf_evsel__set_filter(evsel, filter); 1137 if (err) 1138 break; 1139 } 1140 1141 return err; 1142 } 1143 1144 int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1145 { 1146 char *filter; 1147 int ret = -1; 1148 size_t i; 1149 1150 for (i = 0; i < npids; ++i) { 1151 if (i == 0) { 1152 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1153 return -1; 1154 } else { 1155 char *tmp; 1156 1157 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1158 goto out_free; 1159 1160 free(filter); 1161 filter = tmp; 1162 } 1163 } 1164 1165 ret = perf_evlist__set_tp_filter(evlist, filter); 1166 out_free: 1167 free(filter); 1168 return ret; 1169 } 1170 1171 int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1172 { 1173 return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1174 } 1175 1176 bool perf_evlist__valid_sample_type(struct evlist *evlist) 1177 { 1178 struct evsel *pos; 1179 1180 if (evlist->core.nr_entries == 1) 1181 return true; 1182 1183 if (evlist->id_pos < 0 || evlist->is_pos < 0) 1184 return false; 1185 1186 evlist__for_each_entry(evlist, pos) { 1187 if (pos->id_pos != evlist->id_pos || 1188 pos->is_pos != evlist->is_pos) 1189 return false; 1190 } 1191 1192 return true; 1193 } 1194 1195 u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1196 { 1197 struct evsel *evsel; 1198 1199 if (evlist->combined_sample_type) 1200 return evlist->combined_sample_type; 1201 1202 evlist__for_each_entry(evlist, evsel) 1203 evlist->combined_sample_type |= evsel->core.attr.sample_type; 1204 1205 return evlist->combined_sample_type; 1206 } 1207 1208 u64 perf_evlist__combined_sample_type(struct evlist *evlist) 1209 { 1210 evlist->combined_sample_type = 0; 1211 return __perf_evlist__combined_sample_type(evlist); 1212 } 1213 1214 u64 perf_evlist__combined_branch_type(struct evlist *evlist) 1215 { 1216 struct evsel *evsel; 1217 u64 branch_type = 0; 1218 1219 evlist__for_each_entry(evlist, evsel) 1220 branch_type |= evsel->core.attr.branch_sample_type; 1221 return branch_type; 1222 } 1223 1224 bool perf_evlist__valid_read_format(struct evlist *evlist) 1225 { 1226 struct evsel *first = perf_evlist__first(evlist), *pos = first; 1227 u64 read_format = first->core.attr.read_format; 1228 u64 sample_type = first->core.attr.sample_type; 1229 1230 evlist__for_each_entry(evlist, pos) { 1231 if (read_format != pos->core.attr.read_format) 1232 return false; 1233 } 1234 1235 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 1236 if ((sample_type & PERF_SAMPLE_READ) && 1237 !(read_format & PERF_FORMAT_ID)) { 1238 return false; 1239 } 1240 1241 return true; 1242 } 1243 1244 u64 perf_evlist__read_format(struct evlist *evlist) 1245 { 1246 struct evsel *first = perf_evlist__first(evlist); 1247 return first->core.attr.read_format; 1248 } 1249 1250 u16 perf_evlist__id_hdr_size(struct evlist *evlist) 1251 { 1252 struct evsel *first = perf_evlist__first(evlist); 1253 struct perf_sample *data; 1254 u64 sample_type; 1255 u16 size = 0; 1256 1257 if (!first->core.attr.sample_id_all) 1258 goto out; 1259 1260 sample_type = first->core.attr.sample_type; 1261 1262 if (sample_type & PERF_SAMPLE_TID) 1263 size += sizeof(data->tid) * 2; 1264 1265 if (sample_type & PERF_SAMPLE_TIME) 1266 size += sizeof(data->time); 1267 1268 if (sample_type & PERF_SAMPLE_ID) 1269 size += sizeof(data->id); 1270 1271 if (sample_type & PERF_SAMPLE_STREAM_ID) 1272 size += sizeof(data->stream_id); 1273 1274 if (sample_type & PERF_SAMPLE_CPU) 1275 size += sizeof(data->cpu) * 2; 1276 1277 if (sample_type & PERF_SAMPLE_IDENTIFIER) 1278 size += sizeof(data->id); 1279 out: 1280 return size; 1281 } 1282 1283 bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1284 { 1285 struct evsel *first = perf_evlist__first(evlist), *pos = first; 1286 1287 evlist__for_each_entry_continue(evlist, pos) { 1288 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1289 return false; 1290 } 1291 1292 return true; 1293 } 1294 1295 bool perf_evlist__sample_id_all(struct evlist *evlist) 1296 { 1297 struct evsel *first = perf_evlist__first(evlist); 1298 return first->core.attr.sample_id_all; 1299 } 1300 1301 void perf_evlist__set_selected(struct evlist *evlist, 1302 struct evsel *evsel) 1303 { 1304 evlist->selected = evsel; 1305 } 1306 1307 void evlist__close(struct evlist *evlist) 1308 { 1309 struct evsel *evsel; 1310 1311 evlist__for_each_entry_reverse(evlist, evsel) 1312 evsel__close(evsel); 1313 } 1314 1315 static int perf_evlist__create_syswide_maps(struct evlist *evlist) 1316 { 1317 struct perf_cpu_map *cpus; 1318 struct perf_thread_map *threads; 1319 int err = -ENOMEM; 1320 1321 /* 1322 * Try reading /sys/devices/system/cpu/online to get 1323 * an all cpus map. 1324 * 1325 * FIXME: -ENOMEM is the best we can do here, the cpu_map 1326 * code needs an overhaul to properly forward the 1327 * error, and we may not want to do that fallback to a 1328 * default cpu identity map :-\ 1329 */ 1330 cpus = perf_cpu_map__new(NULL); 1331 if (!cpus) 1332 goto out; 1333 1334 threads = perf_thread_map__new_dummy(); 1335 if (!threads) 1336 goto out_put; 1337 1338 perf_evlist__set_maps(&evlist->core, cpus, threads); 1339 out: 1340 return err; 1341 out_put: 1342 perf_cpu_map__put(cpus); 1343 goto out; 1344 } 1345 1346 int evlist__open(struct evlist *evlist) 1347 { 1348 struct evsel *evsel; 1349 int err; 1350 1351 /* 1352 * Default: one fd per CPU, all threads, aka systemwide 1353 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 1354 */ 1355 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 1356 err = perf_evlist__create_syswide_maps(evlist); 1357 if (err < 0) 1358 goto out_err; 1359 } 1360 1361 perf_evlist__update_id_pos(evlist); 1362 1363 evlist__for_each_entry(evlist, evsel) { 1364 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1365 if (err < 0) 1366 goto out_err; 1367 } 1368 1369 return 0; 1370 out_err: 1371 evlist__close(evlist); 1372 errno = -err; 1373 return err; 1374 } 1375 1376 int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 1377 const char *argv[], bool pipe_output, 1378 void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 1379 { 1380 int child_ready_pipe[2], go_pipe[2]; 1381 char bf; 1382 1383 if (pipe(child_ready_pipe) < 0) { 1384 perror("failed to create 'ready' pipe"); 1385 return -1; 1386 } 1387 1388 if (pipe(go_pipe) < 0) { 1389 perror("failed to create 'go' pipe"); 1390 goto out_close_ready_pipe; 1391 } 1392 1393 evlist->workload.pid = fork(); 1394 if (evlist->workload.pid < 0) { 1395 perror("failed to fork"); 1396 goto out_close_pipes; 1397 } 1398 1399 if (!evlist->workload.pid) { 1400 int ret; 1401 1402 if (pipe_output) 1403 dup2(2, 1); 1404 1405 signal(SIGTERM, SIG_DFL); 1406 1407 close(child_ready_pipe[0]); 1408 close(go_pipe[1]); 1409 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 1410 1411 /* 1412 * Tell the parent we're ready to go 1413 */ 1414 close(child_ready_pipe[1]); 1415 1416 /* 1417 * Wait until the parent tells us to go. 1418 */ 1419 ret = read(go_pipe[0], &bf, 1); 1420 /* 1421 * The parent will ask for the execvp() to be performed by 1422 * writing exactly one byte, in workload.cork_fd, usually via 1423 * perf_evlist__start_workload(). 1424 * 1425 * For cancelling the workload without actually running it, 1426 * the parent will just close workload.cork_fd, without writing 1427 * anything, i.e. read will return zero and we just exit() 1428 * here. 1429 */ 1430 if (ret != 1) { 1431 if (ret == -1) 1432 perror("unable to read pipe"); 1433 exit(ret); 1434 } 1435 1436 execvp(argv[0], (char **)argv); 1437 1438 if (exec_error) { 1439 union sigval val; 1440 1441 val.sival_int = errno; 1442 if (sigqueue(getppid(), SIGUSR1, val)) 1443 perror(argv[0]); 1444 } else 1445 perror(argv[0]); 1446 exit(-1); 1447 } 1448 1449 if (exec_error) { 1450 struct sigaction act = { 1451 .sa_flags = SA_SIGINFO, 1452 .sa_sigaction = exec_error, 1453 }; 1454 sigaction(SIGUSR1, &act, NULL); 1455 } 1456 1457 if (target__none(target)) { 1458 if (evlist->core.threads == NULL) { 1459 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 1460 __func__, __LINE__); 1461 goto out_close_pipes; 1462 } 1463 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 1464 } 1465 1466 close(child_ready_pipe[1]); 1467 close(go_pipe[0]); 1468 /* 1469 * wait for child to settle 1470 */ 1471 if (read(child_ready_pipe[0], &bf, 1) == -1) { 1472 perror("unable to read pipe"); 1473 goto out_close_pipes; 1474 } 1475 1476 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 1477 evlist->workload.cork_fd = go_pipe[1]; 1478 close(child_ready_pipe[0]); 1479 return 0; 1480 1481 out_close_pipes: 1482 close(go_pipe[0]); 1483 close(go_pipe[1]); 1484 out_close_ready_pipe: 1485 close(child_ready_pipe[0]); 1486 close(child_ready_pipe[1]); 1487 return -1; 1488 } 1489 1490 int perf_evlist__start_workload(struct evlist *evlist) 1491 { 1492 if (evlist->workload.cork_fd > 0) { 1493 char bf = 0; 1494 int ret; 1495 /* 1496 * Remove the cork, let it rip! 1497 */ 1498 ret = write(evlist->workload.cork_fd, &bf, 1); 1499 if (ret < 0) 1500 perror("unable to write to pipe"); 1501 1502 close(evlist->workload.cork_fd); 1503 return ret; 1504 } 1505 1506 return 0; 1507 } 1508 1509 int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 1510 struct perf_sample *sample) 1511 { 1512 struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1513 1514 if (!evsel) 1515 return -EFAULT; 1516 return perf_evsel__parse_sample(evsel, event, sample); 1517 } 1518 1519 int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 1520 union perf_event *event, 1521 u64 *timestamp) 1522 { 1523 struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1524 1525 if (!evsel) 1526 return -EFAULT; 1527 return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 1528 } 1529 1530 size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp) 1531 { 1532 struct evsel *evsel; 1533 size_t printed = 0; 1534 1535 evlist__for_each_entry(evlist, evsel) { 1536 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 1537 perf_evsel__name(evsel)); 1538 } 1539 1540 return printed + fprintf(fp, "\n"); 1541 } 1542 1543 int perf_evlist__strerror_open(struct evlist *evlist, 1544 int err, char *buf, size_t size) 1545 { 1546 int printed, value; 1547 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1548 1549 switch (err) { 1550 case EACCES: 1551 case EPERM: 1552 printed = scnprintf(buf, size, 1553 "Error:\t%s.\n" 1554 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1555 1556 value = perf_event_paranoid(); 1557 1558 printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1559 1560 if (value >= 2) { 1561 printed += scnprintf(buf + printed, size - printed, 1562 "For your workloads it needs to be <= 1\nHint:\t"); 1563 } 1564 printed += scnprintf(buf + printed, size - printed, 1565 "For system wide tracing it needs to be set to -1.\n"); 1566 1567 printed += scnprintf(buf + printed, size - printed, 1568 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 1569 "Hint:\tThe current value is %d.", value); 1570 break; 1571 case EINVAL: { 1572 struct evsel *first = perf_evlist__first(evlist); 1573 int max_freq; 1574 1575 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1576 goto out_default; 1577 1578 if (first->core.attr.sample_freq < (u64)max_freq) 1579 goto out_default; 1580 1581 printed = scnprintf(buf, size, 1582 "Error:\t%s.\n" 1583 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1584 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1585 emsg, max_freq, first->core.attr.sample_freq); 1586 break; 1587 } 1588 default: 1589 out_default: 1590 scnprintf(buf, size, "%s", emsg); 1591 break; 1592 } 1593 1594 return 0; 1595 } 1596 1597 int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1598 { 1599 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1600 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1601 1602 switch (err) { 1603 case EPERM: 1604 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1605 printed += scnprintf(buf + printed, size - printed, 1606 "Error:\t%s.\n" 1607 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1608 "Hint:\tTried using %zd kB.\n", 1609 emsg, pages_max_per_user, pages_attempted); 1610 1611 if (pages_attempted >= pages_max_per_user) { 1612 printed += scnprintf(buf + printed, size - printed, 1613 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1614 pages_max_per_user + pages_attempted); 1615 } 1616 1617 printed += scnprintf(buf + printed, size - printed, 1618 "Hint:\tTry using a smaller -m/--mmap-pages value."); 1619 break; 1620 default: 1621 scnprintf(buf, size, "%s", emsg); 1622 break; 1623 } 1624 1625 return 0; 1626 } 1627 1628 void perf_evlist__to_front(struct evlist *evlist, 1629 struct evsel *move_evsel) 1630 { 1631 struct evsel *evsel, *n; 1632 LIST_HEAD(move); 1633 1634 if (move_evsel == perf_evlist__first(evlist)) 1635 return; 1636 1637 evlist__for_each_entry_safe(evlist, n, evsel) { 1638 if (evsel->leader == move_evsel->leader) 1639 list_move_tail(&evsel->core.node, &move); 1640 } 1641 1642 list_splice(&move, &evlist->core.entries); 1643 } 1644 1645 void perf_evlist__set_tracking_event(struct evlist *evlist, 1646 struct evsel *tracking_evsel) 1647 { 1648 struct evsel *evsel; 1649 1650 if (tracking_evsel->tracking) 1651 return; 1652 1653 evlist__for_each_entry(evlist, evsel) { 1654 if (evsel != tracking_evsel) 1655 evsel->tracking = false; 1656 } 1657 1658 tracking_evsel->tracking = true; 1659 } 1660 1661 struct evsel * 1662 perf_evlist__find_evsel_by_str(struct evlist *evlist, 1663 const char *str) 1664 { 1665 struct evsel *evsel; 1666 1667 evlist__for_each_entry(evlist, evsel) { 1668 if (!evsel->name) 1669 continue; 1670 if (strcmp(str, evsel->name) == 0) 1671 return evsel; 1672 } 1673 1674 return NULL; 1675 } 1676 1677 void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 1678 enum bkw_mmap_state state) 1679 { 1680 enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 1681 enum action { 1682 NONE, 1683 PAUSE, 1684 RESUME, 1685 } action = NONE; 1686 1687 if (!evlist->overwrite_mmap) 1688 return; 1689 1690 switch (old_state) { 1691 case BKW_MMAP_NOTREADY: { 1692 if (state != BKW_MMAP_RUNNING) 1693 goto state_err; 1694 break; 1695 } 1696 case BKW_MMAP_RUNNING: { 1697 if (state != BKW_MMAP_DATA_PENDING) 1698 goto state_err; 1699 action = PAUSE; 1700 break; 1701 } 1702 case BKW_MMAP_DATA_PENDING: { 1703 if (state != BKW_MMAP_EMPTY) 1704 goto state_err; 1705 break; 1706 } 1707 case BKW_MMAP_EMPTY: { 1708 if (state != BKW_MMAP_RUNNING) 1709 goto state_err; 1710 action = RESUME; 1711 break; 1712 } 1713 default: 1714 WARN_ONCE(1, "Shouldn't get there\n"); 1715 } 1716 1717 evlist->bkw_mmap_state = state; 1718 1719 switch (action) { 1720 case PAUSE: 1721 perf_evlist__pause(evlist); 1722 break; 1723 case RESUME: 1724 perf_evlist__resume(evlist); 1725 break; 1726 case NONE: 1727 default: 1728 break; 1729 } 1730 1731 state_err: 1732 return; 1733 } 1734 1735 bool perf_evlist__exclude_kernel(struct evlist *evlist) 1736 { 1737 struct evsel *evsel; 1738 1739 evlist__for_each_entry(evlist, evsel) { 1740 if (!evsel->core.attr.exclude_kernel) 1741 return false; 1742 } 1743 1744 return true; 1745 } 1746 1747 /* 1748 * Events in data file are not collect in groups, but we still want 1749 * the group display. Set the artificial group and set the leader's 1750 * forced_leader flag to notify the display code. 1751 */ 1752 void perf_evlist__force_leader(struct evlist *evlist) 1753 { 1754 if (!evlist->nr_groups) { 1755 struct evsel *leader = perf_evlist__first(evlist); 1756 1757 perf_evlist__set_leader(evlist); 1758 leader->forced_leader = true; 1759 } 1760 } 1761 1762 struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 1763 struct evsel *evsel) 1764 { 1765 struct evsel *c2, *leader; 1766 bool is_open = true; 1767 1768 leader = evsel->leader; 1769 pr_debug("Weak group for %s/%d failed\n", 1770 leader->name, leader->core.nr_members); 1771 1772 /* 1773 * for_each_group_member doesn't work here because it doesn't 1774 * include the first entry. 1775 */ 1776 evlist__for_each_entry(evsel_list, c2) { 1777 if (c2 == evsel) 1778 is_open = false; 1779 if (c2->leader == leader) { 1780 if (is_open) 1781 evsel__close(c2); 1782 c2->leader = c2; 1783 c2->core.nr_members = 0; 1784 } 1785 } 1786 return leader; 1787 } 1788 1789 int perf_evlist__add_sb_event(struct evlist **evlist, 1790 struct perf_event_attr *attr, 1791 perf_evsel__sb_cb_t cb, 1792 void *data) 1793 { 1794 struct evsel *evsel; 1795 bool new_evlist = (*evlist) == NULL; 1796 1797 if (*evlist == NULL) 1798 *evlist = evlist__new(); 1799 if (*evlist == NULL) 1800 return -1; 1801 1802 if (!attr->sample_id_all) { 1803 pr_warning("enabling sample_id_all for all side band events\n"); 1804 attr->sample_id_all = 1; 1805 } 1806 1807 evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1808 if (!evsel) 1809 goto out_err; 1810 1811 evsel->side_band.cb = cb; 1812 evsel->side_band.data = data; 1813 evlist__add(*evlist, evsel); 1814 return 0; 1815 1816 out_err: 1817 if (new_evlist) { 1818 evlist__delete(*evlist); 1819 *evlist = NULL; 1820 } 1821 return -1; 1822 } 1823 1824 static void *perf_evlist__poll_thread(void *arg) 1825 { 1826 struct evlist *evlist = arg; 1827 bool draining = false; 1828 int i, done = 0; 1829 /* 1830 * In order to read symbols from other namespaces perf to needs to call 1831 * setns(2). This isn't permitted if the struct_fs has multiple users. 1832 * unshare(2) the fs so that we may continue to setns into namespaces 1833 * that we're observing when, for instance, reading the build-ids at 1834 * the end of a 'perf record' session. 1835 */ 1836 unshare(CLONE_FS); 1837 1838 while (!done) { 1839 bool got_data = false; 1840 1841 if (evlist->thread.done) 1842 draining = true; 1843 1844 if (!draining) 1845 perf_evlist__poll(evlist, 1000); 1846 1847 for (i = 0; i < evlist->nr_mmaps; i++) { 1848 struct perf_mmap *map = &evlist->mmap[i]; 1849 union perf_event *event; 1850 1851 if (perf_mmap__read_init(map)) 1852 continue; 1853 while ((event = perf_mmap__read_event(map)) != NULL) { 1854 struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1855 1856 if (evsel && evsel->side_band.cb) 1857 evsel->side_band.cb(event, evsel->side_band.data); 1858 else 1859 pr_warning("cannot locate proper evsel for the side band event\n"); 1860 1861 perf_mmap__consume(map); 1862 got_data = true; 1863 } 1864 perf_mmap__read_done(map); 1865 } 1866 1867 if (draining && !got_data) 1868 break; 1869 } 1870 return NULL; 1871 } 1872 1873 int perf_evlist__start_sb_thread(struct evlist *evlist, 1874 struct target *target) 1875 { 1876 struct evsel *counter; 1877 1878 if (!evlist) 1879 return 0; 1880 1881 if (perf_evlist__create_maps(evlist, target)) 1882 goto out_delete_evlist; 1883 1884 evlist__for_each_entry(evlist, counter) { 1885 if (evsel__open(counter, evlist->core.cpus, 1886 evlist->core.threads) < 0) 1887 goto out_delete_evlist; 1888 } 1889 1890 if (perf_evlist__mmap(evlist, UINT_MAX)) 1891 goto out_delete_evlist; 1892 1893 evlist__for_each_entry(evlist, counter) { 1894 if (evsel__enable(counter)) 1895 goto out_delete_evlist; 1896 } 1897 1898 evlist->thread.done = 0; 1899 if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1900 goto out_delete_evlist; 1901 1902 return 0; 1903 1904 out_delete_evlist: 1905 evlist__delete(evlist); 1906 evlist = NULL; 1907 return -1; 1908 } 1909 1910 void perf_evlist__stop_sb_thread(struct evlist *evlist) 1911 { 1912 if (!evlist) 1913 return; 1914 evlist->thread.done = 1; 1915 pthread_join(evlist->thread.th, NULL); 1916 evlist__delete(evlist); 1917 } 1918