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