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 <regex.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <linux/compiler.h> 13 #include <linux/list.h> 14 #include <linux/kernel.h> 15 #include <linux/bitops.h> 16 #include <linux/string.h> 17 #include <linux/stringify.h> 18 #include <linux/zalloc.h> 19 #include <sys/stat.h> 20 #include <sys/utsname.h> 21 #include <linux/time64.h> 22 #include <dirent.h> 23 #ifdef HAVE_LIBBPF_SUPPORT 24 #include <bpf/libbpf.h> 25 #endif 26 #include <perf/cpumap.h> 27 #include <tools/libc_compat.h> // reallocarray 28 29 #include "dso.h" 30 #include "evlist.h" 31 #include "evsel.h" 32 #include "util/evsel_fprintf.h" 33 #include "header.h" 34 #include "memswap.h" 35 #include "trace-event.h" 36 #include "session.h" 37 #include "symbol.h" 38 #include "debug.h" 39 #include "cpumap.h" 40 #include "pmu.h" 41 #include "pmus.h" 42 #include "vdso.h" 43 #include "strbuf.h" 44 #include "build-id.h" 45 #include "data.h" 46 #include <api/fs/fs.h> 47 #include <api/io_dir.h> 48 #include "asm/bug.h" 49 #include "tool.h" 50 #include "time-utils.h" 51 #include "units.h" 52 #include "util/util.h" // perf_exe() 53 #include "cputopo.h" 54 #include "bpf-event.h" 55 #include "bpf-utils.h" 56 #include "clockid.h" 57 #include "cacheline.h" 58 59 #include <linux/ctype.h> 60 #include <internal/lib.h> 61 62 #ifdef HAVE_LIBTRACEEVENT 63 #include <event-parse.h> 64 #endif 65 66 #define MAX_BPF_DATA_LEN (256 * 1024 * 1024) 67 #define MAX_BPF_PROGS 131072 68 #define MAX_CACHE_ENTRIES 32768 69 #define MAX_GROUP_DESC 32768 70 #define MAX_NUMA_NODES 4096 71 #define MAX_PMU_CAPS 512 72 #define MAX_PMU_MAPPINGS 4096 73 #define MAX_SCHED_DOMAINS 64 74 75 /* 76 * magic2 = "PERFILE2" 77 * must be a numerical value to let the endianness 78 * determine the memory layout. That way we are able 79 * to detect endianness when reading the perf.data file 80 * back. 81 * 82 * we check for legacy (PERFFILE) format. 83 */ 84 static const char *__perf_magic1 = "PERFFILE"; 85 static const u64 __perf_magic2 = 0x32454c4946524550ULL; 86 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; 87 88 #define PERF_MAGIC __perf_magic2 89 #define DNAME_LEN 16 90 91 const char perf_version_string[] = PERF_VERSION; 92 93 struct perf_file_attr { 94 struct perf_event_attr attr; 95 struct perf_file_section ids; 96 }; 97 98 void perf_header__set_feat(struct perf_header *header, int feat) 99 { 100 __set_bit(feat, header->adds_features); 101 } 102 103 void perf_header__clear_feat(struct perf_header *header, int feat) 104 { 105 __clear_bit(feat, header->adds_features); 106 } 107 108 bool perf_header__has_feat(const struct perf_header *header, int feat) 109 { 110 return test_bit(feat, header->adds_features); 111 } 112 113 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size) 114 { 115 ssize_t ret = writen(ff->fd, buf, size); 116 117 if (ret != (ssize_t)size) 118 return ret < 0 ? (int)ret : -1; 119 return 0; 120 } 121 122 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size) 123 { 124 /* struct perf_event_header::size is u16 */ 125 const size_t max_size = 0xffff - sizeof(struct perf_event_header); 126 size_t new_size = ff->size; 127 void *addr; 128 129 if (size + ff->offset > max_size) 130 return -E2BIG; 131 132 while (size > (new_size - ff->offset)) 133 new_size <<= 1; 134 new_size = min(max_size, new_size); 135 136 if (ff->size < new_size) { 137 addr = realloc(ff->buf, new_size); 138 if (!addr) 139 return -ENOMEM; 140 ff->buf = addr; 141 ff->size = new_size; 142 } 143 144 memcpy(ff->buf + ff->offset, buf, size); 145 ff->offset += size; 146 147 return 0; 148 } 149 150 /* Return: 0 if succeeded, -ERR if failed. */ 151 int do_write(struct feat_fd *ff, const void *buf, size_t size) 152 { 153 if (!ff->buf) 154 return __do_write_fd(ff, buf, size); 155 return __do_write_buf(ff, buf, size); 156 } 157 158 /* Return: 0 if succeeded, -ERR if failed. */ 159 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size) 160 { 161 u64 *p = (u64 *) set; 162 int i, ret; 163 164 ret = do_write(ff, &size, sizeof(size)); 165 if (ret < 0) 166 return ret; 167 168 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 169 ret = do_write(ff, p + i, sizeof(*p)); 170 if (ret < 0) 171 return ret; 172 } 173 174 return 0; 175 } 176 177 /* Return: 0 if succeeded, -ERR if failed. */ 178 int write_padded(struct feat_fd *ff, const void *bf, 179 size_t count, size_t count_aligned) 180 { 181 static const char zero_buf[NAME_ALIGN]; 182 int err = do_write(ff, bf, count); 183 184 if (!err) 185 err = do_write(ff, zero_buf, count_aligned - count); 186 187 return err; 188 } 189 190 #define string_size(str) \ 191 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) 192 193 /* Return: 0 if succeeded, -ERR if failed. */ 194 static int do_write_string(struct feat_fd *ff, const char *str) 195 { 196 u32 len, olen; 197 int ret; 198 199 olen = strlen(str) + 1; 200 len = PERF_ALIGN(olen, NAME_ALIGN); 201 202 /* write len, incl. \0 */ 203 ret = do_write(ff, &len, sizeof(len)); 204 if (ret < 0) 205 return ret; 206 207 return write_padded(ff, str, olen, len); 208 } 209 210 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size) 211 { 212 ssize_t ret = readn(ff->fd, addr, size); 213 214 if (ret != size) 215 return ret < 0 ? (int)ret : -1; 216 return 0; 217 } 218 219 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size) 220 { 221 if (size > (ssize_t)ff->size - ff->offset) 222 return -1; 223 224 memcpy(addr, ff->buf + ff->offset, size); 225 ff->offset += size; 226 227 return 0; 228 229 } 230 231 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size) 232 { 233 if (!ff->buf) 234 return __do_read_fd(ff, addr, size); 235 return __do_read_buf(ff, addr, size); 236 } 237 238 static int do_read_u32(struct feat_fd *ff, u32 *addr) 239 { 240 int ret; 241 242 ret = __do_read(ff, addr, sizeof(*addr)); 243 if (ret) 244 return ret; 245 246 if (ff->ph->needs_swap) 247 *addr = bswap_32(*addr); 248 return 0; 249 } 250 251 static int do_read_u64(struct feat_fd *ff, u64 *addr) 252 { 253 int ret; 254 255 ret = __do_read(ff, addr, sizeof(*addr)); 256 if (ret) 257 return ret; 258 259 if (ff->ph->needs_swap) 260 *addr = bswap_64(*addr); 261 return 0; 262 } 263 264 static char *do_read_string(struct feat_fd *ff) 265 { 266 u32 len; 267 char *buf; 268 269 if (do_read_u32(ff, &len)) 270 return NULL; 271 272 buf = malloc(len); 273 if (!buf) 274 return NULL; 275 276 if (!__do_read(ff, buf, len)) { 277 /* 278 * strings are padded by zeroes 279 * thus the actual strlen of buf 280 * may be less than len 281 */ 282 return buf; 283 } 284 285 free(buf); 286 return NULL; 287 } 288 289 /* Return: 0 if succeeded, -ERR if failed. */ 290 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize) 291 { 292 unsigned long *set; 293 u64 size, *p; 294 int i, ret; 295 296 ret = do_read_u64(ff, &size); 297 if (ret) 298 return ret; 299 300 set = bitmap_zalloc(size); 301 if (!set) 302 return -ENOMEM; 303 304 p = (u64 *) set; 305 306 for (i = 0; (u64) i < BITS_TO_U64(size); i++) { 307 ret = do_read_u64(ff, p + i); 308 if (ret < 0) { 309 free(set); 310 return ret; 311 } 312 } 313 314 *pset = set; 315 *psize = size; 316 return 0; 317 } 318 319 static int write_tracing_data(struct feat_fd *ff, 320 struct evlist *evlist __maybe_unused) 321 { 322 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 323 return -1; 324 325 #ifdef HAVE_LIBTRACEEVENT 326 return read_tracing_data(ff->fd, &evlist->core.entries); 327 #else 328 pr_err("ERROR: Trying to write tracing data without libtraceevent support.\n"); 329 return -1; 330 #endif 331 } 332 333 static int write_build_id(struct feat_fd *ff, 334 struct evlist *evlist __maybe_unused) 335 { 336 struct perf_session *session; 337 int err; 338 339 session = container_of(ff->ph, struct perf_session, header); 340 341 if (!perf_session__read_build_ids(session, true)) 342 return -1; 343 344 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 345 return -1; 346 347 err = perf_session__write_buildid_table(session, ff); 348 if (err < 0) { 349 pr_debug("failed to write buildid table\n"); 350 return err; 351 } 352 353 return 0; 354 } 355 356 static int write_hostname(struct feat_fd *ff, 357 struct evlist *evlist __maybe_unused) 358 { 359 struct utsname uts; 360 int ret; 361 362 ret = uname(&uts); 363 if (ret < 0) 364 return -1; 365 366 return do_write_string(ff, uts.nodename); 367 } 368 369 static int write_osrelease(struct feat_fd *ff, 370 struct evlist *evlist __maybe_unused) 371 { 372 struct utsname uts; 373 int ret; 374 375 ret = uname(&uts); 376 if (ret < 0) 377 return -1; 378 379 return do_write_string(ff, uts.release); 380 } 381 382 static int write_arch(struct feat_fd *ff, 383 struct evlist *evlist __maybe_unused) 384 { 385 struct utsname uts; 386 int ret; 387 388 ret = uname(&uts); 389 if (ret < 0) 390 return -1; 391 392 return do_write_string(ff, uts.machine); 393 } 394 395 static int write_e_machine(struct feat_fd *ff, 396 struct evlist *evlist __maybe_unused) 397 { 398 /* e_machine expanded from 16 to 32-bits for alignment. */ 399 uint32_t e_flags; 400 uint32_t e_machine = perf_session__e_machine(evlist->session, &e_flags); 401 int ret; 402 403 ret = do_write(ff, &e_machine, sizeof(e_machine)); 404 if (ret) 405 return ret; 406 407 return do_write(ff, &e_flags, sizeof(e_flags)); 408 } 409 410 static int write_version(struct feat_fd *ff, 411 struct evlist *evlist __maybe_unused) 412 { 413 return do_write_string(ff, perf_version_string); 414 } 415 416 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc) 417 { 418 FILE *file; 419 char *buf = NULL; 420 char *s, *p; 421 const char *search = cpuinfo_proc; 422 size_t len = 0; 423 int ret = -1; 424 425 if (!search) 426 return -1; 427 428 file = fopen("/proc/cpuinfo", "r"); 429 if (!file) 430 return -1; 431 432 while (getline(&buf, &len, file) > 0) { 433 ret = strncmp(buf, search, strlen(search)); 434 if (!ret) 435 break; 436 } 437 438 if (ret) { 439 ret = -1; 440 goto done; 441 } 442 443 s = buf; 444 445 p = strchr(buf, ':'); 446 if (p && *(p+1) == ' ' && *(p+2)) 447 s = p + 2; 448 p = strchr(s, '\n'); 449 if (p) 450 *p = '\0'; 451 452 /* squash extra space characters (branding string) */ 453 p = s; 454 while (*p) { 455 if (isspace(*p)) { 456 char *r = p + 1; 457 char *q = skip_spaces(r); 458 *p = ' '; 459 if (q != (p+1)) 460 while ((*r++ = *q++)); 461 } 462 p++; 463 } 464 ret = do_write_string(ff, s); 465 done: 466 free(buf); 467 fclose(file); 468 return ret; 469 } 470 471 static int write_cpudesc(struct feat_fd *ff, 472 struct evlist *evlist __maybe_unused) 473 { 474 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__) 475 #define CPUINFO_PROC { "cpu", } 476 #elif defined(__s390__) 477 #define CPUINFO_PROC { "vendor_id", } 478 #elif defined(__sh__) 479 #define CPUINFO_PROC { "cpu type", } 480 #elif defined(__alpha__) || defined(__mips__) 481 #define CPUINFO_PROC { "cpu model", } 482 #elif defined(__arm__) 483 #define CPUINFO_PROC { "model name", "Processor", } 484 #elif defined(__arc__) 485 #define CPUINFO_PROC { "Processor", } 486 #elif defined(__xtensa__) 487 #define CPUINFO_PROC { "core ID", } 488 #elif defined(__loongarch__) 489 #define CPUINFO_PROC { "Model Name", } 490 #else 491 #define CPUINFO_PROC { "model name", } 492 #endif 493 const char *cpuinfo_procs[] = CPUINFO_PROC; 494 #undef CPUINFO_PROC 495 unsigned int i; 496 497 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { 498 int ret; 499 ret = __write_cpudesc(ff, cpuinfo_procs[i]); 500 if (ret >= 0) 501 return ret; 502 } 503 return -1; 504 } 505 506 507 static int write_nrcpus(struct feat_fd *ff, 508 struct evlist *evlist __maybe_unused) 509 { 510 long nr; 511 u32 nrc, nra; 512 int ret; 513 514 nrc = cpu__max_present_cpu().cpu; 515 516 nr = sysconf(_SC_NPROCESSORS_ONLN); 517 if (nr < 0) 518 return -1; 519 520 nra = (u32)(nr & UINT_MAX); 521 522 ret = do_write(ff, &nrc, sizeof(nrc)); 523 if (ret < 0) 524 return ret; 525 526 return do_write(ff, &nra, sizeof(nra)); 527 } 528 529 static int write_event_desc(struct feat_fd *ff, 530 struct evlist *evlist) 531 { 532 struct evsel *evsel; 533 u32 nre, nri, sz; 534 int ret; 535 536 nre = evlist->core.nr_entries; 537 538 /* 539 * write number of events 540 */ 541 ret = do_write(ff, &nre, sizeof(nre)); 542 if (ret < 0) 543 return ret; 544 545 /* 546 * size of perf_event_attr struct 547 */ 548 sz = (u32)sizeof(evsel->core.attr); 549 ret = do_write(ff, &sz, sizeof(sz)); 550 if (ret < 0) 551 return ret; 552 553 evlist__for_each_entry(evlist, evsel) { 554 ret = do_write(ff, &evsel->core.attr, sz); 555 if (ret < 0) 556 return ret; 557 /* 558 * write number of unique id per event 559 * there is one id per instance of an event 560 * 561 * copy into an nri to be independent of the 562 * type of ids, 563 */ 564 nri = evsel->core.ids; 565 ret = do_write(ff, &nri, sizeof(nri)); 566 if (ret < 0) 567 return ret; 568 569 /* 570 * write event string as passed on cmdline 571 */ 572 ret = do_write_string(ff, evsel__name(evsel)); 573 if (ret < 0) 574 return ret; 575 /* 576 * write unique ids for this event 577 */ 578 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 579 if (ret < 0) 580 return ret; 581 } 582 return 0; 583 } 584 585 static int write_cmdline(struct feat_fd *ff, 586 struct evlist *evlist __maybe_unused) 587 { 588 struct perf_env *env = &ff->ph->env; 589 char pbuf[MAXPATHLEN], *buf; 590 int i, ret, n; 591 592 /* actual path to perf binary */ 593 buf = perf_exe(pbuf, MAXPATHLEN); 594 595 /* account for binary path */ 596 n = env->nr_cmdline + 1; 597 598 ret = do_write(ff, &n, sizeof(n)); 599 if (ret < 0) 600 return ret; 601 602 ret = do_write_string(ff, buf); 603 if (ret < 0) 604 return ret; 605 606 for (i = 0 ; i < env->nr_cmdline; i++) { 607 ret = do_write_string(ff, env->cmdline_argv[i]); 608 if (ret < 0) 609 return ret; 610 } 611 return 0; 612 } 613 614 615 static int write_cpu_topology(struct feat_fd *ff, 616 struct evlist *evlist __maybe_unused) 617 { 618 struct perf_env *env = &ff->ph->env; 619 struct cpu_topology *tp; 620 u32 i; 621 int ret, j; 622 623 tp = cpu_topology__new(); 624 if (!tp) 625 return -1; 626 627 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists)); 628 if (ret < 0) 629 goto done; 630 631 for (i = 0; i < tp->package_cpus_lists; i++) { 632 ret = do_write_string(ff, tp->package_cpus_list[i]); 633 if (ret < 0) 634 goto done; 635 } 636 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists)); 637 if (ret < 0) 638 goto done; 639 640 for (i = 0; i < tp->core_cpus_lists; i++) { 641 ret = do_write_string(ff, tp->core_cpus_list[i]); 642 if (ret < 0) 643 break; 644 } 645 646 ret = perf_env__read_cpu_topology_map(env); 647 if (ret < 0) 648 goto done; 649 650 for (j = 0; j < env->nr_cpus_avail; j++) { 651 ret = do_write(ff, &env->cpu[j].core_id, 652 sizeof(env->cpu[j].core_id)); 653 if (ret < 0) 654 return ret; 655 ret = do_write(ff, &env->cpu[j].socket_id, 656 sizeof(env->cpu[j].socket_id)); 657 if (ret < 0) 658 return ret; 659 } 660 661 if (!tp->die_cpus_lists) 662 goto done; 663 664 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists)); 665 if (ret < 0) 666 goto done; 667 668 for (i = 0; i < tp->die_cpus_lists; i++) { 669 ret = do_write_string(ff, tp->die_cpus_list[i]); 670 if (ret < 0) 671 goto done; 672 } 673 674 for (j = 0; j < env->nr_cpus_avail; j++) { 675 ret = do_write(ff, &env->cpu[j].die_id, 676 sizeof(env->cpu[j].die_id)); 677 if (ret < 0) 678 return ret; 679 } 680 681 done: 682 cpu_topology__delete(tp); 683 return ret; 684 } 685 686 687 688 static int write_total_mem(struct feat_fd *ff, 689 struct evlist *evlist __maybe_unused) 690 { 691 char *buf = NULL; 692 FILE *fp; 693 size_t len = 0; 694 int ret = -1, n; 695 uint64_t mem; 696 697 fp = fopen("/proc/meminfo", "r"); 698 if (!fp) 699 return -1; 700 701 while (getline(&buf, &len, fp) > 0) { 702 ret = strncmp(buf, "MemTotal:", 9); 703 if (!ret) 704 break; 705 } 706 if (!ret) { 707 n = sscanf(buf, "%*s %"PRIu64, &mem); 708 if (n == 1) 709 ret = do_write(ff, &mem, sizeof(mem)); 710 } else 711 ret = -1; 712 free(buf); 713 fclose(fp); 714 return ret; 715 } 716 717 static int write_numa_topology(struct feat_fd *ff, 718 struct evlist *evlist __maybe_unused) 719 { 720 struct numa_topology *tp; 721 int ret = -1; 722 u32 i; 723 724 tp = numa_topology__new(); 725 if (!tp) 726 return -ENOMEM; 727 728 ret = do_write(ff, &tp->nr, sizeof(u32)); 729 if (ret < 0) 730 goto err; 731 732 for (i = 0; i < tp->nr; i++) { 733 struct numa_topology_node *n = &tp->nodes[i]; 734 735 ret = do_write(ff, &n->node, sizeof(u32)); 736 if (ret < 0) 737 goto err; 738 739 ret = do_write(ff, &n->mem_total, sizeof(u64)); 740 if (ret) 741 goto err; 742 743 ret = do_write(ff, &n->mem_free, sizeof(u64)); 744 if (ret) 745 goto err; 746 747 ret = do_write_string(ff, n->cpus); 748 if (ret < 0) 749 goto err; 750 } 751 752 ret = 0; 753 754 err: 755 numa_topology__delete(tp); 756 return ret; 757 } 758 759 /* 760 * File format: 761 * 762 * struct pmu_mappings { 763 * u32 pmu_num; 764 * struct pmu_map { 765 * u32 type; 766 * char name[]; 767 * }[pmu_num]; 768 * }; 769 */ 770 771 static int write_pmu_mappings(struct feat_fd *ff, 772 struct evlist *evlist __maybe_unused) 773 { 774 struct perf_pmu *pmu = NULL; 775 u32 pmu_num = 0; 776 int ret; 777 778 /* 779 * Do a first pass to count number of pmu to avoid lseek so this 780 * works in pipe mode as well. 781 */ 782 while ((pmu = perf_pmus__scan(pmu))) 783 pmu_num++; 784 785 ret = do_write(ff, &pmu_num, sizeof(pmu_num)); 786 if (ret < 0) 787 return ret; 788 789 while ((pmu = perf_pmus__scan(pmu))) { 790 ret = do_write(ff, &pmu->type, sizeof(pmu->type)); 791 if (ret < 0) 792 return ret; 793 794 ret = do_write_string(ff, pmu->name); 795 if (ret < 0) 796 return ret; 797 } 798 799 return 0; 800 } 801 802 /* 803 * File format: 804 * 805 * struct group_descs { 806 * u32 nr_groups; 807 * struct group_desc { 808 * char name[]; 809 * u32 leader_idx; 810 * u32 nr_members; 811 * }[nr_groups]; 812 * }; 813 */ 814 static int write_group_desc(struct feat_fd *ff, 815 struct evlist *evlist) 816 { 817 u32 nr_groups = evlist__nr_groups(evlist); 818 struct evsel *evsel; 819 int ret; 820 821 ret = do_write(ff, &nr_groups, sizeof(nr_groups)); 822 if (ret < 0) 823 return ret; 824 825 evlist__for_each_entry(evlist, evsel) { 826 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 827 const char *name = evsel->group_name ?: "{anon_group}"; 828 u32 leader_idx = evsel->core.idx; 829 u32 nr_members = evsel->core.nr_members; 830 831 ret = do_write_string(ff, name); 832 if (ret < 0) 833 return ret; 834 835 ret = do_write(ff, &leader_idx, sizeof(leader_idx)); 836 if (ret < 0) 837 return ret; 838 839 ret = do_write(ff, &nr_members, sizeof(nr_members)); 840 if (ret < 0) 841 return ret; 842 } 843 } 844 return 0; 845 } 846 847 /* 848 * Return the CPU id as a raw string. 849 * 850 * Each architecture should provide a more precise id string that 851 * can be use to match the architecture's "mapfile". 852 */ 853 char * __weak get_cpuid_str(struct perf_cpu cpu __maybe_unused) 854 { 855 return NULL; 856 } 857 858 char *get_cpuid_allow_env_override(struct perf_cpu cpu) 859 { 860 char *cpuid; 861 static bool printed; 862 863 cpuid = getenv("PERF_CPUID"); 864 if (cpuid) 865 cpuid = strdup(cpuid); 866 if (!cpuid) 867 cpuid = get_cpuid_str(cpu); 868 if (!cpuid) 869 return NULL; 870 871 if (!printed) { 872 pr_debug("Using CPUID %s\n", cpuid); 873 printed = true; 874 } 875 return cpuid; 876 } 877 878 /* Return zero when the cpuid from the mapfile.csv matches the 879 * cpuid string generated on this platform. 880 * Otherwise return non-zero. 881 */ 882 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) 883 { 884 regex_t re; 885 regmatch_t pmatch[1]; 886 int match; 887 888 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { 889 /* Warn unable to generate match particular string. */ 890 pr_info("Invalid regular expression %s\n", mapcpuid); 891 return 1; 892 } 893 894 match = !regexec(&re, cpuid, 1, pmatch, 0); 895 regfree(&re); 896 if (match) { 897 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); 898 899 /* Verify the entire string matched. */ 900 if (match_len == strlen(cpuid)) 901 return 0; 902 } 903 return 1; 904 } 905 906 /* 907 * default get_cpuid(): nothing gets recorded 908 * actual implementation must be in arch/$(SRCARCH)/util/header.c 909 */ 910 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused, 911 struct perf_cpu cpu __maybe_unused) 912 { 913 return ENOSYS; /* Not implemented */ 914 } 915 916 static int write_cpuid(struct feat_fd *ff, struct evlist *evlist) 917 { 918 struct perf_cpu cpu = perf_cpu_map__min(evlist->core.all_cpus); 919 char buffer[64]; 920 int ret; 921 922 ret = get_cpuid(buffer, sizeof(buffer), cpu); 923 if (ret) 924 return -1; 925 926 return do_write_string(ff, buffer); 927 } 928 929 static int write_branch_stack(struct feat_fd *ff __maybe_unused, 930 struct evlist *evlist __maybe_unused) 931 { 932 return 0; 933 } 934 935 static int write_auxtrace(struct feat_fd *ff, 936 struct evlist *evlist __maybe_unused) 937 { 938 struct perf_session *session; 939 int err; 940 941 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 942 return -1; 943 944 session = container_of(ff->ph, struct perf_session, header); 945 946 err = auxtrace_index__write(ff->fd, &session->auxtrace_index); 947 if (err < 0) 948 pr_err("Failed to write auxtrace index\n"); 949 return err; 950 } 951 952 static int write_clockid(struct feat_fd *ff, 953 struct evlist *evlist __maybe_unused) 954 { 955 return do_write(ff, &ff->ph->env.clock.clockid_res_ns, 956 sizeof(ff->ph->env.clock.clockid_res_ns)); 957 } 958 959 static int write_clock_data(struct feat_fd *ff, 960 struct evlist *evlist __maybe_unused) 961 { 962 u64 *data64; 963 u32 data32; 964 int ret; 965 966 /* version */ 967 data32 = 1; 968 969 ret = do_write(ff, &data32, sizeof(data32)); 970 if (ret < 0) 971 return ret; 972 973 /* clockid */ 974 data32 = ff->ph->env.clock.clockid; 975 976 ret = do_write(ff, &data32, sizeof(data32)); 977 if (ret < 0) 978 return ret; 979 980 /* TOD ref time */ 981 data64 = &ff->ph->env.clock.tod_ns; 982 983 ret = do_write(ff, data64, sizeof(*data64)); 984 if (ret < 0) 985 return ret; 986 987 /* clockid ref time */ 988 data64 = &ff->ph->env.clock.clockid_ns; 989 990 return do_write(ff, data64, sizeof(*data64)); 991 } 992 993 static int write_hybrid_topology(struct feat_fd *ff, 994 struct evlist *evlist __maybe_unused) 995 { 996 struct hybrid_topology *tp; 997 int ret; 998 u32 i; 999 1000 tp = hybrid_topology__new(); 1001 if (!tp) 1002 return -ENOENT; 1003 1004 ret = do_write(ff, &tp->nr, sizeof(u32)); 1005 if (ret < 0) 1006 goto err; 1007 1008 for (i = 0; i < tp->nr; i++) { 1009 struct hybrid_topology_node *n = &tp->nodes[i]; 1010 1011 ret = do_write_string(ff, n->pmu_name); 1012 if (ret < 0) 1013 goto err; 1014 1015 ret = do_write_string(ff, n->cpus); 1016 if (ret < 0) 1017 goto err; 1018 } 1019 1020 ret = 0; 1021 1022 err: 1023 hybrid_topology__delete(tp); 1024 return ret; 1025 } 1026 1027 static int write_dir_format(struct feat_fd *ff, 1028 struct evlist *evlist __maybe_unused) 1029 { 1030 struct perf_session *session; 1031 struct perf_data *data; 1032 1033 session = container_of(ff->ph, struct perf_session, header); 1034 data = session->data; 1035 1036 if (WARN_ON(!perf_data__is_dir(data))) 1037 return -1; 1038 1039 return do_write(ff, &data->dir.version, sizeof(data->dir.version)); 1040 } 1041 1042 static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused, 1043 struct evlist *evlist __maybe_unused) 1044 { 1045 #ifdef HAVE_LIBBPF_SUPPORT 1046 struct perf_env *env = &ff->ph->env; 1047 struct rb_root *root; 1048 struct rb_node *next; 1049 int ret = 0; 1050 1051 down_read(&env->bpf_progs.lock); 1052 1053 ret = do_write(ff, &env->bpf_progs.infos_cnt, 1054 sizeof(env->bpf_progs.infos_cnt)); 1055 if (ret < 0 || env->bpf_progs.infos_cnt == 0) 1056 goto out; 1057 1058 root = &env->bpf_progs.infos; 1059 next = rb_first(root); 1060 while (next) { 1061 struct bpf_prog_info_node *node; 1062 size_t len; 1063 1064 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 1065 next = rb_next(&node->rb_node); 1066 len = sizeof(struct perf_bpil) + 1067 node->info_linear->data_len; 1068 1069 /* before writing to file, translate address to offset */ 1070 bpil_addr_to_offs(node->info_linear); 1071 ret = do_write(ff, node->info_linear, len); 1072 /* 1073 * translate back to address even when do_write() fails, 1074 * so that this function never changes the data. 1075 */ 1076 bpil_offs_to_addr(node->info_linear); 1077 if (ret < 0) 1078 goto out; 1079 } 1080 out: 1081 up_read(&env->bpf_progs.lock); 1082 return ret; 1083 #else 1084 pr_err("ERROR: Trying to write bpf_prog_info without libbpf support.\n"); 1085 return -1; 1086 #endif // HAVE_LIBBPF_SUPPORT 1087 } 1088 1089 static int write_bpf_btf(struct feat_fd *ff __maybe_unused, 1090 struct evlist *evlist __maybe_unused) 1091 { 1092 #ifdef HAVE_LIBBPF_SUPPORT 1093 struct perf_env *env = &ff->ph->env; 1094 struct rb_root *root; 1095 struct rb_node *next; 1096 int ret = 0; 1097 1098 down_read(&env->bpf_progs.lock); 1099 1100 ret = do_write(ff, &env->bpf_progs.btfs_cnt, 1101 sizeof(env->bpf_progs.btfs_cnt)); 1102 1103 if (ret < 0 || env->bpf_progs.btfs_cnt == 0) 1104 goto out; 1105 1106 root = &env->bpf_progs.btfs; 1107 next = rb_first(root); 1108 while (next) { 1109 struct btf_node *node; 1110 1111 node = rb_entry(next, struct btf_node, rb_node); 1112 next = rb_next(&node->rb_node); 1113 ret = do_write(ff, &node->id, 1114 sizeof(u32) * 2 + node->data_size); 1115 if (ret < 0) 1116 goto out; 1117 } 1118 out: 1119 up_read(&env->bpf_progs.lock); 1120 return ret; 1121 #else 1122 pr_err("ERROR: Trying to write btf data without libbpf support.\n"); 1123 return -1; 1124 #endif // HAVE_LIBBPF_SUPPORT 1125 } 1126 1127 static int cpu_cache_level__sort(const void *a, const void *b) 1128 { 1129 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a; 1130 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b; 1131 1132 return cache_a->level - cache_b->level; 1133 } 1134 1135 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b) 1136 { 1137 if (a->level != b->level) 1138 return false; 1139 1140 if (a->line_size != b->line_size) 1141 return false; 1142 1143 if (a->sets != b->sets) 1144 return false; 1145 1146 if (a->ways != b->ways) 1147 return false; 1148 1149 if (strcmp(a->type, b->type)) 1150 return false; 1151 1152 if (strcmp(a->size, b->size)) 1153 return false; 1154 1155 if (strcmp(a->map, b->map)) 1156 return false; 1157 1158 return true; 1159 } 1160 1161 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) 1162 { 1163 char path[PATH_MAX], file[PATH_MAX]; 1164 struct stat st; 1165 size_t len; 1166 1167 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); 1168 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path); 1169 1170 if (stat(file, &st)) 1171 return 1; 1172 1173 scnprintf(file, PATH_MAX, "%s/level", path); 1174 if (sysfs__read_int(file, (int *) &cache->level)) 1175 return -1; 1176 1177 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path); 1178 if (sysfs__read_int(file, (int *) &cache->line_size)) 1179 return -1; 1180 1181 scnprintf(file, PATH_MAX, "%s/number_of_sets", path); 1182 if (sysfs__read_int(file, (int *) &cache->sets)) 1183 return -1; 1184 1185 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path); 1186 if (sysfs__read_int(file, (int *) &cache->ways)) 1187 return -1; 1188 1189 scnprintf(file, PATH_MAX, "%s/type", path); 1190 if (sysfs__read_str(file, &cache->type, &len)) 1191 return -1; 1192 1193 cache->type[len] = 0; 1194 cache->type = strim(cache->type); 1195 1196 scnprintf(file, PATH_MAX, "%s/size", path); 1197 if (sysfs__read_str(file, &cache->size, &len)) { 1198 zfree(&cache->type); 1199 return -1; 1200 } 1201 1202 cache->size[len] = 0; 1203 cache->size = strim(cache->size); 1204 1205 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); 1206 if (sysfs__read_str(file, &cache->map, &len)) { 1207 zfree(&cache->size); 1208 zfree(&cache->type); 1209 return -1; 1210 } 1211 1212 cache->map[len] = 0; 1213 cache->map = strim(cache->map); 1214 return 0; 1215 } 1216 1217 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c) 1218 { 1219 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); 1220 } 1221 1222 /* 1223 * Build caches levels for a particular CPU from the data in 1224 * /sys/devices/system/cpu/cpu<cpu>/cache/ 1225 * The cache level data is stored in caches[] from index at 1226 * *cntp. 1227 */ 1228 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp) 1229 { 1230 u16 level; 1231 1232 for (level = 0; level < MAX_CACHE_LVL; level++) { 1233 struct cpu_cache_level c; 1234 int err; 1235 u32 i; 1236 1237 err = cpu_cache_level__read(&c, cpu, level); 1238 if (err < 0) 1239 return err; 1240 1241 if (err == 1) 1242 break; 1243 1244 for (i = 0; i < *cntp; i++) { 1245 if (cpu_cache_level__cmp(&c, &caches[i])) 1246 break; 1247 } 1248 1249 if (i == *cntp) { 1250 caches[*cntp] = c; 1251 *cntp = *cntp + 1; 1252 } else 1253 cpu_cache_level__free(&c); 1254 } 1255 1256 return 0; 1257 } 1258 1259 static int build_caches(struct cpu_cache_level caches[], u32 *cntp) 1260 { 1261 u32 nr, cpu, cnt = 0; 1262 1263 nr = cpu__max_cpu().cpu; 1264 1265 for (cpu = 0; cpu < nr; cpu++) { 1266 int ret = build_caches_for_cpu(cpu, caches, &cnt); 1267 1268 if (ret) 1269 return ret; 1270 } 1271 *cntp = cnt; 1272 return 0; 1273 } 1274 1275 static int write_cache(struct feat_fd *ff, 1276 struct evlist *evlist __maybe_unused) 1277 { 1278 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL; 1279 struct cpu_cache_level caches[max_caches]; 1280 u32 cnt = 0, i, version = 1; 1281 int ret; 1282 1283 ret = build_caches(caches, &cnt); 1284 if (ret) 1285 goto out; 1286 1287 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort); 1288 1289 ret = do_write(ff, &version, sizeof(u32)); 1290 if (ret < 0) 1291 goto out; 1292 1293 ret = do_write(ff, &cnt, sizeof(u32)); 1294 if (ret < 0) 1295 goto out; 1296 1297 for (i = 0; i < cnt; i++) { 1298 struct cpu_cache_level *c = &caches[i]; 1299 1300 #define _W(v) \ 1301 ret = do_write(ff, &c->v, sizeof(u32)); \ 1302 if (ret < 0) \ 1303 goto out; 1304 1305 _W(level) 1306 _W(line_size) 1307 _W(sets) 1308 _W(ways) 1309 #undef _W 1310 1311 #define _W(v) \ 1312 ret = do_write_string(ff, (const char *) c->v); \ 1313 if (ret < 0) \ 1314 goto out; 1315 1316 _W(type) 1317 _W(size) 1318 _W(map) 1319 #undef _W 1320 } 1321 1322 out: 1323 for (i = 0; i < cnt; i++) 1324 cpu_cache_level__free(&caches[i]); 1325 return ret; 1326 } 1327 1328 static int write_cln_size(struct feat_fd *ff, 1329 struct evlist *evlist __maybe_unused) 1330 { 1331 int cln_size = cacheline_size(); 1332 1333 if (!cln_size) 1334 cln_size = DEFAULT_CACHELINE_SIZE; 1335 1336 ff->ph->env.cln_size = cln_size; 1337 1338 return do_write(ff, &cln_size, sizeof(cln_size)); 1339 } 1340 1341 static int write_stat(struct feat_fd *ff __maybe_unused, 1342 struct evlist *evlist __maybe_unused) 1343 { 1344 return 0; 1345 } 1346 1347 static int write_sample_time(struct feat_fd *ff, 1348 struct evlist *evlist) 1349 { 1350 int ret; 1351 1352 ret = do_write(ff, &evlist->first_sample_time, 1353 sizeof(evlist->first_sample_time)); 1354 if (ret < 0) 1355 return ret; 1356 1357 return do_write(ff, &evlist->last_sample_time, 1358 sizeof(evlist->last_sample_time)); 1359 } 1360 1361 1362 static int memory_node__read(struct memory_node *n, unsigned long idx) 1363 { 1364 unsigned int phys, size = 0; 1365 char path[PATH_MAX]; 1366 struct io_dirent64 *ent; 1367 struct io_dir dir; 1368 1369 #define for_each_memory(mem, dir) \ 1370 while ((ent = io_dir__readdir(&dir)) != NULL) \ 1371 if (strcmp(ent->d_name, ".") && \ 1372 strcmp(ent->d_name, "..") && \ 1373 sscanf(ent->d_name, "memory%u", &mem) == 1) 1374 1375 scnprintf(path, PATH_MAX, 1376 "%s/devices/system/node/node%lu", 1377 sysfs__mountpoint(), idx); 1378 1379 io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 1380 if (dir.dirfd < 0) { 1381 pr_warning("failed: can't open memory sysfs data '%s'\n", path); 1382 return -1; 1383 } 1384 1385 for_each_memory(phys, dir) { 1386 size = max(phys, size); 1387 } 1388 1389 size++; 1390 1391 n->set = bitmap_zalloc(size); 1392 if (!n->set) { 1393 close(dir.dirfd); 1394 return -ENOMEM; 1395 } 1396 1397 n->node = idx; 1398 n->size = size; 1399 1400 io_dir__rewinddir(&dir); 1401 1402 for_each_memory(phys, dir) { 1403 __set_bit(phys, n->set); 1404 } 1405 1406 close(dir.dirfd); 1407 return 0; 1408 } 1409 1410 static void memory_node__delete_nodes(struct memory_node *nodesp, u64 cnt) 1411 { 1412 for (u64 i = 0; i < cnt; i++) 1413 bitmap_free(nodesp[i].set); 1414 1415 free(nodesp); 1416 } 1417 1418 static int memory_node__sort(const void *a, const void *b) 1419 { 1420 const struct memory_node *na = a; 1421 const struct memory_node *nb = b; 1422 1423 return na->node - nb->node; 1424 } 1425 1426 static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) 1427 { 1428 char path[PATH_MAX]; 1429 struct io_dirent64 *ent; 1430 struct io_dir dir; 1431 int ret = 0; 1432 size_t cnt = 0, size = 0; 1433 struct memory_node *nodes = NULL; 1434 1435 scnprintf(path, PATH_MAX, "%s/devices/system/node/", 1436 sysfs__mountpoint()); 1437 1438 io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 1439 if (dir.dirfd < 0) { 1440 pr_debug2("%s: couldn't read %s, does this arch have topology information?\n", 1441 __func__, path); 1442 return -1; 1443 } 1444 1445 while (!ret && (ent = io_dir__readdir(&dir))) { 1446 unsigned int idx; 1447 int r; 1448 1449 if (!strcmp(ent->d_name, ".") || 1450 !strcmp(ent->d_name, "..")) 1451 continue; 1452 1453 r = sscanf(ent->d_name, "node%u", &idx); 1454 if (r != 1) 1455 continue; 1456 1457 if (cnt >= size) { 1458 struct memory_node *new_nodes = 1459 reallocarray(nodes, cnt + 4, sizeof(*nodes)); 1460 1461 if (!new_nodes) { 1462 pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size); 1463 ret = -ENOMEM; 1464 goto out; 1465 } 1466 nodes = new_nodes; 1467 size += 4; 1468 } 1469 ret = memory_node__read(&nodes[cnt], idx); 1470 if (!ret) 1471 cnt += 1; 1472 } 1473 out: 1474 close(dir.dirfd); 1475 if (!ret) { 1476 *cntp = cnt; 1477 *nodesp = nodes; 1478 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); 1479 } else 1480 memory_node__delete_nodes(nodes, cnt); 1481 1482 return ret; 1483 } 1484 1485 /* 1486 * The MEM_TOPOLOGY holds physical memory map for every 1487 * node in system. The format of data is as follows: 1488 * 1489 * 0 - version | for future changes 1490 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes 1491 * 16 - count | number of nodes 1492 * 1493 * For each node we store map of physical indexes for 1494 * each node: 1495 * 1496 * 32 - node id | node index 1497 * 40 - size | size of bitmap 1498 * 48 - bitmap | bitmap of memory indexes that belongs to node 1499 */ 1500 static int write_mem_topology(struct feat_fd *ff __maybe_unused, 1501 struct evlist *evlist __maybe_unused) 1502 { 1503 struct memory_node *nodes = NULL; 1504 u64 bsize, version = 1, i, nr = 0; 1505 int ret; 1506 1507 ret = sysfs__read_xll("devices/system/memory/block_size_bytes", 1508 (unsigned long long *) &bsize); 1509 if (ret) 1510 return ret; 1511 1512 ret = build_mem_topology(&nodes, &nr); 1513 if (ret) 1514 return ret; 1515 1516 ret = do_write(ff, &version, sizeof(version)); 1517 if (ret < 0) 1518 goto out; 1519 1520 ret = do_write(ff, &bsize, sizeof(bsize)); 1521 if (ret < 0) 1522 goto out; 1523 1524 ret = do_write(ff, &nr, sizeof(nr)); 1525 if (ret < 0) 1526 goto out; 1527 1528 for (i = 0; i < nr; i++) { 1529 struct memory_node *n = &nodes[i]; 1530 1531 #define _W(v) \ 1532 ret = do_write(ff, &n->v, sizeof(n->v)); \ 1533 if (ret < 0) \ 1534 goto out; 1535 1536 _W(node) 1537 _W(size) 1538 1539 #undef _W 1540 1541 ret = do_write_bitmap(ff, n->set, n->size); 1542 if (ret < 0) 1543 goto out; 1544 } 1545 1546 out: 1547 memory_node__delete_nodes(nodes, nr); 1548 return ret; 1549 } 1550 1551 static int write_compressed(struct feat_fd *ff __maybe_unused, 1552 struct evlist *evlist __maybe_unused) 1553 { 1554 int ret; 1555 1556 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); 1557 if (ret) 1558 return ret; 1559 1560 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); 1561 if (ret) 1562 return ret; 1563 1564 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); 1565 if (ret) 1566 return ret; 1567 1568 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); 1569 if (ret) 1570 return ret; 1571 1572 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); 1573 } 1574 1575 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu, 1576 bool write_pmu) 1577 { 1578 struct perf_pmu_caps *caps = NULL; 1579 int ret; 1580 1581 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps)); 1582 if (ret < 0) 1583 return ret; 1584 1585 list_for_each_entry(caps, &pmu->caps, list) { 1586 ret = do_write_string(ff, caps->name); 1587 if (ret < 0) 1588 return ret; 1589 1590 ret = do_write_string(ff, caps->value); 1591 if (ret < 0) 1592 return ret; 1593 } 1594 1595 if (write_pmu) { 1596 ret = do_write_string(ff, pmu->name); 1597 if (ret < 0) 1598 return ret; 1599 } 1600 1601 return ret; 1602 } 1603 1604 static int write_cpu_pmu_caps(struct feat_fd *ff, 1605 struct evlist *evlist __maybe_unused) 1606 { 1607 struct perf_pmu *cpu_pmu = perf_pmus__find_core_pmu(); 1608 int ret; 1609 1610 if (!cpu_pmu) 1611 return -ENOENT; 1612 1613 ret = perf_pmu__caps_parse(cpu_pmu); 1614 if (ret < 0) 1615 return ret; 1616 1617 return __write_pmu_caps(ff, cpu_pmu, false); 1618 } 1619 1620 static int write_pmu_caps(struct feat_fd *ff, 1621 struct evlist *evlist __maybe_unused) 1622 { 1623 struct perf_pmu *pmu = NULL; 1624 int nr_pmu = 0; 1625 int ret; 1626 1627 while ((pmu = perf_pmus__scan(pmu))) { 1628 if (!strcmp(pmu->name, "cpu")) { 1629 /* 1630 * The "cpu" PMU is special and covered by 1631 * HEADER_CPU_PMU_CAPS. Note, core PMUs are 1632 * counted/written here for ARM, s390 and Intel hybrid. 1633 */ 1634 continue; 1635 } 1636 if (perf_pmu__caps_parse(pmu) <= 0) 1637 continue; 1638 nr_pmu++; 1639 } 1640 1641 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu)); 1642 if (ret < 0) 1643 return ret; 1644 1645 if (!nr_pmu) 1646 return 0; 1647 1648 /* 1649 * Note older perf tools assume core PMUs come first, this is a property 1650 * of perf_pmus__scan. 1651 */ 1652 pmu = NULL; 1653 while ((pmu = perf_pmus__scan(pmu))) { 1654 if (!strcmp(pmu->name, "cpu")) { 1655 /* Skip as above. */ 1656 continue; 1657 } 1658 if (perf_pmu__caps_parse(pmu) <= 0) 1659 continue; 1660 ret = __write_pmu_caps(ff, pmu, true); 1661 if (ret < 0) 1662 return ret; 1663 } 1664 return 0; 1665 } 1666 1667 struct cpu_domain_map **build_cpu_domain_map(u32 *schedstat_version, u32 *max_sched_domains, u32 nr) 1668 { 1669 char dname[DNAME_LEN], cpumask[MAX_NR_CPUS]; 1670 struct domain_info *domain_info; 1671 struct cpu_domain_map **cd_map; 1672 char cpulist[MAX_NR_CPUS]; 1673 char *line = NULL; 1674 u32 cpu, domain; 1675 u32 dcount = 0; 1676 size_t len; 1677 FILE *fp; 1678 1679 fp = fopen("/proc/schedstat", "r"); 1680 if (!fp) { 1681 pr_err("Failed to open /proc/schedstat\n"); 1682 return NULL; 1683 } 1684 1685 cd_map = zalloc(sizeof(*cd_map) * nr); 1686 if (!cd_map) 1687 goto out; 1688 1689 while (getline(&line, &len, fp) > 0) { 1690 int retval; 1691 1692 if (strncmp(line, "version", 7) == 0) { 1693 retval = sscanf(line, "version %d\n", schedstat_version); 1694 if (retval != 1) 1695 continue; 1696 1697 } else if (strncmp(line, "cpu", 3) == 0) { 1698 retval = sscanf(line, "cpu%u %*s", &cpu); 1699 if (retval == 1) { 1700 cd_map[cpu] = zalloc(sizeof(*cd_map[cpu])); 1701 if (!cd_map[cpu]) 1702 goto out_free_line; 1703 cd_map[cpu]->cpu = cpu; 1704 } else 1705 continue; 1706 1707 dcount = 0; 1708 } else if (strncmp(line, "domain", 6) == 0) { 1709 struct domain_info **temp_domains; 1710 1711 dcount++; 1712 temp_domains = realloc(cd_map[cpu]->domains, dcount * sizeof(domain_info)); 1713 if (!temp_domains) 1714 goto out_free_line; 1715 else 1716 cd_map[cpu]->domains = temp_domains; 1717 1718 domain_info = zalloc(sizeof(*domain_info)); 1719 if (!domain_info) 1720 goto out_free_line; 1721 1722 cd_map[cpu]->domains[dcount - 1] = domain_info; 1723 1724 if (*schedstat_version >= 17) { 1725 retval = sscanf(line, "domain%u %s %s %*s", &domain, dname, 1726 cpumask); 1727 if (retval != 3) 1728 continue; 1729 1730 domain_info->dname = strdup(dname); 1731 if (!domain_info->dname) 1732 goto out_free_line; 1733 } else { 1734 retval = sscanf(line, "domain%u %s %*s", &domain, cpumask); 1735 if (retval != 2) 1736 continue; 1737 } 1738 1739 domain_info->domain = domain; 1740 if (domain > *max_sched_domains) 1741 *max_sched_domains = domain; 1742 1743 domain_info->cpumask = strdup(cpumask); 1744 if (!domain_info->cpumask) 1745 goto out_free_line; 1746 1747 cpumask_to_cpulist(cpumask, cpulist); 1748 domain_info->cpulist = strdup(cpulist); 1749 if (!domain_info->cpulist) 1750 goto out_free_line; 1751 1752 cd_map[cpu]->nr_domains = dcount; 1753 } 1754 } 1755 1756 out_free_line: 1757 free(line); 1758 out: 1759 fclose(fp); 1760 return cd_map; 1761 } 1762 1763 static int write_cpu_domain_info(struct feat_fd *ff, 1764 struct evlist *evlist __maybe_unused) 1765 { 1766 u32 max_sched_domains = 0, schedstat_version = 0; 1767 struct cpu_domain_map **cd_map; 1768 u32 i, j, nr, ret; 1769 1770 nr = cpu__max_present_cpu().cpu; 1771 1772 cd_map = build_cpu_domain_map(&schedstat_version, &max_sched_domains, nr); 1773 if (!cd_map) 1774 return -1; 1775 1776 ret = do_write(ff, &schedstat_version, sizeof(u32)); 1777 if (ret < 0) 1778 goto out; 1779 1780 max_sched_domains += 1; 1781 ret = do_write(ff, &max_sched_domains, sizeof(u32)); 1782 if (ret < 0) 1783 goto out; 1784 1785 for (i = 0; i < nr; i++) { 1786 if (!cd_map[i]) 1787 continue; 1788 1789 ret = do_write(ff, &cd_map[i]->cpu, sizeof(u32)); 1790 if (ret < 0) 1791 goto out; 1792 1793 ret = do_write(ff, &cd_map[i]->nr_domains, sizeof(u32)); 1794 if (ret < 0) 1795 goto out; 1796 1797 for (j = 0; j < cd_map[i]->nr_domains; j++) { 1798 ret = do_write(ff, &cd_map[i]->domains[j]->domain, sizeof(u32)); 1799 if (ret < 0) 1800 goto out; 1801 if (schedstat_version >= 17) { 1802 ret = do_write_string(ff, cd_map[i]->domains[j]->dname); 1803 if (ret < 0) 1804 goto out; 1805 } 1806 1807 ret = do_write_string(ff, cd_map[i]->domains[j]->cpumask); 1808 if (ret < 0) 1809 goto out; 1810 1811 ret = do_write_string(ff, cd_map[i]->domains[j]->cpulist); 1812 if (ret < 0) 1813 goto out; 1814 } 1815 } 1816 1817 out: 1818 free_cpu_domain_info(cd_map, schedstat_version, nr); 1819 return ret; 1820 } 1821 1822 static void print_hostname(struct feat_fd *ff, FILE *fp) 1823 { 1824 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname); 1825 } 1826 1827 static void print_osrelease(struct feat_fd *ff, FILE *fp) 1828 { 1829 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); 1830 } 1831 1832 static void print_arch(struct feat_fd *ff, FILE *fp) 1833 { 1834 fprintf(fp, "# arch : %s\n", ff->ph->env.arch); 1835 } 1836 1837 static void print_e_machine(struct feat_fd *ff, FILE *fp) 1838 { 1839 fprintf(fp, "# e_machine : %u\n", ff->ph->env.e_machine); 1840 fprintf(fp, "# e_flags : %u\n", ff->ph->env.e_flags); 1841 } 1842 1843 static void print_cpudesc(struct feat_fd *ff, FILE *fp) 1844 { 1845 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); 1846 } 1847 1848 static void print_nrcpus(struct feat_fd *ff, FILE *fp) 1849 { 1850 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); 1851 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); 1852 } 1853 1854 static void print_version(struct feat_fd *ff, FILE *fp) 1855 { 1856 fprintf(fp, "# perf version : %s\n", ff->ph->env.version); 1857 } 1858 1859 static void print_cmdline(struct feat_fd *ff, FILE *fp) 1860 { 1861 int nr, i; 1862 1863 nr = ff->ph->env.nr_cmdline; 1864 1865 fprintf(fp, "# cmdline : "); 1866 1867 for (i = 0; i < nr; i++) { 1868 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); 1869 if (!argv_i) { 1870 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); 1871 } else { 1872 char *mem = argv_i; 1873 do { 1874 char *quote = strchr(argv_i, '\''); 1875 if (!quote) 1876 break; 1877 *quote++ = '\0'; 1878 fprintf(fp, "%s\\\'", argv_i); 1879 argv_i = quote; 1880 } while (1); 1881 fprintf(fp, "%s ", argv_i); 1882 free(mem); 1883 } 1884 } 1885 fputc('\n', fp); 1886 } 1887 1888 static void print_cpu_topology(struct feat_fd *ff, FILE *fp) 1889 { 1890 struct perf_header *ph = ff->ph; 1891 int cpu_nr = ph->env.nr_cpus_avail; 1892 int nr, i; 1893 char *str; 1894 1895 nr = ph->env.nr_sibling_cores; 1896 str = ph->env.sibling_cores; 1897 1898 for (i = 0; i < nr; i++) { 1899 fprintf(fp, "# sibling sockets : %s\n", str); 1900 str += strlen(str) + 1; 1901 } 1902 1903 if (ph->env.nr_sibling_dies) { 1904 nr = ph->env.nr_sibling_dies; 1905 str = ph->env.sibling_dies; 1906 1907 for (i = 0; i < nr; i++) { 1908 fprintf(fp, "# sibling dies : %s\n", str); 1909 str += strlen(str) + 1; 1910 } 1911 } 1912 1913 nr = ph->env.nr_sibling_threads; 1914 str = ph->env.sibling_threads; 1915 1916 for (i = 0; i < nr; i++) { 1917 fprintf(fp, "# sibling threads : %s\n", str); 1918 str += strlen(str) + 1; 1919 } 1920 1921 if (ph->env.nr_sibling_dies) { 1922 if (ph->env.cpu != NULL) { 1923 for (i = 0; i < cpu_nr; i++) 1924 fprintf(fp, "# CPU %d: Core ID %d, " 1925 "Die ID %d, Socket ID %d\n", 1926 i, ph->env.cpu[i].core_id, 1927 ph->env.cpu[i].die_id, 1928 ph->env.cpu[i].socket_id); 1929 } else 1930 fprintf(fp, "# Core ID, Die ID and Socket ID " 1931 "information is not available\n"); 1932 } else { 1933 if (ph->env.cpu != NULL) { 1934 for (i = 0; i < cpu_nr; i++) 1935 fprintf(fp, "# CPU %d: Core ID %d, " 1936 "Socket ID %d\n", 1937 i, ph->env.cpu[i].core_id, 1938 ph->env.cpu[i].socket_id); 1939 } else 1940 fprintf(fp, "# Core ID and Socket ID " 1941 "information is not available\n"); 1942 } 1943 } 1944 1945 static void print_clockid(struct feat_fd *ff, FILE *fp) 1946 { 1947 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n", 1948 ff->ph->env.clock.clockid_res_ns * 1000); 1949 } 1950 1951 static void print_clock_data(struct feat_fd *ff, FILE *fp) 1952 { 1953 struct timespec clockid_ns; 1954 char tstr[64], date[64]; 1955 struct timeval tod_ns; 1956 clockid_t clockid; 1957 struct tm ltime; 1958 u64 ref; 1959 1960 if (!ff->ph->env.clock.enabled) { 1961 fprintf(fp, "# reference time disabled\n"); 1962 return; 1963 } 1964 1965 /* Compute TOD time. */ 1966 ref = ff->ph->env.clock.tod_ns; 1967 tod_ns.tv_sec = ref / NSEC_PER_SEC; 1968 ref -= tod_ns.tv_sec * NSEC_PER_SEC; 1969 tod_ns.tv_usec = ref / NSEC_PER_USEC; 1970 1971 /* Compute clockid time. */ 1972 ref = ff->ph->env.clock.clockid_ns; 1973 clockid_ns.tv_sec = ref / NSEC_PER_SEC; 1974 ref -= clockid_ns.tv_sec * NSEC_PER_SEC; 1975 clockid_ns.tv_nsec = ref; 1976 1977 clockid = ff->ph->env.clock.clockid; 1978 1979 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL) 1980 snprintf(tstr, sizeof(tstr), "<error>"); 1981 else { 1982 strftime(date, sizeof(date), "%F %T", <ime); 1983 scnprintf(tstr, sizeof(tstr), "%s.%06d", 1984 date, (int) tod_ns.tv_usec); 1985 } 1986 1987 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid); 1988 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n", 1989 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec, 1990 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec, 1991 clockid_name(clockid)); 1992 } 1993 1994 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp) 1995 { 1996 int i; 1997 struct hybrid_node *n; 1998 1999 fprintf(fp, "# hybrid cpu system:\n"); 2000 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) { 2001 n = &ff->ph->env.hybrid_nodes[i]; 2002 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus); 2003 } 2004 } 2005 2006 static void print_dir_format(struct feat_fd *ff, FILE *fp) 2007 { 2008 struct perf_session *session; 2009 struct perf_data *data; 2010 2011 session = container_of(ff->ph, struct perf_session, header); 2012 data = session->data; 2013 2014 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); 2015 } 2016 2017 static void print_bpf_prog_info(struct feat_fd *ff __maybe_unused, FILE *fp) 2018 { 2019 #ifdef HAVE_LIBBPF_SUPPORT 2020 struct perf_env *env = &ff->ph->env; 2021 struct rb_root *root; 2022 struct rb_node *next; 2023 2024 down_read(&env->bpf_progs.lock); 2025 2026 root = &env->bpf_progs.infos; 2027 next = rb_first(root); 2028 2029 if (!next) 2030 fprintf(fp, "# bpf_prog_info empty\n"); 2031 2032 while (next) { 2033 struct bpf_prog_info_node *node; 2034 2035 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 2036 next = rb_next(&node->rb_node); 2037 2038 __bpf_event__print_bpf_prog_info(&node->info_linear->info, 2039 env, fp); 2040 } 2041 2042 up_read(&env->bpf_progs.lock); 2043 #else 2044 fprintf(fp, "# bpf_prog_info missing, no libbpf support\n"); 2045 #endif // HAVE_LIBBPF_SUPPORT 2046 } 2047 2048 static void print_bpf_btf(struct feat_fd *ff __maybe_unused, FILE *fp) 2049 { 2050 #ifdef HAVE_LIBBPF_SUPPORT 2051 struct perf_env *env = &ff->ph->env; 2052 struct rb_root *root; 2053 struct rb_node *next; 2054 2055 down_read(&env->bpf_progs.lock); 2056 2057 root = &env->bpf_progs.btfs; 2058 next = rb_first(root); 2059 2060 if (!next) 2061 printf("# btf info empty\n"); 2062 2063 while (next) { 2064 struct btf_node *node; 2065 2066 node = rb_entry(next, struct btf_node, rb_node); 2067 next = rb_next(&node->rb_node); 2068 fprintf(fp, "# btf info of id %u\n", node->id); 2069 } 2070 2071 up_read(&env->bpf_progs.lock); 2072 #else 2073 fprintf(fp, "# bpf btf data missing, no libbpf support\n"); 2074 #endif // HAVE_LIBBPF_SUPPORT 2075 } 2076 2077 static void free_event_desc(struct evsel *events) 2078 { 2079 struct evsel *evsel; 2080 2081 if (!events) 2082 return; 2083 2084 for (evsel = events; evsel->core.attr.size; evsel++) { 2085 zfree(&evsel->name); 2086 zfree(&evsel->core.id); 2087 } 2088 2089 free(events); 2090 } 2091 2092 static bool perf_attr_check(struct perf_event_attr *attr) 2093 { 2094 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) { 2095 pr_warning("Reserved bits are set unexpectedly. " 2096 "Please update perf tool.\n"); 2097 return false; 2098 } 2099 2100 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) { 2101 pr_warning("Unknown sample type (0x%llx) is detected. " 2102 "Please update perf tool.\n", 2103 attr->sample_type); 2104 return false; 2105 } 2106 2107 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) { 2108 pr_warning("Unknown read format (0x%llx) is detected. " 2109 "Please update perf tool.\n", 2110 attr->read_format); 2111 return false; 2112 } 2113 2114 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) && 2115 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) { 2116 pr_warning("Unknown branch sample type (0x%llx) is detected. " 2117 "Please update perf tool.\n", 2118 attr->branch_sample_type); 2119 2120 return false; 2121 } 2122 2123 return true; 2124 } 2125 2126 static struct evsel *read_event_desc(struct feat_fd *ff) 2127 { 2128 struct evsel *evsel, *events = NULL; 2129 u64 *id; 2130 void *buf = NULL; 2131 u32 nre, sz, nr, i, j; 2132 size_t msz; 2133 2134 /* number of events */ 2135 if (do_read_u32(ff, &nre)) 2136 goto error; 2137 2138 if (do_read_u32(ff, &sz)) 2139 goto error; 2140 2141 /* buffer to hold on file attr struct */ 2142 buf = malloc(sz); 2143 if (!buf) 2144 goto error; 2145 2146 /* the last event terminates with evsel->core.attr.size == 0: */ 2147 events = calloc(nre + 1, sizeof(*events)); 2148 if (!events) 2149 goto error; 2150 2151 msz = sizeof(evsel->core.attr); 2152 if (sz < msz) 2153 msz = sz; 2154 2155 for (i = 0, evsel = events; i < nre; evsel++, i++) { 2156 evsel->core.idx = i; 2157 2158 /* 2159 * must read entire on-file attr struct to 2160 * sync up with layout. 2161 */ 2162 if (__do_read(ff, buf, sz)) 2163 goto error; 2164 2165 if (ff->ph->needs_swap) 2166 perf_event__attr_swap(buf); 2167 2168 memcpy(&evsel->core.attr, buf, msz); 2169 2170 if (!perf_attr_check(&evsel->core.attr)) 2171 goto error; 2172 2173 if (do_read_u32(ff, &nr)) 2174 goto error; 2175 2176 if (ff->ph->needs_swap) 2177 evsel->needs_swap = true; 2178 2179 evsel->name = do_read_string(ff); 2180 if (!evsel->name) 2181 goto error; 2182 2183 if (!nr) 2184 continue; 2185 2186 id = calloc(nr, sizeof(*id)); 2187 if (!id) 2188 goto error; 2189 evsel->core.ids = nr; 2190 evsel->core.id = id; 2191 2192 for (j = 0 ; j < nr; j++) { 2193 if (do_read_u64(ff, id)) 2194 goto error; 2195 id++; 2196 } 2197 } 2198 out: 2199 free(buf); 2200 return events; 2201 error: 2202 free_event_desc(events); 2203 events = NULL; 2204 goto out; 2205 } 2206 2207 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val, 2208 void *priv __maybe_unused) 2209 { 2210 return fprintf(fp, ", %s = %s", name, val); 2211 } 2212 2213 static void print_event_desc(struct feat_fd *ff, FILE *fp) 2214 { 2215 struct evsel *evsel, *events; 2216 u32 j; 2217 u64 *id; 2218 2219 if (ff->events) 2220 events = ff->events; 2221 else 2222 events = read_event_desc(ff); 2223 2224 if (!events) { 2225 fprintf(fp, "# event desc: not available or unable to read\n"); 2226 return; 2227 } 2228 2229 for (evsel = events; evsel->core.attr.size; evsel++) { 2230 fprintf(fp, "# event : name = %s, ", evsel->name); 2231 2232 if (evsel->core.ids) { 2233 fprintf(fp, ", id = {"); 2234 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) { 2235 if (j) 2236 fputc(',', fp); 2237 fprintf(fp, " %"PRIu64, *id); 2238 } 2239 fprintf(fp, " }"); 2240 } 2241 2242 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); 2243 2244 fputc('\n', fp); 2245 } 2246 2247 free_event_desc(events); 2248 ff->events = NULL; 2249 } 2250 2251 static void print_total_mem(struct feat_fd *ff, FILE *fp) 2252 { 2253 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); 2254 } 2255 2256 static void print_numa_topology(struct feat_fd *ff, FILE *fp) 2257 { 2258 int i; 2259 struct numa_node *n; 2260 2261 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { 2262 n = &ff->ph->env.numa_nodes[i]; 2263 2264 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," 2265 " free = %"PRIu64" kB\n", 2266 n->node, n->mem_total, n->mem_free); 2267 2268 fprintf(fp, "# node%u cpu list : ", n->node); 2269 cpu_map__fprintf(n->map, fp); 2270 } 2271 } 2272 2273 static void print_cpuid(struct feat_fd *ff, FILE *fp) 2274 { 2275 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); 2276 } 2277 2278 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp) 2279 { 2280 fprintf(fp, "# contains samples with branch stack\n"); 2281 } 2282 2283 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp) 2284 { 2285 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); 2286 } 2287 2288 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp) 2289 { 2290 fprintf(fp, "# contains stat data\n"); 2291 } 2292 2293 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused) 2294 { 2295 int i; 2296 2297 fprintf(fp, "# CPU cache info:\n"); 2298 for (i = 0; i < ff->ph->env.caches_cnt; i++) { 2299 fprintf(fp, "# "); 2300 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); 2301 } 2302 } 2303 2304 static void print_cln_size(struct feat_fd *ff, FILE *fp) 2305 { 2306 fprintf(fp, "# cacheline size: %u\n", ff->ph->env.cln_size); 2307 } 2308 2309 static void print_compressed(struct feat_fd *ff, FILE *fp) 2310 { 2311 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n", 2312 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", 2313 ff->ph->env.comp_level, ff->ph->env.comp_ratio); 2314 } 2315 2316 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name) 2317 { 2318 const char *delimiter = ""; 2319 int i; 2320 2321 if (!nr_caps) { 2322 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name); 2323 return; 2324 } 2325 2326 fprintf(fp, "# %s pmu capabilities: ", pmu_name); 2327 for (i = 0; i < nr_caps; i++) { 2328 fprintf(fp, "%s%s", delimiter, caps[i]); 2329 delimiter = ", "; 2330 } 2331 2332 fprintf(fp, "\n"); 2333 } 2334 2335 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp) 2336 { 2337 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps, 2338 ff->ph->env.cpu_pmu_caps, (char *)"cpu"); 2339 } 2340 2341 static void print_pmu_caps(struct feat_fd *ff, FILE *fp) 2342 { 2343 struct perf_env *env = &ff->ph->env; 2344 struct pmu_caps *pmu_caps; 2345 2346 for (int i = 0; i < env->nr_pmus_with_caps; i++) { 2347 pmu_caps = &env->pmu_caps[i]; 2348 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps, 2349 pmu_caps->pmu_name); 2350 } 2351 2352 if (strcmp(perf_env__arch(env), "x86") == 0 && 2353 perf_env__has_pmu_mapping(env, "ibs_op")) { 2354 char *max_precise = perf_env__find_pmu_cap(env, "cpu", "max_precise"); 2355 2356 if (max_precise != NULL && atoi(max_precise) == 0) 2357 fprintf(fp, "# AMD systems uses ibs_op// PMU for some precise events, e.g.: cycles:p, see the 'perf list' man page for further details.\n"); 2358 } 2359 } 2360 2361 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp) 2362 { 2363 struct perf_env *env = &ff->ph->env; 2364 const char *delimiter = "# pmu mappings: "; 2365 char *str, *tmp; 2366 u32 pmu_num; 2367 u32 type; 2368 2369 pmu_num = env->nr_pmu_mappings; 2370 if (!pmu_num) { 2371 fprintf(fp, "# pmu mappings: not available\n"); 2372 return; 2373 } 2374 2375 str = env->pmu_mappings; 2376 2377 while (pmu_num) { 2378 type = strtoul(str, &tmp, 0); 2379 if (*tmp != ':') 2380 goto error; 2381 2382 str = tmp + 1; 2383 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); 2384 2385 delimiter = ", "; 2386 str += strlen(str) + 1; 2387 pmu_num--; 2388 } 2389 2390 fprintf(fp, "\n"); 2391 2392 if (!pmu_num) 2393 return; 2394 error: 2395 fprintf(fp, "# pmu mappings: unable to read\n"); 2396 } 2397 2398 static void print_group_desc(struct feat_fd *ff, FILE *fp) 2399 { 2400 struct perf_session *session; 2401 struct evsel *evsel; 2402 u32 nr = 0; 2403 2404 session = container_of(ff->ph, struct perf_session, header); 2405 2406 evlist__for_each_entry(session->evlist, evsel) { 2407 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { 2408 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel)); 2409 2410 nr = evsel->core.nr_members - 1; 2411 } else if (nr) { 2412 fprintf(fp, ",%s", evsel__name(evsel)); 2413 2414 if (--nr == 0) 2415 fprintf(fp, "}\n"); 2416 } 2417 } 2418 } 2419 2420 static void print_sample_time(struct feat_fd *ff, FILE *fp) 2421 { 2422 struct perf_session *session; 2423 char time_buf[32]; 2424 double d; 2425 2426 session = container_of(ff->ph, struct perf_session, header); 2427 2428 timestamp__scnprintf_usec(session->evlist->first_sample_time, 2429 time_buf, sizeof(time_buf)); 2430 fprintf(fp, "# time of first sample : %s\n", time_buf); 2431 2432 timestamp__scnprintf_usec(session->evlist->last_sample_time, 2433 time_buf, sizeof(time_buf)); 2434 fprintf(fp, "# time of last sample : %s\n", time_buf); 2435 2436 d = (double)(session->evlist->last_sample_time - 2437 session->evlist->first_sample_time) / NSEC_PER_MSEC; 2438 2439 fprintf(fp, "# sample duration : %10.3f ms\n", d); 2440 } 2441 2442 static void memory_node__fprintf(struct memory_node *n, 2443 unsigned long long bsize, FILE *fp) 2444 { 2445 char buf_map[100], buf_size[50]; 2446 unsigned long long size; 2447 2448 size = bsize * bitmap_weight(n->set, n->size); 2449 unit_number__scnprintf(buf_size, 50, size); 2450 2451 bitmap_scnprintf(n->set, n->size, buf_map, 100); 2452 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); 2453 } 2454 2455 static void print_mem_topology(struct feat_fd *ff, FILE *fp) 2456 { 2457 struct perf_env *env = &ff->ph->env; 2458 struct memory_node *nodes; 2459 int i, nr; 2460 2461 nodes = env->memory_nodes; 2462 nr = env->nr_memory_nodes; 2463 2464 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", 2465 nr, env->memory_bsize); 2466 2467 for (i = 0; i < nr; i++) { 2468 memory_node__fprintf(&nodes[i], env->memory_bsize, fp); 2469 } 2470 } 2471 2472 static void print_cpu_domain_info(struct feat_fd *ff, FILE *fp) 2473 { 2474 struct cpu_domain_map **cd_map = ff->ph->env.cpu_domain; 2475 u32 nr = ff->ph->env.nr_cpus_avail; 2476 struct domain_info *d_info; 2477 u32 i, j; 2478 2479 fprintf(fp, "# schedstat version : %u\n", ff->ph->env.schedstat_version); 2480 fprintf(fp, "# Maximum sched domains : %u\n", ff->ph->env.max_sched_domains); 2481 2482 for (i = 0; i < nr; i++) { 2483 if (!cd_map[i]) 2484 continue; 2485 2486 fprintf(fp, "# cpu : %u\n", cd_map[i]->cpu); 2487 fprintf(fp, "# nr_domains : %u\n", cd_map[i]->nr_domains); 2488 2489 for (j = 0; j < cd_map[i]->nr_domains; j++) { 2490 d_info = cd_map[i]->domains[j]; 2491 if (!d_info) 2492 continue; 2493 2494 fprintf(fp, "# Domain : %u\n", d_info->domain); 2495 2496 if (ff->ph->env.schedstat_version >= 17) 2497 fprintf(fp, "# Domain name : %s\n", d_info->dname); 2498 2499 fprintf(fp, "# Domain cpu map : %s\n", d_info->cpumask); 2500 fprintf(fp, "# Domain cpu list : %s\n", d_info->cpulist); 2501 } 2502 } 2503 } 2504 2505 static int __event_process_build_id(struct perf_record_header_build_id *bev, 2506 char *filename, 2507 struct perf_session *session) 2508 { 2509 int err = -1; 2510 struct machine *machine; 2511 u16 cpumode; 2512 struct dso *dso; 2513 enum dso_space_type dso_space; 2514 2515 machine = perf_session__findnew_machine(session, bev->pid); 2516 if (!machine) 2517 goto out; 2518 2519 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 2520 2521 switch (cpumode) { 2522 case PERF_RECORD_MISC_KERNEL: 2523 dso_space = DSO_SPACE__KERNEL; 2524 break; 2525 case PERF_RECORD_MISC_GUEST_KERNEL: 2526 dso_space = DSO_SPACE__KERNEL_GUEST; 2527 break; 2528 case PERF_RECORD_MISC_USER: 2529 case PERF_RECORD_MISC_GUEST_USER: 2530 dso_space = DSO_SPACE__USER; 2531 break; 2532 default: 2533 goto out; 2534 } 2535 2536 dso = machine__findnew_dso(machine, filename); 2537 if (dso != NULL) { 2538 char sbuild_id[SBUILD_ID_SIZE]; 2539 struct build_id bid; 2540 size_t size = BUILD_ID_SIZE; 2541 2542 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE) 2543 size = bev->size; 2544 2545 build_id__init(&bid, bev->data, size); 2546 dso__set_build_id(dso, &bid); 2547 dso__set_header_build_id(dso, true); 2548 2549 if (dso_space != DSO_SPACE__USER) { 2550 struct kmod_path m = { .name = NULL, }; 2551 2552 if (!kmod_path__parse_name(&m, filename) && m.kmod) 2553 dso__set_module_info(dso, &m, machine); 2554 2555 dso__set_kernel(dso, dso_space); 2556 free(m.name); 2557 } 2558 2559 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 2560 pr_debug("build id event received for %s: %s [%zu]\n", 2561 dso__long_name(dso), sbuild_id, size); 2562 dso__put(dso); 2563 } 2564 2565 err = 0; 2566 out: 2567 return err; 2568 } 2569 2570 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, 2571 int input, u64 offset, u64 size) 2572 { 2573 struct perf_session *session = container_of(header, struct perf_session, header); 2574 struct { 2575 struct perf_event_header header; 2576 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; 2577 char filename[0]; 2578 } old_bev; 2579 struct perf_record_header_build_id bev; 2580 char filename[PATH_MAX]; 2581 u64 limit = offset + size; 2582 2583 while (offset < limit) { 2584 ssize_t len; 2585 2586 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) 2587 return -1; 2588 2589 if (header->needs_swap) 2590 perf_event_header__bswap(&old_bev.header); 2591 2592 len = old_bev.header.size - sizeof(old_bev); 2593 if (len < 0 || len >= PATH_MAX) { 2594 pr_warning("invalid build_id filename length %zd\n", len); 2595 return -1; 2596 } 2597 2598 if (readn(input, filename, len) != len) 2599 return -1; 2600 2601 bev.header = old_bev.header; 2602 2603 /* 2604 * As the pid is the missing value, we need to fill 2605 * it properly. The header.misc value give us nice hint. 2606 */ 2607 bev.pid = HOST_KERNEL_ID; 2608 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || 2609 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) 2610 bev.pid = DEFAULT_GUEST_KERNEL_ID; 2611 2612 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); 2613 __event_process_build_id(&bev, filename, session); 2614 2615 offset += bev.header.size; 2616 } 2617 2618 return 0; 2619 } 2620 2621 static int perf_header__read_build_ids(struct perf_header *header, 2622 int input, u64 offset, u64 size) 2623 { 2624 struct perf_session *session = container_of(header, struct perf_session, header); 2625 struct perf_record_header_build_id bev; 2626 char filename[PATH_MAX]; 2627 u64 limit = offset + size, orig_offset = offset; 2628 int err = -1; 2629 2630 while (offset < limit) { 2631 ssize_t len; 2632 2633 if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) 2634 goto out; 2635 2636 if (header->needs_swap) 2637 perf_event_header__bswap(&bev.header); 2638 2639 len = bev.header.size - sizeof(bev); 2640 if (len < 0 || len >= PATH_MAX) { 2641 pr_warning("invalid build_id filename length %zd\n", len); 2642 goto out; 2643 } 2644 2645 if (readn(input, filename, len) != len) 2646 goto out; 2647 /* 2648 * The a1645ce1 changeset: 2649 * 2650 * "perf: 'perf kvm' tool for monitoring guest performance from host" 2651 * 2652 * Added a field to struct perf_record_header_build_id that broke the file 2653 * format. 2654 * 2655 * Since the kernel build-id is the first entry, process the 2656 * table using the old format if the well known 2657 * '[kernel.kallsyms]' string for the kernel build-id has the 2658 * first 4 characters chopped off (where the pid_t sits). 2659 */ 2660 if (memcmp(filename, "nel.kallsyms]", 13) == 0) { 2661 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) 2662 return -1; 2663 return perf_header__read_build_ids_abi_quirk(header, input, offset, size); 2664 } 2665 2666 __event_process_build_id(&bev, filename, session); 2667 2668 offset += bev.header.size; 2669 } 2670 err = 0; 2671 out: 2672 return err; 2673 } 2674 2675 /* Macro for features that simply need to read and store a string. */ 2676 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ 2677 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ 2678 {\ 2679 free(ff->ph->env.__feat_env); \ 2680 ff->ph->env.__feat_env = do_read_string(ff); \ 2681 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ 2682 } 2683 2684 FEAT_PROCESS_STR_FUN(hostname, hostname); 2685 FEAT_PROCESS_STR_FUN(osrelease, os_release); 2686 FEAT_PROCESS_STR_FUN(version, version); 2687 FEAT_PROCESS_STR_FUN(arch, arch); 2688 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); 2689 FEAT_PROCESS_STR_FUN(cpuid, cpuid); 2690 2691 static int process_e_machine(struct feat_fd *ff, void *data __maybe_unused) 2692 { 2693 int ret; 2694 2695 ret = do_read_u32(ff, &ff->ph->env.e_machine); 2696 if (ret) 2697 return ret; 2698 2699 return do_read_u32(ff, &ff->ph->env.e_flags); 2700 } 2701 2702 static int process_tracing_data(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 2703 { 2704 #ifdef HAVE_LIBTRACEEVENT 2705 ssize_t ret = trace_report(ff->fd, data, false); 2706 2707 return ret < 0 ? -1 : 0; 2708 #else 2709 pr_err("ERROR: Trying to read tracing data without libtraceevent support.\n"); 2710 return -1; 2711 #endif 2712 } 2713 2714 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused) 2715 { 2716 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size)) 2717 pr_debug("Failed to read buildids, continuing...\n"); 2718 return 0; 2719 } 2720 2721 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused) 2722 { 2723 struct perf_env *env = &ff->ph->env; 2724 int ret; 2725 u32 nr_cpus_avail, nr_cpus_online; 2726 2727 ret = do_read_u32(ff, &nr_cpus_avail); 2728 if (ret) 2729 return ret; 2730 2731 ret = do_read_u32(ff, &nr_cpus_online); 2732 if (ret) 2733 return ret; 2734 2735 if (nr_cpus_online > nr_cpus_avail) { 2736 pr_err("Invalid HEADER_NRCPUS: nr_cpus_online (%u) > nr_cpus_avail (%u)\n", 2737 nr_cpus_online, nr_cpus_avail); 2738 return -1; 2739 } 2740 2741 env->nr_cpus_avail = (int)nr_cpus_avail; 2742 env->nr_cpus_online = (int)nr_cpus_online; 2743 return 0; 2744 } 2745 2746 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused) 2747 { 2748 struct perf_env *env = &ff->ph->env; 2749 u64 total_mem; 2750 int ret; 2751 2752 ret = do_read_u64(ff, &total_mem); 2753 if (ret) 2754 return -1; 2755 env->total_mem = (unsigned long long)total_mem; 2756 return 0; 2757 } 2758 2759 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx) 2760 { 2761 struct evsel *evsel; 2762 2763 evlist__for_each_entry(evlist, evsel) { 2764 if (evsel->core.idx == idx) 2765 return evsel; 2766 } 2767 2768 return NULL; 2769 } 2770 2771 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event) 2772 { 2773 struct evsel *evsel; 2774 2775 if (!event->name) 2776 return; 2777 2778 evsel = evlist__find_by_index(evlist, event->core.idx); 2779 if (!evsel) 2780 return; 2781 2782 if (evsel->name) 2783 return; 2784 2785 evsel->name = strdup(event->name); 2786 } 2787 2788 static int 2789 process_event_desc(struct feat_fd *ff, void *data __maybe_unused) 2790 { 2791 struct perf_session *session; 2792 struct evsel *evsel, *events = read_event_desc(ff); 2793 2794 if (!events) 2795 return 0; 2796 2797 session = container_of(ff->ph, struct perf_session, header); 2798 2799 if (session->data->is_pipe) { 2800 /* Save events for reading later by print_event_desc, 2801 * since they can't be read again in pipe mode. */ 2802 ff->events = events; 2803 } 2804 2805 for (evsel = events; evsel->core.attr.size; evsel++) 2806 evlist__set_event_name(session->evlist, evsel); 2807 2808 if (!session->data->is_pipe) 2809 free_event_desc(events); 2810 2811 return 0; 2812 } 2813 2814 /* 2815 * Some arbitrary max for the number of command line arguments, 2816 * Wildcards can expand and end up with tons of command line args. 2817 */ 2818 #define MAX_CMDLINE_NR 1048576 2819 2820 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) 2821 { 2822 struct perf_env *env = &ff->ph->env; 2823 char *str, *cmdline = NULL, **argv = NULL; 2824 u32 nr, i, len = 0; 2825 2826 if (do_read_u32(ff, &nr)) 2827 return -1; 2828 2829 if (nr > MAX_CMDLINE_NR) 2830 return -1; 2831 2832 env->nr_cmdline = nr; 2833 2834 cmdline = zalloc(ff->size + nr + 1); 2835 if (!cmdline) 2836 return -1; 2837 2838 argv = calloc(nr + 1, sizeof(char *)); 2839 if (!argv) 2840 goto error; 2841 2842 for (i = 0; i < nr; i++) { 2843 str = do_read_string(ff); 2844 if (!str) 2845 goto error; 2846 2847 argv[i] = cmdline + len; 2848 memcpy(argv[i], str, strlen(str) + 1); 2849 len += strlen(str) + 1; 2850 free(str); 2851 } 2852 env->cmdline = cmdline; 2853 env->cmdline_argv = (const char **) argv; 2854 return 0; 2855 2856 error: 2857 free(argv); 2858 free(cmdline); 2859 return -1; 2860 } 2861 2862 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused) 2863 { 2864 u32 nr, i; 2865 char *str = NULL; 2866 struct strbuf sb; 2867 struct perf_env *env = &ff->ph->env; 2868 int cpu_nr = env->nr_cpus_avail; 2869 u64 size = 0; 2870 2871 if (cpu_nr == 0) { 2872 pr_err("Invalid HEADER_CPU_TOPOLOGY: missing HEADER_NRCPUS\n"); 2873 return -1; 2874 } 2875 2876 env->cpu = calloc(cpu_nr, sizeof(*env->cpu)); 2877 if (!env->cpu) 2878 return -1; 2879 2880 if (do_read_u32(ff, &nr)) 2881 goto free_cpu; 2882 2883 if (nr > (u32)cpu_nr) { 2884 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_cores (%u) > nr_cpus_avail (%d)\n", 2885 nr, cpu_nr); 2886 goto free_cpu; 2887 } 2888 2889 env->nr_sibling_cores = nr; 2890 size += sizeof(u32); 2891 if (strbuf_init(&sb, 128) < 0) 2892 goto free_cpu; 2893 2894 for (i = 0; i < nr; i++) { 2895 str = do_read_string(ff); 2896 if (!str) 2897 goto error; 2898 2899 /* include a NULL character at the end */ 2900 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2901 goto error; 2902 size += string_size(str); 2903 zfree(&str); 2904 } 2905 env->sibling_cores = strbuf_detach(&sb, NULL); 2906 2907 if (do_read_u32(ff, &nr)) 2908 goto free_cpu; 2909 2910 if (nr > (u32)cpu_nr) { 2911 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_threads (%u) > nr_cpus_avail (%d)\n", 2912 nr, cpu_nr); 2913 goto free_cpu; 2914 } 2915 2916 env->nr_sibling_threads = nr; 2917 size += sizeof(u32); 2918 2919 for (i = 0; i < nr; i++) { 2920 str = do_read_string(ff); 2921 if (!str) 2922 goto error; 2923 2924 /* include a NULL character at the end */ 2925 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2926 goto error; 2927 size += string_size(str); 2928 zfree(&str); 2929 } 2930 env->sibling_threads = strbuf_detach(&sb, NULL); 2931 2932 /* 2933 * The header may be from old perf, 2934 * which doesn't include core id and socket id information. 2935 */ 2936 if (ff->size <= size) { 2937 zfree(&env->cpu); 2938 return 0; 2939 } 2940 2941 for (i = 0; i < (u32)cpu_nr; i++) { 2942 if (do_read_u32(ff, &nr)) 2943 goto free_cpu; 2944 2945 env->cpu[i].core_id = nr; 2946 size += sizeof(u32); 2947 2948 if (do_read_u32(ff, &nr)) 2949 goto free_cpu; 2950 2951 env->cpu[i].socket_id = nr; 2952 size += sizeof(u32); 2953 } 2954 2955 /* 2956 * The header may be from old perf, 2957 * which doesn't include die information. 2958 */ 2959 if (ff->size <= size) 2960 return 0; 2961 2962 if (do_read_u32(ff, &nr)) 2963 goto free_cpu; 2964 2965 if (nr > (u32)cpu_nr) { 2966 pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_dies (%u) > nr_cpus_avail (%d)\n", 2967 nr, cpu_nr); 2968 goto free_cpu; 2969 } 2970 2971 env->nr_sibling_dies = nr; 2972 size += sizeof(u32); 2973 2974 for (i = 0; i < nr; i++) { 2975 str = do_read_string(ff); 2976 if (!str) 2977 goto error; 2978 2979 /* include a NULL character at the end */ 2980 if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 2981 goto error; 2982 size += string_size(str); 2983 zfree(&str); 2984 } 2985 env->sibling_dies = strbuf_detach(&sb, NULL); 2986 2987 for (i = 0; i < (u32)cpu_nr; i++) { 2988 if (do_read_u32(ff, &nr)) 2989 goto free_cpu; 2990 2991 env->cpu[i].die_id = nr; 2992 } 2993 2994 return 0; 2995 2996 error: 2997 strbuf_release(&sb); 2998 zfree(&str); 2999 free_cpu: 3000 zfree(&env->cpu); 3001 return -1; 3002 } 3003 3004 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused) 3005 { 3006 struct perf_env *env = &ff->ph->env; 3007 struct numa_node *nodes, *n; 3008 u32 nr, i; 3009 char *str; 3010 3011 /* nr nodes */ 3012 if (do_read_u32(ff, &nr)) 3013 return -1; 3014 3015 if (nr > MAX_NUMA_NODES) { 3016 pr_err("Invalid HEADER_NUMA_TOPOLOGY: nr_nodes (%u) > %u\n", 3017 nr, MAX_NUMA_NODES); 3018 return -1; 3019 } 3020 3021 if (ff->size < sizeof(u32) + nr * (sizeof(u32) + 2 * sizeof(u64))) { 3022 pr_err("Invalid HEADER_NUMA_TOPOLOGY: section too small (%zu) for %u nodes\n", 3023 ff->size, nr); 3024 return -1; 3025 } 3026 3027 nodes = calloc(nr, sizeof(*nodes)); 3028 if (!nodes) 3029 return -ENOMEM; 3030 3031 for (i = 0; i < nr; i++) { 3032 n = &nodes[i]; 3033 3034 /* node number */ 3035 if (do_read_u32(ff, &n->node)) 3036 goto error; 3037 3038 if (do_read_u64(ff, &n->mem_total)) 3039 goto error; 3040 3041 if (do_read_u64(ff, &n->mem_free)) 3042 goto error; 3043 3044 str = do_read_string(ff); 3045 if (!str) 3046 goto error; 3047 3048 n->map = perf_cpu_map__new(str); 3049 free(str); 3050 if (!n->map) 3051 goto error; 3052 } 3053 env->nr_numa_nodes = nr; 3054 env->numa_nodes = nodes; 3055 return 0; 3056 3057 error: 3058 free(nodes); 3059 return -1; 3060 } 3061 3062 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused) 3063 { 3064 struct perf_env *env = &ff->ph->env; 3065 char *name; 3066 u32 pmu_num; 3067 u32 type; 3068 struct strbuf sb; 3069 3070 if (do_read_u32(ff, &pmu_num)) 3071 return -1; 3072 3073 if (!pmu_num) { 3074 pr_debug("pmu mappings not available\n"); 3075 return 0; 3076 } 3077 3078 if (pmu_num > MAX_PMU_MAPPINGS) { 3079 pr_err("Invalid HEADER_PMU_MAPPINGS: pmu_num (%u) > %u\n", 3080 pmu_num, MAX_PMU_MAPPINGS); 3081 return -1; 3082 } 3083 3084 if (ff->size < sizeof(u32) + pmu_num * 2 * sizeof(u32)) { 3085 pr_err("Invalid HEADER_PMU_MAPPINGS: section too small (%zu) for %u PMUs\n", 3086 ff->size, pmu_num); 3087 return -1; 3088 } 3089 3090 env->nr_pmu_mappings = pmu_num; 3091 if (strbuf_init(&sb, 128) < 0) 3092 return -1; 3093 3094 while (pmu_num) { 3095 if (do_read_u32(ff, &type)) 3096 goto error; 3097 3098 name = do_read_string(ff); 3099 if (!name) 3100 goto error; 3101 3102 if (strbuf_addf(&sb, "%u:%s", type, name) < 0) 3103 goto error; 3104 /* include a NULL character at the end */ 3105 if (strbuf_add(&sb, "", 1) < 0) 3106 goto error; 3107 3108 if (!strcmp(name, "msr")) 3109 env->msr_pmu_type = type; 3110 3111 free(name); 3112 pmu_num--; 3113 } 3114 /* AMD may set it by evlist__has_amd_ibs() from perf_session__new() */ 3115 free(env->pmu_mappings); 3116 env->pmu_mappings = strbuf_detach(&sb, NULL); 3117 return 0; 3118 3119 error: 3120 strbuf_release(&sb); 3121 return -1; 3122 } 3123 3124 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused) 3125 { 3126 struct perf_env *env = &ff->ph->env; 3127 size_t ret = -1; 3128 u32 i, nr, nr_groups; 3129 struct perf_session *session; 3130 struct evsel *evsel, *leader = NULL; 3131 struct group_desc { 3132 char *name; 3133 u32 leader_idx; 3134 u32 nr_members; 3135 } *desc; 3136 3137 if (do_read_u32(ff, &nr_groups)) 3138 return -1; 3139 3140 if (!nr_groups) { 3141 pr_debug("group desc not available\n"); 3142 return 0; 3143 } 3144 3145 if (nr_groups > MAX_GROUP_DESC) { 3146 pr_err("Invalid HEADER_GROUP_DESC: nr_groups (%u) > %u\n", 3147 nr_groups, MAX_GROUP_DESC); 3148 return -1; 3149 } 3150 3151 if (ff->size < sizeof(u32) + nr_groups * 3 * sizeof(u32)) { 3152 pr_err("Invalid HEADER_GROUP_DESC: section too small (%zu) for %u groups\n", 3153 ff->size, nr_groups); 3154 return -1; 3155 } 3156 3157 env->nr_groups = nr_groups; 3158 3159 desc = calloc(nr_groups, sizeof(*desc)); 3160 if (!desc) 3161 return -1; 3162 3163 for (i = 0; i < nr_groups; i++) { 3164 desc[i].name = do_read_string(ff); 3165 if (!desc[i].name) 3166 goto out_free; 3167 3168 if (do_read_u32(ff, &desc[i].leader_idx)) 3169 goto out_free; 3170 3171 if (do_read_u32(ff, &desc[i].nr_members)) 3172 goto out_free; 3173 } 3174 3175 /* 3176 * Rebuild group relationship based on the group_desc 3177 */ 3178 session = container_of(ff->ph, struct perf_session, header); 3179 3180 i = nr = 0; 3181 evlist__for_each_entry(session->evlist, evsel) { 3182 if (i < nr_groups && evsel->core.idx == (int) desc[i].leader_idx) { 3183 evsel__set_leader(evsel, evsel); 3184 /* {anon_group} is a dummy name */ 3185 if (strcmp(desc[i].name, "{anon_group}")) { 3186 evsel->group_name = desc[i].name; 3187 desc[i].name = NULL; 3188 } 3189 evsel->core.nr_members = desc[i].nr_members; 3190 3191 if (i >= nr_groups || nr > 0) { 3192 pr_debug("invalid group desc\n"); 3193 goto out_free; 3194 } 3195 3196 leader = evsel; 3197 nr = evsel->core.nr_members - 1; 3198 i++; 3199 } else if (nr) { 3200 /* This is a group member */ 3201 evsel__set_leader(evsel, leader); 3202 3203 nr--; 3204 } 3205 } 3206 3207 if (i != nr_groups || nr != 0) { 3208 pr_debug("invalid group desc\n"); 3209 goto out_free; 3210 } 3211 3212 ret = 0; 3213 out_free: 3214 for (i = 0; i < nr_groups; i++) 3215 zfree(&desc[i].name); 3216 free(desc); 3217 3218 return ret; 3219 } 3220 3221 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused) 3222 { 3223 struct perf_session *session; 3224 int err; 3225 3226 session = container_of(ff->ph, struct perf_session, header); 3227 3228 err = auxtrace_index__process(ff->fd, ff->size, session, 3229 ff->ph->needs_swap); 3230 if (err < 0) 3231 pr_err("Failed to process auxtrace index\n"); 3232 return err; 3233 } 3234 3235 static int process_cache(struct feat_fd *ff, void *data __maybe_unused) 3236 { 3237 struct perf_env *env = &ff->ph->env; 3238 struct cpu_cache_level *caches; 3239 u32 cnt, i, version; 3240 3241 if (do_read_u32(ff, &version)) 3242 return -1; 3243 3244 if (version != 1) 3245 return -1; 3246 3247 if (do_read_u32(ff, &cnt)) 3248 return -1; 3249 3250 if (cnt > MAX_CACHE_ENTRIES) { 3251 pr_err("Invalid HEADER_CACHE: cnt (%u) > %u\n", 3252 cnt, MAX_CACHE_ENTRIES); 3253 return -1; 3254 } 3255 3256 if (ff->size < 2 * sizeof(u32) + cnt * 7 * sizeof(u32)) { 3257 pr_err("Invalid HEADER_CACHE: section too small (%zu) for %u entries\n", 3258 ff->size, cnt); 3259 return -1; 3260 } 3261 3262 caches = calloc(cnt, sizeof(*caches)); 3263 if (!caches) 3264 return -1; 3265 3266 for (i = 0; i < cnt; i++) { 3267 struct cpu_cache_level *c = &caches[i]; 3268 3269 #define _R(v) \ 3270 if (do_read_u32(ff, &c->v)) \ 3271 goto out_free_caches; \ 3272 3273 _R(level) 3274 _R(line_size) 3275 _R(sets) 3276 _R(ways) 3277 #undef _R 3278 3279 #define _R(v) \ 3280 c->v = do_read_string(ff); \ 3281 if (!c->v) \ 3282 goto out_free_caches; \ 3283 3284 _R(type) 3285 _R(size) 3286 _R(map) 3287 #undef _R 3288 } 3289 3290 env->caches = caches; 3291 env->caches_cnt = cnt; 3292 return 0; 3293 out_free_caches: 3294 for (i = 0; i < cnt; i++) { 3295 free(caches[i].type); 3296 free(caches[i].size); 3297 free(caches[i].map); 3298 } 3299 free(caches); 3300 return -1; 3301 } 3302 3303 static int process_cln_size(struct feat_fd *ff, void *data __maybe_unused) 3304 { 3305 struct perf_env *env = &ff->ph->env; 3306 3307 if (do_read_u32(ff, &env->cln_size)) 3308 return -1; 3309 3310 return 0; 3311 } 3312 3313 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused) 3314 { 3315 struct perf_session *session; 3316 u64 first_sample_time, last_sample_time; 3317 int ret; 3318 3319 session = container_of(ff->ph, struct perf_session, header); 3320 3321 ret = do_read_u64(ff, &first_sample_time); 3322 if (ret) 3323 return -1; 3324 3325 ret = do_read_u64(ff, &last_sample_time); 3326 if (ret) 3327 return -1; 3328 3329 session->evlist->first_sample_time = first_sample_time; 3330 session->evlist->last_sample_time = last_sample_time; 3331 return 0; 3332 } 3333 3334 static int process_mem_topology(struct feat_fd *ff, 3335 void *data __maybe_unused) 3336 { 3337 struct perf_env *env = &ff->ph->env; 3338 struct memory_node *nodes; 3339 u64 version, i, nr, bsize; 3340 int ret = -1; 3341 3342 if (do_read_u64(ff, &version)) 3343 return -1; 3344 3345 if (version != 1) 3346 return -1; 3347 3348 if (do_read_u64(ff, &bsize)) 3349 return -1; 3350 3351 if (do_read_u64(ff, &nr)) 3352 return -1; 3353 3354 if (nr > MAX_NUMA_NODES) { 3355 pr_err("Invalid HEADER_MEM_TOPOLOGY: nr_nodes (%llu) > %u\n", 3356 (unsigned long long)nr, MAX_NUMA_NODES); 3357 return -1; 3358 } 3359 3360 if (ff->size < 3 * sizeof(u64) + nr * 2 * sizeof(u64)) { 3361 pr_err("Invalid HEADER_MEM_TOPOLOGY: section too small (%zu) for %llu nodes\n", 3362 ff->size, (unsigned long long)nr); 3363 return -1; 3364 } 3365 3366 nodes = calloc(nr, sizeof(*nodes)); 3367 if (!nodes) 3368 return -1; 3369 3370 for (i = 0; i < nr; i++) { 3371 struct memory_node n; 3372 3373 #define _R(v) \ 3374 if (do_read_u64(ff, &n.v)) \ 3375 goto out; \ 3376 3377 _R(node) 3378 _R(size) 3379 3380 #undef _R 3381 3382 if (do_read_bitmap(ff, &n.set, &n.size)) 3383 goto out; 3384 3385 nodes[i] = n; 3386 } 3387 3388 env->memory_bsize = bsize; 3389 env->memory_nodes = nodes; 3390 env->nr_memory_nodes = nr; 3391 ret = 0; 3392 3393 out: 3394 if (ret) 3395 free(nodes); 3396 return ret; 3397 } 3398 3399 static int process_clockid(struct feat_fd *ff, 3400 void *data __maybe_unused) 3401 { 3402 struct perf_env *env = &ff->ph->env; 3403 3404 if (do_read_u64(ff, &env->clock.clockid_res_ns)) 3405 return -1; 3406 3407 return 0; 3408 } 3409 3410 static int process_clock_data(struct feat_fd *ff, 3411 void *_data __maybe_unused) 3412 { 3413 struct perf_env *env = &ff->ph->env; 3414 u32 data32; 3415 u64 data64; 3416 3417 /* version */ 3418 if (do_read_u32(ff, &data32)) 3419 return -1; 3420 3421 if (data32 != 1) 3422 return -1; 3423 3424 /* clockid */ 3425 if (do_read_u32(ff, &data32)) 3426 return -1; 3427 3428 env->clock.clockid = data32; 3429 3430 /* TOD ref time */ 3431 if (do_read_u64(ff, &data64)) 3432 return -1; 3433 3434 env->clock.tod_ns = data64; 3435 3436 /* clockid ref time */ 3437 if (do_read_u64(ff, &data64)) 3438 return -1; 3439 3440 env->clock.clockid_ns = data64; 3441 env->clock.enabled = true; 3442 return 0; 3443 } 3444 3445 static int process_hybrid_topology(struct feat_fd *ff, 3446 void *data __maybe_unused) 3447 { 3448 struct perf_env *env = &ff->ph->env; 3449 struct hybrid_node *nodes, *n; 3450 u32 nr, i; 3451 3452 /* nr nodes */ 3453 if (do_read_u32(ff, &nr)) 3454 return -1; 3455 3456 if (nr > MAX_PMU_MAPPINGS) { 3457 pr_err("Invalid HEADER_HYBRID_TOPOLOGY: nr_nodes (%u) > %u\n", 3458 nr, MAX_PMU_MAPPINGS); 3459 return -1; 3460 } 3461 3462 if (ff->size < sizeof(u32) + nr * 2 * sizeof(u32)) { 3463 pr_err("Invalid HEADER_HYBRID_TOPOLOGY: section too small (%zu) for %u nodes\n", 3464 ff->size, nr); 3465 return -1; 3466 } 3467 3468 nodes = calloc(nr, sizeof(*nodes)); 3469 if (!nodes) 3470 return -ENOMEM; 3471 3472 for (i = 0; i < nr; i++) { 3473 n = &nodes[i]; 3474 3475 n->pmu_name = do_read_string(ff); 3476 if (!n->pmu_name) 3477 goto error; 3478 3479 n->cpus = do_read_string(ff); 3480 if (!n->cpus) 3481 goto error; 3482 } 3483 3484 env->nr_hybrid_nodes = nr; 3485 env->hybrid_nodes = nodes; 3486 return 0; 3487 3488 error: 3489 for (i = 0; i < nr; i++) { 3490 free(nodes[i].pmu_name); 3491 free(nodes[i].cpus); 3492 } 3493 3494 free(nodes); 3495 return -1; 3496 } 3497 3498 static int process_dir_format(struct feat_fd *ff, 3499 void *_data __maybe_unused) 3500 { 3501 struct perf_session *session; 3502 struct perf_data *data; 3503 3504 session = container_of(ff->ph, struct perf_session, header); 3505 data = session->data; 3506 3507 if (WARN_ON(!perf_data__is_dir(data))) 3508 return -1; 3509 3510 return do_read_u64(ff, &data->dir.version); 3511 } 3512 3513 static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 3514 { 3515 #ifdef HAVE_LIBBPF_SUPPORT 3516 struct bpf_prog_info_node *info_node; 3517 struct perf_env *env = &ff->ph->env; 3518 struct perf_bpil *info_linear; 3519 u32 count, i; 3520 int err = -1; 3521 3522 if (ff->ph->needs_swap) { 3523 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n"); 3524 return 0; 3525 } 3526 3527 if (do_read_u32(ff, &count)) 3528 return -1; 3529 3530 if (count > MAX_BPF_PROGS) { 3531 pr_err("Invalid HEADER_BPF_PROG_INFO: count (%u) > %u\n", 3532 count, MAX_BPF_PROGS); 3533 return -1; 3534 } 3535 3536 if (ff->size < sizeof(u32) + count * (2 * sizeof(u32) + sizeof(u64))) { 3537 pr_err("Invalid HEADER_BPF_PROG_INFO: section too small (%zu) for %u entries\n", 3538 ff->size, count); 3539 return -1; 3540 } 3541 3542 down_write(&env->bpf_progs.lock); 3543 3544 for (i = 0; i < count; ++i) { 3545 u32 info_len, data_len; 3546 3547 info_linear = NULL; 3548 info_node = NULL; 3549 if (do_read_u32(ff, &info_len)) 3550 goto out; 3551 if (do_read_u32(ff, &data_len)) 3552 goto out; 3553 3554 if (info_len > sizeof(struct bpf_prog_info)) { 3555 pr_warning("detected invalid bpf_prog_info\n"); 3556 goto out; 3557 } 3558 3559 if (data_len > MAX_BPF_DATA_LEN) { 3560 pr_warning("Invalid HEADER_BPF_PROG_INFO: data_len (%u) too large\n", 3561 data_len); 3562 goto out; 3563 } 3564 3565 info_linear = malloc(sizeof(struct perf_bpil) + 3566 data_len); 3567 if (!info_linear) 3568 goto out; 3569 info_linear->info_len = sizeof(struct bpf_prog_info); 3570 info_linear->data_len = data_len; 3571 if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) 3572 goto out; 3573 if (__do_read(ff, &info_linear->info, info_len)) 3574 goto out; 3575 if (info_len < sizeof(struct bpf_prog_info)) 3576 memset(((void *)(&info_linear->info)) + info_len, 0, 3577 sizeof(struct bpf_prog_info) - info_len); 3578 3579 if (__do_read(ff, info_linear->data, data_len)) 3580 goto out; 3581 3582 info_node = malloc(sizeof(struct bpf_prog_info_node)); 3583 if (!info_node) 3584 goto out; 3585 3586 /* after reading from file, translate offset to address */ 3587 bpil_offs_to_addr(info_linear); 3588 info_node->info_linear = info_linear; 3589 info_node->metadata = NULL; 3590 if (!__perf_env__insert_bpf_prog_info(env, info_node)) { 3591 free(info_linear); 3592 free(info_node); 3593 } 3594 } 3595 3596 up_write(&env->bpf_progs.lock); 3597 return 0; 3598 out: 3599 free(info_linear); 3600 free(info_node); 3601 up_write(&env->bpf_progs.lock); 3602 return err; 3603 #else 3604 pr_err("ERROR: Trying to read bpf_prog_info without libbpf support.\n"); 3605 return -1; 3606 #endif // HAVE_LIBBPF_SUPPORT 3607 } 3608 3609 static int process_bpf_btf(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) 3610 { 3611 #ifdef HAVE_LIBBPF_SUPPORT 3612 struct perf_env *env = &ff->ph->env; 3613 struct btf_node *node = NULL; 3614 u32 count, i; 3615 int err = -1; 3616 3617 if (ff->ph->needs_swap) { 3618 pr_warning("interpreting btf from systems with endianness is not yet supported\n"); 3619 return 0; 3620 } 3621 3622 if (do_read_u32(ff, &count)) 3623 return -1; 3624 3625 if (count > MAX_BPF_PROGS) { 3626 pr_err("bpf btf count %u too large (max %u)\n", count, MAX_BPF_PROGS); 3627 return -1; 3628 } 3629 3630 if (ff->size < sizeof(u32) + count * 2 * sizeof(u32)) { 3631 pr_err("Invalid HEADER_BPF_BTF: section too small (%zu) for %u entries\n", 3632 ff->size, count); 3633 return -1; 3634 } 3635 3636 down_write(&env->bpf_progs.lock); 3637 3638 for (i = 0; i < count; ++i) { 3639 u32 id, data_size; 3640 3641 if (do_read_u32(ff, &id)) 3642 goto out; 3643 if (do_read_u32(ff, &data_size)) 3644 goto out; 3645 3646 if (data_size > MAX_BPF_DATA_LEN) { 3647 pr_err("bpf btf data size %u too large (max %u)\n", 3648 data_size, MAX_BPF_DATA_LEN); 3649 goto out; 3650 } 3651 3652 node = malloc(sizeof(struct btf_node) + data_size); 3653 if (!node) 3654 goto out; 3655 3656 node->id = id; 3657 node->data_size = data_size; 3658 3659 if (__do_read(ff, node->data, data_size)) 3660 goto out; 3661 3662 if (!__perf_env__insert_btf(env, node)) 3663 free(node); 3664 node = NULL; 3665 } 3666 3667 err = 0; 3668 out: 3669 up_write(&env->bpf_progs.lock); 3670 free(node); 3671 return err; 3672 #else 3673 pr_err("ERROR: Trying to read btf data without libbpf support.\n"); 3674 return -1; 3675 #endif // HAVE_LIBBPF_SUPPORT 3676 } 3677 3678 static int process_compressed(struct feat_fd *ff, 3679 void *data __maybe_unused) 3680 { 3681 struct perf_env *env = &ff->ph->env; 3682 3683 if (do_read_u32(ff, &(env->comp_ver))) 3684 return -1; 3685 3686 if (do_read_u32(ff, &(env->comp_type))) 3687 return -1; 3688 3689 if (do_read_u32(ff, &(env->comp_level))) 3690 return -1; 3691 3692 if (do_read_u32(ff, &(env->comp_ratio))) 3693 return -1; 3694 3695 if (do_read_u32(ff, &(env->comp_mmap_len))) 3696 return -1; 3697 3698 return 0; 3699 } 3700 3701 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps, 3702 char ***caps, unsigned int *max_branches, 3703 unsigned int *br_cntr_nr, 3704 unsigned int *br_cntr_width) 3705 { 3706 char *name, *value, *ptr; 3707 u32 nr_pmu_caps, i; 3708 3709 *nr_caps = 0; 3710 *caps = NULL; 3711 3712 if (do_read_u32(ff, &nr_pmu_caps)) 3713 return -1; 3714 3715 if (!nr_pmu_caps) 3716 return 0; 3717 3718 if (nr_pmu_caps > MAX_PMU_CAPS) { 3719 pr_err("Invalid pmu caps: nr_pmu_caps (%u) > %u\n", 3720 nr_pmu_caps, MAX_PMU_CAPS); 3721 return -1; 3722 } 3723 3724 *caps = calloc(nr_pmu_caps, sizeof(char *)); 3725 if (!*caps) 3726 return -1; 3727 3728 for (i = 0; i < nr_pmu_caps; i++) { 3729 name = do_read_string(ff); 3730 if (!name) 3731 goto error; 3732 3733 value = do_read_string(ff); 3734 if (!value) 3735 goto free_name; 3736 3737 if (asprintf(&ptr, "%s=%s", name, value) < 0) 3738 goto free_value; 3739 3740 (*caps)[i] = ptr; 3741 3742 if (!strcmp(name, "branches")) 3743 *max_branches = atoi(value); 3744 3745 if (!strcmp(name, "branch_counter_nr")) 3746 *br_cntr_nr = atoi(value); 3747 3748 if (!strcmp(name, "branch_counter_width")) 3749 *br_cntr_width = atoi(value); 3750 3751 free(value); 3752 free(name); 3753 } 3754 *nr_caps = nr_pmu_caps; 3755 return 0; 3756 3757 free_value: 3758 free(value); 3759 free_name: 3760 free(name); 3761 error: 3762 for (; i > 0; i--) 3763 free((*caps)[i - 1]); 3764 free(*caps); 3765 *caps = NULL; 3766 *nr_caps = 0; 3767 return -1; 3768 } 3769 3770 static int process_cpu_pmu_caps(struct feat_fd *ff, 3771 void *data __maybe_unused) 3772 { 3773 struct perf_env *env = &ff->ph->env; 3774 int ret = __process_pmu_caps(ff, &env->nr_cpu_pmu_caps, 3775 &env->cpu_pmu_caps, 3776 &env->max_branches, 3777 &env->br_cntr_nr, 3778 &env->br_cntr_width); 3779 3780 if (!ret && !env->cpu_pmu_caps) 3781 pr_debug("cpu pmu capabilities not available\n"); 3782 return ret; 3783 } 3784 3785 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused) 3786 { 3787 struct perf_env *env = &ff->ph->env; 3788 struct pmu_caps *pmu_caps; 3789 u32 nr_pmu, i; 3790 int ret; 3791 int j; 3792 3793 if (do_read_u32(ff, &nr_pmu)) 3794 return -1; 3795 3796 if (!nr_pmu) { 3797 pr_debug("pmu capabilities not available\n"); 3798 return 0; 3799 } 3800 3801 if (nr_pmu > MAX_PMU_MAPPINGS) { 3802 pr_err("Invalid HEADER_PMU_CAPS: nr_pmu (%u) > %u\n", 3803 nr_pmu, MAX_PMU_MAPPINGS); 3804 return -1; 3805 } 3806 3807 if (ff->size < sizeof(u32) + nr_pmu * sizeof(u32)) { 3808 pr_err("Invalid HEADER_PMU_CAPS: section too small (%zu) for %u PMUs\n", 3809 ff->size, nr_pmu); 3810 return -1; 3811 } 3812 3813 pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps)); 3814 if (!pmu_caps) 3815 return -ENOMEM; 3816 3817 for (i = 0; i < nr_pmu; i++) { 3818 ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps, 3819 &pmu_caps[i].caps, 3820 &pmu_caps[i].max_branches, 3821 &pmu_caps[i].br_cntr_nr, 3822 &pmu_caps[i].br_cntr_width); 3823 if (ret) 3824 goto err; 3825 3826 pmu_caps[i].pmu_name = do_read_string(ff); 3827 if (!pmu_caps[i].pmu_name) { 3828 ret = -1; 3829 goto err; 3830 } 3831 if (!pmu_caps[i].nr_caps) { 3832 pr_debug("%s pmu capabilities not available\n", 3833 pmu_caps[i].pmu_name); 3834 } 3835 } 3836 3837 env->nr_pmus_with_caps = nr_pmu; 3838 env->pmu_caps = pmu_caps; 3839 return 0; 3840 3841 err: 3842 for (i = 0; i < nr_pmu; i++) { 3843 for (j = 0; j < pmu_caps[i].nr_caps; j++) 3844 free(pmu_caps[i].caps[j]); 3845 free(pmu_caps[i].caps); 3846 free(pmu_caps[i].pmu_name); 3847 } 3848 3849 free(pmu_caps); 3850 return ret; 3851 } 3852 3853 static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused) 3854 { 3855 u32 schedstat_version, max_sched_domains, cpu, domain, nr_domains; 3856 struct perf_env *env = &ff->ph->env; 3857 char *dname, *cpumask, *cpulist; 3858 struct cpu_domain_map **cd_map; 3859 struct domain_info *d_info; 3860 u32 nra, nr, i, j; 3861 int ret; 3862 3863 nra = env->nr_cpus_avail; 3864 nr = env->nr_cpus_online; 3865 3866 if (nra == 0 || nr == 0) { 3867 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: missing HEADER_NRCPUS\n"); 3868 return -1; 3869 } 3870 3871 if (ff->size < 2 * sizeof(u32) + nr * 2 * sizeof(u32)) { 3872 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: section too small (%zu) for %u CPUs\n", 3873 (size_t)ff->size, nr); 3874 return -1; 3875 } 3876 3877 cd_map = calloc(nra, sizeof(*cd_map)); 3878 if (!cd_map) 3879 return -1; 3880 3881 env->cpu_domain = cd_map; 3882 3883 ret = do_read_u32(ff, &schedstat_version); 3884 if (ret) 3885 return ret; 3886 3887 env->schedstat_version = schedstat_version; 3888 3889 ret = do_read_u32(ff, &max_sched_domains); 3890 if (ret) 3891 return ret; 3892 3893 /* 3894 * Sanity check: real systems have at most ~10 sched domain levels 3895 * (SMT, CLS, MC, PKG + NUMA hops). Reject obviously bogus values 3896 * from malformed perf.data files before they cause excessive 3897 * allocation in the per-CPU loop. 3898 */ 3899 if (max_sched_domains > MAX_SCHED_DOMAINS) { 3900 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: max_sched_domains %u > %u\n", 3901 max_sched_domains, MAX_SCHED_DOMAINS); 3902 return -1; 3903 } 3904 3905 env->max_sched_domains = max_sched_domains; 3906 3907 for (i = 0; i < nr; i++) { 3908 if (do_read_u32(ff, &cpu)) 3909 return -1; 3910 3911 if (cpu >= nra) { 3912 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: cpu %d >= nr_cpus_avail (%d)\n", cpu, nra); 3913 return -1; 3914 } 3915 3916 if (cd_map[cpu]) { 3917 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate cpu %u\n", cpu); 3918 return -1; 3919 } 3920 3921 cd_map[cpu] = zalloc(sizeof(*cd_map[cpu])); 3922 if (!cd_map[cpu]) 3923 return -1; 3924 3925 cd_map[cpu]->cpu = cpu; 3926 3927 if (do_read_u32(ff, &nr_domains)) 3928 return -1; 3929 3930 if (nr_domains > max_sched_domains) { 3931 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: nr_domains %u > max_sched_domains (%u)\n", 3932 nr_domains, max_sched_domains); 3933 return -1; 3934 } 3935 3936 cd_map[cpu]->nr_domains = nr_domains; 3937 3938 cd_map[cpu]->domains = calloc(max_sched_domains, sizeof(*d_info)); 3939 if (!cd_map[cpu]->domains) 3940 return -1; 3941 3942 for (j = 0; j < nr_domains; j++) { 3943 if (do_read_u32(ff, &domain)) 3944 return -1; 3945 3946 if (domain >= max_sched_domains) { 3947 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: domain %d >= max_sched_domains (%d)\n", 3948 domain, max_sched_domains); 3949 return -1; 3950 } 3951 3952 d_info = zalloc(sizeof(*d_info)); 3953 if (!d_info) 3954 return -1; 3955 3956 if (cd_map[cpu]->domains[domain]) { 3957 pr_err("Invalid HEADER_CPU_DOMAIN_INFO: duplicate domain %u for cpu %u\n", 3958 domain, cpu); 3959 free(d_info); 3960 return -1; 3961 } 3962 3963 cd_map[cpu]->domains[domain] = d_info; 3964 d_info->domain = domain; 3965 3966 if (schedstat_version >= 17) { 3967 dname = do_read_string(ff); 3968 if (!dname) 3969 return -1; 3970 3971 d_info->dname = dname; 3972 } 3973 3974 cpumask = do_read_string(ff); 3975 if (!cpumask) 3976 return -1; 3977 3978 d_info->cpumask = cpumask; 3979 3980 cpulist = do_read_string(ff); 3981 if (!cpulist) 3982 return -1; 3983 3984 d_info->cpulist = cpulist; 3985 } 3986 } 3987 3988 return ret; 3989 } 3990 3991 #define FEAT_OPR(n, func, __full_only) \ 3992 [HEADER_##n] = { \ 3993 .name = __stringify(n), \ 3994 .write = write_##func, \ 3995 .print = print_##func, \ 3996 .full_only = __full_only, \ 3997 .process = process_##func, \ 3998 .synthesize = true \ 3999 } 4000 4001 #define FEAT_OPN(n, func, __full_only) \ 4002 [HEADER_##n] = { \ 4003 .name = __stringify(n), \ 4004 .write = write_##func, \ 4005 .print = print_##func, \ 4006 .full_only = __full_only, \ 4007 .process = process_##func \ 4008 } 4009 4010 /* feature_ops not implemented: */ 4011 #define print_tracing_data NULL 4012 #define print_build_id NULL 4013 4014 #define process_branch_stack NULL 4015 #define process_stat NULL 4016 4017 // Only used in util/synthetic-events.c 4018 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE]; 4019 4020 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = { 4021 FEAT_OPN(TRACING_DATA, tracing_data, false), 4022 FEAT_OPN(BUILD_ID, build_id, false), 4023 FEAT_OPR(HOSTNAME, hostname, false), 4024 FEAT_OPR(OSRELEASE, osrelease, false), 4025 FEAT_OPR(VERSION, version, false), 4026 FEAT_OPR(ARCH, arch, false), 4027 FEAT_OPR(NRCPUS, nrcpus, false), 4028 FEAT_OPR(CPUDESC, cpudesc, false), 4029 FEAT_OPR(CPUID, cpuid, false), 4030 FEAT_OPR(TOTAL_MEM, total_mem, false), 4031 FEAT_OPR(EVENT_DESC, event_desc, false), 4032 FEAT_OPR(CMDLINE, cmdline, false), 4033 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true), 4034 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true), 4035 FEAT_OPN(BRANCH_STACK, branch_stack, false), 4036 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false), 4037 FEAT_OPR(GROUP_DESC, group_desc, false), 4038 FEAT_OPN(AUXTRACE, auxtrace, false), 4039 FEAT_OPN(STAT, stat, false), 4040 FEAT_OPN(CACHE, cache, true), 4041 FEAT_OPR(SAMPLE_TIME, sample_time, false), 4042 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true), 4043 FEAT_OPR(CLOCKID, clockid, false), 4044 FEAT_OPN(DIR_FORMAT, dir_format, false), 4045 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false), 4046 FEAT_OPR(BPF_BTF, bpf_btf, false), 4047 FEAT_OPR(COMPRESSED, compressed, false), 4048 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false), 4049 FEAT_OPR(CLOCK_DATA, clock_data, false), 4050 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true), 4051 FEAT_OPR(PMU_CAPS, pmu_caps, false), 4052 FEAT_OPR(CPU_DOMAIN_INFO, cpu_domain_info, true), 4053 FEAT_OPR(E_MACHINE, e_machine, false), 4054 FEAT_OPR(CLN_SIZE, cln_size, false), 4055 }; 4056 4057 struct header_print_data { 4058 FILE *fp; 4059 bool full; /* extended list of headers */ 4060 }; 4061 4062 const char *header_feat__name(unsigned int id) 4063 { 4064 if (id < HEADER_LAST_FEATURE) 4065 return feat_ops[id].name ?: "INVALID"; 4066 return "INVALID"; 4067 } 4068 4069 static int perf_file_section__fprintf_info(struct perf_file_section *section, 4070 struct perf_header *ph, 4071 int feat, int fd, void *data) 4072 { 4073 struct header_print_data *hd = data; 4074 struct feat_fd ff; 4075 4076 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 4077 pr_debug("Failed to lseek to %" PRIu64 " offset for feature %s (%d), continuing...\n", 4078 section->offset, header_feat__name(feat), feat); 4079 return 0; 4080 } 4081 if (feat >= ph->last_feat) { 4082 pr_warning("unknown feature %d\n", feat); 4083 return 0; 4084 } 4085 if (!feat_ops[feat].print) 4086 return 0; 4087 4088 ff = (struct feat_fd) { 4089 .fd = fd, 4090 .ph = ph, 4091 }; 4092 4093 if (!feat_ops[feat].full_only || hd->full) 4094 feat_ops[feat].print(&ff, hd->fp); 4095 else 4096 fprintf(hd->fp, "# %s info available, use -I to display\n", 4097 feat_ops[feat].name); 4098 4099 return 0; 4100 } 4101 4102 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) 4103 { 4104 struct header_print_data hd; 4105 struct perf_header *header = &session->header; 4106 int fd = perf_data__fd(session->data); 4107 struct stat st; 4108 time_t stctime; 4109 int ret, bit; 4110 4111 hd.fp = fp; 4112 hd.full = full; 4113 4114 ret = fstat(fd, &st); 4115 if (ret == -1) 4116 return -1; 4117 4118 stctime = st.st_mtime; 4119 fprintf(fp, "# captured on : %s", ctime(&stctime)); 4120 4121 fprintf(fp, "# header version : %u\n", header->version); 4122 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); 4123 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); 4124 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); 4125 4126 perf_header__process_sections(header, fd, &hd, 4127 perf_file_section__fprintf_info); 4128 4129 if (session->data->is_pipe) 4130 return 0; 4131 4132 fprintf(fp, "# missing features: "); 4133 for_each_clear_bit(bit, header->adds_features, header->last_feat) { 4134 if (bit) 4135 fprintf(fp, "%s ", feat_ops[bit].name); 4136 } 4137 4138 fprintf(fp, "\n"); 4139 return 0; 4140 } 4141 4142 struct header_fw { 4143 struct feat_writer fw; 4144 struct feat_fd *ff; 4145 }; 4146 4147 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz) 4148 { 4149 struct header_fw *h = container_of(fw, struct header_fw, fw); 4150 4151 return do_write(h->ff, buf, sz); 4152 } 4153 4154 static int do_write_feat(struct feat_fd *ff, int type, 4155 struct perf_file_section **p, 4156 struct evlist *evlist, 4157 struct feat_copier *fc) 4158 { 4159 int err; 4160 int ret = 0; 4161 4162 if (perf_header__has_feat(ff->ph, type)) { 4163 if (!feat_ops[type].write) 4164 return -1; 4165 4166 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) 4167 return -1; 4168 4169 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); 4170 4171 /* 4172 * Hook to let perf inject copy features sections from the input 4173 * file. 4174 */ 4175 if (fc && fc->copy) { 4176 struct header_fw h = { 4177 .fw.write = feat_writer_cb, 4178 .ff = ff, 4179 }; 4180 4181 /* ->copy() returns 0 if the feature was not copied */ 4182 err = fc->copy(fc, type, &h.fw); 4183 } else { 4184 err = 0; 4185 } 4186 if (!err) 4187 err = feat_ops[type].write(ff, evlist); 4188 if (err < 0) { 4189 pr_debug("failed to write feature %s\n", feat_ops[type].name); 4190 4191 /* undo anything written */ 4192 lseek(ff->fd, (*p)->offset, SEEK_SET); 4193 4194 return -1; 4195 } 4196 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; 4197 (*p)++; 4198 } 4199 return ret; 4200 } 4201 4202 static int perf_header__adds_write(struct perf_header *header, 4203 struct evlist *evlist, int fd, 4204 struct feat_copier *fc) 4205 { 4206 int nr_sections; 4207 struct feat_fd ff = { 4208 .fd = fd, 4209 .ph = header, 4210 }; 4211 struct perf_file_section *feat_sec, *p; 4212 int sec_size; 4213 u64 sec_start; 4214 int feat; 4215 int err; 4216 4217 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 4218 if (!nr_sections) 4219 return 0; 4220 4221 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); 4222 if (feat_sec == NULL) 4223 return -ENOMEM; 4224 4225 sec_size = sizeof(*feat_sec) * nr_sections; 4226 4227 sec_start = header->feat_offset; 4228 lseek(fd, sec_start + sec_size, SEEK_SET); 4229 4230 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { 4231 if (do_write_feat(&ff, feat, &p, evlist, fc)) 4232 perf_header__clear_feat(header, feat); 4233 } 4234 4235 lseek(fd, sec_start, SEEK_SET); 4236 /* 4237 * may write more than needed due to dropped feature, but 4238 * this is okay, reader will skip the missing entries 4239 */ 4240 err = do_write(&ff, feat_sec, sec_size); 4241 if (err < 0) 4242 pr_debug("failed to write feature section\n"); 4243 free(ff.buf); /* TODO: added to silence clang-tidy. */ 4244 free(feat_sec); 4245 return err; 4246 } 4247 4248 int perf_header__write_pipe(int fd) 4249 { 4250 struct perf_pipe_file_header f_header; 4251 struct feat_fd ff = { 4252 .fd = fd, 4253 }; 4254 int err; 4255 4256 f_header = (struct perf_pipe_file_header){ 4257 .magic = PERF_MAGIC, 4258 .size = sizeof(f_header), 4259 }; 4260 4261 err = do_write(&ff, &f_header, sizeof(f_header)); 4262 if (err < 0) { 4263 pr_debug("failed to write perf pipe header\n"); 4264 return err; 4265 } 4266 free(ff.buf); 4267 return 0; 4268 } 4269 4270 static int perf_session__do_write_header(struct perf_session *session, 4271 struct evlist *evlist, 4272 int fd, bool at_exit, 4273 struct feat_copier *fc, 4274 bool write_attrs_after_data) 4275 { 4276 struct perf_file_header f_header; 4277 struct perf_header *header = &session->header; 4278 struct evsel *evsel; 4279 struct feat_fd ff = { 4280 .ph = header, 4281 .fd = fd, 4282 }; 4283 u64 attr_offset = sizeof(f_header), attr_size = 0; 4284 int err; 4285 4286 if (write_attrs_after_data && at_exit) { 4287 /* 4288 * Write features at the end of the file first so that 4289 * attributes may come after them. 4290 */ 4291 if (!header->data_offset && header->data_size) { 4292 pr_err("File contains data but offset unknown\n"); 4293 err = -1; 4294 goto err_out; 4295 } 4296 header->feat_offset = header->data_offset + header->data_size; 4297 err = perf_header__adds_write(header, evlist, fd, fc); 4298 if (err < 0) 4299 goto err_out; 4300 attr_offset = lseek(fd, 0, SEEK_CUR); 4301 } else { 4302 lseek(fd, attr_offset, SEEK_SET); 4303 } 4304 4305 evlist__for_each_entry(session->evlist, evsel) { 4306 evsel->id_offset = attr_offset; 4307 /* Avoid writing at the end of the file until the session is exiting. */ 4308 if (!write_attrs_after_data || at_exit) { 4309 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64)); 4310 if (err < 0) { 4311 pr_debug("failed to write perf header\n"); 4312 goto err_out; 4313 } 4314 } 4315 attr_offset += evsel->core.ids * sizeof(u64); 4316 } 4317 4318 evlist__for_each_entry(evlist, evsel) { 4319 if (evsel->core.attr.size < sizeof(evsel->core.attr)) { 4320 /* 4321 * We are likely in "perf inject" and have read 4322 * from an older file. Update attr size so that 4323 * reader gets the right offset to the ids. 4324 */ 4325 evsel->core.attr.size = sizeof(evsel->core.attr); 4326 } 4327 /* Avoid writing at the end of the file until the session is exiting. */ 4328 if (!write_attrs_after_data || at_exit) { 4329 struct perf_file_attr f_attr = { 4330 .attr = evsel->core.attr, 4331 .ids = { 4332 .offset = evsel->id_offset, 4333 .size = evsel->core.ids * sizeof(u64), 4334 } 4335 }; 4336 err = do_write(&ff, &f_attr, sizeof(f_attr)); 4337 if (err < 0) { 4338 pr_debug("failed to write perf header attribute\n"); 4339 goto err_out; 4340 } 4341 } 4342 attr_size += sizeof(struct perf_file_attr); 4343 } 4344 4345 if (!header->data_offset) { 4346 if (write_attrs_after_data) 4347 header->data_offset = sizeof(f_header); 4348 else 4349 header->data_offset = attr_offset + attr_size; 4350 } 4351 header->feat_offset = header->data_offset + header->data_size; 4352 4353 if (!write_attrs_after_data && at_exit) { 4354 /* Write features now feat_offset is known. */ 4355 err = perf_header__adds_write(header, evlist, fd, fc); 4356 if (err < 0) 4357 goto err_out; 4358 } 4359 4360 f_header = (struct perf_file_header){ 4361 .magic = PERF_MAGIC, 4362 .size = sizeof(f_header), 4363 .attr_size = sizeof(struct perf_file_attr), 4364 .attrs = { 4365 .offset = attr_offset, 4366 .size = attr_size, 4367 }, 4368 .data = { 4369 .offset = header->data_offset, 4370 .size = header->data_size, 4371 }, 4372 /* event_types is ignored, store zeros */ 4373 }; 4374 4375 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); 4376 4377 lseek(fd, 0, SEEK_SET); 4378 err = do_write(&ff, &f_header, sizeof(f_header)); 4379 if (err < 0) { 4380 pr_debug("failed to write perf header\n"); 4381 goto err_out; 4382 } else { 4383 lseek(fd, 0, SEEK_END); 4384 err = 0; 4385 } 4386 err_out: 4387 free(ff.buf); 4388 return err; 4389 } 4390 4391 int perf_session__write_header(struct perf_session *session, 4392 struct evlist *evlist, 4393 int fd, bool at_exit) 4394 { 4395 return perf_session__do_write_header(session, evlist, fd, at_exit, /*fc=*/NULL, 4396 /*write_attrs_after_data=*/false); 4397 } 4398 4399 size_t perf_session__data_offset(const struct evlist *evlist) 4400 { 4401 struct evsel *evsel; 4402 size_t data_offset; 4403 4404 data_offset = sizeof(struct perf_file_header); 4405 evlist__for_each_entry(evlist, evsel) { 4406 data_offset += evsel->core.ids * sizeof(u64); 4407 } 4408 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr); 4409 4410 return data_offset; 4411 } 4412 4413 int perf_session__inject_header(struct perf_session *session, 4414 struct evlist *evlist, 4415 int fd, 4416 struct feat_copier *fc, 4417 bool write_attrs_after_data) 4418 { 4419 return perf_session__do_write_header(session, evlist, fd, true, fc, 4420 write_attrs_after_data); 4421 } 4422 4423 static int perf_header__getbuffer64(struct perf_header *header, 4424 int fd, void *buf, size_t size) 4425 { 4426 if (readn(fd, buf, size) <= 0) 4427 return -1; 4428 4429 if (header->needs_swap) 4430 mem_bswap_64(buf, size); 4431 4432 return 0; 4433 } 4434 4435 int perf_header__process_sections(struct perf_header *header, int fd, 4436 void *data, 4437 int (*process)(struct perf_file_section *section, 4438 struct perf_header *ph, 4439 int feat, int fd, void *data)) 4440 { 4441 struct perf_file_section *feat_sec, *sec; 4442 int nr_sections; 4443 int sec_size; 4444 int feat; 4445 int err; 4446 4447 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); 4448 if (!nr_sections) 4449 return 0; 4450 4451 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); 4452 if (!feat_sec) 4453 return -1; 4454 4455 sec_size = sizeof(*feat_sec) * nr_sections; 4456 4457 lseek(fd, header->feat_offset, SEEK_SET); 4458 4459 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); 4460 if (err < 0) 4461 goto out_free; 4462 4463 for_each_set_bit(feat, header->adds_features, header->last_feat) { 4464 err = process(sec++, header, feat, fd, data); 4465 if (err < 0) 4466 goto out_free; 4467 } 4468 err = 0; 4469 out_free: 4470 free(feat_sec); 4471 return err; 4472 } 4473 4474 static const int attr_file_abi_sizes[] = { 4475 [0] = PERF_ATTR_SIZE_VER0, 4476 [1] = PERF_ATTR_SIZE_VER1, 4477 [2] = PERF_ATTR_SIZE_VER2, 4478 [3] = PERF_ATTR_SIZE_VER3, 4479 [4] = PERF_ATTR_SIZE_VER4, 4480 0, 4481 }; 4482 4483 /* 4484 * In the legacy file format, the magic number is not used to encode endianness. 4485 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based 4486 * on ABI revisions, we need to try all combinations for all endianness to 4487 * detect the endianness. 4488 */ 4489 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) 4490 { 4491 uint64_t ref_size, attr_size; 4492 int i; 4493 4494 for (i = 0 ; attr_file_abi_sizes[i]; i++) { 4495 ref_size = attr_file_abi_sizes[i] 4496 + sizeof(struct perf_file_section); 4497 if (hdr_sz != ref_size) { 4498 attr_size = bswap_64(hdr_sz); 4499 if (attr_size != ref_size) 4500 continue; 4501 4502 ph->needs_swap = true; 4503 } 4504 pr_debug("ABI%d perf.data file detected, need_swap=%d\n", 4505 i, 4506 ph->needs_swap); 4507 return 0; 4508 } 4509 /* could not determine endianness */ 4510 return -1; 4511 } 4512 4513 #define PERF_PIPE_HDR_VER0 16 4514 4515 static const size_t attr_pipe_abi_sizes[] = { 4516 [0] = PERF_PIPE_HDR_VER0, 4517 0, 4518 }; 4519 4520 /* 4521 * In the legacy pipe format, there is an implicit assumption that endianness 4522 * between host recording the samples, and host parsing the samples is the 4523 * same. This is not always the case given that the pipe output may always be 4524 * redirected into a file and analyzed on a different machine with possibly a 4525 * different endianness and perf_event ABI revisions in the perf tool itself. 4526 */ 4527 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) 4528 { 4529 u64 attr_size; 4530 int i; 4531 4532 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { 4533 if (hdr_sz != attr_pipe_abi_sizes[i]) { 4534 attr_size = bswap_64(hdr_sz); 4535 if (attr_size != hdr_sz) 4536 continue; 4537 4538 ph->needs_swap = true; 4539 } 4540 pr_debug("Pipe ABI%d perf.data file detected\n", i); 4541 return 0; 4542 } 4543 return -1; 4544 } 4545 4546 bool is_perf_magic(u64 magic) 4547 { 4548 if (!memcmp(&magic, __perf_magic1, sizeof(magic)) 4549 || magic == __perf_magic2 4550 || magic == __perf_magic2_sw) 4551 return true; 4552 4553 return false; 4554 } 4555 4556 static int check_magic_endian(u64 magic, uint64_t hdr_sz, 4557 bool is_pipe, struct perf_header *ph) 4558 { 4559 int ret; 4560 4561 /* check for legacy format */ 4562 ret = memcmp(&magic, __perf_magic1, sizeof(magic)); 4563 if (ret == 0) { 4564 ph->version = PERF_HEADER_VERSION_1; 4565 pr_debug("legacy perf.data format\n"); 4566 if (is_pipe) 4567 return try_all_pipe_abis(hdr_sz, ph); 4568 4569 return try_all_file_abis(hdr_sz, ph); 4570 } 4571 /* 4572 * the new magic number serves two purposes: 4573 * - unique number to identify actual perf.data files 4574 * - encode endianness of file 4575 */ 4576 ph->version = PERF_HEADER_VERSION_2; 4577 4578 /* check magic number with one endianness */ 4579 if (magic == __perf_magic2) 4580 return 0; 4581 4582 /* check magic number with opposite endianness */ 4583 if (magic != __perf_magic2_sw) 4584 return -1; 4585 4586 ph->needs_swap = true; 4587 4588 return 0; 4589 } 4590 4591 int perf_file_header__read(struct perf_file_header *header, 4592 struct perf_header *ph, int fd) 4593 { 4594 ssize_t ret; 4595 4596 lseek(fd, 0, SEEK_SET); 4597 4598 ret = readn(fd, header, sizeof(*header)); 4599 if (ret <= 0) 4600 return -1; 4601 4602 if (check_magic_endian(header->magic, 4603 header->attr_size, false, ph) < 0) { 4604 pr_debug("magic/endian check failed\n"); 4605 return -1; 4606 } 4607 4608 if (ph->needs_swap) { 4609 mem_bswap_64(header, offsetof(struct perf_file_header, 4610 adds_features)); 4611 } 4612 4613 if (header->size > header->attrs.offset) { 4614 pr_err("Perf file header corrupt: header overlaps attrs\n"); 4615 return -1; 4616 } 4617 4618 if (header->size > header->data.offset) { 4619 pr_err("Perf file header corrupt: header overlaps data\n"); 4620 return -1; 4621 } 4622 4623 if ((header->attrs.offset <= header->data.offset && 4624 header->attrs.offset + header->attrs.size > header->data.offset) || 4625 (header->attrs.offset > header->data.offset && 4626 header->data.offset + header->data.size > header->attrs.offset)) { 4627 pr_err("Perf file header corrupt: Attributes and data overlap\n"); 4628 return -1; 4629 } 4630 4631 if (header->size != sizeof(*header)) { 4632 /* Support the previous format */ 4633 if (header->size == offsetof(typeof(*header), adds_features)) 4634 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 4635 else 4636 return -1; 4637 } else if (ph->needs_swap) { 4638 /* 4639 * feature bitmap is declared as an array of unsigned longs -- 4640 * not good since its size can differ between the host that 4641 * generated the data file and the host analyzing the file. 4642 * 4643 * We need to handle endianness, but we don't know the size of 4644 * the unsigned long where the file was generated. Take a best 4645 * guess at determining it: try 64-bit swap first (ie., file 4646 * created on a 64-bit host), and check if the hostname feature 4647 * bit is set (this feature bit is forced on as of fbe96f2). 4648 * If the bit is not, undo the 64-bit swap and try a 32-bit 4649 * swap. If the hostname bit is still not set (e.g., older data 4650 * file), punt and fallback to the original behavior -- 4651 * clearing all feature bits and setting buildid. 4652 */ 4653 mem_bswap_64(&header->adds_features, 4654 BITS_TO_U64(HEADER_FEAT_BITS)); 4655 4656 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 4657 /* unswap as u64 */ 4658 mem_bswap_64(&header->adds_features, 4659 BITS_TO_U64(HEADER_FEAT_BITS)); 4660 4661 /* unswap as u32 */ 4662 mem_bswap_32(&header->adds_features, 4663 BITS_TO_U32(HEADER_FEAT_BITS)); 4664 } 4665 4666 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { 4667 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); 4668 __set_bit(HEADER_BUILD_ID, header->adds_features); 4669 } 4670 } 4671 4672 memcpy(&ph->adds_features, &header->adds_features, 4673 sizeof(ph->adds_features)); 4674 4675 ph->data_offset = header->data.offset; 4676 ph->data_size = header->data.size; 4677 ph->feat_offset = header->data.offset + header->data.size; 4678 ph->last_feat = HEADER_LAST_FEATURE; 4679 return 0; 4680 } 4681 4682 static int perf_file_section__process(struct perf_file_section *section, 4683 struct perf_header *ph, 4684 int feat, int fd, void *data) 4685 { 4686 struct feat_fd fdd = { 4687 .fd = fd, 4688 .ph = ph, 4689 .size = section->size, 4690 .offset = section->offset, 4691 }; 4692 4693 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { 4694 pr_debug("Failed to lseek to %" PRIu64 " offset for feature %s (%d), continuing...\n", 4695 section->offset, header_feat__name(feat), feat); 4696 return 0; 4697 } 4698 4699 if (feat >= HEADER_LAST_FEATURE) { 4700 pr_debug("unknown feature %d, continuing...\n", feat); 4701 return 0; 4702 } 4703 4704 if (!feat_ops[feat].process) 4705 return 0; 4706 4707 return feat_ops[feat].process(&fdd, data); 4708 } 4709 4710 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, 4711 struct perf_header *ph, 4712 struct perf_data *data) 4713 { 4714 ssize_t ret; 4715 4716 ret = perf_data__read(data, header, sizeof(*header)); 4717 if (ret <= 0) 4718 return -1; 4719 4720 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { 4721 pr_debug("endian/magic failed\n"); 4722 return -1; 4723 } 4724 4725 if (ph->needs_swap) 4726 header->size = bswap_64(header->size); 4727 4728 /* The last feature is written out as a 0 sized event and will update this value. */ 4729 ph->last_feat = 0; 4730 return 0; 4731 } 4732 4733 static int perf_header__read_pipe(struct perf_session *session) 4734 { 4735 struct perf_header *header = &session->header; 4736 struct perf_pipe_file_header f_header; 4737 4738 if (perf_file_header__read_pipe(&f_header, header, session->data) < 0) { 4739 pr_debug("incompatible file format\n"); 4740 return -EINVAL; 4741 } 4742 4743 return f_header.size == sizeof(f_header) ? 0 : -1; 4744 } 4745 4746 static int read_attr(int fd, struct perf_header *ph, 4747 struct perf_file_attr *f_attr) 4748 { 4749 struct perf_event_attr *attr = &f_attr->attr; 4750 size_t sz, left; 4751 size_t our_sz = sizeof(f_attr->attr); 4752 ssize_t ret; 4753 4754 memset(f_attr, 0, sizeof(*f_attr)); 4755 4756 /* read minimal guaranteed structure */ 4757 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); 4758 if (ret <= 0) { 4759 pr_debug("cannot read %d bytes of header attr\n", 4760 PERF_ATTR_SIZE_VER0); 4761 return -1; 4762 } 4763 4764 /* on file perf_event_attr size */ 4765 sz = attr->size; 4766 4767 if (ph->needs_swap) 4768 sz = bswap_32(sz); 4769 4770 if (sz == 0) { 4771 /* assume ABI0 */ 4772 sz = PERF_ATTR_SIZE_VER0; 4773 } else if (sz > our_sz) { 4774 pr_debug("file uses a more recent and unsupported ABI" 4775 " (%zu bytes extra)\n", sz - our_sz); 4776 return -1; 4777 } 4778 /* what we have not yet read and that we know about */ 4779 left = sz - PERF_ATTR_SIZE_VER0; 4780 if (left) { 4781 void *ptr = attr; 4782 ptr += PERF_ATTR_SIZE_VER0; 4783 4784 ret = readn(fd, ptr, left); 4785 } 4786 /* read perf_file_section, ids are read in caller */ 4787 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); 4788 4789 return ret <= 0 ? -1 : 0; 4790 } 4791 4792 #ifdef HAVE_LIBTRACEEVENT 4793 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent) 4794 { 4795 struct tep_event *event; 4796 char bf[128]; 4797 4798 /* already prepared */ 4799 if (evsel->tp_format) 4800 return 0; 4801 4802 if (pevent == NULL) { 4803 pr_debug("broken or missing trace data\n"); 4804 return -1; 4805 } 4806 4807 event = tep_find_event(pevent, evsel->core.attr.config); 4808 if (event == NULL) { 4809 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); 4810 return -1; 4811 } 4812 4813 if (!evsel->name) { 4814 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); 4815 evsel->name = strdup(bf); 4816 if (evsel->name == NULL) 4817 return -1; 4818 } 4819 4820 evsel->tp_format = event; 4821 return 0; 4822 } 4823 4824 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent) 4825 { 4826 struct evsel *pos; 4827 4828 evlist__for_each_entry(evlist, pos) { 4829 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && 4830 evsel__prepare_tracepoint_event(pos, pevent)) 4831 return -1; 4832 } 4833 4834 return 0; 4835 } 4836 #endif 4837 4838 int perf_session__read_header(struct perf_session *session) 4839 { 4840 struct perf_data *data = session->data; 4841 struct perf_header *header = &session->header; 4842 struct perf_file_header f_header; 4843 struct perf_file_attr f_attr; 4844 u64 f_id; 4845 int nr_attrs, nr_ids, i, j, err; 4846 int fd = perf_data__fd(data); 4847 4848 session->evlist = evlist__new(); 4849 if (session->evlist == NULL) 4850 return -ENOMEM; 4851 4852 session->evlist->session = session; 4853 session->machines.host.env = &header->env; 4854 4855 /* 4856 * We can read 'pipe' data event from regular file, 4857 * check for the pipe header regardless of source. 4858 */ 4859 err = perf_header__read_pipe(session); 4860 if (!err || perf_data__is_pipe(data)) { 4861 data->is_pipe = true; 4862 return err; 4863 } 4864 4865 if (perf_file_header__read(&f_header, header, fd) < 0) 4866 return -EINVAL; 4867 4868 if (header->needs_swap && data->in_place_update) { 4869 pr_err("In-place update not supported when byte-swapping is required\n"); 4870 return -EINVAL; 4871 } 4872 4873 /* 4874 * Sanity check that perf.data was written cleanly; data size is 4875 * initialized to 0 and updated only if the on_exit function is run. 4876 * If data size is still 0 then the file contains only partial 4877 * information. Just warn user and process it as much as it can. 4878 */ 4879 if (f_header.data.size == 0) { 4880 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" 4881 "Was the 'perf record' command properly terminated?\n", 4882 data->file.path); 4883 } 4884 4885 if (f_header.attr_size == 0) { 4886 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n" 4887 "Was the 'perf record' command properly terminated?\n", 4888 data->file.path); 4889 return -EINVAL; 4890 } 4891 4892 nr_attrs = f_header.attrs.size / f_header.attr_size; 4893 lseek(fd, f_header.attrs.offset, SEEK_SET); 4894 4895 for (i = 0; i < nr_attrs; i++) { 4896 struct evsel *evsel; 4897 off_t tmp; 4898 4899 if (read_attr(fd, header, &f_attr) < 0) 4900 goto out_errno; 4901 4902 if (header->needs_swap) { 4903 f_attr.ids.size = bswap_64(f_attr.ids.size); 4904 f_attr.ids.offset = bswap_64(f_attr.ids.offset); 4905 perf_event__attr_swap(&f_attr.attr); 4906 } 4907 4908 tmp = lseek(fd, 0, SEEK_CUR); 4909 evsel = evsel__new(&f_attr.attr); 4910 4911 if (evsel == NULL) 4912 goto out_delete_evlist; 4913 4914 evsel->needs_swap = header->needs_swap; 4915 /* 4916 * Do it before so that if perf_evsel__alloc_id fails, this 4917 * entry gets purged too at evlist__delete(). 4918 */ 4919 evlist__add(session->evlist, evsel); 4920 4921 nr_ids = f_attr.ids.size / sizeof(u64); 4922 /* 4923 * We don't have the cpu and thread maps on the header, so 4924 * for allocating the perf_sample_id table we fake 1 cpu and 4925 * hattr->ids threads. 4926 */ 4927 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids)) 4928 goto out_delete_evlist; 4929 4930 lseek(fd, f_attr.ids.offset, SEEK_SET); 4931 4932 for (j = 0; j < nr_ids; j++) { 4933 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) 4934 goto out_errno; 4935 4936 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id); 4937 } 4938 4939 lseek(fd, tmp, SEEK_SET); 4940 } 4941 4942 #ifdef HAVE_LIBTRACEEVENT 4943 perf_header__process_sections(header, fd, &session->tevent, 4944 perf_file_section__process); 4945 4946 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent)) 4947 goto out_delete_evlist; 4948 #else 4949 perf_header__process_sections(header, fd, NULL, perf_file_section__process); 4950 #endif 4951 4952 return 0; 4953 out_errno: 4954 return -errno; 4955 4956 out_delete_evlist: 4957 evlist__delete(session->evlist); 4958 session->evlist = NULL; 4959 return -ENOMEM; 4960 } 4961 4962 int perf_event__process_feature(const struct perf_tool *tool __maybe_unused, 4963 struct perf_session *session, 4964 union perf_event *event) 4965 { 4966 struct feat_fd ff = { .fd = 0 }; 4967 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event; 4968 struct perf_header *header = &session->header; 4969 int type = fe->header.type; 4970 int feat = (int)fe->feat_id; 4971 int ret = 0; 4972 bool print = dump_trace; 4973 bool last_feature_mark = false; 4974 4975 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) { 4976 pr_warning("invalid record type %d in pipe-mode\n", type); 4977 return 0; 4978 } 4979 if (feat == HEADER_RESERVED) { 4980 pr_warning("invalid reserved record type in pipe-mode\n"); 4981 return -1; 4982 } 4983 if (feat < 0 || feat == INT_MAX) { 4984 pr_warning("invalid value for feature type %x\n", feat); 4985 return -1; 4986 } 4987 if (feat >= header->last_feat) { 4988 if (event->header.size == sizeof(*fe)) { 4989 /* 4990 * Either an unexpected zero size feature or the 4991 * HEADER_LAST_FEATURE mark. 4992 */ 4993 if (feat > header->last_feat) 4994 header->last_feat = min(feat, HEADER_LAST_FEATURE); 4995 last_feature_mark = true; 4996 } else { 4997 /* 4998 * A feature but beyond what is known as in 4999 * bounds. Assume the last feature is 1 beyond this 5000 * feature. 5001 */ 5002 session->header.last_feat = min(feat + 1, HEADER_LAST_FEATURE); 5003 } 5004 } 5005 if (feat >= HEADER_LAST_FEATURE) { 5006 if (!last_feature_mark) { 5007 pr_warning("unknown feature %d for data file version (%s) in this version of perf (%s)\n", 5008 feat, header->env.version, perf_version_string); 5009 } 5010 return 0; 5011 } 5012 if (event->header.size < sizeof(*fe)) { 5013 pr_warning("feature header size too small\n"); 5014 return -1; 5015 } 5016 ff.buf = (void *)fe->data; 5017 ff.size = event->header.size - sizeof(*fe); 5018 ff.ph = header; 5019 5020 if (feat_ops[feat].process && feat_ops[feat].process(&ff, NULL)) { 5021 // Processing failed, ignore when this is the last feature mark. 5022 if (!last_feature_mark) 5023 ret = -1; 5024 goto out; 5025 } 5026 5027 if (session->tool->show_feat_hdr) { 5028 if (!feat_ops[feat].full_only || 5029 session->tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { 5030 print = true; 5031 } else { 5032 fprintf(stdout, "# %s info available, use -I to display\n", 5033 feat_ops[feat].name); 5034 } 5035 } 5036 5037 if (dump_trace) 5038 printf(", "); 5039 5040 if (print) { 5041 if (feat_ops[feat].print) 5042 feat_ops[feat].print(&ff, stdout); 5043 else 5044 printf("# %s", feat_ops[feat].name); 5045 } 5046 5047 out: 5048 free_event_desc(ff.events); 5049 return ret; 5050 } 5051 5052 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp) 5053 { 5054 struct perf_record_event_update *ev = &event->event_update; 5055 struct perf_cpu_map *map; 5056 size_t ret; 5057 5058 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id); 5059 5060 switch (ev->type) { 5061 case PERF_EVENT_UPDATE__SCALE: 5062 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale); 5063 break; 5064 case PERF_EVENT_UPDATE__UNIT: 5065 ret += fprintf(fp, "... unit: %s\n", ev->unit); 5066 break; 5067 case PERF_EVENT_UPDATE__NAME: 5068 ret += fprintf(fp, "... name: %s\n", ev->name); 5069 break; 5070 case PERF_EVENT_UPDATE__CPUS: 5071 ret += fprintf(fp, "... "); 5072 5073 map = cpu_map__new_data(&ev->cpus.cpus); 5074 if (map) { 5075 ret += cpu_map__fprintf(map, fp); 5076 perf_cpu_map__put(map); 5077 } else 5078 ret += fprintf(fp, "failed to get cpus\n"); 5079 break; 5080 default: 5081 ret += fprintf(fp, "... unknown type\n"); 5082 break; 5083 } 5084 5085 return ret; 5086 } 5087 5088 size_t perf_event__fprintf_attr(union perf_event *event, FILE *fp) 5089 { 5090 return perf_event_attr__fprintf(fp, &event->attr.attr, __desc_attr__fprintf, NULL); 5091 } 5092 5093 int perf_event__process_attr(const struct perf_tool *tool __maybe_unused, 5094 union perf_event *event, 5095 struct evlist **pevlist) 5096 { 5097 u32 i, n_ids; 5098 u64 *ids; 5099 struct evsel *evsel; 5100 struct evlist *evlist = *pevlist; 5101 5102 if (dump_trace) 5103 perf_event__fprintf_attr(event, stdout); 5104 5105 if (evlist == NULL) { 5106 *pevlist = evlist = evlist__new(); 5107 if (evlist == NULL) 5108 return -ENOMEM; 5109 } 5110 5111 evsel = evsel__new(&event->attr.attr); 5112 if (evsel == NULL) 5113 return -ENOMEM; 5114 5115 evlist__add(evlist, evsel); 5116 5117 n_ids = event->header.size - sizeof(event->header) - event->attr.attr.size; 5118 n_ids = n_ids / sizeof(u64); 5119 /* 5120 * We don't have the cpu and thread maps on the header, so 5121 * for allocating the perf_sample_id table we fake 1 cpu and 5122 * hattr->ids threads. 5123 */ 5124 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids)) 5125 return -ENOMEM; 5126 5127 ids = perf_record_header_attr_id(event); 5128 for (i = 0; i < n_ids; i++) { 5129 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, ids[i]); 5130 } 5131 5132 return 0; 5133 } 5134 5135 int perf_event__process_event_update(const struct perf_tool *tool __maybe_unused, 5136 union perf_event *event, 5137 struct evlist **pevlist) 5138 { 5139 struct perf_record_event_update *ev = &event->event_update; 5140 struct evlist *evlist; 5141 struct evsel *evsel; 5142 struct perf_cpu_map *map; 5143 5144 if (dump_trace) 5145 perf_event__fprintf_event_update(event, stdout); 5146 5147 if (!pevlist || *pevlist == NULL) 5148 return -EINVAL; 5149 5150 evlist = *pevlist; 5151 5152 evsel = evlist__id2evsel(evlist, ev->id); 5153 if (evsel == NULL) 5154 return -EINVAL; 5155 5156 switch (ev->type) { 5157 case PERF_EVENT_UPDATE__UNIT: 5158 free((char *)evsel->unit); 5159 evsel->unit = strdup(ev->unit); 5160 break; 5161 case PERF_EVENT_UPDATE__NAME: 5162 free(evsel->name); 5163 evsel->name = strdup(ev->name); 5164 break; 5165 case PERF_EVENT_UPDATE__SCALE: 5166 evsel->scale = ev->scale.scale; 5167 break; 5168 case PERF_EVENT_UPDATE__CPUS: 5169 map = cpu_map__new_data(&ev->cpus.cpus); 5170 if (map) { 5171 perf_cpu_map__put(evsel->core.pmu_cpus); 5172 evsel->core.pmu_cpus = map; 5173 } else 5174 pr_err("failed to get event_update cpus\n"); 5175 default: 5176 break; 5177 } 5178 5179 return 0; 5180 } 5181 5182 #ifdef HAVE_LIBTRACEEVENT 5183 int perf_event__process_tracing_data(const struct perf_tool *tool __maybe_unused, 5184 struct perf_session *session, 5185 union perf_event *event) 5186 { 5187 ssize_t size_read, padding, size = event->tracing_data.size; 5188 int fd = perf_data__fd(session->data); 5189 char buf[BUFSIZ]; 5190 5191 /* 5192 * The pipe fd is already in proper place and in any case 5193 * we can't move it, and we'd screw the case where we read 5194 * 'pipe' data from regular file. The trace_report reads 5195 * data from 'fd' so we need to set it directly behind the 5196 * event, where the tracing data starts. 5197 */ 5198 if (!perf_data__is_pipe(session->data)) { 5199 off_t offset = lseek(fd, 0, SEEK_CUR); 5200 5201 /* setup for reading amidst mmap */ 5202 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data), 5203 SEEK_SET); 5204 } 5205 5206 size_read = trace_report(fd, &session->tevent, session->trace_event_repipe); 5207 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; 5208 5209 if (readn(fd, buf, padding) < 0) { 5210 pr_err("%s: reading input file", __func__); 5211 return -1; 5212 } 5213 if (session->trace_event_repipe) { 5214 int retw = write(STDOUT_FILENO, buf, padding); 5215 if (retw <= 0 || retw != padding) { 5216 pr_err("%s: repiping tracing data padding", __func__); 5217 return -1; 5218 } 5219 } 5220 5221 if (size_read + padding != size) { 5222 pr_err("%s: tracing data size mismatch", __func__); 5223 return -1; 5224 } 5225 5226 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent); 5227 5228 return size_read + padding; 5229 } 5230 #endif 5231 5232 int perf_event__process_build_id(const struct perf_tool *tool __maybe_unused, 5233 struct perf_session *session, 5234 union perf_event *event) 5235 { 5236 __event_process_build_id(&event->build_id, 5237 event->build_id.filename, 5238 session); 5239 return 0; 5240 } 5241