1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/compiler.h>
13 #include <linux/list.h>
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/stringify.h>
18 #include <sys/utsname.h>
19 #include <linux/time64.h>
21 #include <bpf/libbpf.h>
28 #include "trace-event.h"
38 #include <api/fs/fs.h>
41 #include "time-utils.h"
44 #include "bpf-event.h"
46 #include "sane_ctype.h"
50 * must be a numerical value to let the endianness
51 * determine the memory layout. That way we are able
52 * to detect endianness when reading the perf.data file
55 * we check for legacy (PERFFILE) format.
57 static const char *__perf_magic1
= "PERFFILE";
58 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
59 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
61 #define PERF_MAGIC __perf_magic2
63 const char perf_version_string
[] = PERF_VERSION
;
65 struct perf_file_attr
{
66 struct perf_event_attr attr
;
67 struct perf_file_section ids
;
71 struct perf_header
*ph
;
73 void *buf
; /* Either buf != NULL or fd >= 0 */
76 struct perf_evsel
*events
;
79 void perf_header__set_feat(struct perf_header
*header
, int feat
)
81 set_bit(feat
, header
->adds_features
);
84 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
86 clear_bit(feat
, header
->adds_features
);
89 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
91 return test_bit(feat
, header
->adds_features
);
94 static int __do_write_fd(struct feat_fd
*ff
, const void *buf
, size_t size
)
96 ssize_t ret
= writen(ff
->fd
, buf
, size
);
98 if (ret
!= (ssize_t
)size
)
99 return ret
< 0 ? (int)ret
: -1;
103 static int __do_write_buf(struct feat_fd
*ff
, const void *buf
, size_t size
)
105 /* struct perf_event_header::size is u16 */
106 const size_t max_size
= 0xffff - sizeof(struct perf_event_header
);
107 size_t new_size
= ff
->size
;
110 if (size
+ ff
->offset
> max_size
)
113 while (size
> (new_size
- ff
->offset
))
115 new_size
= min(max_size
, new_size
);
117 if (ff
->size
< new_size
) {
118 addr
= realloc(ff
->buf
, new_size
);
125 memcpy(ff
->buf
+ ff
->offset
, buf
, size
);
131 /* Return: 0 if succeded, -ERR if failed. */
132 int do_write(struct feat_fd
*ff
, const void *buf
, size_t size
)
135 return __do_write_fd(ff
, buf
, size
);
136 return __do_write_buf(ff
, buf
, size
);
139 /* Return: 0 if succeded, -ERR if failed. */
140 static int do_write_bitmap(struct feat_fd
*ff
, unsigned long *set
, u64 size
)
142 u64
*p
= (u64
*) set
;
145 ret
= do_write(ff
, &size
, sizeof(size
));
149 for (i
= 0; (u64
) i
< BITS_TO_U64(size
); i
++) {
150 ret
= do_write(ff
, p
+ i
, sizeof(*p
));
158 /* Return: 0 if succeded, -ERR if failed. */
159 int write_padded(struct feat_fd
*ff
, const void *bf
,
160 size_t count
, size_t count_aligned
)
162 static const char zero_buf
[NAME_ALIGN
];
163 int err
= do_write(ff
, bf
, count
);
166 err
= do_write(ff
, zero_buf
, count_aligned
- count
);
171 #define string_size(str) \
172 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
174 /* Return: 0 if succeded, -ERR if failed. */
175 static int do_write_string(struct feat_fd
*ff
, const char *str
)
180 olen
= strlen(str
) + 1;
181 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
183 /* write len, incl. \0 */
184 ret
= do_write(ff
, &len
, sizeof(len
));
188 return write_padded(ff
, str
, olen
, len
);
191 static int __do_read_fd(struct feat_fd
*ff
, void *addr
, ssize_t size
)
193 ssize_t ret
= readn(ff
->fd
, addr
, size
);
196 return ret
< 0 ? (int)ret
: -1;
200 static int __do_read_buf(struct feat_fd
*ff
, void *addr
, ssize_t size
)
202 if (size
> (ssize_t
)ff
->size
- ff
->offset
)
205 memcpy(addr
, ff
->buf
+ ff
->offset
, size
);
212 static int __do_read(struct feat_fd
*ff
, void *addr
, ssize_t size
)
215 return __do_read_fd(ff
, addr
, size
);
216 return __do_read_buf(ff
, addr
, size
);
219 static int do_read_u32(struct feat_fd
*ff
, u32
*addr
)
223 ret
= __do_read(ff
, addr
, sizeof(*addr
));
227 if (ff
->ph
->needs_swap
)
228 *addr
= bswap_32(*addr
);
232 static int do_read_u64(struct feat_fd
*ff
, u64
*addr
)
236 ret
= __do_read(ff
, addr
, sizeof(*addr
));
240 if (ff
->ph
->needs_swap
)
241 *addr
= bswap_64(*addr
);
245 static char *do_read_string(struct feat_fd
*ff
)
250 if (do_read_u32(ff
, &len
))
257 if (!__do_read(ff
, buf
, len
)) {
259 * strings are padded by zeroes
260 * thus the actual strlen of buf
261 * may be less than len
270 /* Return: 0 if succeded, -ERR if failed. */
271 static int do_read_bitmap(struct feat_fd
*ff
, unsigned long **pset
, u64
*psize
)
277 ret
= do_read_u64(ff
, &size
);
281 set
= bitmap_alloc(size
);
287 for (i
= 0; (u64
) i
< BITS_TO_U64(size
); i
++) {
288 ret
= do_read_u64(ff
, p
+ i
);
300 static int write_tracing_data(struct feat_fd
*ff
,
301 struct perf_evlist
*evlist
)
303 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
306 return read_tracing_data(ff
->fd
, &evlist
->entries
);
309 static int write_build_id(struct feat_fd
*ff
,
310 struct perf_evlist
*evlist __maybe_unused
)
312 struct perf_session
*session
;
315 session
= container_of(ff
->ph
, struct perf_session
, header
);
317 if (!perf_session__read_build_ids(session
, true))
320 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
323 err
= perf_session__write_buildid_table(session
, ff
);
325 pr_debug("failed to write buildid table\n");
328 perf_session__cache_build_ids(session
);
333 static int write_hostname(struct feat_fd
*ff
,
334 struct perf_evlist
*evlist __maybe_unused
)
343 return do_write_string(ff
, uts
.nodename
);
346 static int write_osrelease(struct feat_fd
*ff
,
347 struct perf_evlist
*evlist __maybe_unused
)
356 return do_write_string(ff
, uts
.release
);
359 static int write_arch(struct feat_fd
*ff
,
360 struct perf_evlist
*evlist __maybe_unused
)
369 return do_write_string(ff
, uts
.machine
);
372 static int write_version(struct feat_fd
*ff
,
373 struct perf_evlist
*evlist __maybe_unused
)
375 return do_write_string(ff
, perf_version_string
);
378 static int __write_cpudesc(struct feat_fd
*ff
, const char *cpuinfo_proc
)
383 const char *search
= cpuinfo_proc
;
390 file
= fopen("/proc/cpuinfo", "r");
394 while (getline(&buf
, &len
, file
) > 0) {
395 ret
= strncmp(buf
, search
, strlen(search
));
407 p
= strchr(buf
, ':');
408 if (p
&& *(p
+1) == ' ' && *(p
+2))
414 /* squash extra space characters (branding string) */
421 while (*q
&& isspace(*q
))
424 while ((*r
++ = *q
++));
428 ret
= do_write_string(ff
, s
);
435 static int write_cpudesc(struct feat_fd
*ff
,
436 struct perf_evlist
*evlist __maybe_unused
)
438 const char *cpuinfo_procs
[] = CPUINFO_PROC
;
441 for (i
= 0; i
< ARRAY_SIZE(cpuinfo_procs
); i
++) {
443 ret
= __write_cpudesc(ff
, cpuinfo_procs
[i
]);
451 static int write_nrcpus(struct feat_fd
*ff
,
452 struct perf_evlist
*evlist __maybe_unused
)
458 nrc
= cpu__max_present_cpu();
460 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
464 nra
= (u32
)(nr
& UINT_MAX
);
466 ret
= do_write(ff
, &nrc
, sizeof(nrc
));
470 return do_write(ff
, &nra
, sizeof(nra
));
473 static int write_event_desc(struct feat_fd
*ff
,
474 struct perf_evlist
*evlist
)
476 struct perf_evsel
*evsel
;
480 nre
= evlist
->nr_entries
;
483 * write number of events
485 ret
= do_write(ff
, &nre
, sizeof(nre
));
490 * size of perf_event_attr struct
492 sz
= (u32
)sizeof(evsel
->attr
);
493 ret
= do_write(ff
, &sz
, sizeof(sz
));
497 evlist__for_each_entry(evlist
, evsel
) {
498 ret
= do_write(ff
, &evsel
->attr
, sz
);
502 * write number of unique id per event
503 * there is one id per instance of an event
505 * copy into an nri to be independent of the
509 ret
= do_write(ff
, &nri
, sizeof(nri
));
514 * write event string as passed on cmdline
516 ret
= do_write_string(ff
, perf_evsel__name(evsel
));
520 * write unique ids for this event
522 ret
= do_write(ff
, evsel
->id
, evsel
->ids
* sizeof(u64
));
529 static int write_cmdline(struct feat_fd
*ff
,
530 struct perf_evlist
*evlist __maybe_unused
)
532 char pbuf
[MAXPATHLEN
], *buf
;
535 /* actual path to perf binary */
536 buf
= perf_exe(pbuf
, MAXPATHLEN
);
538 /* account for binary path */
539 n
= perf_env
.nr_cmdline
+ 1;
541 ret
= do_write(ff
, &n
, sizeof(n
));
545 ret
= do_write_string(ff
, buf
);
549 for (i
= 0 ; i
< perf_env
.nr_cmdline
; i
++) {
550 ret
= do_write_string(ff
, perf_env
.cmdline_argv
[i
]);
558 static int write_cpu_topology(struct feat_fd
*ff
,
559 struct perf_evlist
*evlist __maybe_unused
)
561 struct cpu_topology
*tp
;
565 tp
= cpu_topology__new();
569 ret
= do_write(ff
, &tp
->core_sib
, sizeof(tp
->core_sib
));
573 for (i
= 0; i
< tp
->core_sib
; i
++) {
574 ret
= do_write_string(ff
, tp
->core_siblings
[i
]);
578 ret
= do_write(ff
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
582 for (i
= 0; i
< tp
->thread_sib
; i
++) {
583 ret
= do_write_string(ff
, tp
->thread_siblings
[i
]);
588 ret
= perf_env__read_cpu_topology_map(&perf_env
);
592 for (j
= 0; j
< perf_env
.nr_cpus_avail
; j
++) {
593 ret
= do_write(ff
, &perf_env
.cpu
[j
].core_id
,
594 sizeof(perf_env
.cpu
[j
].core_id
));
597 ret
= do_write(ff
, &perf_env
.cpu
[j
].socket_id
,
598 sizeof(perf_env
.cpu
[j
].socket_id
));
603 cpu_topology__delete(tp
);
609 static int write_total_mem(struct feat_fd
*ff
,
610 struct perf_evlist
*evlist __maybe_unused
)
618 fp
= fopen("/proc/meminfo", "r");
622 while (getline(&buf
, &len
, fp
) > 0) {
623 ret
= strncmp(buf
, "MemTotal:", 9);
628 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
630 ret
= do_write(ff
, &mem
, sizeof(mem
));
638 static int write_numa_topology(struct feat_fd
*ff
,
639 struct perf_evlist
*evlist __maybe_unused
)
641 struct numa_topology
*tp
;
645 tp
= numa_topology__new();
649 ret
= do_write(ff
, &tp
->nr
, sizeof(u32
));
653 for (i
= 0; i
< tp
->nr
; i
++) {
654 struct numa_topology_node
*n
= &tp
->nodes
[i
];
656 ret
= do_write(ff
, &n
->node
, sizeof(u32
));
660 ret
= do_write(ff
, &n
->mem_total
, sizeof(u64
));
664 ret
= do_write(ff
, &n
->mem_free
, sizeof(u64
));
668 ret
= do_write_string(ff
, n
->cpus
);
676 numa_topology__delete(tp
);
683 * struct pmu_mappings {
692 static int write_pmu_mappings(struct feat_fd
*ff
,
693 struct perf_evlist
*evlist __maybe_unused
)
695 struct perf_pmu
*pmu
= NULL
;
700 * Do a first pass to count number of pmu to avoid lseek so this
701 * works in pipe mode as well.
703 while ((pmu
= perf_pmu__scan(pmu
))) {
709 ret
= do_write(ff
, &pmu_num
, sizeof(pmu_num
));
713 while ((pmu
= perf_pmu__scan(pmu
))) {
717 ret
= do_write(ff
, &pmu
->type
, sizeof(pmu
->type
));
721 ret
= do_write_string(ff
, pmu
->name
);
732 * struct group_descs {
734 * struct group_desc {
741 static int write_group_desc(struct feat_fd
*ff
,
742 struct perf_evlist
*evlist
)
744 u32 nr_groups
= evlist
->nr_groups
;
745 struct perf_evsel
*evsel
;
748 ret
= do_write(ff
, &nr_groups
, sizeof(nr_groups
));
752 evlist__for_each_entry(evlist
, evsel
) {
753 if (perf_evsel__is_group_leader(evsel
) &&
754 evsel
->nr_members
> 1) {
755 const char *name
= evsel
->group_name
?: "{anon_group}";
756 u32 leader_idx
= evsel
->idx
;
757 u32 nr_members
= evsel
->nr_members
;
759 ret
= do_write_string(ff
, name
);
763 ret
= do_write(ff
, &leader_idx
, sizeof(leader_idx
));
767 ret
= do_write(ff
, &nr_members
, sizeof(nr_members
));
776 * Return the CPU id as a raw string.
778 * Each architecture should provide a more precise id string that
779 * can be use to match the architecture's "mapfile".
781 char * __weak
get_cpuid_str(struct perf_pmu
*pmu __maybe_unused
)
786 /* Return zero when the cpuid from the mapfile.csv matches the
787 * cpuid string generated on this platform.
788 * Otherwise return non-zero.
790 int __weak
strcmp_cpuid_str(const char *mapcpuid
, const char *cpuid
)
793 regmatch_t pmatch
[1];
796 if (regcomp(&re
, mapcpuid
, REG_EXTENDED
) != 0) {
797 /* Warn unable to generate match particular string. */
798 pr_info("Invalid regular expression %s\n", mapcpuid
);
802 match
= !regexec(&re
, cpuid
, 1, pmatch
, 0);
805 size_t match_len
= (pmatch
[0].rm_eo
- pmatch
[0].rm_so
);
807 /* Verify the entire string matched. */
808 if (match_len
== strlen(cpuid
))
815 * default get_cpuid(): nothing gets recorded
816 * actual implementation must be in arch/$(SRCARCH)/util/header.c
818 int __weak
get_cpuid(char *buffer __maybe_unused
, size_t sz __maybe_unused
)
823 static int write_cpuid(struct feat_fd
*ff
,
824 struct perf_evlist
*evlist __maybe_unused
)
829 ret
= get_cpuid(buffer
, sizeof(buffer
));
833 return do_write_string(ff
, buffer
);
836 static int write_branch_stack(struct feat_fd
*ff __maybe_unused
,
837 struct perf_evlist
*evlist __maybe_unused
)
842 static int write_auxtrace(struct feat_fd
*ff
,
843 struct perf_evlist
*evlist __maybe_unused
)
845 struct perf_session
*session
;
848 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
851 session
= container_of(ff
->ph
, struct perf_session
, header
);
853 err
= auxtrace_index__write(ff
->fd
, &session
->auxtrace_index
);
855 pr_err("Failed to write auxtrace index\n");
859 static int write_clockid(struct feat_fd
*ff
,
860 struct perf_evlist
*evlist __maybe_unused
)
862 return do_write(ff
, &ff
->ph
->env
.clockid_res_ns
,
863 sizeof(ff
->ph
->env
.clockid_res_ns
));
866 static int write_dir_format(struct feat_fd
*ff
,
867 struct perf_evlist
*evlist __maybe_unused
)
869 struct perf_session
*session
;
870 struct perf_data
*data
;
872 session
= container_of(ff
->ph
, struct perf_session
, header
);
873 data
= session
->data
;
875 if (WARN_ON(!perf_data__is_dir(data
)))
878 return do_write(ff
, &data
->dir
.version
, sizeof(data
->dir
.version
));
881 #ifdef HAVE_LIBBPF_SUPPORT
882 static int write_bpf_prog_info(struct feat_fd
*ff
,
883 struct perf_evlist
*evlist __maybe_unused
)
885 struct perf_env
*env
= &ff
->ph
->env
;
886 struct rb_root
*root
;
887 struct rb_node
*next
;
890 down_read(&env
->bpf_progs
.lock
);
892 ret
= do_write(ff
, &env
->bpf_progs
.infos_cnt
,
893 sizeof(env
->bpf_progs
.infos_cnt
));
897 root
= &env
->bpf_progs
.infos
;
898 next
= rb_first(root
);
900 struct bpf_prog_info_node
*node
;
903 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
904 next
= rb_next(&node
->rb_node
);
905 len
= sizeof(struct bpf_prog_info_linear
) +
906 node
->info_linear
->data_len
;
908 /* before writing to file, translate address to offset */
909 bpf_program__bpil_addr_to_offs(node
->info_linear
);
910 ret
= do_write(ff
, node
->info_linear
, len
);
912 * translate back to address even when do_write() fails,
913 * so that this function never changes the data.
915 bpf_program__bpil_offs_to_addr(node
->info_linear
);
920 up_read(&env
->bpf_progs
.lock
);
923 #else // HAVE_LIBBPF_SUPPORT
924 static int write_bpf_prog_info(struct feat_fd
*ff __maybe_unused
,
925 struct perf_evlist
*evlist __maybe_unused
)
929 #endif // HAVE_LIBBPF_SUPPORT
931 static int write_bpf_btf(struct feat_fd
*ff
,
932 struct perf_evlist
*evlist __maybe_unused
)
934 struct perf_env
*env
= &ff
->ph
->env
;
935 struct rb_root
*root
;
936 struct rb_node
*next
;
939 down_read(&env
->bpf_progs
.lock
);
941 ret
= do_write(ff
, &env
->bpf_progs
.btfs_cnt
,
942 sizeof(env
->bpf_progs
.btfs_cnt
));
947 root
= &env
->bpf_progs
.btfs
;
948 next
= rb_first(root
);
950 struct btf_node
*node
;
952 node
= rb_entry(next
, struct btf_node
, rb_node
);
953 next
= rb_next(&node
->rb_node
);
954 ret
= do_write(ff
, &node
->id
,
955 sizeof(u32
) * 2 + node
->data_size
);
960 up_read(&env
->bpf_progs
.lock
);
964 static int cpu_cache_level__sort(const void *a
, const void *b
)
966 struct cpu_cache_level
*cache_a
= (struct cpu_cache_level
*)a
;
967 struct cpu_cache_level
*cache_b
= (struct cpu_cache_level
*)b
;
969 return cache_a
->level
- cache_b
->level
;
972 static bool cpu_cache_level__cmp(struct cpu_cache_level
*a
, struct cpu_cache_level
*b
)
974 if (a
->level
!= b
->level
)
977 if (a
->line_size
!= b
->line_size
)
980 if (a
->sets
!= b
->sets
)
983 if (a
->ways
!= b
->ways
)
986 if (strcmp(a
->type
, b
->type
))
989 if (strcmp(a
->size
, b
->size
))
992 if (strcmp(a
->map
, b
->map
))
998 static int cpu_cache_level__read(struct cpu_cache_level
*cache
, u32 cpu
, u16 level
)
1000 char path
[PATH_MAX
], file
[PATH_MAX
];
1004 scnprintf(path
, PATH_MAX
, "devices/system/cpu/cpu%d/cache/index%d/", cpu
, level
);
1005 scnprintf(file
, PATH_MAX
, "%s/%s", sysfs__mountpoint(), path
);
1007 if (stat(file
, &st
))
1010 scnprintf(file
, PATH_MAX
, "%s/level", path
);
1011 if (sysfs__read_int(file
, (int *) &cache
->level
))
1014 scnprintf(file
, PATH_MAX
, "%s/coherency_line_size", path
);
1015 if (sysfs__read_int(file
, (int *) &cache
->line_size
))
1018 scnprintf(file
, PATH_MAX
, "%s/number_of_sets", path
);
1019 if (sysfs__read_int(file
, (int *) &cache
->sets
))
1022 scnprintf(file
, PATH_MAX
, "%s/ways_of_associativity", path
);
1023 if (sysfs__read_int(file
, (int *) &cache
->ways
))
1026 scnprintf(file
, PATH_MAX
, "%s/type", path
);
1027 if (sysfs__read_str(file
, &cache
->type
, &len
))
1030 cache
->type
[len
] = 0;
1031 cache
->type
= rtrim(cache
->type
);
1033 scnprintf(file
, PATH_MAX
, "%s/size", path
);
1034 if (sysfs__read_str(file
, &cache
->size
, &len
)) {
1039 cache
->size
[len
] = 0;
1040 cache
->size
= rtrim(cache
->size
);
1042 scnprintf(file
, PATH_MAX
, "%s/shared_cpu_list", path
);
1043 if (sysfs__read_str(file
, &cache
->map
, &len
)) {
1049 cache
->map
[len
] = 0;
1050 cache
->map
= rtrim(cache
->map
);
1054 static void cpu_cache_level__fprintf(FILE *out
, struct cpu_cache_level
*c
)
1056 fprintf(out
, "L%d %-15s %8s [%s]\n", c
->level
, c
->type
, c
->size
, c
->map
);
1059 static int build_caches(struct cpu_cache_level caches
[], u32 size
, u32
*cntp
)
1066 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
1070 nr
= (u32
)(ncpus
& UINT_MAX
);
1072 for (cpu
= 0; cpu
< nr
; cpu
++) {
1073 for (level
= 0; level
< 10; level
++) {
1074 struct cpu_cache_level c
;
1077 err
= cpu_cache_level__read(&c
, cpu
, level
);
1084 for (i
= 0; i
< cnt
; i
++) {
1085 if (cpu_cache_level__cmp(&c
, &caches
[i
]))
1092 cpu_cache_level__free(&c
);
1094 if (WARN_ONCE(cnt
== size
, "way too many cpu caches.."))
1103 #define MAX_CACHES 2000
1105 static int write_cache(struct feat_fd
*ff
,
1106 struct perf_evlist
*evlist __maybe_unused
)
1108 struct cpu_cache_level caches
[MAX_CACHES
];
1109 u32 cnt
= 0, i
, version
= 1;
1112 ret
= build_caches(caches
, MAX_CACHES
, &cnt
);
1116 qsort(&caches
, cnt
, sizeof(struct cpu_cache_level
), cpu_cache_level__sort
);
1118 ret
= do_write(ff
, &version
, sizeof(u32
));
1122 ret
= do_write(ff
, &cnt
, sizeof(u32
));
1126 for (i
= 0; i
< cnt
; i
++) {
1127 struct cpu_cache_level
*c
= &caches
[i
];
1130 ret = do_write(ff, &c->v, sizeof(u32)); \
1141 ret = do_write_string(ff, (const char *) c->v); \
1152 for (i
= 0; i
< cnt
; i
++)
1153 cpu_cache_level__free(&caches
[i
]);
1157 static int write_stat(struct feat_fd
*ff __maybe_unused
,
1158 struct perf_evlist
*evlist __maybe_unused
)
1163 static int write_sample_time(struct feat_fd
*ff
,
1164 struct perf_evlist
*evlist
)
1168 ret
= do_write(ff
, &evlist
->first_sample_time
,
1169 sizeof(evlist
->first_sample_time
));
1173 return do_write(ff
, &evlist
->last_sample_time
,
1174 sizeof(evlist
->last_sample_time
));
1178 static int memory_node__read(struct memory_node
*n
, unsigned long idx
)
1180 unsigned int phys
, size
= 0;
1181 char path
[PATH_MAX
];
1185 #define for_each_memory(mem, dir) \
1186 while ((ent = readdir(dir))) \
1187 if (strcmp(ent->d_name, ".") && \
1188 strcmp(ent->d_name, "..") && \
1189 sscanf(ent->d_name, "memory%u", &mem) == 1)
1191 scnprintf(path
, PATH_MAX
,
1192 "%s/devices/system/node/node%lu",
1193 sysfs__mountpoint(), idx
);
1195 dir
= opendir(path
);
1197 pr_warning("failed: cant' open memory sysfs data\n");
1201 for_each_memory(phys
, dir
) {
1202 size
= max(phys
, size
);
1207 n
->set
= bitmap_alloc(size
);
1218 for_each_memory(phys
, dir
) {
1219 set_bit(phys
, n
->set
);
1226 static int memory_node__sort(const void *a
, const void *b
)
1228 const struct memory_node
*na
= a
;
1229 const struct memory_node
*nb
= b
;
1231 return na
->node
- nb
->node
;
1234 static int build_mem_topology(struct memory_node
*nodes
, u64 size
, u64
*cntp
)
1236 char path
[PATH_MAX
];
1242 scnprintf(path
, PATH_MAX
, "%s/devices/system/node/",
1243 sysfs__mountpoint());
1245 dir
= opendir(path
);
1247 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1252 while (!ret
&& (ent
= readdir(dir
))) {
1256 if (!strcmp(ent
->d_name
, ".") ||
1257 !strcmp(ent
->d_name
, ".."))
1260 r
= sscanf(ent
->d_name
, "node%u", &idx
);
1264 if (WARN_ONCE(cnt
>= size
,
1265 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1268 ret
= memory_node__read(&nodes
[cnt
++], idx
);
1275 qsort(nodes
, cnt
, sizeof(nodes
[0]), memory_node__sort
);
1280 #define MAX_MEMORY_NODES 2000
1283 * The MEM_TOPOLOGY holds physical memory map for every
1284 * node in system. The format of data is as follows:
1286 * 0 - version | for future changes
1287 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1288 * 16 - count | number of nodes
1290 * For each node we store map of physical indexes for
1293 * 32 - node id | node index
1294 * 40 - size | size of bitmap
1295 * 48 - bitmap | bitmap of memory indexes that belongs to node
1297 static int write_mem_topology(struct feat_fd
*ff __maybe_unused
,
1298 struct perf_evlist
*evlist __maybe_unused
)
1300 static struct memory_node nodes
[MAX_MEMORY_NODES
];
1301 u64 bsize
, version
= 1, i
, nr
;
1304 ret
= sysfs__read_xll("devices/system/memory/block_size_bytes",
1305 (unsigned long long *) &bsize
);
1309 ret
= build_mem_topology(&nodes
[0], MAX_MEMORY_NODES
, &nr
);
1313 ret
= do_write(ff
, &version
, sizeof(version
));
1317 ret
= do_write(ff
, &bsize
, sizeof(bsize
));
1321 ret
= do_write(ff
, &nr
, sizeof(nr
));
1325 for (i
= 0; i
< nr
; i
++) {
1326 struct memory_node
*n
= &nodes
[i
];
1329 ret = do_write(ff, &n->v, sizeof(n->v)); \
1338 ret
= do_write_bitmap(ff
, n
->set
, n
->size
);
1347 static void print_hostname(struct feat_fd
*ff
, FILE *fp
)
1349 fprintf(fp
, "# hostname : %s\n", ff
->ph
->env
.hostname
);
1352 static void print_osrelease(struct feat_fd
*ff
, FILE *fp
)
1354 fprintf(fp
, "# os release : %s\n", ff
->ph
->env
.os_release
);
1357 static void print_arch(struct feat_fd
*ff
, FILE *fp
)
1359 fprintf(fp
, "# arch : %s\n", ff
->ph
->env
.arch
);
1362 static void print_cpudesc(struct feat_fd
*ff
, FILE *fp
)
1364 fprintf(fp
, "# cpudesc : %s\n", ff
->ph
->env
.cpu_desc
);
1367 static void print_nrcpus(struct feat_fd
*ff
, FILE *fp
)
1369 fprintf(fp
, "# nrcpus online : %u\n", ff
->ph
->env
.nr_cpus_online
);
1370 fprintf(fp
, "# nrcpus avail : %u\n", ff
->ph
->env
.nr_cpus_avail
);
1373 static void print_version(struct feat_fd
*ff
, FILE *fp
)
1375 fprintf(fp
, "# perf version : %s\n", ff
->ph
->env
.version
);
1378 static void print_cmdline(struct feat_fd
*ff
, FILE *fp
)
1382 nr
= ff
->ph
->env
.nr_cmdline
;
1384 fprintf(fp
, "# cmdline : ");
1386 for (i
= 0; i
< nr
; i
++) {
1387 char *argv_i
= strdup(ff
->ph
->env
.cmdline_argv
[i
]);
1389 fprintf(fp
, "%s ", ff
->ph
->env
.cmdline_argv
[i
]);
1393 char *quote
= strchr(argv_i
, '\'');
1397 fprintf(fp
, "%s\\\'", argv_i
);
1400 fprintf(fp
, "%s ", argv_i
);
1407 static void print_cpu_topology(struct feat_fd
*ff
, FILE *fp
)
1409 struct perf_header
*ph
= ff
->ph
;
1410 int cpu_nr
= ph
->env
.nr_cpus_avail
;
1414 nr
= ph
->env
.nr_sibling_cores
;
1415 str
= ph
->env
.sibling_cores
;
1417 for (i
= 0; i
< nr
; i
++) {
1418 fprintf(fp
, "# sibling cores : %s\n", str
);
1419 str
+= strlen(str
) + 1;
1422 nr
= ph
->env
.nr_sibling_threads
;
1423 str
= ph
->env
.sibling_threads
;
1425 for (i
= 0; i
< nr
; i
++) {
1426 fprintf(fp
, "# sibling threads : %s\n", str
);
1427 str
+= strlen(str
) + 1;
1430 if (ph
->env
.cpu
!= NULL
) {
1431 for (i
= 0; i
< cpu_nr
; i
++)
1432 fprintf(fp
, "# CPU %d: Core ID %d, Socket ID %d\n", i
,
1433 ph
->env
.cpu
[i
].core_id
, ph
->env
.cpu
[i
].socket_id
);
1435 fprintf(fp
, "# Core ID and Socket ID information is not available\n");
1438 static void print_clockid(struct feat_fd
*ff
, FILE *fp
)
1440 fprintf(fp
, "# clockid frequency: %"PRIu64
" MHz\n",
1441 ff
->ph
->env
.clockid_res_ns
* 1000);
1444 static void print_dir_format(struct feat_fd
*ff
, FILE *fp
)
1446 struct perf_session
*session
;
1447 struct perf_data
*data
;
1449 session
= container_of(ff
->ph
, struct perf_session
, header
);
1450 data
= session
->data
;
1452 fprintf(fp
, "# directory data version : %"PRIu64
"\n", data
->dir
.version
);
1455 static void print_bpf_prog_info(struct feat_fd
*ff
, FILE *fp
)
1457 struct perf_env
*env
= &ff
->ph
->env
;
1458 struct rb_root
*root
;
1459 struct rb_node
*next
;
1461 down_read(&env
->bpf_progs
.lock
);
1463 root
= &env
->bpf_progs
.infos
;
1464 next
= rb_first(root
);
1467 struct bpf_prog_info_node
*node
;
1469 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
1470 next
= rb_next(&node
->rb_node
);
1472 bpf_event__print_bpf_prog_info(&node
->info_linear
->info
,
1476 up_read(&env
->bpf_progs
.lock
);
1479 static void print_bpf_btf(struct feat_fd
*ff
, FILE *fp
)
1481 struct perf_env
*env
= &ff
->ph
->env
;
1482 struct rb_root
*root
;
1483 struct rb_node
*next
;
1485 down_read(&env
->bpf_progs
.lock
);
1487 root
= &env
->bpf_progs
.btfs
;
1488 next
= rb_first(root
);
1491 struct btf_node
*node
;
1493 node
= rb_entry(next
, struct btf_node
, rb_node
);
1494 next
= rb_next(&node
->rb_node
);
1495 fprintf(fp
, "# btf info of id %u\n", node
->id
);
1498 up_read(&env
->bpf_progs
.lock
);
1501 static void free_event_desc(struct perf_evsel
*events
)
1503 struct perf_evsel
*evsel
;
1508 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1509 zfree(&evsel
->name
);
1516 static struct perf_evsel
*read_event_desc(struct feat_fd
*ff
)
1518 struct perf_evsel
*evsel
, *events
= NULL
;
1521 u32 nre
, sz
, nr
, i
, j
;
1524 /* number of events */
1525 if (do_read_u32(ff
, &nre
))
1528 if (do_read_u32(ff
, &sz
))
1531 /* buffer to hold on file attr struct */
1536 /* the last event terminates with evsel->attr.size == 0: */
1537 events
= calloc(nre
+ 1, sizeof(*events
));
1541 msz
= sizeof(evsel
->attr
);
1545 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1549 * must read entire on-file attr struct to
1550 * sync up with layout.
1552 if (__do_read(ff
, buf
, sz
))
1555 if (ff
->ph
->needs_swap
)
1556 perf_event__attr_swap(buf
);
1558 memcpy(&evsel
->attr
, buf
, msz
);
1560 if (do_read_u32(ff
, &nr
))
1563 if (ff
->ph
->needs_swap
)
1564 evsel
->needs_swap
= true;
1566 evsel
->name
= do_read_string(ff
);
1573 id
= calloc(nr
, sizeof(*id
));
1579 for (j
= 0 ; j
< nr
; j
++) {
1580 if (do_read_u64(ff
, id
))
1589 free_event_desc(events
);
1594 static int __desc_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1595 void *priv __maybe_unused
)
1597 return fprintf(fp
, ", %s = %s", name
, val
);
1600 static void print_event_desc(struct feat_fd
*ff
, FILE *fp
)
1602 struct perf_evsel
*evsel
, *events
;
1607 events
= ff
->events
;
1609 events
= read_event_desc(ff
);
1612 fprintf(fp
, "# event desc: not available or unable to read\n");
1616 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1617 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1620 fprintf(fp
, ", id = {");
1621 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1624 fprintf(fp
, " %"PRIu64
, *id
);
1629 perf_event_attr__fprintf(fp
, &evsel
->attr
, __desc_attr__fprintf
, NULL
);
1634 free_event_desc(events
);
1638 static void print_total_mem(struct feat_fd
*ff
, FILE *fp
)
1640 fprintf(fp
, "# total memory : %llu kB\n", ff
->ph
->env
.total_mem
);
1643 static void print_numa_topology(struct feat_fd
*ff
, FILE *fp
)
1646 struct numa_node
*n
;
1648 for (i
= 0; i
< ff
->ph
->env
.nr_numa_nodes
; i
++) {
1649 n
= &ff
->ph
->env
.numa_nodes
[i
];
1651 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1652 " free = %"PRIu64
" kB\n",
1653 n
->node
, n
->mem_total
, n
->mem_free
);
1655 fprintf(fp
, "# node%u cpu list : ", n
->node
);
1656 cpu_map__fprintf(n
->map
, fp
);
1660 static void print_cpuid(struct feat_fd
*ff
, FILE *fp
)
1662 fprintf(fp
, "# cpuid : %s\n", ff
->ph
->env
.cpuid
);
1665 static void print_branch_stack(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1667 fprintf(fp
, "# contains samples with branch stack\n");
1670 static void print_auxtrace(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1672 fprintf(fp
, "# contains AUX area data (e.g. instruction trace)\n");
1675 static void print_stat(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1677 fprintf(fp
, "# contains stat data\n");
1680 static void print_cache(struct feat_fd
*ff
, FILE *fp __maybe_unused
)
1684 fprintf(fp
, "# CPU cache info:\n");
1685 for (i
= 0; i
< ff
->ph
->env
.caches_cnt
; i
++) {
1687 cpu_cache_level__fprintf(fp
, &ff
->ph
->env
.caches
[i
]);
1691 static void print_pmu_mappings(struct feat_fd
*ff
, FILE *fp
)
1693 const char *delimiter
= "# pmu mappings: ";
1698 pmu_num
= ff
->ph
->env
.nr_pmu_mappings
;
1700 fprintf(fp
, "# pmu mappings: not available\n");
1704 str
= ff
->ph
->env
.pmu_mappings
;
1707 type
= strtoul(str
, &tmp
, 0);
1712 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1715 str
+= strlen(str
) + 1;
1724 fprintf(fp
, "# pmu mappings: unable to read\n");
1727 static void print_group_desc(struct feat_fd
*ff
, FILE *fp
)
1729 struct perf_session
*session
;
1730 struct perf_evsel
*evsel
;
1733 session
= container_of(ff
->ph
, struct perf_session
, header
);
1735 evlist__for_each_entry(session
->evlist
, evsel
) {
1736 if (perf_evsel__is_group_leader(evsel
) &&
1737 evsel
->nr_members
> 1) {
1738 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1739 perf_evsel__name(evsel
));
1741 nr
= evsel
->nr_members
- 1;
1743 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1751 static void print_sample_time(struct feat_fd
*ff
, FILE *fp
)
1753 struct perf_session
*session
;
1757 session
= container_of(ff
->ph
, struct perf_session
, header
);
1759 timestamp__scnprintf_usec(session
->evlist
->first_sample_time
,
1760 time_buf
, sizeof(time_buf
));
1761 fprintf(fp
, "# time of first sample : %s\n", time_buf
);
1763 timestamp__scnprintf_usec(session
->evlist
->last_sample_time
,
1764 time_buf
, sizeof(time_buf
));
1765 fprintf(fp
, "# time of last sample : %s\n", time_buf
);
1767 d
= (double)(session
->evlist
->last_sample_time
-
1768 session
->evlist
->first_sample_time
) / NSEC_PER_MSEC
;
1770 fprintf(fp
, "# sample duration : %10.3f ms\n", d
);
1773 static void memory_node__fprintf(struct memory_node
*n
,
1774 unsigned long long bsize
, FILE *fp
)
1776 char buf_map
[100], buf_size
[50];
1777 unsigned long long size
;
1779 size
= bsize
* bitmap_weight(n
->set
, n
->size
);
1780 unit_number__scnprintf(buf_size
, 50, size
);
1782 bitmap_scnprintf(n
->set
, n
->size
, buf_map
, 100);
1783 fprintf(fp
, "# %3" PRIu64
" [%s]: %s\n", n
->node
, buf_size
, buf_map
);
1786 static void print_mem_topology(struct feat_fd
*ff
, FILE *fp
)
1788 struct memory_node
*nodes
;
1791 nodes
= ff
->ph
->env
.memory_nodes
;
1792 nr
= ff
->ph
->env
.nr_memory_nodes
;
1794 fprintf(fp
, "# memory nodes (nr %d, block size 0x%llx):\n",
1795 nr
, ff
->ph
->env
.memory_bsize
);
1797 for (i
= 0; i
< nr
; i
++) {
1798 memory_node__fprintf(&nodes
[i
], ff
->ph
->env
.memory_bsize
, fp
);
1802 static int __event_process_build_id(struct build_id_event
*bev
,
1804 struct perf_session
*session
)
1807 struct machine
*machine
;
1810 enum dso_kernel_type dso_type
;
1812 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1816 cpumode
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1819 case PERF_RECORD_MISC_KERNEL
:
1820 dso_type
= DSO_TYPE_KERNEL
;
1822 case PERF_RECORD_MISC_GUEST_KERNEL
:
1823 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1825 case PERF_RECORD_MISC_USER
:
1826 case PERF_RECORD_MISC_GUEST_USER
:
1827 dso_type
= DSO_TYPE_USER
;
1833 dso
= machine__findnew_dso(machine
, filename
);
1835 char sbuild_id
[SBUILD_ID_SIZE
];
1837 dso__set_build_id(dso
, &bev
->build_id
);
1839 if (dso_type
!= DSO_TYPE_USER
) {
1840 struct kmod_path m
= { .name
= NULL
, };
1842 if (!kmod_path__parse_name(&m
, filename
) && m
.kmod
)
1843 dso__set_module_info(dso
, &m
, machine
);
1845 dso
->kernel
= dso_type
;
1850 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1852 pr_debug("build id event received for %s: %s\n",
1853 dso
->long_name
, sbuild_id
);
1862 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1863 int input
, u64 offset
, u64 size
)
1865 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1867 struct perf_event_header header
;
1868 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1871 struct build_id_event bev
;
1872 char filename
[PATH_MAX
];
1873 u64 limit
= offset
+ size
;
1875 while (offset
< limit
) {
1878 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1881 if (header
->needs_swap
)
1882 perf_event_header__bswap(&old_bev
.header
);
1884 len
= old_bev
.header
.size
- sizeof(old_bev
);
1885 if (readn(input
, filename
, len
) != len
)
1888 bev
.header
= old_bev
.header
;
1891 * As the pid is the missing value, we need to fill
1892 * it properly. The header.misc value give us nice hint.
1894 bev
.pid
= HOST_KERNEL_ID
;
1895 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1896 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1897 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1899 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1900 __event_process_build_id(&bev
, filename
, session
);
1902 offset
+= bev
.header
.size
;
1908 static int perf_header__read_build_ids(struct perf_header
*header
,
1909 int input
, u64 offset
, u64 size
)
1911 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1912 struct build_id_event bev
;
1913 char filename
[PATH_MAX
];
1914 u64 limit
= offset
+ size
, orig_offset
= offset
;
1917 while (offset
< limit
) {
1920 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1923 if (header
->needs_swap
)
1924 perf_event_header__bswap(&bev
.header
);
1926 len
= bev
.header
.size
- sizeof(bev
);
1927 if (readn(input
, filename
, len
) != len
)
1930 * The a1645ce1 changeset:
1932 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1934 * Added a field to struct build_id_event that broke the file
1937 * Since the kernel build-id is the first entry, process the
1938 * table using the old format if the well known
1939 * '[kernel.kallsyms]' string for the kernel build-id has the
1940 * first 4 characters chopped off (where the pid_t sits).
1942 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1943 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1945 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1948 __event_process_build_id(&bev
, filename
, session
);
1950 offset
+= bev
.header
.size
;
1957 /* Macro for features that simply need to read and store a string. */
1958 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1959 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1961 ff->ph->env.__feat_env = do_read_string(ff); \
1962 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1965 FEAT_PROCESS_STR_FUN(hostname
, hostname
);
1966 FEAT_PROCESS_STR_FUN(osrelease
, os_release
);
1967 FEAT_PROCESS_STR_FUN(version
, version
);
1968 FEAT_PROCESS_STR_FUN(arch
, arch
);
1969 FEAT_PROCESS_STR_FUN(cpudesc
, cpu_desc
);
1970 FEAT_PROCESS_STR_FUN(cpuid
, cpuid
);
1972 static int process_tracing_data(struct feat_fd
*ff
, void *data
)
1974 ssize_t ret
= trace_report(ff
->fd
, data
, false);
1976 return ret
< 0 ? -1 : 0;
1979 static int process_build_id(struct feat_fd
*ff
, void *data __maybe_unused
)
1981 if (perf_header__read_build_ids(ff
->ph
, ff
->fd
, ff
->offset
, ff
->size
))
1982 pr_debug("Failed to read buildids, continuing...\n");
1986 static int process_nrcpus(struct feat_fd
*ff
, void *data __maybe_unused
)
1989 u32 nr_cpus_avail
, nr_cpus_online
;
1991 ret
= do_read_u32(ff
, &nr_cpus_avail
);
1995 ret
= do_read_u32(ff
, &nr_cpus_online
);
1998 ff
->ph
->env
.nr_cpus_avail
= (int)nr_cpus_avail
;
1999 ff
->ph
->env
.nr_cpus_online
= (int)nr_cpus_online
;
2003 static int process_total_mem(struct feat_fd
*ff
, void *data __maybe_unused
)
2008 ret
= do_read_u64(ff
, &total_mem
);
2011 ff
->ph
->env
.total_mem
= (unsigned long long)total_mem
;
2015 static struct perf_evsel
*
2016 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
2018 struct perf_evsel
*evsel
;
2020 evlist__for_each_entry(evlist
, evsel
) {
2021 if (evsel
->idx
== idx
)
2029 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
2030 struct perf_evsel
*event
)
2032 struct perf_evsel
*evsel
;
2037 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
2044 evsel
->name
= strdup(event
->name
);
2048 process_event_desc(struct feat_fd
*ff
, void *data __maybe_unused
)
2050 struct perf_session
*session
;
2051 struct perf_evsel
*evsel
, *events
= read_event_desc(ff
);
2056 session
= container_of(ff
->ph
, struct perf_session
, header
);
2058 if (session
->data
->is_pipe
) {
2059 /* Save events for reading later by print_event_desc,
2060 * since they can't be read again in pipe mode. */
2061 ff
->events
= events
;
2064 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
2065 perf_evlist__set_event_name(session
->evlist
, evsel
);
2067 if (!session
->data
->is_pipe
)
2068 free_event_desc(events
);
2073 static int process_cmdline(struct feat_fd
*ff
, void *data __maybe_unused
)
2075 char *str
, *cmdline
= NULL
, **argv
= NULL
;
2078 if (do_read_u32(ff
, &nr
))
2081 ff
->ph
->env
.nr_cmdline
= nr
;
2083 cmdline
= zalloc(ff
->size
+ nr
+ 1);
2087 argv
= zalloc(sizeof(char *) * (nr
+ 1));
2091 for (i
= 0; i
< nr
; i
++) {
2092 str
= do_read_string(ff
);
2096 argv
[i
] = cmdline
+ len
;
2097 memcpy(argv
[i
], str
, strlen(str
) + 1);
2098 len
+= strlen(str
) + 1;
2101 ff
->ph
->env
.cmdline
= cmdline
;
2102 ff
->ph
->env
.cmdline_argv
= (const char **) argv
;
2111 static int process_cpu_topology(struct feat_fd
*ff
, void *data __maybe_unused
)
2116 int cpu_nr
= ff
->ph
->env
.nr_cpus_avail
;
2118 struct perf_header
*ph
= ff
->ph
;
2119 bool do_core_id_test
= true;
2121 ph
->env
.cpu
= calloc(cpu_nr
, sizeof(*ph
->env
.cpu
));
2125 if (do_read_u32(ff
, &nr
))
2128 ph
->env
.nr_sibling_cores
= nr
;
2129 size
+= sizeof(u32
);
2130 if (strbuf_init(&sb
, 128) < 0)
2133 for (i
= 0; i
< nr
; i
++) {
2134 str
= do_read_string(ff
);
2138 /* include a NULL character at the end */
2139 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
2141 size
+= string_size(str
);
2144 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
2146 if (do_read_u32(ff
, &nr
))
2149 ph
->env
.nr_sibling_threads
= nr
;
2150 size
+= sizeof(u32
);
2152 for (i
= 0; i
< nr
; i
++) {
2153 str
= do_read_string(ff
);
2157 /* include a NULL character at the end */
2158 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
2160 size
+= string_size(str
);
2163 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
2166 * The header may be from old perf,
2167 * which doesn't include core id and socket id information.
2169 if (ff
->size
<= size
) {
2170 zfree(&ph
->env
.cpu
);
2174 /* On s390 the socket_id number is not related to the numbers of cpus.
2175 * The socket_id number might be higher than the numbers of cpus.
2176 * This depends on the configuration.
2178 if (ph
->env
.arch
&& !strncmp(ph
->env
.arch
, "s390", 4))
2179 do_core_id_test
= false;
2181 for (i
= 0; i
< (u32
)cpu_nr
; i
++) {
2182 if (do_read_u32(ff
, &nr
))
2185 ph
->env
.cpu
[i
].core_id
= nr
;
2187 if (do_read_u32(ff
, &nr
))
2190 if (do_core_id_test
&& nr
!= (u32
)-1 && nr
> (u32
)cpu_nr
) {
2191 pr_debug("socket_id number is too big."
2192 "You may need to upgrade the perf tool.\n");
2196 ph
->env
.cpu
[i
].socket_id
= nr
;
2202 strbuf_release(&sb
);
2204 zfree(&ph
->env
.cpu
);
2208 static int process_numa_topology(struct feat_fd
*ff
, void *data __maybe_unused
)
2210 struct numa_node
*nodes
, *n
;
2215 if (do_read_u32(ff
, &nr
))
2218 nodes
= zalloc(sizeof(*nodes
) * nr
);
2222 for (i
= 0; i
< nr
; i
++) {
2226 if (do_read_u32(ff
, &n
->node
))
2229 if (do_read_u64(ff
, &n
->mem_total
))
2232 if (do_read_u64(ff
, &n
->mem_free
))
2235 str
= do_read_string(ff
);
2239 n
->map
= cpu_map__new(str
);
2245 ff
->ph
->env
.nr_numa_nodes
= nr
;
2246 ff
->ph
->env
.numa_nodes
= nodes
;
2254 static int process_pmu_mappings(struct feat_fd
*ff
, void *data __maybe_unused
)
2261 if (do_read_u32(ff
, &pmu_num
))
2265 pr_debug("pmu mappings not available\n");
2269 ff
->ph
->env
.nr_pmu_mappings
= pmu_num
;
2270 if (strbuf_init(&sb
, 128) < 0)
2274 if (do_read_u32(ff
, &type
))
2277 name
= do_read_string(ff
);
2281 if (strbuf_addf(&sb
, "%u:%s", type
, name
) < 0)
2283 /* include a NULL character at the end */
2284 if (strbuf_add(&sb
, "", 1) < 0)
2287 if (!strcmp(name
, "msr"))
2288 ff
->ph
->env
.msr_pmu_type
= type
;
2293 ff
->ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2297 strbuf_release(&sb
);
2301 static int process_group_desc(struct feat_fd
*ff
, void *data __maybe_unused
)
2304 u32 i
, nr
, nr_groups
;
2305 struct perf_session
*session
;
2306 struct perf_evsel
*evsel
, *leader
= NULL
;
2313 if (do_read_u32(ff
, &nr_groups
))
2316 ff
->ph
->env
.nr_groups
= nr_groups
;
2318 pr_debug("group desc not available\n");
2322 desc
= calloc(nr_groups
, sizeof(*desc
));
2326 for (i
= 0; i
< nr_groups
; i
++) {
2327 desc
[i
].name
= do_read_string(ff
);
2331 if (do_read_u32(ff
, &desc
[i
].leader_idx
))
2334 if (do_read_u32(ff
, &desc
[i
].nr_members
))
2339 * Rebuild group relationship based on the group_desc
2341 session
= container_of(ff
->ph
, struct perf_session
, header
);
2342 session
->evlist
->nr_groups
= nr_groups
;
2345 evlist__for_each_entry(session
->evlist
, evsel
) {
2346 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2347 evsel
->leader
= evsel
;
2348 /* {anon_group} is a dummy name */
2349 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2350 evsel
->group_name
= desc
[i
].name
;
2351 desc
[i
].name
= NULL
;
2353 evsel
->nr_members
= desc
[i
].nr_members
;
2355 if (i
>= nr_groups
|| nr
> 0) {
2356 pr_debug("invalid group desc\n");
2361 nr
= evsel
->nr_members
- 1;
2364 /* This is a group member */
2365 evsel
->leader
= leader
;
2371 if (i
!= nr_groups
|| nr
!= 0) {
2372 pr_debug("invalid group desc\n");
2378 for (i
= 0; i
< nr_groups
; i
++)
2379 zfree(&desc
[i
].name
);
2385 static int process_auxtrace(struct feat_fd
*ff
, void *data __maybe_unused
)
2387 struct perf_session
*session
;
2390 session
= container_of(ff
->ph
, struct perf_session
, header
);
2392 err
= auxtrace_index__process(ff
->fd
, ff
->size
, session
,
2393 ff
->ph
->needs_swap
);
2395 pr_err("Failed to process auxtrace index\n");
2399 static int process_cache(struct feat_fd
*ff
, void *data __maybe_unused
)
2401 struct cpu_cache_level
*caches
;
2402 u32 cnt
, i
, version
;
2404 if (do_read_u32(ff
, &version
))
2410 if (do_read_u32(ff
, &cnt
))
2413 caches
= zalloc(sizeof(*caches
) * cnt
);
2417 for (i
= 0; i
< cnt
; i
++) {
2418 struct cpu_cache_level c
;
2421 if (do_read_u32(ff, &c.v))\
2422 goto out_free_caches; \
2431 c.v = do_read_string(ff); \
2433 goto out_free_caches;
2443 ff
->ph
->env
.caches
= caches
;
2444 ff
->ph
->env
.caches_cnt
= cnt
;
2451 static int process_sample_time(struct feat_fd
*ff
, void *data __maybe_unused
)
2453 struct perf_session
*session
;
2454 u64 first_sample_time
, last_sample_time
;
2457 session
= container_of(ff
->ph
, struct perf_session
, header
);
2459 ret
= do_read_u64(ff
, &first_sample_time
);
2463 ret
= do_read_u64(ff
, &last_sample_time
);
2467 session
->evlist
->first_sample_time
= first_sample_time
;
2468 session
->evlist
->last_sample_time
= last_sample_time
;
2472 static int process_mem_topology(struct feat_fd
*ff
,
2473 void *data __maybe_unused
)
2475 struct memory_node
*nodes
;
2476 u64 version
, i
, nr
, bsize
;
2479 if (do_read_u64(ff
, &version
))
2485 if (do_read_u64(ff
, &bsize
))
2488 if (do_read_u64(ff
, &nr
))
2491 nodes
= zalloc(sizeof(*nodes
) * nr
);
2495 for (i
= 0; i
< nr
; i
++) {
2496 struct memory_node n
;
2499 if (do_read_u64(ff, &n.v)) \
2507 if (do_read_bitmap(ff
, &n
.set
, &n
.size
))
2513 ff
->ph
->env
.memory_bsize
= bsize
;
2514 ff
->ph
->env
.memory_nodes
= nodes
;
2515 ff
->ph
->env
.nr_memory_nodes
= nr
;
2524 static int process_clockid(struct feat_fd
*ff
,
2525 void *data __maybe_unused
)
2527 if (do_read_u64(ff
, &ff
->ph
->env
.clockid_res_ns
))
2533 static int process_dir_format(struct feat_fd
*ff
,
2534 void *_data __maybe_unused
)
2536 struct perf_session
*session
;
2537 struct perf_data
*data
;
2539 session
= container_of(ff
->ph
, struct perf_session
, header
);
2540 data
= session
->data
;
2542 if (WARN_ON(!perf_data__is_dir(data
)))
2545 return do_read_u64(ff
, &data
->dir
.version
);
2548 #ifdef HAVE_LIBBPF_SUPPORT
2549 static int process_bpf_prog_info(struct feat_fd
*ff
, void *data __maybe_unused
)
2551 struct bpf_prog_info_linear
*info_linear
;
2552 struct bpf_prog_info_node
*info_node
;
2553 struct perf_env
*env
= &ff
->ph
->env
;
2557 if (ff
->ph
->needs_swap
) {
2558 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2562 if (do_read_u32(ff
, &count
))
2565 down_write(&env
->bpf_progs
.lock
);
2567 for (i
= 0; i
< count
; ++i
) {
2568 u32 info_len
, data_len
;
2572 if (do_read_u32(ff
, &info_len
))
2574 if (do_read_u32(ff
, &data_len
))
2577 if (info_len
> sizeof(struct bpf_prog_info
)) {
2578 pr_warning("detected invalid bpf_prog_info\n");
2582 info_linear
= malloc(sizeof(struct bpf_prog_info_linear
) +
2586 info_linear
->info_len
= sizeof(struct bpf_prog_info
);
2587 info_linear
->data_len
= data_len
;
2588 if (do_read_u64(ff
, (u64
*)(&info_linear
->arrays
)))
2590 if (__do_read(ff
, &info_linear
->info
, info_len
))
2592 if (info_len
< sizeof(struct bpf_prog_info
))
2593 memset(((void *)(&info_linear
->info
)) + info_len
, 0,
2594 sizeof(struct bpf_prog_info
) - info_len
);
2596 if (__do_read(ff
, info_linear
->data
, data_len
))
2599 info_node
= malloc(sizeof(struct bpf_prog_info_node
));
2603 /* after reading from file, translate offset to address */
2604 bpf_program__bpil_offs_to_addr(info_linear
);
2605 info_node
->info_linear
= info_linear
;
2606 perf_env__insert_bpf_prog_info(env
, info_node
);
2609 up_write(&env
->bpf_progs
.lock
);
2614 up_write(&env
->bpf_progs
.lock
);
2617 #else // HAVE_LIBBPF_SUPPORT
2618 static int process_bpf_prog_info(struct feat_fd
*ff __maybe_unused
, void *data __maybe_unused
)
2622 #endif // HAVE_LIBBPF_SUPPORT
2624 static int process_bpf_btf(struct feat_fd
*ff
, void *data __maybe_unused
)
2626 struct perf_env
*env
= &ff
->ph
->env
;
2627 struct btf_node
*node
= NULL
;
2631 if (ff
->ph
->needs_swap
) {
2632 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2636 if (do_read_u32(ff
, &count
))
2639 down_write(&env
->bpf_progs
.lock
);
2641 for (i
= 0; i
< count
; ++i
) {
2644 if (do_read_u32(ff
, &id
))
2646 if (do_read_u32(ff
, &data_size
))
2649 node
= malloc(sizeof(struct btf_node
) + data_size
);
2654 node
->data_size
= data_size
;
2656 if (__do_read(ff
, node
->data
, data_size
))
2659 perf_env__insert_btf(env
, node
);
2665 up_write(&env
->bpf_progs
.lock
);
2670 struct feature_ops
{
2671 int (*write
)(struct feat_fd
*ff
, struct perf_evlist
*evlist
);
2672 void (*print
)(struct feat_fd
*ff
, FILE *fp
);
2673 int (*process
)(struct feat_fd
*ff
, void *data
);
2679 #define FEAT_OPR(n, func, __full_only) \
2681 .name = __stringify(n), \
2682 .write = write_##func, \
2683 .print = print_##func, \
2684 .full_only = __full_only, \
2685 .process = process_##func, \
2686 .synthesize = true \
2689 #define FEAT_OPN(n, func, __full_only) \
2691 .name = __stringify(n), \
2692 .write = write_##func, \
2693 .print = print_##func, \
2694 .full_only = __full_only, \
2695 .process = process_##func \
2698 /* feature_ops not implemented: */
2699 #define print_tracing_data NULL
2700 #define print_build_id NULL
2702 #define process_branch_stack NULL
2703 #define process_stat NULL
2706 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2707 FEAT_OPN(TRACING_DATA
, tracing_data
, false),
2708 FEAT_OPN(BUILD_ID
, build_id
, false),
2709 FEAT_OPR(HOSTNAME
, hostname
, false),
2710 FEAT_OPR(OSRELEASE
, osrelease
, false),
2711 FEAT_OPR(VERSION
, version
, false),
2712 FEAT_OPR(ARCH
, arch
, false),
2713 FEAT_OPR(NRCPUS
, nrcpus
, false),
2714 FEAT_OPR(CPUDESC
, cpudesc
, false),
2715 FEAT_OPR(CPUID
, cpuid
, false),
2716 FEAT_OPR(TOTAL_MEM
, total_mem
, false),
2717 FEAT_OPR(EVENT_DESC
, event_desc
, false),
2718 FEAT_OPR(CMDLINE
, cmdline
, false),
2719 FEAT_OPR(CPU_TOPOLOGY
, cpu_topology
, true),
2720 FEAT_OPR(NUMA_TOPOLOGY
, numa_topology
, true),
2721 FEAT_OPN(BRANCH_STACK
, branch_stack
, false),
2722 FEAT_OPR(PMU_MAPPINGS
, pmu_mappings
, false),
2723 FEAT_OPR(GROUP_DESC
, group_desc
, false),
2724 FEAT_OPN(AUXTRACE
, auxtrace
, false),
2725 FEAT_OPN(STAT
, stat
, false),
2726 FEAT_OPN(CACHE
, cache
, true),
2727 FEAT_OPR(SAMPLE_TIME
, sample_time
, false),
2728 FEAT_OPR(MEM_TOPOLOGY
, mem_topology
, true),
2729 FEAT_OPR(CLOCKID
, clockid
, false),
2730 FEAT_OPN(DIR_FORMAT
, dir_format
, false),
2731 FEAT_OPR(BPF_PROG_INFO
, bpf_prog_info
, false),
2732 FEAT_OPR(BPF_BTF
, bpf_btf
, false),
2735 struct header_print_data
{
2737 bool full
; /* extended list of headers */
2740 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2741 struct perf_header
*ph
,
2742 int feat
, int fd
, void *data
)
2744 struct header_print_data
*hd
= data
;
2747 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2748 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2749 "%d, continuing...\n", section
->offset
, feat
);
2752 if (feat
>= HEADER_LAST_FEATURE
) {
2753 pr_warning("unknown feature %d\n", feat
);
2756 if (!feat_ops
[feat
].print
)
2759 ff
= (struct feat_fd
) {
2764 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2765 feat_ops
[feat
].print(&ff
, hd
->fp
);
2767 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2768 feat_ops
[feat
].name
);
2773 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2775 struct header_print_data hd
;
2776 struct perf_header
*header
= &session
->header
;
2777 int fd
= perf_data__fd(session
->data
);
2785 ret
= fstat(fd
, &st
);
2789 stctime
= st
.st_ctime
;
2790 fprintf(fp
, "# captured on : %s", ctime(&stctime
));
2792 fprintf(fp
, "# header version : %u\n", header
->version
);
2793 fprintf(fp
, "# data offset : %" PRIu64
"\n", header
->data_offset
);
2794 fprintf(fp
, "# data size : %" PRIu64
"\n", header
->data_size
);
2795 fprintf(fp
, "# feat offset : %" PRIu64
"\n", header
->feat_offset
);
2797 perf_header__process_sections(header
, fd
, &hd
,
2798 perf_file_section__fprintf_info
);
2800 if (session
->data
->is_pipe
)
2803 fprintf(fp
, "# missing features: ");
2804 for_each_clear_bit(bit
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2806 fprintf(fp
, "%s ", feat_ops
[bit
].name
);
2813 static int do_write_feat(struct feat_fd
*ff
, int type
,
2814 struct perf_file_section
**p
,
2815 struct perf_evlist
*evlist
)
2820 if (perf_header__has_feat(ff
->ph
, type
)) {
2821 if (!feat_ops
[type
].write
)
2824 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
2827 (*p
)->offset
= lseek(ff
->fd
, 0, SEEK_CUR
);
2829 err
= feat_ops
[type
].write(ff
, evlist
);
2831 pr_debug("failed to write feature %s\n", feat_ops
[type
].name
);
2833 /* undo anything written */
2834 lseek(ff
->fd
, (*p
)->offset
, SEEK_SET
);
2838 (*p
)->size
= lseek(ff
->fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2844 static int perf_header__adds_write(struct perf_header
*header
,
2845 struct perf_evlist
*evlist
, int fd
)
2849 struct perf_file_section
*feat_sec
, *p
;
2855 ff
= (struct feat_fd
){
2860 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2864 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2865 if (feat_sec
== NULL
)
2868 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2870 sec_start
= header
->feat_offset
;
2871 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2873 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2874 if (do_write_feat(&ff
, feat
, &p
, evlist
))
2875 perf_header__clear_feat(header
, feat
);
2878 lseek(fd
, sec_start
, SEEK_SET
);
2880 * may write more than needed due to dropped feature, but
2881 * this is okay, reader will skip the missing entries
2883 err
= do_write(&ff
, feat_sec
, sec_size
);
2885 pr_debug("failed to write feature section\n");
2890 int perf_header__write_pipe(int fd
)
2892 struct perf_pipe_file_header f_header
;
2896 ff
= (struct feat_fd
){ .fd
= fd
};
2898 f_header
= (struct perf_pipe_file_header
){
2899 .magic
= PERF_MAGIC
,
2900 .size
= sizeof(f_header
),
2903 err
= do_write(&ff
, &f_header
, sizeof(f_header
));
2905 pr_debug("failed to write perf pipe header\n");
2912 int perf_session__write_header(struct perf_session
*session
,
2913 struct perf_evlist
*evlist
,
2914 int fd
, bool at_exit
)
2916 struct perf_file_header f_header
;
2917 struct perf_file_attr f_attr
;
2918 struct perf_header
*header
= &session
->header
;
2919 struct perf_evsel
*evsel
;
2924 ff
= (struct feat_fd
){ .fd
= fd
};
2925 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2927 evlist__for_each_entry(session
->evlist
, evsel
) {
2928 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2929 err
= do_write(&ff
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2931 pr_debug("failed to write perf header\n");
2936 attr_offset
= lseek(ff
.fd
, 0, SEEK_CUR
);
2938 evlist__for_each_entry(evlist
, evsel
) {
2939 f_attr
= (struct perf_file_attr
){
2940 .attr
= evsel
->attr
,
2942 .offset
= evsel
->id_offset
,
2943 .size
= evsel
->ids
* sizeof(u64
),
2946 err
= do_write(&ff
, &f_attr
, sizeof(f_attr
));
2948 pr_debug("failed to write perf header attribute\n");
2953 if (!header
->data_offset
)
2954 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2955 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2958 err
= perf_header__adds_write(header
, evlist
, fd
);
2963 f_header
= (struct perf_file_header
){
2964 .magic
= PERF_MAGIC
,
2965 .size
= sizeof(f_header
),
2966 .attr_size
= sizeof(f_attr
),
2968 .offset
= attr_offset
,
2969 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2972 .offset
= header
->data_offset
,
2973 .size
= header
->data_size
,
2975 /* event_types is ignored, store zeros */
2978 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2980 lseek(fd
, 0, SEEK_SET
);
2981 err
= do_write(&ff
, &f_header
, sizeof(f_header
));
2983 pr_debug("failed to write perf header\n");
2986 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2991 static int perf_header__getbuffer64(struct perf_header
*header
,
2992 int fd
, void *buf
, size_t size
)
2994 if (readn(fd
, buf
, size
) <= 0)
2997 if (header
->needs_swap
)
2998 mem_bswap_64(buf
, size
);
3003 int perf_header__process_sections(struct perf_header
*header
, int fd
,
3005 int (*process
)(struct perf_file_section
*section
,
3006 struct perf_header
*ph
,
3007 int feat
, int fd
, void *data
))
3009 struct perf_file_section
*feat_sec
, *sec
;
3015 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
3019 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
3023 sec_size
= sizeof(*feat_sec
) * nr_sections
;
3025 lseek(fd
, header
->feat_offset
, SEEK_SET
);
3027 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
3031 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
3032 err
= process(sec
++, header
, feat
, fd
, data
);
3042 static const int attr_file_abi_sizes
[] = {
3043 [0] = PERF_ATTR_SIZE_VER0
,
3044 [1] = PERF_ATTR_SIZE_VER1
,
3045 [2] = PERF_ATTR_SIZE_VER2
,
3046 [3] = PERF_ATTR_SIZE_VER3
,
3047 [4] = PERF_ATTR_SIZE_VER4
,
3052 * In the legacy file format, the magic number is not used to encode endianness.
3053 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3054 * on ABI revisions, we need to try all combinations for all endianness to
3055 * detect the endianness.
3057 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
3059 uint64_t ref_size
, attr_size
;
3062 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
3063 ref_size
= attr_file_abi_sizes
[i
]
3064 + sizeof(struct perf_file_section
);
3065 if (hdr_sz
!= ref_size
) {
3066 attr_size
= bswap_64(hdr_sz
);
3067 if (attr_size
!= ref_size
)
3070 ph
->needs_swap
= true;
3072 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3077 /* could not determine endianness */
3081 #define PERF_PIPE_HDR_VER0 16
3083 static const size_t attr_pipe_abi_sizes
[] = {
3084 [0] = PERF_PIPE_HDR_VER0
,
3089 * In the legacy pipe format, there is an implicit assumption that endiannesss
3090 * between host recording the samples, and host parsing the samples is the
3091 * same. This is not always the case given that the pipe output may always be
3092 * redirected into a file and analyzed on a different machine with possibly a
3093 * different endianness and perf_event ABI revsions in the perf tool itself.
3095 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
3100 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
3101 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
3102 attr_size
= bswap_64(hdr_sz
);
3103 if (attr_size
!= hdr_sz
)
3106 ph
->needs_swap
= true;
3108 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
3114 bool is_perf_magic(u64 magic
)
3116 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
3117 || magic
== __perf_magic2
3118 || magic
== __perf_magic2_sw
)
3124 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
3125 bool is_pipe
, struct perf_header
*ph
)
3129 /* check for legacy format */
3130 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
3132 ph
->version
= PERF_HEADER_VERSION_1
;
3133 pr_debug("legacy perf.data format\n");
3135 return try_all_pipe_abis(hdr_sz
, ph
);
3137 return try_all_file_abis(hdr_sz
, ph
);
3140 * the new magic number serves two purposes:
3141 * - unique number to identify actual perf.data files
3142 * - encode endianness of file
3144 ph
->version
= PERF_HEADER_VERSION_2
;
3146 /* check magic number with one endianness */
3147 if (magic
== __perf_magic2
)
3150 /* check magic number with opposite endianness */
3151 if (magic
!= __perf_magic2_sw
)
3154 ph
->needs_swap
= true;
3159 int perf_file_header__read(struct perf_file_header
*header
,
3160 struct perf_header
*ph
, int fd
)
3164 lseek(fd
, 0, SEEK_SET
);
3166 ret
= readn(fd
, header
, sizeof(*header
));
3170 if (check_magic_endian(header
->magic
,
3171 header
->attr_size
, false, ph
) < 0) {
3172 pr_debug("magic/endian check failed\n");
3176 if (ph
->needs_swap
) {
3177 mem_bswap_64(header
, offsetof(struct perf_file_header
,
3181 if (header
->size
!= sizeof(*header
)) {
3182 /* Support the previous format */
3183 if (header
->size
== offsetof(typeof(*header
), adds_features
))
3184 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
3187 } else if (ph
->needs_swap
) {
3189 * feature bitmap is declared as an array of unsigned longs --
3190 * not good since its size can differ between the host that
3191 * generated the data file and the host analyzing the file.
3193 * We need to handle endianness, but we don't know the size of
3194 * the unsigned long where the file was generated. Take a best
3195 * guess at determining it: try 64-bit swap first (ie., file
3196 * created on a 64-bit host), and check if the hostname feature
3197 * bit is set (this feature bit is forced on as of fbe96f2).
3198 * If the bit is not, undo the 64-bit swap and try a 32-bit
3199 * swap. If the hostname bit is still not set (e.g., older data
3200 * file), punt and fallback to the original behavior --
3201 * clearing all feature bits and setting buildid.
3203 mem_bswap_64(&header
->adds_features
,
3204 BITS_TO_U64(HEADER_FEAT_BITS
));
3206 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
3208 mem_bswap_64(&header
->adds_features
,
3209 BITS_TO_U64(HEADER_FEAT_BITS
));
3212 mem_bswap_32(&header
->adds_features
,
3213 BITS_TO_U32(HEADER_FEAT_BITS
));
3216 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
3217 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
3218 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
3222 memcpy(&ph
->adds_features
, &header
->adds_features
,
3223 sizeof(ph
->adds_features
));
3225 ph
->data_offset
= header
->data
.offset
;
3226 ph
->data_size
= header
->data
.size
;
3227 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
3231 static int perf_file_section__process(struct perf_file_section
*section
,
3232 struct perf_header
*ph
,
3233 int feat
, int fd
, void *data
)
3235 struct feat_fd fdd
= {
3238 .size
= section
->size
,
3239 .offset
= section
->offset
,
3242 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
3243 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
3244 "%d, continuing...\n", section
->offset
, feat
);
3248 if (feat
>= HEADER_LAST_FEATURE
) {
3249 pr_debug("unknown feature %d, continuing...\n", feat
);
3253 if (!feat_ops
[feat
].process
)
3256 return feat_ops
[feat
].process(&fdd
, data
);
3259 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
3260 struct perf_header
*ph
, int fd
,
3263 struct feat_fd ff
= {
3264 .fd
= STDOUT_FILENO
,
3269 ret
= readn(fd
, header
, sizeof(*header
));
3273 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
3274 pr_debug("endian/magic failed\n");
3279 header
->size
= bswap_64(header
->size
);
3281 if (repipe
&& do_write(&ff
, header
, sizeof(*header
)) < 0)
3287 static int perf_header__read_pipe(struct perf_session
*session
)
3289 struct perf_header
*header
= &session
->header
;
3290 struct perf_pipe_file_header f_header
;
3292 if (perf_file_header__read_pipe(&f_header
, header
,
3293 perf_data__fd(session
->data
),
3294 session
->repipe
) < 0) {
3295 pr_debug("incompatible file format\n");
3302 static int read_attr(int fd
, struct perf_header
*ph
,
3303 struct perf_file_attr
*f_attr
)
3305 struct perf_event_attr
*attr
= &f_attr
->attr
;
3307 size_t our_sz
= sizeof(f_attr
->attr
);
3310 memset(f_attr
, 0, sizeof(*f_attr
));
3312 /* read minimal guaranteed structure */
3313 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
3315 pr_debug("cannot read %d bytes of header attr\n",
3316 PERF_ATTR_SIZE_VER0
);
3320 /* on file perf_event_attr size */
3328 sz
= PERF_ATTR_SIZE_VER0
;
3329 } else if (sz
> our_sz
) {
3330 pr_debug("file uses a more recent and unsupported ABI"
3331 " (%zu bytes extra)\n", sz
- our_sz
);
3334 /* what we have not yet read and that we know about */
3335 left
= sz
- PERF_ATTR_SIZE_VER0
;
3338 ptr
+= PERF_ATTR_SIZE_VER0
;
3340 ret
= readn(fd
, ptr
, left
);
3342 /* read perf_file_section, ids are read in caller */
3343 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
3345 return ret
<= 0 ? -1 : 0;
3348 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
3349 struct tep_handle
*pevent
)
3351 struct tep_event
*event
;
3354 /* already prepared */
3355 if (evsel
->tp_format
)
3358 if (pevent
== NULL
) {
3359 pr_debug("broken or missing trace data\n");
3363 event
= tep_find_event(pevent
, evsel
->attr
.config
);
3364 if (event
== NULL
) {
3365 pr_debug("cannot find event format for %d\n", (int)evsel
->attr
.config
);
3370 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
3371 evsel
->name
= strdup(bf
);
3372 if (evsel
->name
== NULL
)
3376 evsel
->tp_format
= event
;
3380 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
3381 struct tep_handle
*pevent
)
3383 struct perf_evsel
*pos
;
3385 evlist__for_each_entry(evlist
, pos
) {
3386 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
3387 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
3394 int perf_session__read_header(struct perf_session
*session
)
3396 struct perf_data
*data
= session
->data
;
3397 struct perf_header
*header
= &session
->header
;
3398 struct perf_file_header f_header
;
3399 struct perf_file_attr f_attr
;
3401 int nr_attrs
, nr_ids
, i
, j
;
3402 int fd
= perf_data__fd(data
);
3404 session
->evlist
= perf_evlist__new();
3405 if (session
->evlist
== NULL
)
3408 session
->evlist
->env
= &header
->env
;
3409 session
->machines
.host
.env
= &header
->env
;
3410 if (perf_data__is_pipe(data
))
3411 return perf_header__read_pipe(session
);
3413 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
3417 * Sanity check that perf.data was written cleanly; data size is
3418 * initialized to 0 and updated only if the on_exit function is run.
3419 * If data size is still 0 then the file contains only partial
3420 * information. Just warn user and process it as much as it can.
3422 if (f_header
.data
.size
== 0) {
3423 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3424 "Was the 'perf record' command properly terminated?\n",
3428 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
3429 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
3431 for (i
= 0; i
< nr_attrs
; i
++) {
3432 struct perf_evsel
*evsel
;
3435 if (read_attr(fd
, header
, &f_attr
) < 0)
3438 if (header
->needs_swap
) {
3439 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
3440 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
3441 perf_event__attr_swap(&f_attr
.attr
);
3444 tmp
= lseek(fd
, 0, SEEK_CUR
);
3445 evsel
= perf_evsel__new(&f_attr
.attr
);
3448 goto out_delete_evlist
;
3450 evsel
->needs_swap
= header
->needs_swap
;
3452 * Do it before so that if perf_evsel__alloc_id fails, this
3453 * entry gets purged too at perf_evlist__delete().
3455 perf_evlist__add(session
->evlist
, evsel
);
3457 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
3459 * We don't have the cpu and thread maps on the header, so
3460 * for allocating the perf_sample_id table we fake 1 cpu and
3461 * hattr->ids threads.
3463 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
3464 goto out_delete_evlist
;
3466 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
3468 for (j
= 0; j
< nr_ids
; j
++) {
3469 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
3472 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
3475 lseek(fd
, tmp
, SEEK_SET
);
3478 perf_header__process_sections(header
, fd
, &session
->tevent
,
3479 perf_file_section__process
);
3481 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
3482 session
->tevent
.pevent
))
3483 goto out_delete_evlist
;
3490 perf_evlist__delete(session
->evlist
);
3491 session
->evlist
= NULL
;
3495 int perf_event__synthesize_attr(struct perf_tool
*tool
,
3496 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
3497 perf_event__handler_t process
)
3499 union perf_event
*ev
;
3503 size
= sizeof(struct perf_event_attr
);
3504 size
= PERF_ALIGN(size
, sizeof(u64
));
3505 size
+= sizeof(struct perf_event_header
);
3506 size
+= ids
* sizeof(u64
);
3513 ev
->attr
.attr
= *attr
;
3514 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
3516 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
3517 ev
->attr
.header
.size
= (u16
)size
;
3519 if (ev
->attr
.header
.size
== size
)
3520 err
= process(tool
, ev
, NULL
, NULL
);
3529 int perf_event__synthesize_features(struct perf_tool
*tool
,
3530 struct perf_session
*session
,
3531 struct perf_evlist
*evlist
,
3532 perf_event__handler_t process
)
3534 struct perf_header
*header
= &session
->header
;
3536 struct feature_event
*fe
;
3540 sz_hdr
= sizeof(fe
->header
);
3541 sz
= sizeof(union perf_event
);
3542 /* get a nice alignment */
3543 sz
= PERF_ALIGN(sz
, page_size
);
3545 memset(&ff
, 0, sizeof(ff
));
3547 ff
.buf
= malloc(sz
);
3551 ff
.size
= sz
- sz_hdr
;
3553 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
3554 if (!feat_ops
[feat
].synthesize
) {
3555 pr_debug("No record header feature for header :%d\n", feat
);
3559 ff
.offset
= sizeof(*fe
);
3561 ret
= feat_ops
[feat
].write(&ff
, evlist
);
3562 if (ret
|| ff
.offset
<= (ssize_t
)sizeof(*fe
)) {
3563 pr_debug("Error writing feature\n");
3566 /* ff.buf may have changed due to realloc in do_write() */
3568 memset(fe
, 0, sizeof(*fe
));
3571 fe
->header
.type
= PERF_RECORD_HEADER_FEATURE
;
3572 fe
->header
.size
= ff
.offset
;
3574 ret
= process(tool
, ff
.buf
, NULL
, NULL
);
3581 /* Send HEADER_LAST_FEATURE mark. */
3583 fe
->feat_id
= HEADER_LAST_FEATURE
;
3584 fe
->header
.type
= PERF_RECORD_HEADER_FEATURE
;
3585 fe
->header
.size
= sizeof(*fe
);
3587 ret
= process(tool
, ff
.buf
, NULL
, NULL
);
3593 int perf_event__process_feature(struct perf_session
*session
,
3594 union perf_event
*event
)
3596 struct perf_tool
*tool
= session
->tool
;
3597 struct feat_fd ff
= { .fd
= 0 };
3598 struct feature_event
*fe
= (struct feature_event
*)event
;
3599 int type
= fe
->header
.type
;
3600 u64 feat
= fe
->feat_id
;
3602 if (type
< 0 || type
>= PERF_RECORD_HEADER_MAX
) {
3603 pr_warning("invalid record type %d in pipe-mode\n", type
);
3606 if (feat
== HEADER_RESERVED
|| feat
>= HEADER_LAST_FEATURE
) {
3607 pr_warning("invalid record type %d in pipe-mode\n", type
);
3611 if (!feat_ops
[feat
].process
)
3614 ff
.buf
= (void *)fe
->data
;
3615 ff
.size
= event
->header
.size
- sizeof(event
->header
);
3616 ff
.ph
= &session
->header
;
3618 if (feat_ops
[feat
].process(&ff
, NULL
))
3621 if (!feat_ops
[feat
].print
|| !tool
->show_feat_hdr
)
3624 if (!feat_ops
[feat
].full_only
||
3625 tool
->show_feat_hdr
>= SHOW_FEAT_HEADER_FULL_INFO
) {
3626 feat_ops
[feat
].print(&ff
, stdout
);
3628 fprintf(stdout
, "# %s info available, use -I to display\n",
3629 feat_ops
[feat
].name
);
3635 static struct event_update_event
*
3636 event_update_event__new(size_t size
, u64 type
, u64 id
)
3638 struct event_update_event
*ev
;
3640 size
+= sizeof(*ev
);
3641 size
= PERF_ALIGN(size
, sizeof(u64
));
3645 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3646 ev
->header
.size
= (u16
)size
;
3654 perf_event__synthesize_event_update_unit(struct perf_tool
*tool
,
3655 struct perf_evsel
*evsel
,
3656 perf_event__handler_t process
)
3658 struct event_update_event
*ev
;
3659 size_t size
= strlen(evsel
->unit
);
3662 ev
= event_update_event__new(size
+ 1, PERF_EVENT_UPDATE__UNIT
, evsel
->id
[0]);
3666 strlcpy(ev
->data
, evsel
->unit
, size
+ 1);
3667 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
3673 perf_event__synthesize_event_update_scale(struct perf_tool
*tool
,
3674 struct perf_evsel
*evsel
,
3675 perf_event__handler_t process
)
3677 struct event_update_event
*ev
;
3678 struct event_update_event_scale
*ev_data
;
3681 ev
= event_update_event__new(sizeof(*ev_data
), PERF_EVENT_UPDATE__SCALE
, evsel
->id
[0]);
3685 ev_data
= (struct event_update_event_scale
*) ev
->data
;
3686 ev_data
->scale
= evsel
->scale
;
3687 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3693 perf_event__synthesize_event_update_name(struct perf_tool
*tool
,
3694 struct perf_evsel
*evsel
,
3695 perf_event__handler_t process
)
3697 struct event_update_event
*ev
;
3698 size_t len
= strlen(evsel
->name
);
3701 ev
= event_update_event__new(len
+ 1, PERF_EVENT_UPDATE__NAME
, evsel
->id
[0]);
3705 strlcpy(ev
->data
, evsel
->name
, len
+ 1);
3706 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3712 perf_event__synthesize_event_update_cpus(struct perf_tool
*tool
,
3713 struct perf_evsel
*evsel
,
3714 perf_event__handler_t process
)
3716 size_t size
= sizeof(struct event_update_event
);
3717 struct event_update_event
*ev
;
3721 if (!evsel
->own_cpus
)
3724 ev
= cpu_map_data__alloc(evsel
->own_cpus
, &size
, &type
, &max
);
3728 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3729 ev
->header
.size
= (u16
)size
;
3730 ev
->type
= PERF_EVENT_UPDATE__CPUS
;
3731 ev
->id
= evsel
->id
[0];
3733 cpu_map_data__synthesize((struct cpu_map_data
*) ev
->data
,
3737 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3742 size_t perf_event__fprintf_event_update(union perf_event
*event
, FILE *fp
)
3744 struct event_update_event
*ev
= &event
->event_update
;
3745 struct event_update_event_scale
*ev_scale
;
3746 struct event_update_event_cpus
*ev_cpus
;
3747 struct cpu_map
*map
;
3750 ret
= fprintf(fp
, "\n... id: %" PRIu64
"\n", ev
->id
);
3753 case PERF_EVENT_UPDATE__SCALE
:
3754 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3755 ret
+= fprintf(fp
, "... scale: %f\n", ev_scale
->scale
);
3757 case PERF_EVENT_UPDATE__UNIT
:
3758 ret
+= fprintf(fp
, "... unit: %s\n", ev
->data
);
3760 case PERF_EVENT_UPDATE__NAME
:
3761 ret
+= fprintf(fp
, "... name: %s\n", ev
->data
);
3763 case PERF_EVENT_UPDATE__CPUS
:
3764 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3765 ret
+= fprintf(fp
, "... ");
3767 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3769 ret
+= cpu_map__fprintf(map
, fp
);
3771 ret
+= fprintf(fp
, "failed to get cpus\n");
3774 ret
+= fprintf(fp
, "... unknown type\n");
3781 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
3782 struct perf_evlist
*evlist
,
3783 perf_event__handler_t process
)
3785 struct perf_evsel
*evsel
;
3788 evlist__for_each_entry(evlist
, evsel
) {
3789 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
3790 evsel
->id
, process
);
3792 pr_debug("failed to create perf header attribute\n");
3800 static bool has_unit(struct perf_evsel
*counter
)
3802 return counter
->unit
&& *counter
->unit
;
3805 static bool has_scale(struct perf_evsel
*counter
)
3807 return counter
->scale
!= 1;
3810 int perf_event__synthesize_extra_attr(struct perf_tool
*tool
,
3811 struct perf_evlist
*evsel_list
,
3812 perf_event__handler_t process
,
3815 struct perf_evsel
*counter
;
3819 * Synthesize other events stuff not carried within
3820 * attr event - unit, scale, name
3822 evlist__for_each_entry(evsel_list
, counter
) {
3823 if (!counter
->supported
)
3827 * Synthesize unit and scale only if it's defined.
3829 if (has_unit(counter
)) {
3830 err
= perf_event__synthesize_event_update_unit(tool
, counter
, process
);
3832 pr_err("Couldn't synthesize evsel unit.\n");
3837 if (has_scale(counter
)) {
3838 err
= perf_event__synthesize_event_update_scale(tool
, counter
, process
);
3840 pr_err("Couldn't synthesize evsel counter.\n");
3845 if (counter
->own_cpus
) {
3846 err
= perf_event__synthesize_event_update_cpus(tool
, counter
, process
);
3848 pr_err("Couldn't synthesize evsel cpus.\n");
3854 * Name is needed only for pipe output,
3855 * perf.data carries event names.
3858 err
= perf_event__synthesize_event_update_name(tool
, counter
, process
);
3860 pr_err("Couldn't synthesize evsel name.\n");
3868 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
3869 union perf_event
*event
,
3870 struct perf_evlist
**pevlist
)
3873 struct perf_evsel
*evsel
;
3874 struct perf_evlist
*evlist
= *pevlist
;
3876 if (evlist
== NULL
) {
3877 *pevlist
= evlist
= perf_evlist__new();
3882 evsel
= perf_evsel__new(&event
->attr
.attr
);
3886 perf_evlist__add(evlist
, evsel
);
3888 ids
= event
->header
.size
;
3889 ids
-= (void *)&event
->attr
.id
- (void *)event
;
3890 n_ids
= ids
/ sizeof(u64
);
3892 * We don't have the cpu and thread maps on the header, so
3893 * for allocating the perf_sample_id table we fake 1 cpu and
3894 * hattr->ids threads.
3896 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
3899 for (i
= 0; i
< n_ids
; i
++) {
3900 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
3906 int perf_event__process_event_update(struct perf_tool
*tool __maybe_unused
,
3907 union perf_event
*event
,
3908 struct perf_evlist
**pevlist
)
3910 struct event_update_event
*ev
= &event
->event_update
;
3911 struct event_update_event_scale
*ev_scale
;
3912 struct event_update_event_cpus
*ev_cpus
;
3913 struct perf_evlist
*evlist
;
3914 struct perf_evsel
*evsel
;
3915 struct cpu_map
*map
;
3917 if (!pevlist
|| *pevlist
== NULL
)
3922 evsel
= perf_evlist__id2evsel(evlist
, ev
->id
);
3927 case PERF_EVENT_UPDATE__UNIT
:
3928 evsel
->unit
= strdup(ev
->data
);
3930 case PERF_EVENT_UPDATE__NAME
:
3931 evsel
->name
= strdup(ev
->data
);
3933 case PERF_EVENT_UPDATE__SCALE
:
3934 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3935 evsel
->scale
= ev_scale
->scale
;
3937 case PERF_EVENT_UPDATE__CPUS
:
3938 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3940 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3942 evsel
->own_cpus
= map
;
3944 pr_err("failed to get event_update cpus\n");
3952 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
3953 struct perf_evlist
*evlist
,
3954 perf_event__handler_t process
)
3956 union perf_event ev
;
3957 struct tracing_data
*tdata
;
3958 ssize_t size
= 0, aligned_size
= 0, padding
;
3960 int err __maybe_unused
= 0;
3963 * We are going to store the size of the data followed
3964 * by the data contents. Since the fd descriptor is a pipe,
3965 * we cannot seek back to store the size of the data once
3966 * we know it. Instead we:
3968 * - write the tracing data to the temp file
3969 * - get/write the data size to pipe
3970 * - write the tracing data from the temp file
3973 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3977 memset(&ev
, 0, sizeof(ev
));
3979 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3981 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3982 padding
= aligned_size
- size
;
3983 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3984 ev
.tracing_data
.size
= aligned_size
;
3986 process(tool
, &ev
, NULL
, NULL
);
3989 * The put function will copy all the tracing data
3990 * stored in temp file to the pipe.
3992 tracing_data_put(tdata
);
3994 ff
= (struct feat_fd
){ .fd
= fd
};
3995 if (write_padded(&ff
, NULL
, 0, padding
))
3998 return aligned_size
;
4001 int perf_event__process_tracing_data(struct perf_session
*session
,
4002 union perf_event
*event
)
4004 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
4005 int fd
= perf_data__fd(session
->data
);
4006 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
4009 /* setup for reading amidst mmap */
4010 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
4013 size_read
= trace_report(fd
, &session
->tevent
,
4015 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
4017 if (readn(fd
, buf
, padding
) < 0) {
4018 pr_err("%s: reading input file", __func__
);
4021 if (session
->repipe
) {
4022 int retw
= write(STDOUT_FILENO
, buf
, padding
);
4023 if (retw
<= 0 || retw
!= padding
) {
4024 pr_err("%s: repiping tracing data padding", __func__
);
4029 if (size_read
+ padding
!= size
) {
4030 pr_err("%s: tracing data size mismatch", __func__
);
4034 perf_evlist__prepare_tracepoint_events(session
->evlist
,
4035 session
->tevent
.pevent
);
4037 return size_read
+ padding
;
4040 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
4041 struct dso
*pos
, u16 misc
,
4042 perf_event__handler_t process
,
4043 struct machine
*machine
)
4045 union perf_event ev
;
4052 memset(&ev
, 0, sizeof(ev
));
4054 len
= pos
->long_name_len
+ 1;
4055 len
= PERF_ALIGN(len
, NAME_ALIGN
);
4056 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
4057 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
4058 ev
.build_id
.header
.misc
= misc
;
4059 ev
.build_id
.pid
= machine
->pid
;
4060 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
4061 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
4063 err
= process(tool
, &ev
, NULL
, machine
);
4068 int perf_event__process_build_id(struct perf_session
*session
,
4069 union perf_event
*event
)
4071 __event_process_build_id(&event
->build_id
,
4072 event
->build_id
.filename
,