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