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>
26 #include "trace-event.h"
36 #include <api/fs/fs.h>
39 #include "time-utils.h"
41 #include "sane_ctype.h"
45 * must be a numerical value to let the endianness
46 * determine the memory layout. That way we are able
47 * to detect endianness when reading the perf.data file
50 * we check for legacy (PERFFILE) format.
52 static const char *__perf_magic1
= "PERFFILE";
53 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
54 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
56 #define PERF_MAGIC __perf_magic2
58 const char perf_version_string
[] = PERF_VERSION
;
60 struct perf_file_attr
{
61 struct perf_event_attr attr
;
62 struct perf_file_section ids
;
66 struct perf_header
*ph
;
68 void *buf
; /* Either buf != NULL or fd >= 0 */
71 struct perf_evsel
*events
;
74 void perf_header__set_feat(struct perf_header
*header
, int feat
)
76 set_bit(feat
, header
->adds_features
);
79 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
81 clear_bit(feat
, header
->adds_features
);
84 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
86 return test_bit(feat
, header
->adds_features
);
89 static int __do_write_fd(struct feat_fd
*ff
, const void *buf
, size_t size
)
91 ssize_t ret
= writen(ff
->fd
, buf
, size
);
93 if (ret
!= (ssize_t
)size
)
94 return ret
< 0 ? (int)ret
: -1;
98 static int __do_write_buf(struct feat_fd
*ff
, const void *buf
, size_t size
)
100 /* struct perf_event_header::size is u16 */
101 const size_t max_size
= 0xffff - sizeof(struct perf_event_header
);
102 size_t new_size
= ff
->size
;
105 if (size
+ ff
->offset
> max_size
)
108 while (size
> (new_size
- ff
->offset
))
110 new_size
= min(max_size
, new_size
);
112 if (ff
->size
< new_size
) {
113 addr
= realloc(ff
->buf
, new_size
);
120 memcpy(ff
->buf
+ ff
->offset
, buf
, size
);
126 /* Return: 0 if succeded, -ERR if failed. */
127 int do_write(struct feat_fd
*ff
, const void *buf
, size_t size
)
130 return __do_write_fd(ff
, buf
, size
);
131 return __do_write_buf(ff
, buf
, size
);
134 /* Return: 0 if succeded, -ERR if failed. */
135 int write_padded(struct feat_fd
*ff
, const void *bf
,
136 size_t count
, size_t count_aligned
)
138 static const char zero_buf
[NAME_ALIGN
];
139 int err
= do_write(ff
, bf
, count
);
142 err
= do_write(ff
, zero_buf
, count_aligned
- count
);
147 #define string_size(str) \
148 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
150 /* Return: 0 if succeded, -ERR if failed. */
151 static int do_write_string(struct feat_fd
*ff
, const char *str
)
156 olen
= strlen(str
) + 1;
157 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
159 /* write len, incl. \0 */
160 ret
= do_write(ff
, &len
, sizeof(len
));
164 return write_padded(ff
, str
, olen
, len
);
167 static int __do_read_fd(struct feat_fd
*ff
, void *addr
, ssize_t size
)
169 ssize_t ret
= readn(ff
->fd
, addr
, size
);
172 return ret
< 0 ? (int)ret
: -1;
176 static int __do_read_buf(struct feat_fd
*ff
, void *addr
, ssize_t size
)
178 if (size
> (ssize_t
)ff
->size
- ff
->offset
)
181 memcpy(addr
, ff
->buf
+ ff
->offset
, size
);
188 static int __do_read(struct feat_fd
*ff
, void *addr
, ssize_t size
)
191 return __do_read_fd(ff
, addr
, size
);
192 return __do_read_buf(ff
, addr
, size
);
195 static int do_read_u32(struct feat_fd
*ff
, u32
*addr
)
199 ret
= __do_read(ff
, addr
, sizeof(*addr
));
203 if (ff
->ph
->needs_swap
)
204 *addr
= bswap_32(*addr
);
208 static int do_read_u64(struct feat_fd
*ff
, u64
*addr
)
212 ret
= __do_read(ff
, addr
, sizeof(*addr
));
216 if (ff
->ph
->needs_swap
)
217 *addr
= bswap_64(*addr
);
221 static char *do_read_string(struct feat_fd
*ff
)
226 if (do_read_u32(ff
, &len
))
233 if (!__do_read(ff
, buf
, len
)) {
235 * strings are padded by zeroes
236 * thus the actual strlen of buf
237 * may be less than len
246 static int write_tracing_data(struct feat_fd
*ff
,
247 struct perf_evlist
*evlist
)
249 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
252 return read_tracing_data(ff
->fd
, &evlist
->entries
);
255 static int write_build_id(struct feat_fd
*ff
,
256 struct perf_evlist
*evlist __maybe_unused
)
258 struct perf_session
*session
;
261 session
= container_of(ff
->ph
, struct perf_session
, header
);
263 if (!perf_session__read_build_ids(session
, true))
266 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
269 err
= perf_session__write_buildid_table(session
, ff
);
271 pr_debug("failed to write buildid table\n");
274 perf_session__cache_build_ids(session
);
279 static int write_hostname(struct feat_fd
*ff
,
280 struct perf_evlist
*evlist __maybe_unused
)
289 return do_write_string(ff
, uts
.nodename
);
292 static int write_osrelease(struct feat_fd
*ff
,
293 struct perf_evlist
*evlist __maybe_unused
)
302 return do_write_string(ff
, uts
.release
);
305 static int write_arch(struct feat_fd
*ff
,
306 struct perf_evlist
*evlist __maybe_unused
)
315 return do_write_string(ff
, uts
.machine
);
318 static int write_version(struct feat_fd
*ff
,
319 struct perf_evlist
*evlist __maybe_unused
)
321 return do_write_string(ff
, perf_version_string
);
324 static int __write_cpudesc(struct feat_fd
*ff
, const char *cpuinfo_proc
)
329 const char *search
= cpuinfo_proc
;
336 file
= fopen("/proc/cpuinfo", "r");
340 while (getline(&buf
, &len
, file
) > 0) {
341 ret
= strncmp(buf
, search
, strlen(search
));
353 p
= strchr(buf
, ':');
354 if (p
&& *(p
+1) == ' ' && *(p
+2))
360 /* squash extra space characters (branding string) */
367 while (*q
&& isspace(*q
))
370 while ((*r
++ = *q
++));
374 ret
= do_write_string(ff
, s
);
381 static int write_cpudesc(struct feat_fd
*ff
,
382 struct perf_evlist
*evlist __maybe_unused
)
384 const char *cpuinfo_procs
[] = CPUINFO_PROC
;
387 for (i
= 0; i
< ARRAY_SIZE(cpuinfo_procs
); i
++) {
389 ret
= __write_cpudesc(ff
, cpuinfo_procs
[i
]);
397 static int write_nrcpus(struct feat_fd
*ff
,
398 struct perf_evlist
*evlist __maybe_unused
)
404 nrc
= cpu__max_present_cpu();
406 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
410 nra
= (u32
)(nr
& UINT_MAX
);
412 ret
= do_write(ff
, &nrc
, sizeof(nrc
));
416 return do_write(ff
, &nra
, sizeof(nra
));
419 static int write_event_desc(struct feat_fd
*ff
,
420 struct perf_evlist
*evlist
)
422 struct perf_evsel
*evsel
;
426 nre
= evlist
->nr_entries
;
429 * write number of events
431 ret
= do_write(ff
, &nre
, sizeof(nre
));
436 * size of perf_event_attr struct
438 sz
= (u32
)sizeof(evsel
->attr
);
439 ret
= do_write(ff
, &sz
, sizeof(sz
));
443 evlist__for_each_entry(evlist
, evsel
) {
444 ret
= do_write(ff
, &evsel
->attr
, sz
);
448 * write number of unique id per event
449 * there is one id per instance of an event
451 * copy into an nri to be independent of the
455 ret
= do_write(ff
, &nri
, sizeof(nri
));
460 * write event string as passed on cmdline
462 ret
= do_write_string(ff
, perf_evsel__name(evsel
));
466 * write unique ids for this event
468 ret
= do_write(ff
, evsel
->id
, evsel
->ids
* sizeof(u64
));
475 static int write_cmdline(struct feat_fd
*ff
,
476 struct perf_evlist
*evlist __maybe_unused
)
478 char buf
[MAXPATHLEN
];
482 /* actual path to perf binary */
483 ret
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
487 /* readlink() does not add null termination */
490 /* account for binary path */
491 n
= perf_env
.nr_cmdline
+ 1;
493 ret
= do_write(ff
, &n
, sizeof(n
));
497 ret
= do_write_string(ff
, buf
);
501 for (i
= 0 ; i
< perf_env
.nr_cmdline
; i
++) {
502 ret
= do_write_string(ff
, perf_env
.cmdline_argv
[i
]);
509 #define CORE_SIB_FMT \
510 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
511 #define THRD_SIB_FMT \
512 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
518 char **core_siblings
;
519 char **thread_siblings
;
522 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
525 char filename
[MAXPATHLEN
];
526 char *buf
= NULL
, *p
;
532 sprintf(filename
, CORE_SIB_FMT
, cpu
);
533 fp
= fopen(filename
, "r");
537 sret
= getline(&buf
, &len
, fp
);
542 p
= strchr(buf
, '\n');
546 for (i
= 0; i
< tp
->core_sib
; i
++) {
547 if (!strcmp(buf
, tp
->core_siblings
[i
]))
550 if (i
== tp
->core_sib
) {
551 tp
->core_siblings
[i
] = buf
;
559 sprintf(filename
, THRD_SIB_FMT
, cpu
);
560 fp
= fopen(filename
, "r");
564 if (getline(&buf
, &len
, fp
) <= 0)
567 p
= strchr(buf
, '\n');
571 for (i
= 0; i
< tp
->thread_sib
; i
++) {
572 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
575 if (i
== tp
->thread_sib
) {
576 tp
->thread_siblings
[i
] = buf
;
588 static void free_cpu_topo(struct cpu_topo
*tp
)
595 for (i
= 0 ; i
< tp
->core_sib
; i
++)
596 zfree(&tp
->core_siblings
[i
]);
598 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
599 zfree(&tp
->thread_siblings
[i
]);
604 static struct cpu_topo
*build_cpu_topology(void)
606 struct cpu_topo
*tp
= NULL
;
614 ncpus
= cpu__max_present_cpu();
616 /* build online CPU map */
617 map
= cpu_map__new(NULL
);
619 pr_debug("failed to get system cpumap\n");
623 nr
= (u32
)(ncpus
& UINT_MAX
);
625 sz
= nr
* sizeof(char *);
626 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
633 tp
->core_siblings
= addr
;
635 tp
->thread_siblings
= addr
;
637 for (i
= 0; i
< nr
; i
++) {
638 if (!cpu_map__has(map
, i
))
641 ret
= build_cpu_topo(tp
, i
);
655 static int write_cpu_topology(struct feat_fd
*ff
,
656 struct perf_evlist
*evlist __maybe_unused
)
662 tp
= build_cpu_topology();
666 ret
= do_write(ff
, &tp
->core_sib
, sizeof(tp
->core_sib
));
670 for (i
= 0; i
< tp
->core_sib
; i
++) {
671 ret
= do_write_string(ff
, tp
->core_siblings
[i
]);
675 ret
= do_write(ff
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
679 for (i
= 0; i
< tp
->thread_sib
; i
++) {
680 ret
= do_write_string(ff
, tp
->thread_siblings
[i
]);
685 ret
= perf_env__read_cpu_topology_map(&perf_env
);
689 for (j
= 0; j
< perf_env
.nr_cpus_avail
; j
++) {
690 ret
= do_write(ff
, &perf_env
.cpu
[j
].core_id
,
691 sizeof(perf_env
.cpu
[j
].core_id
));
694 ret
= do_write(ff
, &perf_env
.cpu
[j
].socket_id
,
695 sizeof(perf_env
.cpu
[j
].socket_id
));
706 static int write_total_mem(struct feat_fd
*ff
,
707 struct perf_evlist
*evlist __maybe_unused
)
715 fp
= fopen("/proc/meminfo", "r");
719 while (getline(&buf
, &len
, fp
) > 0) {
720 ret
= strncmp(buf
, "MemTotal:", 9);
725 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
727 ret
= do_write(ff
, &mem
, sizeof(mem
));
735 static int write_topo_node(struct feat_fd
*ff
, int node
)
737 char str
[MAXPATHLEN
];
739 char *buf
= NULL
, *p
;
742 u64 mem_total
, mem_free
, mem
;
745 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
746 fp
= fopen(str
, "r");
750 while (getline(&buf
, &len
, fp
) > 0) {
751 /* skip over invalid lines */
752 if (!strchr(buf
, ':'))
754 if (sscanf(buf
, "%*s %*d %31s %"PRIu64
, field
, &mem
) != 2)
756 if (!strcmp(field
, "MemTotal:"))
758 if (!strcmp(field
, "MemFree:"))
765 ret
= do_write(ff
, &mem_total
, sizeof(u64
));
769 ret
= do_write(ff
, &mem_free
, sizeof(u64
));
774 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
776 fp
= fopen(str
, "r");
780 if (getline(&buf
, &len
, fp
) <= 0)
783 p
= strchr(buf
, '\n');
787 ret
= do_write_string(ff
, buf
);
795 static int write_numa_topology(struct feat_fd
*ff
,
796 struct perf_evlist
*evlist __maybe_unused
)
801 struct cpu_map
*node_map
= NULL
;
806 fp
= fopen("/sys/devices/system/node/online", "r");
810 if (getline(&buf
, &len
, fp
) <= 0)
813 c
= strchr(buf
, '\n');
817 node_map
= cpu_map__new(buf
);
821 nr
= (u32
)node_map
->nr
;
823 ret
= do_write(ff
, &nr
, sizeof(nr
));
827 for (i
= 0; i
< nr
; i
++) {
828 j
= (u32
)node_map
->map
[i
];
829 ret
= do_write(ff
, &j
, sizeof(j
));
833 ret
= write_topo_node(ff
, i
);
840 cpu_map__put(node_map
);
847 * struct pmu_mappings {
856 static int write_pmu_mappings(struct feat_fd
*ff
,
857 struct perf_evlist
*evlist __maybe_unused
)
859 struct perf_pmu
*pmu
= NULL
;
864 * Do a first pass to count number of pmu to avoid lseek so this
865 * works in pipe mode as well.
867 while ((pmu
= perf_pmu__scan(pmu
))) {
873 ret
= do_write(ff
, &pmu_num
, sizeof(pmu_num
));
877 while ((pmu
= perf_pmu__scan(pmu
))) {
881 ret
= do_write(ff
, &pmu
->type
, sizeof(pmu
->type
));
885 ret
= do_write_string(ff
, pmu
->name
);
896 * struct group_descs {
898 * struct group_desc {
905 static int write_group_desc(struct feat_fd
*ff
,
906 struct perf_evlist
*evlist
)
908 u32 nr_groups
= evlist
->nr_groups
;
909 struct perf_evsel
*evsel
;
912 ret
= do_write(ff
, &nr_groups
, sizeof(nr_groups
));
916 evlist__for_each_entry(evlist
, evsel
) {
917 if (perf_evsel__is_group_leader(evsel
) &&
918 evsel
->nr_members
> 1) {
919 const char *name
= evsel
->group_name
?: "{anon_group}";
920 u32 leader_idx
= evsel
->idx
;
921 u32 nr_members
= evsel
->nr_members
;
923 ret
= do_write_string(ff
, name
);
927 ret
= do_write(ff
, &leader_idx
, sizeof(leader_idx
));
931 ret
= do_write(ff
, &nr_members
, sizeof(nr_members
));
940 * default get_cpuid(): nothing gets recorded
941 * actual implementation must be in arch/$(SRCARCH)/util/header.c
943 int __weak
get_cpuid(char *buffer __maybe_unused
, size_t sz __maybe_unused
)
948 static int write_cpuid(struct feat_fd
*ff
,
949 struct perf_evlist
*evlist __maybe_unused
)
954 ret
= get_cpuid(buffer
, sizeof(buffer
));
960 return do_write_string(ff
, buffer
);
963 static int write_branch_stack(struct feat_fd
*ff __maybe_unused
,
964 struct perf_evlist
*evlist __maybe_unused
)
969 static int write_auxtrace(struct feat_fd
*ff
,
970 struct perf_evlist
*evlist __maybe_unused
)
972 struct perf_session
*session
;
975 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
978 session
= container_of(ff
->ph
, struct perf_session
, header
);
980 err
= auxtrace_index__write(ff
->fd
, &session
->auxtrace_index
);
982 pr_err("Failed to write auxtrace index\n");
986 static int cpu_cache_level__sort(const void *a
, const void *b
)
988 struct cpu_cache_level
*cache_a
= (struct cpu_cache_level
*)a
;
989 struct cpu_cache_level
*cache_b
= (struct cpu_cache_level
*)b
;
991 return cache_a
->level
- cache_b
->level
;
994 static bool cpu_cache_level__cmp(struct cpu_cache_level
*a
, struct cpu_cache_level
*b
)
996 if (a
->level
!= b
->level
)
999 if (a
->line_size
!= b
->line_size
)
1002 if (a
->sets
!= b
->sets
)
1005 if (a
->ways
!= b
->ways
)
1008 if (strcmp(a
->type
, b
->type
))
1011 if (strcmp(a
->size
, b
->size
))
1014 if (strcmp(a
->map
, b
->map
))
1020 static int cpu_cache_level__read(struct cpu_cache_level
*cache
, u32 cpu
, u16 level
)
1022 char path
[PATH_MAX
], file
[PATH_MAX
];
1026 scnprintf(path
, PATH_MAX
, "devices/system/cpu/cpu%d/cache/index%d/", cpu
, level
);
1027 scnprintf(file
, PATH_MAX
, "%s/%s", sysfs__mountpoint(), path
);
1029 if (stat(file
, &st
))
1032 scnprintf(file
, PATH_MAX
, "%s/level", path
);
1033 if (sysfs__read_int(file
, (int *) &cache
->level
))
1036 scnprintf(file
, PATH_MAX
, "%s/coherency_line_size", path
);
1037 if (sysfs__read_int(file
, (int *) &cache
->line_size
))
1040 scnprintf(file
, PATH_MAX
, "%s/number_of_sets", path
);
1041 if (sysfs__read_int(file
, (int *) &cache
->sets
))
1044 scnprintf(file
, PATH_MAX
, "%s/ways_of_associativity", path
);
1045 if (sysfs__read_int(file
, (int *) &cache
->ways
))
1048 scnprintf(file
, PATH_MAX
, "%s/type", path
);
1049 if (sysfs__read_str(file
, &cache
->type
, &len
))
1052 cache
->type
[len
] = 0;
1053 cache
->type
= rtrim(cache
->type
);
1055 scnprintf(file
, PATH_MAX
, "%s/size", path
);
1056 if (sysfs__read_str(file
, &cache
->size
, &len
)) {
1061 cache
->size
[len
] = 0;
1062 cache
->size
= rtrim(cache
->size
);
1064 scnprintf(file
, PATH_MAX
, "%s/shared_cpu_list", path
);
1065 if (sysfs__read_str(file
, &cache
->map
, &len
)) {
1071 cache
->map
[len
] = 0;
1072 cache
->map
= rtrim(cache
->map
);
1076 static void cpu_cache_level__fprintf(FILE *out
, struct cpu_cache_level
*c
)
1078 fprintf(out
, "L%d %-15s %8s [%s]\n", c
->level
, c
->type
, c
->size
, c
->map
);
1081 static int build_caches(struct cpu_cache_level caches
[], u32 size
, u32
*cntp
)
1088 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
1092 nr
= (u32
)(ncpus
& UINT_MAX
);
1094 for (cpu
= 0; cpu
< nr
; cpu
++) {
1095 for (level
= 0; level
< 10; level
++) {
1096 struct cpu_cache_level c
;
1099 err
= cpu_cache_level__read(&c
, cpu
, level
);
1106 for (i
= 0; i
< cnt
; i
++) {
1107 if (cpu_cache_level__cmp(&c
, &caches
[i
]))
1114 cpu_cache_level__free(&c
);
1116 if (WARN_ONCE(cnt
== size
, "way too many cpu caches.."))
1125 #define MAX_CACHES 2000
1127 static int write_cache(struct feat_fd
*ff
,
1128 struct perf_evlist
*evlist __maybe_unused
)
1130 struct cpu_cache_level caches
[MAX_CACHES
];
1131 u32 cnt
= 0, i
, version
= 1;
1134 ret
= build_caches(caches
, MAX_CACHES
, &cnt
);
1138 qsort(&caches
, cnt
, sizeof(struct cpu_cache_level
), cpu_cache_level__sort
);
1140 ret
= do_write(ff
, &version
, sizeof(u32
));
1144 ret
= do_write(ff
, &cnt
, sizeof(u32
));
1148 for (i
= 0; i
< cnt
; i
++) {
1149 struct cpu_cache_level
*c
= &caches
[i
];
1152 ret = do_write(ff, &c->v, sizeof(u32)); \
1163 ret = do_write_string(ff, (const char *) c->v); \
1174 for (i
= 0; i
< cnt
; i
++)
1175 cpu_cache_level__free(&caches
[i
]);
1179 static int write_stat(struct feat_fd
*ff __maybe_unused
,
1180 struct perf_evlist
*evlist __maybe_unused
)
1185 static int write_sample_time(struct feat_fd
*ff
,
1186 struct perf_evlist
*evlist
)
1190 ret
= do_write(ff
, &evlist
->first_sample_time
,
1191 sizeof(evlist
->first_sample_time
));
1195 return do_write(ff
, &evlist
->last_sample_time
,
1196 sizeof(evlist
->last_sample_time
));
1199 static void print_hostname(struct feat_fd
*ff
, FILE *fp
)
1201 fprintf(fp
, "# hostname : %s\n", ff
->ph
->env
.hostname
);
1204 static void print_osrelease(struct feat_fd
*ff
, FILE *fp
)
1206 fprintf(fp
, "# os release : %s\n", ff
->ph
->env
.os_release
);
1209 static void print_arch(struct feat_fd
*ff
, FILE *fp
)
1211 fprintf(fp
, "# arch : %s\n", ff
->ph
->env
.arch
);
1214 static void print_cpudesc(struct feat_fd
*ff
, FILE *fp
)
1216 fprintf(fp
, "# cpudesc : %s\n", ff
->ph
->env
.cpu_desc
);
1219 static void print_nrcpus(struct feat_fd
*ff
, FILE *fp
)
1221 fprintf(fp
, "# nrcpus online : %u\n", ff
->ph
->env
.nr_cpus_online
);
1222 fprintf(fp
, "# nrcpus avail : %u\n", ff
->ph
->env
.nr_cpus_avail
);
1225 static void print_version(struct feat_fd
*ff
, FILE *fp
)
1227 fprintf(fp
, "# perf version : %s\n", ff
->ph
->env
.version
);
1230 static void print_cmdline(struct feat_fd
*ff
, FILE *fp
)
1234 nr
= ff
->ph
->env
.nr_cmdline
;
1236 fprintf(fp
, "# cmdline : ");
1238 for (i
= 0; i
< nr
; i
++)
1239 fprintf(fp
, "%s ", ff
->ph
->env
.cmdline_argv
[i
]);
1243 static void print_cpu_topology(struct feat_fd
*ff
, FILE *fp
)
1245 struct perf_header
*ph
= ff
->ph
;
1246 int cpu_nr
= ph
->env
.nr_cpus_avail
;
1250 nr
= ph
->env
.nr_sibling_cores
;
1251 str
= ph
->env
.sibling_cores
;
1253 for (i
= 0; i
< nr
; i
++) {
1254 fprintf(fp
, "# sibling cores : %s\n", str
);
1255 str
+= strlen(str
) + 1;
1258 nr
= ph
->env
.nr_sibling_threads
;
1259 str
= ph
->env
.sibling_threads
;
1261 for (i
= 0; i
< nr
; i
++) {
1262 fprintf(fp
, "# sibling threads : %s\n", str
);
1263 str
+= strlen(str
) + 1;
1266 if (ph
->env
.cpu
!= NULL
) {
1267 for (i
= 0; i
< cpu_nr
; i
++)
1268 fprintf(fp
, "# CPU %d: Core ID %d, Socket ID %d\n", i
,
1269 ph
->env
.cpu
[i
].core_id
, ph
->env
.cpu
[i
].socket_id
);
1271 fprintf(fp
, "# Core ID and Socket ID information is not available\n");
1274 static void free_event_desc(struct perf_evsel
*events
)
1276 struct perf_evsel
*evsel
;
1281 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1282 zfree(&evsel
->name
);
1289 static struct perf_evsel
*read_event_desc(struct feat_fd
*ff
)
1291 struct perf_evsel
*evsel
, *events
= NULL
;
1294 u32 nre
, sz
, nr
, i
, j
;
1297 /* number of events */
1298 if (do_read_u32(ff
, &nre
))
1301 if (do_read_u32(ff
, &sz
))
1304 /* buffer to hold on file attr struct */
1309 /* the last event terminates with evsel->attr.size == 0: */
1310 events
= calloc(nre
+ 1, sizeof(*events
));
1314 msz
= sizeof(evsel
->attr
);
1318 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1322 * must read entire on-file attr struct to
1323 * sync up with layout.
1325 if (__do_read(ff
, buf
, sz
))
1328 if (ff
->ph
->needs_swap
)
1329 perf_event__attr_swap(buf
);
1331 memcpy(&evsel
->attr
, buf
, msz
);
1333 if (do_read_u32(ff
, &nr
))
1336 if (ff
->ph
->needs_swap
)
1337 evsel
->needs_swap
= true;
1339 evsel
->name
= do_read_string(ff
);
1346 id
= calloc(nr
, sizeof(*id
));
1352 for (j
= 0 ; j
< nr
; j
++) {
1353 if (do_read_u64(ff
, id
))
1362 free_event_desc(events
);
1367 static int __desc_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1368 void *priv __maybe_unused
)
1370 return fprintf(fp
, ", %s = %s", name
, val
);
1373 static void print_event_desc(struct feat_fd
*ff
, FILE *fp
)
1375 struct perf_evsel
*evsel
, *events
;
1380 events
= ff
->events
;
1382 events
= read_event_desc(ff
);
1385 fprintf(fp
, "# event desc: not available or unable to read\n");
1389 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1390 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1393 fprintf(fp
, ", id = {");
1394 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1397 fprintf(fp
, " %"PRIu64
, *id
);
1402 perf_event_attr__fprintf(fp
, &evsel
->attr
, __desc_attr__fprintf
, NULL
);
1407 free_event_desc(events
);
1411 static void print_total_mem(struct feat_fd
*ff
, FILE *fp
)
1413 fprintf(fp
, "# total memory : %llu kB\n", ff
->ph
->env
.total_mem
);
1416 static void print_numa_topology(struct feat_fd
*ff
, FILE *fp
)
1419 struct numa_node
*n
;
1421 for (i
= 0; i
< ff
->ph
->env
.nr_numa_nodes
; i
++) {
1422 n
= &ff
->ph
->env
.numa_nodes
[i
];
1424 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1425 " free = %"PRIu64
" kB\n",
1426 n
->node
, n
->mem_total
, n
->mem_free
);
1428 fprintf(fp
, "# node%u cpu list : ", n
->node
);
1429 cpu_map__fprintf(n
->map
, fp
);
1433 static void print_cpuid(struct feat_fd
*ff
, FILE *fp
)
1435 fprintf(fp
, "# cpuid : %s\n", ff
->ph
->env
.cpuid
);
1438 static void print_branch_stack(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1440 fprintf(fp
, "# contains samples with branch stack\n");
1443 static void print_auxtrace(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1445 fprintf(fp
, "# contains AUX area data (e.g. instruction trace)\n");
1448 static void print_stat(struct feat_fd
*ff __maybe_unused
, FILE *fp
)
1450 fprintf(fp
, "# contains stat data\n");
1453 static void print_cache(struct feat_fd
*ff
, FILE *fp __maybe_unused
)
1457 fprintf(fp
, "# CPU cache info:\n");
1458 for (i
= 0; i
< ff
->ph
->env
.caches_cnt
; i
++) {
1460 cpu_cache_level__fprintf(fp
, &ff
->ph
->env
.caches
[i
]);
1464 static void print_pmu_mappings(struct feat_fd
*ff
, FILE *fp
)
1466 const char *delimiter
= "# pmu mappings: ";
1471 pmu_num
= ff
->ph
->env
.nr_pmu_mappings
;
1473 fprintf(fp
, "# pmu mappings: not available\n");
1477 str
= ff
->ph
->env
.pmu_mappings
;
1480 type
= strtoul(str
, &tmp
, 0);
1485 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1488 str
+= strlen(str
) + 1;
1497 fprintf(fp
, "# pmu mappings: unable to read\n");
1500 static void print_group_desc(struct feat_fd
*ff
, FILE *fp
)
1502 struct perf_session
*session
;
1503 struct perf_evsel
*evsel
;
1506 session
= container_of(ff
->ph
, struct perf_session
, header
);
1508 evlist__for_each_entry(session
->evlist
, evsel
) {
1509 if (perf_evsel__is_group_leader(evsel
) &&
1510 evsel
->nr_members
> 1) {
1511 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1512 perf_evsel__name(evsel
));
1514 nr
= evsel
->nr_members
- 1;
1516 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1524 static void print_sample_time(struct feat_fd
*ff
, FILE *fp
)
1526 struct perf_session
*session
;
1530 session
= container_of(ff
->ph
, struct perf_session
, header
);
1532 timestamp__scnprintf_usec(session
->evlist
->first_sample_time
,
1533 time_buf
, sizeof(time_buf
));
1534 fprintf(fp
, "# time of first sample : %s\n", time_buf
);
1536 timestamp__scnprintf_usec(session
->evlist
->last_sample_time
,
1537 time_buf
, sizeof(time_buf
));
1538 fprintf(fp
, "# time of last sample : %s\n", time_buf
);
1540 d
= (double)(session
->evlist
->last_sample_time
-
1541 session
->evlist
->first_sample_time
) / NSEC_PER_MSEC
;
1543 fprintf(fp
, "# sample duration : %10.3f ms\n", d
);
1546 static int __event_process_build_id(struct build_id_event
*bev
,
1548 struct perf_session
*session
)
1551 struct machine
*machine
;
1554 enum dso_kernel_type dso_type
;
1556 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1560 cpumode
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1563 case PERF_RECORD_MISC_KERNEL
:
1564 dso_type
= DSO_TYPE_KERNEL
;
1566 case PERF_RECORD_MISC_GUEST_KERNEL
:
1567 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1569 case PERF_RECORD_MISC_USER
:
1570 case PERF_RECORD_MISC_GUEST_USER
:
1571 dso_type
= DSO_TYPE_USER
;
1577 dso
= machine__findnew_dso(machine
, filename
);
1579 char sbuild_id
[SBUILD_ID_SIZE
];
1581 dso__set_build_id(dso
, &bev
->build_id
);
1583 if (dso_type
!= DSO_TYPE_USER
) {
1584 struct kmod_path m
= { .name
= NULL
, };
1586 if (!kmod_path__parse_name(&m
, filename
) && m
.kmod
)
1587 dso__set_module_info(dso
, &m
, machine
);
1589 dso
->kernel
= dso_type
;
1594 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1596 pr_debug("build id event received for %s: %s\n",
1597 dso
->long_name
, sbuild_id
);
1606 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1607 int input
, u64 offset
, u64 size
)
1609 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1611 struct perf_event_header header
;
1612 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1615 struct build_id_event bev
;
1616 char filename
[PATH_MAX
];
1617 u64 limit
= offset
+ size
;
1619 while (offset
< limit
) {
1622 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1625 if (header
->needs_swap
)
1626 perf_event_header__bswap(&old_bev
.header
);
1628 len
= old_bev
.header
.size
- sizeof(old_bev
);
1629 if (readn(input
, filename
, len
) != len
)
1632 bev
.header
= old_bev
.header
;
1635 * As the pid is the missing value, we need to fill
1636 * it properly. The header.misc value give us nice hint.
1638 bev
.pid
= HOST_KERNEL_ID
;
1639 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1640 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1641 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1643 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1644 __event_process_build_id(&bev
, filename
, session
);
1646 offset
+= bev
.header
.size
;
1652 static int perf_header__read_build_ids(struct perf_header
*header
,
1653 int input
, u64 offset
, u64 size
)
1655 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1656 struct build_id_event bev
;
1657 char filename
[PATH_MAX
];
1658 u64 limit
= offset
+ size
, orig_offset
= offset
;
1661 while (offset
< limit
) {
1664 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1667 if (header
->needs_swap
)
1668 perf_event_header__bswap(&bev
.header
);
1670 len
= bev
.header
.size
- sizeof(bev
);
1671 if (readn(input
, filename
, len
) != len
)
1674 * The a1645ce1 changeset:
1676 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1678 * Added a field to struct build_id_event that broke the file
1681 * Since the kernel build-id is the first entry, process the
1682 * table using the old format if the well known
1683 * '[kernel.kallsyms]' string for the kernel build-id has the
1684 * first 4 characters chopped off (where the pid_t sits).
1686 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1687 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1689 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1692 __event_process_build_id(&bev
, filename
, session
);
1694 offset
+= bev
.header
.size
;
1701 /* Macro for features that simply need to read and store a string. */
1702 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1703 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1705 ff->ph->env.__feat_env = do_read_string(ff); \
1706 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1709 FEAT_PROCESS_STR_FUN(hostname
, hostname
);
1710 FEAT_PROCESS_STR_FUN(osrelease
, os_release
);
1711 FEAT_PROCESS_STR_FUN(version
, version
);
1712 FEAT_PROCESS_STR_FUN(arch
, arch
);
1713 FEAT_PROCESS_STR_FUN(cpudesc
, cpu_desc
);
1714 FEAT_PROCESS_STR_FUN(cpuid
, cpuid
);
1716 static int process_tracing_data(struct feat_fd
*ff
, void *data
)
1718 ssize_t ret
= trace_report(ff
->fd
, data
, false);
1720 return ret
< 0 ? -1 : 0;
1723 static int process_build_id(struct feat_fd
*ff
, void *data __maybe_unused
)
1725 if (perf_header__read_build_ids(ff
->ph
, ff
->fd
, ff
->offset
, ff
->size
))
1726 pr_debug("Failed to read buildids, continuing...\n");
1730 static int process_nrcpus(struct feat_fd
*ff
, void *data __maybe_unused
)
1733 u32 nr_cpus_avail
, nr_cpus_online
;
1735 ret
= do_read_u32(ff
, &nr_cpus_avail
);
1739 ret
= do_read_u32(ff
, &nr_cpus_online
);
1742 ff
->ph
->env
.nr_cpus_avail
= (int)nr_cpus_avail
;
1743 ff
->ph
->env
.nr_cpus_online
= (int)nr_cpus_online
;
1747 static int process_total_mem(struct feat_fd
*ff
, void *data __maybe_unused
)
1752 ret
= do_read_u64(ff
, &total_mem
);
1755 ff
->ph
->env
.total_mem
= (unsigned long long)total_mem
;
1759 static struct perf_evsel
*
1760 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1762 struct perf_evsel
*evsel
;
1764 evlist__for_each_entry(evlist
, evsel
) {
1765 if (evsel
->idx
== idx
)
1773 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1774 struct perf_evsel
*event
)
1776 struct perf_evsel
*evsel
;
1781 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1788 evsel
->name
= strdup(event
->name
);
1792 process_event_desc(struct feat_fd
*ff
, void *data __maybe_unused
)
1794 struct perf_session
*session
;
1795 struct perf_evsel
*evsel
, *events
= read_event_desc(ff
);
1800 session
= container_of(ff
->ph
, struct perf_session
, header
);
1802 if (session
->data
->is_pipe
) {
1803 /* Save events for reading later by print_event_desc,
1804 * since they can't be read again in pipe mode. */
1805 ff
->events
= events
;
1808 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1809 perf_evlist__set_event_name(session
->evlist
, evsel
);
1811 if (!session
->data
->is_pipe
)
1812 free_event_desc(events
);
1817 static int process_cmdline(struct feat_fd
*ff
, void *data __maybe_unused
)
1819 char *str
, *cmdline
= NULL
, **argv
= NULL
;
1822 if (do_read_u32(ff
, &nr
))
1825 ff
->ph
->env
.nr_cmdline
= nr
;
1827 cmdline
= zalloc(ff
->size
+ nr
+ 1);
1831 argv
= zalloc(sizeof(char *) * (nr
+ 1));
1835 for (i
= 0; i
< nr
; i
++) {
1836 str
= do_read_string(ff
);
1840 argv
[i
] = cmdline
+ len
;
1841 memcpy(argv
[i
], str
, strlen(str
) + 1);
1842 len
+= strlen(str
) + 1;
1845 ff
->ph
->env
.cmdline
= cmdline
;
1846 ff
->ph
->env
.cmdline_argv
= (const char **) argv
;
1855 static int process_cpu_topology(struct feat_fd
*ff
, void *data __maybe_unused
)
1860 int cpu_nr
= ff
->ph
->env
.nr_cpus_avail
;
1862 struct perf_header
*ph
= ff
->ph
;
1864 ph
->env
.cpu
= calloc(cpu_nr
, sizeof(*ph
->env
.cpu
));
1868 if (do_read_u32(ff
, &nr
))
1871 ph
->env
.nr_sibling_cores
= nr
;
1872 size
+= sizeof(u32
);
1873 if (strbuf_init(&sb
, 128) < 0)
1876 for (i
= 0; i
< nr
; i
++) {
1877 str
= do_read_string(ff
);
1881 /* include a NULL character at the end */
1882 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1884 size
+= string_size(str
);
1887 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1889 if (do_read_u32(ff
, &nr
))
1892 ph
->env
.nr_sibling_threads
= nr
;
1893 size
+= sizeof(u32
);
1895 for (i
= 0; i
< nr
; i
++) {
1896 str
= do_read_string(ff
);
1900 /* include a NULL character at the end */
1901 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1903 size
+= string_size(str
);
1906 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1909 * The header may be from old perf,
1910 * which doesn't include core id and socket id information.
1912 if (ff
->size
<= size
) {
1913 zfree(&ph
->env
.cpu
);
1917 for (i
= 0; i
< (u32
)cpu_nr
; i
++) {
1918 if (do_read_u32(ff
, &nr
))
1921 ph
->env
.cpu
[i
].core_id
= nr
;
1923 if (do_read_u32(ff
, &nr
))
1926 if (nr
!= (u32
)-1 && nr
> (u32
)cpu_nr
) {
1927 pr_debug("socket_id number is too big."
1928 "You may need to upgrade the perf tool.\n");
1932 ph
->env
.cpu
[i
].socket_id
= nr
;
1938 strbuf_release(&sb
);
1940 zfree(&ph
->env
.cpu
);
1944 static int process_numa_topology(struct feat_fd
*ff
, void *data __maybe_unused
)
1946 struct numa_node
*nodes
, *n
;
1951 if (do_read_u32(ff
, &nr
))
1954 nodes
= zalloc(sizeof(*nodes
) * nr
);
1958 for (i
= 0; i
< nr
; i
++) {
1962 if (do_read_u32(ff
, &n
->node
))
1965 if (do_read_u64(ff
, &n
->mem_total
))
1968 if (do_read_u64(ff
, &n
->mem_free
))
1971 str
= do_read_string(ff
);
1975 n
->map
= cpu_map__new(str
);
1981 ff
->ph
->env
.nr_numa_nodes
= nr
;
1982 ff
->ph
->env
.numa_nodes
= nodes
;
1990 static int process_pmu_mappings(struct feat_fd
*ff
, void *data __maybe_unused
)
1997 if (do_read_u32(ff
, &pmu_num
))
2001 pr_debug("pmu mappings not available\n");
2005 ff
->ph
->env
.nr_pmu_mappings
= pmu_num
;
2006 if (strbuf_init(&sb
, 128) < 0)
2010 if (do_read_u32(ff
, &type
))
2013 name
= do_read_string(ff
);
2017 if (strbuf_addf(&sb
, "%u:%s", type
, name
) < 0)
2019 /* include a NULL character at the end */
2020 if (strbuf_add(&sb
, "", 1) < 0)
2023 if (!strcmp(name
, "msr"))
2024 ff
->ph
->env
.msr_pmu_type
= type
;
2029 ff
->ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2033 strbuf_release(&sb
);
2037 static int process_group_desc(struct feat_fd
*ff
, void *data __maybe_unused
)
2040 u32 i
, nr
, nr_groups
;
2041 struct perf_session
*session
;
2042 struct perf_evsel
*evsel
, *leader
= NULL
;
2049 if (do_read_u32(ff
, &nr_groups
))
2052 ff
->ph
->env
.nr_groups
= nr_groups
;
2054 pr_debug("group desc not available\n");
2058 desc
= calloc(nr_groups
, sizeof(*desc
));
2062 for (i
= 0; i
< nr_groups
; i
++) {
2063 desc
[i
].name
= do_read_string(ff
);
2067 if (do_read_u32(ff
, &desc
[i
].leader_idx
))
2070 if (do_read_u32(ff
, &desc
[i
].nr_members
))
2075 * Rebuild group relationship based on the group_desc
2077 session
= container_of(ff
->ph
, struct perf_session
, header
);
2078 session
->evlist
->nr_groups
= nr_groups
;
2081 evlist__for_each_entry(session
->evlist
, evsel
) {
2082 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2083 evsel
->leader
= evsel
;
2084 /* {anon_group} is a dummy name */
2085 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2086 evsel
->group_name
= desc
[i
].name
;
2087 desc
[i
].name
= NULL
;
2089 evsel
->nr_members
= desc
[i
].nr_members
;
2091 if (i
>= nr_groups
|| nr
> 0) {
2092 pr_debug("invalid group desc\n");
2097 nr
= evsel
->nr_members
- 1;
2100 /* This is a group member */
2101 evsel
->leader
= leader
;
2107 if (i
!= nr_groups
|| nr
!= 0) {
2108 pr_debug("invalid group desc\n");
2114 for (i
= 0; i
< nr_groups
; i
++)
2115 zfree(&desc
[i
].name
);
2121 static int process_auxtrace(struct feat_fd
*ff
, void *data __maybe_unused
)
2123 struct perf_session
*session
;
2126 session
= container_of(ff
->ph
, struct perf_session
, header
);
2128 err
= auxtrace_index__process(ff
->fd
, ff
->size
, session
,
2129 ff
->ph
->needs_swap
);
2131 pr_err("Failed to process auxtrace index\n");
2135 static int process_cache(struct feat_fd
*ff
, void *data __maybe_unused
)
2137 struct cpu_cache_level
*caches
;
2138 u32 cnt
, i
, version
;
2140 if (do_read_u32(ff
, &version
))
2146 if (do_read_u32(ff
, &cnt
))
2149 caches
= zalloc(sizeof(*caches
) * cnt
);
2153 for (i
= 0; i
< cnt
; i
++) {
2154 struct cpu_cache_level c
;
2157 if (do_read_u32(ff, &c.v))\
2158 goto out_free_caches; \
2167 c.v = do_read_string(ff); \
2169 goto out_free_caches;
2179 ff
->ph
->env
.caches
= caches
;
2180 ff
->ph
->env
.caches_cnt
= cnt
;
2187 static int process_sample_time(struct feat_fd
*ff
, void *data __maybe_unused
)
2189 struct perf_session
*session
;
2190 u64 first_sample_time
, last_sample_time
;
2193 session
= container_of(ff
->ph
, struct perf_session
, header
);
2195 ret
= do_read_u64(ff
, &first_sample_time
);
2199 ret
= do_read_u64(ff
, &last_sample_time
);
2203 session
->evlist
->first_sample_time
= first_sample_time
;
2204 session
->evlist
->last_sample_time
= last_sample_time
;
2208 struct feature_ops
{
2209 int (*write
)(struct feat_fd
*ff
, struct perf_evlist
*evlist
);
2210 void (*print
)(struct feat_fd
*ff
, FILE *fp
);
2211 int (*process
)(struct feat_fd
*ff
, void *data
);
2217 #define FEAT_OPR(n, func, __full_only) \
2219 .name = __stringify(n), \
2220 .write = write_##func, \
2221 .print = print_##func, \
2222 .full_only = __full_only, \
2223 .process = process_##func, \
2224 .synthesize = true \
2227 #define FEAT_OPN(n, func, __full_only) \
2229 .name = __stringify(n), \
2230 .write = write_##func, \
2231 .print = print_##func, \
2232 .full_only = __full_only, \
2233 .process = process_##func \
2236 /* feature_ops not implemented: */
2237 #define print_tracing_data NULL
2238 #define print_build_id NULL
2240 #define process_branch_stack NULL
2241 #define process_stat NULL
2244 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2245 FEAT_OPN(TRACING_DATA
, tracing_data
, false),
2246 FEAT_OPN(BUILD_ID
, build_id
, false),
2247 FEAT_OPR(HOSTNAME
, hostname
, false),
2248 FEAT_OPR(OSRELEASE
, osrelease
, false),
2249 FEAT_OPR(VERSION
, version
, false),
2250 FEAT_OPR(ARCH
, arch
, false),
2251 FEAT_OPR(NRCPUS
, nrcpus
, false),
2252 FEAT_OPR(CPUDESC
, cpudesc
, false),
2253 FEAT_OPR(CPUID
, cpuid
, false),
2254 FEAT_OPR(TOTAL_MEM
, total_mem
, false),
2255 FEAT_OPR(EVENT_DESC
, event_desc
, false),
2256 FEAT_OPR(CMDLINE
, cmdline
, false),
2257 FEAT_OPR(CPU_TOPOLOGY
, cpu_topology
, true),
2258 FEAT_OPR(NUMA_TOPOLOGY
, numa_topology
, true),
2259 FEAT_OPN(BRANCH_STACK
, branch_stack
, false),
2260 FEAT_OPR(PMU_MAPPINGS
, pmu_mappings
, false),
2261 FEAT_OPN(GROUP_DESC
, group_desc
, false),
2262 FEAT_OPN(AUXTRACE
, auxtrace
, false),
2263 FEAT_OPN(STAT
, stat
, false),
2264 FEAT_OPN(CACHE
, cache
, true),
2265 FEAT_OPR(SAMPLE_TIME
, sample_time
, false),
2268 struct header_print_data
{
2270 bool full
; /* extended list of headers */
2273 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2274 struct perf_header
*ph
,
2275 int feat
, int fd
, void *data
)
2277 struct header_print_data
*hd
= data
;
2280 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2281 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2282 "%d, continuing...\n", section
->offset
, feat
);
2285 if (feat
>= HEADER_LAST_FEATURE
) {
2286 pr_warning("unknown feature %d\n", feat
);
2289 if (!feat_ops
[feat
].print
)
2292 ff
= (struct feat_fd
) {
2297 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2298 feat_ops
[feat
].print(&ff
, hd
->fp
);
2300 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2301 feat_ops
[feat
].name
);
2306 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2308 struct header_print_data hd
;
2309 struct perf_header
*header
= &session
->header
;
2310 int fd
= perf_data__fd(session
->data
);
2317 ret
= fstat(fd
, &st
);
2321 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
2323 perf_header__process_sections(header
, fd
, &hd
,
2324 perf_file_section__fprintf_info
);
2326 if (session
->data
->is_pipe
)
2329 fprintf(fp
, "# missing features: ");
2330 for_each_clear_bit(bit
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2332 fprintf(fp
, "%s ", feat_ops
[bit
].name
);
2339 static int do_write_feat(struct feat_fd
*ff
, int type
,
2340 struct perf_file_section
**p
,
2341 struct perf_evlist
*evlist
)
2346 if (perf_header__has_feat(ff
->ph
, type
)) {
2347 if (!feat_ops
[type
].write
)
2350 if (WARN(ff
->buf
, "Error: calling %s in pipe-mode.\n", __func__
))
2353 (*p
)->offset
= lseek(ff
->fd
, 0, SEEK_CUR
);
2355 err
= feat_ops
[type
].write(ff
, evlist
);
2357 pr_debug("failed to write feature %s\n", feat_ops
[type
].name
);
2359 /* undo anything written */
2360 lseek(ff
->fd
, (*p
)->offset
, SEEK_SET
);
2364 (*p
)->size
= lseek(ff
->fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2370 static int perf_header__adds_write(struct perf_header
*header
,
2371 struct perf_evlist
*evlist
, int fd
)
2375 struct perf_file_section
*feat_sec
, *p
;
2381 ff
= (struct feat_fd
){
2386 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2390 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2391 if (feat_sec
== NULL
)
2394 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2396 sec_start
= header
->feat_offset
;
2397 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2399 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2400 if (do_write_feat(&ff
, feat
, &p
, evlist
))
2401 perf_header__clear_feat(header
, feat
);
2404 lseek(fd
, sec_start
, SEEK_SET
);
2406 * may write more than needed due to dropped feature, but
2407 * this is okay, reader will skip the mising entries
2409 err
= do_write(&ff
, feat_sec
, sec_size
);
2411 pr_debug("failed to write feature section\n");
2416 int perf_header__write_pipe(int fd
)
2418 struct perf_pipe_file_header f_header
;
2422 ff
= (struct feat_fd
){ .fd
= fd
};
2424 f_header
= (struct perf_pipe_file_header
){
2425 .magic
= PERF_MAGIC
,
2426 .size
= sizeof(f_header
),
2429 err
= do_write(&ff
, &f_header
, sizeof(f_header
));
2431 pr_debug("failed to write perf pipe header\n");
2438 int perf_session__write_header(struct perf_session
*session
,
2439 struct perf_evlist
*evlist
,
2440 int fd
, bool at_exit
)
2442 struct perf_file_header f_header
;
2443 struct perf_file_attr f_attr
;
2444 struct perf_header
*header
= &session
->header
;
2445 struct perf_evsel
*evsel
;
2450 ff
= (struct feat_fd
){ .fd
= fd
};
2451 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2453 evlist__for_each_entry(session
->evlist
, evsel
) {
2454 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2455 err
= do_write(&ff
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2457 pr_debug("failed to write perf header\n");
2462 attr_offset
= lseek(ff
.fd
, 0, SEEK_CUR
);
2464 evlist__for_each_entry(evlist
, evsel
) {
2465 f_attr
= (struct perf_file_attr
){
2466 .attr
= evsel
->attr
,
2468 .offset
= evsel
->id_offset
,
2469 .size
= evsel
->ids
* sizeof(u64
),
2472 err
= do_write(&ff
, &f_attr
, sizeof(f_attr
));
2474 pr_debug("failed to write perf header attribute\n");
2479 if (!header
->data_offset
)
2480 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2481 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2484 err
= perf_header__adds_write(header
, evlist
, fd
);
2489 f_header
= (struct perf_file_header
){
2490 .magic
= PERF_MAGIC
,
2491 .size
= sizeof(f_header
),
2492 .attr_size
= sizeof(f_attr
),
2494 .offset
= attr_offset
,
2495 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2498 .offset
= header
->data_offset
,
2499 .size
= header
->data_size
,
2501 /* event_types is ignored, store zeros */
2504 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2506 lseek(fd
, 0, SEEK_SET
);
2507 err
= do_write(&ff
, &f_header
, sizeof(f_header
));
2509 pr_debug("failed to write perf header\n");
2512 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2517 static int perf_header__getbuffer64(struct perf_header
*header
,
2518 int fd
, void *buf
, size_t size
)
2520 if (readn(fd
, buf
, size
) <= 0)
2523 if (header
->needs_swap
)
2524 mem_bswap_64(buf
, size
);
2529 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2531 int (*process
)(struct perf_file_section
*section
,
2532 struct perf_header
*ph
,
2533 int feat
, int fd
, void *data
))
2535 struct perf_file_section
*feat_sec
, *sec
;
2541 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2545 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2549 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2551 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2553 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2557 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2558 err
= process(sec
++, header
, feat
, fd
, data
);
2568 static const int attr_file_abi_sizes
[] = {
2569 [0] = PERF_ATTR_SIZE_VER0
,
2570 [1] = PERF_ATTR_SIZE_VER1
,
2571 [2] = PERF_ATTR_SIZE_VER2
,
2572 [3] = PERF_ATTR_SIZE_VER3
,
2573 [4] = PERF_ATTR_SIZE_VER4
,
2578 * In the legacy file format, the magic number is not used to encode endianness.
2579 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2580 * on ABI revisions, we need to try all combinations for all endianness to
2581 * detect the endianness.
2583 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2585 uint64_t ref_size
, attr_size
;
2588 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2589 ref_size
= attr_file_abi_sizes
[i
]
2590 + sizeof(struct perf_file_section
);
2591 if (hdr_sz
!= ref_size
) {
2592 attr_size
= bswap_64(hdr_sz
);
2593 if (attr_size
!= ref_size
)
2596 ph
->needs_swap
= true;
2598 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2603 /* could not determine endianness */
2607 #define PERF_PIPE_HDR_VER0 16
2609 static const size_t attr_pipe_abi_sizes
[] = {
2610 [0] = PERF_PIPE_HDR_VER0
,
2615 * In the legacy pipe format, there is an implicit assumption that endiannesss
2616 * between host recording the samples, and host parsing the samples is the
2617 * same. This is not always the case given that the pipe output may always be
2618 * redirected into a file and analyzed on a different machine with possibly a
2619 * different endianness and perf_event ABI revsions in the perf tool itself.
2621 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2626 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2627 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2628 attr_size
= bswap_64(hdr_sz
);
2629 if (attr_size
!= hdr_sz
)
2632 ph
->needs_swap
= true;
2634 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2640 bool is_perf_magic(u64 magic
)
2642 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2643 || magic
== __perf_magic2
2644 || magic
== __perf_magic2_sw
)
2650 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2651 bool is_pipe
, struct perf_header
*ph
)
2655 /* check for legacy format */
2656 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2658 ph
->version
= PERF_HEADER_VERSION_1
;
2659 pr_debug("legacy perf.data format\n");
2661 return try_all_pipe_abis(hdr_sz
, ph
);
2663 return try_all_file_abis(hdr_sz
, ph
);
2666 * the new magic number serves two purposes:
2667 * - unique number to identify actual perf.data files
2668 * - encode endianness of file
2670 ph
->version
= PERF_HEADER_VERSION_2
;
2672 /* check magic number with one endianness */
2673 if (magic
== __perf_magic2
)
2676 /* check magic number with opposite endianness */
2677 if (magic
!= __perf_magic2_sw
)
2680 ph
->needs_swap
= true;
2685 int perf_file_header__read(struct perf_file_header
*header
,
2686 struct perf_header
*ph
, int fd
)
2690 lseek(fd
, 0, SEEK_SET
);
2692 ret
= readn(fd
, header
, sizeof(*header
));
2696 if (check_magic_endian(header
->magic
,
2697 header
->attr_size
, false, ph
) < 0) {
2698 pr_debug("magic/endian check failed\n");
2702 if (ph
->needs_swap
) {
2703 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2707 if (header
->size
!= sizeof(*header
)) {
2708 /* Support the previous format */
2709 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2710 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2713 } else if (ph
->needs_swap
) {
2715 * feature bitmap is declared as an array of unsigned longs --
2716 * not good since its size can differ between the host that
2717 * generated the data file and the host analyzing the file.
2719 * We need to handle endianness, but we don't know the size of
2720 * the unsigned long where the file was generated. Take a best
2721 * guess at determining it: try 64-bit swap first (ie., file
2722 * created on a 64-bit host), and check if the hostname feature
2723 * bit is set (this feature bit is forced on as of fbe96f2).
2724 * If the bit is not, undo the 64-bit swap and try a 32-bit
2725 * swap. If the hostname bit is still not set (e.g., older data
2726 * file), punt and fallback to the original behavior --
2727 * clearing all feature bits and setting buildid.
2729 mem_bswap_64(&header
->adds_features
,
2730 BITS_TO_U64(HEADER_FEAT_BITS
));
2732 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2734 mem_bswap_64(&header
->adds_features
,
2735 BITS_TO_U64(HEADER_FEAT_BITS
));
2738 mem_bswap_32(&header
->adds_features
,
2739 BITS_TO_U32(HEADER_FEAT_BITS
));
2742 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2743 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2744 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2748 memcpy(&ph
->adds_features
, &header
->adds_features
,
2749 sizeof(ph
->adds_features
));
2751 ph
->data_offset
= header
->data
.offset
;
2752 ph
->data_size
= header
->data
.size
;
2753 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2757 static int perf_file_section__process(struct perf_file_section
*section
,
2758 struct perf_header
*ph
,
2759 int feat
, int fd
, void *data
)
2761 struct feat_fd fdd
= {
2764 .size
= section
->size
,
2765 .offset
= section
->offset
,
2768 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2769 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2770 "%d, continuing...\n", section
->offset
, feat
);
2774 if (feat
>= HEADER_LAST_FEATURE
) {
2775 pr_debug("unknown feature %d, continuing...\n", feat
);
2779 if (!feat_ops
[feat
].process
)
2782 return feat_ops
[feat
].process(&fdd
, data
);
2785 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2786 struct perf_header
*ph
, int fd
,
2789 struct feat_fd ff
= {
2790 .fd
= STDOUT_FILENO
,
2795 ret
= readn(fd
, header
, sizeof(*header
));
2799 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2800 pr_debug("endian/magic failed\n");
2805 header
->size
= bswap_64(header
->size
);
2807 if (repipe
&& do_write(&ff
, header
, sizeof(*header
)) < 0)
2813 static int perf_header__read_pipe(struct perf_session
*session
)
2815 struct perf_header
*header
= &session
->header
;
2816 struct perf_pipe_file_header f_header
;
2818 if (perf_file_header__read_pipe(&f_header
, header
,
2819 perf_data__fd(session
->data
),
2820 session
->repipe
) < 0) {
2821 pr_debug("incompatible file format\n");
2828 static int read_attr(int fd
, struct perf_header
*ph
,
2829 struct perf_file_attr
*f_attr
)
2831 struct perf_event_attr
*attr
= &f_attr
->attr
;
2833 size_t our_sz
= sizeof(f_attr
->attr
);
2836 memset(f_attr
, 0, sizeof(*f_attr
));
2838 /* read minimal guaranteed structure */
2839 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2841 pr_debug("cannot read %d bytes of header attr\n",
2842 PERF_ATTR_SIZE_VER0
);
2846 /* on file perf_event_attr size */
2854 sz
= PERF_ATTR_SIZE_VER0
;
2855 } else if (sz
> our_sz
) {
2856 pr_debug("file uses a more recent and unsupported ABI"
2857 " (%zu bytes extra)\n", sz
- our_sz
);
2860 /* what we have not yet read and that we know about */
2861 left
= sz
- PERF_ATTR_SIZE_VER0
;
2864 ptr
+= PERF_ATTR_SIZE_VER0
;
2866 ret
= readn(fd
, ptr
, left
);
2868 /* read perf_file_section, ids are read in caller */
2869 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2871 return ret
<= 0 ? -1 : 0;
2874 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2875 struct pevent
*pevent
)
2877 struct event_format
*event
;
2880 /* already prepared */
2881 if (evsel
->tp_format
)
2884 if (pevent
== NULL
) {
2885 pr_debug("broken or missing trace data\n");
2889 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2890 if (event
== NULL
) {
2891 pr_debug("cannot find event format for %d\n", (int)evsel
->attr
.config
);
2896 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2897 evsel
->name
= strdup(bf
);
2898 if (evsel
->name
== NULL
)
2902 evsel
->tp_format
= event
;
2906 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2907 struct pevent
*pevent
)
2909 struct perf_evsel
*pos
;
2911 evlist__for_each_entry(evlist
, pos
) {
2912 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2913 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2920 int perf_session__read_header(struct perf_session
*session
)
2922 struct perf_data
*data
= session
->data
;
2923 struct perf_header
*header
= &session
->header
;
2924 struct perf_file_header f_header
;
2925 struct perf_file_attr f_attr
;
2927 int nr_attrs
, nr_ids
, i
, j
;
2928 int fd
= perf_data__fd(data
);
2930 session
->evlist
= perf_evlist__new();
2931 if (session
->evlist
== NULL
)
2934 session
->evlist
->env
= &header
->env
;
2935 session
->machines
.host
.env
= &header
->env
;
2936 if (perf_data__is_pipe(data
))
2937 return perf_header__read_pipe(session
);
2939 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2943 * Sanity check that perf.data was written cleanly; data size is
2944 * initialized to 0 and updated only if the on_exit function is run.
2945 * If data size is still 0 then the file contains only partial
2946 * information. Just warn user and process it as much as it can.
2948 if (f_header
.data
.size
== 0) {
2949 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2950 "Was the 'perf record' command properly terminated?\n",
2954 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2955 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2957 for (i
= 0; i
< nr_attrs
; i
++) {
2958 struct perf_evsel
*evsel
;
2961 if (read_attr(fd
, header
, &f_attr
) < 0)
2964 if (header
->needs_swap
) {
2965 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
2966 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
2967 perf_event__attr_swap(&f_attr
.attr
);
2970 tmp
= lseek(fd
, 0, SEEK_CUR
);
2971 evsel
= perf_evsel__new(&f_attr
.attr
);
2974 goto out_delete_evlist
;
2976 evsel
->needs_swap
= header
->needs_swap
;
2978 * Do it before so that if perf_evsel__alloc_id fails, this
2979 * entry gets purged too at perf_evlist__delete().
2981 perf_evlist__add(session
->evlist
, evsel
);
2983 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2985 * We don't have the cpu and thread maps on the header, so
2986 * for allocating the perf_sample_id table we fake 1 cpu and
2987 * hattr->ids threads.
2989 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2990 goto out_delete_evlist
;
2992 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2994 for (j
= 0; j
< nr_ids
; j
++) {
2995 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2998 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
3001 lseek(fd
, tmp
, SEEK_SET
);
3004 symbol_conf
.nr_events
= nr_attrs
;
3006 perf_header__process_sections(header
, fd
, &session
->tevent
,
3007 perf_file_section__process
);
3009 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
3010 session
->tevent
.pevent
))
3011 goto out_delete_evlist
;
3018 perf_evlist__delete(session
->evlist
);
3019 session
->evlist
= NULL
;
3023 int perf_event__synthesize_attr(struct perf_tool
*tool
,
3024 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
3025 perf_event__handler_t process
)
3027 union perf_event
*ev
;
3031 size
= sizeof(struct perf_event_attr
);
3032 size
= PERF_ALIGN(size
, sizeof(u64
));
3033 size
+= sizeof(struct perf_event_header
);
3034 size
+= ids
* sizeof(u64
);
3041 ev
->attr
.attr
= *attr
;
3042 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
3044 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
3045 ev
->attr
.header
.size
= (u16
)size
;
3047 if (ev
->attr
.header
.size
== size
)
3048 err
= process(tool
, ev
, NULL
, NULL
);
3057 int perf_event__synthesize_features(struct perf_tool
*tool
,
3058 struct perf_session
*session
,
3059 struct perf_evlist
*evlist
,
3060 perf_event__handler_t process
)
3062 struct perf_header
*header
= &session
->header
;
3064 struct feature_event
*fe
;
3068 sz_hdr
= sizeof(fe
->header
);
3069 sz
= sizeof(union perf_event
);
3070 /* get a nice alignment */
3071 sz
= PERF_ALIGN(sz
, page_size
);
3073 memset(&ff
, 0, sizeof(ff
));
3075 ff
.buf
= malloc(sz
);
3079 ff
.size
= sz
- sz_hdr
;
3081 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
3082 if (!feat_ops
[feat
].synthesize
) {
3083 pr_debug("No record header feature for header :%d\n", feat
);
3087 ff
.offset
= sizeof(*fe
);
3089 ret
= feat_ops
[feat
].write(&ff
, evlist
);
3090 if (ret
|| ff
.offset
<= (ssize_t
)sizeof(*fe
)) {
3091 pr_debug("Error writing feature\n");
3094 /* ff.buf may have changed due to realloc in do_write() */
3096 memset(fe
, 0, sizeof(*fe
));
3099 fe
->header
.type
= PERF_RECORD_HEADER_FEATURE
;
3100 fe
->header
.size
= ff
.offset
;
3102 ret
= process(tool
, ff
.buf
, NULL
, NULL
);
3112 int perf_event__process_feature(struct perf_tool
*tool
,
3113 union perf_event
*event
,
3114 struct perf_session
*session __maybe_unused
)
3116 struct feat_fd ff
= { .fd
= 0 };
3117 struct feature_event
*fe
= (struct feature_event
*)event
;
3118 int type
= fe
->header
.type
;
3119 u64 feat
= fe
->feat_id
;
3121 if (type
< 0 || type
>= PERF_RECORD_HEADER_MAX
) {
3122 pr_warning("invalid record type %d in pipe-mode\n", type
);
3125 if (feat
== HEADER_RESERVED
|| feat
> HEADER_LAST_FEATURE
) {
3126 pr_warning("invalid record type %d in pipe-mode\n", type
);
3130 if (!feat_ops
[feat
].process
)
3133 ff
.buf
= (void *)fe
->data
;
3134 ff
.size
= event
->header
.size
- sizeof(event
->header
);
3135 ff
.ph
= &session
->header
;
3137 if (feat_ops
[feat
].process(&ff
, NULL
))
3140 if (!feat_ops
[feat
].print
|| !tool
->show_feat_hdr
)
3143 if (!feat_ops
[feat
].full_only
||
3144 tool
->show_feat_hdr
>= SHOW_FEAT_HEADER_FULL_INFO
) {
3145 feat_ops
[feat
].print(&ff
, stdout
);
3147 fprintf(stdout
, "# %s info available, use -I to display\n",
3148 feat_ops
[feat
].name
);
3154 static struct event_update_event
*
3155 event_update_event__new(size_t size
, u64 type
, u64 id
)
3157 struct event_update_event
*ev
;
3159 size
+= sizeof(*ev
);
3160 size
= PERF_ALIGN(size
, sizeof(u64
));
3164 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3165 ev
->header
.size
= (u16
)size
;
3173 perf_event__synthesize_event_update_unit(struct perf_tool
*tool
,
3174 struct perf_evsel
*evsel
,
3175 perf_event__handler_t process
)
3177 struct event_update_event
*ev
;
3178 size_t size
= strlen(evsel
->unit
);
3181 ev
= event_update_event__new(size
+ 1, PERF_EVENT_UPDATE__UNIT
, evsel
->id
[0]);
3185 strncpy(ev
->data
, evsel
->unit
, size
);
3186 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
3192 perf_event__synthesize_event_update_scale(struct perf_tool
*tool
,
3193 struct perf_evsel
*evsel
,
3194 perf_event__handler_t process
)
3196 struct event_update_event
*ev
;
3197 struct event_update_event_scale
*ev_data
;
3200 ev
= event_update_event__new(sizeof(*ev_data
), PERF_EVENT_UPDATE__SCALE
, evsel
->id
[0]);
3204 ev_data
= (struct event_update_event_scale
*) ev
->data
;
3205 ev_data
->scale
= evsel
->scale
;
3206 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3212 perf_event__synthesize_event_update_name(struct perf_tool
*tool
,
3213 struct perf_evsel
*evsel
,
3214 perf_event__handler_t process
)
3216 struct event_update_event
*ev
;
3217 size_t len
= strlen(evsel
->name
);
3220 ev
= event_update_event__new(len
+ 1, PERF_EVENT_UPDATE__NAME
, evsel
->id
[0]);
3224 strncpy(ev
->data
, evsel
->name
, len
);
3225 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3231 perf_event__synthesize_event_update_cpus(struct perf_tool
*tool
,
3232 struct perf_evsel
*evsel
,
3233 perf_event__handler_t process
)
3235 size_t size
= sizeof(struct event_update_event
);
3236 struct event_update_event
*ev
;
3240 if (!evsel
->own_cpus
)
3243 ev
= cpu_map_data__alloc(evsel
->own_cpus
, &size
, &type
, &max
);
3247 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3248 ev
->header
.size
= (u16
)size
;
3249 ev
->type
= PERF_EVENT_UPDATE__CPUS
;
3250 ev
->id
= evsel
->id
[0];
3252 cpu_map_data__synthesize((struct cpu_map_data
*) ev
->data
,
3256 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3261 size_t perf_event__fprintf_event_update(union perf_event
*event
, FILE *fp
)
3263 struct event_update_event
*ev
= &event
->event_update
;
3264 struct event_update_event_scale
*ev_scale
;
3265 struct event_update_event_cpus
*ev_cpus
;
3266 struct cpu_map
*map
;
3269 ret
= fprintf(fp
, "\n... id: %" PRIu64
"\n", ev
->id
);
3272 case PERF_EVENT_UPDATE__SCALE
:
3273 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3274 ret
+= fprintf(fp
, "... scale: %f\n", ev_scale
->scale
);
3276 case PERF_EVENT_UPDATE__UNIT
:
3277 ret
+= fprintf(fp
, "... unit: %s\n", ev
->data
);
3279 case PERF_EVENT_UPDATE__NAME
:
3280 ret
+= fprintf(fp
, "... name: %s\n", ev
->data
);
3282 case PERF_EVENT_UPDATE__CPUS
:
3283 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3284 ret
+= fprintf(fp
, "... ");
3286 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3288 ret
+= cpu_map__fprintf(map
, fp
);
3290 ret
+= fprintf(fp
, "failed to get cpus\n");
3293 ret
+= fprintf(fp
, "... unknown type\n");
3300 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
3301 struct perf_session
*session
,
3302 perf_event__handler_t process
)
3304 struct perf_evsel
*evsel
;
3307 evlist__for_each_entry(session
->evlist
, evsel
) {
3308 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
3309 evsel
->id
, process
);
3311 pr_debug("failed to create perf header attribute\n");
3319 static bool has_unit(struct perf_evsel
*counter
)
3321 return counter
->unit
&& *counter
->unit
;
3324 static bool has_scale(struct perf_evsel
*counter
)
3326 return counter
->scale
!= 1;
3329 int perf_event__synthesize_extra_attr(struct perf_tool
*tool
,
3330 struct perf_evlist
*evsel_list
,
3331 perf_event__handler_t process
,
3334 struct perf_evsel
*counter
;
3338 * Synthesize other events stuff not carried within
3339 * attr event - unit, scale, name
3341 evlist__for_each_entry(evsel_list
, counter
) {
3342 if (!counter
->supported
)
3346 * Synthesize unit and scale only if it's defined.
3348 if (has_unit(counter
)) {
3349 err
= perf_event__synthesize_event_update_unit(tool
, counter
, process
);
3351 pr_err("Couldn't synthesize evsel unit.\n");
3356 if (has_scale(counter
)) {
3357 err
= perf_event__synthesize_event_update_scale(tool
, counter
, process
);
3359 pr_err("Couldn't synthesize evsel counter.\n");
3364 if (counter
->own_cpus
) {
3365 err
= perf_event__synthesize_event_update_cpus(tool
, counter
, process
);
3367 pr_err("Couldn't synthesize evsel cpus.\n");
3373 * Name is needed only for pipe output,
3374 * perf.data carries event names.
3377 err
= perf_event__synthesize_event_update_name(tool
, counter
, process
);
3379 pr_err("Couldn't synthesize evsel name.\n");
3387 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
3388 union perf_event
*event
,
3389 struct perf_evlist
**pevlist
)
3392 struct perf_evsel
*evsel
;
3393 struct perf_evlist
*evlist
= *pevlist
;
3395 if (evlist
== NULL
) {
3396 *pevlist
= evlist
= perf_evlist__new();
3401 evsel
= perf_evsel__new(&event
->attr
.attr
);
3405 perf_evlist__add(evlist
, evsel
);
3407 ids
= event
->header
.size
;
3408 ids
-= (void *)&event
->attr
.id
- (void *)event
;
3409 n_ids
= ids
/ sizeof(u64
);
3411 * We don't have the cpu and thread maps on the header, so
3412 * for allocating the perf_sample_id table we fake 1 cpu and
3413 * hattr->ids threads.
3415 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
3418 for (i
= 0; i
< n_ids
; i
++) {
3419 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
3422 symbol_conf
.nr_events
= evlist
->nr_entries
;
3427 int perf_event__process_event_update(struct perf_tool
*tool __maybe_unused
,
3428 union perf_event
*event
,
3429 struct perf_evlist
**pevlist
)
3431 struct event_update_event
*ev
= &event
->event_update
;
3432 struct event_update_event_scale
*ev_scale
;
3433 struct event_update_event_cpus
*ev_cpus
;
3434 struct perf_evlist
*evlist
;
3435 struct perf_evsel
*evsel
;
3436 struct cpu_map
*map
;
3438 if (!pevlist
|| *pevlist
== NULL
)
3443 evsel
= perf_evlist__id2evsel(evlist
, ev
->id
);
3448 case PERF_EVENT_UPDATE__UNIT
:
3449 evsel
->unit
= strdup(ev
->data
);
3451 case PERF_EVENT_UPDATE__NAME
:
3452 evsel
->name
= strdup(ev
->data
);
3454 case PERF_EVENT_UPDATE__SCALE
:
3455 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3456 evsel
->scale
= ev_scale
->scale
;
3458 case PERF_EVENT_UPDATE__CPUS
:
3459 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3461 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3463 evsel
->own_cpus
= map
;
3465 pr_err("failed to get event_update cpus\n");
3473 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
3474 struct perf_evlist
*evlist
,
3475 perf_event__handler_t process
)
3477 union perf_event ev
;
3478 struct tracing_data
*tdata
;
3479 ssize_t size
= 0, aligned_size
= 0, padding
;
3481 int err __maybe_unused
= 0;
3484 * We are going to store the size of the data followed
3485 * by the data contents. Since the fd descriptor is a pipe,
3486 * we cannot seek back to store the size of the data once
3487 * we know it. Instead we:
3489 * - write the tracing data to the temp file
3490 * - get/write the data size to pipe
3491 * - write the tracing data from the temp file
3494 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3498 memset(&ev
, 0, sizeof(ev
));
3500 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3502 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3503 padding
= aligned_size
- size
;
3504 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3505 ev
.tracing_data
.size
= aligned_size
;
3507 process(tool
, &ev
, NULL
, NULL
);
3510 * The put function will copy all the tracing data
3511 * stored in temp file to the pipe.
3513 tracing_data_put(tdata
);
3515 ff
= (struct feat_fd
){ .fd
= fd
};
3516 if (write_padded(&ff
, NULL
, 0, padding
))
3519 return aligned_size
;
3522 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
3523 union perf_event
*event
,
3524 struct perf_session
*session
)
3526 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3527 int fd
= perf_data__fd(session
->data
);
3528 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
3531 /* setup for reading amidst mmap */
3532 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
3535 size_read
= trace_report(fd
, &session
->tevent
,
3537 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3539 if (readn(fd
, buf
, padding
) < 0) {
3540 pr_err("%s: reading input file", __func__
);
3543 if (session
->repipe
) {
3544 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3545 if (retw
<= 0 || retw
!= padding
) {
3546 pr_err("%s: repiping tracing data padding", __func__
);
3551 if (size_read
+ padding
!= size
) {
3552 pr_err("%s: tracing data size mismatch", __func__
);
3556 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3557 session
->tevent
.pevent
);
3559 return size_read
+ padding
;
3562 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3563 struct dso
*pos
, u16 misc
,
3564 perf_event__handler_t process
,
3565 struct machine
*machine
)
3567 union perf_event ev
;
3574 memset(&ev
, 0, sizeof(ev
));
3576 len
= pos
->long_name_len
+ 1;
3577 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3578 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3579 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3580 ev
.build_id
.header
.misc
= misc
;
3581 ev
.build_id
.pid
= machine
->pid
;
3582 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3583 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3585 err
= process(tool
, &ev
, NULL
, machine
);
3590 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3591 union perf_event
*event
,
3592 struct perf_session
*session
)
3594 __event_process_build_id(&event
->build_id
,
3595 event
->build_id
.filename
,