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 bool no_buildid_cache
= false;
29 static u32 header_argc
;
30 static const char **header_argv
;
34 * must be a numerical value to let the endianness
35 * determine the memory layout. That way we are able
36 * to detect endianness when reading the perf.data file
39 * we check for legacy (PERFFILE) format.
41 static const char *__perf_magic1
= "PERFFILE";
42 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
43 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
45 #define PERF_MAGIC __perf_magic2
47 struct perf_file_attr
{
48 struct perf_event_attr attr
;
49 struct perf_file_section ids
;
52 void perf_header__set_feat(struct perf_header
*header
, int feat
)
54 set_bit(feat
, header
->adds_features
);
57 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
59 clear_bit(feat
, header
->adds_features
);
62 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
64 return test_bit(feat
, header
->adds_features
);
67 static int do_write(int fd
, const void *buf
, size_t size
)
70 int ret
= write(fd
, buf
, size
);
84 static int write_padded(int fd
, const void *bf
, size_t count
,
87 static const char zero_buf
[NAME_ALIGN
];
88 int err
= do_write(fd
, bf
, count
);
91 err
= do_write(fd
, zero_buf
, count_aligned
- count
);
96 static int do_write_string(int fd
, const char *str
)
101 olen
= strlen(str
) + 1;
102 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
104 /* write len, incl. \0 */
105 ret
= do_write(fd
, &len
, sizeof(len
));
109 return write_padded(fd
, str
, olen
, len
);
112 static char *do_read_string(int fd
, struct perf_header
*ph
)
118 sz
= readn(fd
, &len
, sizeof(len
));
119 if (sz
< (ssize_t
)sizeof(len
))
129 ret
= readn(fd
, buf
, len
);
130 if (ret
== (ssize_t
)len
) {
132 * strings are padded by zeroes
133 * thus the actual strlen of buf
134 * may be less than len
144 perf_header__set_cmdline(int argc
, const char **argv
)
149 * If header_argv has already been set, do not override it.
150 * This allows a command to set the cmdline, parse args and
151 * then call another builtin function that implements a
152 * command -- e.g, cmd_kvm calling cmd_record.
157 header_argc
= (u32
)argc
;
159 /* do not include NULL termination */
160 header_argv
= calloc(argc
, sizeof(char *));
165 * must copy argv contents because it gets moved
166 * around during option parsing
168 for (i
= 0; i
< argc
; i
++)
169 header_argv
[i
] = argv
[i
];
174 #define dsos__for_each_with_build_id(pos, head) \
175 list_for_each_entry(pos, head, node) \
176 if (!pos->has_build_id) \
180 static int write_buildid(const char *name
, size_t name_len
, u8
*build_id
,
181 pid_t pid
, u16 misc
, int fd
)
184 struct build_id_event b
;
188 len
= PERF_ALIGN(len
, NAME_ALIGN
);
190 memset(&b
, 0, sizeof(b
));
191 memcpy(&b
.build_id
, build_id
, BUILD_ID_SIZE
);
193 b
.header
.misc
= misc
;
194 b
.header
.size
= sizeof(b
) + len
;
196 err
= do_write(fd
, &b
, sizeof(b
));
200 return write_padded(fd
, name
, name_len
+ 1, len
);
203 static int __dsos__hit_all(struct list_head
*head
)
207 list_for_each_entry(pos
, head
, node
)
213 static int machine__hit_all_dsos(struct machine
*machine
)
217 err
= __dsos__hit_all(&machine
->kernel_dsos
.head
);
221 return __dsos__hit_all(&machine
->user_dsos
.head
);
224 int dsos__hit_all(struct perf_session
*session
)
229 err
= machine__hit_all_dsos(&session
->machines
.host
);
233 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
234 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
236 err
= machine__hit_all_dsos(pos
);
244 static int __dsos__write_buildid_table(struct list_head
*head
,
245 struct machine
*machine
,
246 pid_t pid
, u16 misc
, int fd
)
251 dsos__for_each_with_build_id(pos
, head
) {
259 if (dso__is_vdso(pos
)) {
260 name
= pos
->short_name
;
261 name_len
= pos
->short_name_len
+ 1;
262 } else if (dso__is_kcore(pos
)) {
263 machine__mmap_name(machine
, nm
, sizeof(nm
));
265 name_len
= strlen(nm
) + 1;
267 name
= pos
->long_name
;
268 name_len
= pos
->long_name_len
+ 1;
271 err
= write_buildid(name
, name_len
, pos
->build_id
,
280 static int machine__write_buildid_table(struct machine
*machine
, int fd
)
283 u16 kmisc
= PERF_RECORD_MISC_KERNEL
,
284 umisc
= PERF_RECORD_MISC_USER
;
286 if (!machine__is_host(machine
)) {
287 kmisc
= PERF_RECORD_MISC_GUEST_KERNEL
;
288 umisc
= PERF_RECORD_MISC_GUEST_USER
;
291 err
= __dsos__write_buildid_table(&machine
->kernel_dsos
.head
, machine
,
292 machine
->pid
, kmisc
, fd
);
294 err
= __dsos__write_buildid_table(&machine
->user_dsos
.head
,
295 machine
, machine
->pid
, umisc
,
300 static int dsos__write_buildid_table(struct perf_header
*header
, int fd
)
302 struct perf_session
*session
= container_of(header
,
303 struct perf_session
, header
);
305 int err
= machine__write_buildid_table(&session
->machines
.host
, fd
);
310 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
311 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
312 err
= machine__write_buildid_table(pos
, fd
);
319 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
320 const char *name
, bool is_kallsyms
, bool is_vdso
)
322 const size_t size
= PATH_MAX
;
323 char *realname
, *filename
= zalloc(size
),
324 *linkname
= zalloc(size
), *targetname
;
326 bool slash
= is_kallsyms
|| is_vdso
;
329 if (symbol_conf
.kptr_restrict
) {
330 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
334 realname
= (char *) name
;
336 realname
= realpath(name
, NULL
);
338 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
341 len
= scnprintf(filename
, size
, "%s%s%s",
342 debugdir
, slash
? "/" : "",
343 is_vdso
? DSO__NAME_VDSO
: realname
);
344 if (mkdir_p(filename
, 0755))
347 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
349 if (access(filename
, F_OK
)) {
351 if (copyfile("/proc/kallsyms", filename
))
353 } else if (link(realname
, filename
) && copyfile(name
, filename
))
357 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
358 debugdir
, sbuild_id
);
360 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
363 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
364 targetname
= filename
+ strlen(debugdir
) - 5;
365 memcpy(targetname
, "../..", 5);
367 if (symlink(targetname
, linkname
) == 0)
377 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
378 const char *name
, const char *debugdir
,
379 bool is_kallsyms
, bool is_vdso
)
381 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
383 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
385 return build_id_cache__add_s(sbuild_id
, debugdir
, name
,
386 is_kallsyms
, is_vdso
);
389 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
391 const size_t size
= PATH_MAX
;
392 char *filename
= zalloc(size
),
393 *linkname
= zalloc(size
);
396 if (filename
== NULL
|| linkname
== NULL
)
399 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
400 debugdir
, sbuild_id
, sbuild_id
+ 2);
402 if (access(linkname
, F_OK
))
405 if (readlink(linkname
, filename
, size
- 1) < 0)
408 if (unlink(linkname
))
412 * Since the link is relative, we must make it absolute:
414 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
415 debugdir
, sbuild_id
, filename
);
417 if (unlink(linkname
))
427 static int dso__cache_build_id(struct dso
*dso
, struct machine
*machine
,
428 const char *debugdir
)
430 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
431 bool is_vdso
= dso__is_vdso(dso
);
432 const char *name
= dso
->long_name
;
435 if (dso__is_kcore(dso
)) {
437 machine__mmap_name(machine
, nm
, sizeof(nm
));
440 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
), name
,
441 debugdir
, is_kallsyms
, is_vdso
);
444 static int __dsos__cache_build_ids(struct list_head
*head
,
445 struct machine
*machine
, const char *debugdir
)
450 dsos__for_each_with_build_id(pos
, head
)
451 if (dso__cache_build_id(pos
, machine
, debugdir
))
457 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
459 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
.head
, machine
,
461 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
.head
, machine
,
466 static int perf_session__cache_build_ids(struct perf_session
*session
)
470 char debugdir
[PATH_MAX
];
472 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
474 if (mkdir(debugdir
, 0755) != 0 && errno
!= EEXIST
)
477 ret
= machine__cache_build_ids(&session
->machines
.host
, debugdir
);
479 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
480 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
481 ret
|= machine__cache_build_ids(pos
, debugdir
);
486 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
490 ret
= __dsos__read_build_ids(&machine
->kernel_dsos
.head
, with_hits
);
491 ret
|= __dsos__read_build_ids(&machine
->user_dsos
.head
, with_hits
);
495 static bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
498 bool ret
= machine__read_build_ids(&session
->machines
.host
, with_hits
);
500 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
501 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
502 ret
|= machine__read_build_ids(pos
, with_hits
);
508 static int write_tracing_data(int fd
, struct perf_header
*h __maybe_unused
,
509 struct perf_evlist
*evlist
)
511 return read_tracing_data(fd
, &evlist
->entries
);
515 static int write_build_id(int fd
, struct perf_header
*h
,
516 struct perf_evlist
*evlist __maybe_unused
)
518 struct perf_session
*session
;
521 session
= container_of(h
, struct perf_session
, header
);
523 if (!perf_session__read_build_ids(session
, true))
526 err
= dsos__write_buildid_table(h
, fd
);
528 pr_debug("failed to write buildid table\n");
531 if (!no_buildid_cache
)
532 perf_session__cache_build_ids(session
);
537 static int write_hostname(int fd
, struct perf_header
*h __maybe_unused
,
538 struct perf_evlist
*evlist __maybe_unused
)
547 return do_write_string(fd
, uts
.nodename
);
550 static int write_osrelease(int fd
, struct perf_header
*h __maybe_unused
,
551 struct perf_evlist
*evlist __maybe_unused
)
560 return do_write_string(fd
, uts
.release
);
563 static int write_arch(int fd
, struct perf_header
*h __maybe_unused
,
564 struct perf_evlist
*evlist __maybe_unused
)
573 return do_write_string(fd
, uts
.machine
);
576 static int write_version(int fd
, struct perf_header
*h __maybe_unused
,
577 struct perf_evlist
*evlist __maybe_unused
)
579 return do_write_string(fd
, perf_version_string
);
582 static int write_cpudesc(int fd
, struct perf_header
*h __maybe_unused
,
583 struct perf_evlist
*evlist __maybe_unused
)
586 #define CPUINFO_PROC NULL
591 const char *search
= CPUINFO_PROC
;
598 file
= fopen("/proc/cpuinfo", "r");
602 while (getline(&buf
, &len
, file
) > 0) {
603 ret
= strncmp(buf
, search
, strlen(search
));
613 p
= strchr(buf
, ':');
614 if (p
&& *(p
+1) == ' ' && *(p
+2))
620 /* squash extra space characters (branding string) */
627 while (*q
&& isspace(*q
))
630 while ((*r
++ = *q
++));
634 ret
= do_write_string(fd
, s
);
641 static int write_nrcpus(int fd
, struct perf_header
*h __maybe_unused
,
642 struct perf_evlist
*evlist __maybe_unused
)
648 nr
= sysconf(_SC_NPROCESSORS_CONF
);
652 nrc
= (u32
)(nr
& UINT_MAX
);
654 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
658 nra
= (u32
)(nr
& UINT_MAX
);
660 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
664 return do_write(fd
, &nra
, sizeof(nra
));
667 static int write_event_desc(int fd
, struct perf_header
*h __maybe_unused
,
668 struct perf_evlist
*evlist
)
670 struct perf_evsel
*evsel
;
674 nre
= evlist
->nr_entries
;
677 * write number of events
679 ret
= do_write(fd
, &nre
, sizeof(nre
));
684 * size of perf_event_attr struct
686 sz
= (u32
)sizeof(evsel
->attr
);
687 ret
= do_write(fd
, &sz
, sizeof(sz
));
691 evlist__for_each(evlist
, evsel
) {
692 ret
= do_write(fd
, &evsel
->attr
, sz
);
696 * write number of unique id per event
697 * there is one id per instance of an event
699 * copy into an nri to be independent of the
703 ret
= do_write(fd
, &nri
, sizeof(nri
));
708 * write event string as passed on cmdline
710 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
714 * write unique ids for this event
716 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
723 static int write_cmdline(int fd
, struct perf_header
*h __maybe_unused
,
724 struct perf_evlist
*evlist __maybe_unused
)
726 char buf
[MAXPATHLEN
];
732 * actual atual path to perf binary
734 sprintf(proc
, "/proc/%d/exe", getpid());
735 ret
= readlink(proc
, buf
, sizeof(buf
));
739 /* readlink() does not add null termination */
742 /* account for binary path */
745 ret
= do_write(fd
, &n
, sizeof(n
));
749 ret
= do_write_string(fd
, buf
);
753 for (i
= 0 ; i
< header_argc
; i
++) {
754 ret
= do_write_string(fd
, header_argv
[i
]);
761 #define CORE_SIB_FMT \
762 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
763 #define THRD_SIB_FMT \
764 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
769 char **core_siblings
;
770 char **thread_siblings
;
773 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
776 char filename
[MAXPATHLEN
];
777 char *buf
= NULL
, *p
;
783 sprintf(filename
, CORE_SIB_FMT
, cpu
);
784 fp
= fopen(filename
, "r");
788 sret
= getline(&buf
, &len
, fp
);
793 p
= strchr(buf
, '\n');
797 for (i
= 0; i
< tp
->core_sib
; i
++) {
798 if (!strcmp(buf
, tp
->core_siblings
[i
]))
801 if (i
== tp
->core_sib
) {
802 tp
->core_siblings
[i
] = buf
;
810 sprintf(filename
, THRD_SIB_FMT
, cpu
);
811 fp
= fopen(filename
, "r");
815 if (getline(&buf
, &len
, fp
) <= 0)
818 p
= strchr(buf
, '\n');
822 for (i
= 0; i
< tp
->thread_sib
; i
++) {
823 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
826 if (i
== tp
->thread_sib
) {
827 tp
->thread_siblings
[i
] = buf
;
839 static void free_cpu_topo(struct cpu_topo
*tp
)
846 for (i
= 0 ; i
< tp
->core_sib
; i
++)
847 zfree(&tp
->core_siblings
[i
]);
849 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
850 zfree(&tp
->thread_siblings
[i
]);
855 static struct cpu_topo
*build_cpu_topology(void)
864 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
868 nr
= (u32
)(ncpus
& UINT_MAX
);
870 sz
= nr
* sizeof(char *);
872 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
879 tp
->core_siblings
= addr
;
881 tp
->thread_siblings
= addr
;
883 for (i
= 0; i
< nr
; i
++) {
884 ret
= build_cpu_topo(tp
, i
);
895 static int write_cpu_topology(int fd
, struct perf_header
*h __maybe_unused
,
896 struct perf_evlist
*evlist __maybe_unused
)
902 tp
= build_cpu_topology();
906 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
910 for (i
= 0; i
< tp
->core_sib
; i
++) {
911 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
915 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
919 for (i
= 0; i
< tp
->thread_sib
; i
++) {
920 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
931 static int write_total_mem(int fd
, struct perf_header
*h __maybe_unused
,
932 struct perf_evlist
*evlist __maybe_unused
)
940 fp
= fopen("/proc/meminfo", "r");
944 while (getline(&buf
, &len
, fp
) > 0) {
945 ret
= strncmp(buf
, "MemTotal:", 9);
950 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
952 ret
= do_write(fd
, &mem
, sizeof(mem
));
959 static int write_topo_node(int fd
, int node
)
961 char str
[MAXPATHLEN
];
963 char *buf
= NULL
, *p
;
966 u64 mem_total
, mem_free
, mem
;
969 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
970 fp
= fopen(str
, "r");
974 while (getline(&buf
, &len
, fp
) > 0) {
975 /* skip over invalid lines */
976 if (!strchr(buf
, ':'))
978 if (sscanf(buf
, "%*s %*d %31s %"PRIu64
, field
, &mem
) != 2)
980 if (!strcmp(field
, "MemTotal:"))
982 if (!strcmp(field
, "MemFree:"))
989 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
993 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
998 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
1000 fp
= fopen(str
, "r");
1004 if (getline(&buf
, &len
, fp
) <= 0)
1007 p
= strchr(buf
, '\n');
1011 ret
= do_write_string(fd
, buf
);
1019 static int write_numa_topology(int fd
, struct perf_header
*h __maybe_unused
,
1020 struct perf_evlist
*evlist __maybe_unused
)
1025 struct cpu_map
*node_map
= NULL
;
1030 fp
= fopen("/sys/devices/system/node/online", "r");
1034 if (getline(&buf
, &len
, fp
) <= 0)
1037 c
= strchr(buf
, '\n');
1041 node_map
= cpu_map__new(buf
);
1045 nr
= (u32
)node_map
->nr
;
1047 ret
= do_write(fd
, &nr
, sizeof(nr
));
1051 for (i
= 0; i
< nr
; i
++) {
1052 j
= (u32
)node_map
->map
[i
];
1053 ret
= do_write(fd
, &j
, sizeof(j
));
1057 ret
= write_topo_node(fd
, i
);
1071 * struct pmu_mappings {
1080 static int write_pmu_mappings(int fd
, struct perf_header
*h __maybe_unused
,
1081 struct perf_evlist
*evlist __maybe_unused
)
1083 struct perf_pmu
*pmu
= NULL
;
1084 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
1088 /* write real pmu_num later */
1089 ret
= do_write(fd
, &pmu_num
, sizeof(pmu_num
));
1093 while ((pmu
= perf_pmu__scan(pmu
))) {
1098 ret
= do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
1102 ret
= do_write_string(fd
, pmu
->name
);
1107 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
1109 lseek(fd
, offset
, SEEK_SET
);
1119 * struct group_descs {
1121 * struct group_desc {
1128 static int write_group_desc(int fd
, struct perf_header
*h __maybe_unused
,
1129 struct perf_evlist
*evlist
)
1131 u32 nr_groups
= evlist
->nr_groups
;
1132 struct perf_evsel
*evsel
;
1135 ret
= do_write(fd
, &nr_groups
, sizeof(nr_groups
));
1139 evlist__for_each(evlist
, evsel
) {
1140 if (perf_evsel__is_group_leader(evsel
) &&
1141 evsel
->nr_members
> 1) {
1142 const char *name
= evsel
->group_name
?: "{anon_group}";
1143 u32 leader_idx
= evsel
->idx
;
1144 u32 nr_members
= evsel
->nr_members
;
1146 ret
= do_write_string(fd
, name
);
1150 ret
= do_write(fd
, &leader_idx
, sizeof(leader_idx
));
1154 ret
= do_write(fd
, &nr_members
, sizeof(nr_members
));
1163 * default get_cpuid(): nothing gets recorded
1164 * actual implementation must be in arch/$(ARCH)/util/header.c
1166 int __attribute__ ((weak
)) get_cpuid(char *buffer __maybe_unused
,
1167 size_t sz __maybe_unused
)
1172 static int write_cpuid(int fd
, struct perf_header
*h __maybe_unused
,
1173 struct perf_evlist
*evlist __maybe_unused
)
1178 ret
= get_cpuid(buffer
, sizeof(buffer
));
1184 return do_write_string(fd
, buffer
);
1187 static int write_branch_stack(int fd __maybe_unused
,
1188 struct perf_header
*h __maybe_unused
,
1189 struct perf_evlist
*evlist __maybe_unused
)
1194 static void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
1197 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
1200 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
1203 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
1206 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1208 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
1211 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
1214 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
1217 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
1220 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
1221 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
1224 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
1227 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
1230 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
1236 nr
= ph
->env
.nr_cmdline
;
1237 str
= ph
->env
.cmdline
;
1239 fprintf(fp
, "# cmdline : ");
1241 for (i
= 0; i
< nr
; i
++) {
1242 fprintf(fp
, "%s ", str
);
1243 str
+= strlen(str
) + 1;
1248 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1254 nr
= ph
->env
.nr_sibling_cores
;
1255 str
= ph
->env
.sibling_cores
;
1257 for (i
= 0; i
< nr
; i
++) {
1258 fprintf(fp
, "# sibling cores : %s\n", str
);
1259 str
+= strlen(str
) + 1;
1262 nr
= ph
->env
.nr_sibling_threads
;
1263 str
= ph
->env
.sibling_threads
;
1265 for (i
= 0; i
< nr
; i
++) {
1266 fprintf(fp
, "# sibling threads : %s\n", str
);
1267 str
+= strlen(str
) + 1;
1271 static void free_event_desc(struct perf_evsel
*events
)
1273 struct perf_evsel
*evsel
;
1278 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1279 zfree(&evsel
->name
);
1286 static struct perf_evsel
*
1287 read_event_desc(struct perf_header
*ph
, int fd
)
1289 struct perf_evsel
*evsel
, *events
= NULL
;
1292 u32 nre
, sz
, nr
, i
, j
;
1296 /* number of events */
1297 ret
= readn(fd
, &nre
, sizeof(nre
));
1298 if (ret
!= (ssize_t
)sizeof(nre
))
1302 nre
= bswap_32(nre
);
1304 ret
= readn(fd
, &sz
, sizeof(sz
));
1305 if (ret
!= (ssize_t
)sizeof(sz
))
1311 /* buffer to hold on file attr struct */
1316 /* the last event terminates with evsel->attr.size == 0: */
1317 events
= calloc(nre
+ 1, sizeof(*events
));
1321 msz
= sizeof(evsel
->attr
);
1325 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1329 * must read entire on-file attr struct to
1330 * sync up with layout.
1332 ret
= readn(fd
, buf
, sz
);
1333 if (ret
!= (ssize_t
)sz
)
1337 perf_event__attr_swap(buf
);
1339 memcpy(&evsel
->attr
, buf
, msz
);
1341 ret
= readn(fd
, &nr
, sizeof(nr
));
1342 if (ret
!= (ssize_t
)sizeof(nr
))
1345 if (ph
->needs_swap
) {
1347 evsel
->needs_swap
= true;
1350 evsel
->name
= do_read_string(fd
, ph
);
1355 id
= calloc(nr
, sizeof(*id
));
1361 for (j
= 0 ; j
< nr
; j
++) {
1362 ret
= readn(fd
, id
, sizeof(*id
));
1363 if (ret
!= (ssize_t
)sizeof(*id
))
1366 *id
= bswap_64(*id
);
1375 free_event_desc(events
);
1380 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1382 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1387 fprintf(fp
, "# event desc: not available or unable to read\n");
1391 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1392 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1394 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1395 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1397 (u64
)evsel
->attr
.config
,
1398 (u64
)evsel
->attr
.config1
,
1399 (u64
)evsel
->attr
.config2
);
1401 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1402 evsel
->attr
.exclude_user
,
1403 evsel
->attr
.exclude_kernel
);
1405 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1406 evsel
->attr
.exclude_host
,
1407 evsel
->attr
.exclude_guest
);
1409 fprintf(fp
, ", precise_ip = %d", evsel
->attr
.precise_ip
);
1411 fprintf(fp
, ", attr_mmap2 = %d", evsel
->attr
.mmap2
);
1412 fprintf(fp
, ", attr_mmap = %d", evsel
->attr
.mmap
);
1413 fprintf(fp
, ", attr_mmap_data = %d", evsel
->attr
.mmap_data
);
1415 fprintf(fp
, ", id = {");
1416 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1419 fprintf(fp
, " %"PRIu64
, *id
);
1427 free_event_desc(events
);
1430 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1433 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1436 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1441 uint64_t mem_total
, mem_free
;
1444 nr
= ph
->env
.nr_numa_nodes
;
1445 str
= ph
->env
.numa_nodes
;
1447 for (i
= 0; i
< nr
; i
++) {
1449 c
= strtoul(str
, &tmp
, 0);
1454 mem_total
= strtoull(str
, &tmp
, 0);
1459 mem_free
= strtoull(str
, &tmp
, 0);
1463 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1464 " free = %"PRIu64
" kB\n",
1465 c
, mem_total
, mem_free
);
1468 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1470 str
+= strlen(str
) + 1;
1474 fprintf(fp
, "# numa topology : not available\n");
1477 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1479 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1482 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1483 int fd __maybe_unused
, FILE *fp
)
1485 fprintf(fp
, "# contains samples with branch stack\n");
1488 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1491 const char *delimiter
= "# pmu mappings: ";
1496 pmu_num
= ph
->env
.nr_pmu_mappings
;
1498 fprintf(fp
, "# pmu mappings: not available\n");
1502 str
= ph
->env
.pmu_mappings
;
1505 type
= strtoul(str
, &tmp
, 0);
1510 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1513 str
+= strlen(str
) + 1;
1522 fprintf(fp
, "# pmu mappings: unable to read\n");
1525 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1528 struct perf_session
*session
;
1529 struct perf_evsel
*evsel
;
1532 session
= container_of(ph
, struct perf_session
, header
);
1534 evlist__for_each(session
->evlist
, evsel
) {
1535 if (perf_evsel__is_group_leader(evsel
) &&
1536 evsel
->nr_members
> 1) {
1537 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1538 perf_evsel__name(evsel
));
1540 nr
= evsel
->nr_members
- 1;
1542 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1550 static int __event_process_build_id(struct build_id_event
*bev
,
1552 struct perf_session
*session
)
1556 struct machine
*machine
;
1559 enum dso_kernel_type dso_type
;
1561 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1565 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1568 case PERF_RECORD_MISC_KERNEL
:
1569 dso_type
= DSO_TYPE_KERNEL
;
1570 dsos
= &machine
->kernel_dsos
;
1572 case PERF_RECORD_MISC_GUEST_KERNEL
:
1573 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1574 dsos
= &machine
->kernel_dsos
;
1576 case PERF_RECORD_MISC_USER
:
1577 case PERF_RECORD_MISC_GUEST_USER
:
1578 dso_type
= DSO_TYPE_USER
;
1579 dsos
= &machine
->user_dsos
;
1585 dso
= __dsos__findnew(dsos
, filename
);
1587 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1589 dso__set_build_id(dso
, &bev
->build_id
);
1591 if (filename
[0] == '[')
1592 dso
->kernel
= dso_type
;
1594 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1596 pr_debug("build id event received for %s: %s\n",
1597 dso
->long_name
, sbuild_id
);
1605 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1606 int input
, u64 offset
, u64 size
)
1608 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1610 struct perf_event_header header
;
1611 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1614 struct build_id_event bev
;
1615 char filename
[PATH_MAX
];
1616 u64 limit
= offset
+ size
;
1618 while (offset
< limit
) {
1621 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1624 if (header
->needs_swap
)
1625 perf_event_header__bswap(&old_bev
.header
);
1627 len
= old_bev
.header
.size
- sizeof(old_bev
);
1628 if (readn(input
, filename
, len
) != len
)
1631 bev
.header
= old_bev
.header
;
1634 * As the pid is the missing value, we need to fill
1635 * it properly. The header.misc value give us nice hint.
1637 bev
.pid
= HOST_KERNEL_ID
;
1638 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1639 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1640 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1642 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1643 __event_process_build_id(&bev
, filename
, session
);
1645 offset
+= bev
.header
.size
;
1651 static int perf_header__read_build_ids(struct perf_header
*header
,
1652 int input
, u64 offset
, u64 size
)
1654 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1655 struct build_id_event bev
;
1656 char filename
[PATH_MAX
];
1657 u64 limit
= offset
+ size
, orig_offset
= offset
;
1660 while (offset
< limit
) {
1663 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1666 if (header
->needs_swap
)
1667 perf_event_header__bswap(&bev
.header
);
1669 len
= bev
.header
.size
- sizeof(bev
);
1670 if (readn(input
, filename
, len
) != len
)
1673 * The a1645ce1 changeset:
1675 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1677 * Added a field to struct build_id_event that broke the file
1680 * Since the kernel build-id is the first entry, process the
1681 * table using the old format if the well known
1682 * '[kernel.kallsyms]' string for the kernel build-id has the
1683 * first 4 characters chopped off (where the pid_t sits).
1685 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1686 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1688 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1691 __event_process_build_id(&bev
, filename
, session
);
1693 offset
+= bev
.header
.size
;
1700 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1701 struct perf_header
*ph __maybe_unused
,
1704 ssize_t ret
= trace_report(fd
, data
, false);
1705 return ret
< 0 ? -1 : 0;
1708 static int process_build_id(struct perf_file_section
*section
,
1709 struct perf_header
*ph
, int fd
,
1710 void *data __maybe_unused
)
1712 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1713 pr_debug("Failed to read buildids, continuing...\n");
1717 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1718 struct perf_header
*ph
, int fd
,
1719 void *data __maybe_unused
)
1721 ph
->env
.hostname
= do_read_string(fd
, ph
);
1722 return ph
->env
.hostname
? 0 : -ENOMEM
;
1725 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1726 struct perf_header
*ph
, int fd
,
1727 void *data __maybe_unused
)
1729 ph
->env
.os_release
= do_read_string(fd
, ph
);
1730 return ph
->env
.os_release
? 0 : -ENOMEM
;
1733 static int process_version(struct perf_file_section
*section __maybe_unused
,
1734 struct perf_header
*ph
, int fd
,
1735 void *data __maybe_unused
)
1737 ph
->env
.version
= do_read_string(fd
, ph
);
1738 return ph
->env
.version
? 0 : -ENOMEM
;
1741 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1742 struct perf_header
*ph
, int fd
,
1743 void *data __maybe_unused
)
1745 ph
->env
.arch
= do_read_string(fd
, ph
);
1746 return ph
->env
.arch
? 0 : -ENOMEM
;
1749 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1750 struct perf_header
*ph
, int fd
,
1751 void *data __maybe_unused
)
1756 ret
= readn(fd
, &nr
, sizeof(nr
));
1757 if (ret
!= sizeof(nr
))
1763 ph
->env
.nr_cpus_online
= nr
;
1765 ret
= readn(fd
, &nr
, sizeof(nr
));
1766 if (ret
!= sizeof(nr
))
1772 ph
->env
.nr_cpus_avail
= nr
;
1776 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1777 struct perf_header
*ph
, int fd
,
1778 void *data __maybe_unused
)
1780 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1781 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1784 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1785 struct perf_header
*ph
, int fd
,
1786 void *data __maybe_unused
)
1788 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1789 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1792 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1793 struct perf_header
*ph
, int fd
,
1794 void *data __maybe_unused
)
1799 ret
= readn(fd
, &mem
, sizeof(mem
));
1800 if (ret
!= sizeof(mem
))
1804 mem
= bswap_64(mem
);
1806 ph
->env
.total_mem
= mem
;
1810 static struct perf_evsel
*
1811 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1813 struct perf_evsel
*evsel
;
1815 evlist__for_each(evlist
, evsel
) {
1816 if (evsel
->idx
== idx
)
1824 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1825 struct perf_evsel
*event
)
1827 struct perf_evsel
*evsel
;
1832 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1839 evsel
->name
= strdup(event
->name
);
1843 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1844 struct perf_header
*header
, int fd
,
1845 void *data __maybe_unused
)
1847 struct perf_session
*session
;
1848 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1853 session
= container_of(header
, struct perf_session
, header
);
1854 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1855 perf_evlist__set_event_name(session
->evlist
, evsel
);
1857 free_event_desc(events
);
1862 static int process_cmdline(struct perf_file_section
*section __maybe_unused
,
1863 struct perf_header
*ph
, int fd
,
1864 void *data __maybe_unused
)
1871 ret
= readn(fd
, &nr
, sizeof(nr
));
1872 if (ret
!= sizeof(nr
))
1878 ph
->env
.nr_cmdline
= nr
;
1879 strbuf_init(&sb
, 128);
1881 for (i
= 0; i
< nr
; i
++) {
1882 str
= do_read_string(fd
, ph
);
1886 /* include a NULL character at the end */
1887 strbuf_add(&sb
, str
, strlen(str
) + 1);
1890 ph
->env
.cmdline
= strbuf_detach(&sb
, NULL
);
1894 strbuf_release(&sb
);
1898 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1899 struct perf_header
*ph
, int fd
,
1900 void *data __maybe_unused
)
1907 ret
= readn(fd
, &nr
, sizeof(nr
));
1908 if (ret
!= sizeof(nr
))
1914 ph
->env
.nr_sibling_cores
= nr
;
1915 strbuf_init(&sb
, 128);
1917 for (i
= 0; i
< nr
; i
++) {
1918 str
= do_read_string(fd
, ph
);
1922 /* include a NULL character at the end */
1923 strbuf_add(&sb
, str
, strlen(str
) + 1);
1926 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1928 ret
= readn(fd
, &nr
, sizeof(nr
));
1929 if (ret
!= sizeof(nr
))
1935 ph
->env
.nr_sibling_threads
= nr
;
1937 for (i
= 0; i
< nr
; i
++) {
1938 str
= do_read_string(fd
, ph
);
1942 /* include a NULL character at the end */
1943 strbuf_add(&sb
, str
, strlen(str
) + 1);
1946 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1950 strbuf_release(&sb
);
1954 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1955 struct perf_header
*ph
, int fd
,
1956 void *data __maybe_unused
)
1961 uint64_t mem_total
, mem_free
;
1965 ret
= readn(fd
, &nr
, sizeof(nr
));
1966 if (ret
!= sizeof(nr
))
1972 ph
->env
.nr_numa_nodes
= nr
;
1973 strbuf_init(&sb
, 256);
1975 for (i
= 0; i
< nr
; i
++) {
1977 ret
= readn(fd
, &node
, sizeof(node
));
1978 if (ret
!= sizeof(node
))
1981 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1982 if (ret
!= sizeof(u64
))
1985 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1986 if (ret
!= sizeof(u64
))
1989 if (ph
->needs_swap
) {
1990 node
= bswap_32(node
);
1991 mem_total
= bswap_64(mem_total
);
1992 mem_free
= bswap_64(mem_free
);
1995 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1996 node
, mem_total
, mem_free
);
1998 str
= do_read_string(fd
, ph
);
2002 /* include a NULL character at the end */
2003 strbuf_add(&sb
, str
, strlen(str
) + 1);
2006 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
2010 strbuf_release(&sb
);
2014 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
2015 struct perf_header
*ph
, int fd
,
2016 void *data __maybe_unused
)
2024 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
2025 if (ret
!= sizeof(pmu_num
))
2029 pmu_num
= bswap_32(pmu_num
);
2032 pr_debug("pmu mappings not available\n");
2036 ph
->env
.nr_pmu_mappings
= pmu_num
;
2037 strbuf_init(&sb
, 128);
2040 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
2043 type
= bswap_32(type
);
2045 name
= do_read_string(fd
, ph
);
2049 strbuf_addf(&sb
, "%u:%s", type
, name
);
2050 /* include a NULL character at the end */
2051 strbuf_add(&sb
, "", 1);
2056 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2060 strbuf_release(&sb
);
2064 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
2065 struct perf_header
*ph
, int fd
,
2066 void *data __maybe_unused
)
2069 u32 i
, nr
, nr_groups
;
2070 struct perf_session
*session
;
2071 struct perf_evsel
*evsel
, *leader
= NULL
;
2078 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
2082 nr_groups
= bswap_32(nr_groups
);
2084 ph
->env
.nr_groups
= nr_groups
;
2086 pr_debug("group desc not available\n");
2090 desc
= calloc(nr_groups
, sizeof(*desc
));
2094 for (i
= 0; i
< nr_groups
; i
++) {
2095 desc
[i
].name
= do_read_string(fd
, ph
);
2099 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
2102 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
2105 if (ph
->needs_swap
) {
2106 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
2107 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
2112 * Rebuild group relationship based on the group_desc
2114 session
= container_of(ph
, struct perf_session
, header
);
2115 session
->evlist
->nr_groups
= nr_groups
;
2118 evlist__for_each(session
->evlist
, evsel
) {
2119 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2120 evsel
->leader
= evsel
;
2121 /* {anon_group} is a dummy name */
2122 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2123 evsel
->group_name
= desc
[i
].name
;
2124 desc
[i
].name
= NULL
;
2126 evsel
->nr_members
= desc
[i
].nr_members
;
2128 if (i
>= nr_groups
|| nr
> 0) {
2129 pr_debug("invalid group desc\n");
2134 nr
= evsel
->nr_members
- 1;
2137 /* This is a group member */
2138 evsel
->leader
= leader
;
2144 if (i
!= nr_groups
|| nr
!= 0) {
2145 pr_debug("invalid group desc\n");
2151 for (i
= 0; i
< nr_groups
; i
++)
2152 zfree(&desc
[i
].name
);
2158 struct feature_ops
{
2159 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
2160 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
2161 int (*process
)(struct perf_file_section
*section
,
2162 struct perf_header
*h
, int fd
, void *data
);
2167 #define FEAT_OPA(n, func) \
2168 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2169 #define FEAT_OPP(n, func) \
2170 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2171 .process = process_##func }
2172 #define FEAT_OPF(n, func) \
2173 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2174 .process = process_##func, .full_only = true }
2176 /* feature_ops not implemented: */
2177 #define print_tracing_data NULL
2178 #define print_build_id NULL
2180 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2181 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
2182 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
2183 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
2184 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
2185 FEAT_OPP(HEADER_VERSION
, version
),
2186 FEAT_OPP(HEADER_ARCH
, arch
),
2187 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
2188 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
2189 FEAT_OPP(HEADER_CPUID
, cpuid
),
2190 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
2191 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
2192 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
2193 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
2194 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
2195 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
2196 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
2197 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
2200 struct header_print_data
{
2202 bool full
; /* extended list of headers */
2205 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2206 struct perf_header
*ph
,
2207 int feat
, int fd
, void *data
)
2209 struct header_print_data
*hd
= data
;
2211 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2212 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2213 "%d, continuing...\n", section
->offset
, feat
);
2216 if (feat
>= HEADER_LAST_FEATURE
) {
2217 pr_warning("unknown feature %d\n", feat
);
2220 if (!feat_ops
[feat
].print
)
2223 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2224 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
2226 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2227 feat_ops
[feat
].name
);
2232 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2234 struct header_print_data hd
;
2235 struct perf_header
*header
= &session
->header
;
2236 int fd
= perf_data_file__fd(session
->file
);
2240 perf_header__process_sections(header
, fd
, &hd
,
2241 perf_file_section__fprintf_info
);
2245 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
2246 struct perf_file_section
**p
,
2247 struct perf_evlist
*evlist
)
2252 if (perf_header__has_feat(h
, type
)) {
2253 if (!feat_ops
[type
].write
)
2256 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
2258 err
= feat_ops
[type
].write(fd
, h
, evlist
);
2260 pr_debug("failed to write feature %d\n", type
);
2262 /* undo anything written */
2263 lseek(fd
, (*p
)->offset
, SEEK_SET
);
2267 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2273 static int perf_header__adds_write(struct perf_header
*header
,
2274 struct perf_evlist
*evlist
, int fd
)
2277 struct perf_file_section
*feat_sec
, *p
;
2283 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2287 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2288 if (feat_sec
== NULL
)
2291 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2293 sec_start
= header
->feat_offset
;
2294 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2296 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2297 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2298 perf_header__clear_feat(header
, feat
);
2301 lseek(fd
, sec_start
, SEEK_SET
);
2303 * may write more than needed due to dropped feature, but
2304 * this is okay, reader will skip the mising entries
2306 err
= do_write(fd
, feat_sec
, sec_size
);
2308 pr_debug("failed to write feature section\n");
2313 int perf_header__write_pipe(int fd
)
2315 struct perf_pipe_file_header f_header
;
2318 f_header
= (struct perf_pipe_file_header
){
2319 .magic
= PERF_MAGIC
,
2320 .size
= sizeof(f_header
),
2323 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2325 pr_debug("failed to write perf pipe header\n");
2332 int perf_session__write_header(struct perf_session
*session
,
2333 struct perf_evlist
*evlist
,
2334 int fd
, bool at_exit
)
2336 struct perf_file_header f_header
;
2337 struct perf_file_attr f_attr
;
2338 struct perf_header
*header
= &session
->header
;
2339 struct perf_evsel
*evsel
;
2343 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2345 evlist__for_each(session
->evlist
, evsel
) {
2346 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2347 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2349 pr_debug("failed to write perf header\n");
2354 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2356 evlist__for_each(evlist
, evsel
) {
2357 f_attr
= (struct perf_file_attr
){
2358 .attr
= evsel
->attr
,
2360 .offset
= evsel
->id_offset
,
2361 .size
= evsel
->ids
* sizeof(u64
),
2364 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2366 pr_debug("failed to write perf header attribute\n");
2371 if (!header
->data_offset
)
2372 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2373 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2376 err
= perf_header__adds_write(header
, evlist
, fd
);
2381 f_header
= (struct perf_file_header
){
2382 .magic
= PERF_MAGIC
,
2383 .size
= sizeof(f_header
),
2384 .attr_size
= sizeof(f_attr
),
2386 .offset
= attr_offset
,
2387 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2390 .offset
= header
->data_offset
,
2391 .size
= header
->data_size
,
2393 /* event_types is ignored, store zeros */
2396 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2398 lseek(fd
, 0, SEEK_SET
);
2399 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2401 pr_debug("failed to write perf header\n");
2404 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2409 static int perf_header__getbuffer64(struct perf_header
*header
,
2410 int fd
, void *buf
, size_t size
)
2412 if (readn(fd
, buf
, size
) <= 0)
2415 if (header
->needs_swap
)
2416 mem_bswap_64(buf
, size
);
2421 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2423 int (*process
)(struct perf_file_section
*section
,
2424 struct perf_header
*ph
,
2425 int feat
, int fd
, void *data
))
2427 struct perf_file_section
*feat_sec
, *sec
;
2433 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2437 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2441 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2443 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2445 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2449 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2450 err
= process(sec
++, header
, feat
, fd
, data
);
2460 static const int attr_file_abi_sizes
[] = {
2461 [0] = PERF_ATTR_SIZE_VER0
,
2462 [1] = PERF_ATTR_SIZE_VER1
,
2463 [2] = PERF_ATTR_SIZE_VER2
,
2464 [3] = PERF_ATTR_SIZE_VER3
,
2469 * In the legacy file format, the magic number is not used to encode endianness.
2470 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2471 * on ABI revisions, we need to try all combinations for all endianness to
2472 * detect the endianness.
2474 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2476 uint64_t ref_size
, attr_size
;
2479 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2480 ref_size
= attr_file_abi_sizes
[i
]
2481 + sizeof(struct perf_file_section
);
2482 if (hdr_sz
!= ref_size
) {
2483 attr_size
= bswap_64(hdr_sz
);
2484 if (attr_size
!= ref_size
)
2487 ph
->needs_swap
= true;
2489 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2494 /* could not determine endianness */
2498 #define PERF_PIPE_HDR_VER0 16
2500 static const size_t attr_pipe_abi_sizes
[] = {
2501 [0] = PERF_PIPE_HDR_VER0
,
2506 * In the legacy pipe format, there is an implicit assumption that endiannesss
2507 * between host recording the samples, and host parsing the samples is the
2508 * same. This is not always the case given that the pipe output may always be
2509 * redirected into a file and analyzed on a different machine with possibly a
2510 * different endianness and perf_event ABI revsions in the perf tool itself.
2512 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2517 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2518 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2519 attr_size
= bswap_64(hdr_sz
);
2520 if (attr_size
!= hdr_sz
)
2523 ph
->needs_swap
= true;
2525 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2531 bool is_perf_magic(u64 magic
)
2533 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2534 || magic
== __perf_magic2
2535 || magic
== __perf_magic2_sw
)
2541 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2542 bool is_pipe
, struct perf_header
*ph
)
2546 /* check for legacy format */
2547 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2549 ph
->version
= PERF_HEADER_VERSION_1
;
2550 pr_debug("legacy perf.data format\n");
2552 return try_all_pipe_abis(hdr_sz
, ph
);
2554 return try_all_file_abis(hdr_sz
, ph
);
2557 * the new magic number serves two purposes:
2558 * - unique number to identify actual perf.data files
2559 * - encode endianness of file
2562 /* check magic number with one endianness */
2563 if (magic
== __perf_magic2
)
2566 /* check magic number with opposite endianness */
2567 if (magic
!= __perf_magic2_sw
)
2570 ph
->needs_swap
= true;
2571 ph
->version
= PERF_HEADER_VERSION_2
;
2576 int perf_file_header__read(struct perf_file_header
*header
,
2577 struct perf_header
*ph
, int fd
)
2581 lseek(fd
, 0, SEEK_SET
);
2583 ret
= readn(fd
, header
, sizeof(*header
));
2587 if (check_magic_endian(header
->magic
,
2588 header
->attr_size
, false, ph
) < 0) {
2589 pr_debug("magic/endian check failed\n");
2593 if (ph
->needs_swap
) {
2594 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2598 if (header
->size
!= sizeof(*header
)) {
2599 /* Support the previous format */
2600 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2601 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2604 } else if (ph
->needs_swap
) {
2606 * feature bitmap is declared as an array of unsigned longs --
2607 * not good since its size can differ between the host that
2608 * generated the data file and the host analyzing the file.
2610 * We need to handle endianness, but we don't know the size of
2611 * the unsigned long where the file was generated. Take a best
2612 * guess at determining it: try 64-bit swap first (ie., file
2613 * created on a 64-bit host), and check if the hostname feature
2614 * bit is set (this feature bit is forced on as of fbe96f2).
2615 * If the bit is not, undo the 64-bit swap and try a 32-bit
2616 * swap. If the hostname bit is still not set (e.g., older data
2617 * file), punt and fallback to the original behavior --
2618 * clearing all feature bits and setting buildid.
2620 mem_bswap_64(&header
->adds_features
,
2621 BITS_TO_U64(HEADER_FEAT_BITS
));
2623 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2625 mem_bswap_64(&header
->adds_features
,
2626 BITS_TO_U64(HEADER_FEAT_BITS
));
2629 mem_bswap_32(&header
->adds_features
,
2630 BITS_TO_U32(HEADER_FEAT_BITS
));
2633 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2634 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2635 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2639 memcpy(&ph
->adds_features
, &header
->adds_features
,
2640 sizeof(ph
->adds_features
));
2642 ph
->data_offset
= header
->data
.offset
;
2643 ph
->data_size
= header
->data
.size
;
2644 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2648 static int perf_file_section__process(struct perf_file_section
*section
,
2649 struct perf_header
*ph
,
2650 int feat
, int fd
, void *data
)
2652 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2653 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2654 "%d, continuing...\n", section
->offset
, feat
);
2658 if (feat
>= HEADER_LAST_FEATURE
) {
2659 pr_debug("unknown feature %d, continuing...\n", feat
);
2663 if (!feat_ops
[feat
].process
)
2666 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2669 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2670 struct perf_header
*ph
, int fd
,
2675 ret
= readn(fd
, header
, sizeof(*header
));
2679 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2680 pr_debug("endian/magic failed\n");
2685 header
->size
= bswap_64(header
->size
);
2687 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2693 static int perf_header__read_pipe(struct perf_session
*session
)
2695 struct perf_header
*header
= &session
->header
;
2696 struct perf_pipe_file_header f_header
;
2698 if (perf_file_header__read_pipe(&f_header
, header
,
2699 perf_data_file__fd(session
->file
),
2700 session
->repipe
) < 0) {
2701 pr_debug("incompatible file format\n");
2708 static int read_attr(int fd
, struct perf_header
*ph
,
2709 struct perf_file_attr
*f_attr
)
2711 struct perf_event_attr
*attr
= &f_attr
->attr
;
2713 size_t our_sz
= sizeof(f_attr
->attr
);
2716 memset(f_attr
, 0, sizeof(*f_attr
));
2718 /* read minimal guaranteed structure */
2719 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2721 pr_debug("cannot read %d bytes of header attr\n",
2722 PERF_ATTR_SIZE_VER0
);
2726 /* on file perf_event_attr size */
2734 sz
= PERF_ATTR_SIZE_VER0
;
2735 } else if (sz
> our_sz
) {
2736 pr_debug("file uses a more recent and unsupported ABI"
2737 " (%zu bytes extra)\n", sz
- our_sz
);
2740 /* what we have not yet read and that we know about */
2741 left
= sz
- PERF_ATTR_SIZE_VER0
;
2744 ptr
+= PERF_ATTR_SIZE_VER0
;
2746 ret
= readn(fd
, ptr
, left
);
2748 /* read perf_file_section, ids are read in caller */
2749 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2751 return ret
<= 0 ? -1 : 0;
2754 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2755 struct pevent
*pevent
)
2757 struct event_format
*event
;
2760 /* already prepared */
2761 if (evsel
->tp_format
)
2764 if (pevent
== NULL
) {
2765 pr_debug("broken or missing trace data\n");
2769 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2774 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2775 evsel
->name
= strdup(bf
);
2776 if (evsel
->name
== NULL
)
2780 evsel
->tp_format
= event
;
2784 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2785 struct pevent
*pevent
)
2787 struct perf_evsel
*pos
;
2789 evlist__for_each(evlist
, pos
) {
2790 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2791 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2798 int perf_session__read_header(struct perf_session
*session
)
2800 struct perf_data_file
*file
= session
->file
;
2801 struct perf_header
*header
= &session
->header
;
2802 struct perf_file_header f_header
;
2803 struct perf_file_attr f_attr
;
2805 int nr_attrs
, nr_ids
, i
, j
;
2806 int fd
= perf_data_file__fd(file
);
2808 session
->evlist
= perf_evlist__new();
2809 if (session
->evlist
== NULL
)
2812 if (perf_data_file__is_pipe(file
))
2813 return perf_header__read_pipe(session
);
2815 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2819 * Sanity check that perf.data was written cleanly; data size is
2820 * initialized to 0 and updated only if the on_exit function is run.
2821 * If data size is still 0 then the file contains only partial
2822 * information. Just warn user and process it as much as it can.
2824 if (f_header
.data
.size
== 0) {
2825 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2826 "Was the 'perf record' command properly terminated?\n",
2830 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2831 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2833 for (i
= 0; i
< nr_attrs
; i
++) {
2834 struct perf_evsel
*evsel
;
2837 if (read_attr(fd
, header
, &f_attr
) < 0)
2840 if (header
->needs_swap
)
2841 perf_event__attr_swap(&f_attr
.attr
);
2843 tmp
= lseek(fd
, 0, SEEK_CUR
);
2844 evsel
= perf_evsel__new(&f_attr
.attr
);
2847 goto out_delete_evlist
;
2849 evsel
->needs_swap
= header
->needs_swap
;
2851 * Do it before so that if perf_evsel__alloc_id fails, this
2852 * entry gets purged too at perf_evlist__delete().
2854 perf_evlist__add(session
->evlist
, evsel
);
2856 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2858 * We don't have the cpu and thread maps on the header, so
2859 * for allocating the perf_sample_id table we fake 1 cpu and
2860 * hattr->ids threads.
2862 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2863 goto out_delete_evlist
;
2865 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2867 for (j
= 0; j
< nr_ids
; j
++) {
2868 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2871 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2874 lseek(fd
, tmp
, SEEK_SET
);
2877 symbol_conf
.nr_events
= nr_attrs
;
2879 perf_header__process_sections(header
, fd
, &session
->tevent
,
2880 perf_file_section__process
);
2882 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2883 session
->tevent
.pevent
))
2884 goto out_delete_evlist
;
2891 perf_evlist__delete(session
->evlist
);
2892 session
->evlist
= NULL
;
2896 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2897 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2898 perf_event__handler_t process
)
2900 union perf_event
*ev
;
2904 size
= sizeof(struct perf_event_attr
);
2905 size
= PERF_ALIGN(size
, sizeof(u64
));
2906 size
+= sizeof(struct perf_event_header
);
2907 size
+= ids
* sizeof(u64
);
2914 ev
->attr
.attr
= *attr
;
2915 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2917 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2918 ev
->attr
.header
.size
= (u16
)size
;
2920 if (ev
->attr
.header
.size
== size
)
2921 err
= process(tool
, ev
, NULL
, NULL
);
2930 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2931 struct perf_session
*session
,
2932 perf_event__handler_t process
)
2934 struct perf_evsel
*evsel
;
2937 evlist__for_each(session
->evlist
, evsel
) {
2938 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2939 evsel
->id
, process
);
2941 pr_debug("failed to create perf header attribute\n");
2949 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
2950 union perf_event
*event
,
2951 struct perf_evlist
**pevlist
)
2954 struct perf_evsel
*evsel
;
2955 struct perf_evlist
*evlist
= *pevlist
;
2957 if (evlist
== NULL
) {
2958 *pevlist
= evlist
= perf_evlist__new();
2963 evsel
= perf_evsel__new(&event
->attr
.attr
);
2967 perf_evlist__add(evlist
, evsel
);
2969 ids
= event
->header
.size
;
2970 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2971 n_ids
= ids
/ sizeof(u64
);
2973 * We don't have the cpu and thread maps on the header, so
2974 * for allocating the perf_sample_id table we fake 1 cpu and
2975 * hattr->ids threads.
2977 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2980 for (i
= 0; i
< n_ids
; i
++) {
2981 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2984 symbol_conf
.nr_events
= evlist
->nr_entries
;
2989 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2990 struct perf_evlist
*evlist
,
2991 perf_event__handler_t process
)
2993 union perf_event ev
;
2994 struct tracing_data
*tdata
;
2995 ssize_t size
= 0, aligned_size
= 0, padding
;
2996 int err __maybe_unused
= 0;
2999 * We are going to store the size of the data followed
3000 * by the data contents. Since the fd descriptor is a pipe,
3001 * we cannot seek back to store the size of the data once
3002 * we know it. Instead we:
3004 * - write the tracing data to the temp file
3005 * - get/write the data size to pipe
3006 * - write the tracing data from the temp file
3009 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3013 memset(&ev
, 0, sizeof(ev
));
3015 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3017 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3018 padding
= aligned_size
- size
;
3019 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3020 ev
.tracing_data
.size
= aligned_size
;
3022 process(tool
, &ev
, NULL
, NULL
);
3025 * The put function will copy all the tracing data
3026 * stored in temp file to the pipe.
3028 tracing_data_put(tdata
);
3030 write_padded(fd
, NULL
, 0, padding
);
3032 return aligned_size
;
3035 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
3036 union perf_event
*event
,
3037 struct perf_session
*session
)
3039 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3040 int fd
= perf_data_file__fd(session
->file
);
3041 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
3044 /* setup for reading amidst mmap */
3045 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
3048 size_read
= trace_report(fd
, &session
->tevent
,
3050 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3052 if (readn(fd
, buf
, padding
) < 0) {
3053 pr_err("%s: reading input file", __func__
);
3056 if (session
->repipe
) {
3057 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3058 if (retw
<= 0 || retw
!= padding
) {
3059 pr_err("%s: repiping tracing data padding", __func__
);
3064 if (size_read
+ padding
!= size
) {
3065 pr_err("%s: tracing data size mismatch", __func__
);
3069 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3070 session
->tevent
.pevent
);
3072 return size_read
+ padding
;
3075 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3076 struct dso
*pos
, u16 misc
,
3077 perf_event__handler_t process
,
3078 struct machine
*machine
)
3080 union perf_event ev
;
3087 memset(&ev
, 0, sizeof(ev
));
3089 len
= pos
->long_name_len
+ 1;
3090 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3091 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3092 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3093 ev
.build_id
.header
.misc
= misc
;
3094 ev
.build_id
.pid
= machine
->pid
;
3095 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3096 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3098 err
= process(tool
, &ev
, NULL
, machine
);
3103 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3104 union perf_event
*event
,
3105 struct perf_session
*session
)
3107 __event_process_build_id(&event
->build_id
,
3108 event
->build_id
.filename
,
3113 void disable_buildid_cache(void)
3115 no_buildid_cache
= true;