1 /* 2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3 * 4 * Parts came from builtin-{top,stat,record}.c, see those files for further 5 * copyright notes. 6 * 7 * Released under the GPL v2. (and only v2, not any later version) 8 */ 9 #include "util.h" 10 #include "debugfs.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 <unistd.h> 18 19 #include "parse-events.h" 20 21 #include <sys/mman.h> 22 23 #include <linux/bitops.h> 24 #include <linux/hash.h> 25 26 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 27 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 28 29 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 30 struct thread_map *threads) 31 { 32 int i; 33 34 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 35 INIT_HLIST_HEAD(&evlist->heads[i]); 36 INIT_LIST_HEAD(&evlist->entries); 37 perf_evlist__set_maps(evlist, cpus, threads); 38 evlist->workload.pid = -1; 39 } 40 41 struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, 42 struct thread_map *threads) 43 { 44 struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 45 46 if (evlist != NULL) 47 perf_evlist__init(evlist, cpus, threads); 48 49 return evlist; 50 } 51 52 void perf_evlist__config_attrs(struct perf_evlist *evlist, 53 struct perf_record_opts *opts) 54 { 55 struct perf_evsel *evsel; 56 57 if (evlist->cpus->map[0] < 0) 58 opts->no_inherit = true; 59 60 list_for_each_entry(evsel, &evlist->entries, node) { 61 perf_evsel__config(evsel, opts); 62 63 if (evlist->nr_entries > 1) 64 evsel->attr.sample_type |= PERF_SAMPLE_ID; 65 } 66 } 67 68 static void perf_evlist__purge(struct perf_evlist *evlist) 69 { 70 struct perf_evsel *pos, *n; 71 72 list_for_each_entry_safe(pos, n, &evlist->entries, node) { 73 list_del_init(&pos->node); 74 perf_evsel__delete(pos); 75 } 76 77 evlist->nr_entries = 0; 78 } 79 80 void perf_evlist__exit(struct perf_evlist *evlist) 81 { 82 free(evlist->mmap); 83 free(evlist->pollfd); 84 evlist->mmap = NULL; 85 evlist->pollfd = NULL; 86 } 87 88 void perf_evlist__delete(struct perf_evlist *evlist) 89 { 90 perf_evlist__purge(evlist); 91 perf_evlist__exit(evlist); 92 free(evlist); 93 } 94 95 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 96 { 97 list_add_tail(&entry->node, &evlist->entries); 98 ++evlist->nr_entries; 99 } 100 101 void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 102 struct list_head *list, 103 int nr_entries) 104 { 105 list_splice_tail(list, &evlist->entries); 106 evlist->nr_entries += nr_entries; 107 } 108 109 void __perf_evlist__set_leader(struct list_head *list) 110 { 111 struct perf_evsel *evsel, *leader; 112 113 leader = list_entry(list->next, struct perf_evsel, node); 114 leader->leader = NULL; 115 116 list_for_each_entry(evsel, list, node) { 117 if (evsel != leader) 118 evsel->leader = leader; 119 } 120 } 121 122 void perf_evlist__set_leader(struct perf_evlist *evlist) 123 { 124 if (evlist->nr_entries) 125 __perf_evlist__set_leader(&evlist->entries); 126 } 127 128 int perf_evlist__add_default(struct perf_evlist *evlist) 129 { 130 struct perf_event_attr attr = { 131 .type = PERF_TYPE_HARDWARE, 132 .config = PERF_COUNT_HW_CPU_CYCLES, 133 }; 134 struct perf_evsel *evsel; 135 136 event_attr_init(&attr); 137 138 evsel = perf_evsel__new(&attr, 0); 139 if (evsel == NULL) 140 goto error; 141 142 /* use strdup() because free(evsel) assumes name is allocated */ 143 evsel->name = strdup("cycles"); 144 if (!evsel->name) 145 goto error_free; 146 147 perf_evlist__add(evlist, evsel); 148 return 0; 149 error_free: 150 perf_evsel__delete(evsel); 151 error: 152 return -ENOMEM; 153 } 154 155 static int perf_evlist__add_attrs(struct perf_evlist *evlist, 156 struct perf_event_attr *attrs, size_t nr_attrs) 157 { 158 struct perf_evsel *evsel, *n; 159 LIST_HEAD(head); 160 size_t i; 161 162 for (i = 0; i < nr_attrs; i++) { 163 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 164 if (evsel == NULL) 165 goto out_delete_partial_list; 166 list_add_tail(&evsel->node, &head); 167 } 168 169 perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 170 171 return 0; 172 173 out_delete_partial_list: 174 list_for_each_entry_safe(evsel, n, &head, node) 175 perf_evsel__delete(evsel); 176 return -1; 177 } 178 179 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 180 struct perf_event_attr *attrs, size_t nr_attrs) 181 { 182 size_t i; 183 184 for (i = 0; i < nr_attrs; i++) 185 event_attr_init(attrs + i); 186 187 return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 188 } 189 190 struct perf_evsel * 191 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 192 { 193 struct perf_evsel *evsel; 194 195 list_for_each_entry(evsel, &evlist->entries, node) { 196 if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 197 (int)evsel->attr.config == id) 198 return evsel; 199 } 200 201 return NULL; 202 } 203 204 int perf_evlist__add_newtp(struct perf_evlist *evlist, 205 const char *sys, const char *name, void *handler) 206 { 207 struct perf_evsel *evsel; 208 209 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 210 if (evsel == NULL) 211 return -1; 212 213 evsel->handler.func = handler; 214 perf_evlist__add(evlist, evsel); 215 return 0; 216 } 217 218 void perf_evlist__disable(struct perf_evlist *evlist) 219 { 220 int cpu, thread; 221 struct perf_evsel *pos; 222 223 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 224 list_for_each_entry(pos, &evlist->entries, node) { 225 if (perf_evsel__is_group_member(pos)) 226 continue; 227 for (thread = 0; thread < evlist->threads->nr; thread++) 228 ioctl(FD(pos, cpu, thread), 229 PERF_EVENT_IOC_DISABLE, 0); 230 } 231 } 232 } 233 234 void perf_evlist__enable(struct perf_evlist *evlist) 235 { 236 int cpu, thread; 237 struct perf_evsel *pos; 238 239 for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) { 240 list_for_each_entry(pos, &evlist->entries, node) { 241 if (perf_evsel__is_group_member(pos)) 242 continue; 243 for (thread = 0; thread < evlist->threads->nr; thread++) 244 ioctl(FD(pos, cpu, thread), 245 PERF_EVENT_IOC_ENABLE, 0); 246 } 247 } 248 } 249 250 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 251 { 252 int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries; 253 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 254 return evlist->pollfd != NULL ? 0 : -ENOMEM; 255 } 256 257 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 258 { 259 fcntl(fd, F_SETFL, O_NONBLOCK); 260 evlist->pollfd[evlist->nr_fds].fd = fd; 261 evlist->pollfd[evlist->nr_fds].events = POLLIN; 262 evlist->nr_fds++; 263 } 264 265 static void perf_evlist__id_hash(struct perf_evlist *evlist, 266 struct perf_evsel *evsel, 267 int cpu, int thread, u64 id) 268 { 269 int hash; 270 struct perf_sample_id *sid = SID(evsel, cpu, thread); 271 272 sid->id = id; 273 sid->evsel = evsel; 274 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 275 hlist_add_head(&sid->node, &evlist->heads[hash]); 276 } 277 278 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 279 int cpu, int thread, u64 id) 280 { 281 perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 282 evsel->id[evsel->ids++] = id; 283 } 284 285 static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 286 struct perf_evsel *evsel, 287 int cpu, int thread, int fd) 288 { 289 u64 read_data[4] = { 0, }; 290 int id_idx = 1; /* The first entry is the counter value */ 291 292 if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 293 read(fd, &read_data, sizeof(read_data)) == -1) 294 return -1; 295 296 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 297 ++id_idx; 298 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 299 ++id_idx; 300 301 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 302 return 0; 303 } 304 305 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 306 { 307 struct hlist_head *head; 308 struct hlist_node *pos; 309 struct perf_sample_id *sid; 310 int hash; 311 312 if (evlist->nr_entries == 1) 313 return perf_evlist__first(evlist); 314 315 hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 316 head = &evlist->heads[hash]; 317 318 hlist_for_each_entry(sid, pos, head, node) 319 if (sid->id == id) 320 return sid->evsel; 321 322 if (!perf_evlist__sample_id_all(evlist)) 323 return perf_evlist__first(evlist); 324 325 return NULL; 326 } 327 328 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 329 { 330 struct perf_mmap *md = &evlist->mmap[idx]; 331 unsigned int head = perf_mmap__read_head(md); 332 unsigned int old = md->prev; 333 unsigned char *data = md->base + page_size; 334 union perf_event *event = NULL; 335 336 if (evlist->overwrite) { 337 /* 338 * If we're further behind than half the buffer, there's a chance 339 * the writer will bite our tail and mess up the samples under us. 340 * 341 * If we somehow ended up ahead of the head, we got messed up. 342 * 343 * In either case, truncate and restart at head. 344 */ 345 int diff = head - old; 346 if (diff > md->mask / 2 || diff < 0) { 347 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 348 349 /* 350 * head points to a known good entry, start there. 351 */ 352 old = head; 353 } 354 } 355 356 if (old != head) { 357 size_t size; 358 359 event = (union perf_event *)&data[old & md->mask]; 360 size = event->header.size; 361 362 /* 363 * Event straddles the mmap boundary -- header should always 364 * be inside due to u64 alignment of output. 365 */ 366 if ((old & md->mask) + size != ((old + size) & md->mask)) { 367 unsigned int offset = old; 368 unsigned int len = min(sizeof(*event), size), cpy; 369 void *dst = &evlist->event_copy; 370 371 do { 372 cpy = min(md->mask + 1 - (offset & md->mask), len); 373 memcpy(dst, &data[offset & md->mask], cpy); 374 offset += cpy; 375 dst += cpy; 376 len -= cpy; 377 } while (len); 378 379 event = &evlist->event_copy; 380 } 381 382 old += size; 383 } 384 385 md->prev = old; 386 387 if (!evlist->overwrite) 388 perf_mmap__write_tail(md, old); 389 390 return event; 391 } 392 393 void perf_evlist__munmap(struct perf_evlist *evlist) 394 { 395 int i; 396 397 for (i = 0; i < evlist->nr_mmaps; i++) { 398 if (evlist->mmap[i].base != NULL) { 399 munmap(evlist->mmap[i].base, evlist->mmap_len); 400 evlist->mmap[i].base = NULL; 401 } 402 } 403 404 free(evlist->mmap); 405 evlist->mmap = NULL; 406 } 407 408 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 409 { 410 evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 411 if (cpu_map__all(evlist->cpus)) 412 evlist->nr_mmaps = evlist->threads->nr; 413 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 414 return evlist->mmap != NULL ? 0 : -ENOMEM; 415 } 416 417 static int __perf_evlist__mmap(struct perf_evlist *evlist, 418 int idx, int prot, int mask, int fd) 419 { 420 evlist->mmap[idx].prev = 0; 421 evlist->mmap[idx].mask = mask; 422 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 423 MAP_SHARED, fd, 0); 424 if (evlist->mmap[idx].base == MAP_FAILED) { 425 evlist->mmap[idx].base = NULL; 426 return -1; 427 } 428 429 perf_evlist__add_pollfd(evlist, fd); 430 return 0; 431 } 432 433 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 434 { 435 struct perf_evsel *evsel; 436 int cpu, thread; 437 438 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 439 int output = -1; 440 441 for (thread = 0; thread < evlist->threads->nr; thread++) { 442 list_for_each_entry(evsel, &evlist->entries, node) { 443 int fd = FD(evsel, cpu, thread); 444 445 if (output == -1) { 446 output = fd; 447 if (__perf_evlist__mmap(evlist, cpu, 448 prot, mask, output) < 0) 449 goto out_unmap; 450 } else { 451 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 452 goto out_unmap; 453 } 454 455 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 456 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 457 goto out_unmap; 458 } 459 } 460 } 461 462 return 0; 463 464 out_unmap: 465 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 466 if (evlist->mmap[cpu].base != NULL) { 467 munmap(evlist->mmap[cpu].base, evlist->mmap_len); 468 evlist->mmap[cpu].base = NULL; 469 } 470 } 471 return -1; 472 } 473 474 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 475 { 476 struct perf_evsel *evsel; 477 int thread; 478 479 for (thread = 0; thread < evlist->threads->nr; thread++) { 480 int output = -1; 481 482 list_for_each_entry(evsel, &evlist->entries, node) { 483 int fd = FD(evsel, 0, thread); 484 485 if (output == -1) { 486 output = fd; 487 if (__perf_evlist__mmap(evlist, thread, 488 prot, mask, output) < 0) 489 goto out_unmap; 490 } else { 491 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 492 goto out_unmap; 493 } 494 495 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 496 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 497 goto out_unmap; 498 } 499 } 500 501 return 0; 502 503 out_unmap: 504 for (thread = 0; thread < evlist->threads->nr; thread++) { 505 if (evlist->mmap[thread].base != NULL) { 506 munmap(evlist->mmap[thread].base, evlist->mmap_len); 507 evlist->mmap[thread].base = NULL; 508 } 509 } 510 return -1; 511 } 512 513 /** perf_evlist__mmap - Create per cpu maps to receive events 514 * 515 * @evlist - list of events 516 * @pages - map length in pages 517 * @overwrite - overwrite older events? 518 * 519 * If overwrite is false the user needs to signal event consuption using: 520 * 521 * struct perf_mmap *m = &evlist->mmap[cpu]; 522 * unsigned int head = perf_mmap__read_head(m); 523 * 524 * perf_mmap__write_tail(m, head) 525 * 526 * Using perf_evlist__read_on_cpu does this automatically. 527 */ 528 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 529 bool overwrite) 530 { 531 struct perf_evsel *evsel; 532 const struct cpu_map *cpus = evlist->cpus; 533 const struct thread_map *threads = evlist->threads; 534 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 535 536 /* 512 kiB: default amount of unprivileged mlocked memory */ 537 if (pages == UINT_MAX) 538 pages = (512 * 1024) / page_size; 539 else if (!is_power_of_2(pages)) 540 return -EINVAL; 541 542 mask = pages * page_size - 1; 543 544 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 545 return -ENOMEM; 546 547 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 548 return -ENOMEM; 549 550 evlist->overwrite = overwrite; 551 evlist->mmap_len = (pages + 1) * page_size; 552 553 list_for_each_entry(evsel, &evlist->entries, node) { 554 if ((evsel->attr.read_format & PERF_FORMAT_ID) && 555 evsel->sample_id == NULL && 556 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 557 return -ENOMEM; 558 } 559 560 if (cpu_map__all(cpus)) 561 return perf_evlist__mmap_per_thread(evlist, prot, mask); 562 563 return perf_evlist__mmap_per_cpu(evlist, prot, mask); 564 } 565 566 int perf_evlist__create_maps(struct perf_evlist *evlist, 567 struct perf_target *target) 568 { 569 evlist->threads = thread_map__new_str(target->pid, target->tid, 570 target->uid); 571 572 if (evlist->threads == NULL) 573 return -1; 574 575 if (perf_target__has_task(target)) 576 evlist->cpus = cpu_map__dummy_new(); 577 else if (!perf_target__has_cpu(target) && !target->uses_mmap) 578 evlist->cpus = cpu_map__dummy_new(); 579 else 580 evlist->cpus = cpu_map__new(target->cpu_list); 581 582 if (evlist->cpus == NULL) 583 goto out_delete_threads; 584 585 return 0; 586 587 out_delete_threads: 588 thread_map__delete(evlist->threads); 589 return -1; 590 } 591 592 void perf_evlist__delete_maps(struct perf_evlist *evlist) 593 { 594 cpu_map__delete(evlist->cpus); 595 thread_map__delete(evlist->threads); 596 evlist->cpus = NULL; 597 evlist->threads = NULL; 598 } 599 600 int perf_evlist__apply_filters(struct perf_evlist *evlist) 601 { 602 struct perf_evsel *evsel; 603 int err = 0; 604 const int ncpus = cpu_map__nr(evlist->cpus), 605 nthreads = evlist->threads->nr; 606 607 list_for_each_entry(evsel, &evlist->entries, node) { 608 if (evsel->filter == NULL) 609 continue; 610 611 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 612 if (err) 613 break; 614 } 615 616 return err; 617 } 618 619 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 620 { 621 struct perf_evsel *evsel; 622 int err = 0; 623 const int ncpus = cpu_map__nr(evlist->cpus), 624 nthreads = evlist->threads->nr; 625 626 list_for_each_entry(evsel, &evlist->entries, node) { 627 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 628 if (err) 629 break; 630 } 631 632 return err; 633 } 634 635 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 636 { 637 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 638 639 list_for_each_entry_continue(pos, &evlist->entries, node) { 640 if (first->attr.sample_type != pos->attr.sample_type) 641 return false; 642 } 643 644 return true; 645 } 646 647 u64 perf_evlist__sample_type(struct perf_evlist *evlist) 648 { 649 struct perf_evsel *first = perf_evlist__first(evlist); 650 return first->attr.sample_type; 651 } 652 653 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 654 { 655 struct perf_evsel *first = perf_evlist__first(evlist); 656 struct perf_sample *data; 657 u64 sample_type; 658 u16 size = 0; 659 660 if (!first->attr.sample_id_all) 661 goto out; 662 663 sample_type = first->attr.sample_type; 664 665 if (sample_type & PERF_SAMPLE_TID) 666 size += sizeof(data->tid) * 2; 667 668 if (sample_type & PERF_SAMPLE_TIME) 669 size += sizeof(data->time); 670 671 if (sample_type & PERF_SAMPLE_ID) 672 size += sizeof(data->id); 673 674 if (sample_type & PERF_SAMPLE_STREAM_ID) 675 size += sizeof(data->stream_id); 676 677 if (sample_type & PERF_SAMPLE_CPU) 678 size += sizeof(data->cpu) * 2; 679 out: 680 return size; 681 } 682 683 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 684 { 685 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 686 687 list_for_each_entry_continue(pos, &evlist->entries, node) { 688 if (first->attr.sample_id_all != pos->attr.sample_id_all) 689 return false; 690 } 691 692 return true; 693 } 694 695 bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 696 { 697 struct perf_evsel *first = perf_evlist__first(evlist); 698 return first->attr.sample_id_all; 699 } 700 701 void perf_evlist__set_selected(struct perf_evlist *evlist, 702 struct perf_evsel *evsel) 703 { 704 evlist->selected = evsel; 705 } 706 707 int perf_evlist__open(struct perf_evlist *evlist) 708 { 709 struct perf_evsel *evsel; 710 int err, ncpus, nthreads; 711 712 list_for_each_entry(evsel, &evlist->entries, node) { 713 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 714 if (err < 0) 715 goto out_err; 716 } 717 718 return 0; 719 out_err: 720 ncpus = evlist->cpus ? evlist->cpus->nr : 1; 721 nthreads = evlist->threads ? evlist->threads->nr : 1; 722 723 list_for_each_entry_reverse(evsel, &evlist->entries, node) 724 perf_evsel__close(evsel, ncpus, nthreads); 725 726 errno = -err; 727 return err; 728 } 729 730 int perf_evlist__prepare_workload(struct perf_evlist *evlist, 731 struct perf_record_opts *opts, 732 const char *argv[]) 733 { 734 int child_ready_pipe[2], go_pipe[2]; 735 char bf; 736 737 if (pipe(child_ready_pipe) < 0) { 738 perror("failed to create 'ready' pipe"); 739 return -1; 740 } 741 742 if (pipe(go_pipe) < 0) { 743 perror("failed to create 'go' pipe"); 744 goto out_close_ready_pipe; 745 } 746 747 evlist->workload.pid = fork(); 748 if (evlist->workload.pid < 0) { 749 perror("failed to fork"); 750 goto out_close_pipes; 751 } 752 753 if (!evlist->workload.pid) { 754 if (opts->pipe_output) 755 dup2(2, 1); 756 757 close(child_ready_pipe[0]); 758 close(go_pipe[1]); 759 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 760 761 /* 762 * Do a dummy execvp to get the PLT entry resolved, 763 * so we avoid the resolver overhead on the real 764 * execvp call. 765 */ 766 execvp("", (char **)argv); 767 768 /* 769 * Tell the parent we're ready to go 770 */ 771 close(child_ready_pipe[1]); 772 773 /* 774 * Wait until the parent tells us to go. 775 */ 776 if (read(go_pipe[0], &bf, 1) == -1) 777 perror("unable to read pipe"); 778 779 execvp(argv[0], (char **)argv); 780 781 perror(argv[0]); 782 kill(getppid(), SIGUSR1); 783 exit(-1); 784 } 785 786 if (perf_target__none(&opts->target)) 787 evlist->threads->map[0] = evlist->workload.pid; 788 789 close(child_ready_pipe[1]); 790 close(go_pipe[0]); 791 /* 792 * wait for child to settle 793 */ 794 if (read(child_ready_pipe[0], &bf, 1) == -1) { 795 perror("unable to read pipe"); 796 goto out_close_pipes; 797 } 798 799 evlist->workload.cork_fd = go_pipe[1]; 800 close(child_ready_pipe[0]); 801 return 0; 802 803 out_close_pipes: 804 close(go_pipe[0]); 805 close(go_pipe[1]); 806 out_close_ready_pipe: 807 close(child_ready_pipe[0]); 808 close(child_ready_pipe[1]); 809 return -1; 810 } 811 812 int perf_evlist__start_workload(struct perf_evlist *evlist) 813 { 814 if (evlist->workload.cork_fd > 0) { 815 /* 816 * Remove the cork, let it rip! 817 */ 818 return close(evlist->workload.cork_fd); 819 } 820 821 return 0; 822 } 823 824 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 825 struct perf_sample *sample) 826 { 827 struct perf_evsel *evsel = perf_evlist__first(evlist); 828 return perf_evsel__parse_sample(evsel, event, sample); 829 } 830 831 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 832 { 833 struct perf_evsel *evsel; 834 size_t printed = 0; 835 836 list_for_each_entry(evsel, &evlist->entries, node) { 837 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 838 perf_evsel__name(evsel)); 839 } 840 841 return printed + fprintf(fp, "\n");; 842 } 843