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
);
221 return __dsos__hit_all(&machine
->user_dsos
);
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
, machine
,
292 machine
->pid
, kmisc
, fd
);
294 err
= __dsos__write_buildid_table(&machine
->user_dsos
, machine
,
295 machine
->pid
, umisc
, fd
);
299 static int dsos__write_buildid_table(struct perf_header
*header
, int fd
)
301 struct perf_session
*session
= container_of(header
,
302 struct perf_session
, header
);
304 int err
= machine__write_buildid_table(&session
->machines
.host
, fd
);
309 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
310 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
311 err
= machine__write_buildid_table(pos
, fd
);
318 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
319 const char *name
, bool is_kallsyms
, bool is_vdso
)
321 const size_t size
= PATH_MAX
;
322 char *realname
, *filename
= zalloc(size
),
323 *linkname
= zalloc(size
), *targetname
;
325 bool slash
= is_kallsyms
|| is_vdso
;
328 if (symbol_conf
.kptr_restrict
) {
329 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
333 realname
= (char *) name
;
335 realname
= realpath(name
, NULL
);
337 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
340 len
= scnprintf(filename
, size
, "%s%s%s",
341 debugdir
, slash
? "/" : "",
342 is_vdso
? DSO__NAME_VDSO
: realname
);
343 if (mkdir_p(filename
, 0755))
346 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
348 if (access(filename
, F_OK
)) {
350 if (copyfile("/proc/kallsyms", filename
))
352 } else if (link(realname
, filename
) && copyfile(name
, filename
))
356 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
357 debugdir
, sbuild_id
);
359 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
362 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
363 targetname
= filename
+ strlen(debugdir
) - 5;
364 memcpy(targetname
, "../..", 5);
366 if (symlink(targetname
, linkname
) == 0)
376 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
377 const char *name
, const char *debugdir
,
378 bool is_kallsyms
, bool is_vdso
)
380 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
382 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
384 return build_id_cache__add_s(sbuild_id
, debugdir
, name
,
385 is_kallsyms
, is_vdso
);
388 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
390 const size_t size
= PATH_MAX
;
391 char *filename
= zalloc(size
),
392 *linkname
= zalloc(size
);
395 if (filename
== NULL
|| linkname
== NULL
)
398 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
399 debugdir
, sbuild_id
, sbuild_id
+ 2);
401 if (access(linkname
, F_OK
))
404 if (readlink(linkname
, filename
, size
- 1) < 0)
407 if (unlink(linkname
))
411 * Since the link is relative, we must make it absolute:
413 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
414 debugdir
, sbuild_id
, filename
);
416 if (unlink(linkname
))
426 static int dso__cache_build_id(struct dso
*dso
, struct machine
*machine
,
427 const char *debugdir
)
429 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
430 bool is_vdso
= dso__is_vdso(dso
);
431 const char *name
= dso
->long_name
;
434 if (dso__is_kcore(dso
)) {
436 machine__mmap_name(machine
, nm
, sizeof(nm
));
439 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
), name
,
440 debugdir
, is_kallsyms
, is_vdso
);
443 static int __dsos__cache_build_ids(struct list_head
*head
,
444 struct machine
*machine
, const char *debugdir
)
449 dsos__for_each_with_build_id(pos
, head
)
450 if (dso__cache_build_id(pos
, machine
, debugdir
))
456 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
458 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
, machine
,
460 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
, machine
, debugdir
);
464 static int perf_session__cache_build_ids(struct perf_session
*session
)
468 char debugdir
[PATH_MAX
];
470 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
472 if (mkdir(debugdir
, 0755) != 0 && errno
!= EEXIST
)
475 ret
= machine__cache_build_ids(&session
->machines
.host
, debugdir
);
477 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
478 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
479 ret
|= machine__cache_build_ids(pos
, debugdir
);
484 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
486 bool ret
= __dsos__read_build_ids(&machine
->kernel_dsos
, with_hits
);
487 ret
|= __dsos__read_build_ids(&machine
->user_dsos
, with_hits
);
491 static bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
494 bool ret
= machine__read_build_ids(&session
->machines
.host
, with_hits
);
496 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
497 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
498 ret
|= machine__read_build_ids(pos
, with_hits
);
504 static int write_tracing_data(int fd
, struct perf_header
*h __maybe_unused
,
505 struct perf_evlist
*evlist
)
507 return read_tracing_data(fd
, &evlist
->entries
);
511 static int write_build_id(int fd
, struct perf_header
*h
,
512 struct perf_evlist
*evlist __maybe_unused
)
514 struct perf_session
*session
;
517 session
= container_of(h
, struct perf_session
, header
);
519 if (!perf_session__read_build_ids(session
, true))
522 err
= dsos__write_buildid_table(h
, fd
);
524 pr_debug("failed to write buildid table\n");
527 if (!no_buildid_cache
)
528 perf_session__cache_build_ids(session
);
533 static int write_hostname(int fd
, struct perf_header
*h __maybe_unused
,
534 struct perf_evlist
*evlist __maybe_unused
)
543 return do_write_string(fd
, uts
.nodename
);
546 static int write_osrelease(int fd
, struct perf_header
*h __maybe_unused
,
547 struct perf_evlist
*evlist __maybe_unused
)
556 return do_write_string(fd
, uts
.release
);
559 static int write_arch(int fd
, struct perf_header
*h __maybe_unused
,
560 struct perf_evlist
*evlist __maybe_unused
)
569 return do_write_string(fd
, uts
.machine
);
572 static int write_version(int fd
, struct perf_header
*h __maybe_unused
,
573 struct perf_evlist
*evlist __maybe_unused
)
575 return do_write_string(fd
, perf_version_string
);
578 static int write_cpudesc(int fd
, struct perf_header
*h __maybe_unused
,
579 struct perf_evlist
*evlist __maybe_unused
)
582 #define CPUINFO_PROC NULL
587 const char *search
= CPUINFO_PROC
;
594 file
= fopen("/proc/cpuinfo", "r");
598 while (getline(&buf
, &len
, file
) > 0) {
599 ret
= strncmp(buf
, search
, strlen(search
));
609 p
= strchr(buf
, ':');
610 if (p
&& *(p
+1) == ' ' && *(p
+2))
616 /* squash extra space characters (branding string) */
623 while (*q
&& isspace(*q
))
626 while ((*r
++ = *q
++));
630 ret
= do_write_string(fd
, s
);
637 static int write_nrcpus(int fd
, struct perf_header
*h __maybe_unused
,
638 struct perf_evlist
*evlist __maybe_unused
)
644 nr
= sysconf(_SC_NPROCESSORS_CONF
);
648 nrc
= (u32
)(nr
& UINT_MAX
);
650 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
654 nra
= (u32
)(nr
& UINT_MAX
);
656 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
660 return do_write(fd
, &nra
, sizeof(nra
));
663 static int write_event_desc(int fd
, struct perf_header
*h __maybe_unused
,
664 struct perf_evlist
*evlist
)
666 struct perf_evsel
*evsel
;
670 nre
= evlist
->nr_entries
;
673 * write number of events
675 ret
= do_write(fd
, &nre
, sizeof(nre
));
680 * size of perf_event_attr struct
682 sz
= (u32
)sizeof(evsel
->attr
);
683 ret
= do_write(fd
, &sz
, sizeof(sz
));
687 evlist__for_each(evlist
, evsel
) {
688 ret
= do_write(fd
, &evsel
->attr
, sz
);
692 * write number of unique id per event
693 * there is one id per instance of an event
695 * copy into an nri to be independent of the
699 ret
= do_write(fd
, &nri
, sizeof(nri
));
704 * write event string as passed on cmdline
706 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
710 * write unique ids for this event
712 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
719 static int write_cmdline(int fd
, struct perf_header
*h __maybe_unused
,
720 struct perf_evlist
*evlist __maybe_unused
)
722 char buf
[MAXPATHLEN
];
728 * actual atual path to perf binary
730 sprintf(proc
, "/proc/%d/exe", getpid());
731 ret
= readlink(proc
, buf
, sizeof(buf
));
735 /* readlink() does not add null termination */
738 /* account for binary path */
741 ret
= do_write(fd
, &n
, sizeof(n
));
745 ret
= do_write_string(fd
, buf
);
749 for (i
= 0 ; i
< header_argc
; i
++) {
750 ret
= do_write_string(fd
, header_argv
[i
]);
757 #define CORE_SIB_FMT \
758 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
759 #define THRD_SIB_FMT \
760 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
765 char **core_siblings
;
766 char **thread_siblings
;
769 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
772 char filename
[MAXPATHLEN
];
773 char *buf
= NULL
, *p
;
779 sprintf(filename
, CORE_SIB_FMT
, cpu
);
780 fp
= fopen(filename
, "r");
784 sret
= getline(&buf
, &len
, fp
);
789 p
= strchr(buf
, '\n');
793 for (i
= 0; i
< tp
->core_sib
; i
++) {
794 if (!strcmp(buf
, tp
->core_siblings
[i
]))
797 if (i
== tp
->core_sib
) {
798 tp
->core_siblings
[i
] = buf
;
806 sprintf(filename
, THRD_SIB_FMT
, cpu
);
807 fp
= fopen(filename
, "r");
811 if (getline(&buf
, &len
, fp
) <= 0)
814 p
= strchr(buf
, '\n');
818 for (i
= 0; i
< tp
->thread_sib
; i
++) {
819 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
822 if (i
== tp
->thread_sib
) {
823 tp
->thread_siblings
[i
] = buf
;
835 static void free_cpu_topo(struct cpu_topo
*tp
)
842 for (i
= 0 ; i
< tp
->core_sib
; i
++)
843 zfree(&tp
->core_siblings
[i
]);
845 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
846 zfree(&tp
->thread_siblings
[i
]);
851 static struct cpu_topo
*build_cpu_topology(void)
860 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
864 nr
= (u32
)(ncpus
& UINT_MAX
);
866 sz
= nr
* sizeof(char *);
868 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
875 tp
->core_siblings
= addr
;
877 tp
->thread_siblings
= addr
;
879 for (i
= 0; i
< nr
; i
++) {
880 ret
= build_cpu_topo(tp
, i
);
891 static int write_cpu_topology(int fd
, struct perf_header
*h __maybe_unused
,
892 struct perf_evlist
*evlist __maybe_unused
)
898 tp
= build_cpu_topology();
902 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
906 for (i
= 0; i
< tp
->core_sib
; i
++) {
907 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
911 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
915 for (i
= 0; i
< tp
->thread_sib
; i
++) {
916 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
927 static int write_total_mem(int fd
, struct perf_header
*h __maybe_unused
,
928 struct perf_evlist
*evlist __maybe_unused
)
936 fp
= fopen("/proc/meminfo", "r");
940 while (getline(&buf
, &len
, fp
) > 0) {
941 ret
= strncmp(buf
, "MemTotal:", 9);
946 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
948 ret
= do_write(fd
, &mem
, sizeof(mem
));
955 static int write_topo_node(int fd
, int node
)
957 char str
[MAXPATHLEN
];
959 char *buf
= NULL
, *p
;
962 u64 mem_total
, mem_free
, mem
;
965 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
966 fp
= fopen(str
, "r");
970 while (getline(&buf
, &len
, fp
) > 0) {
971 /* skip over invalid lines */
972 if (!strchr(buf
, ':'))
974 if (sscanf(buf
, "%*s %*d %31s %"PRIu64
, field
, &mem
) != 2)
976 if (!strcmp(field
, "MemTotal:"))
978 if (!strcmp(field
, "MemFree:"))
985 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
989 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
994 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
996 fp
= fopen(str
, "r");
1000 if (getline(&buf
, &len
, fp
) <= 0)
1003 p
= strchr(buf
, '\n');
1007 ret
= do_write_string(fd
, buf
);
1015 static int write_numa_topology(int fd
, struct perf_header
*h __maybe_unused
,
1016 struct perf_evlist
*evlist __maybe_unused
)
1021 struct cpu_map
*node_map
= NULL
;
1026 fp
= fopen("/sys/devices/system/node/online", "r");
1030 if (getline(&buf
, &len
, fp
) <= 0)
1033 c
= strchr(buf
, '\n');
1037 node_map
= cpu_map__new(buf
);
1041 nr
= (u32
)node_map
->nr
;
1043 ret
= do_write(fd
, &nr
, sizeof(nr
));
1047 for (i
= 0; i
< nr
; i
++) {
1048 j
= (u32
)node_map
->map
[i
];
1049 ret
= do_write(fd
, &j
, sizeof(j
));
1053 ret
= write_topo_node(fd
, i
);
1067 * struct pmu_mappings {
1076 static int write_pmu_mappings(int fd
, struct perf_header
*h __maybe_unused
,
1077 struct perf_evlist
*evlist __maybe_unused
)
1079 struct perf_pmu
*pmu
= NULL
;
1080 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
1084 /* write real pmu_num later */
1085 ret
= do_write(fd
, &pmu_num
, sizeof(pmu_num
));
1089 while ((pmu
= perf_pmu__scan(pmu
))) {
1094 ret
= do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
1098 ret
= do_write_string(fd
, pmu
->name
);
1103 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
1105 lseek(fd
, offset
, SEEK_SET
);
1115 * struct group_descs {
1117 * struct group_desc {
1124 static int write_group_desc(int fd
, struct perf_header
*h __maybe_unused
,
1125 struct perf_evlist
*evlist
)
1127 u32 nr_groups
= evlist
->nr_groups
;
1128 struct perf_evsel
*evsel
;
1131 ret
= do_write(fd
, &nr_groups
, sizeof(nr_groups
));
1135 evlist__for_each(evlist
, evsel
) {
1136 if (perf_evsel__is_group_leader(evsel
) &&
1137 evsel
->nr_members
> 1) {
1138 const char *name
= evsel
->group_name
?: "{anon_group}";
1139 u32 leader_idx
= evsel
->idx
;
1140 u32 nr_members
= evsel
->nr_members
;
1142 ret
= do_write_string(fd
, name
);
1146 ret
= do_write(fd
, &leader_idx
, sizeof(leader_idx
));
1150 ret
= do_write(fd
, &nr_members
, sizeof(nr_members
));
1159 * default get_cpuid(): nothing gets recorded
1160 * actual implementation must be in arch/$(ARCH)/util/header.c
1162 int __attribute__ ((weak
)) get_cpuid(char *buffer __maybe_unused
,
1163 size_t sz __maybe_unused
)
1168 static int write_cpuid(int fd
, struct perf_header
*h __maybe_unused
,
1169 struct perf_evlist
*evlist __maybe_unused
)
1174 ret
= get_cpuid(buffer
, sizeof(buffer
));
1180 return do_write_string(fd
, buffer
);
1183 static int write_branch_stack(int fd __maybe_unused
,
1184 struct perf_header
*h __maybe_unused
,
1185 struct perf_evlist
*evlist __maybe_unused
)
1190 static void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
1193 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
1196 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
1199 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
1202 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1204 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
1207 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
1210 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
1213 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
1216 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
1217 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
1220 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
1223 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
1226 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
1232 nr
= ph
->env
.nr_cmdline
;
1233 str
= ph
->env
.cmdline
;
1235 fprintf(fp
, "# cmdline : ");
1237 for (i
= 0; i
< nr
; i
++) {
1238 fprintf(fp
, "%s ", str
);
1239 str
+= strlen(str
) + 1;
1244 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1250 nr
= ph
->env
.nr_sibling_cores
;
1251 str
= ph
->env
.sibling_cores
;
1253 for (i
= 0; i
< nr
; i
++) {
1254 fprintf(fp
, "# sibling cores : %s\n", str
);
1255 str
+= strlen(str
) + 1;
1258 nr
= ph
->env
.nr_sibling_threads
;
1259 str
= ph
->env
.sibling_threads
;
1261 for (i
= 0; i
< nr
; i
++) {
1262 fprintf(fp
, "# sibling threads : %s\n", str
);
1263 str
+= strlen(str
) + 1;
1267 static void free_event_desc(struct perf_evsel
*events
)
1269 struct perf_evsel
*evsel
;
1274 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1275 zfree(&evsel
->name
);
1282 static struct perf_evsel
*
1283 read_event_desc(struct perf_header
*ph
, int fd
)
1285 struct perf_evsel
*evsel
, *events
= NULL
;
1288 u32 nre
, sz
, nr
, i
, j
;
1292 /* number of events */
1293 ret
= readn(fd
, &nre
, sizeof(nre
));
1294 if (ret
!= (ssize_t
)sizeof(nre
))
1298 nre
= bswap_32(nre
);
1300 ret
= readn(fd
, &sz
, sizeof(sz
));
1301 if (ret
!= (ssize_t
)sizeof(sz
))
1307 /* buffer to hold on file attr struct */
1312 /* the last event terminates with evsel->attr.size == 0: */
1313 events
= calloc(nre
+ 1, sizeof(*events
));
1317 msz
= sizeof(evsel
->attr
);
1321 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1325 * must read entire on-file attr struct to
1326 * sync up with layout.
1328 ret
= readn(fd
, buf
, sz
);
1329 if (ret
!= (ssize_t
)sz
)
1333 perf_event__attr_swap(buf
);
1335 memcpy(&evsel
->attr
, buf
, msz
);
1337 ret
= readn(fd
, &nr
, sizeof(nr
));
1338 if (ret
!= (ssize_t
)sizeof(nr
))
1341 if (ph
->needs_swap
) {
1343 evsel
->needs_swap
= true;
1346 evsel
->name
= do_read_string(fd
, ph
);
1351 id
= calloc(nr
, sizeof(*id
));
1357 for (j
= 0 ; j
< nr
; j
++) {
1358 ret
= readn(fd
, id
, sizeof(*id
));
1359 if (ret
!= (ssize_t
)sizeof(*id
))
1362 *id
= bswap_64(*id
);
1371 free_event_desc(events
);
1376 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1378 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1383 fprintf(fp
, "# event desc: not available or unable to read\n");
1387 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1388 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1390 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1391 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1393 (u64
)evsel
->attr
.config
,
1394 (u64
)evsel
->attr
.config1
,
1395 (u64
)evsel
->attr
.config2
);
1397 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1398 evsel
->attr
.exclude_user
,
1399 evsel
->attr
.exclude_kernel
);
1401 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1402 evsel
->attr
.exclude_host
,
1403 evsel
->attr
.exclude_guest
);
1405 fprintf(fp
, ", precise_ip = %d", evsel
->attr
.precise_ip
);
1407 fprintf(fp
, ", attr_mmap2 = %d", evsel
->attr
.mmap2
);
1408 fprintf(fp
, ", attr_mmap = %d", evsel
->attr
.mmap
);
1409 fprintf(fp
, ", attr_mmap_data = %d", evsel
->attr
.mmap_data
);
1411 fprintf(fp
, ", id = {");
1412 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1415 fprintf(fp
, " %"PRIu64
, *id
);
1423 free_event_desc(events
);
1426 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1429 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1432 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1437 uint64_t mem_total
, mem_free
;
1440 nr
= ph
->env
.nr_numa_nodes
;
1441 str
= ph
->env
.numa_nodes
;
1443 for (i
= 0; i
< nr
; i
++) {
1445 c
= strtoul(str
, &tmp
, 0);
1450 mem_total
= strtoull(str
, &tmp
, 0);
1455 mem_free
= strtoull(str
, &tmp
, 0);
1459 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1460 " free = %"PRIu64
" kB\n",
1461 c
, mem_total
, mem_free
);
1464 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1466 str
+= strlen(str
) + 1;
1470 fprintf(fp
, "# numa topology : not available\n");
1473 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1475 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1478 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1479 int fd __maybe_unused
, FILE *fp
)
1481 fprintf(fp
, "# contains samples with branch stack\n");
1484 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1487 const char *delimiter
= "# pmu mappings: ";
1492 pmu_num
= ph
->env
.nr_pmu_mappings
;
1494 fprintf(fp
, "# pmu mappings: not available\n");
1498 str
= ph
->env
.pmu_mappings
;
1501 type
= strtoul(str
, &tmp
, 0);
1506 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1509 str
+= strlen(str
) + 1;
1518 fprintf(fp
, "# pmu mappings: unable to read\n");
1521 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1524 struct perf_session
*session
;
1525 struct perf_evsel
*evsel
;
1528 session
= container_of(ph
, struct perf_session
, header
);
1530 evlist__for_each(session
->evlist
, evsel
) {
1531 if (perf_evsel__is_group_leader(evsel
) &&
1532 evsel
->nr_members
> 1) {
1533 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1534 perf_evsel__name(evsel
));
1536 nr
= evsel
->nr_members
- 1;
1538 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1546 static int __event_process_build_id(struct build_id_event
*bev
,
1548 struct perf_session
*session
)
1551 struct list_head
*head
;
1552 struct machine
*machine
;
1555 enum dso_kernel_type dso_type
;
1557 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1561 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1564 case PERF_RECORD_MISC_KERNEL
:
1565 dso_type
= DSO_TYPE_KERNEL
;
1566 head
= &machine
->kernel_dsos
;
1568 case PERF_RECORD_MISC_GUEST_KERNEL
:
1569 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1570 head
= &machine
->kernel_dsos
;
1572 case PERF_RECORD_MISC_USER
:
1573 case PERF_RECORD_MISC_GUEST_USER
:
1574 dso_type
= DSO_TYPE_USER
;
1575 head
= &machine
->user_dsos
;
1581 dso
= __dsos__findnew(head
, filename
);
1583 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1585 dso__set_build_id(dso
, &bev
->build_id
);
1587 if (filename
[0] == '[')
1588 dso
->kernel
= dso_type
;
1590 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1592 pr_debug("build id event received for %s: %s\n",
1593 dso
->long_name
, sbuild_id
);
1601 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1602 int input
, u64 offset
, u64 size
)
1604 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1606 struct perf_event_header header
;
1607 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1610 struct build_id_event bev
;
1611 char filename
[PATH_MAX
];
1612 u64 limit
= offset
+ size
;
1614 while (offset
< limit
) {
1617 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1620 if (header
->needs_swap
)
1621 perf_event_header__bswap(&old_bev
.header
);
1623 len
= old_bev
.header
.size
- sizeof(old_bev
);
1624 if (readn(input
, filename
, len
) != len
)
1627 bev
.header
= old_bev
.header
;
1630 * As the pid is the missing value, we need to fill
1631 * it properly. The header.misc value give us nice hint.
1633 bev
.pid
= HOST_KERNEL_ID
;
1634 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1635 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1636 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1638 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1639 __event_process_build_id(&bev
, filename
, session
);
1641 offset
+= bev
.header
.size
;
1647 static int perf_header__read_build_ids(struct perf_header
*header
,
1648 int input
, u64 offset
, u64 size
)
1650 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1651 struct build_id_event bev
;
1652 char filename
[PATH_MAX
];
1653 u64 limit
= offset
+ size
, orig_offset
= offset
;
1656 while (offset
< limit
) {
1659 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1662 if (header
->needs_swap
)
1663 perf_event_header__bswap(&bev
.header
);
1665 len
= bev
.header
.size
- sizeof(bev
);
1666 if (readn(input
, filename
, len
) != len
)
1669 * The a1645ce1 changeset:
1671 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1673 * Added a field to struct build_id_event that broke the file
1676 * Since the kernel build-id is the first entry, process the
1677 * table using the old format if the well known
1678 * '[kernel.kallsyms]' string for the kernel build-id has the
1679 * first 4 characters chopped off (where the pid_t sits).
1681 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1682 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1684 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1687 __event_process_build_id(&bev
, filename
, session
);
1689 offset
+= bev
.header
.size
;
1696 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1697 struct perf_header
*ph __maybe_unused
,
1700 ssize_t ret
= trace_report(fd
, data
, false);
1701 return ret
< 0 ? -1 : 0;
1704 static int process_build_id(struct perf_file_section
*section
,
1705 struct perf_header
*ph
, int fd
,
1706 void *data __maybe_unused
)
1708 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1709 pr_debug("Failed to read buildids, continuing...\n");
1713 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1714 struct perf_header
*ph
, int fd
,
1715 void *data __maybe_unused
)
1717 ph
->env
.hostname
= do_read_string(fd
, ph
);
1718 return ph
->env
.hostname
? 0 : -ENOMEM
;
1721 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1722 struct perf_header
*ph
, int fd
,
1723 void *data __maybe_unused
)
1725 ph
->env
.os_release
= do_read_string(fd
, ph
);
1726 return ph
->env
.os_release
? 0 : -ENOMEM
;
1729 static int process_version(struct perf_file_section
*section __maybe_unused
,
1730 struct perf_header
*ph
, int fd
,
1731 void *data __maybe_unused
)
1733 ph
->env
.version
= do_read_string(fd
, ph
);
1734 return ph
->env
.version
? 0 : -ENOMEM
;
1737 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1738 struct perf_header
*ph
, int fd
,
1739 void *data __maybe_unused
)
1741 ph
->env
.arch
= do_read_string(fd
, ph
);
1742 return ph
->env
.arch
? 0 : -ENOMEM
;
1745 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1746 struct perf_header
*ph
, int fd
,
1747 void *data __maybe_unused
)
1752 ret
= readn(fd
, &nr
, sizeof(nr
));
1753 if (ret
!= sizeof(nr
))
1759 ph
->env
.nr_cpus_online
= nr
;
1761 ret
= readn(fd
, &nr
, sizeof(nr
));
1762 if (ret
!= sizeof(nr
))
1768 ph
->env
.nr_cpus_avail
= nr
;
1772 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1773 struct perf_header
*ph
, int fd
,
1774 void *data __maybe_unused
)
1776 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1777 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1780 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1781 struct perf_header
*ph
, int fd
,
1782 void *data __maybe_unused
)
1784 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1785 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1788 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1789 struct perf_header
*ph
, int fd
,
1790 void *data __maybe_unused
)
1795 ret
= readn(fd
, &mem
, sizeof(mem
));
1796 if (ret
!= sizeof(mem
))
1800 mem
= bswap_64(mem
);
1802 ph
->env
.total_mem
= mem
;
1806 static struct perf_evsel
*
1807 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1809 struct perf_evsel
*evsel
;
1811 evlist__for_each(evlist
, evsel
) {
1812 if (evsel
->idx
== idx
)
1820 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1821 struct perf_evsel
*event
)
1823 struct perf_evsel
*evsel
;
1828 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1835 evsel
->name
= strdup(event
->name
);
1839 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1840 struct perf_header
*header
, int fd
,
1841 void *data __maybe_unused
)
1843 struct perf_session
*session
;
1844 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1849 session
= container_of(header
, struct perf_session
, header
);
1850 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1851 perf_evlist__set_event_name(session
->evlist
, evsel
);
1853 free_event_desc(events
);
1858 static int process_cmdline(struct perf_file_section
*section __maybe_unused
,
1859 struct perf_header
*ph
, int fd
,
1860 void *data __maybe_unused
)
1867 ret
= readn(fd
, &nr
, sizeof(nr
));
1868 if (ret
!= sizeof(nr
))
1874 ph
->env
.nr_cmdline
= nr
;
1875 strbuf_init(&sb
, 128);
1877 for (i
= 0; i
< nr
; i
++) {
1878 str
= do_read_string(fd
, ph
);
1882 /* include a NULL character at the end */
1883 strbuf_add(&sb
, str
, strlen(str
) + 1);
1886 ph
->env
.cmdline
= strbuf_detach(&sb
, NULL
);
1890 strbuf_release(&sb
);
1894 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1895 struct perf_header
*ph
, int fd
,
1896 void *data __maybe_unused
)
1903 ret
= readn(fd
, &nr
, sizeof(nr
));
1904 if (ret
!= sizeof(nr
))
1910 ph
->env
.nr_sibling_cores
= nr
;
1911 strbuf_init(&sb
, 128);
1913 for (i
= 0; i
< nr
; i
++) {
1914 str
= do_read_string(fd
, ph
);
1918 /* include a NULL character at the end */
1919 strbuf_add(&sb
, str
, strlen(str
) + 1);
1922 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1924 ret
= readn(fd
, &nr
, sizeof(nr
));
1925 if (ret
!= sizeof(nr
))
1931 ph
->env
.nr_sibling_threads
= nr
;
1933 for (i
= 0; i
< nr
; i
++) {
1934 str
= do_read_string(fd
, ph
);
1938 /* include a NULL character at the end */
1939 strbuf_add(&sb
, str
, strlen(str
) + 1);
1942 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1946 strbuf_release(&sb
);
1950 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1951 struct perf_header
*ph
, int fd
,
1952 void *data __maybe_unused
)
1957 uint64_t mem_total
, mem_free
;
1961 ret
= readn(fd
, &nr
, sizeof(nr
));
1962 if (ret
!= sizeof(nr
))
1968 ph
->env
.nr_numa_nodes
= nr
;
1969 strbuf_init(&sb
, 256);
1971 for (i
= 0; i
< nr
; i
++) {
1973 ret
= readn(fd
, &node
, sizeof(node
));
1974 if (ret
!= sizeof(node
))
1977 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1978 if (ret
!= sizeof(u64
))
1981 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1982 if (ret
!= sizeof(u64
))
1985 if (ph
->needs_swap
) {
1986 node
= bswap_32(node
);
1987 mem_total
= bswap_64(mem_total
);
1988 mem_free
= bswap_64(mem_free
);
1991 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1992 node
, mem_total
, mem_free
);
1994 str
= do_read_string(fd
, ph
);
1998 /* include a NULL character at the end */
1999 strbuf_add(&sb
, str
, strlen(str
) + 1);
2002 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
2006 strbuf_release(&sb
);
2010 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
2011 struct perf_header
*ph
, int fd
,
2012 void *data __maybe_unused
)
2020 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
2021 if (ret
!= sizeof(pmu_num
))
2025 pmu_num
= bswap_32(pmu_num
);
2028 pr_debug("pmu mappings not available\n");
2032 ph
->env
.nr_pmu_mappings
= pmu_num
;
2033 strbuf_init(&sb
, 128);
2036 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
2039 type
= bswap_32(type
);
2041 name
= do_read_string(fd
, ph
);
2045 strbuf_addf(&sb
, "%u:%s", type
, name
);
2046 /* include a NULL character at the end */
2047 strbuf_add(&sb
, "", 1);
2052 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2056 strbuf_release(&sb
);
2060 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
2061 struct perf_header
*ph
, int fd
,
2062 void *data __maybe_unused
)
2065 u32 i
, nr
, nr_groups
;
2066 struct perf_session
*session
;
2067 struct perf_evsel
*evsel
, *leader
= NULL
;
2074 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
2078 nr_groups
= bswap_32(nr_groups
);
2080 ph
->env
.nr_groups
= nr_groups
;
2082 pr_debug("group desc not available\n");
2086 desc
= calloc(nr_groups
, sizeof(*desc
));
2090 for (i
= 0; i
< nr_groups
; i
++) {
2091 desc
[i
].name
= do_read_string(fd
, ph
);
2095 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
2098 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
2101 if (ph
->needs_swap
) {
2102 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
2103 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
2108 * Rebuild group relationship based on the group_desc
2110 session
= container_of(ph
, struct perf_session
, header
);
2111 session
->evlist
->nr_groups
= nr_groups
;
2114 evlist__for_each(session
->evlist
, evsel
) {
2115 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2116 evsel
->leader
= evsel
;
2117 /* {anon_group} is a dummy name */
2118 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2119 evsel
->group_name
= desc
[i
].name
;
2120 desc
[i
].name
= NULL
;
2122 evsel
->nr_members
= desc
[i
].nr_members
;
2124 if (i
>= nr_groups
|| nr
> 0) {
2125 pr_debug("invalid group desc\n");
2130 nr
= evsel
->nr_members
- 1;
2133 /* This is a group member */
2134 evsel
->leader
= leader
;
2140 if (i
!= nr_groups
|| nr
!= 0) {
2141 pr_debug("invalid group desc\n");
2147 for (i
= 0; i
< nr_groups
; i
++)
2148 zfree(&desc
[i
].name
);
2154 struct feature_ops
{
2155 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
2156 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
2157 int (*process
)(struct perf_file_section
*section
,
2158 struct perf_header
*h
, int fd
, void *data
);
2163 #define FEAT_OPA(n, func) \
2164 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2165 #define FEAT_OPP(n, func) \
2166 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2167 .process = process_##func }
2168 #define FEAT_OPF(n, func) \
2169 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2170 .process = process_##func, .full_only = true }
2172 /* feature_ops not implemented: */
2173 #define print_tracing_data NULL
2174 #define print_build_id NULL
2176 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2177 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
2178 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
2179 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
2180 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
2181 FEAT_OPP(HEADER_VERSION
, version
),
2182 FEAT_OPP(HEADER_ARCH
, arch
),
2183 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
2184 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
2185 FEAT_OPP(HEADER_CPUID
, cpuid
),
2186 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
2187 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
2188 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
2189 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
2190 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
2191 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
2192 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
2193 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
2196 struct header_print_data
{
2198 bool full
; /* extended list of headers */
2201 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2202 struct perf_header
*ph
,
2203 int feat
, int fd
, void *data
)
2205 struct header_print_data
*hd
= data
;
2207 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2208 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2209 "%d, continuing...\n", section
->offset
, feat
);
2212 if (feat
>= HEADER_LAST_FEATURE
) {
2213 pr_warning("unknown feature %d\n", feat
);
2216 if (!feat_ops
[feat
].print
)
2219 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2220 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
2222 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2223 feat_ops
[feat
].name
);
2228 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2230 struct header_print_data hd
;
2231 struct perf_header
*header
= &session
->header
;
2232 int fd
= perf_data_file__fd(session
->file
);
2236 perf_header__process_sections(header
, fd
, &hd
,
2237 perf_file_section__fprintf_info
);
2241 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
2242 struct perf_file_section
**p
,
2243 struct perf_evlist
*evlist
)
2248 if (perf_header__has_feat(h
, type
)) {
2249 if (!feat_ops
[type
].write
)
2252 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
2254 err
= feat_ops
[type
].write(fd
, h
, evlist
);
2256 pr_debug("failed to write feature %d\n", type
);
2258 /* undo anything written */
2259 lseek(fd
, (*p
)->offset
, SEEK_SET
);
2263 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2269 static int perf_header__adds_write(struct perf_header
*header
,
2270 struct perf_evlist
*evlist
, int fd
)
2273 struct perf_file_section
*feat_sec
, *p
;
2279 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2283 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2284 if (feat_sec
== NULL
)
2287 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2289 sec_start
= header
->feat_offset
;
2290 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2292 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2293 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2294 perf_header__clear_feat(header
, feat
);
2297 lseek(fd
, sec_start
, SEEK_SET
);
2299 * may write more than needed due to dropped feature, but
2300 * this is okay, reader will skip the mising entries
2302 err
= do_write(fd
, feat_sec
, sec_size
);
2304 pr_debug("failed to write feature section\n");
2309 int perf_header__write_pipe(int fd
)
2311 struct perf_pipe_file_header f_header
;
2314 f_header
= (struct perf_pipe_file_header
){
2315 .magic
= PERF_MAGIC
,
2316 .size
= sizeof(f_header
),
2319 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2321 pr_debug("failed to write perf pipe header\n");
2328 int perf_session__write_header(struct perf_session
*session
,
2329 struct perf_evlist
*evlist
,
2330 int fd
, bool at_exit
)
2332 struct perf_file_header f_header
;
2333 struct perf_file_attr f_attr
;
2334 struct perf_header
*header
= &session
->header
;
2335 struct perf_evsel
*evsel
;
2339 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2341 evlist__for_each(session
->evlist
, evsel
) {
2342 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2343 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2345 pr_debug("failed to write perf header\n");
2350 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2352 evlist__for_each(evlist
, evsel
) {
2353 f_attr
= (struct perf_file_attr
){
2354 .attr
= evsel
->attr
,
2356 .offset
= evsel
->id_offset
,
2357 .size
= evsel
->ids
* sizeof(u64
),
2360 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2362 pr_debug("failed to write perf header attribute\n");
2367 if (!header
->data_offset
)
2368 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2369 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2372 err
= perf_header__adds_write(header
, evlist
, fd
);
2377 f_header
= (struct perf_file_header
){
2378 .magic
= PERF_MAGIC
,
2379 .size
= sizeof(f_header
),
2380 .attr_size
= sizeof(f_attr
),
2382 .offset
= attr_offset
,
2383 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2386 .offset
= header
->data_offset
,
2387 .size
= header
->data_size
,
2389 /* event_types is ignored, store zeros */
2392 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2394 lseek(fd
, 0, SEEK_SET
);
2395 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2397 pr_debug("failed to write perf header\n");
2400 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2405 static int perf_header__getbuffer64(struct perf_header
*header
,
2406 int fd
, void *buf
, size_t size
)
2408 if (readn(fd
, buf
, size
) <= 0)
2411 if (header
->needs_swap
)
2412 mem_bswap_64(buf
, size
);
2417 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2419 int (*process
)(struct perf_file_section
*section
,
2420 struct perf_header
*ph
,
2421 int feat
, int fd
, void *data
))
2423 struct perf_file_section
*feat_sec
, *sec
;
2429 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2433 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2437 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2439 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2441 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2445 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2446 err
= process(sec
++, header
, feat
, fd
, data
);
2456 static const int attr_file_abi_sizes
[] = {
2457 [0] = PERF_ATTR_SIZE_VER0
,
2458 [1] = PERF_ATTR_SIZE_VER1
,
2459 [2] = PERF_ATTR_SIZE_VER2
,
2460 [3] = PERF_ATTR_SIZE_VER3
,
2465 * In the legacy file format, the magic number is not used to encode endianness.
2466 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2467 * on ABI revisions, we need to try all combinations for all endianness to
2468 * detect the endianness.
2470 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2472 uint64_t ref_size
, attr_size
;
2475 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2476 ref_size
= attr_file_abi_sizes
[i
]
2477 + sizeof(struct perf_file_section
);
2478 if (hdr_sz
!= ref_size
) {
2479 attr_size
= bswap_64(hdr_sz
);
2480 if (attr_size
!= ref_size
)
2483 ph
->needs_swap
= true;
2485 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2490 /* could not determine endianness */
2494 #define PERF_PIPE_HDR_VER0 16
2496 static const size_t attr_pipe_abi_sizes
[] = {
2497 [0] = PERF_PIPE_HDR_VER0
,
2502 * In the legacy pipe format, there is an implicit assumption that endiannesss
2503 * between host recording the samples, and host parsing the samples is the
2504 * same. This is not always the case given that the pipe output may always be
2505 * redirected into a file and analyzed on a different machine with possibly a
2506 * different endianness and perf_event ABI revsions in the perf tool itself.
2508 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2513 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2514 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2515 attr_size
= bswap_64(hdr_sz
);
2516 if (attr_size
!= hdr_sz
)
2519 ph
->needs_swap
= true;
2521 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2527 bool is_perf_magic(u64 magic
)
2529 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2530 || magic
== __perf_magic2
2531 || magic
== __perf_magic2_sw
)
2537 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2538 bool is_pipe
, struct perf_header
*ph
)
2542 /* check for legacy format */
2543 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2545 ph
->version
= PERF_HEADER_VERSION_1
;
2546 pr_debug("legacy perf.data format\n");
2548 return try_all_pipe_abis(hdr_sz
, ph
);
2550 return try_all_file_abis(hdr_sz
, ph
);
2553 * the new magic number serves two purposes:
2554 * - unique number to identify actual perf.data files
2555 * - encode endianness of file
2558 /* check magic number with one endianness */
2559 if (magic
== __perf_magic2
)
2562 /* check magic number with opposite endianness */
2563 if (magic
!= __perf_magic2_sw
)
2566 ph
->needs_swap
= true;
2567 ph
->version
= PERF_HEADER_VERSION_2
;
2572 int perf_file_header__read(struct perf_file_header
*header
,
2573 struct perf_header
*ph
, int fd
)
2577 lseek(fd
, 0, SEEK_SET
);
2579 ret
= readn(fd
, header
, sizeof(*header
));
2583 if (check_magic_endian(header
->magic
,
2584 header
->attr_size
, false, ph
) < 0) {
2585 pr_debug("magic/endian check failed\n");
2589 if (ph
->needs_swap
) {
2590 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2594 if (header
->size
!= sizeof(*header
)) {
2595 /* Support the previous format */
2596 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2597 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2600 } else if (ph
->needs_swap
) {
2602 * feature bitmap is declared as an array of unsigned longs --
2603 * not good since its size can differ between the host that
2604 * generated the data file and the host analyzing the file.
2606 * We need to handle endianness, but we don't know the size of
2607 * the unsigned long where the file was generated. Take a best
2608 * guess at determining it: try 64-bit swap first (ie., file
2609 * created on a 64-bit host), and check if the hostname feature
2610 * bit is set (this feature bit is forced on as of fbe96f2).
2611 * If the bit is not, undo the 64-bit swap and try a 32-bit
2612 * swap. If the hostname bit is still not set (e.g., older data
2613 * file), punt and fallback to the original behavior --
2614 * clearing all feature bits and setting buildid.
2616 mem_bswap_64(&header
->adds_features
,
2617 BITS_TO_U64(HEADER_FEAT_BITS
));
2619 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2621 mem_bswap_64(&header
->adds_features
,
2622 BITS_TO_U64(HEADER_FEAT_BITS
));
2625 mem_bswap_32(&header
->adds_features
,
2626 BITS_TO_U32(HEADER_FEAT_BITS
));
2629 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2630 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2631 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2635 memcpy(&ph
->adds_features
, &header
->adds_features
,
2636 sizeof(ph
->adds_features
));
2638 ph
->data_offset
= header
->data
.offset
;
2639 ph
->data_size
= header
->data
.size
;
2640 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2644 static int perf_file_section__process(struct perf_file_section
*section
,
2645 struct perf_header
*ph
,
2646 int feat
, int fd
, void *data
)
2648 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2649 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2650 "%d, continuing...\n", section
->offset
, feat
);
2654 if (feat
>= HEADER_LAST_FEATURE
) {
2655 pr_debug("unknown feature %d, continuing...\n", feat
);
2659 if (!feat_ops
[feat
].process
)
2662 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2665 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2666 struct perf_header
*ph
, int fd
,
2671 ret
= readn(fd
, header
, sizeof(*header
));
2675 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2676 pr_debug("endian/magic failed\n");
2681 header
->size
= bswap_64(header
->size
);
2683 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2689 static int perf_header__read_pipe(struct perf_session
*session
)
2691 struct perf_header
*header
= &session
->header
;
2692 struct perf_pipe_file_header f_header
;
2694 if (perf_file_header__read_pipe(&f_header
, header
,
2695 perf_data_file__fd(session
->file
),
2696 session
->repipe
) < 0) {
2697 pr_debug("incompatible file format\n");
2704 static int read_attr(int fd
, struct perf_header
*ph
,
2705 struct perf_file_attr
*f_attr
)
2707 struct perf_event_attr
*attr
= &f_attr
->attr
;
2709 size_t our_sz
= sizeof(f_attr
->attr
);
2712 memset(f_attr
, 0, sizeof(*f_attr
));
2714 /* read minimal guaranteed structure */
2715 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2717 pr_debug("cannot read %d bytes of header attr\n",
2718 PERF_ATTR_SIZE_VER0
);
2722 /* on file perf_event_attr size */
2730 sz
= PERF_ATTR_SIZE_VER0
;
2731 } else if (sz
> our_sz
) {
2732 pr_debug("file uses a more recent and unsupported ABI"
2733 " (%zu bytes extra)\n", sz
- our_sz
);
2736 /* what we have not yet read and that we know about */
2737 left
= sz
- PERF_ATTR_SIZE_VER0
;
2740 ptr
+= PERF_ATTR_SIZE_VER0
;
2742 ret
= readn(fd
, ptr
, left
);
2744 /* read perf_file_section, ids are read in caller */
2745 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2747 return ret
<= 0 ? -1 : 0;
2750 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2751 struct pevent
*pevent
)
2753 struct event_format
*event
;
2756 /* already prepared */
2757 if (evsel
->tp_format
)
2760 if (pevent
== NULL
) {
2761 pr_debug("broken or missing trace data\n");
2765 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2770 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2771 evsel
->name
= strdup(bf
);
2772 if (evsel
->name
== NULL
)
2776 evsel
->tp_format
= event
;
2780 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2781 struct pevent
*pevent
)
2783 struct perf_evsel
*pos
;
2785 evlist__for_each(evlist
, pos
) {
2786 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2787 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2794 int perf_session__read_header(struct perf_session
*session
)
2796 struct perf_data_file
*file
= session
->file
;
2797 struct perf_header
*header
= &session
->header
;
2798 struct perf_file_header f_header
;
2799 struct perf_file_attr f_attr
;
2801 int nr_attrs
, nr_ids
, i
, j
;
2802 int fd
= perf_data_file__fd(file
);
2804 session
->evlist
= perf_evlist__new();
2805 if (session
->evlist
== NULL
)
2808 if (perf_data_file__is_pipe(file
))
2809 return perf_header__read_pipe(session
);
2811 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2815 * Sanity check that perf.data was written cleanly; data size is
2816 * initialized to 0 and updated only if the on_exit function is run.
2817 * If data size is still 0 then the file contains only partial
2818 * information. Just warn user and process it as much as it can.
2820 if (f_header
.data
.size
== 0) {
2821 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2822 "Was the 'perf record' command properly terminated?\n",
2826 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2827 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2829 for (i
= 0; i
< nr_attrs
; i
++) {
2830 struct perf_evsel
*evsel
;
2833 if (read_attr(fd
, header
, &f_attr
) < 0)
2836 if (header
->needs_swap
)
2837 perf_event__attr_swap(&f_attr
.attr
);
2839 tmp
= lseek(fd
, 0, SEEK_CUR
);
2840 evsel
= perf_evsel__new(&f_attr
.attr
);
2843 goto out_delete_evlist
;
2845 evsel
->needs_swap
= header
->needs_swap
;
2847 * Do it before so that if perf_evsel__alloc_id fails, this
2848 * entry gets purged too at perf_evlist__delete().
2850 perf_evlist__add(session
->evlist
, evsel
);
2852 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2854 * We don't have the cpu and thread maps on the header, so
2855 * for allocating the perf_sample_id table we fake 1 cpu and
2856 * hattr->ids threads.
2858 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2859 goto out_delete_evlist
;
2861 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2863 for (j
= 0; j
< nr_ids
; j
++) {
2864 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2867 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2870 lseek(fd
, tmp
, SEEK_SET
);
2873 symbol_conf
.nr_events
= nr_attrs
;
2875 perf_header__process_sections(header
, fd
, &session
->tevent
,
2876 perf_file_section__process
);
2878 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2879 session
->tevent
.pevent
))
2880 goto out_delete_evlist
;
2887 perf_evlist__delete(session
->evlist
);
2888 session
->evlist
= NULL
;
2892 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2893 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2894 perf_event__handler_t process
)
2896 union perf_event
*ev
;
2900 size
= sizeof(struct perf_event_attr
);
2901 size
= PERF_ALIGN(size
, sizeof(u64
));
2902 size
+= sizeof(struct perf_event_header
);
2903 size
+= ids
* sizeof(u64
);
2910 ev
->attr
.attr
= *attr
;
2911 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2913 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2914 ev
->attr
.header
.size
= (u16
)size
;
2916 if (ev
->attr
.header
.size
== size
)
2917 err
= process(tool
, ev
, NULL
, NULL
);
2926 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2927 struct perf_session
*session
,
2928 perf_event__handler_t process
)
2930 struct perf_evsel
*evsel
;
2933 evlist__for_each(session
->evlist
, evsel
) {
2934 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2935 evsel
->id
, process
);
2937 pr_debug("failed to create perf header attribute\n");
2945 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
2946 union perf_event
*event
,
2947 struct perf_evlist
**pevlist
)
2950 struct perf_evsel
*evsel
;
2951 struct perf_evlist
*evlist
= *pevlist
;
2953 if (evlist
== NULL
) {
2954 *pevlist
= evlist
= perf_evlist__new();
2959 evsel
= perf_evsel__new(&event
->attr
.attr
);
2963 perf_evlist__add(evlist
, evsel
);
2965 ids
= event
->header
.size
;
2966 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2967 n_ids
= ids
/ sizeof(u64
);
2969 * We don't have the cpu and thread maps on the header, so
2970 * for allocating the perf_sample_id table we fake 1 cpu and
2971 * hattr->ids threads.
2973 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2976 for (i
= 0; i
< n_ids
; i
++) {
2977 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2980 symbol_conf
.nr_events
= evlist
->nr_entries
;
2985 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
2986 struct perf_evlist
*evlist
,
2987 perf_event__handler_t process
)
2989 union perf_event ev
;
2990 struct tracing_data
*tdata
;
2991 ssize_t size
= 0, aligned_size
= 0, padding
;
2992 int err __maybe_unused
= 0;
2995 * We are going to store the size of the data followed
2996 * by the data contents. Since the fd descriptor is a pipe,
2997 * we cannot seek back to store the size of the data once
2998 * we know it. Instead we:
3000 * - write the tracing data to the temp file
3001 * - get/write the data size to pipe
3002 * - write the tracing data from the temp file
3005 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3009 memset(&ev
, 0, sizeof(ev
));
3011 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3013 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3014 padding
= aligned_size
- size
;
3015 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3016 ev
.tracing_data
.size
= aligned_size
;
3018 process(tool
, &ev
, NULL
, NULL
);
3021 * The put function will copy all the tracing data
3022 * stored in temp file to the pipe.
3024 tracing_data_put(tdata
);
3026 write_padded(fd
, NULL
, 0, padding
);
3028 return aligned_size
;
3031 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
3032 union perf_event
*event
,
3033 struct perf_session
*session
)
3035 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3036 int fd
= perf_data_file__fd(session
->file
);
3037 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
3040 /* setup for reading amidst mmap */
3041 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
3044 size_read
= trace_report(fd
, &session
->tevent
,
3046 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3048 if (readn(fd
, buf
, padding
) < 0) {
3049 pr_err("%s: reading input file", __func__
);
3052 if (session
->repipe
) {
3053 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3054 if (retw
<= 0 || retw
!= padding
) {
3055 pr_err("%s: repiping tracing data padding", __func__
);
3060 if (size_read
+ padding
!= size
) {
3061 pr_err("%s: tracing data size mismatch", __func__
);
3065 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3066 session
->tevent
.pevent
);
3068 return size_read
+ padding
;
3071 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3072 struct dso
*pos
, u16 misc
,
3073 perf_event__handler_t process
,
3074 struct machine
*machine
)
3076 union perf_event ev
;
3083 memset(&ev
, 0, sizeof(ev
));
3085 len
= pos
->long_name_len
+ 1;
3086 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3087 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3088 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3089 ev
.build_id
.header
.misc
= misc
;
3090 ev
.build_id
.pid
= machine
->pid
;
3091 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3092 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3094 err
= process(tool
, &ev
, NULL
, machine
);
3099 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3100 union perf_event
*event
,
3101 struct perf_session
*session
)
3103 __event_process_build_id(&event
->build_id
,
3104 event
->build_id
.filename
,
3109 void disable_buildid_cache(void)
3111 no_buildid_cache
= true;