7 #include <linux/list.h>
8 #include <linux/kernel.h>
9 #include <linux/bitops.h>
10 #include <sys/utsname.h>
16 #include "trace-event.h"
27 static u32 header_argc
;
28 static const char **header_argv
;
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
37 * we check for legacy (PERFFILE) format.
39 static const char *__perf_magic1
= "PERFFILE";
40 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
41 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
43 #define PERF_MAGIC __perf_magic2
45 struct perf_file_attr
{
46 struct perf_event_attr attr
;
47 struct perf_file_section ids
;
50 void perf_header__set_feat(struct perf_header
*header
, int feat
)
52 set_bit(feat
, header
->adds_features
);
55 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
57 clear_bit(feat
, header
->adds_features
);
60 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
62 return test_bit(feat
, header
->adds_features
);
65 static int do_write(int fd
, const void *buf
, size_t size
)
68 int ret
= write(fd
, buf
, size
);
80 int write_padded(int fd
, const void *bf
, size_t count
, size_t count_aligned
)
82 static const char zero_buf
[NAME_ALIGN
];
83 int err
= do_write(fd
, bf
, count
);
86 err
= do_write(fd
, zero_buf
, count_aligned
- count
);
91 static int do_write_string(int fd
, const char *str
)
96 olen
= strlen(str
) + 1;
97 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
99 /* write len, incl. \0 */
100 ret
= do_write(fd
, &len
, sizeof(len
));
104 return write_padded(fd
, str
, olen
, len
);
107 static char *do_read_string(int fd
, struct perf_header
*ph
)
113 sz
= readn(fd
, &len
, sizeof(len
));
114 if (sz
< (ssize_t
)sizeof(len
))
124 ret
= readn(fd
, buf
, len
);
125 if (ret
== (ssize_t
)len
) {
127 * strings are padded by zeroes
128 * thus the actual strlen of buf
129 * may be less than len
139 perf_header__set_cmdline(int argc
, const char **argv
)
144 * If header_argv has already been set, do not override it.
145 * This allows a command to set the cmdline, parse args and
146 * then call another builtin function that implements a
147 * command -- e.g, cmd_kvm calling cmd_record.
152 header_argc
= (u32
)argc
;
154 /* do not include NULL termination */
155 header_argv
= calloc(argc
, sizeof(char *));
160 * must copy argv contents because it gets moved
161 * around during option parsing
163 for (i
= 0; i
< argc
; i
++)
164 header_argv
[i
] = argv
[i
];
169 static int write_tracing_data(int fd
, struct perf_header
*h __maybe_unused
,
170 struct perf_evlist
*evlist
)
172 return read_tracing_data(fd
, &evlist
->entries
);
176 static int write_build_id(int fd
, struct perf_header
*h
,
177 struct perf_evlist
*evlist __maybe_unused
)
179 struct perf_session
*session
;
182 session
= container_of(h
, struct perf_session
, header
);
184 if (!perf_session__read_build_ids(session
, true))
187 err
= perf_session__write_buildid_table(session
, fd
);
189 pr_debug("failed to write buildid table\n");
192 perf_session__cache_build_ids(session
);
197 static int write_hostname(int fd
, struct perf_header
*h __maybe_unused
,
198 struct perf_evlist
*evlist __maybe_unused
)
207 return do_write_string(fd
, uts
.nodename
);
210 static int write_osrelease(int fd
, struct perf_header
*h __maybe_unused
,
211 struct perf_evlist
*evlist __maybe_unused
)
220 return do_write_string(fd
, uts
.release
);
223 static int write_arch(int fd
, struct perf_header
*h __maybe_unused
,
224 struct perf_evlist
*evlist __maybe_unused
)
233 return do_write_string(fd
, uts
.machine
);
236 static int write_version(int fd
, struct perf_header
*h __maybe_unused
,
237 struct perf_evlist
*evlist __maybe_unused
)
239 return do_write_string(fd
, perf_version_string
);
242 static int __write_cpudesc(int fd
, const char *cpuinfo_proc
)
247 const char *search
= cpuinfo_proc
;
254 file
= fopen("/proc/cpuinfo", "r");
258 while (getline(&buf
, &len
, file
) > 0) {
259 ret
= strncmp(buf
, search
, strlen(search
));
271 p
= strchr(buf
, ':');
272 if (p
&& *(p
+1) == ' ' && *(p
+2))
278 /* squash extra space characters (branding string) */
285 while (*q
&& isspace(*q
))
288 while ((*r
++ = *q
++));
292 ret
= do_write_string(fd
, s
);
299 static int write_cpudesc(int fd
, struct perf_header
*h __maybe_unused
,
300 struct perf_evlist
*evlist __maybe_unused
)
303 #define CPUINFO_PROC {"model name", }
305 const char *cpuinfo_procs
[] = CPUINFO_PROC
;
308 for (i
= 0; i
< ARRAY_SIZE(cpuinfo_procs
); i
++) {
310 ret
= __write_cpudesc(fd
, cpuinfo_procs
[i
]);
318 static int write_nrcpus(int fd
, struct perf_header
*h __maybe_unused
,
319 struct perf_evlist
*evlist __maybe_unused
)
325 nr
= sysconf(_SC_NPROCESSORS_CONF
);
329 nrc
= (u32
)(nr
& UINT_MAX
);
331 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
335 nra
= (u32
)(nr
& UINT_MAX
);
337 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
341 return do_write(fd
, &nra
, sizeof(nra
));
344 static int write_event_desc(int fd
, struct perf_header
*h __maybe_unused
,
345 struct perf_evlist
*evlist
)
347 struct perf_evsel
*evsel
;
351 nre
= evlist
->nr_entries
;
354 * write number of events
356 ret
= do_write(fd
, &nre
, sizeof(nre
));
361 * size of perf_event_attr struct
363 sz
= (u32
)sizeof(evsel
->attr
);
364 ret
= do_write(fd
, &sz
, sizeof(sz
));
368 evlist__for_each(evlist
, evsel
) {
369 ret
= do_write(fd
, &evsel
->attr
, sz
);
373 * write number of unique id per event
374 * there is one id per instance of an event
376 * copy into an nri to be independent of the
380 ret
= do_write(fd
, &nri
, sizeof(nri
));
385 * write event string as passed on cmdline
387 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
391 * write unique ids for this event
393 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
400 static int write_cmdline(int fd
, struct perf_header
*h __maybe_unused
,
401 struct perf_evlist
*evlist __maybe_unused
)
403 char buf
[MAXPATHLEN
];
409 * actual atual path to perf binary
411 sprintf(proc
, "/proc/%d/exe", getpid());
412 ret
= readlink(proc
, buf
, sizeof(buf
));
416 /* readlink() does not add null termination */
419 /* account for binary path */
422 ret
= do_write(fd
, &n
, sizeof(n
));
426 ret
= do_write_string(fd
, buf
);
430 for (i
= 0 ; i
< header_argc
; i
++) {
431 ret
= do_write_string(fd
, header_argv
[i
]);
438 #define CORE_SIB_FMT \
439 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
440 #define THRD_SIB_FMT \
441 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
446 char **core_siblings
;
447 char **thread_siblings
;
450 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
453 char filename
[MAXPATHLEN
];
454 char *buf
= NULL
, *p
;
460 sprintf(filename
, CORE_SIB_FMT
, cpu
);
461 fp
= fopen(filename
, "r");
465 sret
= getline(&buf
, &len
, fp
);
470 p
= strchr(buf
, '\n');
474 for (i
= 0; i
< tp
->core_sib
; i
++) {
475 if (!strcmp(buf
, tp
->core_siblings
[i
]))
478 if (i
== tp
->core_sib
) {
479 tp
->core_siblings
[i
] = buf
;
487 sprintf(filename
, THRD_SIB_FMT
, cpu
);
488 fp
= fopen(filename
, "r");
492 if (getline(&buf
, &len
, fp
) <= 0)
495 p
= strchr(buf
, '\n');
499 for (i
= 0; i
< tp
->thread_sib
; i
++) {
500 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
503 if (i
== tp
->thread_sib
) {
504 tp
->thread_siblings
[i
] = buf
;
516 static void free_cpu_topo(struct cpu_topo
*tp
)
523 for (i
= 0 ; i
< tp
->core_sib
; i
++)
524 zfree(&tp
->core_siblings
[i
]);
526 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
527 zfree(&tp
->thread_siblings
[i
]);
532 static struct cpu_topo
*build_cpu_topology(void)
541 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
545 nr
= (u32
)(ncpus
& UINT_MAX
);
547 sz
= nr
* sizeof(char *);
549 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
556 tp
->core_siblings
= addr
;
558 tp
->thread_siblings
= addr
;
560 for (i
= 0; i
< nr
; i
++) {
561 ret
= build_cpu_topo(tp
, i
);
572 static int write_cpu_topology(int fd
, struct perf_header
*h __maybe_unused
,
573 struct perf_evlist
*evlist __maybe_unused
)
579 tp
= build_cpu_topology();
583 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
587 for (i
= 0; i
< tp
->core_sib
; i
++) {
588 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
592 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
596 for (i
= 0; i
< tp
->thread_sib
; i
++) {
597 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
608 static int write_total_mem(int fd
, struct perf_header
*h __maybe_unused
,
609 struct perf_evlist
*evlist __maybe_unused
)
617 fp
= fopen("/proc/meminfo", "r");
621 while (getline(&buf
, &len
, fp
) > 0) {
622 ret
= strncmp(buf
, "MemTotal:", 9);
627 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
629 ret
= do_write(fd
, &mem
, sizeof(mem
));
637 static int write_topo_node(int fd
, int node
)
639 char str
[MAXPATHLEN
];
641 char *buf
= NULL
, *p
;
644 u64 mem_total
, mem_free
, mem
;
647 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
648 fp
= fopen(str
, "r");
652 while (getline(&buf
, &len
, fp
) > 0) {
653 /* skip over invalid lines */
654 if (!strchr(buf
, ':'))
656 if (sscanf(buf
, "%*s %*d %31s %"PRIu64
, field
, &mem
) != 2)
658 if (!strcmp(field
, "MemTotal:"))
660 if (!strcmp(field
, "MemFree:"))
667 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
671 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
676 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
678 fp
= fopen(str
, "r");
682 if (getline(&buf
, &len
, fp
) <= 0)
685 p
= strchr(buf
, '\n');
689 ret
= do_write_string(fd
, buf
);
697 static int write_numa_topology(int fd
, struct perf_header
*h __maybe_unused
,
698 struct perf_evlist
*evlist __maybe_unused
)
703 struct cpu_map
*node_map
= NULL
;
708 fp
= fopen("/sys/devices/system/node/online", "r");
712 if (getline(&buf
, &len
, fp
) <= 0)
715 c
= strchr(buf
, '\n');
719 node_map
= cpu_map__new(buf
);
723 nr
= (u32
)node_map
->nr
;
725 ret
= do_write(fd
, &nr
, sizeof(nr
));
729 for (i
= 0; i
< nr
; i
++) {
730 j
= (u32
)node_map
->map
[i
];
731 ret
= do_write(fd
, &j
, sizeof(j
));
735 ret
= write_topo_node(fd
, i
);
749 * struct pmu_mappings {
758 static int write_pmu_mappings(int fd
, struct perf_header
*h __maybe_unused
,
759 struct perf_evlist
*evlist __maybe_unused
)
761 struct perf_pmu
*pmu
= NULL
;
762 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
766 /* write real pmu_num later */
767 ret
= do_write(fd
, &pmu_num
, sizeof(pmu_num
));
771 while ((pmu
= perf_pmu__scan(pmu
))) {
776 ret
= do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
780 ret
= do_write_string(fd
, pmu
->name
);
785 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
787 lseek(fd
, offset
, SEEK_SET
);
797 * struct group_descs {
799 * struct group_desc {
806 static int write_group_desc(int fd
, struct perf_header
*h __maybe_unused
,
807 struct perf_evlist
*evlist
)
809 u32 nr_groups
= evlist
->nr_groups
;
810 struct perf_evsel
*evsel
;
813 ret
= do_write(fd
, &nr_groups
, sizeof(nr_groups
));
817 evlist__for_each(evlist
, evsel
) {
818 if (perf_evsel__is_group_leader(evsel
) &&
819 evsel
->nr_members
> 1) {
820 const char *name
= evsel
->group_name
?: "{anon_group}";
821 u32 leader_idx
= evsel
->idx
;
822 u32 nr_members
= evsel
->nr_members
;
824 ret
= do_write_string(fd
, name
);
828 ret
= do_write(fd
, &leader_idx
, sizeof(leader_idx
));
832 ret
= do_write(fd
, &nr_members
, sizeof(nr_members
));
841 * default get_cpuid(): nothing gets recorded
842 * actual implementation must be in arch/$(ARCH)/util/header.c
844 int __attribute__ ((weak
)) get_cpuid(char *buffer __maybe_unused
,
845 size_t sz __maybe_unused
)
850 static int write_cpuid(int fd
, struct perf_header
*h __maybe_unused
,
851 struct perf_evlist
*evlist __maybe_unused
)
856 ret
= get_cpuid(buffer
, sizeof(buffer
));
862 return do_write_string(fd
, buffer
);
865 static int write_branch_stack(int fd __maybe_unused
,
866 struct perf_header
*h __maybe_unused
,
867 struct perf_evlist
*evlist __maybe_unused
)
872 static int write_auxtrace(int fd
, struct perf_header
*h
,
873 struct perf_evlist
*evlist __maybe_unused
)
875 struct perf_session
*session
;
878 session
= container_of(h
, struct perf_session
, header
);
880 err
= auxtrace_index__write(fd
, &session
->auxtrace_index
);
882 pr_err("Failed to write auxtrace index\n");
886 static void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
889 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
892 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
895 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
898 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
900 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
903 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
906 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
909 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
912 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
913 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
916 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
919 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
922 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
927 nr
= ph
->env
.nr_cmdline
;
929 fprintf(fp
, "# cmdline : ");
931 for (i
= 0; i
< nr
; i
++)
932 fprintf(fp
, "%s ", ph
->env
.cmdline_argv
[i
]);
936 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
942 nr
= ph
->env
.nr_sibling_cores
;
943 str
= ph
->env
.sibling_cores
;
945 for (i
= 0; i
< nr
; i
++) {
946 fprintf(fp
, "# sibling cores : %s\n", str
);
947 str
+= strlen(str
) + 1;
950 nr
= ph
->env
.nr_sibling_threads
;
951 str
= ph
->env
.sibling_threads
;
953 for (i
= 0; i
< nr
; i
++) {
954 fprintf(fp
, "# sibling threads : %s\n", str
);
955 str
+= strlen(str
) + 1;
959 static void free_event_desc(struct perf_evsel
*events
)
961 struct perf_evsel
*evsel
;
966 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
974 static struct perf_evsel
*
975 read_event_desc(struct perf_header
*ph
, int fd
)
977 struct perf_evsel
*evsel
, *events
= NULL
;
980 u32 nre
, sz
, nr
, i
, j
;
984 /* number of events */
985 ret
= readn(fd
, &nre
, sizeof(nre
));
986 if (ret
!= (ssize_t
)sizeof(nre
))
992 ret
= readn(fd
, &sz
, sizeof(sz
));
993 if (ret
!= (ssize_t
)sizeof(sz
))
999 /* buffer to hold on file attr struct */
1004 /* the last event terminates with evsel->attr.size == 0: */
1005 events
= calloc(nre
+ 1, sizeof(*events
));
1009 msz
= sizeof(evsel
->attr
);
1013 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1017 * must read entire on-file attr struct to
1018 * sync up with layout.
1020 ret
= readn(fd
, buf
, sz
);
1021 if (ret
!= (ssize_t
)sz
)
1025 perf_event__attr_swap(buf
);
1027 memcpy(&evsel
->attr
, buf
, msz
);
1029 ret
= readn(fd
, &nr
, sizeof(nr
));
1030 if (ret
!= (ssize_t
)sizeof(nr
))
1033 if (ph
->needs_swap
) {
1035 evsel
->needs_swap
= true;
1038 evsel
->name
= do_read_string(fd
, ph
);
1043 id
= calloc(nr
, sizeof(*id
));
1049 for (j
= 0 ; j
< nr
; j
++) {
1050 ret
= readn(fd
, id
, sizeof(*id
));
1051 if (ret
!= (ssize_t
)sizeof(*id
))
1054 *id
= bswap_64(*id
);
1062 free_event_desc(events
);
1067 static int __desc_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1068 void *priv
__attribute__((unused
)))
1070 return fprintf(fp
, ", %s = %s", name
, val
);
1073 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1075 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1080 fprintf(fp
, "# event desc: not available or unable to read\n");
1084 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1085 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1088 fprintf(fp
, ", id = {");
1089 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1092 fprintf(fp
, " %"PRIu64
, *id
);
1097 perf_event_attr__fprintf(fp
, &evsel
->attr
, __desc_attr__fprintf
, NULL
);
1102 free_event_desc(events
);
1105 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1108 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1111 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1116 uint64_t mem_total
, mem_free
;
1119 nr
= ph
->env
.nr_numa_nodes
;
1120 str
= ph
->env
.numa_nodes
;
1122 for (i
= 0; i
< nr
; i
++) {
1124 c
= strtoul(str
, &tmp
, 0);
1129 mem_total
= strtoull(str
, &tmp
, 0);
1134 mem_free
= strtoull(str
, &tmp
, 0);
1138 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1139 " free = %"PRIu64
" kB\n",
1140 c
, mem_total
, mem_free
);
1143 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1145 str
+= strlen(str
) + 1;
1149 fprintf(fp
, "# numa topology : not available\n");
1152 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1154 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1157 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1158 int fd __maybe_unused
, FILE *fp
)
1160 fprintf(fp
, "# contains samples with branch stack\n");
1163 static void print_auxtrace(struct perf_header
*ph __maybe_unused
,
1164 int fd __maybe_unused
, FILE *fp
)
1166 fprintf(fp
, "# contains AUX area data (e.g. instruction trace)\n");
1169 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1172 const char *delimiter
= "# pmu mappings: ";
1177 pmu_num
= ph
->env
.nr_pmu_mappings
;
1179 fprintf(fp
, "# pmu mappings: not available\n");
1183 str
= ph
->env
.pmu_mappings
;
1186 type
= strtoul(str
, &tmp
, 0);
1191 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1194 str
+= strlen(str
) + 1;
1203 fprintf(fp
, "# pmu mappings: unable to read\n");
1206 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1209 struct perf_session
*session
;
1210 struct perf_evsel
*evsel
;
1213 session
= container_of(ph
, struct perf_session
, header
);
1215 evlist__for_each(session
->evlist
, evsel
) {
1216 if (perf_evsel__is_group_leader(evsel
) &&
1217 evsel
->nr_members
> 1) {
1218 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1219 perf_evsel__name(evsel
));
1221 nr
= evsel
->nr_members
- 1;
1223 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1231 static int __event_process_build_id(struct build_id_event
*bev
,
1233 struct perf_session
*session
)
1236 struct machine
*machine
;
1239 enum dso_kernel_type dso_type
;
1241 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1245 cpumode
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1248 case PERF_RECORD_MISC_KERNEL
:
1249 dso_type
= DSO_TYPE_KERNEL
;
1251 case PERF_RECORD_MISC_GUEST_KERNEL
:
1252 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1254 case PERF_RECORD_MISC_USER
:
1255 case PERF_RECORD_MISC_GUEST_USER
:
1256 dso_type
= DSO_TYPE_USER
;
1262 dso
= machine__findnew_dso(machine
, filename
);
1264 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1266 dso__set_build_id(dso
, &bev
->build_id
);
1268 if (!is_kernel_module(filename
, cpumode
))
1269 dso
->kernel
= dso_type
;
1271 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1273 pr_debug("build id event received for %s: %s\n",
1274 dso
->long_name
, sbuild_id
);
1283 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1284 int input
, u64 offset
, u64 size
)
1286 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1288 struct perf_event_header header
;
1289 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1292 struct build_id_event bev
;
1293 char filename
[PATH_MAX
];
1294 u64 limit
= offset
+ size
;
1296 while (offset
< limit
) {
1299 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1302 if (header
->needs_swap
)
1303 perf_event_header__bswap(&old_bev
.header
);
1305 len
= old_bev
.header
.size
- sizeof(old_bev
);
1306 if (readn(input
, filename
, len
) != len
)
1309 bev
.header
= old_bev
.header
;
1312 * As the pid is the missing value, we need to fill
1313 * it properly. The header.misc value give us nice hint.
1315 bev
.pid
= HOST_KERNEL_ID
;
1316 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1317 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1318 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1320 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1321 __event_process_build_id(&bev
, filename
, session
);
1323 offset
+= bev
.header
.size
;
1329 static int perf_header__read_build_ids(struct perf_header
*header
,
1330 int input
, u64 offset
, u64 size
)
1332 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1333 struct build_id_event bev
;
1334 char filename
[PATH_MAX
];
1335 u64 limit
= offset
+ size
, orig_offset
= offset
;
1338 while (offset
< limit
) {
1341 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1344 if (header
->needs_swap
)
1345 perf_event_header__bswap(&bev
.header
);
1347 len
= bev
.header
.size
- sizeof(bev
);
1348 if (readn(input
, filename
, len
) != len
)
1351 * The a1645ce1 changeset:
1353 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1355 * Added a field to struct build_id_event that broke the file
1358 * Since the kernel build-id is the first entry, process the
1359 * table using the old format if the well known
1360 * '[kernel.kallsyms]' string for the kernel build-id has the
1361 * first 4 characters chopped off (where the pid_t sits).
1363 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1364 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1366 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1369 __event_process_build_id(&bev
, filename
, session
);
1371 offset
+= bev
.header
.size
;
1378 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1379 struct perf_header
*ph __maybe_unused
,
1382 ssize_t ret
= trace_report(fd
, data
, false);
1383 return ret
< 0 ? -1 : 0;
1386 static int process_build_id(struct perf_file_section
*section
,
1387 struct perf_header
*ph
, int fd
,
1388 void *data __maybe_unused
)
1390 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1391 pr_debug("Failed to read buildids, continuing...\n");
1395 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1396 struct perf_header
*ph
, int fd
,
1397 void *data __maybe_unused
)
1399 ph
->env
.hostname
= do_read_string(fd
, ph
);
1400 return ph
->env
.hostname
? 0 : -ENOMEM
;
1403 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1404 struct perf_header
*ph
, int fd
,
1405 void *data __maybe_unused
)
1407 ph
->env
.os_release
= do_read_string(fd
, ph
);
1408 return ph
->env
.os_release
? 0 : -ENOMEM
;
1411 static int process_version(struct perf_file_section
*section __maybe_unused
,
1412 struct perf_header
*ph
, int fd
,
1413 void *data __maybe_unused
)
1415 ph
->env
.version
= do_read_string(fd
, ph
);
1416 return ph
->env
.version
? 0 : -ENOMEM
;
1419 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1420 struct perf_header
*ph
, int fd
,
1421 void *data __maybe_unused
)
1423 ph
->env
.arch
= do_read_string(fd
, ph
);
1424 return ph
->env
.arch
? 0 : -ENOMEM
;
1427 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1428 struct perf_header
*ph
, int fd
,
1429 void *data __maybe_unused
)
1434 ret
= readn(fd
, &nr
, sizeof(nr
));
1435 if (ret
!= sizeof(nr
))
1441 ph
->env
.nr_cpus_online
= nr
;
1443 ret
= readn(fd
, &nr
, sizeof(nr
));
1444 if (ret
!= sizeof(nr
))
1450 ph
->env
.nr_cpus_avail
= nr
;
1454 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1455 struct perf_header
*ph
, int fd
,
1456 void *data __maybe_unused
)
1458 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1459 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1462 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1463 struct perf_header
*ph
, int fd
,
1464 void *data __maybe_unused
)
1466 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1467 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1470 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1471 struct perf_header
*ph
, int fd
,
1472 void *data __maybe_unused
)
1477 ret
= readn(fd
, &mem
, sizeof(mem
));
1478 if (ret
!= sizeof(mem
))
1482 mem
= bswap_64(mem
);
1484 ph
->env
.total_mem
= mem
;
1488 static struct perf_evsel
*
1489 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1491 struct perf_evsel
*evsel
;
1493 evlist__for_each(evlist
, evsel
) {
1494 if (evsel
->idx
== idx
)
1502 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1503 struct perf_evsel
*event
)
1505 struct perf_evsel
*evsel
;
1510 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1517 evsel
->name
= strdup(event
->name
);
1521 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1522 struct perf_header
*header
, int fd
,
1523 void *data __maybe_unused
)
1525 struct perf_session
*session
;
1526 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1531 session
= container_of(header
, struct perf_session
, header
);
1532 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1533 perf_evlist__set_event_name(session
->evlist
, evsel
);
1535 free_event_desc(events
);
1540 static int process_cmdline(struct perf_file_section
*section
,
1541 struct perf_header
*ph
, int fd
,
1542 void *data __maybe_unused
)
1545 char *str
, *cmdline
= NULL
, **argv
= NULL
;
1548 ret
= readn(fd
, &nr
, sizeof(nr
));
1549 if (ret
!= sizeof(nr
))
1555 ph
->env
.nr_cmdline
= nr
;
1557 cmdline
= zalloc(section
->size
+ nr
+ 1);
1561 argv
= zalloc(sizeof(char *) * (nr
+ 1));
1565 for (i
= 0; i
< nr
; i
++) {
1566 str
= do_read_string(fd
, ph
);
1570 argv
[i
] = cmdline
+ len
;
1571 memcpy(argv
[i
], str
, strlen(str
) + 1);
1572 len
+= strlen(str
) + 1;
1575 ph
->env
.cmdline
= cmdline
;
1576 ph
->env
.cmdline_argv
= (const char **) argv
;
1585 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1586 struct perf_header
*ph
, int fd
,
1587 void *data __maybe_unused
)
1594 ret
= readn(fd
, &nr
, sizeof(nr
));
1595 if (ret
!= sizeof(nr
))
1601 ph
->env
.nr_sibling_cores
= nr
;
1602 strbuf_init(&sb
, 128);
1604 for (i
= 0; i
< nr
; i
++) {
1605 str
= do_read_string(fd
, ph
);
1609 /* include a NULL character at the end */
1610 strbuf_add(&sb
, str
, strlen(str
) + 1);
1613 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1615 ret
= readn(fd
, &nr
, sizeof(nr
));
1616 if (ret
!= sizeof(nr
))
1622 ph
->env
.nr_sibling_threads
= nr
;
1624 for (i
= 0; i
< nr
; i
++) {
1625 str
= do_read_string(fd
, ph
);
1629 /* include a NULL character at the end */
1630 strbuf_add(&sb
, str
, strlen(str
) + 1);
1633 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1637 strbuf_release(&sb
);
1641 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1642 struct perf_header
*ph
, int fd
,
1643 void *data __maybe_unused
)
1648 uint64_t mem_total
, mem_free
;
1652 ret
= readn(fd
, &nr
, sizeof(nr
));
1653 if (ret
!= sizeof(nr
))
1659 ph
->env
.nr_numa_nodes
= nr
;
1660 strbuf_init(&sb
, 256);
1662 for (i
= 0; i
< nr
; i
++) {
1664 ret
= readn(fd
, &node
, sizeof(node
));
1665 if (ret
!= sizeof(node
))
1668 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1669 if (ret
!= sizeof(u64
))
1672 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1673 if (ret
!= sizeof(u64
))
1676 if (ph
->needs_swap
) {
1677 node
= bswap_32(node
);
1678 mem_total
= bswap_64(mem_total
);
1679 mem_free
= bswap_64(mem_free
);
1682 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1683 node
, mem_total
, mem_free
);
1685 str
= do_read_string(fd
, ph
);
1689 /* include a NULL character at the end */
1690 strbuf_add(&sb
, str
, strlen(str
) + 1);
1693 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
1697 strbuf_release(&sb
);
1701 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1702 struct perf_header
*ph
, int fd
,
1703 void *data __maybe_unused
)
1711 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1712 if (ret
!= sizeof(pmu_num
))
1716 pmu_num
= bswap_32(pmu_num
);
1719 pr_debug("pmu mappings not available\n");
1723 ph
->env
.nr_pmu_mappings
= pmu_num
;
1724 strbuf_init(&sb
, 128);
1727 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
1730 type
= bswap_32(type
);
1732 name
= do_read_string(fd
, ph
);
1736 strbuf_addf(&sb
, "%u:%s", type
, name
);
1737 /* include a NULL character at the end */
1738 strbuf_add(&sb
, "", 1);
1743 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
1747 strbuf_release(&sb
);
1751 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
1752 struct perf_header
*ph
, int fd
,
1753 void *data __maybe_unused
)
1756 u32 i
, nr
, nr_groups
;
1757 struct perf_session
*session
;
1758 struct perf_evsel
*evsel
, *leader
= NULL
;
1765 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
1769 nr_groups
= bswap_32(nr_groups
);
1771 ph
->env
.nr_groups
= nr_groups
;
1773 pr_debug("group desc not available\n");
1777 desc
= calloc(nr_groups
, sizeof(*desc
));
1781 for (i
= 0; i
< nr_groups
; i
++) {
1782 desc
[i
].name
= do_read_string(fd
, ph
);
1786 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
1789 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
1792 if (ph
->needs_swap
) {
1793 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
1794 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
1799 * Rebuild group relationship based on the group_desc
1801 session
= container_of(ph
, struct perf_session
, header
);
1802 session
->evlist
->nr_groups
= nr_groups
;
1805 evlist__for_each(session
->evlist
, evsel
) {
1806 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
1807 evsel
->leader
= evsel
;
1808 /* {anon_group} is a dummy name */
1809 if (strcmp(desc
[i
].name
, "{anon_group}")) {
1810 evsel
->group_name
= desc
[i
].name
;
1811 desc
[i
].name
= NULL
;
1813 evsel
->nr_members
= desc
[i
].nr_members
;
1815 if (i
>= nr_groups
|| nr
> 0) {
1816 pr_debug("invalid group desc\n");
1821 nr
= evsel
->nr_members
- 1;
1824 /* This is a group member */
1825 evsel
->leader
= leader
;
1831 if (i
!= nr_groups
|| nr
!= 0) {
1832 pr_debug("invalid group desc\n");
1838 for (i
= 0; i
< nr_groups
; i
++)
1839 zfree(&desc
[i
].name
);
1845 static int process_auxtrace(struct perf_file_section
*section
,
1846 struct perf_header
*ph
, int fd
,
1847 void *data __maybe_unused
)
1849 struct perf_session
*session
;
1852 session
= container_of(ph
, struct perf_session
, header
);
1854 err
= auxtrace_index__process(fd
, section
->size
, session
,
1857 pr_err("Failed to process auxtrace index\n");
1861 struct feature_ops
{
1862 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
1863 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
1864 int (*process
)(struct perf_file_section
*section
,
1865 struct perf_header
*h
, int fd
, void *data
);
1870 #define FEAT_OPA(n, func) \
1871 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1872 #define FEAT_OPP(n, func) \
1873 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1874 .process = process_##func }
1875 #define FEAT_OPF(n, func) \
1876 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1877 .process = process_##func, .full_only = true }
1879 /* feature_ops not implemented: */
1880 #define print_tracing_data NULL
1881 #define print_build_id NULL
1883 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
1884 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
1885 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
1886 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
1887 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
1888 FEAT_OPP(HEADER_VERSION
, version
),
1889 FEAT_OPP(HEADER_ARCH
, arch
),
1890 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
1891 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
1892 FEAT_OPP(HEADER_CPUID
, cpuid
),
1893 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
1894 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
1895 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
1896 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
1897 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
1898 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
1899 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
1900 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
1901 FEAT_OPP(HEADER_AUXTRACE
, auxtrace
),
1904 struct header_print_data
{
1906 bool full
; /* extended list of headers */
1909 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
1910 struct perf_header
*ph
,
1911 int feat
, int fd
, void *data
)
1913 struct header_print_data
*hd
= data
;
1915 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
1916 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
1917 "%d, continuing...\n", section
->offset
, feat
);
1920 if (feat
>= HEADER_LAST_FEATURE
) {
1921 pr_warning("unknown feature %d\n", feat
);
1924 if (!feat_ops
[feat
].print
)
1927 if (!feat_ops
[feat
].full_only
|| hd
->full
)
1928 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
1930 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
1931 feat_ops
[feat
].name
);
1936 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
1938 struct header_print_data hd
;
1939 struct perf_header
*header
= &session
->header
;
1940 int fd
= perf_data_file__fd(session
->file
);
1944 perf_header__process_sections(header
, fd
, &hd
,
1945 perf_file_section__fprintf_info
);
1949 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
1950 struct perf_file_section
**p
,
1951 struct perf_evlist
*evlist
)
1956 if (perf_header__has_feat(h
, type
)) {
1957 if (!feat_ops
[type
].write
)
1960 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
1962 err
= feat_ops
[type
].write(fd
, h
, evlist
);
1964 pr_debug("failed to write feature %d\n", type
);
1966 /* undo anything written */
1967 lseek(fd
, (*p
)->offset
, SEEK_SET
);
1971 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
1977 static int perf_header__adds_write(struct perf_header
*header
,
1978 struct perf_evlist
*evlist
, int fd
)
1981 struct perf_file_section
*feat_sec
, *p
;
1987 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1991 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
1992 if (feat_sec
== NULL
)
1995 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1997 sec_start
= header
->feat_offset
;
1998 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2000 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2001 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2002 perf_header__clear_feat(header
, feat
);
2005 lseek(fd
, sec_start
, SEEK_SET
);
2007 * may write more than needed due to dropped feature, but
2008 * this is okay, reader will skip the mising entries
2010 err
= do_write(fd
, feat_sec
, sec_size
);
2012 pr_debug("failed to write feature section\n");
2017 int perf_header__write_pipe(int fd
)
2019 struct perf_pipe_file_header f_header
;
2022 f_header
= (struct perf_pipe_file_header
){
2023 .magic
= PERF_MAGIC
,
2024 .size
= sizeof(f_header
),
2027 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2029 pr_debug("failed to write perf pipe header\n");
2036 int perf_session__write_header(struct perf_session
*session
,
2037 struct perf_evlist
*evlist
,
2038 int fd
, bool at_exit
)
2040 struct perf_file_header f_header
;
2041 struct perf_file_attr f_attr
;
2042 struct perf_header
*header
= &session
->header
;
2043 struct perf_evsel
*evsel
;
2047 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2049 evlist__for_each(session
->evlist
, evsel
) {
2050 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2051 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2053 pr_debug("failed to write perf header\n");
2058 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2060 evlist__for_each(evlist
, evsel
) {
2061 f_attr
= (struct perf_file_attr
){
2062 .attr
= evsel
->attr
,
2064 .offset
= evsel
->id_offset
,
2065 .size
= evsel
->ids
* sizeof(u64
),
2068 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2070 pr_debug("failed to write perf header attribute\n");
2075 if (!header
->data_offset
)
2076 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2077 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2080 err
= perf_header__adds_write(header
, evlist
, fd
);
2085 f_header
= (struct perf_file_header
){
2086 .magic
= PERF_MAGIC
,
2087 .size
= sizeof(f_header
),
2088 .attr_size
= sizeof(f_attr
),
2090 .offset
= attr_offset
,
2091 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2094 .offset
= header
->data_offset
,
2095 .size
= header
->data_size
,
2097 /* event_types is ignored, store zeros */
2100 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2102 lseek(fd
, 0, SEEK_SET
);
2103 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2105 pr_debug("failed to write perf header\n");
2108 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2113 static int perf_header__getbuffer64(struct perf_header
*header
,
2114 int fd
, void *buf
, size_t size
)
2116 if (readn(fd
, buf
, size
) <= 0)
2119 if (header
->needs_swap
)
2120 mem_bswap_64(buf
, size
);
2125 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2127 int (*process
)(struct perf_file_section
*section
,
2128 struct perf_header
*ph
,
2129 int feat
, int fd
, void *data
))
2131 struct perf_file_section
*feat_sec
, *sec
;
2137 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2141 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2145 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2147 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2149 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2153 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2154 err
= process(sec
++, header
, feat
, fd
, data
);
2164 static const int attr_file_abi_sizes
[] = {
2165 [0] = PERF_ATTR_SIZE_VER0
,
2166 [1] = PERF_ATTR_SIZE_VER1
,
2167 [2] = PERF_ATTR_SIZE_VER2
,
2168 [3] = PERF_ATTR_SIZE_VER3
,
2169 [4] = PERF_ATTR_SIZE_VER4
,
2174 * In the legacy file format, the magic number is not used to encode endianness.
2175 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2176 * on ABI revisions, we need to try all combinations for all endianness to
2177 * detect the endianness.
2179 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2181 uint64_t ref_size
, attr_size
;
2184 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2185 ref_size
= attr_file_abi_sizes
[i
]
2186 + sizeof(struct perf_file_section
);
2187 if (hdr_sz
!= ref_size
) {
2188 attr_size
= bswap_64(hdr_sz
);
2189 if (attr_size
!= ref_size
)
2192 ph
->needs_swap
= true;
2194 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2199 /* could not determine endianness */
2203 #define PERF_PIPE_HDR_VER0 16
2205 static const size_t attr_pipe_abi_sizes
[] = {
2206 [0] = PERF_PIPE_HDR_VER0
,
2211 * In the legacy pipe format, there is an implicit assumption that endiannesss
2212 * between host recording the samples, and host parsing the samples is the
2213 * same. This is not always the case given that the pipe output may always be
2214 * redirected into a file and analyzed on a different machine with possibly a
2215 * different endianness and perf_event ABI revsions in the perf tool itself.
2217 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2222 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2223 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2224 attr_size
= bswap_64(hdr_sz
);
2225 if (attr_size
!= hdr_sz
)
2228 ph
->needs_swap
= true;
2230 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2236 bool is_perf_magic(u64 magic
)
2238 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2239 || magic
== __perf_magic2
2240 || magic
== __perf_magic2_sw
)
2246 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2247 bool is_pipe
, struct perf_header
*ph
)
2251 /* check for legacy format */
2252 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2254 ph
->version
= PERF_HEADER_VERSION_1
;
2255 pr_debug("legacy perf.data format\n");
2257 return try_all_pipe_abis(hdr_sz
, ph
);
2259 return try_all_file_abis(hdr_sz
, ph
);
2262 * the new magic number serves two purposes:
2263 * - unique number to identify actual perf.data files
2264 * - encode endianness of file
2266 ph
->version
= PERF_HEADER_VERSION_2
;
2268 /* check magic number with one endianness */
2269 if (magic
== __perf_magic2
)
2272 /* check magic number with opposite endianness */
2273 if (magic
!= __perf_magic2_sw
)
2276 ph
->needs_swap
= true;
2281 int perf_file_header__read(struct perf_file_header
*header
,
2282 struct perf_header
*ph
, int fd
)
2286 lseek(fd
, 0, SEEK_SET
);
2288 ret
= readn(fd
, header
, sizeof(*header
));
2292 if (check_magic_endian(header
->magic
,
2293 header
->attr_size
, false, ph
) < 0) {
2294 pr_debug("magic/endian check failed\n");
2298 if (ph
->needs_swap
) {
2299 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2303 if (header
->size
!= sizeof(*header
)) {
2304 /* Support the previous format */
2305 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2306 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2309 } else if (ph
->needs_swap
) {
2311 * feature bitmap is declared as an array of unsigned longs --
2312 * not good since its size can differ between the host that
2313 * generated the data file and the host analyzing the file.
2315 * We need to handle endianness, but we don't know the size of
2316 * the unsigned long where the file was generated. Take a best
2317 * guess at determining it: try 64-bit swap first (ie., file
2318 * created on a 64-bit host), and check if the hostname feature
2319 * bit is set (this feature bit is forced on as of fbe96f2).
2320 * If the bit is not, undo the 64-bit swap and try a 32-bit
2321 * swap. If the hostname bit is still not set (e.g., older data
2322 * file), punt and fallback to the original behavior --
2323 * clearing all feature bits and setting buildid.
2325 mem_bswap_64(&header
->adds_features
,
2326 BITS_TO_U64(HEADER_FEAT_BITS
));
2328 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2330 mem_bswap_64(&header
->adds_features
,
2331 BITS_TO_U64(HEADER_FEAT_BITS
));
2334 mem_bswap_32(&header
->adds_features
,
2335 BITS_TO_U32(HEADER_FEAT_BITS
));
2338 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2339 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2340 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2344 memcpy(&ph
->adds_features
, &header
->adds_features
,
2345 sizeof(ph
->adds_features
));
2347 ph
->data_offset
= header
->data
.offset
;
2348 ph
->data_size
= header
->data
.size
;
2349 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2353 static int perf_file_section__process(struct perf_file_section
*section
,
2354 struct perf_header
*ph
,
2355 int feat
, int fd
, void *data
)
2357 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2358 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2359 "%d, continuing...\n", section
->offset
, feat
);
2363 if (feat
>= HEADER_LAST_FEATURE
) {
2364 pr_debug("unknown feature %d, continuing...\n", feat
);
2368 if (!feat_ops
[feat
].process
)
2371 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2374 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2375 struct perf_header
*ph
, int fd
,
2380 ret
= readn(fd
, header
, sizeof(*header
));
2384 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2385 pr_debug("endian/magic failed\n");
2390 header
->size
= bswap_64(header
->size
);
2392 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2398 static int perf_header__read_pipe(struct perf_session
*session
)
2400 struct perf_header
*header
= &session
->header
;
2401 struct perf_pipe_file_header f_header
;
2403 if (perf_file_header__read_pipe(&f_header
, header
,
2404 perf_data_file__fd(session
->file
),
2405 session
->repipe
) < 0) {
2406 pr_debug("incompatible file format\n");
2413 static int read_attr(int fd
, struct perf_header
*ph
,
2414 struct perf_file_attr
*f_attr
)
2416 struct perf_event_attr
*attr
= &f_attr
->attr
;
2418 size_t our_sz
= sizeof(f_attr
->attr
);
2421 memset(f_attr
, 0, sizeof(*f_attr
));
2423 /* read minimal guaranteed structure */
2424 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2426 pr_debug("cannot read %d bytes of header attr\n",
2427 PERF_ATTR_SIZE_VER0
);
2431 /* on file perf_event_attr size */
2439 sz
= PERF_ATTR_SIZE_VER0
;
2440 } else if (sz
> our_sz
) {
2441 pr_debug("file uses a more recent and unsupported ABI"
2442 " (%zu bytes extra)\n", sz
- our_sz
);
2445 /* what we have not yet read and that we know about */
2446 left
= sz
- PERF_ATTR_SIZE_VER0
;
2449 ptr
+= PERF_ATTR_SIZE_VER0
;
2451 ret
= readn(fd
, ptr
, left
);
2453 /* read perf_file_section, ids are read in caller */
2454 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2456 return ret
<= 0 ? -1 : 0;
2459 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2460 struct pevent
*pevent
)
2462 struct event_format
*event
;
2465 /* already prepared */
2466 if (evsel
->tp_format
)
2469 if (pevent
== NULL
) {
2470 pr_debug("broken or missing trace data\n");
2474 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2479 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2480 evsel
->name
= strdup(bf
);
2481 if (evsel
->name
== NULL
)
2485 evsel
->tp_format
= event
;
2489 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2490 struct pevent
*pevent
)
2492 struct perf_evsel
*pos
;
2494 evlist__for_each(evlist
, pos
) {
2495 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2496 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2503 int perf_session__read_header(struct perf_session
*session
)
2505 struct perf_data_file
*file
= session
->file
;
2506 struct perf_header
*header
= &session
->header
;
2507 struct perf_file_header f_header
;
2508 struct perf_file_attr f_attr
;
2510 int nr_attrs
, nr_ids
, i
, j
;
2511 int fd
= perf_data_file__fd(file
);
2513 session
->evlist
= perf_evlist__new();
2514 if (session
->evlist
== NULL
)
2517 if (perf_data_file__is_pipe(file
))
2518 return perf_header__read_pipe(session
);
2520 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2524 * Sanity check that perf.data was written cleanly; data size is
2525 * initialized to 0 and updated only if the on_exit function is run.
2526 * If data size is still 0 then the file contains only partial
2527 * information. Just warn user and process it as much as it can.
2529 if (f_header
.data
.size
== 0) {
2530 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2531 "Was the 'perf record' command properly terminated?\n",
2535 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2536 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2538 for (i
= 0; i
< nr_attrs
; i
++) {
2539 struct perf_evsel
*evsel
;
2542 if (read_attr(fd
, header
, &f_attr
) < 0)
2545 if (header
->needs_swap
) {
2546 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
2547 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
2548 perf_event__attr_swap(&f_attr
.attr
);
2551 tmp
= lseek(fd
, 0, SEEK_CUR
);
2552 evsel
= perf_evsel__new(&f_attr
.attr
);
2555 goto out_delete_evlist
;
2557 evsel
->needs_swap
= header
->needs_swap
;
2559 * Do it before so that if perf_evsel__alloc_id fails, this
2560 * entry gets purged too at perf_evlist__delete().
2562 perf_evlist__add(session
->evlist
, evsel
);
2564 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2566 * We don't have the cpu and thread maps on the header, so
2567 * for allocating the perf_sample_id table we fake 1 cpu and
2568 * hattr->ids threads.
2570 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2571 goto out_delete_evlist
;
2573 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2575 for (j
= 0; j
< nr_ids
; j
++) {
2576 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2579 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2582 lseek(fd
, tmp
, SEEK_SET
);
2585 symbol_conf
.nr_events
= nr_attrs
;
2587 perf_header__process_sections(header
, fd
, &session
->tevent
,
2588 perf_file_section__process
);
2590 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2591 session
->tevent
.pevent
))
2592 goto out_delete_evlist
;
2599 perf_evlist__delete(session
->evlist
);
2600 session
->evlist
= NULL
;
2604 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2605 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2606 perf_event__handler_t process
)
2608 union perf_event
*ev
;
2612 size
= sizeof(struct perf_event_attr
);
2613 size
= PERF_ALIGN(size
, sizeof(u64
));
2614 size
+= sizeof(struct perf_event_header
);
2615 size
+= ids
* sizeof(u64
);
2622 ev
->attr
.attr
= *attr
;
2623 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2625 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2626 ev
->attr
.header
.size
= (u16
)size
;
2628 if (ev
->attr
.header
.size
== size
)
2629 err
= process(tool
, ev
, NULL
, NULL
);
2638 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2639 struct perf_session
*session
,
2640 perf_event__handler_t process
)
2642 struct perf_evsel
*evsel
;
2645 evlist__for_each(session
->evlist
, evsel
) {
2646 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2647 evsel
->id
, process
);
2649 pr_debug("failed to create perf header attribute\n");
2657 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
2658 union perf_event
*event
,
2659 struct perf_evlist
**pevlist
)
2662 struct perf_evsel
*evsel
;
2663 struct perf_evlist
*evlist
= *pevlist
;
2665 if (evlist
== NULL
) {
2666 *pevlist
= evlist
= perf_evlist__new();
2671 evsel
= perf_evsel__new(&event
->attr
.attr
);
2675 perf_evlist__add(evlist
, evsel
);
2677 ids
= event
->header
.size
;
2678 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2679 n_ids
= ids
/ sizeof(u64
);
2681 * We don't have the cpu and thread maps on the header, so
2682 * for allocating the perf_sample_id table we fake 1 cpu and
2683 * hattr->ids threads.
2685 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2688 for (i
= 0; i
< n_ids
; i
++) {
2689 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2692 symbol_conf
.nr_events
= evlist
->nr_entries
;
2697 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2698 struct perf_evlist
*evlist
,
2699 perf_event__handler_t process
)
2701 union perf_event ev
;
2702 struct tracing_data
*tdata
;
2703 ssize_t size
= 0, aligned_size
= 0, padding
;
2704 int err __maybe_unused
= 0;
2707 * We are going to store the size of the data followed
2708 * by the data contents. Since the fd descriptor is a pipe,
2709 * we cannot seek back to store the size of the data once
2710 * we know it. Instead we:
2712 * - write the tracing data to the temp file
2713 * - get/write the data size to pipe
2714 * - write the tracing data from the temp file
2717 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
2721 memset(&ev
, 0, sizeof(ev
));
2723 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
2725 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
2726 padding
= aligned_size
- size
;
2727 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
2728 ev
.tracing_data
.size
= aligned_size
;
2730 process(tool
, &ev
, NULL
, NULL
);
2733 * The put function will copy all the tracing data
2734 * stored in temp file to the pipe.
2736 tracing_data_put(tdata
);
2738 write_padded(fd
, NULL
, 0, padding
);
2740 return aligned_size
;
2743 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
2744 union perf_event
*event
,
2745 struct perf_session
*session
)
2747 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
2748 int fd
= perf_data_file__fd(session
->file
);
2749 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
2752 /* setup for reading amidst mmap */
2753 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
2756 size_read
= trace_report(fd
, &session
->tevent
,
2758 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
2760 if (readn(fd
, buf
, padding
) < 0) {
2761 pr_err("%s: reading input file", __func__
);
2764 if (session
->repipe
) {
2765 int retw
= write(STDOUT_FILENO
, buf
, padding
);
2766 if (retw
<= 0 || retw
!= padding
) {
2767 pr_err("%s: repiping tracing data padding", __func__
);
2772 if (size_read
+ padding
!= size
) {
2773 pr_err("%s: tracing data size mismatch", __func__
);
2777 perf_evlist__prepare_tracepoint_events(session
->evlist
,
2778 session
->tevent
.pevent
);
2780 return size_read
+ padding
;
2783 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
2784 struct dso
*pos
, u16 misc
,
2785 perf_event__handler_t process
,
2786 struct machine
*machine
)
2788 union perf_event ev
;
2795 memset(&ev
, 0, sizeof(ev
));
2797 len
= pos
->long_name_len
+ 1;
2798 len
= PERF_ALIGN(len
, NAME_ALIGN
);
2799 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
2800 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
2801 ev
.build_id
.header
.misc
= misc
;
2802 ev
.build_id
.pid
= machine
->pid
;
2803 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
2804 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
2806 err
= process(tool
, &ev
, NULL
, machine
);
2811 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
2812 union perf_event
*event
,
2813 struct perf_session
*session
)
2815 __event_process_build_id(&event
->build_id
,
2816 event
->build_id
.filename
,