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"
26 static bool no_buildid_cache
= false;
28 static int trace_event_count
;
29 static struct perf_trace_event_type
*trace_events
;
31 static u32 header_argc
;
32 static const char **header_argv
;
34 int perf_header__push_event(u64 id
, const char *name
)
36 struct perf_trace_event_type
*nevents
;
38 if (strlen(name
) > MAX_EVENT_NAME
)
39 pr_warning("Event %s will be truncated\n", name
);
41 nevents
= realloc(trace_events
, (trace_event_count
+ 1) * sizeof(*trace_events
));
44 trace_events
= nevents
;
46 memset(&trace_events
[trace_event_count
], 0, sizeof(struct perf_trace_event_type
));
47 trace_events
[trace_event_count
].event_id
= id
;
48 strncpy(trace_events
[trace_event_count
].name
, name
, MAX_EVENT_NAME
- 1);
53 char *perf_header__find_event(u64 id
)
56 for (i
= 0 ; i
< trace_event_count
; i
++) {
57 if (trace_events
[i
].event_id
== id
)
58 return trace_events
[i
].name
;
65 * must be a numerical value to let the endianness
66 * determine the memory layout. That way we are able
67 * to detect endianness when reading the perf.data file
70 * we check for legacy (PERFFILE) format.
72 static const char *__perf_magic1
= "PERFFILE";
73 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
74 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
76 #define PERF_MAGIC __perf_magic2
78 struct perf_file_attr
{
79 struct perf_event_attr attr
;
80 struct perf_file_section ids
;
83 void perf_header__set_feat(struct perf_header
*header
, int feat
)
85 set_bit(feat
, header
->adds_features
);
88 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
90 clear_bit(feat
, header
->adds_features
);
93 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
95 return test_bit(feat
, header
->adds_features
);
98 static int do_write(int fd
, const void *buf
, size_t size
)
101 int ret
= write(fd
, buf
, size
);
113 #define NAME_ALIGN 64
115 static int write_padded(int fd
, const void *bf
, size_t count
,
116 size_t count_aligned
)
118 static const char zero_buf
[NAME_ALIGN
];
119 int err
= do_write(fd
, bf
, count
);
122 err
= do_write(fd
, zero_buf
, count_aligned
- count
);
127 static int do_write_string(int fd
, const char *str
)
132 olen
= strlen(str
) + 1;
133 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
135 /* write len, incl. \0 */
136 ret
= do_write(fd
, &len
, sizeof(len
));
140 return write_padded(fd
, str
, olen
, len
);
143 static char *do_read_string(int fd
, struct perf_header
*ph
)
149 sz
= readn(fd
, &len
, sizeof(len
));
150 if (sz
< (ssize_t
)sizeof(len
))
160 ret
= readn(fd
, buf
, len
);
161 if (ret
== (ssize_t
)len
) {
163 * strings are padded by zeroes
164 * thus the actual strlen of buf
165 * may be less than len
175 perf_header__set_cmdline(int argc
, const char **argv
)
180 * If header_argv has already been set, do not override it.
181 * This allows a command to set the cmdline, parse args and
182 * then call another builtin function that implements a
183 * command -- e.g, cmd_kvm calling cmd_record.
188 header_argc
= (u32
)argc
;
190 /* do not include NULL termination */
191 header_argv
= calloc(argc
, sizeof(char *));
196 * must copy argv contents because it gets moved
197 * around during option parsing
199 for (i
= 0; i
< argc
; i
++)
200 header_argv
[i
] = argv
[i
];
205 #define dsos__for_each_with_build_id(pos, head) \
206 list_for_each_entry(pos, head, node) \
207 if (!pos->has_build_id) \
211 static int write_buildid(char *name
, size_t name_len
, u8
*build_id
,
212 pid_t pid
, u16 misc
, int fd
)
215 struct build_id_event b
;
219 len
= PERF_ALIGN(len
, NAME_ALIGN
);
221 memset(&b
, 0, sizeof(b
));
222 memcpy(&b
.build_id
, build_id
, BUILD_ID_SIZE
);
224 b
.header
.misc
= misc
;
225 b
.header
.size
= sizeof(b
) + len
;
227 err
= do_write(fd
, &b
, sizeof(b
));
231 return write_padded(fd
, name
, name_len
+ 1, len
);
234 static int __dsos__write_buildid_table(struct list_head
*head
, pid_t pid
,
239 dsos__for_each_with_build_id(pos
, head
) {
247 if (is_vdso_map(pos
->short_name
)) {
248 name
= (char *) VDSO__MAP_NAME
;
249 name_len
= sizeof(VDSO__MAP_NAME
) + 1;
251 name
= pos
->long_name
;
252 name_len
= pos
->long_name_len
+ 1;
255 err
= write_buildid(name
, name_len
, pos
->build_id
,
264 static int machine__write_buildid_table(struct machine
*machine
, int fd
)
267 u16 kmisc
= PERF_RECORD_MISC_KERNEL
,
268 umisc
= PERF_RECORD_MISC_USER
;
270 if (!machine__is_host(machine
)) {
271 kmisc
= PERF_RECORD_MISC_GUEST_KERNEL
;
272 umisc
= PERF_RECORD_MISC_GUEST_USER
;
275 err
= __dsos__write_buildid_table(&machine
->kernel_dsos
, machine
->pid
,
278 err
= __dsos__write_buildid_table(&machine
->user_dsos
,
279 machine
->pid
, umisc
, fd
);
283 static int dsos__write_buildid_table(struct perf_header
*header
, int fd
)
285 struct perf_session
*session
= container_of(header
,
286 struct perf_session
, header
);
288 int err
= machine__write_buildid_table(&session
->machines
.host
, fd
);
293 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
294 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
295 err
= machine__write_buildid_table(pos
, fd
);
302 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
303 const char *name
, bool is_kallsyms
, bool is_vdso
)
305 const size_t size
= PATH_MAX
;
306 char *realname
, *filename
= zalloc(size
),
307 *linkname
= zalloc(size
), *targetname
;
309 bool slash
= is_kallsyms
|| is_vdso
;
312 if (symbol_conf
.kptr_restrict
) {
313 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
317 realname
= (char *) name
;
319 realname
= realpath(name
, NULL
);
321 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
324 len
= scnprintf(filename
, size
, "%s%s%s",
325 debugdir
, slash
? "/" : "",
326 is_vdso
? VDSO__MAP_NAME
: realname
);
327 if (mkdir_p(filename
, 0755))
330 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
332 if (access(filename
, F_OK
)) {
334 if (copyfile("/proc/kallsyms", filename
))
336 } else if (link(realname
, filename
) && copyfile(name
, filename
))
340 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
341 debugdir
, sbuild_id
);
343 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
346 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
347 targetname
= filename
+ strlen(debugdir
) - 5;
348 memcpy(targetname
, "../..", 5);
350 if (symlink(targetname
, linkname
) == 0)
360 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
361 const char *name
, const char *debugdir
,
362 bool is_kallsyms
, bool is_vdso
)
364 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
366 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
368 return build_id_cache__add_s(sbuild_id
, debugdir
, name
,
369 is_kallsyms
, is_vdso
);
372 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
374 const size_t size
= PATH_MAX
;
375 char *filename
= zalloc(size
),
376 *linkname
= zalloc(size
);
379 if (filename
== NULL
|| linkname
== NULL
)
382 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
383 debugdir
, sbuild_id
, sbuild_id
+ 2);
385 if (access(linkname
, F_OK
))
388 if (readlink(linkname
, filename
, size
- 1) < 0)
391 if (unlink(linkname
))
395 * Since the link is relative, we must make it absolute:
397 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
398 debugdir
, sbuild_id
, filename
);
400 if (unlink(linkname
))
410 static int dso__cache_build_id(struct dso
*dso
, const char *debugdir
)
412 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
413 bool is_vdso
= is_vdso_map(dso
->short_name
);
415 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
),
416 dso
->long_name
, debugdir
,
417 is_kallsyms
, is_vdso
);
420 static int __dsos__cache_build_ids(struct list_head
*head
, const char *debugdir
)
425 dsos__for_each_with_build_id(pos
, head
)
426 if (dso__cache_build_id(pos
, debugdir
))
432 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
434 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
, debugdir
);
435 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
, debugdir
);
439 static int perf_session__cache_build_ids(struct perf_session
*session
)
443 char debugdir
[PATH_MAX
];
445 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
447 if (mkdir(debugdir
, 0755) != 0 && errno
!= EEXIST
)
450 ret
= machine__cache_build_ids(&session
->machines
.host
, debugdir
);
452 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
453 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
454 ret
|= machine__cache_build_ids(pos
, debugdir
);
459 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
461 bool ret
= __dsos__read_build_ids(&machine
->kernel_dsos
, with_hits
);
462 ret
|= __dsos__read_build_ids(&machine
->user_dsos
, with_hits
);
466 static bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
469 bool ret
= machine__read_build_ids(&session
->machines
.host
, with_hits
);
471 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
472 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
473 ret
|= machine__read_build_ids(pos
, with_hits
);
479 static int write_tracing_data(int fd
, struct perf_header
*h __maybe_unused
,
480 struct perf_evlist
*evlist
)
482 return read_tracing_data(fd
, &evlist
->entries
);
486 static int write_build_id(int fd
, struct perf_header
*h
,
487 struct perf_evlist
*evlist __maybe_unused
)
489 struct perf_session
*session
;
492 session
= container_of(h
, struct perf_session
, header
);
494 if (!perf_session__read_build_ids(session
, true))
497 err
= dsos__write_buildid_table(h
, fd
);
499 pr_debug("failed to write buildid table\n");
502 if (!no_buildid_cache
)
503 perf_session__cache_build_ids(session
);
508 static int write_hostname(int fd
, struct perf_header
*h __maybe_unused
,
509 struct perf_evlist
*evlist __maybe_unused
)
518 return do_write_string(fd
, uts
.nodename
);
521 static int write_osrelease(int fd
, struct perf_header
*h __maybe_unused
,
522 struct perf_evlist
*evlist __maybe_unused
)
531 return do_write_string(fd
, uts
.release
);
534 static int write_arch(int fd
, struct perf_header
*h __maybe_unused
,
535 struct perf_evlist
*evlist __maybe_unused
)
544 return do_write_string(fd
, uts
.machine
);
547 static int write_version(int fd
, struct perf_header
*h __maybe_unused
,
548 struct perf_evlist
*evlist __maybe_unused
)
550 return do_write_string(fd
, perf_version_string
);
553 static int write_cpudesc(int fd
, struct perf_header
*h __maybe_unused
,
554 struct perf_evlist
*evlist __maybe_unused
)
557 #define CPUINFO_PROC NULL
562 const char *search
= CPUINFO_PROC
;
569 file
= fopen("/proc/cpuinfo", "r");
573 while (getline(&buf
, &len
, file
) > 0) {
574 ret
= strncmp(buf
, search
, strlen(search
));
584 p
= strchr(buf
, ':');
585 if (p
&& *(p
+1) == ' ' && *(p
+2))
591 /* squash extra space characters (branding string) */
598 while (*q
&& isspace(*q
))
601 while ((*r
++ = *q
++));
605 ret
= do_write_string(fd
, s
);
612 static int write_nrcpus(int fd
, struct perf_header
*h __maybe_unused
,
613 struct perf_evlist
*evlist __maybe_unused
)
619 nr
= sysconf(_SC_NPROCESSORS_CONF
);
623 nrc
= (u32
)(nr
& UINT_MAX
);
625 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
629 nra
= (u32
)(nr
& UINT_MAX
);
631 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
635 return do_write(fd
, &nra
, sizeof(nra
));
638 static int write_event_desc(int fd
, struct perf_header
*h __maybe_unused
,
639 struct perf_evlist
*evlist
)
641 struct perf_evsel
*evsel
;
645 nre
= evlist
->nr_entries
;
648 * write number of events
650 ret
= do_write(fd
, &nre
, sizeof(nre
));
655 * size of perf_event_attr struct
657 sz
= (u32
)sizeof(evsel
->attr
);
658 ret
= do_write(fd
, &sz
, sizeof(sz
));
662 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
664 ret
= do_write(fd
, &evsel
->attr
, sz
);
668 * write number of unique id per event
669 * there is one id per instance of an event
671 * copy into an nri to be independent of the
675 ret
= do_write(fd
, &nri
, sizeof(nri
));
680 * write event string as passed on cmdline
682 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
686 * write unique ids for this event
688 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
695 static int write_cmdline(int fd
, struct perf_header
*h __maybe_unused
,
696 struct perf_evlist
*evlist __maybe_unused
)
698 char buf
[MAXPATHLEN
];
704 * actual atual path to perf binary
706 sprintf(proc
, "/proc/%d/exe", getpid());
707 ret
= readlink(proc
, buf
, sizeof(buf
));
711 /* readlink() does not add null termination */
714 /* account for binary path */
717 ret
= do_write(fd
, &n
, sizeof(n
));
721 ret
= do_write_string(fd
, buf
);
725 for (i
= 0 ; i
< header_argc
; i
++) {
726 ret
= do_write_string(fd
, header_argv
[i
]);
733 #define CORE_SIB_FMT \
734 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
735 #define THRD_SIB_FMT \
736 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
741 char **core_siblings
;
742 char **thread_siblings
;
745 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
748 char filename
[MAXPATHLEN
];
749 char *buf
= NULL
, *p
;
754 sprintf(filename
, CORE_SIB_FMT
, cpu
);
755 fp
= fopen(filename
, "r");
759 if (getline(&buf
, &len
, fp
) <= 0)
764 p
= strchr(buf
, '\n');
768 for (i
= 0; i
< tp
->core_sib
; i
++) {
769 if (!strcmp(buf
, tp
->core_siblings
[i
]))
772 if (i
== tp
->core_sib
) {
773 tp
->core_siblings
[i
] = buf
;
779 sprintf(filename
, THRD_SIB_FMT
, cpu
);
780 fp
= fopen(filename
, "r");
784 if (getline(&buf
, &len
, fp
) <= 0)
787 p
= strchr(buf
, '\n');
791 for (i
= 0; i
< tp
->thread_sib
; i
++) {
792 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
795 if (i
== tp
->thread_sib
) {
796 tp
->thread_siblings
[i
] = buf
;
808 static void free_cpu_topo(struct cpu_topo
*tp
)
815 for (i
= 0 ; i
< tp
->core_sib
; i
++)
816 free(tp
->core_siblings
[i
]);
818 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
819 free(tp
->thread_siblings
[i
]);
824 static struct cpu_topo
*build_cpu_topology(void)
833 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
837 nr
= (u32
)(ncpus
& UINT_MAX
);
839 sz
= nr
* sizeof(char *);
841 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
848 tp
->core_siblings
= addr
;
850 tp
->thread_siblings
= addr
;
852 for (i
= 0; i
< nr
; i
++) {
853 ret
= build_cpu_topo(tp
, i
);
864 static int write_cpu_topology(int fd
, struct perf_header
*h __maybe_unused
,
865 struct perf_evlist
*evlist __maybe_unused
)
871 tp
= build_cpu_topology();
875 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
879 for (i
= 0; i
< tp
->core_sib
; i
++) {
880 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
884 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
888 for (i
= 0; i
< tp
->thread_sib
; i
++) {
889 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
900 static int write_total_mem(int fd
, struct perf_header
*h __maybe_unused
,
901 struct perf_evlist
*evlist __maybe_unused
)
909 fp
= fopen("/proc/meminfo", "r");
913 while (getline(&buf
, &len
, fp
) > 0) {
914 ret
= strncmp(buf
, "MemTotal:", 9);
919 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
921 ret
= do_write(fd
, &mem
, sizeof(mem
));
928 static int write_topo_node(int fd
, int node
)
930 char str
[MAXPATHLEN
];
932 char *buf
= NULL
, *p
;
935 u64 mem_total
, mem_free
, mem
;
938 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
939 fp
= fopen(str
, "r");
943 while (getline(&buf
, &len
, fp
) > 0) {
944 /* skip over invalid lines */
945 if (!strchr(buf
, ':'))
947 if (sscanf(buf
, "%*s %*d %s %"PRIu64
, field
, &mem
) != 2)
949 if (!strcmp(field
, "MemTotal:"))
951 if (!strcmp(field
, "MemFree:"))
958 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
962 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
967 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
969 fp
= fopen(str
, "r");
973 if (getline(&buf
, &len
, fp
) <= 0)
976 p
= strchr(buf
, '\n');
980 ret
= do_write_string(fd
, buf
);
988 static int write_numa_topology(int fd
, struct perf_header
*h __maybe_unused
,
989 struct perf_evlist
*evlist __maybe_unused
)
994 struct cpu_map
*node_map
= NULL
;
999 fp
= fopen("/sys/devices/system/node/online", "r");
1003 if (getline(&buf
, &len
, fp
) <= 0)
1006 c
= strchr(buf
, '\n');
1010 node_map
= cpu_map__new(buf
);
1014 nr
= (u32
)node_map
->nr
;
1016 ret
= do_write(fd
, &nr
, sizeof(nr
));
1020 for (i
= 0; i
< nr
; i
++) {
1021 j
= (u32
)node_map
->map
[i
];
1022 ret
= do_write(fd
, &j
, sizeof(j
));
1026 ret
= write_topo_node(fd
, i
);
1040 * struct pmu_mappings {
1049 static int write_pmu_mappings(int fd
, struct perf_header
*h __maybe_unused
,
1050 struct perf_evlist
*evlist __maybe_unused
)
1052 struct perf_pmu
*pmu
= NULL
;
1053 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
1057 /* write real pmu_num later */
1058 ret
= do_write(fd
, &pmu_num
, sizeof(pmu_num
));
1062 while ((pmu
= perf_pmu__scan(pmu
))) {
1067 ret
= do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
1071 ret
= do_write_string(fd
, pmu
->name
);
1076 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
1078 lseek(fd
, offset
, SEEK_SET
);
1088 * struct group_descs {
1090 * struct group_desc {
1097 static int write_group_desc(int fd
, struct perf_header
*h __maybe_unused
,
1098 struct perf_evlist
*evlist
)
1100 u32 nr_groups
= evlist
->nr_groups
;
1101 struct perf_evsel
*evsel
;
1104 ret
= do_write(fd
, &nr_groups
, sizeof(nr_groups
));
1108 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
1109 if (perf_evsel__is_group_leader(evsel
) &&
1110 evsel
->nr_members
> 1) {
1111 const char *name
= evsel
->group_name
?: "{anon_group}";
1112 u32 leader_idx
= evsel
->idx
;
1113 u32 nr_members
= evsel
->nr_members
;
1115 ret
= do_write_string(fd
, name
);
1119 ret
= do_write(fd
, &leader_idx
, sizeof(leader_idx
));
1123 ret
= do_write(fd
, &nr_members
, sizeof(nr_members
));
1132 * default get_cpuid(): nothing gets recorded
1133 * actual implementation must be in arch/$(ARCH)/util/header.c
1135 int __attribute__ ((weak
)) get_cpuid(char *buffer __maybe_unused
,
1136 size_t sz __maybe_unused
)
1141 static int write_cpuid(int fd
, struct perf_header
*h __maybe_unused
,
1142 struct perf_evlist
*evlist __maybe_unused
)
1147 ret
= get_cpuid(buffer
, sizeof(buffer
));
1153 return do_write_string(fd
, buffer
);
1156 static int write_branch_stack(int fd __maybe_unused
,
1157 struct perf_header
*h __maybe_unused
,
1158 struct perf_evlist
*evlist __maybe_unused
)
1163 static void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
1166 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
1169 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
1172 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
1175 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1177 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
1180 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
1183 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
1186 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
1189 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
1190 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
1193 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
1196 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
1199 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
1205 nr
= ph
->env
.nr_cmdline
;
1206 str
= ph
->env
.cmdline
;
1208 fprintf(fp
, "# cmdline : ");
1210 for (i
= 0; i
< nr
; i
++) {
1211 fprintf(fp
, "%s ", str
);
1212 str
+= strlen(str
) + 1;
1217 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1223 nr
= ph
->env
.nr_sibling_cores
;
1224 str
= ph
->env
.sibling_cores
;
1226 for (i
= 0; i
< nr
; i
++) {
1227 fprintf(fp
, "# sibling cores : %s\n", str
);
1228 str
+= strlen(str
) + 1;
1231 nr
= ph
->env
.nr_sibling_threads
;
1232 str
= ph
->env
.sibling_threads
;
1234 for (i
= 0; i
< nr
; i
++) {
1235 fprintf(fp
, "# sibling threads : %s\n", str
);
1236 str
+= strlen(str
) + 1;
1240 static void free_event_desc(struct perf_evsel
*events
)
1242 struct perf_evsel
*evsel
;
1247 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1257 static struct perf_evsel
*
1258 read_event_desc(struct perf_header
*ph
, int fd
)
1260 struct perf_evsel
*evsel
, *events
= NULL
;
1263 u32 nre
, sz
, nr
, i
, j
;
1267 /* number of events */
1268 ret
= readn(fd
, &nre
, sizeof(nre
));
1269 if (ret
!= (ssize_t
)sizeof(nre
))
1273 nre
= bswap_32(nre
);
1275 ret
= readn(fd
, &sz
, sizeof(sz
));
1276 if (ret
!= (ssize_t
)sizeof(sz
))
1282 /* buffer to hold on file attr struct */
1287 /* the last event terminates with evsel->attr.size == 0: */
1288 events
= calloc(nre
+ 1, sizeof(*events
));
1292 msz
= sizeof(evsel
->attr
);
1296 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1300 * must read entire on-file attr struct to
1301 * sync up with layout.
1303 ret
= readn(fd
, buf
, sz
);
1304 if (ret
!= (ssize_t
)sz
)
1308 perf_event__attr_swap(buf
);
1310 memcpy(&evsel
->attr
, buf
, msz
);
1312 ret
= readn(fd
, &nr
, sizeof(nr
));
1313 if (ret
!= (ssize_t
)sizeof(nr
))
1316 if (ph
->needs_swap
) {
1318 evsel
->needs_swap
= true;
1321 evsel
->name
= do_read_string(fd
, ph
);
1326 id
= calloc(nr
, sizeof(*id
));
1332 for (j
= 0 ; j
< nr
; j
++) {
1333 ret
= readn(fd
, id
, sizeof(*id
));
1334 if (ret
!= (ssize_t
)sizeof(*id
))
1337 *id
= bswap_64(*id
);
1347 free_event_desc(events
);
1352 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1354 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1359 fprintf(fp
, "# event desc: not available or unable to read\n");
1363 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1364 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1366 fprintf(fp
, "type = %d, config = 0x%"PRIx64
1367 ", config1 = 0x%"PRIx64
", config2 = 0x%"PRIx64
,
1369 (u64
)evsel
->attr
.config
,
1370 (u64
)evsel
->attr
.config1
,
1371 (u64
)evsel
->attr
.config2
);
1373 fprintf(fp
, ", excl_usr = %d, excl_kern = %d",
1374 evsel
->attr
.exclude_user
,
1375 evsel
->attr
.exclude_kernel
);
1377 fprintf(fp
, ", excl_host = %d, excl_guest = %d",
1378 evsel
->attr
.exclude_host
,
1379 evsel
->attr
.exclude_guest
);
1381 fprintf(fp
, ", precise_ip = %d", evsel
->attr
.precise_ip
);
1384 fprintf(fp
, ", id = {");
1385 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1388 fprintf(fp
, " %"PRIu64
, *id
);
1396 free_event_desc(events
);
1399 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1402 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1405 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1410 uint64_t mem_total
, mem_free
;
1413 nr
= ph
->env
.nr_numa_nodes
;
1414 str
= ph
->env
.numa_nodes
;
1416 for (i
= 0; i
< nr
; i
++) {
1418 c
= strtoul(str
, &tmp
, 0);
1423 mem_total
= strtoull(str
, &tmp
, 0);
1428 mem_free
= strtoull(str
, &tmp
, 0);
1432 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1433 " free = %"PRIu64
" kB\n",
1434 c
, mem_total
, mem_free
);
1437 fprintf(fp
, "# node%u cpu list : %s\n", c
, str
);
1439 str
+= strlen(str
) + 1;
1443 fprintf(fp
, "# numa topology : not available\n");
1446 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1448 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1451 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1452 int fd __maybe_unused
, FILE *fp
)
1454 fprintf(fp
, "# contains samples with branch stack\n");
1457 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1460 const char *delimiter
= "# pmu mappings: ";
1465 pmu_num
= ph
->env
.nr_pmu_mappings
;
1467 fprintf(fp
, "# pmu mappings: not available\n");
1471 str
= ph
->env
.pmu_mappings
;
1474 type
= strtoul(str
, &tmp
, 0);
1479 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1482 str
+= strlen(str
) + 1;
1491 fprintf(fp
, "# pmu mappings: unable to read\n");
1494 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1497 struct perf_session
*session
;
1498 struct perf_evsel
*evsel
;
1501 session
= container_of(ph
, struct perf_session
, header
);
1503 list_for_each_entry(evsel
, &session
->evlist
->entries
, node
) {
1504 if (perf_evsel__is_group_leader(evsel
) &&
1505 evsel
->nr_members
> 1) {
1506 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1507 perf_evsel__name(evsel
));
1509 nr
= evsel
->nr_members
- 1;
1511 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1519 static int __event_process_build_id(struct build_id_event
*bev
,
1521 struct perf_session
*session
)
1524 struct list_head
*head
;
1525 struct machine
*machine
;
1528 enum dso_kernel_type dso_type
;
1530 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1534 misc
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1537 case PERF_RECORD_MISC_KERNEL
:
1538 dso_type
= DSO_TYPE_KERNEL
;
1539 head
= &machine
->kernel_dsos
;
1541 case PERF_RECORD_MISC_GUEST_KERNEL
:
1542 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1543 head
= &machine
->kernel_dsos
;
1545 case PERF_RECORD_MISC_USER
:
1546 case PERF_RECORD_MISC_GUEST_USER
:
1547 dso_type
= DSO_TYPE_USER
;
1548 head
= &machine
->user_dsos
;
1554 dso
= __dsos__findnew(head
, filename
);
1556 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1558 dso__set_build_id(dso
, &bev
->build_id
);
1560 if (filename
[0] == '[')
1561 dso
->kernel
= dso_type
;
1563 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1565 pr_debug("build id event received for %s: %s\n",
1566 dso
->long_name
, sbuild_id
);
1574 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1575 int input
, u64 offset
, u64 size
)
1577 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1579 struct perf_event_header header
;
1580 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1583 struct build_id_event bev
;
1584 char filename
[PATH_MAX
];
1585 u64 limit
= offset
+ size
;
1587 while (offset
< limit
) {
1590 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1593 if (header
->needs_swap
)
1594 perf_event_header__bswap(&old_bev
.header
);
1596 len
= old_bev
.header
.size
- sizeof(old_bev
);
1597 if (readn(input
, filename
, len
) != len
)
1600 bev
.header
= old_bev
.header
;
1603 * As the pid is the missing value, we need to fill
1604 * it properly. The header.misc value give us nice hint.
1606 bev
.pid
= HOST_KERNEL_ID
;
1607 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1608 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1609 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1611 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1612 __event_process_build_id(&bev
, filename
, session
);
1614 offset
+= bev
.header
.size
;
1620 static int perf_header__read_build_ids(struct perf_header
*header
,
1621 int input
, u64 offset
, u64 size
)
1623 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1624 struct build_id_event bev
;
1625 char filename
[PATH_MAX
];
1626 u64 limit
= offset
+ size
, orig_offset
= offset
;
1629 while (offset
< limit
) {
1632 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1635 if (header
->needs_swap
)
1636 perf_event_header__bswap(&bev
.header
);
1638 len
= bev
.header
.size
- sizeof(bev
);
1639 if (readn(input
, filename
, len
) != len
)
1642 * The a1645ce1 changeset:
1644 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1646 * Added a field to struct build_id_event that broke the file
1649 * Since the kernel build-id is the first entry, process the
1650 * table using the old format if the well known
1651 * '[kernel.kallsyms]' string for the kernel build-id has the
1652 * first 4 characters chopped off (where the pid_t sits).
1654 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1655 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1657 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1660 __event_process_build_id(&bev
, filename
, session
);
1662 offset
+= bev
.header
.size
;
1669 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1670 struct perf_header
*ph __maybe_unused
,
1673 ssize_t ret
= trace_report(fd
, data
, false);
1674 return ret
< 0 ? -1 : 0;
1677 static int process_build_id(struct perf_file_section
*section
,
1678 struct perf_header
*ph
, int fd
,
1679 void *data __maybe_unused
)
1681 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1682 pr_debug("Failed to read buildids, continuing...\n");
1686 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1687 struct perf_header
*ph
, int fd
,
1688 void *data __maybe_unused
)
1690 ph
->env
.hostname
= do_read_string(fd
, ph
);
1691 return ph
->env
.hostname
? 0 : -ENOMEM
;
1694 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1695 struct perf_header
*ph
, int fd
,
1696 void *data __maybe_unused
)
1698 ph
->env
.os_release
= do_read_string(fd
, ph
);
1699 return ph
->env
.os_release
? 0 : -ENOMEM
;
1702 static int process_version(struct perf_file_section
*section __maybe_unused
,
1703 struct perf_header
*ph
, int fd
,
1704 void *data __maybe_unused
)
1706 ph
->env
.version
= do_read_string(fd
, ph
);
1707 return ph
->env
.version
? 0 : -ENOMEM
;
1710 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1711 struct perf_header
*ph
, int fd
,
1712 void *data __maybe_unused
)
1714 ph
->env
.arch
= do_read_string(fd
, ph
);
1715 return ph
->env
.arch
? 0 : -ENOMEM
;
1718 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1719 struct perf_header
*ph
, int fd
,
1720 void *data __maybe_unused
)
1725 ret
= readn(fd
, &nr
, sizeof(nr
));
1726 if (ret
!= sizeof(nr
))
1732 ph
->env
.nr_cpus_avail
= nr
;
1734 ret
= readn(fd
, &nr
, sizeof(nr
));
1735 if (ret
!= sizeof(nr
))
1741 ph
->env
.nr_cpus_online
= nr
;
1745 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1746 struct perf_header
*ph
, int fd
,
1747 void *data __maybe_unused
)
1749 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1750 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1753 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1754 struct perf_header
*ph
, int fd
,
1755 void *data __maybe_unused
)
1757 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1758 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1761 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1762 struct perf_header
*ph
, int fd
,
1763 void *data __maybe_unused
)
1768 ret
= readn(fd
, &mem
, sizeof(mem
));
1769 if (ret
!= sizeof(mem
))
1773 mem
= bswap_64(mem
);
1775 ph
->env
.total_mem
= mem
;
1779 static struct perf_evsel
*
1780 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1782 struct perf_evsel
*evsel
;
1784 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
1785 if (evsel
->idx
== idx
)
1793 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1794 struct perf_evsel
*event
)
1796 struct perf_evsel
*evsel
;
1801 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1808 evsel
->name
= strdup(event
->name
);
1812 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1813 struct perf_header
*header
, int fd
,
1814 void *data __maybe_unused
)
1816 struct perf_session
*session
;
1817 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1822 session
= container_of(header
, struct perf_session
, header
);
1823 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1824 perf_evlist__set_event_name(session
->evlist
, evsel
);
1826 free_event_desc(events
);
1831 static int process_cmdline(struct perf_file_section
*section __maybe_unused
,
1832 struct perf_header
*ph
, int fd
,
1833 void *data __maybe_unused
)
1840 ret
= readn(fd
, &nr
, sizeof(nr
));
1841 if (ret
!= sizeof(nr
))
1847 ph
->env
.nr_cmdline
= nr
;
1848 strbuf_init(&sb
, 128);
1850 for (i
= 0; i
< nr
; i
++) {
1851 str
= do_read_string(fd
, ph
);
1855 /* include a NULL character at the end */
1856 strbuf_add(&sb
, str
, strlen(str
) + 1);
1859 ph
->env
.cmdline
= strbuf_detach(&sb
, NULL
);
1863 strbuf_release(&sb
);
1867 static int process_cpu_topology(struct perf_file_section
*section __maybe_unused
,
1868 struct perf_header
*ph
, int fd
,
1869 void *data __maybe_unused
)
1876 ret
= readn(fd
, &nr
, sizeof(nr
));
1877 if (ret
!= sizeof(nr
))
1883 ph
->env
.nr_sibling_cores
= nr
;
1884 strbuf_init(&sb
, 128);
1886 for (i
= 0; i
< nr
; i
++) {
1887 str
= do_read_string(fd
, ph
);
1891 /* include a NULL character at the end */
1892 strbuf_add(&sb
, str
, strlen(str
) + 1);
1895 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1897 ret
= readn(fd
, &nr
, sizeof(nr
));
1898 if (ret
!= sizeof(nr
))
1904 ph
->env
.nr_sibling_threads
= nr
;
1906 for (i
= 0; i
< nr
; i
++) {
1907 str
= do_read_string(fd
, ph
);
1911 /* include a NULL character at the end */
1912 strbuf_add(&sb
, str
, strlen(str
) + 1);
1915 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1919 strbuf_release(&sb
);
1923 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1924 struct perf_header
*ph
, int fd
,
1925 void *data __maybe_unused
)
1930 uint64_t mem_total
, mem_free
;
1934 ret
= readn(fd
, &nr
, sizeof(nr
));
1935 if (ret
!= sizeof(nr
))
1941 ph
->env
.nr_numa_nodes
= nr
;
1942 strbuf_init(&sb
, 256);
1944 for (i
= 0; i
< nr
; i
++) {
1946 ret
= readn(fd
, &node
, sizeof(node
));
1947 if (ret
!= sizeof(node
))
1950 ret
= readn(fd
, &mem_total
, sizeof(u64
));
1951 if (ret
!= sizeof(u64
))
1954 ret
= readn(fd
, &mem_free
, sizeof(u64
));
1955 if (ret
!= sizeof(u64
))
1958 if (ph
->needs_swap
) {
1959 node
= bswap_32(node
);
1960 mem_total
= bswap_64(mem_total
);
1961 mem_free
= bswap_64(mem_free
);
1964 strbuf_addf(&sb
, "%u:%"PRIu64
":%"PRIu64
":",
1965 node
, mem_total
, mem_free
);
1967 str
= do_read_string(fd
, ph
);
1971 /* include a NULL character at the end */
1972 strbuf_add(&sb
, str
, strlen(str
) + 1);
1975 ph
->env
.numa_nodes
= strbuf_detach(&sb
, NULL
);
1979 strbuf_release(&sb
);
1983 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1984 struct perf_header
*ph
, int fd
,
1985 void *data __maybe_unused
)
1993 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1994 if (ret
!= sizeof(pmu_num
))
1998 pmu_num
= bswap_32(pmu_num
);
2001 pr_debug("pmu mappings not available\n");
2005 ph
->env
.nr_pmu_mappings
= pmu_num
;
2006 strbuf_init(&sb
, 128);
2009 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
2012 type
= bswap_32(type
);
2014 name
= do_read_string(fd
, ph
);
2018 strbuf_addf(&sb
, "%u:%s", type
, name
);
2019 /* include a NULL character at the end */
2020 strbuf_add(&sb
, "", 1);
2025 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2029 strbuf_release(&sb
);
2033 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
2034 struct perf_header
*ph
, int fd
,
2035 void *data __maybe_unused
)
2038 u32 i
, nr
, nr_groups
;
2039 struct perf_session
*session
;
2040 struct perf_evsel
*evsel
, *leader
= NULL
;
2047 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
2051 nr_groups
= bswap_32(nr_groups
);
2053 ph
->env
.nr_groups
= nr_groups
;
2055 pr_debug("group desc not available\n");
2059 desc
= calloc(nr_groups
, sizeof(*desc
));
2063 for (i
= 0; i
< nr_groups
; i
++) {
2064 desc
[i
].name
= do_read_string(fd
, ph
);
2068 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
2071 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
2074 if (ph
->needs_swap
) {
2075 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
2076 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
2081 * Rebuild group relationship based on the group_desc
2083 session
= container_of(ph
, struct perf_session
, header
);
2084 session
->evlist
->nr_groups
= nr_groups
;
2087 list_for_each_entry(evsel
, &session
->evlist
->entries
, node
) {
2088 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2089 evsel
->leader
= evsel
;
2090 /* {anon_group} is a dummy name */
2091 if (strcmp(desc
[i
].name
, "{anon_group}"))
2092 evsel
->group_name
= desc
[i
].name
;
2093 evsel
->nr_members
= desc
[i
].nr_members
;
2095 if (i
>= nr_groups
|| nr
> 0) {
2096 pr_debug("invalid group desc\n");
2101 nr
= evsel
->nr_members
- 1;
2104 /* This is a group member */
2105 evsel
->leader
= leader
;
2111 if (i
!= nr_groups
|| nr
!= 0) {
2112 pr_debug("invalid group desc\n");
2118 while ((int) --i
>= 0)
2125 struct feature_ops
{
2126 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
2127 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
2128 int (*process
)(struct perf_file_section
*section
,
2129 struct perf_header
*h
, int fd
, void *data
);
2134 #define FEAT_OPA(n, func) \
2135 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2136 #define FEAT_OPP(n, func) \
2137 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2138 .process = process_##func }
2139 #define FEAT_OPF(n, func) \
2140 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2141 .process = process_##func, .full_only = true }
2143 /* feature_ops not implemented: */
2144 #define print_tracing_data NULL
2145 #define print_build_id NULL
2147 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2148 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
2149 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
2150 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
2151 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
2152 FEAT_OPP(HEADER_VERSION
, version
),
2153 FEAT_OPP(HEADER_ARCH
, arch
),
2154 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
2155 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
2156 FEAT_OPP(HEADER_CPUID
, cpuid
),
2157 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
2158 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
2159 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
2160 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
2161 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
2162 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
2163 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
2164 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
2167 struct header_print_data
{
2169 bool full
; /* extended list of headers */
2172 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2173 struct perf_header
*ph
,
2174 int feat
, int fd
, void *data
)
2176 struct header_print_data
*hd
= data
;
2178 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2179 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2180 "%d, continuing...\n", section
->offset
, feat
);
2183 if (feat
>= HEADER_LAST_FEATURE
) {
2184 pr_warning("unknown feature %d\n", feat
);
2187 if (!feat_ops
[feat
].print
)
2190 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2191 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
2193 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2194 feat_ops
[feat
].name
);
2199 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2201 struct header_print_data hd
;
2202 struct perf_header
*header
= &session
->header
;
2203 int fd
= session
->fd
;
2207 perf_header__process_sections(header
, fd
, &hd
,
2208 perf_file_section__fprintf_info
);
2212 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
2213 struct perf_file_section
**p
,
2214 struct perf_evlist
*evlist
)
2219 if (perf_header__has_feat(h
, type
)) {
2220 if (!feat_ops
[type
].write
)
2223 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
2225 err
= feat_ops
[type
].write(fd
, h
, evlist
);
2227 pr_debug("failed to write feature %d\n", type
);
2229 /* undo anything written */
2230 lseek(fd
, (*p
)->offset
, SEEK_SET
);
2234 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2240 static int perf_header__adds_write(struct perf_header
*header
,
2241 struct perf_evlist
*evlist
, int fd
)
2244 struct perf_file_section
*feat_sec
, *p
;
2250 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2254 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2255 if (feat_sec
== NULL
)
2258 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2260 sec_start
= header
->data_offset
+ header
->data_size
;
2261 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2263 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2264 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2265 perf_header__clear_feat(header
, feat
);
2268 lseek(fd
, sec_start
, SEEK_SET
);
2270 * may write more than needed due to dropped feature, but
2271 * this is okay, reader will skip the mising entries
2273 err
= do_write(fd
, feat_sec
, sec_size
);
2275 pr_debug("failed to write feature section\n");
2280 int perf_header__write_pipe(int fd
)
2282 struct perf_pipe_file_header f_header
;
2285 f_header
= (struct perf_pipe_file_header
){
2286 .magic
= PERF_MAGIC
,
2287 .size
= sizeof(f_header
),
2290 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2292 pr_debug("failed to write perf pipe header\n");
2299 int perf_session__write_header(struct perf_session
*session
,
2300 struct perf_evlist
*evlist
,
2301 int fd
, bool at_exit
)
2303 struct perf_file_header f_header
;
2304 struct perf_file_attr f_attr
;
2305 struct perf_header
*header
= &session
->header
;
2306 struct perf_evsel
*evsel
, *pair
= NULL
;
2309 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2311 if (session
->evlist
!= evlist
)
2312 pair
= perf_evlist__first(session
->evlist
);
2314 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
2315 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2316 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2319 pr_debug("failed to write perf header\n");
2322 if (session
->evlist
!= evlist
) {
2323 err
= do_write(fd
, pair
->id
, pair
->ids
* sizeof(u64
));
2326 evsel
->ids
+= pair
->ids
;
2327 pair
= perf_evsel__next(pair
);
2331 header
->attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2333 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
2334 f_attr
= (struct perf_file_attr
){
2335 .attr
= evsel
->attr
,
2337 .offset
= evsel
->id_offset
,
2338 .size
= evsel
->ids
* sizeof(u64
),
2341 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2343 pr_debug("failed to write perf header attribute\n");
2348 header
->event_offset
= lseek(fd
, 0, SEEK_CUR
);
2349 header
->event_size
= trace_event_count
* sizeof(struct perf_trace_event_type
);
2351 err
= do_write(fd
, trace_events
, header
->event_size
);
2353 pr_debug("failed to write perf header events\n");
2358 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2361 err
= perf_header__adds_write(header
, evlist
, fd
);
2366 f_header
= (struct perf_file_header
){
2367 .magic
= PERF_MAGIC
,
2368 .size
= sizeof(f_header
),
2369 .attr_size
= sizeof(f_attr
),
2371 .offset
= header
->attr_offset
,
2372 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2375 .offset
= header
->data_offset
,
2376 .size
= header
->data_size
,
2379 .offset
= header
->event_offset
,
2380 .size
= header
->event_size
,
2384 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2386 lseek(fd
, 0, SEEK_SET
);
2387 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2389 pr_debug("failed to write perf header\n");
2392 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2398 static int perf_header__getbuffer64(struct perf_header
*header
,
2399 int fd
, void *buf
, size_t size
)
2401 if (readn(fd
, buf
, size
) <= 0)
2404 if (header
->needs_swap
)
2405 mem_bswap_64(buf
, size
);
2410 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2412 int (*process
)(struct perf_file_section
*section
,
2413 struct perf_header
*ph
,
2414 int feat
, int fd
, void *data
))
2416 struct perf_file_section
*feat_sec
, *sec
;
2422 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2426 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2430 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2432 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2434 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2438 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2439 err
= process(sec
++, header
, feat
, fd
, data
);
2449 static const int attr_file_abi_sizes
[] = {
2450 [0] = PERF_ATTR_SIZE_VER0
,
2451 [1] = PERF_ATTR_SIZE_VER1
,
2452 [2] = PERF_ATTR_SIZE_VER2
,
2453 [3] = PERF_ATTR_SIZE_VER3
,
2458 * In the legacy file format, the magic number is not used to encode endianness.
2459 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2460 * on ABI revisions, we need to try all combinations for all endianness to
2461 * detect the endianness.
2463 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2465 uint64_t ref_size
, attr_size
;
2468 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2469 ref_size
= attr_file_abi_sizes
[i
]
2470 + sizeof(struct perf_file_section
);
2471 if (hdr_sz
!= ref_size
) {
2472 attr_size
= bswap_64(hdr_sz
);
2473 if (attr_size
!= ref_size
)
2476 ph
->needs_swap
= true;
2478 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2483 /* could not determine endianness */
2487 #define PERF_PIPE_HDR_VER0 16
2489 static const size_t attr_pipe_abi_sizes
[] = {
2490 [0] = PERF_PIPE_HDR_VER0
,
2495 * In the legacy pipe format, there is an implicit assumption that endiannesss
2496 * between host recording the samples, and host parsing the samples is the
2497 * same. This is not always the case given that the pipe output may always be
2498 * redirected into a file and analyzed on a different machine with possibly a
2499 * different endianness and perf_event ABI revsions in the perf tool itself.
2501 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2506 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2507 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2508 attr_size
= bswap_64(hdr_sz
);
2509 if (attr_size
!= hdr_sz
)
2512 ph
->needs_swap
= true;
2514 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2520 bool is_perf_magic(u64 magic
)
2522 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2523 || magic
== __perf_magic2
2524 || magic
== __perf_magic2_sw
)
2530 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2531 bool is_pipe
, struct perf_header
*ph
)
2535 /* check for legacy format */
2536 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2538 pr_debug("legacy perf.data format\n");
2540 return try_all_pipe_abis(hdr_sz
, ph
);
2542 return try_all_file_abis(hdr_sz
, ph
);
2545 * the new magic number serves two purposes:
2546 * - unique number to identify actual perf.data files
2547 * - encode endianness of file
2550 /* check magic number with one endianness */
2551 if (magic
== __perf_magic2
)
2554 /* check magic number with opposite endianness */
2555 if (magic
!= __perf_magic2_sw
)
2558 ph
->needs_swap
= true;
2563 int perf_file_header__read(struct perf_file_header
*header
,
2564 struct perf_header
*ph
, int fd
)
2568 lseek(fd
, 0, SEEK_SET
);
2570 ret
= readn(fd
, header
, sizeof(*header
));
2574 if (check_magic_endian(header
->magic
,
2575 header
->attr_size
, false, ph
) < 0) {
2576 pr_debug("magic/endian check failed\n");
2580 if (ph
->needs_swap
) {
2581 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2585 if (header
->size
!= sizeof(*header
)) {
2586 /* Support the previous format */
2587 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2588 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2591 } else if (ph
->needs_swap
) {
2593 * feature bitmap is declared as an array of unsigned longs --
2594 * not good since its size can differ between the host that
2595 * generated the data file and the host analyzing the file.
2597 * We need to handle endianness, but we don't know the size of
2598 * the unsigned long where the file was generated. Take a best
2599 * guess at determining it: try 64-bit swap first (ie., file
2600 * created on a 64-bit host), and check if the hostname feature
2601 * bit is set (this feature bit is forced on as of fbe96f2).
2602 * If the bit is not, undo the 64-bit swap and try a 32-bit
2603 * swap. If the hostname bit is still not set (e.g., older data
2604 * file), punt and fallback to the original behavior --
2605 * clearing all feature bits and setting buildid.
2607 mem_bswap_64(&header
->adds_features
,
2608 BITS_TO_U64(HEADER_FEAT_BITS
));
2610 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2612 mem_bswap_64(&header
->adds_features
,
2613 BITS_TO_U64(HEADER_FEAT_BITS
));
2616 mem_bswap_32(&header
->adds_features
,
2617 BITS_TO_U32(HEADER_FEAT_BITS
));
2620 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2621 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2622 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2626 memcpy(&ph
->adds_features
, &header
->adds_features
,
2627 sizeof(ph
->adds_features
));
2629 ph
->event_offset
= header
->event_types
.offset
;
2630 ph
->event_size
= header
->event_types
.size
;
2631 ph
->data_offset
= header
->data
.offset
;
2632 ph
->data_size
= header
->data
.size
;
2636 static int perf_file_section__process(struct perf_file_section
*section
,
2637 struct perf_header
*ph
,
2638 int feat
, int fd
, void *data
)
2640 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2641 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2642 "%d, continuing...\n", section
->offset
, feat
);
2646 if (feat
>= HEADER_LAST_FEATURE
) {
2647 pr_debug("unknown feature %d, continuing...\n", feat
);
2651 if (!feat_ops
[feat
].process
)
2654 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2657 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2658 struct perf_header
*ph
, int fd
,
2663 ret
= readn(fd
, header
, sizeof(*header
));
2667 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2668 pr_debug("endian/magic failed\n");
2673 header
->size
= bswap_64(header
->size
);
2675 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2681 static int perf_header__read_pipe(struct perf_session
*session
, int fd
)
2683 struct perf_header
*header
= &session
->header
;
2684 struct perf_pipe_file_header f_header
;
2686 if (perf_file_header__read_pipe(&f_header
, header
, fd
,
2687 session
->repipe
) < 0) {
2688 pr_debug("incompatible file format\n");
2697 static int read_attr(int fd
, struct perf_header
*ph
,
2698 struct perf_file_attr
*f_attr
)
2700 struct perf_event_attr
*attr
= &f_attr
->attr
;
2702 size_t our_sz
= sizeof(f_attr
->attr
);
2705 memset(f_attr
, 0, sizeof(*f_attr
));
2707 /* read minimal guaranteed structure */
2708 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2710 pr_debug("cannot read %d bytes of header attr\n",
2711 PERF_ATTR_SIZE_VER0
);
2715 /* on file perf_event_attr size */
2723 sz
= PERF_ATTR_SIZE_VER0
;
2724 } else if (sz
> our_sz
) {
2725 pr_debug("file uses a more recent and unsupported ABI"
2726 " (%zu bytes extra)\n", sz
- our_sz
);
2729 /* what we have not yet read and that we know about */
2730 left
= sz
- PERF_ATTR_SIZE_VER0
;
2733 ptr
+= PERF_ATTR_SIZE_VER0
;
2735 ret
= readn(fd
, ptr
, left
);
2737 /* read perf_file_section, ids are read in caller */
2738 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2740 return ret
<= 0 ? -1 : 0;
2743 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2744 struct pevent
*pevent
)
2746 struct event_format
*event
;
2749 /* already prepared */
2750 if (evsel
->tp_format
)
2753 if (pevent
== NULL
) {
2754 pr_debug("broken or missing trace data\n");
2758 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2763 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2764 evsel
->name
= strdup(bf
);
2765 if (evsel
->name
== NULL
)
2769 evsel
->tp_format
= event
;
2773 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2774 struct pevent
*pevent
)
2776 struct perf_evsel
*pos
;
2778 list_for_each_entry(pos
, &evlist
->entries
, node
) {
2779 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2780 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2787 int perf_session__read_header(struct perf_session
*session
, int fd
)
2789 struct perf_header
*header
= &session
->header
;
2790 struct perf_file_header f_header
;
2791 struct perf_file_attr f_attr
;
2793 int nr_attrs
, nr_ids
, i
, j
;
2795 session
->evlist
= perf_evlist__new();
2796 if (session
->evlist
== NULL
)
2799 if (session
->fd_pipe
)
2800 return perf_header__read_pipe(session
, fd
);
2802 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2805 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2806 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2808 for (i
= 0; i
< nr_attrs
; i
++) {
2809 struct perf_evsel
*evsel
;
2812 if (read_attr(fd
, header
, &f_attr
) < 0)
2815 if (header
->needs_swap
)
2816 perf_event__attr_swap(&f_attr
.attr
);
2818 tmp
= lseek(fd
, 0, SEEK_CUR
);
2819 evsel
= perf_evsel__new(&f_attr
.attr
, i
);
2822 goto out_delete_evlist
;
2824 evsel
->needs_swap
= header
->needs_swap
;
2826 * Do it before so that if perf_evsel__alloc_id fails, this
2827 * entry gets purged too at perf_evlist__delete().
2829 perf_evlist__add(session
->evlist
, evsel
);
2831 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2833 * We don't have the cpu and thread maps on the header, so
2834 * for allocating the perf_sample_id table we fake 1 cpu and
2835 * hattr->ids threads.
2837 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2838 goto out_delete_evlist
;
2840 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2842 for (j
= 0; j
< nr_ids
; j
++) {
2843 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2846 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2849 lseek(fd
, tmp
, SEEK_SET
);
2852 symbol_conf
.nr_events
= nr_attrs
;
2854 if (f_header
.event_types
.size
) {
2855 lseek(fd
, f_header
.event_types
.offset
, SEEK_SET
);
2856 trace_events
= malloc(f_header
.event_types
.size
);
2857 if (trace_events
== NULL
)
2859 if (perf_header__getbuffer64(header
, fd
, trace_events
,
2860 f_header
.event_types
.size
))
2862 trace_event_count
= f_header
.event_types
.size
/ sizeof(struct perf_trace_event_type
);
2865 perf_header__process_sections(header
, fd
, &session
->pevent
,
2866 perf_file_section__process
);
2868 lseek(fd
, header
->data_offset
, SEEK_SET
);
2870 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2872 goto out_delete_evlist
;
2880 perf_evlist__delete(session
->evlist
);
2881 session
->evlist
= NULL
;
2885 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2886 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2887 perf_event__handler_t process
)
2889 union perf_event
*ev
;
2893 size
= sizeof(struct perf_event_attr
);
2894 size
= PERF_ALIGN(size
, sizeof(u64
));
2895 size
+= sizeof(struct perf_event_header
);
2896 size
+= ids
* sizeof(u64
);
2903 ev
->attr
.attr
= *attr
;
2904 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2906 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2907 ev
->attr
.header
.size
= (u16
)size
;
2909 if (ev
->attr
.header
.size
== size
)
2910 err
= process(tool
, ev
, NULL
, NULL
);
2919 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
2920 struct perf_session
*session
,
2921 perf_event__handler_t process
)
2923 struct perf_evsel
*evsel
;
2926 list_for_each_entry(evsel
, &session
->evlist
->entries
, node
) {
2927 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
2928 evsel
->id
, process
);
2930 pr_debug("failed to create perf header attribute\n");
2938 int perf_event__process_attr(union perf_event
*event
,
2939 struct perf_evlist
**pevlist
)
2942 struct perf_evsel
*evsel
;
2943 struct perf_evlist
*evlist
= *pevlist
;
2945 if (evlist
== NULL
) {
2946 *pevlist
= evlist
= perf_evlist__new();
2951 evsel
= perf_evsel__new(&event
->attr
.attr
, evlist
->nr_entries
);
2955 perf_evlist__add(evlist
, evsel
);
2957 ids
= event
->header
.size
;
2958 ids
-= (void *)&event
->attr
.id
- (void *)event
;
2959 n_ids
= ids
/ sizeof(u64
);
2961 * We don't have the cpu and thread maps on the header, so
2962 * for allocating the perf_sample_id table we fake 1 cpu and
2963 * hattr->ids threads.
2965 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
2968 for (i
= 0; i
< n_ids
; i
++) {
2969 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
2975 int perf_event__synthesize_event_type(struct perf_tool
*tool
,
2976 u64 event_id
, char *name
,
2977 perf_event__handler_t process
,
2978 struct machine
*machine
)
2980 union perf_event ev
;
2984 memset(&ev
, 0, sizeof(ev
));
2986 ev
.event_type
.event_type
.event_id
= event_id
;
2987 memset(ev
.event_type
.event_type
.name
, 0, MAX_EVENT_NAME
);
2988 strncpy(ev
.event_type
.event_type
.name
, name
, MAX_EVENT_NAME
- 1);
2990 ev
.event_type
.header
.type
= PERF_RECORD_HEADER_EVENT_TYPE
;
2991 size
= strlen(ev
.event_type
.event_type
.name
);
2992 size
= PERF_ALIGN(size
, sizeof(u64
));
2993 ev
.event_type
.header
.size
= sizeof(ev
.event_type
) -
2994 (sizeof(ev
.event_type
.event_type
.name
) - size
);
2996 err
= process(tool
, &ev
, NULL
, machine
);
3001 int perf_event__synthesize_event_types(struct perf_tool
*tool
,
3002 perf_event__handler_t process
,
3003 struct machine
*machine
)
3005 struct perf_trace_event_type
*type
;
3008 for (i
= 0; i
< trace_event_count
; i
++) {
3009 type
= &trace_events
[i
];
3011 err
= perf_event__synthesize_event_type(tool
, type
->event_id
,
3012 type
->name
, process
,
3015 pr_debug("failed to create perf header event type\n");
3023 int perf_event__process_event_type(struct perf_tool
*tool __maybe_unused
,
3024 union perf_event
*event
)
3026 if (perf_header__push_event(event
->event_type
.event_type
.event_id
,
3027 event
->event_type
.event_type
.name
) < 0)
3033 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
3034 struct perf_evlist
*evlist
,
3035 perf_event__handler_t process
)
3037 union perf_event ev
;
3038 struct tracing_data
*tdata
;
3039 ssize_t size
= 0, aligned_size
= 0, padding
;
3040 int err __maybe_unused
= 0;
3043 * We are going to store the size of the data followed
3044 * by the data contents. Since the fd descriptor is a pipe,
3045 * we cannot seek back to store the size of the data once
3046 * we know it. Instead we:
3048 * - write the tracing data to the temp file
3049 * - get/write the data size to pipe
3050 * - write the tracing data from the temp file
3053 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3057 memset(&ev
, 0, sizeof(ev
));
3059 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3061 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3062 padding
= aligned_size
- size
;
3063 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3064 ev
.tracing_data
.size
= aligned_size
;
3066 process(tool
, &ev
, NULL
, NULL
);
3069 * The put function will copy all the tracing data
3070 * stored in temp file to the pipe.
3072 tracing_data_put(tdata
);
3074 write_padded(fd
, NULL
, 0, padding
);
3076 return aligned_size
;
3079 int perf_event__process_tracing_data(union perf_event
*event
,
3080 struct perf_session
*session
)
3082 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3083 off_t offset
= lseek(session
->fd
, 0, SEEK_CUR
);
3086 /* setup for reading amidst mmap */
3087 lseek(session
->fd
, offset
+ sizeof(struct tracing_data_event
),
3090 size_read
= trace_report(session
->fd
, &session
->pevent
,
3092 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3094 if (readn(session
->fd
, buf
, padding
) < 0) {
3095 pr_err("%s: reading input file", __func__
);
3098 if (session
->repipe
) {
3099 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3100 if (retw
<= 0 || retw
!= padding
) {
3101 pr_err("%s: repiping tracing data padding", __func__
);
3106 if (size_read
+ padding
!= size
) {
3107 pr_err("%s: tracing data size mismatch", __func__
);
3111 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3114 return size_read
+ padding
;
3117 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3118 struct dso
*pos
, u16 misc
,
3119 perf_event__handler_t process
,
3120 struct machine
*machine
)
3122 union perf_event ev
;
3129 memset(&ev
, 0, sizeof(ev
));
3131 len
= pos
->long_name_len
+ 1;
3132 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3133 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3134 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3135 ev
.build_id
.header
.misc
= misc
;
3136 ev
.build_id
.pid
= machine
->pid
;
3137 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3138 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3140 err
= process(tool
, &ev
, NULL
, machine
);
3145 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3146 union perf_event
*event
,
3147 struct perf_session
*session
)
3149 __event_process_build_id(&event
->build_id
,
3150 event
->build_id
.filename
,
3155 void disable_buildid_cache(void)
3157 no_buildid_cache
= true;