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 void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
875 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
878 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
881 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
884 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
886 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
889 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
892 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
895 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
898 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
899 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
902 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
905 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
908 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
914 nr
= ph
->env
.nr_cmdline
;
915 str
= ph
->env
.cmdline
;
917 fprintf(fp
, "# cmdline : ");
919 for (i
= 0; i
< nr
; i
++) {
920 fprintf(fp
, "%s ", str
);
921 str
+= strlen(str
) + 1;
926 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
932 nr
= ph
->env
.nr_sibling_cores
;
933 str
= ph
->env
.sibling_cores
;
935 for (i
= 0; i
< nr
; i
++) {
936 fprintf(fp
, "# sibling cores : %s\n", str
);
937 str
+= strlen(str
) + 1;
940 nr
= ph
->env
.nr_sibling_threads
;
941 str
= ph
->env
.sibling_threads
;
943 for (i
= 0; i
< nr
; i
++) {
944 fprintf(fp
, "# sibling threads : %s\n", str
);
945 str
+= strlen(str
) + 1;
949 static void free_event_desc(struct perf_evsel
*events
)
951 struct perf_evsel
*evsel
;
956 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
964 static struct perf_evsel
*
965 read_event_desc(struct perf_header
*ph
, int fd
)
967 struct perf_evsel
*evsel
, *events
= NULL
;
970 u32 nre
, sz
, nr
, i
, j
;
974 /* number of events */
975 ret
= readn(fd
, &nre
, sizeof(nre
));
976 if (ret
!= (ssize_t
)sizeof(nre
))
982 ret
= readn(fd
, &sz
, sizeof(sz
));
983 if (ret
!= (ssize_t
)sizeof(sz
))
989 /* buffer to hold on file attr struct */
994 /* the last event terminates with evsel->attr.size == 0: */
995 events
= calloc(nre
+ 1, sizeof(*events
));
999 msz
= sizeof(evsel
->attr
);
1003 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1007 * must read entire on-file attr struct to
1008 * sync up with layout.
1010 ret
= readn(fd
, buf
, sz
);
1011 if (ret
!= (ssize_t
)sz
)
1015 perf_event__attr_swap(buf
);
1017 memcpy(&evsel
->attr
, buf
, msz
);
1019 ret
= readn(fd
, &nr
, sizeof(nr
));
1020 if (ret
!= (ssize_t
)sizeof(nr
))
1023 if (ph
->needs_swap
) {
1025 evsel
->needs_swap
= true;
1028 evsel
->name
= do_read_string(fd
, ph
);
1033 id
= calloc(nr
, sizeof(*id
));
1039 for (j
= 0 ; j
< nr
; j
++) {
1040 ret
= readn(fd
, id
, sizeof(*id
));
1041 if (ret
!= (ssize_t
)sizeof(*id
))
1044 *id
= bswap_64(*id
);
1053 free_event_desc(events
);
1058 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1060 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1065 fprintf(fp
, "# event desc: not available or unable to read\n");
1069 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1070 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1072 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1073 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1075 (u64
)evsel
->attr
.config
,
1076 (u64
)evsel
->attr
.config1
,
1077 (u64
)evsel
->attr
.config2
);
1079 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1080 evsel
->attr
.exclude_user
,
1081 evsel
->attr
.exclude_kernel
);
1083 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1084 evsel
->attr
.exclude_host
,
1085 evsel
->attr
.exclude_guest
);
1087 fprintf(fp
, ", precise_ip = %d", evsel
->attr
.precise_ip
);
1089 fprintf(fp
, ", attr_mmap2 = %d", evsel
->attr
.mmap2
);
1090 fprintf(fp
, ", attr_mmap = %d", evsel
->attr
.mmap
);
1091 fprintf(fp
, ", attr_mmap_data = %d", evsel
->attr
.mmap_data
);
1093 fprintf(fp
, ", id = {");
1094 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1097 fprintf(fp
, " %"PRIu64
, *id
);
1105 free_event_desc(events
);
1108 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1111 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1114 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1119 uint64_t mem_total
, mem_free
;
1122 nr
= ph
->env
.nr_numa_nodes
;
1123 str
= ph
->env
.numa_nodes
;
1125 for (i
= 0; i
< nr
; i
++) {
1127 c
= strtoul(str
, &tmp
, 0);
1132 mem_total
= strtoull(str
, &tmp
, 0);
1137 mem_free
= strtoull(str
, &tmp
, 0);
1141 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1142 " free = %"PRIu64
" kB\n",
1143 c
, mem_total
, mem_free
);
1146 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1148 str
+= strlen(str
) + 1;
1152 fprintf(fp
, "# numa topology : not available\n");
1155 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1157 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1160 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1161 int fd __maybe_unused
, FILE *fp
)
1163 fprintf(fp
, "# contains samples with branch stack\n");
1166 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1169 const char *delimiter
= "# pmu mappings: ";
1174 pmu_num
= ph
->env
.nr_pmu_mappings
;
1176 fprintf(fp
, "# pmu mappings: not available\n");
1180 str
= ph
->env
.pmu_mappings
;
1183 type
= strtoul(str
, &tmp
, 0);
1188 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1191 str
+= strlen(str
) + 1;
1200 fprintf(fp
, "# pmu mappings: unable to read\n");
1203 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1206 struct perf_session
*session
;
1207 struct perf_evsel
*evsel
;
1210 session
= container_of(ph
, struct perf_session
, header
);
1212 evlist__for_each(session
->evlist
, evsel
) {
1213 if (perf_evsel__is_group_leader(evsel
) &&
1214 evsel
->nr_members
> 1) {
1215 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1216 perf_evsel__name(evsel
));
1218 nr
= evsel
->nr_members
- 1;
1220 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1228 static int __event_process_build_id(struct build_id_event
*bev
,
1230 struct perf_session
*session
)
1234 struct machine
*machine
;
1237 enum dso_kernel_type dso_type
;
1239 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1243 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1246 case PERF_RECORD_MISC_KERNEL
:
1247 dso_type
= DSO_TYPE_KERNEL
;
1248 dsos
= &machine
->kernel_dsos
;
1250 case PERF_RECORD_MISC_GUEST_KERNEL
:
1251 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1252 dsos
= &machine
->kernel_dsos
;
1254 case PERF_RECORD_MISC_USER
:
1255 case PERF_RECORD_MISC_GUEST_USER
:
1256 dso_type
= DSO_TYPE_USER
;
1257 dsos
= &machine
->user_dsos
;
1263 dso
= __dsos__findnew(dsos
, filename
);
1265 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1267 dso__set_build_id(dso
, &bev
->build_id
);
1269 if (!is_kernel_module(filename
, NULL
))
1270 dso
->kernel
= dso_type
;
1272 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1274 pr_debug("build id event received for %s: %s\n",
1275 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 __maybe_unused
,
1541 struct perf_header
*ph
, int fd
,
1542 void *data __maybe_unused
)
1549 ret
= readn(fd
, &nr
, sizeof(nr
));
1550 if (ret
!= sizeof(nr
))
1556 ph
->env
.nr_cmdline
= nr
;
1557 strbuf_init(&sb
, 128);
1559 for (i
= 0; i
< nr
; i
++) {
1560 str
= do_read_string(fd
, ph
);
1564 /* include a NULL character at the end */
1565 strbuf_add(&sb
, str
, strlen(str
) + 1);
1568 ph
->env
.cmdline
= strbuf_detach(&sb
, NULL
);
1572 strbuf_release(&sb
);
1576 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1577 struct perf_header
*ph
, int fd
,
1578 void *data __maybe_unused
)
1585 ret
= readn(fd
, &nr
, sizeof(nr
));
1586 if (ret
!= sizeof(nr
))
1592 ph
->env
.nr_sibling_cores
= nr
;
1593 strbuf_init(&sb
, 128);
1595 for (i
= 0; i
< nr
; i
++) {
1596 str
= do_read_string(fd
, ph
);
1600 /* include a NULL character at the end */
1601 strbuf_add(&sb
, str
, strlen(str
) + 1);
1604 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1606 ret
= readn(fd
, &nr
, sizeof(nr
));
1607 if (ret
!= sizeof(nr
))
1613 ph
->env
.nr_sibling_threads
= nr
;
1615 for (i
= 0; i
< nr
; i
++) {
1616 str
= do_read_string(fd
, ph
);
1620 /* include a NULL character at the end */
1621 strbuf_add(&sb
, str
, strlen(str
) + 1);
1624 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1628 strbuf_release(&sb
);
1632 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1633 struct perf_header
*ph
, int fd
,
1634 void *data __maybe_unused
)
1639 uint64_t mem_total
, mem_free
;
1643 ret
= readn(fd
, &nr
, sizeof(nr
));
1644 if (ret
!= sizeof(nr
))
1650 ph
->env
.nr_numa_nodes
= nr
;
1651 strbuf_init(&sb
, 256);
1653 for (i
= 0; i
< nr
; i
++) {
1655 ret
= readn(fd
, &node
, sizeof(node
));
1656 if (ret
!= sizeof(node
))
1659 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1660 if (ret
!= sizeof(u64
))
1663 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1664 if (ret
!= sizeof(u64
))
1667 if (ph
->needs_swap
) {
1668 node
= bswap_32(node
);
1669 mem_total
= bswap_64(mem_total
);
1670 mem_free
= bswap_64(mem_free
);
1673 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1674 node
, mem_total
, mem_free
);
1676 str
= do_read_string(fd
, ph
);
1680 /* include a NULL character at the end */
1681 strbuf_add(&sb
, str
, strlen(str
) + 1);
1684 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
1688 strbuf_release(&sb
);
1692 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1693 struct perf_header
*ph
, int fd
,
1694 void *data __maybe_unused
)
1702 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1703 if (ret
!= sizeof(pmu_num
))
1707 pmu_num
= bswap_32(pmu_num
);
1710 pr_debug("pmu mappings not available\n");
1714 ph
->env
.nr_pmu_mappings
= pmu_num
;
1715 strbuf_init(&sb
, 128);
1718 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
1721 type
= bswap_32(type
);
1723 name
= do_read_string(fd
, ph
);
1727 strbuf_addf(&sb
, "%u:%s", type
, name
);
1728 /* include a NULL character at the end */
1729 strbuf_add(&sb
, "", 1);
1734 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
1738 strbuf_release(&sb
);
1742 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
1743 struct perf_header
*ph
, int fd
,
1744 void *data __maybe_unused
)
1747 u32 i
, nr
, nr_groups
;
1748 struct perf_session
*session
;
1749 struct perf_evsel
*evsel
, *leader
= NULL
;
1756 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
1760 nr_groups
= bswap_32(nr_groups
);
1762 ph
->env
.nr_groups
= nr_groups
;
1764 pr_debug("group desc not available\n");
1768 desc
= calloc(nr_groups
, sizeof(*desc
));
1772 for (i
= 0; i
< nr_groups
; i
++) {
1773 desc
[i
].name
= do_read_string(fd
, ph
);
1777 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
1780 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
1783 if (ph
->needs_swap
) {
1784 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
1785 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
1790 * Rebuild group relationship based on the group_desc
1792 session
= container_of(ph
, struct perf_session
, header
);
1793 session
->evlist
->nr_groups
= nr_groups
;
1796 evlist__for_each(session
->evlist
, evsel
) {
1797 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
1798 evsel
->leader
= evsel
;
1799 /* {anon_group} is a dummy name */
1800 if (strcmp(desc
[i
].name
, "{anon_group}")) {
1801 evsel
->group_name
= desc
[i
].name
;
1802 desc
[i
].name
= NULL
;
1804 evsel
->nr_members
= desc
[i
].nr_members
;
1806 if (i
>= nr_groups
|| nr
> 0) {
1807 pr_debug("invalid group desc\n");
1812 nr
= evsel
->nr_members
- 1;
1815 /* This is a group member */
1816 evsel
->leader
= leader
;
1822 if (i
!= nr_groups
|| nr
!= 0) {
1823 pr_debug("invalid group desc\n");
1829 for (i
= 0; i
< nr_groups
; i
++)
1830 zfree(&desc
[i
].name
);
1836 struct feature_ops
{
1837 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
1838 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
1839 int (*process
)(struct perf_file_section
*section
,
1840 struct perf_header
*h
, int fd
, void *data
);
1845 #define FEAT_OPA(n, func) \
1846 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1847 #define FEAT_OPP(n, func) \
1848 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1849 .process = process_##func }
1850 #define FEAT_OPF(n, func) \
1851 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1852 .process = process_##func, .full_only = true }
1854 /* feature_ops not implemented: */
1855 #define print_tracing_data NULL
1856 #define print_build_id NULL
1858 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
1859 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
1860 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
1861 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
1862 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
1863 FEAT_OPP(HEADER_VERSION
, version
),
1864 FEAT_OPP(HEADER_ARCH
, arch
),
1865 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
1866 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
1867 FEAT_OPP(HEADER_CPUID
, cpuid
),
1868 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
1869 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
1870 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
1871 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
1872 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
1873 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
1874 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
1875 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
1878 struct header_print_data
{
1880 bool full
; /* extended list of headers */
1883 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
1884 struct perf_header
*ph
,
1885 int feat
, int fd
, void *data
)
1887 struct header_print_data
*hd
= data
;
1889 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
1890 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
1891 "%d, continuing...\n", section
->offset
, feat
);
1894 if (feat
>= HEADER_LAST_FEATURE
) {
1895 pr_warning("unknown feature %d\n", feat
);
1898 if (!feat_ops
[feat
].print
)
1901 if (!feat_ops
[feat
].full_only
|| hd
->full
)
1902 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
1904 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
1905 feat_ops
[feat
].name
);
1910 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
1912 struct header_print_data hd
;
1913 struct perf_header
*header
= &session
->header
;
1914 int fd
= perf_data_file__fd(session
->file
);
1918 perf_header__process_sections(header
, fd
, &hd
,
1919 perf_file_section__fprintf_info
);
1923 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
1924 struct perf_file_section
**p
,
1925 struct perf_evlist
*evlist
)
1930 if (perf_header__has_feat(h
, type
)) {
1931 if (!feat_ops
[type
].write
)
1934 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
1936 err
= feat_ops
[type
].write(fd
, h
, evlist
);
1938 pr_debug("failed to write feature %d\n", type
);
1940 /* undo anything written */
1941 lseek(fd
, (*p
)->offset
, SEEK_SET
);
1945 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
1951 static int perf_header__adds_write(struct perf_header
*header
,
1952 struct perf_evlist
*evlist
, int fd
)
1955 struct perf_file_section
*feat_sec
, *p
;
1961 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
1965 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
1966 if (feat_sec
== NULL
)
1969 sec_size
= sizeof(*feat_sec
) * nr_sections
;
1971 sec_start
= header
->feat_offset
;
1972 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
1974 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
1975 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
1976 perf_header__clear_feat(header
, feat
);
1979 lseek(fd
, sec_start
, SEEK_SET
);
1981 * may write more than needed due to dropped feature, but
1982 * this is okay, reader will skip the mising entries
1984 err
= do_write(fd
, feat_sec
, sec_size
);
1986 pr_debug("failed to write feature section\n");
1991 int perf_header__write_pipe(int fd
)
1993 struct perf_pipe_file_header f_header
;
1996 f_header
= (struct perf_pipe_file_header
){
1997 .magic
= PERF_MAGIC
,
1998 .size
= sizeof(f_header
),
2001 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2003 pr_debug("failed to write perf pipe header\n");
2010 int perf_session__write_header(struct perf_session
*session
,
2011 struct perf_evlist
*evlist
,
2012 int fd
, bool at_exit
)
2014 struct perf_file_header f_header
;
2015 struct perf_file_attr f_attr
;
2016 struct perf_header
*header
= &session
->header
;
2017 struct perf_evsel
*evsel
;
2021 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2023 evlist__for_each(session
->evlist
, evsel
) {
2024 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2025 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2027 pr_debug("failed to write perf header\n");
2032 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2034 evlist__for_each(evlist
, evsel
) {
2035 f_attr
= (struct perf_file_attr
){
2036 .attr
= evsel
->attr
,
2038 .offset
= evsel
->id_offset
,
2039 .size
= evsel
->ids
* sizeof(u64
),
2042 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2044 pr_debug("failed to write perf header attribute\n");
2049 if (!header
->data_offset
)
2050 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2051 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2054 err
= perf_header__adds_write(header
, evlist
, fd
);
2059 f_header
= (struct perf_file_header
){
2060 .magic
= PERF_MAGIC
,
2061 .size
= sizeof(f_header
),
2062 .attr_size
= sizeof(f_attr
),
2064 .offset
= attr_offset
,
2065 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2068 .offset
= header
->data_offset
,
2069 .size
= header
->data_size
,
2071 /* event_types is ignored, store zeros */
2074 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2076 lseek(fd
, 0, SEEK_SET
);
2077 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2079 pr_debug("failed to write perf header\n");
2082 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2087 static int perf_header__getbuffer64(struct perf_header
*header
,
2088 int fd
, void *buf
, size_t size
)
2090 if (readn(fd
, buf
, size
) <= 0)
2093 if (header
->needs_swap
)
2094 mem_bswap_64(buf
, size
);
2099 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2101 int (*process
)(struct perf_file_section
*section
,
2102 struct perf_header
*ph
,
2103 int feat
, int fd
, void *data
))
2105 struct perf_file_section
*feat_sec
, *sec
;
2111 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2115 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2119 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2121 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2123 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2127 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2128 err
= process(sec
++, header
, feat
, fd
, data
);
2138 static const int attr_file_abi_sizes
[] = {
2139 [0] = PERF_ATTR_SIZE_VER0
,
2140 [1] = PERF_ATTR_SIZE_VER1
,
2141 [2] = PERF_ATTR_SIZE_VER2
,
2142 [3] = PERF_ATTR_SIZE_VER3
,
2143 [4] = PERF_ATTR_SIZE_VER4
,
2148 * In the legacy file format, the magic number is not used to encode endianness.
2149 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2150 * on ABI revisions, we need to try all combinations for all endianness to
2151 * detect the endianness.
2153 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2155 uint64_t ref_size
, attr_size
;
2158 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2159 ref_size
= attr_file_abi_sizes
[i
]
2160 + sizeof(struct perf_file_section
);
2161 if (hdr_sz
!= ref_size
) {
2162 attr_size
= bswap_64(hdr_sz
);
2163 if (attr_size
!= ref_size
)
2166 ph
->needs_swap
= true;
2168 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2173 /* could not determine endianness */
2177 #define PERF_PIPE_HDR_VER0 16
2179 static const size_t attr_pipe_abi_sizes
[] = {
2180 [0] = PERF_PIPE_HDR_VER0
,
2185 * In the legacy pipe format, there is an implicit assumption that endiannesss
2186 * between host recording the samples, and host parsing the samples is the
2187 * same. This is not always the case given that the pipe output may always be
2188 * redirected into a file and analyzed on a different machine with possibly a
2189 * different endianness and perf_event ABI revsions in the perf tool itself.
2191 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2196 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2197 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2198 attr_size
= bswap_64(hdr_sz
);
2199 if (attr_size
!= hdr_sz
)
2202 ph
->needs_swap
= true;
2204 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2210 bool is_perf_magic(u64 magic
)
2212 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2213 || magic
== __perf_magic2
2214 || magic
== __perf_magic2_sw
)
2220 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2221 bool is_pipe
, struct perf_header
*ph
)
2225 /* check for legacy format */
2226 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2228 ph
->version
= PERF_HEADER_VERSION_1
;
2229 pr_debug("legacy perf.data format\n");
2231 return try_all_pipe_abis(hdr_sz
, ph
);
2233 return try_all_file_abis(hdr_sz
, ph
);
2236 * the new magic number serves two purposes:
2237 * - unique number to identify actual perf.data files
2238 * - encode endianness of file
2241 /* check magic number with one endianness */
2242 if (magic
== __perf_magic2
)
2245 /* check magic number with opposite endianness */
2246 if (magic
!= __perf_magic2_sw
)
2249 ph
->needs_swap
= true;
2250 ph
->version
= PERF_HEADER_VERSION_2
;
2255 int perf_file_header__read(struct perf_file_header
*header
,
2256 struct perf_header
*ph
, int fd
)
2260 lseek(fd
, 0, SEEK_SET
);
2262 ret
= readn(fd
, header
, sizeof(*header
));
2266 if (check_magic_endian(header
->magic
,
2267 header
->attr_size
, false, ph
) < 0) {
2268 pr_debug("magic/endian check failed\n");
2272 if (ph
->needs_swap
) {
2273 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2277 if (header
->size
!= sizeof(*header
)) {
2278 /* Support the previous format */
2279 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2280 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2283 } else if (ph
->needs_swap
) {
2285 * feature bitmap is declared as an array of unsigned longs --
2286 * not good since its size can differ between the host that
2287 * generated the data file and the host analyzing the file.
2289 * We need to handle endianness, but we don't know the size of
2290 * the unsigned long where the file was generated. Take a best
2291 * guess at determining it: try 64-bit swap first (ie., file
2292 * created on a 64-bit host), and check if the hostname feature
2293 * bit is set (this feature bit is forced on as of fbe96f2).
2294 * If the bit is not, undo the 64-bit swap and try a 32-bit
2295 * swap. If the hostname bit is still not set (e.g., older data
2296 * file), punt and fallback to the original behavior --
2297 * clearing all feature bits and setting buildid.
2299 mem_bswap_64(&header
->adds_features
,
2300 BITS_TO_U64(HEADER_FEAT_BITS
));
2302 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2304 mem_bswap_64(&header
->adds_features
,
2305 BITS_TO_U64(HEADER_FEAT_BITS
));
2308 mem_bswap_32(&header
->adds_features
,
2309 BITS_TO_U32(HEADER_FEAT_BITS
));
2312 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2313 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2314 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2318 memcpy(&ph
->adds_features
, &header
->adds_features
,
2319 sizeof(ph
->adds_features
));
2321 ph
->data_offset
= header
->data
.offset
;
2322 ph
->data_size
= header
->data
.size
;
2323 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2327 static int perf_file_section__process(struct perf_file_section
*section
,
2328 struct perf_header
*ph
,
2329 int feat
, int fd
, void *data
)
2331 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2332 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2333 "%d, continuing...\n", section
->offset
, feat
);
2337 if (feat
>= HEADER_LAST_FEATURE
) {
2338 pr_debug("unknown feature %d, continuing...\n", feat
);
2342 if (!feat_ops
[feat
].process
)
2345 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2348 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2349 struct perf_header
*ph
, int fd
,
2354 ret
= readn(fd
, header
, sizeof(*header
));
2358 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2359 pr_debug("endian/magic failed\n");
2364 header
->size
= bswap_64(header
->size
);
2366 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2372 static int perf_header__read_pipe(struct perf_session
*session
)
2374 struct perf_header
*header
= &session
->header
;
2375 struct perf_pipe_file_header f_header
;
2377 if (perf_file_header__read_pipe(&f_header
, header
,
2378 perf_data_file__fd(session
->file
),
2379 session
->repipe
) < 0) {
2380 pr_debug("incompatible file format\n");
2387 static int read_attr(int fd
, struct perf_header
*ph
,
2388 struct perf_file_attr
*f_attr
)
2390 struct perf_event_attr
*attr
= &f_attr
->attr
;
2392 size_t our_sz
= sizeof(f_attr
->attr
);
2395 memset(f_attr
, 0, sizeof(*f_attr
));
2397 /* read minimal guaranteed structure */
2398 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2400 pr_debug("cannot read %d bytes of header attr\n",
2401 PERF_ATTR_SIZE_VER0
);
2405 /* on file perf_event_attr size */
2413 sz
= PERF_ATTR_SIZE_VER0
;
2414 } else if (sz
> our_sz
) {
2415 pr_debug("file uses a more recent and unsupported ABI"
2416 " (%zu bytes extra)\n", sz
- our_sz
);
2419 /* what we have not yet read and that we know about */
2420 left
= sz
- PERF_ATTR_SIZE_VER0
;
2423 ptr
+= PERF_ATTR_SIZE_VER0
;
2425 ret
= readn(fd
, ptr
, left
);
2427 /* read perf_file_section, ids are read in caller */
2428 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2430 return ret
<= 0 ? -1 : 0;
2433 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2434 struct pevent
*pevent
)
2436 struct event_format
*event
;
2439 /* already prepared */
2440 if (evsel
->tp_format
)
2443 if (pevent
== NULL
) {
2444 pr_debug("broken or missing trace data\n");
2448 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2453 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2454 evsel
->name
= strdup(bf
);
2455 if (evsel
->name
== NULL
)
2459 evsel
->tp_format
= event
;
2463 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2464 struct pevent
*pevent
)
2466 struct perf_evsel
*pos
;
2468 evlist__for_each(evlist
, pos
) {
2469 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2470 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2477 int perf_session__read_header(struct perf_session
*session
)
2479 struct perf_data_file
*file
= session
->file
;
2480 struct perf_header
*header
= &session
->header
;
2481 struct perf_file_header f_header
;
2482 struct perf_file_attr f_attr
;
2484 int nr_attrs
, nr_ids
, i
, j
;
2485 int fd
= perf_data_file__fd(file
);
2487 session
->evlist
= perf_evlist__new();
2488 if (session
->evlist
== NULL
)
2491 if (perf_data_file__is_pipe(file
))
2492 return perf_header__read_pipe(session
);
2494 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2498 * Sanity check that perf.data was written cleanly; data size is
2499 * initialized to 0 and updated only if the on_exit function is run.
2500 * If data size is still 0 then the file contains only partial
2501 * information. Just warn user and process it as much as it can.
2503 if (f_header
.data
.size
== 0) {
2504 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2505 "Was the 'perf record' command properly terminated?\n",
2509 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2510 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2512 for (i
= 0; i
< nr_attrs
; i
++) {
2513 struct perf_evsel
*evsel
;
2516 if (read_attr(fd
, header
, &f_attr
) < 0)
2519 if (header
->needs_swap
)
2520 perf_event__attr_swap(&f_attr
.attr
);
2522 tmp
= lseek(fd
, 0, SEEK_CUR
);
2523 evsel
= perf_evsel__new(&f_attr
.attr
);
2526 goto out_delete_evlist
;
2528 evsel
->needs_swap
= header
->needs_swap
;
2530 * Do it before so that if perf_evsel__alloc_id fails, this
2531 * entry gets purged too at perf_evlist__delete().
2533 perf_evlist__add(session
->evlist
, evsel
);
2535 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2537 * We don't have the cpu and thread maps on the header, so
2538 * for allocating the perf_sample_id table we fake 1 cpu and
2539 * hattr->ids threads.
2541 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2542 goto out_delete_evlist
;
2544 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2546 for (j
= 0; j
< nr_ids
; j
++) {
2547 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2550 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2553 lseek(fd
, tmp
, SEEK_SET
);
2556 symbol_conf
.nr_events
= nr_attrs
;
2558 perf_header__process_sections(header
, fd
, &session
->tevent
,
2559 perf_file_section__process
);
2561 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2562 session
->tevent
.pevent
))
2563 goto out_delete_evlist
;
2570 perf_evlist__delete(session
->evlist
);
2571 session
->evlist
= NULL
;
2575 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2576 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2577 perf_event__handler_t process
)
2579 union perf_event
*ev
;
2583 size
= sizeof(struct perf_event_attr
);
2584 size
= PERF_ALIGN(size
, sizeof(u64
));
2585 size
+= sizeof(struct perf_event_header
);
2586 size
+= ids
* sizeof(u64
);
2593 ev
->attr
.attr
= *attr
;
2594 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2596 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2597 ev
->attr
.header
.size
= (u16
)size
;
2599 if (ev
->attr
.header
.size
== size
)
2600 err
= process(tool
, ev
, NULL
, NULL
);
2609 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2610 struct perf_session
*session
,
2611 perf_event__handler_t process
)
2613 struct perf_evsel
*evsel
;
2616 evlist__for_each(session
->evlist
, evsel
) {
2617 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2618 evsel
->id
, process
);
2620 pr_debug("failed to create perf header attribute\n");
2628 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
2629 union perf_event
*event
,
2630 struct perf_evlist
**pevlist
)
2633 struct perf_evsel
*evsel
;
2634 struct perf_evlist
*evlist
= *pevlist
;
2636 if (evlist
== NULL
) {
2637 *pevlist
= evlist
= perf_evlist__new();
2642 evsel
= perf_evsel__new(&event
->attr
.attr
);
2646 perf_evlist__add(evlist
, evsel
);
2648 ids
= event
->header
.size
;
2649 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2650 n_ids
= ids
/ sizeof(u64
);
2652 * We don't have the cpu and thread maps on the header, so
2653 * for allocating the perf_sample_id table we fake 1 cpu and
2654 * hattr->ids threads.
2656 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2659 for (i
= 0; i
< n_ids
; i
++) {
2660 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2663 symbol_conf
.nr_events
= evlist
->nr_entries
;
2668 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2669 struct perf_evlist
*evlist
,
2670 perf_event__handler_t process
)
2672 union perf_event ev
;
2673 struct tracing_data
*tdata
;
2674 ssize_t size
= 0, aligned_size
= 0, padding
;
2675 int err __maybe_unused
= 0;
2678 * We are going to store the size of the data followed
2679 * by the data contents. Since the fd descriptor is a pipe,
2680 * we cannot seek back to store the size of the data once
2681 * we know it. Instead we:
2683 * - write the tracing data to the temp file
2684 * - get/write the data size to pipe
2685 * - write the tracing data from the temp file
2688 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
2692 memset(&ev
, 0, sizeof(ev
));
2694 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
2696 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
2697 padding
= aligned_size
- size
;
2698 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
2699 ev
.tracing_data
.size
= aligned_size
;
2701 process(tool
, &ev
, NULL
, NULL
);
2704 * The put function will copy all the tracing data
2705 * stored in temp file to the pipe.
2707 tracing_data_put(tdata
);
2709 write_padded(fd
, NULL
, 0, padding
);
2711 return aligned_size
;
2714 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
2715 union perf_event
*event
,
2716 struct perf_session
*session
)
2718 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
2719 int fd
= perf_data_file__fd(session
->file
);
2720 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
2723 /* setup for reading amidst mmap */
2724 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
2727 size_read
= trace_report(fd
, &session
->tevent
,
2729 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
2731 if (readn(fd
, buf
, padding
) < 0) {
2732 pr_err("%s: reading input file", __func__
);
2735 if (session
->repipe
) {
2736 int retw
= write(STDOUT_FILENO
, buf
, padding
);
2737 if (retw
<= 0 || retw
!= padding
) {
2738 pr_err("%s: repiping tracing data padding", __func__
);
2743 if (size_read
+ padding
!= size
) {
2744 pr_err("%s: tracing data size mismatch", __func__
);
2748 perf_evlist__prepare_tracepoint_events(session
->evlist
,
2749 session
->tevent
.pevent
);
2751 return size_read
+ padding
;
2754 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
2755 struct dso
*pos
, u16 misc
,
2756 perf_event__handler_t process
,
2757 struct machine
*machine
)
2759 union perf_event ev
;
2766 memset(&ev
, 0, sizeof(ev
));
2768 len
= pos
->long_name_len
+ 1;
2769 len
= PERF_ALIGN(len
, NAME_ALIGN
);
2770 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
2771 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
2772 ev
.build_id
.header
.misc
= misc
;
2773 ev
.build_id
.pid
= machine
->pid
;
2774 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
2775 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
2777 err
= process(tool
, &ev
, NULL
, machine
);
2782 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
2783 union perf_event
*event
,
2784 struct perf_session
*session
)
2786 __event_process_build_id(&event
->build_id
,
2787 event
->build_id
.filename
,