1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include "string2.h" 5 #include <sys/param.h> 6 #include <sys/types.h> 7 #include <byteswap.h> 8 #include <unistd.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <linux/compiler.h> 12 #include <linux/list.h> 13 #include <linux/kernel.h> 14 #include <linux/bitops.h> 15 #include <linux/string.h> 16 #include <linux/stringify.h> 17 #include <linux/zalloc.h> 18 #include <sys/stat.h> 19 #include <sys/utsname.h> 20 #include <linux/time64.h> 21 #include <dirent.h> 22 #include <bpf/libbpf.h> 23 #include <perf/cpumap.h> 24 25 #include "evlist.h" 26 #include "evsel.h" 27 #include "header.h" 28 #include "memswap.h" 29 #include "../perf.h" 30 #include "trace-event.h" 31 #include "session.h" 32 #include "symbol.h" 33 #include "debug.h" 34 #include "cpumap.h" 35 #include "pmu.h" 36 #include "vdso.h" 37 #include "strbuf.h" 38 #include "build-id.h" 39 #include "data.h" 40 #include <api/fs/fs.h> 41 #include "asm/bug.h" 42 #include "tool.h" 43 #include "time-utils.h" 44 #include "units.h" 45 #include "cputopo.h" 46 #include "bpf-event.h" 47 48 #include <linux/ctype.h> 49 50 /* 51 * magic2 = "PERFILE2" 52 * must be a numerical value to let the endianness 53 * determine the memory layout. That way we are able 54 * to detect endianness when reading the perf.data file 55 * back. 56 * 57 * we check for legacy (PERFFILE) format. 58 */ 59 static const char *__perf_magic1 = "PERFFILE"; 60 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 61 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 62 63 #define PERF_MAGIC __perf_magic2 64 65 const char perf_version_string[] = PERF_VERSION; 66 67 struct perf_file_attr { 68 struct perf_event_attr attr; 69 struct perf_file_section ids; 70 }; 71 72 struct feat_fd { 73 struct perf_header *ph; 74 int fd; 75 void *buf; /* Either buf != NULL or fd >= 0 */ 76 ssize_t offset; 77 size_t size; 78 struct evsel *events; 79 }; 80 81 void perf_header__set_feat(struct perf_header *header, int feat) 82 { 83 set_bit(feat, header->adds_features); 84 } 85 86 void perf_header__clear_feat(struct perf_header *header, int feat) 87 { 88 clear_bit(feat, header->adds_features); 89 } 90 91 bool perf_header__has_feat(const struct perf_header *header, int feat) 92 { 93 return test_bit(feat, header->adds_features); 94 } 95 96 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size) 97 { 98 ssize_t ret = writen(ff->fd, buf, size); 99 100 if (ret != (ssize_t)size) 101 return ret < 0 ? (int)ret : -1; 102 return 0; 103 } 104 105 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size) 106 { 107 /* struct perf_event_header::size is u16 */ 108 const size_t max_size = 0xffff - sizeof(struct perf_event_header); 109 size_t new_size = ff->size; 110 void *addr; 111 112 if (size + ff->offset > max_size) 113 return -E2BIG; 114 115 while (size > (new_size - ff->offset)) 116 new_size <<= 1; 117 new_size = min(max_size, new_size); 118 119 if (ff->size < new_size) { 120 addr = realloc(ff->buf, new_size); 121 if (!addr) 122 return -ENOMEM; 123 ff->buf = addr; 124 ff->size = new_size; 125 } 126 127 memcpy(ff->buf + ff->offset, buf, size); 128 ff->offset += size; 129 130 return 0; 131 } 132 133 /* Return: 0 if succeded, -ERR if failed. */ 134 int do_write(struct feat_fd *ff, const void *buf, size_t size) 135 { 136 if (!ff->buf) 137 return __do_write_fd(ff, buf, size); 138 return __do_write_buf(ff, buf, size); 139 } 140 141 /* Return: 0 if succeded, -ERR if failed. */ 142 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size) 143 { 144 u64 *p = (u64 *) set; 145 int i, ret; 146 147 ret = do_write(ff, &size, sizeof(size)); 148 if (ret < 0) 149 return ret; 150 151 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 152 ret = do_write(ff, p + i, sizeof(*p)); 153 if (ret < 0) 154 return ret; 155 } 156 157 return 0; 158 } 159 160 /* Return: 0 if succeded, -ERR if failed. */ 161 int write_padded(struct feat_fd *ff, const void *bf, 162 size_t count, size_t count_aligned) 163 { 164 static const char zero_buf[NAME_ALIGN]; 165 int err = do_write(ff, bf, count); 166 167 if (!err) 168 err = do_write(ff, zero_buf, count_aligned - count); 169 170 return err; 171 } 172 173 #define string_size(str) \ 174 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) 175 176 /* Return: 0 if succeded, -ERR if failed. */ 177 static int do_write_string(struct feat_fd *ff, const char *str) 178 { 179 u32 len, olen; 180 int ret; 181 182 olen = strlen(str) + 1; 183 len = PERF_ALIGN(olen, NAME_ALIGN); 184 185 /* write len, incl. \0 */ 186 ret = do_write(ff, &len, sizeof(len)); 187 if (ret < 0) 188 return ret; 189 190 return write_padded(ff, str, olen, len); 191 } 192 193 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size) 194 { 195 ssize_t ret = readn(ff->fd, addr, size); 196 197 if (ret != size) 198 return ret < 0 ? (int)ret : -1; 199 return 0; 200 } 201 202 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size) 203 { 204 if (size > (ssize_t)ff->size - ff->offset) 205 return -1; 206 207 memcpy(addr, ff->buf + ff->offset, size); 208 ff->offset += size; 209 210 return 0; 211 212 } 213 214 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size) 215 { 216 if (!ff->buf) 217 return __do_read_fd(ff, addr, size); 218 return __do_read_buf(ff, addr, size); 219 } 220 221 static int do_read_u32(struct feat_fd *ff, u32 *addr) 222 { 223 int ret; 224 225 ret = __do_read(ff, addr, sizeof(*addr)); 226 if (ret) 227 return ret; 228 229 if (ff->ph->needs_swap) 230 *addr = bswap_32(*addr); 231 return 0; 232 } 233 234 static int do_read_u64(struct feat_fd *ff, u64 *addr) 235 { 236 int ret; 237 238 ret = __do_read(ff, addr, sizeof(*addr)); 239 if (ret) 240 return ret; 241 242 if (ff->ph->needs_swap) 243 *addr = bswap_64(*addr); 244 return 0; 245 } 246 247 static char *do_read_string(struct feat_fd *ff) 248 { 249 u32 len; 250 char *buf; 251 252 if (do_read_u32(ff, &len)) 253 return NULL; 254 255 buf = malloc(len); 256 if (!buf) 257 return NULL; 258 259 if (!__do_read(ff, buf, len)) { 260 /* 261 * strings are padded by zeroes 262 * thus the actual strlen of buf 263 * may be less than len 264 */ 265 return buf; 266 } 267 268 free(buf); 269 return NULL; 270 } 271 272 /* Return: 0 if succeded, -ERR if failed. */ 273 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize) 274 { 275 unsigned long *set; 276 u64 size, *p; 277 int i, ret; 278 279 ret = do_read_u64(ff, &size); 280 if (ret) 281 return ret; 282 283 set = bitmap_alloc(size); 284 if (!set) 285 return -ENOMEM; 286 287 p = (u64 *) set; 288 289 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 290 ret = do_read_u64(ff, p + i); 291 if (ret < 0) { 292 free(set); 293 return ret; 294 } 295 } 296 297 *pset = set; 298 *psize = size; 299 return 0; 300 } 301 302 static int write_tracing_data(struct feat_fd *ff, 303 struct evlist *evlist) 304 { 305 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 306 return -1; 307 308 return read_tracing_data(ff->fd, &evlist->core.entries); 309 } 310 311 static int write_build_id(struct feat_fd *ff, 312 struct evlist *evlist __maybe_unused) 313 { 314 struct perf_session *session; 315 int err; 316 317 session = container_of(ff->ph, struct perf_session, header); 318 319 if (!perf_session__read_build_ids(session, true)) 320 return -1; 321 322 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 323 return -1; 324 325 err = perf_session__write_buildid_table(session, ff); 326 if (err < 0) { 327 pr_debug("failed to write buildid table\n"); 328 return err; 329 } 330 perf_session__cache_build_ids(session); 331 332 return 0; 333 } 334 335 static int write_hostname(struct feat_fd *ff, 336 struct evlist *evlist __maybe_unused) 337 { 338 struct utsname uts; 339 int ret; 340 341 ret = uname(&uts); 342 if (ret < 0) 343 return -1; 344 345 return do_write_string(ff, uts.nodename); 346 } 347 348 static int write_osrelease(struct feat_fd *ff, 349 struct evlist *evlist __maybe_unused) 350 { 351 struct utsname uts; 352 int ret; 353 354 ret = uname(&uts); 355 if (ret < 0) 356 return -1; 357 358 return do_write_string(ff, uts.release); 359 } 360 361 static int write_arch(struct feat_fd *ff, 362 struct evlist *evlist __maybe_unused) 363 { 364 struct utsname uts; 365 int ret; 366 367 ret = uname(&uts); 368 if (ret < 0) 369 return -1; 370 371 return do_write_string(ff, uts.machine); 372 } 373 374 static int write_version(struct feat_fd *ff, 375 struct evlist *evlist __maybe_unused) 376 { 377 return do_write_string(ff, perf_version_string); 378 } 379 380 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc) 381 { 382 FILE *file; 383 char *buf = NULL; 384 char *s, *p; 385 const char *search = cpuinfo_proc; 386 size_t len = 0; 387 int ret = -1; 388 389 if (!search) 390 return -1; 391 392 file = fopen("/proc/cpuinfo", "r"); 393 if (!file) 394 return -1; 395 396 while (getline(&buf, &len, file) > 0) { 397 ret = strncmp(buf, search, strlen(search)); 398 if (!ret) 399 break; 400 } 401 402 if (ret) { 403 ret = -1; 404 goto done; 405 } 406 407 s = buf; 408 409 p = strchr(buf, ':'); 410 if (p && *(p+1) == ' ' && *(p+2)) 411 s = p + 2; 412 p = strchr(s, '\n'); 413 if (p) 414 *p = '\0'; 415 416 /* squash extra space characters (branding string) */ 417 p = s; 418 while (*p) { 419 if (isspace(*p)) { 420 char *r = p + 1; 421 char *q = skip_spaces(r); 422 *p = ' '; 423 if (q != (p+1)) 424 while ((*r++ = *q++)); 425 } 426 p++; 427 } 428 ret = do_write_string(ff, s); 429 done: 430 free(buf); 431 fclose(file); 432 return ret; 433 } 434 435 static int write_cpudesc(struct feat_fd *ff, 436 struct evlist *evlist __maybe_unused) 437 { 438 const char *cpuinfo_procs[] = CPUINFO_PROC; 439 unsigned int i; 440 441 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { 442 int ret; 443 ret = __write_cpudesc(ff, cpuinfo_procs[i]); 444 if (ret >= 0) 445 return ret; 446 } 447 return -1; 448 } 449 450 451 static int write_nrcpus(struct feat_fd *ff, 452 struct evlist *evlist __maybe_unused) 453 { 454 long nr; 455 u32 nrc, nra; 456 int ret; 457 458 nrc = cpu__max_present_cpu(); 459 460 nr = sysconf(_SC_NPROCESSORS_ONLN); 461 if (nr < 0) 462 return -1; 463 464 nra = (u32)(nr & UINT_MAX); 465 466 ret = do_write(ff, &nrc, sizeof(nrc)); 467 if (ret < 0) 468 return ret; 469 470 return do_write(ff, &nra, sizeof(nra)); 471 } 472 473 static int write_event_desc(struct feat_fd *ff, 474 struct evlist *evlist) 475 { 476 struct evsel *evsel; 477 u32 nre, nri, sz; 478 int ret; 479 480 nre = evlist->core.nr_entries; 481 482 /* 483 * write number of events 484 */ 485 ret = do_write(ff, &nre, sizeof(nre)); 486 if (ret < 0) 487 return ret; 488 489 /* 490 * size of perf_event_attr struct 491 */ 492 sz = (u32)sizeof(evsel->core.attr); 493 ret = do_write(ff, &sz, sizeof(sz)); 494 if (ret < 0) 495 return ret; 496 497 evlist__for_each_entry(evlist, evsel) { 498 ret = do_write(ff, &evsel->core.attr, sz); 499 if (ret < 0) 500 return ret; 501 /* 502 * write number of unique id per event 503 * there is one id per instance of an event 504 * 505 * copy into an nri to be independent of the 506 * type of ids, 507 */ 508 nri = evsel->ids; 509 ret = do_write(ff, &nri, sizeof(nri)); 510 if (ret < 0) 511 return ret; 512 513 /* 514 * write event string as passed on cmdline 515 */ 516 ret = do_write_string(ff, perf_evsel__name(evsel)); 517 if (ret < 0) 518 return ret; 519 /* 520 * write unique ids for this event 521 */ 522 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64)); 523 if (ret < 0) 524 return ret; 525 } 526 return 0; 527 } 528 529 static int write_cmdline(struct feat_fd *ff, 530 struct evlist *evlist __maybe_unused) 531 { 532 char pbuf[MAXPATHLEN], *buf; 533 int i, ret, n; 534 535 /* actual path to perf binary */ 536 buf = perf_exe(pbuf, MAXPATHLEN); 537 538 /* account for binary path */ 539 n = perf_env.nr_cmdline + 1; 540 541 ret = do_write(ff, &n, sizeof(n)); 542 if (ret < 0) 543 return ret; 544 545 ret = do_write_string(ff, buf); 546 if (ret < 0) 547 return ret; 548 549 for (i = 0 ; i < perf_env.nr_cmdline; i++) { 550 ret = do_write_string(ff, perf_env.cmdline_argv[i]); 551 if (ret < 0) 552 return ret; 553 } 554 return 0; 555 } 556 557 558 static int write_cpu_topology(struct feat_fd *ff, 559 struct evlist *evlist __maybe_unused) 560 { 561 struct cpu_topology *tp; 562 u32 i; 563 int ret, j; 564 565 tp = cpu_topology__new(); 566 if (!tp) 567 return -1; 568 569 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib)); 570 if (ret < 0) 571 goto done; 572 573 for (i = 0; i < tp->core_sib; i++) { 574 ret = do_write_string(ff, tp->core_siblings[i]); 575 if (ret < 0) 576 goto done; 577 } 578 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib)); 579 if (ret < 0) 580 goto done; 581 582 for (i = 0; i < tp->thread_sib; i++) { 583 ret = do_write_string(ff, tp->thread_siblings[i]); 584 if (ret < 0) 585 break; 586 } 587 588 ret = perf_env__read_cpu_topology_map(&perf_env); 589 if (ret < 0) 590 goto done; 591 592 for (j = 0; j < perf_env.nr_cpus_avail; j++) { 593 ret = do_write(ff, &perf_env.cpu[j].core_id, 594 sizeof(perf_env.cpu[j].core_id)); 595 if (ret < 0) 596 return ret; 597 ret = do_write(ff, &perf_env.cpu[j].socket_id, 598 sizeof(perf_env.cpu[j].socket_id)); 599 if (ret < 0) 600 return ret; 601 } 602 603 if (!tp->die_sib) 604 goto done; 605 606 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib)); 607 if (ret < 0) 608 goto done; 609 610 for (i = 0; i < tp->die_sib; i++) { 611 ret = do_write_string(ff, tp->die_siblings[i]); 612 if (ret < 0) 613 goto done; 614 } 615 616 for (j = 0; j < perf_env.nr_cpus_avail; j++) { 617 ret = do_write(ff, &perf_env.cpu[j].die_id, 618 sizeof(perf_env.cpu[j].die_id)); 619 if (ret < 0) 620 return ret; 621 } 622 623 done: 624 cpu_topology__delete(tp); 625 return ret; 626 } 627 628 629 630 static int write_total_mem(struct feat_fd *ff, 631 struct evlist *evlist __maybe_unused) 632 { 633 char *buf = NULL; 634 FILE *fp; 635 size_t len = 0; 636 int ret = -1, n; 637 uint64_t mem; 638 639 fp = fopen("/proc/meminfo", "r"); 640 if (!fp) 641 return -1; 642 643 while (getline(&buf, &len, fp) > 0) { 644 ret = strncmp(buf, "MemTotal:", 9); 645 if (!ret) 646 break; 647 } 648 if (!ret) { 649 n = sscanf(buf, "%*s %"PRIu64, &mem); 650 if (n == 1) 651 ret = do_write(ff, &mem, sizeof(mem)); 652 } else 653 ret = -1; 654 free(buf); 655 fclose(fp); 656 return ret; 657 } 658 659 static int write_numa_topology(struct feat_fd *ff, 660 struct evlist *evlist __maybe_unused) 661 { 662 struct numa_topology *tp; 663 int ret = -1; 664 u32 i; 665 666 tp = numa_topology__new(); 667 if (!tp) 668 return -ENOMEM; 669 670 ret = do_write(ff, &tp->nr, sizeof(u32)); 671 if (ret < 0) 672 goto err; 673 674 for (i = 0; i < tp->nr; i++) { 675 struct numa_topology_node *n = &tp->nodes[i]; 676 677 ret = do_write(ff, &n->node, sizeof(u32)); 678 if (ret < 0) 679 goto err; 680 681 ret = do_write(ff, &n->mem_total, sizeof(u64)); 682 if (ret) 683 goto err; 684 685 ret = do_write(ff, &n->mem_free, sizeof(u64)); 686 if (ret) 687 goto err; 688 689 ret = do_write_string(ff, n->cpus); 690 if (ret < 0) 691 goto err; 692 } 693 694 ret = 0; 695 696 err: 697 numa_topology__delete(tp); 698 return ret; 699 } 700 701 /* 702 * File format: 703 * 704 * struct pmu_mappings { 705 * u32 pmu_num; 706 * struct pmu_map { 707 * u32 type; 708 * char name[]; 709 * }[pmu_num]; 710 * }; 711 */ 712 713 static int write_pmu_mappings(struct feat_fd *ff, 714 struct evlist *evlist __maybe_unused) 715 { 716 struct perf_pmu *pmu = NULL; 717 u32 pmu_num = 0; 718 int ret; 719 720 /* 721 * Do a first pass to count number of pmu to avoid lseek so this 722 * works in pipe mode as well. 723 */ 724 while ((pmu = perf_pmu__scan(pmu))) { 725 if (!pmu->name) 726 continue; 727 pmu_num++; 728 } 729 730 ret = do_write(ff, &pmu_num, sizeof(pmu_num)); 731 if (ret < 0) 732 return ret; 733 734 while ((pmu = perf_pmu__scan(pmu))) { 735 if (!pmu->name) 736 continue; 737 738 ret = do_write(ff, &pmu->type, sizeof(pmu->type)); 739 if (ret < 0) 740 return ret; 741 742 ret = do_write_string(ff, pmu->name); 743 if (ret < 0) 744 return ret; 745 } 746 747 return 0; 748 } 749 750 /* 751 * File format: 752 * 753 * struct group_descs { 754 * u32 nr_groups; 755 * struct group_desc { 756 * char name[]; 757 * u32 leader_idx; 758 * u32 nr_members; 759 * }[nr_groups]; 760 * }; 761 */ 762 static int write_group_desc(struct feat_fd *ff, 763 struct evlist *evlist) 764 { 765 u32 nr_groups = evlist->nr_groups; 766 struct evsel *evsel; 767 int ret; 768 769 ret = do_write(ff, &nr_groups, sizeof(nr_groups)); 770 if (ret < 0) 771 return ret; 772 773 evlist__for_each_entry(evlist, evsel) { 774 if (perf_evsel__is_group_leader(evsel) && 775 evsel->core.nr_members > 1) { 776 const char *name = evsel->group_name ?: "{anon_group}"; 777 u32 leader_idx = evsel->idx; 778 u32 nr_members = evsel->core.nr_members; 779 780 ret = do_write_string(ff, name); 781 if (ret < 0) 782 return ret; 783 784 ret = do_write(ff, &leader_idx, sizeof(leader_idx)); 785 if (ret < 0) 786 return ret; 787 788 ret = do_write(ff, &nr_members, sizeof(nr_members)); 789 if (ret < 0) 790 return ret; 791 } 792 } 793 return 0; 794 } 795 796 /* 797 * Return the CPU id as a raw string. 798 * 799 * Each architecture should provide a more precise id string that 800 * can be use to match the architecture's "mapfile". 801 */ 802 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 803 { 804 return NULL; 805 } 806 807 /* Return zero when the cpuid from the mapfile.csv matches the 808 * cpuid string generated on this platform. 809 * Otherwise return non-zero. 810 */ 811 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) 812 { 813 regex_t re; 814 regmatch_t pmatch[1]; 815 int match; 816 817 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { 818 /* Warn unable to generate match particular string. */ 819 pr_info("Invalid regular expression %s\n", mapcpuid); 820 return 1; 821 } 822 823 match = !regexec(&re, cpuid, 1, pmatch, 0); 824 regfree(&re); 825 if (match) { 826 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); 827 828 /* Verify the entire string matched. */ 829 if (match_len == strlen(cpuid)) 830 return 0; 831 } 832 return 1; 833 } 834 835 /* 836 * default get_cpuid(): nothing gets recorded 837 * actual implementation must be in arch/$(SRCARCH)/util/header.c 838 */ 839 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused) 840 { 841 return -1; 842 } 843 844 static int write_cpuid(struct feat_fd *ff, 845 struct evlist *evlist __maybe_unused) 846 { 847 char buffer[64]; 848 int ret; 849 850 ret = get_cpuid(buffer, sizeof(buffer)); 851 if (ret) 852 return -1; 853 854 return do_write_string(ff, buffer); 855 } 856 857 static int write_branch_stack(struct feat_fd *ff __maybe_unused, 858 struct evlist *evlist __maybe_unused) 859 { 860 return 0; 861 } 862 863 static int write_auxtrace(struct feat_fd *ff, 864 struct evlist *evlist __maybe_unused) 865 { 866 struct perf_session *session; 867 int err; 868 869 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 870 return -1; 871 872 session = container_of(ff->ph, struct perf_session, header); 873 874 err = auxtrace_index__write(ff->fd, &session->auxtrace_index); 875 if (err < 0) 876 pr_err("Failed to write auxtrace index\n"); 877 return err; 878 } 879 880 static int write_clockid(struct feat_fd *ff, 881 struct evlist *evlist __maybe_unused) 882 { 883 return do_write(ff, &ff->ph->env.clockid_res_ns, 884 sizeof(ff->ph->env.clockid_res_ns)); 885 } 886 887 static int write_dir_format(struct feat_fd *ff, 888 struct evlist *evlist __maybe_unused) 889 { 890 struct perf_session *session; 891 struct perf_data *data; 892 893 session = container_of(ff->ph, struct perf_session, header); 894 data = session->data; 895 896 if (WARN_ON(!perf_data__is_dir(data))) 897 return -1; 898 899 return do_write(ff, &data->dir.version, sizeof(data->dir.version)); 900 } 901 902 #ifdef HAVE_LIBBPF_SUPPORT 903 static int write_bpf_prog_info(struct feat_fd *ff, 904 struct evlist *evlist __maybe_unused) 905 { 906 struct perf_env *env = &ff->ph->env; 907 struct rb_root *root; 908 struct rb_node *next; 909 int ret; 910 911 down_read(&env->bpf_progs.lock); 912 913 ret = do_write(ff, &env->bpf_progs.infos_cnt, 914 sizeof(env->bpf_progs.infos_cnt)); 915 if (ret < 0) 916 goto out; 917 918 root = &env->bpf_progs.infos; 919 next = rb_first(root); 920 while (next) { 921 struct bpf_prog_info_node *node; 922 size_t len; 923 924 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 925 next = rb_next(&node->rb_node); 926 len = sizeof(struct bpf_prog_info_linear) + 927 node->info_linear->data_len; 928 929 /* before writing to file, translate address to offset */ 930 bpf_program__bpil_addr_to_offs(node->info_linear); 931 ret = do_write(ff, node->info_linear, len); 932 /* 933 * translate back to address even when do_write() fails, 934 * so that this function never changes the data. 935 */ 936 bpf_program__bpil_offs_to_addr(node->info_linear); 937 if (ret < 0) 938 goto out; 939 } 940 out: 941 up_read(&env->bpf_progs.lock); 942 return ret; 943 } 944 #else // HAVE_LIBBPF_SUPPORT 945 static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused, 946 struct evlist *evlist __maybe_unused) 947 { 948 return 0; 949 } 950 #endif // HAVE_LIBBPF_SUPPORT 951 952 static int write_bpf_btf(struct feat_fd *ff, 953 struct evlist *evlist __maybe_unused) 954 { 955 struct perf_env *env = &ff->ph->env; 956 struct rb_root *root; 957 struct rb_node *next; 958 int ret; 959 960 down_read(&env->bpf_progs.lock); 961 962 ret = do_write(ff, &env->bpf_progs.btfs_cnt, 963 sizeof(env->bpf_progs.btfs_cnt)); 964 965 if (ret < 0) 966 goto out; 967 968 root = &env->bpf_progs.btfs; 969 next = rb_first(root); 970 while (next) { 971 struct btf_node *node; 972 973 node = rb_entry(next, struct btf_node, rb_node); 974 next = rb_next(&node->rb_node); 975 ret = do_write(ff, &node->id, 976 sizeof(u32) * 2 + node->data_size); 977 if (ret < 0) 978 goto out; 979 } 980 out: 981 up_read(&env->bpf_progs.lock); 982 return ret; 983 } 984 985 static int cpu_cache_level__sort(const void *a, const void *b) 986 { 987 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a; 988 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b; 989 990 return cache_a->level - cache_b->level; 991 } 992 993 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b) 994 { 995 if (a->level != b->level) 996 return false; 997 998 if (a->line_size != b->line_size) 999 return false; 1000 1001 if (a->sets != b->sets) 1002 return false; 1003 1004 if (a->ways != b->ways) 1005 return false; 1006 1007 if (strcmp(a->type, b->type)) 1008 return false; 1009 1010 if (strcmp(a->size, b->size)) 1011 return false; 1012 1013 if (strcmp(a->map, b->map)) 1014 return false; 1015 1016 return true; 1017 } 1018 1019 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) 1020 { 1021 char path[PATH_MAX], file[PATH_MAX]; 1022 struct stat st; 1023 size_t len; 1024 1025 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); 1026 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path); 1027 1028 if (stat(file, &st)) 1029 return 1; 1030 1031 scnprintf(file, PATH_MAX, "%s/level", path); 1032 if (sysfs__read_int(file, (int *) &cache->level)) 1033 return -1; 1034 1035 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path); 1036 if (sysfs__read_int(file, (int *) &cache->line_size)) 1037 return -1; 1038 1039 scnprintf(file, PATH_MAX, "%s/number_of_sets", path); 1040 if (sysfs__read_int(file, (int *) &cache->sets)) 1041 return -1; 1042 1043 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path); 1044 if (sysfs__read_int(file, (int *) &cache->ways)) 1045 return -1; 1046 1047 scnprintf(file, PATH_MAX, "%s/type", path); 1048 if (sysfs__read_str(file, &cache->type, &len)) 1049 return -1; 1050 1051 cache->type[len] = 0; 1052 cache->type = strim(cache->type); 1053 1054 scnprintf(file, PATH_MAX, "%s/size", path); 1055 if (sysfs__read_str(file, &cache->size, &len)) { 1056 zfree(&cache->type); 1057 return -1; 1058 } 1059 1060 cache->size[len] = 0; 1061 cache->size = strim(cache->size); 1062 1063 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); 1064 if (sysfs__read_str(file, &cache->map, &len)) { 1065 zfree(&cache->map); 1066 zfree(&cache->type); 1067 return -1; 1068 } 1069 1070 cache->map[len] = 0; 1071 cache->map = strim(cache->map); 1072 return 0; 1073 } 1074 1075 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c) 1076 { 1077 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); 1078 } 1079 1080 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp) 1081 { 1082 u32 i, cnt = 0; 1083 long ncpus; 1084 u32 nr, cpu; 1085 u16 level; 1086 1087 ncpus = sysconf(_SC_NPROCESSORS_CONF); 1088 if (ncpus < 0) 1089 return -1; 1090 1091 nr = (u32)(ncpus & UINT_MAX); 1092 1093 for (cpu = 0; cpu < nr; cpu++) { 1094 for (level = 0; level < 10; level++) { 1095 struct cpu_cache_level c; 1096 int err; 1097 1098 err = cpu_cache_level__read(&c, cpu, level); 1099 if (err < 0) 1100 return err; 1101 1102 if (err == 1) 1103 break; 1104 1105 for (i = 0; i < cnt; i++) { 1106 if (cpu_cache_level__cmp(&c, &caches[i])) 1107 break; 1108 } 1109 1110 if (i == cnt) 1111 caches[cnt++] = c; 1112 else 1113 cpu_cache_level__free(&c); 1114 1115 if (WARN_ONCE(cnt == size, "way too many cpu caches..")) 1116 goto out; 1117 } 1118 } 1119 out: 1120 *cntp = cnt; 1121 return 0; 1122 } 1123 1124 #define MAX_CACHES (MAX_NR_CPUS * 4) 1125 1126 static int write_cache(struct feat_fd *ff, 1127 struct evlist *evlist __maybe_unused) 1128 { 1129 struct cpu_cache_level caches[MAX_CACHES]; 1130 u32 cnt = 0, i, version = 1; 1131 int ret; 1132 1133 ret = build_caches(caches, MAX_CACHES, &cnt); 1134 if (ret) 1135 goto out; 1136 1137 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort); 1138 1139 ret = do_write(ff, &version, sizeof(u32)); 1140 if (ret < 0) 1141 goto out; 1142 1143 ret = do_write(ff, &cnt, sizeof(u32)); 1144 if (ret < 0) 1145 goto out; 1146 1147 for (i = 0; i < cnt; i++) { 1148 struct cpu_cache_level *c = &caches[i]; 1149 1150 #define _W(v) \ 1151 ret = do_write(ff, &c->v, sizeof(u32)); \ 1152 if (ret < 0) \ 1153 goto out; 1154 1155 _W(level) 1156 _W(line_size) 1157 _W(sets) 1158 _W(ways) 1159 #undef _W 1160 1161 #define _W(v) \ 1162 ret = do_write_string(ff, (const char *) c->v); \ 1163 if (ret < 0) \ 1164 goto out; 1165 1166 _W(type) 1167 _W(size) 1168 _W(map) 1169 #undef _W 1170 } 1171 1172 out: 1173 for (i = 0; i < cnt; i++) 1174 cpu_cache_level__free(&caches[i]); 1175 return ret; 1176 } 1177 1178 static int write_stat(struct feat_fd *ff __maybe_unused, 1179 struct evlist *evlist __maybe_unused) 1180 { 1181 return 0; 1182 } 1183 1184 static int write_sample_time(struct feat_fd *ff, 1185 struct evlist *evlist) 1186 { 1187 int ret; 1188 1189 ret = do_write(ff, &evlist->first_sample_time, 1190 sizeof(evlist->first_sample_time)); 1191 if (ret < 0) 1192 return ret; 1193 1194 return do_write(ff, &evlist->last_sample_time, 1195 sizeof(evlist->last_sample_time)); 1196 } 1197 1198 1199 static int memory_node__read(struct memory_node *n, unsigned long idx) 1200 { 1201 unsigned int phys, size = 0; 1202 char path[PATH_MAX]; 1203 struct dirent *ent; 1204 DIR *dir; 1205 1206 #define for_each_memory(mem, dir) \ 1207 while ((ent = readdir(dir))) \ 1208 if (strcmp(ent->d_name, ".") && \ 1209 strcmp(ent->d_name, "..") && \ 1210 sscanf(ent->d_name, "memory%u", &mem) == 1) 1211 1212 scnprintf(path, PATH_MAX, 1213 "%s/devices/system/node/node%lu", 1214 sysfs__mountpoint(), idx); 1215 1216 dir = opendir(path); 1217 if (!dir) { 1218 pr_warning("failed: cant' open memory sysfs data\n"); 1219 return -1; 1220 } 1221 1222 for_each_memory(phys, dir) { 1223 size = max(phys, size); 1224 } 1225 1226 size++; 1227 1228 n->set = bitmap_alloc(size); 1229 if (!n->set) { 1230 closedir(dir); 1231 return -ENOMEM; 1232 } 1233 1234 n->node = idx; 1235 n->size = size; 1236 1237 rewinddir(dir); 1238 1239 for_each_memory(phys, dir) { 1240 set_bit(phys, n->set); 1241 } 1242 1243 closedir(dir); 1244 return 0; 1245 } 1246 1247 static int memory_node__sort(const void *a, const void *b) 1248 { 1249 const struct memory_node *na = a; 1250 const struct memory_node *nb = b; 1251 1252 return na->node - nb->node; 1253 } 1254 1255 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) 1256 { 1257 char path[PATH_MAX]; 1258 struct dirent *ent; 1259 DIR *dir; 1260 u64 cnt = 0; 1261 int ret = 0; 1262 1263 scnprintf(path, PATH_MAX, "%s/devices/system/node/", 1264 sysfs__mountpoint()); 1265 1266 dir = opendir(path); 1267 if (!dir) { 1268 pr_debug2("%s: could't read %s, does this arch have topology information?\n", 1269 __func__, path); 1270 return -1; 1271 } 1272 1273 while (!ret && (ent = readdir(dir))) { 1274 unsigned int idx; 1275 int r; 1276 1277 if (!strcmp(ent->d_name, ".") || 1278 !strcmp(ent->d_name, "..")) 1279 continue; 1280 1281 r = sscanf(ent->d_name, "node%u", &idx); 1282 if (r != 1) 1283 continue; 1284 1285 if (WARN_ONCE(cnt >= size, 1286 "failed to write MEM_TOPOLOGY, way too many nodes\n")) 1287 return -1; 1288 1289 ret = memory_node__read(&nodes[cnt++], idx); 1290 } 1291 1292 *cntp = cnt; 1293 closedir(dir); 1294 1295 if (!ret) 1296 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); 1297 1298 return ret; 1299 } 1300 1301 #define MAX_MEMORY_NODES 2000 1302 1303 /* 1304 * The MEM_TOPOLOGY holds physical memory map for every 1305 * node in system. The format of data is as follows: 1306 * 1307 * 0 - version | for future changes 1308 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes 1309 * 16 - count | number of nodes 1310 * 1311 * For each node we store map of physical indexes for 1312 * each node: 1313 * 1314 * 32 - node id | node index 1315 * 40 - size | size of bitmap 1316 * 48 - bitmap | bitmap of memory indexes that belongs to node 1317 */ 1318 static int write_mem_topology(struct feat_fd *ff __maybe_unused, 1319 struct evlist *evlist __maybe_unused) 1320 { 1321 static struct memory_node nodes[MAX_MEMORY_NODES]; 1322 u64 bsize, version = 1, i, nr; 1323 int ret; 1324 1325 ret = sysfs__read_xll("devices/system/memory/block_size_bytes", 1326 (unsigned long long *) &bsize); 1327 if (ret) 1328 return ret; 1329 1330 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr); 1331 if (ret) 1332 return ret; 1333 1334 ret = do_write(ff, &version, sizeof(version)); 1335 if (ret < 0) 1336 goto out; 1337 1338 ret = do_write(ff, &bsize, sizeof(bsize)); 1339 if (ret < 0) 1340 goto out; 1341 1342 ret = do_write(ff, &nr, sizeof(nr)); 1343 if (ret < 0) 1344 goto out; 1345 1346 for (i = 0; i < nr; i++) { 1347 struct memory_node *n = &nodes[i]; 1348 1349 #define _W(v) \ 1350 ret = do_write(ff, &n->v, sizeof(n->v)); \ 1351 if (ret < 0) \ 1352 goto out; 1353 1354 _W(node) 1355 _W(size) 1356 1357 #undef _W 1358 1359 ret = do_write_bitmap(ff, n->set, n->size); 1360 if (ret < 0) 1361 goto out; 1362 } 1363 1364 out: 1365 return ret; 1366 } 1367 1368 static int write_compressed(struct feat_fd *ff __maybe_unused, 1369 struct evlist *evlist __maybe_unused) 1370 { 1371 int ret; 1372 1373 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); 1374 if (ret) 1375 return ret; 1376 1377 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); 1378 if (ret) 1379 return ret; 1380 1381 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); 1382 if (ret) 1383 return ret; 1384 1385 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); 1386 if (ret) 1387 return ret; 1388 1389 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); 1390 } 1391 1392 static void print_hostname(struct feat_fd *ff, FILE *fp) 1393 { 1394 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname); 1395 } 1396 1397 static void print_osrelease(struct feat_fd *ff, FILE *fp) 1398 { 1399 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); 1400 } 1401 1402 static void print_arch(struct feat_fd *ff, FILE *fp) 1403 { 1404 fprintf(fp, "# arch : %s\n", ff->ph->env.arch); 1405 } 1406 1407 static void print_cpudesc(struct feat_fd *ff, FILE *fp) 1408 { 1409 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); 1410 } 1411 1412 static void print_nrcpus(struct feat_fd *ff, FILE *fp) 1413 { 1414 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); 1415 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); 1416 } 1417 1418 static void print_version(struct feat_fd *ff, FILE *fp) 1419 { 1420 fprintf(fp, "# perf version : %s\n", ff->ph->env.version); 1421 } 1422 1423 static void print_cmdline(struct feat_fd *ff, FILE *fp) 1424 { 1425 int nr, i; 1426 1427 nr = ff->ph->env.nr_cmdline; 1428 1429 fprintf(fp, "# cmdline : "); 1430 1431 for (i = 0; i < nr; i++) { 1432 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); 1433 if (!argv_i) { 1434 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); 1435 } else { 1436 char *mem = argv_i; 1437 do { 1438 char *quote = strchr(argv_i, '\''); 1439 if (!quote) 1440 break; 1441 *quote++ = '\0'; 1442 fprintf(fp, "%s\\\'", argv_i); 1443 argv_i = quote; 1444 } while (1); 1445 fprintf(fp, "%s ", argv_i); 1446 free(mem); 1447 } 1448 } 1449 fputc('\n', fp); 1450 } 1451 1452 static void print_cpu_topology(struct feat_fd *ff, FILE *fp) 1453 { 1454 struct perf_header *ph = ff->ph; 1455 int cpu_nr = ph->env.nr_cpus_avail; 1456 int nr, i; 1457 char *str; 1458 1459 nr = ph->env.nr_sibling_cores; 1460 str = ph->env.sibling_cores; 1461 1462 for (i = 0; i < nr; i++) { 1463 fprintf(fp, "# sibling sockets : %s\n", str); 1464 str += strlen(str) + 1; 1465 } 1466 1467 if (ph->env.nr_sibling_dies) { 1468 nr = ph->env.nr_sibling_dies; 1469 str = ph->env.sibling_dies; 1470 1471 for (i = 0; i < nr; i++) { 1472 fprintf(fp, "# sibling dies : %s\n", str); 1473 str += strlen(str) + 1; 1474 } 1475 } 1476 1477 nr = ph->env.nr_sibling_threads; 1478 str = ph->env.sibling_threads; 1479 1480 for (i = 0; i < nr; i++) { 1481 fprintf(fp, "# sibling threads : %s\n", str); 1482 str += strlen(str) + 1; 1483 } 1484 1485 if (ph->env.nr_sibling_dies) { 1486 if (ph->env.cpu != NULL) { 1487 for (i = 0; i < cpu_nr; i++) 1488 fprintf(fp, "# CPU %d: Core ID %d, " 1489 "Die ID %d, Socket ID %d\n", 1490 i, ph->env.cpu[i].core_id, 1491 ph->env.cpu[i].die_id, 1492 ph->env.cpu[i].socket_id); 1493 } else 1494 fprintf(fp, "# Core ID, Die ID and Socket ID " 1495 "information is not available\n"); 1496 } else { 1497 if (ph->env.cpu != NULL) { 1498 for (i = 0; i < cpu_nr; i++) 1499 fprintf(fp, "# CPU %d: Core ID %d, " 1500 "Socket ID %d\n", 1501 i, ph->env.cpu[i].core_id, 1502 ph->env.cpu[i].socket_id); 1503 } else 1504 fprintf(fp, "# Core ID and Socket ID " 1505 "information is not available\n"); 1506 } 1507 } 1508 1509 static void print_clockid(struct feat_fd *ff, FILE *fp) 1510 { 1511 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n", 1512 ff->ph->env.clockid_res_ns * 1000); 1513 } 1514 1515 static void print_dir_format(struct feat_fd *ff, FILE *fp) 1516 { 1517 struct perf_session *session; 1518 struct perf_data *data; 1519 1520 session = container_of(ff->ph, struct perf_session, header); 1521 data = session->data; 1522 1523 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); 1524 } 1525 1526 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp) 1527 { 1528 struct perf_env *env = &ff->ph->env; 1529 struct rb_root *root; 1530 struct rb_node *next; 1531 1532 down_read(&env->bpf_progs.lock); 1533 1534 root = &env->bpf_progs.infos; 1535 next = rb_first(root); 1536 1537 while (next) { 1538 struct bpf_prog_info_node *node; 1539 1540 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 1541 next = rb_next(&node->rb_node); 1542 1543 bpf_event__print_bpf_prog_info(&node->info_linear->info, 1544 env, fp); 1545 } 1546 1547 up_read(&env->bpf_progs.lock); 1548 } 1549 1550 static void print_bpf_btf(struct feat_fd *ff, FILE *fp) 1551 { 1552 struct perf_env *env = &ff->ph->env; 1553 struct rb_root *root; 1554 struct rb_node *next; 1555 1556 down_read(&env->bpf_progs.lock); 1557 1558 root = &env->bpf_progs.btfs; 1559 next = rb_first(root); 1560 1561 while (next) { 1562 struct btf_node *node; 1563 1564 node = rb_entry(next, struct btf_node, rb_node); 1565 next = rb_next(&node->rb_node); 1566 fprintf(fp, "# btf info of id %u\n", node->id); 1567 } 1568 1569 up_read(&env->bpf_progs.lock); 1570 } 1571 1572 static void free_event_desc(struct evsel *events) 1573 { 1574 struct evsel *evsel; 1575 1576 if (!events) 1577 return; 1578 1579 for (evsel = events; evsel->core.attr.size; evsel++) { 1580 zfree(&evsel->name); 1581 zfree(&evsel->id); 1582 } 1583 1584 free(events); 1585 } 1586 1587 static struct evsel *read_event_desc(struct feat_fd *ff) 1588 { 1589 struct evsel *evsel, *events = NULL; 1590 u64 *id; 1591 void *buf = NULL; 1592 u32 nre, sz, nr, i, j; 1593 size_t msz; 1594 1595 /* number of events */ 1596 if (do_read_u32(ff, &nre)) 1597 goto error; 1598 1599 if (do_read_u32(ff, &sz)) 1600 goto error; 1601 1602 /* buffer to hold on file attr struct */ 1603 buf = malloc(sz); 1604 if (!buf) 1605 goto error; 1606 1607 /* the last event terminates with evsel->core.attr.size == 0: */ 1608 events = calloc(nre + 1, sizeof(*events)); 1609 if (!events) 1610 goto error; 1611 1612 msz = sizeof(evsel->core.attr); 1613 if (sz < msz) 1614 msz = sz; 1615 1616 for (i = 0, evsel = events; i < nre; evsel++, i++) { 1617 evsel->idx = i; 1618 1619 /* 1620 * must read entire on-file attr struct to 1621 * sync up with layout. 1622 */ 1623 if (__do_read(ff, buf, sz)) 1624 goto error; 1625 1626 if (ff->ph->needs_swap) 1627 perf_event__attr_swap(buf); 1628 1629 memcpy(&evsel->core.attr, buf, msz); 1630 1631 if (do_read_u32(ff, &nr)) 1632 goto error; 1633 1634 if (ff->ph->needs_swap) 1635 evsel->needs_swap = true; 1636 1637 evsel->name = do_read_string(ff); 1638 if (!evsel->name) 1639 goto error; 1640 1641 if (!nr) 1642 continue; 1643 1644 id = calloc(nr, sizeof(*id)); 1645 if (!id) 1646 goto error; 1647 evsel->ids = nr; 1648 evsel->id = id; 1649 1650 for (j = 0 ; j < nr; j++) { 1651 if (do_read_u64(ff, id)) 1652 goto error; 1653 id++; 1654 } 1655 } 1656 out: 1657 free(buf); 1658 return events; 1659 error: 1660 free_event_desc(events); 1661 events = NULL; 1662 goto out; 1663 } 1664 1665 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val, 1666 void *priv __maybe_unused) 1667 { 1668 return fprintf(fp, ", %s = %s", name, val); 1669 } 1670 1671 static void print_event_desc(struct feat_fd *ff, FILE *fp) 1672 { 1673 struct evsel *evsel, *events; 1674 u32 j; 1675 u64 *id; 1676 1677 if (ff->events) 1678 events = ff->events; 1679 else 1680 events = read_event_desc(ff); 1681 1682 if (!events) { 1683 fprintf(fp, "# event desc: not available or unable to read\n"); 1684 return; 1685 } 1686 1687 for (evsel = events; evsel->core.attr.size; evsel++) { 1688 fprintf(fp, "# event : name = %s, ", evsel->name); 1689 1690 if (evsel->ids) { 1691 fprintf(fp, ", id = {"); 1692 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { 1693 if (j) 1694 fputc(',', fp); 1695 fprintf(fp, " %"PRIu64, *id); 1696 } 1697 fprintf(fp, " }"); 1698 } 1699 1700 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); 1701 1702 fputc('\n', fp); 1703 } 1704 1705 free_event_desc(events); 1706 ff->events = NULL; 1707 } 1708 1709 static void print_total_mem(struct feat_fd *ff, FILE *fp) 1710 { 1711 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); 1712 } 1713 1714 static void print_numa_topology(struct feat_fd *ff, FILE *fp) 1715 { 1716 int i; 1717 struct numa_node *n; 1718 1719 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { 1720 n = &ff->ph->env.numa_nodes[i]; 1721 1722 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 1723 " free = %"PRIu64" kB\n", 1724 n->node, n->mem_total, n->mem_free); 1725 1726 fprintf(fp, "# node%u cpu list : ", n->node); 1727 cpu_map__fprintf(n->map, fp); 1728 } 1729 } 1730 1731 static void print_cpuid(struct feat_fd *ff, FILE *fp) 1732 { 1733 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); 1734 } 1735 1736 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp) 1737 { 1738 fprintf(fp, "# contains samples with branch stack\n"); 1739 } 1740 1741 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp) 1742 { 1743 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); 1744 } 1745 1746 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp) 1747 { 1748 fprintf(fp, "# contains stat data\n"); 1749 } 1750 1751 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused) 1752 { 1753 int i; 1754 1755 fprintf(fp, "# CPU cache info:\n"); 1756 for (i = 0; i < ff->ph->env.caches_cnt; i++) { 1757 fprintf(fp, "# "); 1758 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); 1759 } 1760 } 1761 1762 static void print_compressed(struct feat_fd *ff, FILE *fp) 1763 { 1764 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n", 1765 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", 1766 ff->ph->env.comp_level, ff->ph->env.comp_ratio); 1767 } 1768 1769 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp) 1770 { 1771 const char *delimiter = "# pmu mappings: "; 1772 char *str, *tmp; 1773 u32 pmu_num; 1774 u32 type; 1775 1776 pmu_num = ff->ph->env.nr_pmu_mappings; 1777 if (!pmu_num) { 1778 fprintf(fp, "# pmu mappings: not available\n"); 1779 return; 1780 } 1781 1782 str = ff->ph->env.pmu_mappings; 1783 1784 while (pmu_num) { 1785 type = strtoul(str, &tmp, 0); 1786 if (*tmp != ':') 1787 goto error; 1788 1789 str = tmp + 1; 1790 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); 1791 1792 delimiter = ", "; 1793 str += strlen(str) + 1; 1794 pmu_num--; 1795 } 1796 1797 fprintf(fp, "\n"); 1798 1799 if (!pmu_num) 1800 return; 1801 error: 1802 fprintf(fp, "# pmu mappings: unable to read\n"); 1803 } 1804 1805 static void print_group_desc(struct feat_fd *ff, FILE *fp) 1806 { 1807 struct perf_session *session; 1808 struct evsel *evsel; 1809 u32 nr = 0; 1810 1811 session = container_of(ff->ph, struct perf_session, header); 1812 1813 evlist__for_each_entry(session->evlist, evsel) { 1814 if (perf_evsel__is_group_leader(evsel) && 1815 evsel->core.nr_members > 1) { 1816 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", 1817 perf_evsel__name(evsel)); 1818 1819 nr = evsel->core.nr_members - 1; 1820 } else if (nr) { 1821 fprintf(fp, ",%s", perf_evsel__name(evsel)); 1822 1823 if (--nr == 0) 1824 fprintf(fp, "}\n"); 1825 } 1826 } 1827 } 1828 1829 static void print_sample_time(struct feat_fd *ff, FILE *fp) 1830 { 1831 struct perf_session *session; 1832 char time_buf[32]; 1833 double d; 1834 1835 session = container_of(ff->ph, struct perf_session, header); 1836 1837 timestamp__scnprintf_usec(session->evlist->first_sample_time, 1838 time_buf, sizeof(time_buf)); 1839 fprintf(fp, "# time of first sample : %s\n", time_buf); 1840 1841 timestamp__scnprintf_usec(session->evlist->last_sample_time, 1842 time_buf, sizeof(time_buf)); 1843 fprintf(fp, "# time of last sample : %s\n", time_buf); 1844 1845 d = (double)(session->evlist->last_sample_time - 1846 session->evlist->first_sample_time) / NSEC_PER_MSEC; 1847 1848 fprintf(fp, "# sample duration : %10.3f ms\n", d); 1849 } 1850 1851 static void memory_node__fprintf(struct memory_node *n, 1852 unsigned long long bsize, FILE *fp) 1853 { 1854 char buf_map[100], buf_size[50]; 1855 unsigned long long size; 1856 1857 size = bsize * bitmap_weight(n->set, n->size); 1858 unit_number__scnprintf(buf_size, 50, size); 1859 1860 bitmap_scnprintf(n->set, n->size, buf_map, 100); 1861 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); 1862 } 1863 1864 static void print_mem_topology(struct feat_fd *ff, FILE *fp) 1865 { 1866 struct memory_node *nodes; 1867 int i, nr; 1868 1869 nodes = ff->ph->env.memory_nodes; 1870 nr = ff->ph->env.nr_memory_nodes; 1871 1872 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", 1873 nr, ff->ph->env.memory_bsize); 1874 1875 for (i = 0; i < nr; i++) { 1876 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp); 1877 } 1878 } 1879 1880 static int __event_process_build_id(struct build_id_event *bev, 1881 char *filename, 1882 struct perf_session *session) 1883 { 1884 int err = -1; 1885 struct machine *machine; 1886 u16 cpumode; 1887 struct dso *dso; 1888 enum dso_kernel_type dso_type; 1889 1890 machine = perf_session__findnew_machine(session, bev->pid); 1891 if (!machine) 1892 goto out; 1893 1894 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1895 1896 switch (cpumode) { 1897 case PERF_RECORD_MISC_KERNEL: 1898 dso_type = DSO_TYPE_KERNEL; 1899 break; 1900 case PERF_RECORD_MISC_GUEST_KERNEL: 1901 dso_type = DSO_TYPE_GUEST_KERNEL; 1902 break; 1903 case PERF_RECORD_MISC_USER: 1904 case PERF_RECORD_MISC_GUEST_USER: 1905 dso_type = DSO_TYPE_USER; 1906 break; 1907 default: 1908 goto out; 1909 } 1910 1911 dso = machine__findnew_dso(machine, filename); 1912 if (dso != NULL) { 1913 char sbuild_id[SBUILD_ID_SIZE]; 1914 1915 dso__set_build_id(dso, &bev->build_id); 1916 1917 if (dso_type != DSO_TYPE_USER) { 1918 struct kmod_path m = { .name = NULL, }; 1919 1920 if (!kmod_path__parse_name(&m, filename) && m.kmod) 1921 dso__set_module_info(dso, &m, machine); 1922 else 1923 dso->kernel = dso_type; 1924 1925 free(m.name); 1926 } 1927 1928 build_id__sprintf(dso->build_id, sizeof(dso->build_id), 1929 sbuild_id); 1930 pr_debug("build id event received for %s: %s\n", 1931 dso->long_name, sbuild_id); 1932 dso__put(dso); 1933 } 1934 1935 err = 0; 1936 out: 1937 return err; 1938 } 1939 1940 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 1941 int input, u64 offset, u64 size) 1942 { 1943 struct perf_session *session = container_of(header, struct perf_session, header); 1944 struct { 1945 struct perf_event_header header; 1946 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 1947 char filename[0]; 1948 } old_bev; 1949 struct build_id_event bev; 1950 char filename[PATH_MAX]; 1951 u64 limit = offset + size; 1952 1953 while (offset < limit) { 1954 ssize_t len; 1955 1956 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 1957 return -1; 1958 1959 if (header->needs_swap) 1960 perf_event_header__bswap(&old_bev.header); 1961 1962 len = old_bev.header.size - sizeof(old_bev); 1963 if (readn(input, filename, len) != len) 1964 return -1; 1965 1966 bev.header = old_bev.header; 1967 1968 /* 1969 * As the pid is the missing value, we need to fill 1970 * it properly. The header.misc value give us nice hint. 1971 */ 1972 bev.pid = HOST_KERNEL_ID; 1973 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 1974 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 1975 bev.pid = DEFAULT_GUEST_KERNEL_ID; 1976 1977 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 1978 __event_process_build_id(&bev, filename, session); 1979 1980 offset += bev.header.size; 1981 } 1982 1983 return 0; 1984 } 1985 1986 static int perf_header__read_build_ids(struct perf_header *header, 1987 int input, u64 offset, u64 size) 1988 { 1989 struct perf_session *session = container_of(header, struct perf_session, header); 1990 struct build_id_event bev; 1991 char filename[PATH_MAX]; 1992 u64 limit = offset + size, orig_offset = offset; 1993 int err = -1; 1994 1995 while (offset < limit) { 1996 ssize_t len; 1997 1998 if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) 1999 goto out; 2000 2001 if (header->needs_swap) 2002 perf_event_header__bswap(&bev.header); 2003 2004 len = bev.header.size - sizeof(bev); 2005 if (readn(input, filename, len) != len) 2006 goto out; 2007 /* 2008 * The a1645ce1 changeset: 2009 * 2010 * "perf: 'perf kvm' tool for monitoring guest performance from host" 2011 * 2012 * Added a field to struct build_id_event that broke the file 2013 * format. 2014 * 2015 * Since the kernel build-id is the first entry, process the 2016 * table using the old format if the well known 2017 * '[kernel.kallsyms]' string for the kernel build-id has the 2018 * first 4 characters chopped off (where the pid_t sits). 2019 */ 2020 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 2021 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 2022 return -1; 2023 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 2024 } 2025 2026 __event_process_build_id(&bev, filename, session); 2027 2028 offset += bev.header.size; 2029 } 2030 err = 0; 2031 out: 2032 return err; 2033 } 2034 2035 /* Macro for features that simply need to read and store a string. */ 2036 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ 2037 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ 2038 {\ 2039 ff->ph->env.__feat_env = do_read_string(ff); \ 2040 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ 2041 } 2042 2043 FEAT_PROCESS_STR_FUN(hostname, hostname); 2044 FEAT_PROCESS_STR_FUN(osrelease, os_release); 2045 FEAT_PROCESS_STR_FUN(version, version); 2046 FEAT_PROCESS_STR_FUN(arch, arch); 2047 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); 2048 FEAT_PROCESS_STR_FUN(cpuid, cpuid); 2049 2050 static int process_tracing_data(struct feat_fd *ff, void *data) 2051 { 2052 ssize_t ret = trace_report(ff->fd, data, false); 2053 2054 return ret < 0 ? -1 : 0; 2055 } 2056 2057 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused) 2058 { 2059 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size)) 2060 pr_debug("Failed to read buildids, continuing...\n"); 2061 return 0; 2062 } 2063 2064 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused) 2065 { 2066 int ret; 2067 u32 nr_cpus_avail, nr_cpus_online; 2068 2069 ret = do_read_u32(ff, &nr_cpus_avail); 2070 if (ret) 2071 return ret; 2072 2073 ret = do_read_u32(ff, &nr_cpus_online); 2074 if (ret) 2075 return ret; 2076 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail; 2077 ff->ph->env.nr_cpus_online = (int)nr_cpus_online; 2078 return 0; 2079 } 2080 2081 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused) 2082 { 2083 u64 total_mem; 2084 int ret; 2085 2086 ret = do_read_u64(ff, &total_mem); 2087 if (ret) 2088 return -1; 2089 ff->ph->env.total_mem = (unsigned long long)total_mem; 2090 return 0; 2091 } 2092 2093 static struct evsel * 2094 perf_evlist__find_by_index(struct evlist *evlist, int idx) 2095 { 2096 struct evsel *evsel; 2097 2098 evlist__for_each_entry(evlist, evsel) { 2099 if (evsel->idx == idx) 2100 return evsel; 2101 } 2102 2103 return NULL; 2104 } 2105 2106 static void 2107 perf_evlist__set_event_name(struct evlist *evlist, 2108 struct evsel *event) 2109 { 2110 struct evsel *evsel; 2111 2112 if (!event->name) 2113 return; 2114 2115 evsel = perf_evlist__find_by_index(evlist, event->idx); 2116 if (!evsel) 2117 return; 2118 2119 if (evsel->name) 2120 return; 2121 2122 evsel->name = strdup(event->name); 2123 } 2124 2125 static int 2126 process_event_desc(struct feat_fd *ff, void *data __maybe_unused) 2127 { 2128 struct perf_session *session; 2129 struct evsel *evsel, *events = read_event_desc(ff); 2130 2131 if (!events) 2132 return 0; 2133 2134 session = container_of(ff->ph, struct perf_session, header); 2135 2136 if (session->data->is_pipe) { 2137 /* Save events for reading later by print_event_desc, 2138 * since they can't be read again in pipe mode. */ 2139 ff->events = events; 2140 } 2141 2142 for (evsel = events; evsel->core.attr.size; evsel++) 2143 perf_evlist__set_event_name(session->evlist, evsel); 2144 2145 if (!session->data->is_pipe) 2146 free_event_desc(events); 2147 2148 return 0; 2149 } 2150 2151 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) 2152 { 2153 char *str, *cmdline = NULL, **argv = NULL; 2154 u32 nr, i, len = 0; 2155 2156 if (do_read_u32(ff, &nr)) 2157 return -1; 2158 2159 ff->ph->env.nr_cmdline = nr; 2160 2161 cmdline = zalloc(ff->size + nr + 1); 2162 if (!cmdline) 2163 return -1; 2164 2165 argv = zalloc(sizeof(char *) * (nr + 1)); 2166 if (!argv) 2167 goto error; 2168 2169 for (i = 0; i < nr; i++) { 2170 str = do_read_string(ff); 2171 if (!str) 2172 goto error; 2173 2174 argv[i] = cmdline + len; 2175 memcpy(argv[i], str, strlen(str) + 1); 2176 len += strlen(str) + 1; 2177 free(str); 2178 } 2179 ff->ph->env.cmdline = cmdline; 2180 ff->ph->env.cmdline_argv = (const char **) argv; 2181 return 0; 2182 2183 error: 2184 free(argv); 2185 free(cmdline); 2186 return -1; 2187 } 2188 2189 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused) 2190 { 2191 u32 nr, i; 2192 char *str; 2193 struct strbuf sb; 2194 int cpu_nr = ff->ph->env.nr_cpus_avail; 2195 u64 size = 0; 2196 struct perf_header *ph = ff->ph; 2197 bool do_core_id_test = true; 2198 2199 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu)); 2200 if (!ph->env.cpu) 2201 return -1; 2202 2203 if (do_read_u32(ff, &nr)) 2204 goto free_cpu; 2205 2206 ph->env.nr_sibling_cores = nr; 2207 size += sizeof(u32); 2208 if (strbuf_init(&sb, 128) < 0) 2209 goto free_cpu; 2210 2211 for (i = 0; i < nr; i++) { 2212 str = do_read_string(ff); 2213 if (!str) 2214 goto error; 2215 2216 /* include a NULL character at the end */ 2217 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2218 goto error; 2219 size += string_size(str); 2220 free(str); 2221 } 2222 ph->env.sibling_cores = strbuf_detach(&sb, NULL); 2223 2224 if (do_read_u32(ff, &nr)) 2225 return -1; 2226 2227 ph->env.nr_sibling_threads = nr; 2228 size += sizeof(u32); 2229 2230 for (i = 0; i < nr; i++) { 2231 str = do_read_string(ff); 2232 if (!str) 2233 goto error; 2234 2235 /* include a NULL character at the end */ 2236 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2237 goto error; 2238 size += string_size(str); 2239 free(str); 2240 } 2241 ph->env.sibling_threads = strbuf_detach(&sb, NULL); 2242 2243 /* 2244 * The header may be from old perf, 2245 * which doesn't include core id and socket id information. 2246 */ 2247 if (ff->size <= size) { 2248 zfree(&ph->env.cpu); 2249 return 0; 2250 } 2251 2252 /* On s390 the socket_id number is not related to the numbers of cpus. 2253 * The socket_id number might be higher than the numbers of cpus. 2254 * This depends on the configuration. 2255 * AArch64 is the same. 2256 */ 2257 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4) 2258 || !strncmp(ph->env.arch, "aarch64", 7))) 2259 do_core_id_test = false; 2260 2261 for (i = 0; i < (u32)cpu_nr; i++) { 2262 if (do_read_u32(ff, &nr)) 2263 goto free_cpu; 2264 2265 ph->env.cpu[i].core_id = nr; 2266 size += sizeof(u32); 2267 2268 if (do_read_u32(ff, &nr)) 2269 goto free_cpu; 2270 2271 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) { 2272 pr_debug("socket_id number is too big." 2273 "You may need to upgrade the perf tool.\n"); 2274 goto free_cpu; 2275 } 2276 2277 ph->env.cpu[i].socket_id = nr; 2278 size += sizeof(u32); 2279 } 2280 2281 /* 2282 * The header may be from old perf, 2283 * which doesn't include die information. 2284 */ 2285 if (ff->size <= size) 2286 return 0; 2287 2288 if (do_read_u32(ff, &nr)) 2289 return -1; 2290 2291 ph->env.nr_sibling_dies = nr; 2292 size += sizeof(u32); 2293 2294 for (i = 0; i < nr; i++) { 2295 str = do_read_string(ff); 2296 if (!str) 2297 goto error; 2298 2299 /* include a NULL character at the end */ 2300 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2301 goto error; 2302 size += string_size(str); 2303 free(str); 2304 } 2305 ph->env.sibling_dies = strbuf_detach(&sb, NULL); 2306 2307 for (i = 0; i < (u32)cpu_nr; i++) { 2308 if (do_read_u32(ff, &nr)) 2309 goto free_cpu; 2310 2311 ph->env.cpu[i].die_id = nr; 2312 } 2313 2314 return 0; 2315 2316 error: 2317 strbuf_release(&sb); 2318 free_cpu: 2319 zfree(&ph->env.cpu); 2320 return -1; 2321 } 2322 2323 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused) 2324 { 2325 struct numa_node *nodes, *n; 2326 u32 nr, i; 2327 char *str; 2328 2329 /* nr nodes */ 2330 if (do_read_u32(ff, &nr)) 2331 return -1; 2332 2333 nodes = zalloc(sizeof(*nodes) * nr); 2334 if (!nodes) 2335 return -ENOMEM; 2336 2337 for (i = 0; i < nr; i++) { 2338 n = &nodes[i]; 2339 2340 /* node number */ 2341 if (do_read_u32(ff, &n->node)) 2342 goto error; 2343 2344 if (do_read_u64(ff, &n->mem_total)) 2345 goto error; 2346 2347 if (do_read_u64(ff, &n->mem_free)) 2348 goto error; 2349 2350 str = do_read_string(ff); 2351 if (!str) 2352 goto error; 2353 2354 n->map = perf_cpu_map__new(str); 2355 if (!n->map) 2356 goto error; 2357 2358 free(str); 2359 } 2360 ff->ph->env.nr_numa_nodes = nr; 2361 ff->ph->env.numa_nodes = nodes; 2362 return 0; 2363 2364 error: 2365 free(nodes); 2366 return -1; 2367 } 2368 2369 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused) 2370 { 2371 char *name; 2372 u32 pmu_num; 2373 u32 type; 2374 struct strbuf sb; 2375 2376 if (do_read_u32(ff, &pmu_num)) 2377 return -1; 2378 2379 if (!pmu_num) { 2380 pr_debug("pmu mappings not available\n"); 2381 return 0; 2382 } 2383 2384 ff->ph->env.nr_pmu_mappings = pmu_num; 2385 if (strbuf_init(&sb, 128) < 0) 2386 return -1; 2387 2388 while (pmu_num) { 2389 if (do_read_u32(ff, &type)) 2390 goto error; 2391 2392 name = do_read_string(ff); 2393 if (!name) 2394 goto error; 2395 2396 if (strbuf_addf(&sb, "%u:%s", type, name) < 0) 2397 goto error; 2398 /* include a NULL character at the end */ 2399 if (strbuf_add(&sb, "", 1) < 0) 2400 goto error; 2401 2402 if (!strcmp(name, "msr")) 2403 ff->ph->env.msr_pmu_type = type; 2404 2405 free(name); 2406 pmu_num--; 2407 } 2408 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL); 2409 return 0; 2410 2411 error: 2412 strbuf_release(&sb); 2413 return -1; 2414 } 2415 2416 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused) 2417 { 2418 size_t ret = -1; 2419 u32 i, nr, nr_groups; 2420 struct perf_session *session; 2421 struct evsel *evsel, *leader = NULL; 2422 struct group_desc { 2423 char *name; 2424 u32 leader_idx; 2425 u32 nr_members; 2426 } *desc; 2427 2428 if (do_read_u32(ff, &nr_groups)) 2429 return -1; 2430 2431 ff->ph->env.nr_groups = nr_groups; 2432 if (!nr_groups) { 2433 pr_debug("group desc not available\n"); 2434 return 0; 2435 } 2436 2437 desc = calloc(nr_groups, sizeof(*desc)); 2438 if (!desc) 2439 return -1; 2440 2441 for (i = 0; i < nr_groups; i++) { 2442 desc[i].name = do_read_string(ff); 2443 if (!desc[i].name) 2444 goto out_free; 2445 2446 if (do_read_u32(ff, &desc[i].leader_idx)) 2447 goto out_free; 2448 2449 if (do_read_u32(ff, &desc[i].nr_members)) 2450 goto out_free; 2451 } 2452 2453 /* 2454 * Rebuild group relationship based on the group_desc 2455 */ 2456 session = container_of(ff->ph, struct perf_session, header); 2457 session->evlist->nr_groups = nr_groups; 2458 2459 i = nr = 0; 2460 evlist__for_each_entry(session->evlist, evsel) { 2461 if (evsel->idx == (int) desc[i].leader_idx) { 2462 evsel->leader = evsel; 2463 /* {anon_group} is a dummy name */ 2464 if (strcmp(desc[i].name, "{anon_group}")) { 2465 evsel->group_name = desc[i].name; 2466 desc[i].name = NULL; 2467 } 2468 evsel->core.nr_members = desc[i].nr_members; 2469 2470 if (i >= nr_groups || nr > 0) { 2471 pr_debug("invalid group desc\n"); 2472 goto out_free; 2473 } 2474 2475 leader = evsel; 2476 nr = evsel->core.nr_members - 1; 2477 i++; 2478 } else if (nr) { 2479 /* This is a group member */ 2480 evsel->leader = leader; 2481 2482 nr--; 2483 } 2484 } 2485 2486 if (i != nr_groups || nr != 0) { 2487 pr_debug("invalid group desc\n"); 2488 goto out_free; 2489 } 2490 2491 ret = 0; 2492 out_free: 2493 for (i = 0; i < nr_groups; i++) 2494 zfree(&desc[i].name); 2495 free(desc); 2496 2497 return ret; 2498 } 2499 2500 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused) 2501 { 2502 struct perf_session *session; 2503 int err; 2504 2505 session = container_of(ff->ph, struct perf_session, header); 2506 2507 err = auxtrace_index__process(ff->fd, ff->size, session, 2508 ff->ph->needs_swap); 2509 if (err < 0) 2510 pr_err("Failed to process auxtrace index\n"); 2511 return err; 2512 } 2513 2514 static int process_cache(struct feat_fd *ff, void *data __maybe_unused) 2515 { 2516 struct cpu_cache_level *caches; 2517 u32 cnt, i, version; 2518 2519 if (do_read_u32(ff, &version)) 2520 return -1; 2521 2522 if (version != 1) 2523 return -1; 2524 2525 if (do_read_u32(ff, &cnt)) 2526 return -1; 2527 2528 caches = zalloc(sizeof(*caches) * cnt); 2529 if (!caches) 2530 return -1; 2531 2532 for (i = 0; i < cnt; i++) { 2533 struct cpu_cache_level c; 2534 2535 #define _R(v) \ 2536 if (do_read_u32(ff, &c.v))\ 2537 goto out_free_caches; \ 2538 2539 _R(level) 2540 _R(line_size) 2541 _R(sets) 2542 _R(ways) 2543 #undef _R 2544 2545 #define _R(v) \ 2546 c.v = do_read_string(ff); \ 2547 if (!c.v) \ 2548 goto out_free_caches; 2549 2550 _R(type) 2551 _R(size) 2552 _R(map) 2553 #undef _R 2554 2555 caches[i] = c; 2556 } 2557 2558 ff->ph->env.caches = caches; 2559 ff->ph->env.caches_cnt = cnt; 2560 return 0; 2561 out_free_caches: 2562 free(caches); 2563 return -1; 2564 } 2565 2566 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused) 2567 { 2568 struct perf_session *session; 2569 u64 first_sample_time, last_sample_time; 2570 int ret; 2571 2572 session = container_of(ff->ph, struct perf_session, header); 2573 2574 ret = do_read_u64(ff, &first_sample_time); 2575 if (ret) 2576 return -1; 2577 2578 ret = do_read_u64(ff, &last_sample_time); 2579 if (ret) 2580 return -1; 2581 2582 session->evlist->first_sample_time = first_sample_time; 2583 session->evlist->last_sample_time = last_sample_time; 2584 return 0; 2585 } 2586 2587 static int process_mem_topology(struct feat_fd *ff, 2588 void *data __maybe_unused) 2589 { 2590 struct memory_node *nodes; 2591 u64 version, i, nr, bsize; 2592 int ret = -1; 2593 2594 if (do_read_u64(ff, &version)) 2595 return -1; 2596 2597 if (version != 1) 2598 return -1; 2599 2600 if (do_read_u64(ff, &bsize)) 2601 return -1; 2602 2603 if (do_read_u64(ff, &nr)) 2604 return -1; 2605 2606 nodes = zalloc(sizeof(*nodes) * nr); 2607 if (!nodes) 2608 return -1; 2609 2610 for (i = 0; i < nr; i++) { 2611 struct memory_node n; 2612 2613 #define _R(v) \ 2614 if (do_read_u64(ff, &n.v)) \ 2615 goto out; \ 2616 2617 _R(node) 2618 _R(size) 2619 2620 #undef _R 2621 2622 if (do_read_bitmap(ff, &n.set, &n.size)) 2623 goto out; 2624 2625 nodes[i] = n; 2626 } 2627 2628 ff->ph->env.memory_bsize = bsize; 2629 ff->ph->env.memory_nodes = nodes; 2630 ff->ph->env.nr_memory_nodes = nr; 2631 ret = 0; 2632 2633 out: 2634 if (ret) 2635 free(nodes); 2636 return ret; 2637 } 2638 2639 static int process_clockid(struct feat_fd *ff, 2640 void *data __maybe_unused) 2641 { 2642 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns)) 2643 return -1; 2644 2645 return 0; 2646 } 2647 2648 static int process_dir_format(struct feat_fd *ff, 2649 void *_data __maybe_unused) 2650 { 2651 struct perf_session *session; 2652 struct perf_data *data; 2653 2654 session = container_of(ff->ph, struct perf_session, header); 2655 data = session->data; 2656 2657 if (WARN_ON(!perf_data__is_dir(data))) 2658 return -1; 2659 2660 return do_read_u64(ff, &data->dir.version); 2661 } 2662 2663 #ifdef HAVE_LIBBPF_SUPPORT 2664 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) 2665 { 2666 struct bpf_prog_info_linear *info_linear; 2667 struct bpf_prog_info_node *info_node; 2668 struct perf_env *env = &ff->ph->env; 2669 u32 count, i; 2670 int err = -1; 2671 2672 if (ff->ph->needs_swap) { 2673 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n"); 2674 return 0; 2675 } 2676 2677 if (do_read_u32(ff, &count)) 2678 return -1; 2679 2680 down_write(&env->bpf_progs.lock); 2681 2682 for (i = 0; i < count; ++i) { 2683 u32 info_len, data_len; 2684 2685 info_linear = NULL; 2686 info_node = NULL; 2687 if (do_read_u32(ff, &info_len)) 2688 goto out; 2689 if (do_read_u32(ff, &data_len)) 2690 goto out; 2691 2692 if (info_len > sizeof(struct bpf_prog_info)) { 2693 pr_warning("detected invalid bpf_prog_info\n"); 2694 goto out; 2695 } 2696 2697 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + 2698 data_len); 2699 if (!info_linear) 2700 goto out; 2701 info_linear->info_len = sizeof(struct bpf_prog_info); 2702 info_linear->data_len = data_len; 2703 if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) 2704 goto out; 2705 if (__do_read(ff, &info_linear->info, info_len)) 2706 goto out; 2707 if (info_len < sizeof(struct bpf_prog_info)) 2708 memset(((void *)(&info_linear->info)) + info_len, 0, 2709 sizeof(struct bpf_prog_info) - info_len); 2710 2711 if (__do_read(ff, info_linear->data, data_len)) 2712 goto out; 2713 2714 info_node = malloc(sizeof(struct bpf_prog_info_node)); 2715 if (!info_node) 2716 goto out; 2717 2718 /* after reading from file, translate offset to address */ 2719 bpf_program__bpil_offs_to_addr(info_linear); 2720 info_node->info_linear = info_linear; 2721 perf_env__insert_bpf_prog_info(env, info_node); 2722 } 2723 2724 up_write(&env->bpf_progs.lock); 2725 return 0; 2726 out: 2727 free(info_linear); 2728 free(info_node); 2729 up_write(&env->bpf_progs.lock); 2730 return err; 2731 } 2732 #else // HAVE_LIBBPF_SUPPORT 2733 static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 2734 { 2735 return 0; 2736 } 2737 #endif // HAVE_LIBBPF_SUPPORT 2738 2739 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) 2740 { 2741 struct perf_env *env = &ff->ph->env; 2742 struct btf_node *node = NULL; 2743 u32 count, i; 2744 int err = -1; 2745 2746 if (ff->ph->needs_swap) { 2747 pr_warning("interpreting btf from systems with endianity is not yet supported\n"); 2748 return 0; 2749 } 2750 2751 if (do_read_u32(ff, &count)) 2752 return -1; 2753 2754 down_write(&env->bpf_progs.lock); 2755 2756 for (i = 0; i < count; ++i) { 2757 u32 id, data_size; 2758 2759 if (do_read_u32(ff, &id)) 2760 goto out; 2761 if (do_read_u32(ff, &data_size)) 2762 goto out; 2763 2764 node = malloc(sizeof(struct btf_node) + data_size); 2765 if (!node) 2766 goto out; 2767 2768 node->id = id; 2769 node->data_size = data_size; 2770 2771 if (__do_read(ff, node->data, data_size)) 2772 goto out; 2773 2774 perf_env__insert_btf(env, node); 2775 node = NULL; 2776 } 2777 2778 err = 0; 2779 out: 2780 up_write(&env->bpf_progs.lock); 2781 free(node); 2782 return err; 2783 } 2784 2785 static int process_compressed(struct feat_fd *ff, 2786 void *data __maybe_unused) 2787 { 2788 if (do_read_u32(ff, &(ff->ph->env.comp_ver))) 2789 return -1; 2790 2791 if (do_read_u32(ff, &(ff->ph->env.comp_type))) 2792 return -1; 2793 2794 if (do_read_u32(ff, &(ff->ph->env.comp_level))) 2795 return -1; 2796 2797 if (do_read_u32(ff, &(ff->ph->env.comp_ratio))) 2798 return -1; 2799 2800 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len))) 2801 return -1; 2802 2803 return 0; 2804 } 2805 2806 struct feature_ops { 2807 int (*write)(struct feat_fd *ff, struct evlist *evlist); 2808 void (*print)(struct feat_fd *ff, FILE *fp); 2809 int (*process)(struct feat_fd *ff, void *data); 2810 const char *name; 2811 bool full_only; 2812 bool synthesize; 2813 }; 2814 2815 #define FEAT_OPR(n, func, __full_only) \ 2816 [HEADER_##n] = { \ 2817 .name = __stringify(n), \ 2818 .write = write_##func, \ 2819 .print = print_##func, \ 2820 .full_only = __full_only, \ 2821 .process = process_##func, \ 2822 .synthesize = true \ 2823 } 2824 2825 #define FEAT_OPN(n, func, __full_only) \ 2826 [HEADER_##n] = { \ 2827 .name = __stringify(n), \ 2828 .write = write_##func, \ 2829 .print = print_##func, \ 2830 .full_only = __full_only, \ 2831 .process = process_##func \ 2832 } 2833 2834 /* feature_ops not implemented: */ 2835 #define print_tracing_data NULL 2836 #define print_build_id NULL 2837 2838 #define process_branch_stack NULL 2839 #define process_stat NULL 2840 2841 2842 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { 2843 FEAT_OPN(TRACING_DATA, tracing_data, false), 2844 FEAT_OPN(BUILD_ID, build_id, false), 2845 FEAT_OPR(HOSTNAME, hostname, false), 2846 FEAT_OPR(OSRELEASE, osrelease, false), 2847 FEAT_OPR(VERSION, version, false), 2848 FEAT_OPR(ARCH, arch, false), 2849 FEAT_OPR(NRCPUS, nrcpus, false), 2850 FEAT_OPR(CPUDESC, cpudesc, false), 2851 FEAT_OPR(CPUID, cpuid, false), 2852 FEAT_OPR(TOTAL_MEM, total_mem, false), 2853 FEAT_OPR(EVENT_DESC, event_desc, false), 2854 FEAT_OPR(CMDLINE, cmdline, false), 2855 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true), 2856 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true), 2857 FEAT_OPN(BRANCH_STACK, branch_stack, false), 2858 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false), 2859 FEAT_OPR(GROUP_DESC, group_desc, false), 2860 FEAT_OPN(AUXTRACE, auxtrace, false), 2861 FEAT_OPN(STAT, stat, false), 2862 FEAT_OPN(CACHE, cache, true), 2863 FEAT_OPR(SAMPLE_TIME, sample_time, false), 2864 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true), 2865 FEAT_OPR(CLOCKID, clockid, false), 2866 FEAT_OPN(DIR_FORMAT, dir_format, false), 2867 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false), 2868 FEAT_OPR(BPF_BTF, bpf_btf, false), 2869 FEAT_OPR(COMPRESSED, compressed, false), 2870 }; 2871 2872 struct header_print_data { 2873 FILE *fp; 2874 bool full; /* extended list of headers */ 2875 }; 2876 2877 static int perf_file_section__fprintf_info(struct perf_file_section *section, 2878 struct perf_header *ph, 2879 int feat, int fd, void *data) 2880 { 2881 struct header_print_data *hd = data; 2882 struct feat_fd ff; 2883 2884 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 2885 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 2886 "%d, continuing...\n", section->offset, feat); 2887 return 0; 2888 } 2889 if (feat >= HEADER_LAST_FEATURE) { 2890 pr_warning("unknown feature %d\n", feat); 2891 return 0; 2892 } 2893 if (!feat_ops[feat].print) 2894 return 0; 2895 2896 ff = (struct feat_fd) { 2897 .fd = fd, 2898 .ph = ph, 2899 }; 2900 2901 if (!feat_ops[feat].full_only || hd->full) 2902 feat_ops[feat].print(&ff, hd->fp); 2903 else 2904 fprintf(hd->fp, "# %s info available, use -I to display\n", 2905 feat_ops[feat].name); 2906 2907 return 0; 2908 } 2909 2910 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 2911 { 2912 struct header_print_data hd; 2913 struct perf_header *header = &session->header; 2914 int fd = perf_data__fd(session->data); 2915 struct stat st; 2916 time_t stctime; 2917 int ret, bit; 2918 2919 hd.fp = fp; 2920 hd.full = full; 2921 2922 ret = fstat(fd, &st); 2923 if (ret == -1) 2924 return -1; 2925 2926 stctime = st.st_ctime; 2927 fprintf(fp, "# captured on : %s", ctime(&stctime)); 2928 2929 fprintf(fp, "# header version : %u\n", header->version); 2930 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); 2931 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); 2932 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); 2933 2934 perf_header__process_sections(header, fd, &hd, 2935 perf_file_section__fprintf_info); 2936 2937 if (session->data->is_pipe) 2938 return 0; 2939 2940 fprintf(fp, "# missing features: "); 2941 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) { 2942 if (bit) 2943 fprintf(fp, "%s ", feat_ops[bit].name); 2944 } 2945 2946 fprintf(fp, "\n"); 2947 return 0; 2948 } 2949 2950 static int do_write_feat(struct feat_fd *ff, int type, 2951 struct perf_file_section **p, 2952 struct evlist *evlist) 2953 { 2954 int err; 2955 int ret = 0; 2956 2957 if (perf_header__has_feat(ff->ph, type)) { 2958 if (!feat_ops[type].write) 2959 return -1; 2960 2961 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 2962 return -1; 2963 2964 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); 2965 2966 err = feat_ops[type].write(ff, evlist); 2967 if (err < 0) { 2968 pr_debug("failed to write feature %s\n", feat_ops[type].name); 2969 2970 /* undo anything written */ 2971 lseek(ff->fd, (*p)->offset, SEEK_SET); 2972 2973 return -1; 2974 } 2975 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; 2976 (*p)++; 2977 } 2978 return ret; 2979 } 2980 2981 static int perf_header__adds_write(struct perf_header *header, 2982 struct evlist *evlist, int fd) 2983 { 2984 int nr_sections; 2985 struct feat_fd ff; 2986 struct perf_file_section *feat_sec, *p; 2987 int sec_size; 2988 u64 sec_start; 2989 int feat; 2990 int err; 2991 2992 ff = (struct feat_fd){ 2993 .fd = fd, 2994 .ph = header, 2995 }; 2996 2997 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 2998 if (!nr_sections) 2999 return 0; 3000 3001 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); 3002 if (feat_sec == NULL) 3003 return -ENOMEM; 3004 3005 sec_size = sizeof(*feat_sec) * nr_sections; 3006 3007 sec_start = header->feat_offset; 3008 lseek(fd, sec_start + sec_size, SEEK_SET); 3009 3010 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 3011 if (do_write_feat(&ff, feat, &p, evlist)) 3012 perf_header__clear_feat(header, feat); 3013 } 3014 3015 lseek(fd, sec_start, SEEK_SET); 3016 /* 3017 * may write more than needed due to dropped feature, but 3018 * this is okay, reader will skip the missing entries 3019 */ 3020 err = do_write(&ff, feat_sec, sec_size); 3021 if (err < 0) 3022 pr_debug("failed to write feature section\n"); 3023 free(feat_sec); 3024 return err; 3025 } 3026 3027 int perf_header__write_pipe(int fd) 3028 { 3029 struct perf_pipe_file_header f_header; 3030 struct feat_fd ff; 3031 int err; 3032 3033 ff = (struct feat_fd){ .fd = fd }; 3034 3035 f_header = (struct perf_pipe_file_header){ 3036 .magic = PERF_MAGIC, 3037 .size = sizeof(f_header), 3038 }; 3039 3040 err = do_write(&ff, &f_header, sizeof(f_header)); 3041 if (err < 0) { 3042 pr_debug("failed to write perf pipe header\n"); 3043 return err; 3044 } 3045 3046 return 0; 3047 } 3048 3049 int perf_session__write_header(struct perf_session *session, 3050 struct evlist *evlist, 3051 int fd, bool at_exit) 3052 { 3053 struct perf_file_header f_header; 3054 struct perf_file_attr f_attr; 3055 struct perf_header *header = &session->header; 3056 struct evsel *evsel; 3057 struct feat_fd ff; 3058 u64 attr_offset; 3059 int err; 3060 3061 ff = (struct feat_fd){ .fd = fd}; 3062 lseek(fd, sizeof(f_header), SEEK_SET); 3063 3064 evlist__for_each_entry(session->evlist, evsel) { 3065 evsel->id_offset = lseek(fd, 0, SEEK_CUR); 3066 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64)); 3067 if (err < 0) { 3068 pr_debug("failed to write perf header\n"); 3069 return err; 3070 } 3071 } 3072 3073 attr_offset = lseek(ff.fd, 0, SEEK_CUR); 3074 3075 evlist__for_each_entry(evlist, evsel) { 3076 f_attr = (struct perf_file_attr){ 3077 .attr = evsel->core.attr, 3078 .ids = { 3079 .offset = evsel->id_offset, 3080 .size = evsel->ids * sizeof(u64), 3081 } 3082 }; 3083 err = do_write(&ff, &f_attr, sizeof(f_attr)); 3084 if (err < 0) { 3085 pr_debug("failed to write perf header attribute\n"); 3086 return err; 3087 } 3088 } 3089 3090 if (!header->data_offset) 3091 header->data_offset = lseek(fd, 0, SEEK_CUR); 3092 header->feat_offset = header->data_offset + header->data_size; 3093 3094 if (at_exit) { 3095 err = perf_header__adds_write(header, evlist, fd); 3096 if (err < 0) 3097 return err; 3098 } 3099 3100 f_header = (struct perf_file_header){ 3101 .magic = PERF_MAGIC, 3102 .size = sizeof(f_header), 3103 .attr_size = sizeof(f_attr), 3104 .attrs = { 3105 .offset = attr_offset, 3106 .size = evlist->core.nr_entries * sizeof(f_attr), 3107 }, 3108 .data = { 3109 .offset = header->data_offset, 3110 .size = header->data_size, 3111 }, 3112 /* event_types is ignored, store zeros */ 3113 }; 3114 3115 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 3116 3117 lseek(fd, 0, SEEK_SET); 3118 err = do_write(&ff, &f_header, sizeof(f_header)); 3119 if (err < 0) { 3120 pr_debug("failed to write perf header\n"); 3121 return err; 3122 } 3123 lseek(fd, header->data_offset + header->data_size, SEEK_SET); 3124 3125 return 0; 3126 } 3127 3128 static int perf_header__getbuffer64(struct perf_header *header, 3129 int fd, void *buf, size_t size) 3130 { 3131 if (readn(fd, buf, size) <= 0) 3132 return -1; 3133 3134 if (header->needs_swap) 3135 mem_bswap_64(buf, size); 3136 3137 return 0; 3138 } 3139 3140 int perf_header__process_sections(struct perf_header *header, int fd, 3141 void *data, 3142 int (*process)(struct perf_file_section *section, 3143 struct perf_header *ph, 3144 int feat, int fd, void *data)) 3145 { 3146 struct perf_file_section *feat_sec, *sec; 3147 int nr_sections; 3148 int sec_size; 3149 int feat; 3150 int err; 3151 3152 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 3153 if (!nr_sections) 3154 return 0; 3155 3156 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); 3157 if (!feat_sec) 3158 return -1; 3159 3160 sec_size = sizeof(*feat_sec) * nr_sections; 3161 3162 lseek(fd, header->feat_offset, SEEK_SET); 3163 3164 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 3165 if (err < 0) 3166 goto out_free; 3167 3168 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { 3169 err = process(sec++, header, feat, fd, data); 3170 if (err < 0) 3171 goto out_free; 3172 } 3173 err = 0; 3174 out_free: 3175 free(feat_sec); 3176 return err; 3177 } 3178 3179 static const int attr_file_abi_sizes[] = { 3180 [0] = PERF_ATTR_SIZE_VER0, 3181 [1] = PERF_ATTR_SIZE_VER1, 3182 [2] = PERF_ATTR_SIZE_VER2, 3183 [3] = PERF_ATTR_SIZE_VER3, 3184 [4] = PERF_ATTR_SIZE_VER4, 3185 0, 3186 }; 3187 3188 /* 3189 * In the legacy file format, the magic number is not used to encode endianness. 3190 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 3191 * on ABI revisions, we need to try all combinations for all endianness to 3192 * detect the endianness. 3193 */ 3194 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 3195 { 3196 uint64_t ref_size, attr_size; 3197 int i; 3198 3199 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 3200 ref_size = attr_file_abi_sizes[i] 3201 + sizeof(struct perf_file_section); 3202 if (hdr_sz != ref_size) { 3203 attr_size = bswap_64(hdr_sz); 3204 if (attr_size != ref_size) 3205 continue; 3206 3207 ph->needs_swap = true; 3208 } 3209 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 3210 i, 3211 ph->needs_swap); 3212 return 0; 3213 } 3214 /* could not determine endianness */ 3215 return -1; 3216 } 3217 3218 #define PERF_PIPE_HDR_VER0 16 3219 3220 static const size_t attr_pipe_abi_sizes[] = { 3221 [0] = PERF_PIPE_HDR_VER0, 3222 0, 3223 }; 3224 3225 /* 3226 * In the legacy pipe format, there is an implicit assumption that endiannesss 3227 * between host recording the samples, and host parsing the samples is the 3228 * same. This is not always the case given that the pipe output may always be 3229 * redirected into a file and analyzed on a different machine with possibly a 3230 * different endianness and perf_event ABI revsions in the perf tool itself. 3231 */ 3232 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 3233 { 3234 u64 attr_size; 3235 int i; 3236 3237 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 3238 if (hdr_sz != attr_pipe_abi_sizes[i]) { 3239 attr_size = bswap_64(hdr_sz); 3240 if (attr_size != hdr_sz) 3241 continue; 3242 3243 ph->needs_swap = true; 3244 } 3245 pr_debug("Pipe ABI%d perf.data file detected\n", i); 3246 return 0; 3247 } 3248 return -1; 3249 } 3250 3251 bool is_perf_magic(u64 magic) 3252 { 3253 if (!memcmp(&magic, __perf_magic1, sizeof(magic)) 3254 || magic == __perf_magic2 3255 || magic == __perf_magic2_sw) 3256 return true; 3257 3258 return false; 3259 } 3260 3261 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 3262 bool is_pipe, struct perf_header *ph) 3263 { 3264 int ret; 3265 3266 /* check for legacy format */ 3267 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 3268 if (ret == 0) { 3269 ph->version = PERF_HEADER_VERSION_1; 3270 pr_debug("legacy perf.data format\n"); 3271 if (is_pipe) 3272 return try_all_pipe_abis(hdr_sz, ph); 3273 3274 return try_all_file_abis(hdr_sz, ph); 3275 } 3276 /* 3277 * the new magic number serves two purposes: 3278 * - unique number to identify actual perf.data files 3279 * - encode endianness of file 3280 */ 3281 ph->version = PERF_HEADER_VERSION_2; 3282 3283 /* check magic number with one endianness */ 3284 if (magic == __perf_magic2) 3285 return 0; 3286 3287 /* check magic number with opposite endianness */ 3288 if (magic != __perf_magic2_sw) 3289 return -1; 3290 3291 ph->needs_swap = true; 3292 3293 return 0; 3294 } 3295 3296 int perf_file_header__read(struct perf_file_header *header, 3297 struct perf_header *ph, int fd) 3298 { 3299 ssize_t ret; 3300 3301 lseek(fd, 0, SEEK_SET); 3302 3303 ret = readn(fd, header, sizeof(*header)); 3304 if (ret <= 0) 3305 return -1; 3306 3307 if (check_magic_endian(header->magic, 3308 header->attr_size, false, ph) < 0) { 3309 pr_debug("magic/endian check failed\n"); 3310 return -1; 3311 } 3312 3313 if (ph->needs_swap) { 3314 mem_bswap_64(header, offsetof(struct perf_file_header, 3315 adds_features)); 3316 } 3317 3318 if (header->size != sizeof(*header)) { 3319 /* Support the previous format */ 3320 if (header->size == offsetof(typeof(*header), adds_features)) 3321 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 3322 else 3323 return -1; 3324 } else if (ph->needs_swap) { 3325 /* 3326 * feature bitmap is declared as an array of unsigned longs -- 3327 * not good since its size can differ between the host that 3328 * generated the data file and the host analyzing the file. 3329 * 3330 * We need to handle endianness, but we don't know the size of 3331 * the unsigned long where the file was generated. Take a best 3332 * guess at determining it: try 64-bit swap first (ie., file 3333 * created on a 64-bit host), and check if the hostname feature 3334 * bit is set (this feature bit is forced on as of fbe96f2). 3335 * If the bit is not, undo the 64-bit swap and try a 32-bit 3336 * swap. If the hostname bit is still not set (e.g., older data 3337 * file), punt and fallback to the original behavior -- 3338 * clearing all feature bits and setting buildid. 3339 */ 3340 mem_bswap_64(&header->adds_features, 3341 BITS_TO_U64(HEADER_FEAT_BITS)); 3342 3343 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 3344 /* unswap as u64 */ 3345 mem_bswap_64(&header->adds_features, 3346 BITS_TO_U64(HEADER_FEAT_BITS)); 3347 3348 /* unswap as u32 */ 3349 mem_bswap_32(&header->adds_features, 3350 BITS_TO_U32(HEADER_FEAT_BITS)); 3351 } 3352 3353 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 3354 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 3355 set_bit(HEADER_BUILD_ID, header->adds_features); 3356 } 3357 } 3358 3359 memcpy(&ph->adds_features, &header->adds_features, 3360 sizeof(ph->adds_features)); 3361 3362 ph->data_offset = header->data.offset; 3363 ph->data_size = header->data.size; 3364 ph->feat_offset = header->data.offset + header->data.size; 3365 return 0; 3366 } 3367 3368 static int perf_file_section__process(struct perf_file_section *section, 3369 struct perf_header *ph, 3370 int feat, int fd, void *data) 3371 { 3372 struct feat_fd fdd = { 3373 .fd = fd, 3374 .ph = ph, 3375 .size = section->size, 3376 .offset = section->offset, 3377 }; 3378 3379 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 3380 pr_debug("Failed to lseek to %" PRIu64 " offset for feature " 3381 "%d, continuing...\n", section->offset, feat); 3382 return 0; 3383 } 3384 3385 if (feat >= HEADER_LAST_FEATURE) { 3386 pr_debug("unknown feature %d, continuing...\n", feat); 3387 return 0; 3388 } 3389 3390 if (!feat_ops[feat].process) 3391 return 0; 3392 3393 return feat_ops[feat].process(&fdd, data); 3394 } 3395 3396 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 3397 struct perf_header *ph, int fd, 3398 bool repipe) 3399 { 3400 struct feat_fd ff = { 3401 .fd = STDOUT_FILENO, 3402 .ph = ph, 3403 }; 3404 ssize_t ret; 3405 3406 ret = readn(fd, header, sizeof(*header)); 3407 if (ret <= 0) 3408 return -1; 3409 3410 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 3411 pr_debug("endian/magic failed\n"); 3412 return -1; 3413 } 3414 3415 if (ph->needs_swap) 3416 header->size = bswap_64(header->size); 3417 3418 if (repipe && do_write(&ff, header, sizeof(*header)) < 0) 3419 return -1; 3420 3421 return 0; 3422 } 3423 3424 static int perf_header__read_pipe(struct perf_session *session) 3425 { 3426 struct perf_header *header = &session->header; 3427 struct perf_pipe_file_header f_header; 3428 3429 if (perf_file_header__read_pipe(&f_header, header, 3430 perf_data__fd(session->data), 3431 session->repipe) < 0) { 3432 pr_debug("incompatible file format\n"); 3433 return -EINVAL; 3434 } 3435 3436 return 0; 3437 } 3438 3439 static int read_attr(int fd, struct perf_header *ph, 3440 struct perf_file_attr *f_attr) 3441 { 3442 struct perf_event_attr *attr = &f_attr->attr; 3443 size_t sz, left; 3444 size_t our_sz = sizeof(f_attr->attr); 3445 ssize_t ret; 3446 3447 memset(f_attr, 0, sizeof(*f_attr)); 3448 3449 /* read minimal guaranteed structure */ 3450 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 3451 if (ret <= 0) { 3452 pr_debug("cannot read %d bytes of header attr\n", 3453 PERF_ATTR_SIZE_VER0); 3454 return -1; 3455 } 3456 3457 /* on file perf_event_attr size */ 3458 sz = attr->size; 3459 3460 if (ph->needs_swap) 3461 sz = bswap_32(sz); 3462 3463 if (sz == 0) { 3464 /* assume ABI0 */ 3465 sz = PERF_ATTR_SIZE_VER0; 3466 } else if (sz > our_sz) { 3467 pr_debug("file uses a more recent and unsupported ABI" 3468 " (%zu bytes extra)\n", sz - our_sz); 3469 return -1; 3470 } 3471 /* what we have not yet read and that we know about */ 3472 left = sz - PERF_ATTR_SIZE_VER0; 3473 if (left) { 3474 void *ptr = attr; 3475 ptr += PERF_ATTR_SIZE_VER0; 3476 3477 ret = readn(fd, ptr, left); 3478 } 3479 /* read perf_file_section, ids are read in caller */ 3480 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 3481 3482 return ret <= 0 ? -1 : 0; 3483 } 3484 3485 static int perf_evsel__prepare_tracepoint_event(struct evsel *evsel, 3486 struct tep_handle *pevent) 3487 { 3488 struct tep_event *event; 3489 char bf[128]; 3490 3491 /* already prepared */ 3492 if (evsel->tp_format) 3493 return 0; 3494 3495 if (pevent == NULL) { 3496 pr_debug("broken or missing trace data\n"); 3497 return -1; 3498 } 3499 3500 event = tep_find_event(pevent, evsel->core.attr.config); 3501 if (event == NULL) { 3502 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); 3503 return -1; 3504 } 3505 3506 if (!evsel->name) { 3507 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 3508 evsel->name = strdup(bf); 3509 if (evsel->name == NULL) 3510 return -1; 3511 } 3512 3513 evsel->tp_format = event; 3514 return 0; 3515 } 3516 3517 static int perf_evlist__prepare_tracepoint_events(struct evlist *evlist, 3518 struct tep_handle *pevent) 3519 { 3520 struct evsel *pos; 3521 3522 evlist__for_each_entry(evlist, pos) { 3523 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && 3524 perf_evsel__prepare_tracepoint_event(pos, pevent)) 3525 return -1; 3526 } 3527 3528 return 0; 3529 } 3530 3531 int perf_session__read_header(struct perf_session *session) 3532 { 3533 struct perf_data *data = session->data; 3534 struct perf_header *header = &session->header; 3535 struct perf_file_header f_header; 3536 struct perf_file_attr f_attr; 3537 u64 f_id; 3538 int nr_attrs, nr_ids, i, j; 3539 int fd = perf_data__fd(data); 3540 3541 session->evlist = evlist__new(); 3542 if (session->evlist == NULL) 3543 return -ENOMEM; 3544 3545 session->evlist->env = &header->env; 3546 session->machines.host.env = &header->env; 3547 if (perf_data__is_pipe(data)) 3548 return perf_header__read_pipe(session); 3549 3550 if (perf_file_header__read(&f_header, header, fd) < 0) 3551 return -EINVAL; 3552 3553 /* 3554 * Sanity check that perf.data was written cleanly; data size is 3555 * initialized to 0 and updated only if the on_exit function is run. 3556 * If data size is still 0 then the file contains only partial 3557 * information. Just warn user and process it as much as it can. 3558 */ 3559 if (f_header.data.size == 0) { 3560 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" 3561 "Was the 'perf record' command properly terminated?\n", 3562 data->file.path); 3563 } 3564 3565 if (f_header.attr_size == 0) { 3566 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n" 3567 "Was the 'perf record' command properly terminated?\n", 3568 data->file.path); 3569 return -EINVAL; 3570 } 3571 3572 nr_attrs = f_header.attrs.size / f_header.attr_size; 3573 lseek(fd, f_header.attrs.offset, SEEK_SET); 3574 3575 for (i = 0; i < nr_attrs; i++) { 3576 struct evsel *evsel; 3577 off_t tmp; 3578 3579 if (read_attr(fd, header, &f_attr) < 0) 3580 goto out_errno; 3581 3582 if (header->needs_swap) { 3583 f_attr.ids.size = bswap_64(f_attr.ids.size); 3584 f_attr.ids.offset = bswap_64(f_attr.ids.offset); 3585 perf_event__attr_swap(&f_attr.attr); 3586 } 3587 3588 tmp = lseek(fd, 0, SEEK_CUR); 3589 evsel = evsel__new(&f_attr.attr); 3590 3591 if (evsel == NULL) 3592 goto out_delete_evlist; 3593 3594 evsel->needs_swap = header->needs_swap; 3595 /* 3596 * Do it before so that if perf_evsel__alloc_id fails, this 3597 * entry gets purged too at evlist__delete(). 3598 */ 3599 evlist__add(session->evlist, evsel); 3600 3601 nr_ids = f_attr.ids.size / sizeof(u64); 3602 /* 3603 * We don't have the cpu and thread maps on the header, so 3604 * for allocating the perf_sample_id table we fake 1 cpu and 3605 * hattr->ids threads. 3606 */ 3607 if (perf_evsel__alloc_id(evsel, 1, nr_ids)) 3608 goto out_delete_evlist; 3609 3610 lseek(fd, f_attr.ids.offset, SEEK_SET); 3611 3612 for (j = 0; j < nr_ids; j++) { 3613 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 3614 goto out_errno; 3615 3616 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); 3617 } 3618 3619 lseek(fd, tmp, SEEK_SET); 3620 } 3621 3622 perf_header__process_sections(header, fd, &session->tevent, 3623 perf_file_section__process); 3624 3625 if (perf_evlist__prepare_tracepoint_events(session->evlist, 3626 session->tevent.pevent)) 3627 goto out_delete_evlist; 3628 3629 return 0; 3630 out_errno: 3631 return -errno; 3632 3633 out_delete_evlist: 3634 evlist__delete(session->evlist); 3635 session->evlist = NULL; 3636 return -ENOMEM; 3637 } 3638 3639 int perf_event__synthesize_attr(struct perf_tool *tool, 3640 struct perf_event_attr *attr, u32 ids, u64 *id, 3641 perf_event__handler_t process) 3642 { 3643 union perf_event *ev; 3644 size_t size; 3645 int err; 3646 3647 size = sizeof(struct perf_event_attr); 3648 size = PERF_ALIGN(size, sizeof(u64)); 3649 size += sizeof(struct perf_event_header); 3650 size += ids * sizeof(u64); 3651 3652 ev = zalloc(size); 3653 3654 if (ev == NULL) 3655 return -ENOMEM; 3656 3657 ev->attr.attr = *attr; 3658 memcpy(ev->attr.id, id, ids * sizeof(u64)); 3659 3660 ev->attr.header.type = PERF_RECORD_HEADER_ATTR; 3661 ev->attr.header.size = (u16)size; 3662 3663 if (ev->attr.header.size == size) 3664 err = process(tool, ev, NULL, NULL); 3665 else 3666 err = -E2BIG; 3667 3668 free(ev); 3669 3670 return err; 3671 } 3672 3673 int perf_event__synthesize_features(struct perf_tool *tool, 3674 struct perf_session *session, 3675 struct evlist *evlist, 3676 perf_event__handler_t process) 3677 { 3678 struct perf_header *header = &session->header; 3679 struct feat_fd ff; 3680 struct feature_event *fe; 3681 size_t sz, sz_hdr; 3682 int feat, ret; 3683 3684 sz_hdr = sizeof(fe->header); 3685 sz = sizeof(union perf_event); 3686 /* get a nice alignment */ 3687 sz = PERF_ALIGN(sz, page_size); 3688 3689 memset(&ff, 0, sizeof(ff)); 3690 3691 ff.buf = malloc(sz); 3692 if (!ff.buf) 3693 return -ENOMEM; 3694 3695 ff.size = sz - sz_hdr; 3696 ff.ph = &session->header; 3697 3698 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 3699 if (!feat_ops[feat].synthesize) { 3700 pr_debug("No record header feature for header :%d\n", feat); 3701 continue; 3702 } 3703 3704 ff.offset = sizeof(*fe); 3705 3706 ret = feat_ops[feat].write(&ff, evlist); 3707 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) { 3708 pr_debug("Error writing feature\n"); 3709 continue; 3710 } 3711 /* ff.buf may have changed due to realloc in do_write() */ 3712 fe = ff.buf; 3713 memset(fe, 0, sizeof(*fe)); 3714 3715 fe->feat_id = feat; 3716 fe->header.type = PERF_RECORD_HEADER_FEATURE; 3717 fe->header.size = ff.offset; 3718 3719 ret = process(tool, ff.buf, NULL, NULL); 3720 if (ret) { 3721 free(ff.buf); 3722 return ret; 3723 } 3724 } 3725 3726 /* Send HEADER_LAST_FEATURE mark. */ 3727 fe = ff.buf; 3728 fe->feat_id = HEADER_LAST_FEATURE; 3729 fe->header.type = PERF_RECORD_HEADER_FEATURE; 3730 fe->header.size = sizeof(*fe); 3731 3732 ret = process(tool, ff.buf, NULL, NULL); 3733 3734 free(ff.buf); 3735 return ret; 3736 } 3737 3738 int perf_event__process_feature(struct perf_session *session, 3739 union perf_event *event) 3740 { 3741 struct perf_tool *tool = session->tool; 3742 struct feat_fd ff = { .fd = 0 }; 3743 struct feature_event *fe = (struct feature_event *)event; 3744 int type = fe->header.type; 3745 u64 feat = fe->feat_id; 3746 3747 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) { 3748 pr_warning("invalid record type %d in pipe-mode\n", type); 3749 return 0; 3750 } 3751 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) { 3752 pr_warning("invalid record type %d in pipe-mode\n", type); 3753 return -1; 3754 } 3755 3756 if (!feat_ops[feat].process) 3757 return 0; 3758 3759 ff.buf = (void *)fe->data; 3760 ff.size = event->header.size - sizeof(*fe); 3761 ff.ph = &session->header; 3762 3763 if (feat_ops[feat].process(&ff, NULL)) 3764 return -1; 3765 3766 if (!feat_ops[feat].print || !tool->show_feat_hdr) 3767 return 0; 3768 3769 if (!feat_ops[feat].full_only || 3770 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { 3771 feat_ops[feat].print(&ff, stdout); 3772 } else { 3773 fprintf(stdout, "# %s info available, use -I to display\n", 3774 feat_ops[feat].name); 3775 } 3776 3777 return 0; 3778 } 3779 3780 static struct event_update_event * 3781 event_update_event__new(size_t size, u64 type, u64 id) 3782 { 3783 struct event_update_event *ev; 3784 3785 size += sizeof(*ev); 3786 size = PERF_ALIGN(size, sizeof(u64)); 3787 3788 ev = zalloc(size); 3789 if (ev) { 3790 ev->header.type = PERF_RECORD_EVENT_UPDATE; 3791 ev->header.size = (u16)size; 3792 ev->type = type; 3793 ev->id = id; 3794 } 3795 return ev; 3796 } 3797 3798 int 3799 perf_event__synthesize_event_update_unit(struct perf_tool *tool, 3800 struct evsel *evsel, 3801 perf_event__handler_t process) 3802 { 3803 struct event_update_event *ev; 3804 size_t size = strlen(evsel->unit); 3805 int err; 3806 3807 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]); 3808 if (ev == NULL) 3809 return -ENOMEM; 3810 3811 strlcpy(ev->data, evsel->unit, size + 1); 3812 err = process(tool, (union perf_event *)ev, NULL, NULL); 3813 free(ev); 3814 return err; 3815 } 3816 3817 int 3818 perf_event__synthesize_event_update_scale(struct perf_tool *tool, 3819 struct evsel *evsel, 3820 perf_event__handler_t process) 3821 { 3822 struct event_update_event *ev; 3823 struct event_update_event_scale *ev_data; 3824 int err; 3825 3826 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]); 3827 if (ev == NULL) 3828 return -ENOMEM; 3829 3830 ev_data = (struct event_update_event_scale *) ev->data; 3831 ev_data->scale = evsel->scale; 3832 err = process(tool, (union perf_event*) ev, NULL, NULL); 3833 free(ev); 3834 return err; 3835 } 3836 3837 int 3838 perf_event__synthesize_event_update_name(struct perf_tool *tool, 3839 struct evsel *evsel, 3840 perf_event__handler_t process) 3841 { 3842 struct event_update_event *ev; 3843 size_t len = strlen(evsel->name); 3844 int err; 3845 3846 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]); 3847 if (ev == NULL) 3848 return -ENOMEM; 3849 3850 strlcpy(ev->data, evsel->name, len + 1); 3851 err = process(tool, (union perf_event*) ev, NULL, NULL); 3852 free(ev); 3853 return err; 3854 } 3855 3856 int 3857 perf_event__synthesize_event_update_cpus(struct perf_tool *tool, 3858 struct evsel *evsel, 3859 perf_event__handler_t process) 3860 { 3861 size_t size = sizeof(struct event_update_event); 3862 struct event_update_event *ev; 3863 int max, err; 3864 u16 type; 3865 3866 if (!evsel->core.own_cpus) 3867 return 0; 3868 3869 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max); 3870 if (!ev) 3871 return -ENOMEM; 3872 3873 ev->header.type = PERF_RECORD_EVENT_UPDATE; 3874 ev->header.size = (u16)size; 3875 ev->type = PERF_EVENT_UPDATE__CPUS; 3876 ev->id = evsel->id[0]; 3877 3878 cpu_map_data__synthesize((struct cpu_map_data *) ev->data, 3879 evsel->core.own_cpus, 3880 type, max); 3881 3882 err = process(tool, (union perf_event*) ev, NULL, NULL); 3883 free(ev); 3884 return err; 3885 } 3886 3887 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp) 3888 { 3889 struct event_update_event *ev = &event->event_update; 3890 struct event_update_event_scale *ev_scale; 3891 struct event_update_event_cpus *ev_cpus; 3892 struct perf_cpu_map *map; 3893 size_t ret; 3894 3895 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id); 3896 3897 switch (ev->type) { 3898 case PERF_EVENT_UPDATE__SCALE: 3899 ev_scale = (struct event_update_event_scale *) ev->data; 3900 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale); 3901 break; 3902 case PERF_EVENT_UPDATE__UNIT: 3903 ret += fprintf(fp, "... unit: %s\n", ev->data); 3904 break; 3905 case PERF_EVENT_UPDATE__NAME: 3906 ret += fprintf(fp, "... name: %s\n", ev->data); 3907 break; 3908 case PERF_EVENT_UPDATE__CPUS: 3909 ev_cpus = (struct event_update_event_cpus *) ev->data; 3910 ret += fprintf(fp, "... "); 3911 3912 map = cpu_map__new_data(&ev_cpus->cpus); 3913 if (map) 3914 ret += cpu_map__fprintf(map, fp); 3915 else 3916 ret += fprintf(fp, "failed to get cpus\n"); 3917 break; 3918 default: 3919 ret += fprintf(fp, "... unknown type\n"); 3920 break; 3921 } 3922 3923 return ret; 3924 } 3925 3926 int perf_event__synthesize_attrs(struct perf_tool *tool, 3927 struct evlist *evlist, 3928 perf_event__handler_t process) 3929 { 3930 struct evsel *evsel; 3931 int err = 0; 3932 3933 evlist__for_each_entry(evlist, evsel) { 3934 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->ids, 3935 evsel->id, process); 3936 if (err) { 3937 pr_debug("failed to create perf header attribute\n"); 3938 return err; 3939 } 3940 } 3941 3942 return err; 3943 } 3944 3945 static bool has_unit(struct evsel *counter) 3946 { 3947 return counter->unit && *counter->unit; 3948 } 3949 3950 static bool has_scale(struct evsel *counter) 3951 { 3952 return counter->scale != 1; 3953 } 3954 3955 int perf_event__synthesize_extra_attr(struct perf_tool *tool, 3956 struct evlist *evsel_list, 3957 perf_event__handler_t process, 3958 bool is_pipe) 3959 { 3960 struct evsel *counter; 3961 int err; 3962 3963 /* 3964 * Synthesize other events stuff not carried within 3965 * attr event - unit, scale, name 3966 */ 3967 evlist__for_each_entry(evsel_list, counter) { 3968 if (!counter->supported) 3969 continue; 3970 3971 /* 3972 * Synthesize unit and scale only if it's defined. 3973 */ 3974 if (has_unit(counter)) { 3975 err = perf_event__synthesize_event_update_unit(tool, counter, process); 3976 if (err < 0) { 3977 pr_err("Couldn't synthesize evsel unit.\n"); 3978 return err; 3979 } 3980 } 3981 3982 if (has_scale(counter)) { 3983 err = perf_event__synthesize_event_update_scale(tool, counter, process); 3984 if (err < 0) { 3985 pr_err("Couldn't synthesize evsel counter.\n"); 3986 return err; 3987 } 3988 } 3989 3990 if (counter->core.own_cpus) { 3991 err = perf_event__synthesize_event_update_cpus(tool, counter, process); 3992 if (err < 0) { 3993 pr_err("Couldn't synthesize evsel cpus.\n"); 3994 return err; 3995 } 3996 } 3997 3998 /* 3999 * Name is needed only for pipe output, 4000 * perf.data carries event names. 4001 */ 4002 if (is_pipe) { 4003 err = perf_event__synthesize_event_update_name(tool, counter, process); 4004 if (err < 0) { 4005 pr_err("Couldn't synthesize evsel name.\n"); 4006 return err; 4007 } 4008 } 4009 } 4010 return 0; 4011 } 4012 4013 int perf_event__process_attr(struct perf_tool *tool __maybe_unused, 4014 union perf_event *event, 4015 struct evlist **pevlist) 4016 { 4017 u32 i, ids, n_ids; 4018 struct evsel *evsel; 4019 struct evlist *evlist = *pevlist; 4020 4021 if (evlist == NULL) { 4022 *pevlist = evlist = evlist__new(); 4023 if (evlist == NULL) 4024 return -ENOMEM; 4025 } 4026 4027 evsel = evsel__new(&event->attr.attr); 4028 if (evsel == NULL) 4029 return -ENOMEM; 4030 4031 evlist__add(evlist, evsel); 4032 4033 ids = event->header.size; 4034 ids -= (void *)&event->attr.id - (void *)event; 4035 n_ids = ids / sizeof(u64); 4036 /* 4037 * We don't have the cpu and thread maps on the header, so 4038 * for allocating the perf_sample_id table we fake 1 cpu and 4039 * hattr->ids threads. 4040 */ 4041 if (perf_evsel__alloc_id(evsel, 1, n_ids)) 4042 return -ENOMEM; 4043 4044 for (i = 0; i < n_ids; i++) { 4045 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); 4046 } 4047 4048 return 0; 4049 } 4050 4051 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, 4052 union perf_event *event, 4053 struct evlist **pevlist) 4054 { 4055 struct event_update_event *ev = &event->event_update; 4056 struct event_update_event_scale *ev_scale; 4057 struct event_update_event_cpus *ev_cpus; 4058 struct evlist *evlist; 4059 struct evsel *evsel; 4060 struct perf_cpu_map *map; 4061 4062 if (!pevlist || *pevlist == NULL) 4063 return -EINVAL; 4064 4065 evlist = *pevlist; 4066 4067 evsel = perf_evlist__id2evsel(evlist, ev->id); 4068 if (evsel == NULL) 4069 return -EINVAL; 4070 4071 switch (ev->type) { 4072 case PERF_EVENT_UPDATE__UNIT: 4073 evsel->unit = strdup(ev->data); 4074 break; 4075 case PERF_EVENT_UPDATE__NAME: 4076 evsel->name = strdup(ev->data); 4077 break; 4078 case PERF_EVENT_UPDATE__SCALE: 4079 ev_scale = (struct event_update_event_scale *) ev->data; 4080 evsel->scale = ev_scale->scale; 4081 break; 4082 case PERF_EVENT_UPDATE__CPUS: 4083 ev_cpus = (struct event_update_event_cpus *) ev->data; 4084 4085 map = cpu_map__new_data(&ev_cpus->cpus); 4086 if (map) 4087 evsel->core.own_cpus = map; 4088 else 4089 pr_err("failed to get event_update cpus\n"); 4090 default: 4091 break; 4092 } 4093 4094 return 0; 4095 } 4096 4097 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, 4098 struct evlist *evlist, 4099 perf_event__handler_t process) 4100 { 4101 union perf_event ev; 4102 struct tracing_data *tdata; 4103 ssize_t size = 0, aligned_size = 0, padding; 4104 struct feat_fd ff; 4105 int err __maybe_unused = 0; 4106 4107 /* 4108 * We are going to store the size of the data followed 4109 * by the data contents. Since the fd descriptor is a pipe, 4110 * we cannot seek back to store the size of the data once 4111 * we know it. Instead we: 4112 * 4113 * - write the tracing data to the temp file 4114 * - get/write the data size to pipe 4115 * - write the tracing data from the temp file 4116 * to the pipe 4117 */ 4118 tdata = tracing_data_get(&evlist->core.entries, fd, true); 4119 if (!tdata) 4120 return -1; 4121 4122 memset(&ev, 0, sizeof(ev)); 4123 4124 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; 4125 size = tdata->size; 4126 aligned_size = PERF_ALIGN(size, sizeof(u64)); 4127 padding = aligned_size - size; 4128 ev.tracing_data.header.size = sizeof(ev.tracing_data); 4129 ev.tracing_data.size = aligned_size; 4130 4131 process(tool, &ev, NULL, NULL); 4132 4133 /* 4134 * The put function will copy all the tracing data 4135 * stored in temp file to the pipe. 4136 */ 4137 tracing_data_put(tdata); 4138 4139 ff = (struct feat_fd){ .fd = fd }; 4140 if (write_padded(&ff, NULL, 0, padding)) 4141 return -1; 4142 4143 return aligned_size; 4144 } 4145 4146 int perf_event__process_tracing_data(struct perf_session *session, 4147 union perf_event *event) 4148 { 4149 ssize_t size_read, padding, size = event->tracing_data.size; 4150 int fd = perf_data__fd(session->data); 4151 off_t offset = lseek(fd, 0, SEEK_CUR); 4152 char buf[BUFSIZ]; 4153 4154 /* setup for reading amidst mmap */ 4155 lseek(fd, offset + sizeof(struct tracing_data_event), 4156 SEEK_SET); 4157 4158 size_read = trace_report(fd, &session->tevent, 4159 session->repipe); 4160 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; 4161 4162 if (readn(fd, buf, padding) < 0) { 4163 pr_err("%s: reading input file", __func__); 4164 return -1; 4165 } 4166 if (session->repipe) { 4167 int retw = write(STDOUT_FILENO, buf, padding); 4168 if (retw <= 0 || retw != padding) { 4169 pr_err("%s: repiping tracing data padding", __func__); 4170 return -1; 4171 } 4172 } 4173 4174 if (size_read + padding != size) { 4175 pr_err("%s: tracing data size mismatch", __func__); 4176 return -1; 4177 } 4178 4179 perf_evlist__prepare_tracepoint_events(session->evlist, 4180 session->tevent.pevent); 4181 4182 return size_read + padding; 4183 } 4184 4185 int perf_event__synthesize_build_id(struct perf_tool *tool, 4186 struct dso *pos, u16 misc, 4187 perf_event__handler_t process, 4188 struct machine *machine) 4189 { 4190 union perf_event ev; 4191 size_t len; 4192 int err = 0; 4193 4194 if (!pos->hit) 4195 return err; 4196 4197 memset(&ev, 0, sizeof(ev)); 4198 4199 len = pos->long_name_len + 1; 4200 len = PERF_ALIGN(len, NAME_ALIGN); 4201 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); 4202 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; 4203 ev.build_id.header.misc = misc; 4204 ev.build_id.pid = machine->pid; 4205 ev.build_id.header.size = sizeof(ev.build_id) + len; 4206 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); 4207 4208 err = process(tool, &ev, NULL, machine); 4209 4210 return err; 4211 } 4212 4213 int perf_event__process_build_id(struct perf_session *session, 4214 union perf_event *event) 4215 { 4216 __event_process_build_id(&event->build_id, 4217 event->build_id.filename, 4218 session); 4219 return 0; 4220 } 4221