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
,
928 nr
= ph
->env
.nr_cmdline
;
929 str
= ph
->env
.cmdline
;
931 fprintf(fp
, "# cmdline : ");
933 for (i
= 0; i
< nr
; i
++) {
934 fprintf(fp
, "%s ", str
);
935 str
+= strlen(str
) + 1;
940 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
946 nr
= ph
->env
.nr_sibling_cores
;
947 str
= ph
->env
.sibling_cores
;
949 for (i
= 0; i
< nr
; i
++) {
950 fprintf(fp
, "# sibling cores : %s\n", str
);
951 str
+= strlen(str
) + 1;
954 nr
= ph
->env
.nr_sibling_threads
;
955 str
= ph
->env
.sibling_threads
;
957 for (i
= 0; i
< nr
; i
++) {
958 fprintf(fp
, "# sibling threads : %s\n", str
);
959 str
+= strlen(str
) + 1;
963 static void free_event_desc(struct perf_evsel
*events
)
965 struct perf_evsel
*evsel
;
970 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
978 static struct perf_evsel
*
979 read_event_desc(struct perf_header
*ph
, int fd
)
981 struct perf_evsel
*evsel
, *events
= NULL
;
984 u32 nre
, sz
, nr
, i
, j
;
988 /* number of events */
989 ret
= readn(fd
, &nre
, sizeof(nre
));
990 if (ret
!= (ssize_t
)sizeof(nre
))
996 ret
= readn(fd
, &sz
, sizeof(sz
));
997 if (ret
!= (ssize_t
)sizeof(sz
))
1003 /* buffer to hold on file attr struct */
1008 /* the last event terminates with evsel->attr.size == 0: */
1009 events
= calloc(nre
+ 1, sizeof(*events
));
1013 msz
= sizeof(evsel
->attr
);
1017 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1021 * must read entire on-file attr struct to
1022 * sync up with layout.
1024 ret
= readn(fd
, buf
, sz
);
1025 if (ret
!= (ssize_t
)sz
)
1029 perf_event__attr_swap(buf
);
1031 memcpy(&evsel
->attr
, buf
, msz
);
1033 ret
= readn(fd
, &nr
, sizeof(nr
));
1034 if (ret
!= (ssize_t
)sizeof(nr
))
1037 if (ph
->needs_swap
) {
1039 evsel
->needs_swap
= true;
1042 evsel
->name
= do_read_string(fd
, ph
);
1047 id
= calloc(nr
, sizeof(*id
));
1053 for (j
= 0 ; j
< nr
; j
++) {
1054 ret
= readn(fd
, id
, sizeof(*id
));
1055 if (ret
!= (ssize_t
)sizeof(*id
))
1058 *id
= bswap_64(*id
);
1067 free_event_desc(events
);
1072 static int __desc_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1073 void *priv
__attribute__((unused
)))
1075 return fprintf(fp
, ", %s = %s", name
, val
);
1078 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1080 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1085 fprintf(fp
, "# event desc: not available or unable to read\n");
1089 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1090 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1093 fprintf(fp
, ", id = {");
1094 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1097 fprintf(fp
, " %"PRIu64
, *id
);
1102 perf_event_attr__fprintf(fp
, &evsel
->attr
, __desc_attr__fprintf
, NULL
);
1107 free_event_desc(events
);
1110 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1113 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1116 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1121 uint64_t mem_total
, mem_free
;
1124 nr
= ph
->env
.nr_numa_nodes
;
1125 str
= ph
->env
.numa_nodes
;
1127 for (i
= 0; i
< nr
; i
++) {
1129 c
= strtoul(str
, &tmp
, 0);
1134 mem_total
= strtoull(str
, &tmp
, 0);
1139 mem_free
= strtoull(str
, &tmp
, 0);
1143 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1144 " free = %"PRIu64
" kB\n",
1145 c
, mem_total
, mem_free
);
1148 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1150 str
+= strlen(str
) + 1;
1154 fprintf(fp
, "# numa topology : not available\n");
1157 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1159 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1162 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1163 int fd __maybe_unused
, FILE *fp
)
1165 fprintf(fp
, "# contains samples with branch stack\n");
1168 static void print_auxtrace(struct perf_header
*ph __maybe_unused
,
1169 int fd __maybe_unused
, FILE *fp
)
1171 fprintf(fp
, "# contains AUX area data (e.g. instruction trace)\n");
1174 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1177 const char *delimiter
= "# pmu mappings: ";
1182 pmu_num
= ph
->env
.nr_pmu_mappings
;
1184 fprintf(fp
, "# pmu mappings: not available\n");
1188 str
= ph
->env
.pmu_mappings
;
1191 type
= strtoul(str
, &tmp
, 0);
1196 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1199 str
+= strlen(str
) + 1;
1208 fprintf(fp
, "# pmu mappings: unable to read\n");
1211 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1214 struct perf_session
*session
;
1215 struct perf_evsel
*evsel
;
1218 session
= container_of(ph
, struct perf_session
, header
);
1220 evlist__for_each(session
->evlist
, evsel
) {
1221 if (perf_evsel__is_group_leader(evsel
) &&
1222 evsel
->nr_members
> 1) {
1223 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1224 perf_evsel__name(evsel
));
1226 nr
= evsel
->nr_members
- 1;
1228 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1236 static int __event_process_build_id(struct build_id_event
*bev
,
1238 struct perf_session
*session
)
1241 struct machine
*machine
;
1244 enum dso_kernel_type dso_type
;
1246 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1250 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1253 case PERF_RECORD_MISC_KERNEL
:
1254 dso_type
= DSO_TYPE_KERNEL
;
1256 case PERF_RECORD_MISC_GUEST_KERNEL
:
1257 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1259 case PERF_RECORD_MISC_USER
:
1260 case PERF_RECORD_MISC_GUEST_USER
:
1261 dso_type
= DSO_TYPE_USER
;
1267 dso
= machine__findnew_dso(machine
, filename
);
1269 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1271 dso__set_build_id(dso
, &bev
->build_id
);
1273 if (!is_kernel_module(filename
))
1274 dso
->kernel
= dso_type
;
1276 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1278 pr_debug("build id event received for %s: %s\n",
1279 dso
->long_name
, sbuild_id
);
1287 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1288 int input
, u64 offset
, u64 size
)
1290 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1292 struct perf_event_header header
;
1293 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1296 struct build_id_event bev
;
1297 char filename
[PATH_MAX
];
1298 u64 limit
= offset
+ size
;
1300 while (offset
< limit
) {
1303 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1306 if (header
->needs_swap
)
1307 perf_event_header__bswap(&old_bev
.header
);
1309 len
= old_bev
.header
.size
- sizeof(old_bev
);
1310 if (readn(input
, filename
, len
) != len
)
1313 bev
.header
= old_bev
.header
;
1316 * As the pid is the missing value, we need to fill
1317 * it properly. The header.misc value give us nice hint.
1319 bev
.pid
= HOST_KERNEL_ID
;
1320 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1321 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1322 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1324 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1325 __event_process_build_id(&bev
, filename
, session
);
1327 offset
+= bev
.header
.size
;
1333 static int perf_header__read_build_ids(struct perf_header
*header
,
1334 int input
, u64 offset
, u64 size
)
1336 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1337 struct build_id_event bev
;
1338 char filename
[PATH_MAX
];
1339 u64 limit
= offset
+ size
, orig_offset
= offset
;
1342 while (offset
< limit
) {
1345 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1348 if (header
->needs_swap
)
1349 perf_event_header__bswap(&bev
.header
);
1351 len
= bev
.header
.size
- sizeof(bev
);
1352 if (readn(input
, filename
, len
) != len
)
1355 * The a1645ce1 changeset:
1357 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1359 * Added a field to struct build_id_event that broke the file
1362 * Since the kernel build-id is the first entry, process the
1363 * table using the old format if the well known
1364 * '[kernel.kallsyms]' string for the kernel build-id has the
1365 * first 4 characters chopped off (where the pid_t sits).
1367 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1368 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1370 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1373 __event_process_build_id(&bev
, filename
, session
);
1375 offset
+= bev
.header
.size
;
1382 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1383 struct perf_header
*ph __maybe_unused
,
1386 ssize_t ret
= trace_report(fd
, data
, false);
1387 return ret
< 0 ? -1 : 0;
1390 static int process_build_id(struct perf_file_section
*section
,
1391 struct perf_header
*ph
, int fd
,
1392 void *data __maybe_unused
)
1394 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1395 pr_debug("Failed to read buildids, continuing...\n");
1399 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1400 struct perf_header
*ph
, int fd
,
1401 void *data __maybe_unused
)
1403 ph
->env
.hostname
= do_read_string(fd
, ph
);
1404 return ph
->env
.hostname
? 0 : -ENOMEM
;
1407 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1408 struct perf_header
*ph
, int fd
,
1409 void *data __maybe_unused
)
1411 ph
->env
.os_release
= do_read_string(fd
, ph
);
1412 return ph
->env
.os_release
? 0 : -ENOMEM
;
1415 static int process_version(struct perf_file_section
*section __maybe_unused
,
1416 struct perf_header
*ph
, int fd
,
1417 void *data __maybe_unused
)
1419 ph
->env
.version
= do_read_string(fd
, ph
);
1420 return ph
->env
.version
? 0 : -ENOMEM
;
1423 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1424 struct perf_header
*ph
, int fd
,
1425 void *data __maybe_unused
)
1427 ph
->env
.arch
= do_read_string(fd
, ph
);
1428 return ph
->env
.arch
? 0 : -ENOMEM
;
1431 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1432 struct perf_header
*ph
, int fd
,
1433 void *data __maybe_unused
)
1438 ret
= readn(fd
, &nr
, sizeof(nr
));
1439 if (ret
!= sizeof(nr
))
1445 ph
->env
.nr_cpus_online
= nr
;
1447 ret
= readn(fd
, &nr
, sizeof(nr
));
1448 if (ret
!= sizeof(nr
))
1454 ph
->env
.nr_cpus_avail
= nr
;
1458 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1459 struct perf_header
*ph
, int fd
,
1460 void *data __maybe_unused
)
1462 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1463 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1466 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1467 struct perf_header
*ph
, int fd
,
1468 void *data __maybe_unused
)
1470 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1471 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1474 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1475 struct perf_header
*ph
, int fd
,
1476 void *data __maybe_unused
)
1481 ret
= readn(fd
, &mem
, sizeof(mem
));
1482 if (ret
!= sizeof(mem
))
1486 mem
= bswap_64(mem
);
1488 ph
->env
.total_mem
= mem
;
1492 static struct perf_evsel
*
1493 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1495 struct perf_evsel
*evsel
;
1497 evlist__for_each(evlist
, evsel
) {
1498 if (evsel
->idx
== idx
)
1506 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1507 struct perf_evsel
*event
)
1509 struct perf_evsel
*evsel
;
1514 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1521 evsel
->name
= strdup(event
->name
);
1525 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1526 struct perf_header
*header
, int fd
,
1527 void *data __maybe_unused
)
1529 struct perf_session
*session
;
1530 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1535 session
= container_of(header
, struct perf_session
, header
);
1536 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1537 perf_evlist__set_event_name(session
->evlist
, evsel
);
1539 free_event_desc(events
);
1544 static int process_cmdline(struct perf_file_section
*section __maybe_unused
,
1545 struct perf_header
*ph
, int fd
,
1546 void *data __maybe_unused
)
1553 ret
= readn(fd
, &nr
, sizeof(nr
));
1554 if (ret
!= sizeof(nr
))
1560 ph
->env
.nr_cmdline
= nr
;
1561 strbuf_init(&sb
, 128);
1563 for (i
= 0; i
< nr
; i
++) {
1564 str
= do_read_string(fd
, ph
);
1568 /* include a NULL character at the end */
1569 strbuf_add(&sb
, str
, strlen(str
) + 1);
1572 ph
->env
.cmdline
= strbuf_detach(&sb
, NULL
);
1576 strbuf_release(&sb
);
1580 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1581 struct perf_header
*ph
, int fd
,
1582 void *data __maybe_unused
)
1589 ret
= readn(fd
, &nr
, sizeof(nr
));
1590 if (ret
!= sizeof(nr
))
1596 ph
->env
.nr_sibling_cores
= nr
;
1597 strbuf_init(&sb
, 128);
1599 for (i
= 0; i
< nr
; i
++) {
1600 str
= do_read_string(fd
, ph
);
1604 /* include a NULL character at the end */
1605 strbuf_add(&sb
, str
, strlen(str
) + 1);
1608 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1610 ret
= readn(fd
, &nr
, sizeof(nr
));
1611 if (ret
!= sizeof(nr
))
1617 ph
->env
.nr_sibling_threads
= nr
;
1619 for (i
= 0; i
< nr
; i
++) {
1620 str
= do_read_string(fd
, ph
);
1624 /* include a NULL character at the end */
1625 strbuf_add(&sb
, str
, strlen(str
) + 1);
1628 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1632 strbuf_release(&sb
);
1636 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1637 struct perf_header
*ph
, int fd
,
1638 void *data __maybe_unused
)
1643 uint64_t mem_total
, mem_free
;
1647 ret
= readn(fd
, &nr
, sizeof(nr
));
1648 if (ret
!= sizeof(nr
))
1654 ph
->env
.nr_numa_nodes
= nr
;
1655 strbuf_init(&sb
, 256);
1657 for (i
= 0; i
< nr
; i
++) {
1659 ret
= readn(fd
, &node
, sizeof(node
));
1660 if (ret
!= sizeof(node
))
1663 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1664 if (ret
!= sizeof(u64
))
1667 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1668 if (ret
!= sizeof(u64
))
1671 if (ph
->needs_swap
) {
1672 node
= bswap_32(node
);
1673 mem_total
= bswap_64(mem_total
);
1674 mem_free
= bswap_64(mem_free
);
1677 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1678 node
, mem_total
, mem_free
);
1680 str
= do_read_string(fd
, ph
);
1684 /* include a NULL character at the end */
1685 strbuf_add(&sb
, str
, strlen(str
) + 1);
1688 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
1692 strbuf_release(&sb
);
1696 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1697 struct perf_header
*ph
, int fd
,
1698 void *data __maybe_unused
)
1706 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1707 if (ret
!= sizeof(pmu_num
))
1711 pmu_num
= bswap_32(pmu_num
);
1714 pr_debug("pmu mappings not available\n");
1718 ph
->env
.nr_pmu_mappings
= pmu_num
;
1719 strbuf_init(&sb
, 128);
1722 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
1725 type
= bswap_32(type
);
1727 name
= do_read_string(fd
, ph
);
1731 strbuf_addf(&sb
, "%u:%s", type
, name
);
1732 /* include a NULL character at the end */
1733 strbuf_add(&sb
, "", 1);
1738 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
1742 strbuf_release(&sb
);
1746 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
1747 struct perf_header
*ph
, int fd
,
1748 void *data __maybe_unused
)
1751 u32 i
, nr
, nr_groups
;
1752 struct perf_session
*session
;
1753 struct perf_evsel
*evsel
, *leader
= NULL
;
1760 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
1764 nr_groups
= bswap_32(nr_groups
);
1766 ph
->env
.nr_groups
= nr_groups
;
1768 pr_debug("group desc not available\n");
1772 desc
= calloc(nr_groups
, sizeof(*desc
));
1776 for (i
= 0; i
< nr_groups
; i
++) {
1777 desc
[i
].name
= do_read_string(fd
, ph
);
1781 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
1784 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
1787 if (ph
->needs_swap
) {
1788 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
1789 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
1794 * Rebuild group relationship based on the group_desc
1796 session
= container_of(ph
, struct perf_session
, header
);
1797 session
->evlist
->nr_groups
= nr_groups
;
1800 evlist__for_each(session
->evlist
, evsel
) {
1801 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
1802 evsel
->leader
= evsel
;
1803 /* {anon_group} is a dummy name */
1804 if (strcmp(desc
[i
].name
, "{anon_group}")) {
1805 evsel
->group_name
= desc
[i
].name
;
1806 desc
[i
].name
= NULL
;
1808 evsel
->nr_members
= desc
[i
].nr_members
;
1810 if (i
>= nr_groups
|| nr
> 0) {
1811 pr_debug("invalid group desc\n");
1816 nr
= evsel
->nr_members
- 1;
1819 /* This is a group member */
1820 evsel
->leader
= leader
;
1826 if (i
!= nr_groups
|| nr
!= 0) {
1827 pr_debug("invalid group desc\n");
1833 for (i
= 0; i
< nr_groups
; i
++)
1834 zfree(&desc
[i
].name
);
1840 static int process_auxtrace(struct perf_file_section
*section
,
1841 struct perf_header
*ph
, int fd
,
1842 void *data __maybe_unused
)
1844 struct perf_session
*session
;
1847 session
= container_of(ph
, struct perf_session
, header
);
1849 err
= auxtrace_index__process(fd
, section
->size
, session
,
1852 pr_err("Failed to process auxtrace index\n");
1856 struct feature_ops
{
1857 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
1858 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
1859 int (*process
)(struct perf_file_section
*section
,
1860 struct perf_header
*h
, int fd
, void *data
);
1865 #define FEAT_OPA(n, func) \
1866 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1867 #define FEAT_OPP(n, func) \
1868 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1869 .process = process_##func }
1870 #define FEAT_OPF(n, func) \
1871 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1872 .process = process_##func, .full_only = true }
1874 /* feature_ops not implemented: */
1875 #define print_tracing_data NULL
1876 #define print_build_id NULL
1878 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
1879 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
1880 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
1881 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
1882 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
1883 FEAT_OPP(HEADER_VERSION
, version
),
1884 FEAT_OPP(HEADER_ARCH
, arch
),
1885 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
1886 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
1887 FEAT_OPP(HEADER_CPUID
, cpuid
),
1888 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
1889 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
1890 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
1891 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
1892 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
1893 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
1894 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
1895 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
1896 FEAT_OPP(HEADER_AUXTRACE
, auxtrace
),
1899 struct header_print_data
{
1901 bool full
; /* extended list of headers */
1904 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
1905 struct perf_header
*ph
,
1906 int feat
, int fd
, void *data
)
1908 struct header_print_data
*hd
= data
;
1910 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
1911 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
1912 "%d, continuing...\n", section
->offset
, feat
);
1915 if (feat
>= HEADER_LAST_FEATURE
) {
1916 pr_warning("unknown feature %d\n", feat
);
1919 if (!feat_ops
[feat
].print
)
1922 if (!feat_ops
[feat
].full_only
|| hd
->full
)
1923 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
1925 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
1926 feat_ops
[feat
].name
);
1931 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
1933 struct header_print_data hd
;
1934 struct perf_header
*header
= &session
->header
;
1935 int fd
= perf_data_file__fd(session
->file
);
1939 perf_header__process_sections(header
, fd
, &hd
,
1940 perf_file_section__fprintf_info
);
1944 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
1945 struct perf_file_section
**p
,
1946 struct perf_evlist
*evlist
)
1951 if (perf_header__has_feat(h
, type
)) {
1952 if (!feat_ops
[type
].write
)
1955 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
1957 err
= feat_ops
[type
].write(fd
, h
, evlist
);
1959 pr_debug("failed to write feature %d\n", type
);
1961 /* undo anything written */
1962 lseek(fd
, (*p
)->offset
, SEEK_SET
);
1966 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
1972 static int perf_header__adds_write(struct perf_header
*header
,
1973 struct perf_evlist
*evlist
, int fd
)
1976 struct perf_file_section
*feat_sec
, *p
;
1982 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1986 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
1987 if (feat_sec
== NULL
)
1990 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1992 sec_start
= header
->feat_offset
;
1993 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
1995 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
1996 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
1997 perf_header__clear_feat(header
, feat
);
2000 lseek(fd
, sec_start
, SEEK_SET
);
2002 * may write more than needed due to dropped feature, but
2003 * this is okay, reader will skip the mising entries
2005 err
= do_write(fd
, feat_sec
, sec_size
);
2007 pr_debug("failed to write feature section\n");
2012 int perf_header__write_pipe(int fd
)
2014 struct perf_pipe_file_header f_header
;
2017 f_header
= (struct perf_pipe_file_header
){
2018 .magic
= PERF_MAGIC
,
2019 .size
= sizeof(f_header
),
2022 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2024 pr_debug("failed to write perf pipe header\n");
2031 int perf_session__write_header(struct perf_session
*session
,
2032 struct perf_evlist
*evlist
,
2033 int fd
, bool at_exit
)
2035 struct perf_file_header f_header
;
2036 struct perf_file_attr f_attr
;
2037 struct perf_header
*header
= &session
->header
;
2038 struct perf_evsel
*evsel
;
2042 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2044 evlist__for_each(session
->evlist
, evsel
) {
2045 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2046 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2048 pr_debug("failed to write perf header\n");
2053 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2055 evlist__for_each(evlist
, evsel
) {
2056 f_attr
= (struct perf_file_attr
){
2057 .attr
= evsel
->attr
,
2059 .offset
= evsel
->id_offset
,
2060 .size
= evsel
->ids
* sizeof(u64
),
2063 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2065 pr_debug("failed to write perf header attribute\n");
2070 if (!header
->data_offset
)
2071 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2072 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2075 err
= perf_header__adds_write(header
, evlist
, fd
);
2080 f_header
= (struct perf_file_header
){
2081 .magic
= PERF_MAGIC
,
2082 .size
= sizeof(f_header
),
2083 .attr_size
= sizeof(f_attr
),
2085 .offset
= attr_offset
,
2086 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2089 .offset
= header
->data_offset
,
2090 .size
= header
->data_size
,
2092 /* event_types is ignored, store zeros */
2095 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2097 lseek(fd
, 0, SEEK_SET
);
2098 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2100 pr_debug("failed to write perf header\n");
2103 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2108 static int perf_header__getbuffer64(struct perf_header
*header
,
2109 int fd
, void *buf
, size_t size
)
2111 if (readn(fd
, buf
, size
) <= 0)
2114 if (header
->needs_swap
)
2115 mem_bswap_64(buf
, size
);
2120 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2122 int (*process
)(struct perf_file_section
*section
,
2123 struct perf_header
*ph
,
2124 int feat
, int fd
, void *data
))
2126 struct perf_file_section
*feat_sec
, *sec
;
2132 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2136 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2140 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2142 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2144 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2148 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2149 err
= process(sec
++, header
, feat
, fd
, data
);
2159 static const int attr_file_abi_sizes
[] = {
2160 [0] = PERF_ATTR_SIZE_VER0
,
2161 [1] = PERF_ATTR_SIZE_VER1
,
2162 [2] = PERF_ATTR_SIZE_VER2
,
2163 [3] = PERF_ATTR_SIZE_VER3
,
2164 [4] = PERF_ATTR_SIZE_VER4
,
2169 * In the legacy file format, the magic number is not used to encode endianness.
2170 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2171 * on ABI revisions, we need to try all combinations for all endianness to
2172 * detect the endianness.
2174 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2176 uint64_t ref_size
, attr_size
;
2179 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2180 ref_size
= attr_file_abi_sizes
[i
]
2181 + sizeof(struct perf_file_section
);
2182 if (hdr_sz
!= ref_size
) {
2183 attr_size
= bswap_64(hdr_sz
);
2184 if (attr_size
!= ref_size
)
2187 ph
->needs_swap
= true;
2189 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2194 /* could not determine endianness */
2198 #define PERF_PIPE_HDR_VER0 16
2200 static const size_t attr_pipe_abi_sizes
[] = {
2201 [0] = PERF_PIPE_HDR_VER0
,
2206 * In the legacy pipe format, there is an implicit assumption that endiannesss
2207 * between host recording the samples, and host parsing the samples is the
2208 * same. This is not always the case given that the pipe output may always be
2209 * redirected into a file and analyzed on a different machine with possibly a
2210 * different endianness and perf_event ABI revsions in the perf tool itself.
2212 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2217 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2218 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2219 attr_size
= bswap_64(hdr_sz
);
2220 if (attr_size
!= hdr_sz
)
2223 ph
->needs_swap
= true;
2225 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2231 bool is_perf_magic(u64 magic
)
2233 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2234 || magic
== __perf_magic2
2235 || magic
== __perf_magic2_sw
)
2241 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2242 bool is_pipe
, struct perf_header
*ph
)
2246 /* check for legacy format */
2247 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2249 ph
->version
= PERF_HEADER_VERSION_1
;
2250 pr_debug("legacy perf.data format\n");
2252 return try_all_pipe_abis(hdr_sz
, ph
);
2254 return try_all_file_abis(hdr_sz
, ph
);
2257 * the new magic number serves two purposes:
2258 * - unique number to identify actual perf.data files
2259 * - encode endianness of file
2261 ph
->version
= PERF_HEADER_VERSION_2
;
2263 /* check magic number with one endianness */
2264 if (magic
== __perf_magic2
)
2267 /* check magic number with opposite endianness */
2268 if (magic
!= __perf_magic2_sw
)
2271 ph
->needs_swap
= true;
2276 int perf_file_header__read(struct perf_file_header
*header
,
2277 struct perf_header
*ph
, int fd
)
2281 lseek(fd
, 0, SEEK_SET
);
2283 ret
= readn(fd
, header
, sizeof(*header
));
2287 if (check_magic_endian(header
->magic
,
2288 header
->attr_size
, false, ph
) < 0) {
2289 pr_debug("magic/endian check failed\n");
2293 if (ph
->needs_swap
) {
2294 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2298 if (header
->size
!= sizeof(*header
)) {
2299 /* Support the previous format */
2300 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2301 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2304 } else if (ph
->needs_swap
) {
2306 * feature bitmap is declared as an array of unsigned longs --
2307 * not good since its size can differ between the host that
2308 * generated the data file and the host analyzing the file.
2310 * We need to handle endianness, but we don't know the size of
2311 * the unsigned long where the file was generated. Take a best
2312 * guess at determining it: try 64-bit swap first (ie., file
2313 * created on a 64-bit host), and check if the hostname feature
2314 * bit is set (this feature bit is forced on as of fbe96f2).
2315 * If the bit is not, undo the 64-bit swap and try a 32-bit
2316 * swap. If the hostname bit is still not set (e.g., older data
2317 * file), punt and fallback to the original behavior --
2318 * clearing all feature bits and setting buildid.
2320 mem_bswap_64(&header
->adds_features
,
2321 BITS_TO_U64(HEADER_FEAT_BITS
));
2323 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2325 mem_bswap_64(&header
->adds_features
,
2326 BITS_TO_U64(HEADER_FEAT_BITS
));
2329 mem_bswap_32(&header
->adds_features
,
2330 BITS_TO_U32(HEADER_FEAT_BITS
));
2333 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2334 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2335 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2339 memcpy(&ph
->adds_features
, &header
->adds_features
,
2340 sizeof(ph
->adds_features
));
2342 ph
->data_offset
= header
->data
.offset
;
2343 ph
->data_size
= header
->data
.size
;
2344 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2348 static int perf_file_section__process(struct perf_file_section
*section
,
2349 struct perf_header
*ph
,
2350 int feat
, int fd
, void *data
)
2352 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2353 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2354 "%d, continuing...\n", section
->offset
, feat
);
2358 if (feat
>= HEADER_LAST_FEATURE
) {
2359 pr_debug("unknown feature %d, continuing...\n", feat
);
2363 if (!feat_ops
[feat
].process
)
2366 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2369 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2370 struct perf_header
*ph
, int fd
,
2375 ret
= readn(fd
, header
, sizeof(*header
));
2379 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2380 pr_debug("endian/magic failed\n");
2385 header
->size
= bswap_64(header
->size
);
2387 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2393 static int perf_header__read_pipe(struct perf_session
*session
)
2395 struct perf_header
*header
= &session
->header
;
2396 struct perf_pipe_file_header f_header
;
2398 if (perf_file_header__read_pipe(&f_header
, header
,
2399 perf_data_file__fd(session
->file
),
2400 session
->repipe
) < 0) {
2401 pr_debug("incompatible file format\n");
2408 static int read_attr(int fd
, struct perf_header
*ph
,
2409 struct perf_file_attr
*f_attr
)
2411 struct perf_event_attr
*attr
= &f_attr
->attr
;
2413 size_t our_sz
= sizeof(f_attr
->attr
);
2416 memset(f_attr
, 0, sizeof(*f_attr
));
2418 /* read minimal guaranteed structure */
2419 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2421 pr_debug("cannot read %d bytes of header attr\n",
2422 PERF_ATTR_SIZE_VER0
);
2426 /* on file perf_event_attr size */
2434 sz
= PERF_ATTR_SIZE_VER0
;
2435 } else if (sz
> our_sz
) {
2436 pr_debug("file uses a more recent and unsupported ABI"
2437 " (%zu bytes extra)\n", sz
- our_sz
);
2440 /* what we have not yet read and that we know about */
2441 left
= sz
- PERF_ATTR_SIZE_VER0
;
2444 ptr
+= PERF_ATTR_SIZE_VER0
;
2446 ret
= readn(fd
, ptr
, left
);
2448 /* read perf_file_section, ids are read in caller */
2449 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2451 return ret
<= 0 ? -1 : 0;
2454 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2455 struct pevent
*pevent
)
2457 struct event_format
*event
;
2460 /* already prepared */
2461 if (evsel
->tp_format
)
2464 if (pevent
== NULL
) {
2465 pr_debug("broken or missing trace data\n");
2469 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2474 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2475 evsel
->name
= strdup(bf
);
2476 if (evsel
->name
== NULL
)
2480 evsel
->tp_format
= event
;
2484 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2485 struct pevent
*pevent
)
2487 struct perf_evsel
*pos
;
2489 evlist__for_each(evlist
, pos
) {
2490 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2491 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2498 int perf_session__read_header(struct perf_session
*session
)
2500 struct perf_data_file
*file
= session
->file
;
2501 struct perf_header
*header
= &session
->header
;
2502 struct perf_file_header f_header
;
2503 struct perf_file_attr f_attr
;
2505 int nr_attrs
, nr_ids
, i
, j
;
2506 int fd
= perf_data_file__fd(file
);
2508 session
->evlist
= perf_evlist__new();
2509 if (session
->evlist
== NULL
)
2512 if (perf_data_file__is_pipe(file
))
2513 return perf_header__read_pipe(session
);
2515 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2519 * Sanity check that perf.data was written cleanly; data size is
2520 * initialized to 0 and updated only if the on_exit function is run.
2521 * If data size is still 0 then the file contains only partial
2522 * information. Just warn user and process it as much as it can.
2524 if (f_header
.data
.size
== 0) {
2525 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2526 "Was the 'perf record' command properly terminated?\n",
2530 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2531 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2533 for (i
= 0; i
< nr_attrs
; i
++) {
2534 struct perf_evsel
*evsel
;
2537 if (read_attr(fd
, header
, &f_attr
) < 0)
2540 if (header
->needs_swap
) {
2541 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
2542 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
2543 perf_event__attr_swap(&f_attr
.attr
);
2546 tmp
= lseek(fd
, 0, SEEK_CUR
);
2547 evsel
= perf_evsel__new(&f_attr
.attr
);
2550 goto out_delete_evlist
;
2552 evsel
->needs_swap
= header
->needs_swap
;
2554 * Do it before so that if perf_evsel__alloc_id fails, this
2555 * entry gets purged too at perf_evlist__delete().
2557 perf_evlist__add(session
->evlist
, evsel
);
2559 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2561 * We don't have the cpu and thread maps on the header, so
2562 * for allocating the perf_sample_id table we fake 1 cpu and
2563 * hattr->ids threads.
2565 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2566 goto out_delete_evlist
;
2568 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2570 for (j
= 0; j
< nr_ids
; j
++) {
2571 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2574 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2577 lseek(fd
, tmp
, SEEK_SET
);
2580 symbol_conf
.nr_events
= nr_attrs
;
2582 perf_header__process_sections(header
, fd
, &session
->tevent
,
2583 perf_file_section__process
);
2585 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2586 session
->tevent
.pevent
))
2587 goto out_delete_evlist
;
2594 perf_evlist__delete(session
->evlist
);
2595 session
->evlist
= NULL
;
2599 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2600 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2601 perf_event__handler_t process
)
2603 union perf_event
*ev
;
2607 size
= sizeof(struct perf_event_attr
);
2608 size
= PERF_ALIGN(size
, sizeof(u64
));
2609 size
+= sizeof(struct perf_event_header
);
2610 size
+= ids
* sizeof(u64
);
2617 ev
->attr
.attr
= *attr
;
2618 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2620 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2621 ev
->attr
.header
.size
= (u16
)size
;
2623 if (ev
->attr
.header
.size
== size
)
2624 err
= process(tool
, ev
, NULL
, NULL
);
2633 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2634 struct perf_session
*session
,
2635 perf_event__handler_t process
)
2637 struct perf_evsel
*evsel
;
2640 evlist__for_each(session
->evlist
, evsel
) {
2641 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2642 evsel
->id
, process
);
2644 pr_debug("failed to create perf header attribute\n");
2652 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
2653 union perf_event
*event
,
2654 struct perf_evlist
**pevlist
)
2657 struct perf_evsel
*evsel
;
2658 struct perf_evlist
*evlist
= *pevlist
;
2660 if (evlist
== NULL
) {
2661 *pevlist
= evlist
= perf_evlist__new();
2666 evsel
= perf_evsel__new(&event
->attr
.attr
);
2670 perf_evlist__add(evlist
, evsel
);
2672 ids
= event
->header
.size
;
2673 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2674 n_ids
= ids
/ sizeof(u64
);
2676 * We don't have the cpu and thread maps on the header, so
2677 * for allocating the perf_sample_id table we fake 1 cpu and
2678 * hattr->ids threads.
2680 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2683 for (i
= 0; i
< n_ids
; i
++) {
2684 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2687 symbol_conf
.nr_events
= evlist
->nr_entries
;
2692 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2693 struct perf_evlist
*evlist
,
2694 perf_event__handler_t process
)
2696 union perf_event ev
;
2697 struct tracing_data
*tdata
;
2698 ssize_t size
= 0, aligned_size
= 0, padding
;
2699 int err __maybe_unused
= 0;
2702 * We are going to store the size of the data followed
2703 * by the data contents. Since the fd descriptor is a pipe,
2704 * we cannot seek back to store the size of the data once
2705 * we know it. Instead we:
2707 * - write the tracing data to the temp file
2708 * - get/write the data size to pipe
2709 * - write the tracing data from the temp file
2712 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
2716 memset(&ev
, 0, sizeof(ev
));
2718 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
2720 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
2721 padding
= aligned_size
- size
;
2722 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
2723 ev
.tracing_data
.size
= aligned_size
;
2725 process(tool
, &ev
, NULL
, NULL
);
2728 * The put function will copy all the tracing data
2729 * stored in temp file to the pipe.
2731 tracing_data_put(tdata
);
2733 write_padded(fd
, NULL
, 0, padding
);
2735 return aligned_size
;
2738 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
2739 union perf_event
*event
,
2740 struct perf_session
*session
)
2742 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
2743 int fd
= perf_data_file__fd(session
->file
);
2744 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
2747 /* setup for reading amidst mmap */
2748 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
2751 size_read
= trace_report(fd
, &session
->tevent
,
2753 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
2755 if (readn(fd
, buf
, padding
) < 0) {
2756 pr_err("%s: reading input file", __func__
);
2759 if (session
->repipe
) {
2760 int retw
= write(STDOUT_FILENO
, buf
, padding
);
2761 if (retw
<= 0 || retw
!= padding
) {
2762 pr_err("%s: repiping tracing data padding", __func__
);
2767 if (size_read
+ padding
!= size
) {
2768 pr_err("%s: tracing data size mismatch", __func__
);
2772 perf_evlist__prepare_tracepoint_events(session
->evlist
,
2773 session
->tevent
.pevent
);
2775 return size_read
+ padding
;
2778 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
2779 struct dso
*pos
, u16 misc
,
2780 perf_event__handler_t process
,
2781 struct machine
*machine
)
2783 union perf_event ev
;
2790 memset(&ev
, 0, sizeof(ev
));
2792 len
= pos
->long_name_len
+ 1;
2793 len
= PERF_ALIGN(len
, NAME_ALIGN
);
2794 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
2795 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
2796 ev
.build_id
.header
.misc
= misc
;
2797 ev
.build_id
.pid
= machine
->pid
;
2798 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
2799 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
2801 err
= process(tool
, &ev
, NULL
, machine
);
2806 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
2807 union perf_event
*event
,
2808 struct perf_session
*session
)
2810 __event_process_build_id(&event
->build_id
,
2811 event
->build_id
.filename
,