11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 #include <linux/bitops.h>
15 #include <sys/types.h>
16 #include <sys/utsname.h>
24 #include "trace-event.h"
34 #include <api/fs/fs.h>
37 #include "sane_ctype.h"
41 * must be a numerical value to let the endianness
42 * determine the memory layout. That way we are able
43 * to detect endianness when reading the perf.data file
46 * we check for legacy (PERFFILE) format.
48 static const char *__perf_magic1
= "PERFFILE";
49 static const u64 __perf_magic2
= 0x32454c4946524550ULL
;
50 static const u64 __perf_magic2_sw
= 0x50455246494c4532ULL
;
52 #define PERF_MAGIC __perf_magic2
54 const char perf_version_string
[] = PERF_VERSION
;
56 struct perf_file_attr
{
57 struct perf_event_attr attr
;
58 struct perf_file_section ids
;
61 void perf_header__set_feat(struct perf_header
*header
, int feat
)
63 set_bit(feat
, header
->adds_features
);
66 void perf_header__clear_feat(struct perf_header
*header
, int feat
)
68 clear_bit(feat
, header
->adds_features
);
71 bool perf_header__has_feat(const struct perf_header
*header
, int feat
)
73 return test_bit(feat
, header
->adds_features
);
76 static int do_write(int fd
, const void *buf
, size_t size
)
79 int ret
= write(fd
, buf
, size
);
91 int write_padded(int fd
, const void *bf
, size_t count
, size_t count_aligned
)
93 static const char zero_buf
[NAME_ALIGN
];
94 int err
= do_write(fd
, bf
, count
);
97 err
= do_write(fd
, zero_buf
, count_aligned
- count
);
102 #define string_size(str) \
103 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
105 static int do_write_string(int fd
, const char *str
)
110 olen
= strlen(str
) + 1;
111 len
= PERF_ALIGN(olen
, NAME_ALIGN
);
113 /* write len, incl. \0 */
114 ret
= do_write(fd
, &len
, sizeof(len
));
118 return write_padded(fd
, str
, olen
, len
);
121 static char *do_read_string(int fd
, struct perf_header
*ph
)
127 sz
= readn(fd
, &len
, sizeof(len
));
128 if (sz
< (ssize_t
)sizeof(len
))
138 ret
= readn(fd
, buf
, len
);
139 if (ret
== (ssize_t
)len
) {
141 * strings are padded by zeroes
142 * thus the actual strlen of buf
143 * may be less than len
152 static int write_tracing_data(int fd
, struct perf_header
*h __maybe_unused
,
153 struct perf_evlist
*evlist
)
155 return read_tracing_data(fd
, &evlist
->entries
);
159 static int write_build_id(int fd
, struct perf_header
*h
,
160 struct perf_evlist
*evlist __maybe_unused
)
162 struct perf_session
*session
;
165 session
= container_of(h
, struct perf_session
, header
);
167 if (!perf_session__read_build_ids(session
, true))
170 err
= perf_session__write_buildid_table(session
, fd
);
172 pr_debug("failed to write buildid table\n");
175 perf_session__cache_build_ids(session
);
180 static int write_hostname(int fd
, struct perf_header
*h __maybe_unused
,
181 struct perf_evlist
*evlist __maybe_unused
)
190 return do_write_string(fd
, uts
.nodename
);
193 static int write_osrelease(int fd
, struct perf_header
*h __maybe_unused
,
194 struct perf_evlist
*evlist __maybe_unused
)
203 return do_write_string(fd
, uts
.release
);
206 static int write_arch(int fd
, struct perf_header
*h __maybe_unused
,
207 struct perf_evlist
*evlist __maybe_unused
)
216 return do_write_string(fd
, uts
.machine
);
219 static int write_version(int fd
, struct perf_header
*h __maybe_unused
,
220 struct perf_evlist
*evlist __maybe_unused
)
222 return do_write_string(fd
, perf_version_string
);
225 static int __write_cpudesc(int fd
, const char *cpuinfo_proc
)
230 const char *search
= cpuinfo_proc
;
237 file
= fopen("/proc/cpuinfo", "r");
241 while (getline(&buf
, &len
, file
) > 0) {
242 ret
= strncmp(buf
, search
, strlen(search
));
254 p
= strchr(buf
, ':');
255 if (p
&& *(p
+1) == ' ' && *(p
+2))
261 /* squash extra space characters (branding string) */
268 while (*q
&& isspace(*q
))
271 while ((*r
++ = *q
++));
275 ret
= do_write_string(fd
, s
);
282 static int write_cpudesc(int fd
, struct perf_header
*h __maybe_unused
,
283 struct perf_evlist
*evlist __maybe_unused
)
286 #define CPUINFO_PROC {"model name", }
288 const char *cpuinfo_procs
[] = CPUINFO_PROC
;
291 for (i
= 0; i
< ARRAY_SIZE(cpuinfo_procs
); i
++) {
293 ret
= __write_cpudesc(fd
, cpuinfo_procs
[i
]);
301 static int write_nrcpus(int fd
, struct perf_header
*h __maybe_unused
,
302 struct perf_evlist
*evlist __maybe_unused
)
308 nrc
= cpu__max_present_cpu();
310 nr
= sysconf(_SC_NPROCESSORS_ONLN
);
314 nra
= (u32
)(nr
& UINT_MAX
);
316 ret
= do_write(fd
, &nrc
, sizeof(nrc
));
320 return do_write(fd
, &nra
, sizeof(nra
));
323 static int write_event_desc(int fd
, struct perf_header
*h __maybe_unused
,
324 struct perf_evlist
*evlist
)
326 struct perf_evsel
*evsel
;
330 nre
= evlist
->nr_entries
;
333 * write number of events
335 ret
= do_write(fd
, &nre
, sizeof(nre
));
340 * size of perf_event_attr struct
342 sz
= (u32
)sizeof(evsel
->attr
);
343 ret
= do_write(fd
, &sz
, sizeof(sz
));
347 evlist__for_each_entry(evlist
, evsel
) {
348 ret
= do_write(fd
, &evsel
->attr
, sz
);
352 * write number of unique id per event
353 * there is one id per instance of an event
355 * copy into an nri to be independent of the
359 ret
= do_write(fd
, &nri
, sizeof(nri
));
364 * write event string as passed on cmdline
366 ret
= do_write_string(fd
, perf_evsel__name(evsel
));
370 * write unique ids for this event
372 ret
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
379 static int write_cmdline(int fd
, struct perf_header
*h __maybe_unused
,
380 struct perf_evlist
*evlist __maybe_unused
)
382 char buf
[MAXPATHLEN
];
386 /* actual path to perf binary */
387 ret
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
391 /* readlink() does not add null termination */
394 /* account for binary path */
395 n
= perf_env
.nr_cmdline
+ 1;
397 ret
= do_write(fd
, &n
, sizeof(n
));
401 ret
= do_write_string(fd
, buf
);
405 for (i
= 0 ; i
< perf_env
.nr_cmdline
; i
++) {
406 ret
= do_write_string(fd
, perf_env
.cmdline_argv
[i
]);
413 #define CORE_SIB_FMT \
414 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
415 #define THRD_SIB_FMT \
416 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
422 char **core_siblings
;
423 char **thread_siblings
;
426 static int build_cpu_topo(struct cpu_topo
*tp
, int cpu
)
429 char filename
[MAXPATHLEN
];
430 char *buf
= NULL
, *p
;
436 sprintf(filename
, CORE_SIB_FMT
, cpu
);
437 fp
= fopen(filename
, "r");
441 sret
= getline(&buf
, &len
, fp
);
446 p
= strchr(buf
, '\n');
450 for (i
= 0; i
< tp
->core_sib
; i
++) {
451 if (!strcmp(buf
, tp
->core_siblings
[i
]))
454 if (i
== tp
->core_sib
) {
455 tp
->core_siblings
[i
] = buf
;
463 sprintf(filename
, THRD_SIB_FMT
, cpu
);
464 fp
= fopen(filename
, "r");
468 if (getline(&buf
, &len
, fp
) <= 0)
471 p
= strchr(buf
, '\n');
475 for (i
= 0; i
< tp
->thread_sib
; i
++) {
476 if (!strcmp(buf
, tp
->thread_siblings
[i
]))
479 if (i
== tp
->thread_sib
) {
480 tp
->thread_siblings
[i
] = buf
;
492 static void free_cpu_topo(struct cpu_topo
*tp
)
499 for (i
= 0 ; i
< tp
->core_sib
; i
++)
500 zfree(&tp
->core_siblings
[i
]);
502 for (i
= 0 ; i
< tp
->thread_sib
; i
++)
503 zfree(&tp
->thread_siblings
[i
]);
508 static struct cpu_topo
*build_cpu_topology(void)
510 struct cpu_topo
*tp
= NULL
;
518 ncpus
= cpu__max_present_cpu();
520 /* build online CPU map */
521 map
= cpu_map__new(NULL
);
523 pr_debug("failed to get system cpumap\n");
527 nr
= (u32
)(ncpus
& UINT_MAX
);
529 sz
= nr
* sizeof(char *);
530 addr
= calloc(1, sizeof(*tp
) + 2 * sz
);
537 tp
->core_siblings
= addr
;
539 tp
->thread_siblings
= addr
;
541 for (i
= 0; i
< nr
; i
++) {
542 if (!cpu_map__has(map
, i
))
545 ret
= build_cpu_topo(tp
, i
);
559 static int write_cpu_topology(int fd
, struct perf_header
*h __maybe_unused
,
560 struct perf_evlist
*evlist __maybe_unused
)
566 tp
= build_cpu_topology();
570 ret
= do_write(fd
, &tp
->core_sib
, sizeof(tp
->core_sib
));
574 for (i
= 0; i
< tp
->core_sib
; i
++) {
575 ret
= do_write_string(fd
, tp
->core_siblings
[i
]);
579 ret
= do_write(fd
, &tp
->thread_sib
, sizeof(tp
->thread_sib
));
583 for (i
= 0; i
< tp
->thread_sib
; i
++) {
584 ret
= do_write_string(fd
, tp
->thread_siblings
[i
]);
589 ret
= perf_env__read_cpu_topology_map(&perf_env
);
593 for (j
= 0; j
< perf_env
.nr_cpus_avail
; j
++) {
594 ret
= do_write(fd
, &perf_env
.cpu
[j
].core_id
,
595 sizeof(perf_env
.cpu
[j
].core_id
));
598 ret
= do_write(fd
, &perf_env
.cpu
[j
].socket_id
,
599 sizeof(perf_env
.cpu
[j
].socket_id
));
610 static int write_total_mem(int fd
, struct perf_header
*h __maybe_unused
,
611 struct perf_evlist
*evlist __maybe_unused
)
619 fp
= fopen("/proc/meminfo", "r");
623 while (getline(&buf
, &len
, fp
) > 0) {
624 ret
= strncmp(buf
, "MemTotal:", 9);
629 n
= sscanf(buf
, "%*s %"PRIu64
, &mem
);
631 ret
= do_write(fd
, &mem
, sizeof(mem
));
639 static int write_topo_node(int fd
, int node
)
641 char str
[MAXPATHLEN
];
643 char *buf
= NULL
, *p
;
646 u64 mem_total
, mem_free
, mem
;
649 sprintf(str
, "/sys/devices/system/node/node%d/meminfo", node
);
650 fp
= fopen(str
, "r");
654 while (getline(&buf
, &len
, fp
) > 0) {
655 /* skip over invalid lines */
656 if (!strchr(buf
, ':'))
658 if (sscanf(buf
, "%*s %*d %31s %"PRIu64
, field
, &mem
) != 2)
660 if (!strcmp(field
, "MemTotal:"))
662 if (!strcmp(field
, "MemFree:"))
669 ret
= do_write(fd
, &mem_total
, sizeof(u64
));
673 ret
= do_write(fd
, &mem_free
, sizeof(u64
));
678 sprintf(str
, "/sys/devices/system/node/node%d/cpulist", node
);
680 fp
= fopen(str
, "r");
684 if (getline(&buf
, &len
, fp
) <= 0)
687 p
= strchr(buf
, '\n');
691 ret
= do_write_string(fd
, buf
);
699 static int write_numa_topology(int fd
, struct perf_header
*h __maybe_unused
,
700 struct perf_evlist
*evlist __maybe_unused
)
705 struct cpu_map
*node_map
= NULL
;
710 fp
= fopen("/sys/devices/system/node/online", "r");
714 if (getline(&buf
, &len
, fp
) <= 0)
717 c
= strchr(buf
, '\n');
721 node_map
= cpu_map__new(buf
);
725 nr
= (u32
)node_map
->nr
;
727 ret
= do_write(fd
, &nr
, sizeof(nr
));
731 for (i
= 0; i
< nr
; i
++) {
732 j
= (u32
)node_map
->map
[i
];
733 ret
= do_write(fd
, &j
, sizeof(j
));
737 ret
= write_topo_node(fd
, i
);
744 cpu_map__put(node_map
);
751 * struct pmu_mappings {
760 static int write_pmu_mappings(int fd
, struct perf_header
*h __maybe_unused
,
761 struct perf_evlist
*evlist __maybe_unused
)
763 struct perf_pmu
*pmu
= NULL
;
764 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
768 /* write real pmu_num later */
769 ret
= do_write(fd
, &pmu_num
, sizeof(pmu_num
));
773 while ((pmu
= perf_pmu__scan(pmu
))) {
778 ret
= do_write(fd
, &pmu
->type
, sizeof(pmu
->type
));
782 ret
= do_write_string(fd
, pmu
->name
);
787 if (pwrite(fd
, &pmu_num
, sizeof(pmu_num
), offset
) != sizeof(pmu_num
)) {
789 lseek(fd
, offset
, SEEK_SET
);
799 * struct group_descs {
801 * struct group_desc {
808 static int write_group_desc(int fd
, struct perf_header
*h __maybe_unused
,
809 struct perf_evlist
*evlist
)
811 u32 nr_groups
= evlist
->nr_groups
;
812 struct perf_evsel
*evsel
;
815 ret
= do_write(fd
, &nr_groups
, sizeof(nr_groups
));
819 evlist__for_each_entry(evlist
, evsel
) {
820 if (perf_evsel__is_group_leader(evsel
) &&
821 evsel
->nr_members
> 1) {
822 const char *name
= evsel
->group_name
?: "{anon_group}";
823 u32 leader_idx
= evsel
->idx
;
824 u32 nr_members
= evsel
->nr_members
;
826 ret
= do_write_string(fd
, name
);
830 ret
= do_write(fd
, &leader_idx
, sizeof(leader_idx
));
834 ret
= do_write(fd
, &nr_members
, sizeof(nr_members
));
843 * default get_cpuid(): nothing gets recorded
844 * actual implementation must be in arch/$(ARCH)/util/header.c
846 int __weak
get_cpuid(char *buffer __maybe_unused
, size_t sz __maybe_unused
)
851 static int write_cpuid(int fd
, struct perf_header
*h __maybe_unused
,
852 struct perf_evlist
*evlist __maybe_unused
)
857 ret
= get_cpuid(buffer
, sizeof(buffer
));
863 return do_write_string(fd
, buffer
);
866 static int write_branch_stack(int fd __maybe_unused
,
867 struct perf_header
*h __maybe_unused
,
868 struct perf_evlist
*evlist __maybe_unused
)
873 static int write_auxtrace(int fd
, struct perf_header
*h
,
874 struct perf_evlist
*evlist __maybe_unused
)
876 struct perf_session
*session
;
879 session
= container_of(h
, struct perf_session
, header
);
881 err
= auxtrace_index__write(fd
, &session
->auxtrace_index
);
883 pr_err("Failed to write auxtrace index\n");
887 static int cpu_cache_level__sort(const void *a
, const void *b
)
889 struct cpu_cache_level
*cache_a
= (struct cpu_cache_level
*)a
;
890 struct cpu_cache_level
*cache_b
= (struct cpu_cache_level
*)b
;
892 return cache_a
->level
- cache_b
->level
;
895 static bool cpu_cache_level__cmp(struct cpu_cache_level
*a
, struct cpu_cache_level
*b
)
897 if (a
->level
!= b
->level
)
900 if (a
->line_size
!= b
->line_size
)
903 if (a
->sets
!= b
->sets
)
906 if (a
->ways
!= b
->ways
)
909 if (strcmp(a
->type
, b
->type
))
912 if (strcmp(a
->size
, b
->size
))
915 if (strcmp(a
->map
, b
->map
))
921 static int cpu_cache_level__read(struct cpu_cache_level
*cache
, u32 cpu
, u16 level
)
923 char path
[PATH_MAX
], file
[PATH_MAX
];
927 scnprintf(path
, PATH_MAX
, "devices/system/cpu/cpu%d/cache/index%d/", cpu
, level
);
928 scnprintf(file
, PATH_MAX
, "%s/%s", sysfs__mountpoint(), path
);
933 scnprintf(file
, PATH_MAX
, "%s/level", path
);
934 if (sysfs__read_int(file
, (int *) &cache
->level
))
937 scnprintf(file
, PATH_MAX
, "%s/coherency_line_size", path
);
938 if (sysfs__read_int(file
, (int *) &cache
->line_size
))
941 scnprintf(file
, PATH_MAX
, "%s/number_of_sets", path
);
942 if (sysfs__read_int(file
, (int *) &cache
->sets
))
945 scnprintf(file
, PATH_MAX
, "%s/ways_of_associativity", path
);
946 if (sysfs__read_int(file
, (int *) &cache
->ways
))
949 scnprintf(file
, PATH_MAX
, "%s/type", path
);
950 if (sysfs__read_str(file
, &cache
->type
, &len
))
953 cache
->type
[len
] = 0;
954 cache
->type
= rtrim(cache
->type
);
956 scnprintf(file
, PATH_MAX
, "%s/size", path
);
957 if (sysfs__read_str(file
, &cache
->size
, &len
)) {
962 cache
->size
[len
] = 0;
963 cache
->size
= rtrim(cache
->size
);
965 scnprintf(file
, PATH_MAX
, "%s/shared_cpu_list", path
);
966 if (sysfs__read_str(file
, &cache
->map
, &len
)) {
973 cache
->map
= rtrim(cache
->map
);
977 static void cpu_cache_level__fprintf(FILE *out
, struct cpu_cache_level
*c
)
979 fprintf(out
, "L%d %-15s %8s [%s]\n", c
->level
, c
->type
, c
->size
, c
->map
);
982 static int build_caches(struct cpu_cache_level caches
[], u32 size
, u32
*cntp
)
989 ncpus
= sysconf(_SC_NPROCESSORS_CONF
);
993 nr
= (u32
)(ncpus
& UINT_MAX
);
995 for (cpu
= 0; cpu
< nr
; cpu
++) {
996 for (level
= 0; level
< 10; level
++) {
997 struct cpu_cache_level c
;
1000 err
= cpu_cache_level__read(&c
, cpu
, level
);
1007 for (i
= 0; i
< cnt
; i
++) {
1008 if (cpu_cache_level__cmp(&c
, &caches
[i
]))
1015 cpu_cache_level__free(&c
);
1017 if (WARN_ONCE(cnt
== size
, "way too many cpu caches.."))
1026 #define MAX_CACHES 2000
1028 static int write_cache(int fd
, struct perf_header
*h __maybe_unused
,
1029 struct perf_evlist
*evlist __maybe_unused
)
1031 struct cpu_cache_level caches
[MAX_CACHES
];
1032 u32 cnt
= 0, i
, version
= 1;
1035 ret
= build_caches(caches
, MAX_CACHES
, &cnt
);
1039 qsort(&caches
, cnt
, sizeof(struct cpu_cache_level
), cpu_cache_level__sort
);
1041 ret
= do_write(fd
, &version
, sizeof(u32
));
1045 ret
= do_write(fd
, &cnt
, sizeof(u32
));
1049 for (i
= 0; i
< cnt
; i
++) {
1050 struct cpu_cache_level
*c
= &caches
[i
];
1053 ret = do_write(fd, &c->v, sizeof(u32)); \
1064 ret = do_write_string(fd, (const char *) c->v); \
1075 for (i
= 0; i
< cnt
; i
++)
1076 cpu_cache_level__free(&caches
[i
]);
1080 static int write_stat(int fd __maybe_unused
,
1081 struct perf_header
*h __maybe_unused
,
1082 struct perf_evlist
*evlist __maybe_unused
)
1087 static void print_hostname(struct perf_header
*ph
, int fd __maybe_unused
,
1090 fprintf(fp
, "# hostname : %s\n", ph
->env
.hostname
);
1093 static void print_osrelease(struct perf_header
*ph
, int fd __maybe_unused
,
1096 fprintf(fp
, "# os release : %s\n", ph
->env
.os_release
);
1099 static void print_arch(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1101 fprintf(fp
, "# arch : %s\n", ph
->env
.arch
);
1104 static void print_cpudesc(struct perf_header
*ph
, int fd __maybe_unused
,
1107 fprintf(fp
, "# cpudesc : %s\n", ph
->env
.cpu_desc
);
1110 static void print_nrcpus(struct perf_header
*ph
, int fd __maybe_unused
,
1113 fprintf(fp
, "# nrcpus online : %u\n", ph
->env
.nr_cpus_online
);
1114 fprintf(fp
, "# nrcpus avail : %u\n", ph
->env
.nr_cpus_avail
);
1117 static void print_version(struct perf_header
*ph
, int fd __maybe_unused
,
1120 fprintf(fp
, "# perf version : %s\n", ph
->env
.version
);
1123 static void print_cmdline(struct perf_header
*ph
, int fd __maybe_unused
,
1128 nr
= ph
->env
.nr_cmdline
;
1130 fprintf(fp
, "# cmdline : ");
1132 for (i
= 0; i
< nr
; i
++)
1133 fprintf(fp
, "%s ", ph
->env
.cmdline_argv
[i
]);
1137 static void print_cpu_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1142 int cpu_nr
= ph
->env
.nr_cpus_avail
;
1144 nr
= ph
->env
.nr_sibling_cores
;
1145 str
= ph
->env
.sibling_cores
;
1147 for (i
= 0; i
< nr
; i
++) {
1148 fprintf(fp
, "# sibling cores : %s\n", str
);
1149 str
+= strlen(str
) + 1;
1152 nr
= ph
->env
.nr_sibling_threads
;
1153 str
= ph
->env
.sibling_threads
;
1155 for (i
= 0; i
< nr
; i
++) {
1156 fprintf(fp
, "# sibling threads : %s\n", str
);
1157 str
+= strlen(str
) + 1;
1160 if (ph
->env
.cpu
!= NULL
) {
1161 for (i
= 0; i
< cpu_nr
; i
++)
1162 fprintf(fp
, "# CPU %d: Core ID %d, Socket ID %d\n", i
,
1163 ph
->env
.cpu
[i
].core_id
, ph
->env
.cpu
[i
].socket_id
);
1165 fprintf(fp
, "# Core ID and Socket ID information is not available\n");
1168 static void free_event_desc(struct perf_evsel
*events
)
1170 struct perf_evsel
*evsel
;
1175 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1176 zfree(&evsel
->name
);
1183 static struct perf_evsel
*
1184 read_event_desc(struct perf_header
*ph
, int fd
)
1186 struct perf_evsel
*evsel
, *events
= NULL
;
1189 u32 nre
, sz
, nr
, i
, j
;
1193 /* number of events */
1194 ret
= readn(fd
, &nre
, sizeof(nre
));
1195 if (ret
!= (ssize_t
)sizeof(nre
))
1199 nre
= bswap_32(nre
);
1201 ret
= readn(fd
, &sz
, sizeof(sz
));
1202 if (ret
!= (ssize_t
)sizeof(sz
))
1208 /* buffer to hold on file attr struct */
1213 /* the last event terminates with evsel->attr.size == 0: */
1214 events
= calloc(nre
+ 1, sizeof(*events
));
1218 msz
= sizeof(evsel
->attr
);
1222 for (i
= 0, evsel
= events
; i
< nre
; evsel
++, i
++) {
1226 * must read entire on-file attr struct to
1227 * sync up with layout.
1229 ret
= readn(fd
, buf
, sz
);
1230 if (ret
!= (ssize_t
)sz
)
1234 perf_event__attr_swap(buf
);
1236 memcpy(&evsel
->attr
, buf
, msz
);
1238 ret
= readn(fd
, &nr
, sizeof(nr
));
1239 if (ret
!= (ssize_t
)sizeof(nr
))
1242 if (ph
->needs_swap
) {
1244 evsel
->needs_swap
= true;
1247 evsel
->name
= do_read_string(fd
, ph
);
1252 id
= calloc(nr
, sizeof(*id
));
1258 for (j
= 0 ; j
< nr
; j
++) {
1259 ret
= readn(fd
, id
, sizeof(*id
));
1260 if (ret
!= (ssize_t
)sizeof(*id
))
1263 *id
= bswap_64(*id
);
1271 free_event_desc(events
);
1276 static int __desc_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1277 void *priv
__attribute__((unused
)))
1279 return fprintf(fp
, ", %s = %s", name
, val
);
1282 static void print_event_desc(struct perf_header
*ph
, int fd
, FILE *fp
)
1284 struct perf_evsel
*evsel
, *events
= read_event_desc(ph
, fd
);
1289 fprintf(fp
, "# event desc: not available or unable to read\n");
1293 for (evsel
= events
; evsel
->attr
.size
; evsel
++) {
1294 fprintf(fp
, "# event : name = %s, ", evsel
->name
);
1297 fprintf(fp
, ", id = {");
1298 for (j
= 0, id
= evsel
->id
; j
< evsel
->ids
; j
++, id
++) {
1301 fprintf(fp
, " %"PRIu64
, *id
);
1306 perf_event_attr__fprintf(fp
, &evsel
->attr
, __desc_attr__fprintf
, NULL
);
1311 free_event_desc(events
);
1314 static void print_total_mem(struct perf_header
*ph
, int fd __maybe_unused
,
1317 fprintf(fp
, "# total memory : %Lu kB\n", ph
->env
.total_mem
);
1320 static void print_numa_topology(struct perf_header
*ph
, int fd __maybe_unused
,
1324 struct numa_node
*n
;
1326 for (i
= 0; i
< ph
->env
.nr_numa_nodes
; i
++) {
1327 n
= &ph
->env
.numa_nodes
[i
];
1329 fprintf(fp
, "# node%u meminfo : total = %"PRIu64
" kB,"
1330 " free = %"PRIu64
" kB\n",
1331 n
->node
, n
->mem_total
, n
->mem_free
);
1333 fprintf(fp
, "# node%u cpu list : ", n
->node
);
1334 cpu_map__fprintf(n
->map
, fp
);
1338 static void print_cpuid(struct perf_header
*ph
, int fd __maybe_unused
, FILE *fp
)
1340 fprintf(fp
, "# cpuid : %s\n", ph
->env
.cpuid
);
1343 static void print_branch_stack(struct perf_header
*ph __maybe_unused
,
1344 int fd __maybe_unused
, FILE *fp
)
1346 fprintf(fp
, "# contains samples with branch stack\n");
1349 static void print_auxtrace(struct perf_header
*ph __maybe_unused
,
1350 int fd __maybe_unused
, FILE *fp
)
1352 fprintf(fp
, "# contains AUX area data (e.g. instruction trace)\n");
1355 static void print_stat(struct perf_header
*ph __maybe_unused
,
1356 int fd __maybe_unused
, FILE *fp
)
1358 fprintf(fp
, "# contains stat data\n");
1361 static void print_cache(struct perf_header
*ph __maybe_unused
,
1362 int fd __maybe_unused
, FILE *fp __maybe_unused
)
1366 fprintf(fp
, "# CPU cache info:\n");
1367 for (i
= 0; i
< ph
->env
.caches_cnt
; i
++) {
1369 cpu_cache_level__fprintf(fp
, &ph
->env
.caches
[i
]);
1373 static void print_pmu_mappings(struct perf_header
*ph
, int fd __maybe_unused
,
1376 const char *delimiter
= "# pmu mappings: ";
1381 pmu_num
= ph
->env
.nr_pmu_mappings
;
1383 fprintf(fp
, "# pmu mappings: not available\n");
1387 str
= ph
->env
.pmu_mappings
;
1390 type
= strtoul(str
, &tmp
, 0);
1395 fprintf(fp
, "%s%s = %" PRIu32
, delimiter
, str
, type
);
1398 str
+= strlen(str
) + 1;
1407 fprintf(fp
, "# pmu mappings: unable to read\n");
1410 static void print_group_desc(struct perf_header
*ph
, int fd __maybe_unused
,
1413 struct perf_session
*session
;
1414 struct perf_evsel
*evsel
;
1417 session
= container_of(ph
, struct perf_session
, header
);
1419 evlist__for_each_entry(session
->evlist
, evsel
) {
1420 if (perf_evsel__is_group_leader(evsel
) &&
1421 evsel
->nr_members
> 1) {
1422 fprintf(fp
, "# group: %s{%s", evsel
->group_name
?: "",
1423 perf_evsel__name(evsel
));
1425 nr
= evsel
->nr_members
- 1;
1427 fprintf(fp
, ",%s", perf_evsel__name(evsel
));
1435 static int __event_process_build_id(struct build_id_event
*bev
,
1437 struct perf_session
*session
)
1440 struct machine
*machine
;
1443 enum dso_kernel_type dso_type
;
1445 machine
= perf_session__findnew_machine(session
, bev
->pid
);
1449 cpumode
= bev
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1452 case PERF_RECORD_MISC_KERNEL
:
1453 dso_type
= DSO_TYPE_KERNEL
;
1455 case PERF_RECORD_MISC_GUEST_KERNEL
:
1456 dso_type
= DSO_TYPE_GUEST_KERNEL
;
1458 case PERF_RECORD_MISC_USER
:
1459 case PERF_RECORD_MISC_GUEST_USER
:
1460 dso_type
= DSO_TYPE_USER
;
1466 dso
= machine__findnew_dso(machine
, filename
);
1468 char sbuild_id
[SBUILD_ID_SIZE
];
1470 dso__set_build_id(dso
, &bev
->build_id
);
1472 if (!is_kernel_module(filename
, cpumode
))
1473 dso
->kernel
= dso_type
;
1475 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1477 pr_debug("build id event received for %s: %s\n",
1478 dso
->long_name
, sbuild_id
);
1487 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1488 int input
, u64 offset
, u64 size
)
1490 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1492 struct perf_event_header header
;
1493 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1496 struct build_id_event bev
;
1497 char filename
[PATH_MAX
];
1498 u64 limit
= offset
+ size
;
1500 while (offset
< limit
) {
1503 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1506 if (header
->needs_swap
)
1507 perf_event_header__bswap(&old_bev
.header
);
1509 len
= old_bev
.header
.size
- sizeof(old_bev
);
1510 if (readn(input
, filename
, len
) != len
)
1513 bev
.header
= old_bev
.header
;
1516 * As the pid is the missing value, we need to fill
1517 * it properly. The header.misc value give us nice hint.
1519 bev
.pid
= HOST_KERNEL_ID
;
1520 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1521 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1522 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1524 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1525 __event_process_build_id(&bev
, filename
, session
);
1527 offset
+= bev
.header
.size
;
1533 static int perf_header__read_build_ids(struct perf_header
*header
,
1534 int input
, u64 offset
, u64 size
)
1536 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1537 struct build_id_event bev
;
1538 char filename
[PATH_MAX
];
1539 u64 limit
= offset
+ size
, orig_offset
= offset
;
1542 while (offset
< limit
) {
1545 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1548 if (header
->needs_swap
)
1549 perf_event_header__bswap(&bev
.header
);
1551 len
= bev
.header
.size
- sizeof(bev
);
1552 if (readn(input
, filename
, len
) != len
)
1555 * The a1645ce1 changeset:
1557 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1559 * Added a field to struct build_id_event that broke the file
1562 * Since the kernel build-id is the first entry, process the
1563 * table using the old format if the well known
1564 * '[kernel.kallsyms]' string for the kernel build-id has the
1565 * first 4 characters chopped off (where the pid_t sits).
1567 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1568 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1570 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1573 __event_process_build_id(&bev
, filename
, session
);
1575 offset
+= bev
.header
.size
;
1582 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1583 struct perf_header
*ph __maybe_unused
,
1586 ssize_t ret
= trace_report(fd
, data
, false);
1587 return ret
< 0 ? -1 : 0;
1590 static int process_build_id(struct perf_file_section
*section
,
1591 struct perf_header
*ph
, int fd
,
1592 void *data __maybe_unused
)
1594 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1595 pr_debug("Failed to read buildids, continuing...\n");
1599 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1600 struct perf_header
*ph
, int fd
,
1601 void *data __maybe_unused
)
1603 ph
->env
.hostname
= do_read_string(fd
, ph
);
1604 return ph
->env
.hostname
? 0 : -ENOMEM
;
1607 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1608 struct perf_header
*ph
, int fd
,
1609 void *data __maybe_unused
)
1611 ph
->env
.os_release
= do_read_string(fd
, ph
);
1612 return ph
->env
.os_release
? 0 : -ENOMEM
;
1615 static int process_version(struct perf_file_section
*section __maybe_unused
,
1616 struct perf_header
*ph
, int fd
,
1617 void *data __maybe_unused
)
1619 ph
->env
.version
= do_read_string(fd
, ph
);
1620 return ph
->env
.version
? 0 : -ENOMEM
;
1623 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1624 struct perf_header
*ph
, int fd
,
1625 void *data __maybe_unused
)
1627 ph
->env
.arch
= do_read_string(fd
, ph
);
1628 return ph
->env
.arch
? 0 : -ENOMEM
;
1631 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1632 struct perf_header
*ph
, int fd
,
1633 void *data __maybe_unused
)
1638 ret
= readn(fd
, &nr
, sizeof(nr
));
1639 if (ret
!= sizeof(nr
))
1645 ph
->env
.nr_cpus_avail
= nr
;
1647 ret
= readn(fd
, &nr
, sizeof(nr
));
1648 if (ret
!= sizeof(nr
))
1654 ph
->env
.nr_cpus_online
= nr
;
1658 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1659 struct perf_header
*ph
, int fd
,
1660 void *data __maybe_unused
)
1662 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1663 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1666 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1667 struct perf_header
*ph
, int fd
,
1668 void *data __maybe_unused
)
1670 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1671 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1674 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1675 struct perf_header
*ph
, int fd
,
1676 void *data __maybe_unused
)
1681 ret
= readn(fd
, &mem
, sizeof(mem
));
1682 if (ret
!= sizeof(mem
))
1686 mem
= bswap_64(mem
);
1688 ph
->env
.total_mem
= mem
;
1692 static struct perf_evsel
*
1693 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1695 struct perf_evsel
*evsel
;
1697 evlist__for_each_entry(evlist
, evsel
) {
1698 if (evsel
->idx
== idx
)
1706 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1707 struct perf_evsel
*event
)
1709 struct perf_evsel
*evsel
;
1714 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1721 evsel
->name
= strdup(event
->name
);
1725 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1726 struct perf_header
*header
, int fd
,
1727 void *data __maybe_unused
)
1729 struct perf_session
*session
;
1730 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1735 session
= container_of(header
, struct perf_session
, header
);
1736 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1737 perf_evlist__set_event_name(session
->evlist
, evsel
);
1739 free_event_desc(events
);
1744 static int process_cmdline(struct perf_file_section
*section
,
1745 struct perf_header
*ph
, int fd
,
1746 void *data __maybe_unused
)
1749 char *str
, *cmdline
= NULL
, **argv
= NULL
;
1752 ret
= readn(fd
, &nr
, sizeof(nr
));
1753 if (ret
!= sizeof(nr
))
1759 ph
->env
.nr_cmdline
= nr
;
1761 cmdline
= zalloc(section
->size
+ nr
+ 1);
1765 argv
= zalloc(sizeof(char *) * (nr
+ 1));
1769 for (i
= 0; i
< nr
; i
++) {
1770 str
= do_read_string(fd
, ph
);
1774 argv
[i
] = cmdline
+ len
;
1775 memcpy(argv
[i
], str
, strlen(str
) + 1);
1776 len
+= strlen(str
) + 1;
1779 ph
->env
.cmdline
= cmdline
;
1780 ph
->env
.cmdline_argv
= (const char **) argv
;
1789 static int process_cpu_topology(struct perf_file_section
*section
,
1790 struct perf_header
*ph
, int fd
,
1791 void *data __maybe_unused
)
1797 int cpu_nr
= ph
->env
.nr_cpus_avail
;
1800 ph
->env
.cpu
= calloc(cpu_nr
, sizeof(*ph
->env
.cpu
));
1804 ret
= readn(fd
, &nr
, sizeof(nr
));
1805 if (ret
!= sizeof(nr
))
1811 ph
->env
.nr_sibling_cores
= nr
;
1812 size
+= sizeof(u32
);
1813 if (strbuf_init(&sb
, 128) < 0)
1816 for (i
= 0; i
< nr
; i
++) {
1817 str
= do_read_string(fd
, ph
);
1821 /* include a NULL character at the end */
1822 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1824 size
+= string_size(str
);
1827 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1829 ret
= readn(fd
, &nr
, sizeof(nr
));
1830 if (ret
!= sizeof(nr
))
1836 ph
->env
.nr_sibling_threads
= nr
;
1837 size
+= sizeof(u32
);
1839 for (i
= 0; i
< nr
; i
++) {
1840 str
= do_read_string(fd
, ph
);
1844 /* include a NULL character at the end */
1845 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1847 size
+= string_size(str
);
1850 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1853 * The header may be from old perf,
1854 * which doesn't include core id and socket id information.
1856 if (section
->size
<= size
) {
1857 zfree(&ph
->env
.cpu
);
1861 for (i
= 0; i
< (u32
)cpu_nr
; i
++) {
1862 ret
= readn(fd
, &nr
, sizeof(nr
));
1863 if (ret
!= sizeof(nr
))
1869 ph
->env
.cpu
[i
].core_id
= nr
;
1871 ret
= readn(fd
, &nr
, sizeof(nr
));
1872 if (ret
!= sizeof(nr
))
1878 if (nr
!= (u32
)-1 && nr
> (u32
)cpu_nr
) {
1879 pr_debug("socket_id number is too big."
1880 "You may need to upgrade the perf tool.\n");
1884 ph
->env
.cpu
[i
].socket_id
= nr
;
1890 strbuf_release(&sb
);
1892 zfree(&ph
->env
.cpu
);
1896 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1897 struct perf_header
*ph
, int fd
,
1898 void *data __maybe_unused
)
1900 struct numa_node
*nodes
, *n
;
1906 ret
= readn(fd
, &nr
, sizeof(nr
));
1907 if (ret
!= sizeof(nr
))
1913 nodes
= zalloc(sizeof(*nodes
) * nr
);
1917 for (i
= 0; i
< nr
; i
++) {
1921 ret
= readn(fd
, &n
->node
, sizeof(u32
));
1922 if (ret
!= sizeof(n
->node
))
1925 ret
= readn(fd
, &n
->mem_total
, sizeof(u64
));
1926 if (ret
!= sizeof(u64
))
1929 ret
= readn(fd
, &n
->mem_free
, sizeof(u64
));
1930 if (ret
!= sizeof(u64
))
1933 if (ph
->needs_swap
) {
1934 n
->node
= bswap_32(n
->node
);
1935 n
->mem_total
= bswap_64(n
->mem_total
);
1936 n
->mem_free
= bswap_64(n
->mem_free
);
1939 str
= do_read_string(fd
, ph
);
1943 n
->map
= cpu_map__new(str
);
1949 ph
->env
.nr_numa_nodes
= nr
;
1950 ph
->env
.numa_nodes
= nodes
;
1958 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1959 struct perf_header
*ph
, int fd
,
1960 void *data __maybe_unused
)
1968 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1969 if (ret
!= sizeof(pmu_num
))
1973 pmu_num
= bswap_32(pmu_num
);
1976 pr_debug("pmu mappings not available\n");
1980 ph
->env
.nr_pmu_mappings
= pmu_num
;
1981 if (strbuf_init(&sb
, 128) < 0)
1985 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
1988 type
= bswap_32(type
);
1990 name
= do_read_string(fd
, ph
);
1994 if (strbuf_addf(&sb
, "%u:%s", type
, name
) < 0)
1996 /* include a NULL character at the end */
1997 if (strbuf_add(&sb
, "", 1) < 0)
2000 if (!strcmp(name
, "msr"))
2001 ph
->env
.msr_pmu_type
= type
;
2006 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2010 strbuf_release(&sb
);
2014 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
2015 struct perf_header
*ph
, int fd
,
2016 void *data __maybe_unused
)
2019 u32 i
, nr
, nr_groups
;
2020 struct perf_session
*session
;
2021 struct perf_evsel
*evsel
, *leader
= NULL
;
2028 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
2032 nr_groups
= bswap_32(nr_groups
);
2034 ph
->env
.nr_groups
= nr_groups
;
2036 pr_debug("group desc not available\n");
2040 desc
= calloc(nr_groups
, sizeof(*desc
));
2044 for (i
= 0; i
< nr_groups
; i
++) {
2045 desc
[i
].name
= do_read_string(fd
, ph
);
2049 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
2052 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
2055 if (ph
->needs_swap
) {
2056 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
2057 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
2062 * Rebuild group relationship based on the group_desc
2064 session
= container_of(ph
, struct perf_session
, header
);
2065 session
->evlist
->nr_groups
= nr_groups
;
2068 evlist__for_each_entry(session
->evlist
, evsel
) {
2069 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2070 evsel
->leader
= evsel
;
2071 /* {anon_group} is a dummy name */
2072 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2073 evsel
->group_name
= desc
[i
].name
;
2074 desc
[i
].name
= NULL
;
2076 evsel
->nr_members
= desc
[i
].nr_members
;
2078 if (i
>= nr_groups
|| nr
> 0) {
2079 pr_debug("invalid group desc\n");
2084 nr
= evsel
->nr_members
- 1;
2087 /* This is a group member */
2088 evsel
->leader
= leader
;
2094 if (i
!= nr_groups
|| nr
!= 0) {
2095 pr_debug("invalid group desc\n");
2101 for (i
= 0; i
< nr_groups
; i
++)
2102 zfree(&desc
[i
].name
);
2108 static int process_auxtrace(struct perf_file_section
*section
,
2109 struct perf_header
*ph
, int fd
,
2110 void *data __maybe_unused
)
2112 struct perf_session
*session
;
2115 session
= container_of(ph
, struct perf_session
, header
);
2117 err
= auxtrace_index__process(fd
, section
->size
, session
,
2120 pr_err("Failed to process auxtrace index\n");
2124 static int process_cache(struct perf_file_section
*section __maybe_unused
,
2125 struct perf_header
*ph __maybe_unused
, int fd __maybe_unused
,
2126 void *data __maybe_unused
)
2128 struct cpu_cache_level
*caches
;
2129 u32 cnt
, i
, version
;
2131 if (readn(fd
, &version
, sizeof(version
)) != sizeof(version
))
2135 version
= bswap_32(version
);
2140 if (readn(fd
, &cnt
, sizeof(cnt
)) != sizeof(cnt
))
2144 cnt
= bswap_32(cnt
);
2146 caches
= zalloc(sizeof(*caches
) * cnt
);
2150 for (i
= 0; i
< cnt
; i
++) {
2151 struct cpu_cache_level c
;
2154 if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
2155 goto out_free_caches; \
2156 if (ph->needs_swap) \
2157 c.v = bswap_32(c.v); \
2166 c.v = do_read_string(fd, ph); \
2168 goto out_free_caches;
2178 ph
->env
.caches
= caches
;
2179 ph
->env
.caches_cnt
= cnt
;
2186 struct feature_ops
{
2187 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
2188 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
2189 int (*process
)(struct perf_file_section
*section
,
2190 struct perf_header
*h
, int fd
, void *data
);
2195 #define FEAT_OPA(n, func) \
2196 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2197 #define FEAT_OPP(n, func) \
2198 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2199 .process = process_##func }
2200 #define FEAT_OPF(n, func) \
2201 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2202 .process = process_##func, .full_only = true }
2204 /* feature_ops not implemented: */
2205 #define print_tracing_data NULL
2206 #define print_build_id NULL
2208 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2209 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
2210 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
2211 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
2212 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
2213 FEAT_OPP(HEADER_VERSION
, version
),
2214 FEAT_OPP(HEADER_ARCH
, arch
),
2215 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
2216 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
2217 FEAT_OPP(HEADER_CPUID
, cpuid
),
2218 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
2219 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
2220 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
2221 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
2222 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
2223 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
2224 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
2225 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
2226 FEAT_OPP(HEADER_AUXTRACE
, auxtrace
),
2227 FEAT_OPA(HEADER_STAT
, stat
),
2228 FEAT_OPF(HEADER_CACHE
, cache
),
2231 struct header_print_data
{
2233 bool full
; /* extended list of headers */
2236 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2237 struct perf_header
*ph
,
2238 int feat
, int fd
, void *data
)
2240 struct header_print_data
*hd
= data
;
2242 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2243 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2244 "%d, continuing...\n", section
->offset
, feat
);
2247 if (feat
>= HEADER_LAST_FEATURE
) {
2248 pr_warning("unknown feature %d\n", feat
);
2251 if (!feat_ops
[feat
].print
)
2254 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2255 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
2257 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2258 feat_ops
[feat
].name
);
2263 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2265 struct header_print_data hd
;
2266 struct perf_header
*header
= &session
->header
;
2267 int fd
= perf_data_file__fd(session
->file
);
2274 ret
= fstat(fd
, &st
);
2278 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
2280 perf_header__process_sections(header
, fd
, &hd
,
2281 perf_file_section__fprintf_info
);
2283 if (session
->file
->is_pipe
)
2286 fprintf(fp
, "# missing features: ");
2287 for_each_clear_bit(bit
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2289 fprintf(fp
, "%s ", feat_ops
[bit
].name
);
2296 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
2297 struct perf_file_section
**p
,
2298 struct perf_evlist
*evlist
)
2303 if (perf_header__has_feat(h
, type
)) {
2304 if (!feat_ops
[type
].write
)
2307 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
2309 err
= feat_ops
[type
].write(fd
, h
, evlist
);
2311 pr_debug("failed to write feature %s\n", feat_ops
[type
].name
);
2313 /* undo anything written */
2314 lseek(fd
, (*p
)->offset
, SEEK_SET
);
2318 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2324 static int perf_header__adds_write(struct perf_header
*header
,
2325 struct perf_evlist
*evlist
, int fd
)
2328 struct perf_file_section
*feat_sec
, *p
;
2334 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2338 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2339 if (feat_sec
== NULL
)
2342 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2344 sec_start
= header
->feat_offset
;
2345 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2347 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2348 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2349 perf_header__clear_feat(header
, feat
);
2352 lseek(fd
, sec_start
, SEEK_SET
);
2354 * may write more than needed due to dropped feature, but
2355 * this is okay, reader will skip the mising entries
2357 err
= do_write(fd
, feat_sec
, sec_size
);
2359 pr_debug("failed to write feature section\n");
2364 int perf_header__write_pipe(int fd
)
2366 struct perf_pipe_file_header f_header
;
2369 f_header
= (struct perf_pipe_file_header
){
2370 .magic
= PERF_MAGIC
,
2371 .size
= sizeof(f_header
),
2374 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2376 pr_debug("failed to write perf pipe header\n");
2383 int perf_session__write_header(struct perf_session
*session
,
2384 struct perf_evlist
*evlist
,
2385 int fd
, bool at_exit
)
2387 struct perf_file_header f_header
;
2388 struct perf_file_attr f_attr
;
2389 struct perf_header
*header
= &session
->header
;
2390 struct perf_evsel
*evsel
;
2394 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2396 evlist__for_each_entry(session
->evlist
, evsel
) {
2397 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2398 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2400 pr_debug("failed to write perf header\n");
2405 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2407 evlist__for_each_entry(evlist
, evsel
) {
2408 f_attr
= (struct perf_file_attr
){
2409 .attr
= evsel
->attr
,
2411 .offset
= evsel
->id_offset
,
2412 .size
= evsel
->ids
* sizeof(u64
),
2415 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2417 pr_debug("failed to write perf header attribute\n");
2422 if (!header
->data_offset
)
2423 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2424 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2427 err
= perf_header__adds_write(header
, evlist
, fd
);
2432 f_header
= (struct perf_file_header
){
2433 .magic
= PERF_MAGIC
,
2434 .size
= sizeof(f_header
),
2435 .attr_size
= sizeof(f_attr
),
2437 .offset
= attr_offset
,
2438 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2441 .offset
= header
->data_offset
,
2442 .size
= header
->data_size
,
2444 /* event_types is ignored, store zeros */
2447 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2449 lseek(fd
, 0, SEEK_SET
);
2450 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2452 pr_debug("failed to write perf header\n");
2455 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2460 static int perf_header__getbuffer64(struct perf_header
*header
,
2461 int fd
, void *buf
, size_t size
)
2463 if (readn(fd
, buf
, size
) <= 0)
2466 if (header
->needs_swap
)
2467 mem_bswap_64(buf
, size
);
2472 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2474 int (*process
)(struct perf_file_section
*section
,
2475 struct perf_header
*ph
,
2476 int feat
, int fd
, void *data
))
2478 struct perf_file_section
*feat_sec
, *sec
;
2484 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2488 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2492 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2494 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2496 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2500 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2501 err
= process(sec
++, header
, feat
, fd
, data
);
2511 static const int attr_file_abi_sizes
[] = {
2512 [0] = PERF_ATTR_SIZE_VER0
,
2513 [1] = PERF_ATTR_SIZE_VER1
,
2514 [2] = PERF_ATTR_SIZE_VER2
,
2515 [3] = PERF_ATTR_SIZE_VER3
,
2516 [4] = PERF_ATTR_SIZE_VER4
,
2521 * In the legacy file format, the magic number is not used to encode endianness.
2522 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2523 * on ABI revisions, we need to try all combinations for all endianness to
2524 * detect the endianness.
2526 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2528 uint64_t ref_size
, attr_size
;
2531 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2532 ref_size
= attr_file_abi_sizes
[i
]
2533 + sizeof(struct perf_file_section
);
2534 if (hdr_sz
!= ref_size
) {
2535 attr_size
= bswap_64(hdr_sz
);
2536 if (attr_size
!= ref_size
)
2539 ph
->needs_swap
= true;
2541 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2546 /* could not determine endianness */
2550 #define PERF_PIPE_HDR_VER0 16
2552 static const size_t attr_pipe_abi_sizes
[] = {
2553 [0] = PERF_PIPE_HDR_VER0
,
2558 * In the legacy pipe format, there is an implicit assumption that endiannesss
2559 * between host recording the samples, and host parsing the samples is the
2560 * same. This is not always the case given that the pipe output may always be
2561 * redirected into a file and analyzed on a different machine with possibly a
2562 * different endianness and perf_event ABI revsions in the perf tool itself.
2564 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2569 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2570 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2571 attr_size
= bswap_64(hdr_sz
);
2572 if (attr_size
!= hdr_sz
)
2575 ph
->needs_swap
= true;
2577 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2583 bool is_perf_magic(u64 magic
)
2585 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2586 || magic
== __perf_magic2
2587 || magic
== __perf_magic2_sw
)
2593 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2594 bool is_pipe
, struct perf_header
*ph
)
2598 /* check for legacy format */
2599 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2601 ph
->version
= PERF_HEADER_VERSION_1
;
2602 pr_debug("legacy perf.data format\n");
2604 return try_all_pipe_abis(hdr_sz
, ph
);
2606 return try_all_file_abis(hdr_sz
, ph
);
2609 * the new magic number serves two purposes:
2610 * - unique number to identify actual perf.data files
2611 * - encode endianness of file
2613 ph
->version
= PERF_HEADER_VERSION_2
;
2615 /* check magic number with one endianness */
2616 if (magic
== __perf_magic2
)
2619 /* check magic number with opposite endianness */
2620 if (magic
!= __perf_magic2_sw
)
2623 ph
->needs_swap
= true;
2628 int perf_file_header__read(struct perf_file_header
*header
,
2629 struct perf_header
*ph
, int fd
)
2633 lseek(fd
, 0, SEEK_SET
);
2635 ret
= readn(fd
, header
, sizeof(*header
));
2639 if (check_magic_endian(header
->magic
,
2640 header
->attr_size
, false, ph
) < 0) {
2641 pr_debug("magic/endian check failed\n");
2645 if (ph
->needs_swap
) {
2646 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2650 if (header
->size
!= sizeof(*header
)) {
2651 /* Support the previous format */
2652 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2653 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2656 } else if (ph
->needs_swap
) {
2658 * feature bitmap is declared as an array of unsigned longs --
2659 * not good since its size can differ between the host that
2660 * generated the data file and the host analyzing the file.
2662 * We need to handle endianness, but we don't know the size of
2663 * the unsigned long where the file was generated. Take a best
2664 * guess at determining it: try 64-bit swap first (ie., file
2665 * created on a 64-bit host), and check if the hostname feature
2666 * bit is set (this feature bit is forced on as of fbe96f2).
2667 * If the bit is not, undo the 64-bit swap and try a 32-bit
2668 * swap. If the hostname bit is still not set (e.g., older data
2669 * file), punt and fallback to the original behavior --
2670 * clearing all feature bits and setting buildid.
2672 mem_bswap_64(&header
->adds_features
,
2673 BITS_TO_U64(HEADER_FEAT_BITS
));
2675 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2677 mem_bswap_64(&header
->adds_features
,
2678 BITS_TO_U64(HEADER_FEAT_BITS
));
2681 mem_bswap_32(&header
->adds_features
,
2682 BITS_TO_U32(HEADER_FEAT_BITS
));
2685 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2686 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2687 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2691 memcpy(&ph
->adds_features
, &header
->adds_features
,
2692 sizeof(ph
->adds_features
));
2694 ph
->data_offset
= header
->data
.offset
;
2695 ph
->data_size
= header
->data
.size
;
2696 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2700 static int perf_file_section__process(struct perf_file_section
*section
,
2701 struct perf_header
*ph
,
2702 int feat
, int fd
, void *data
)
2704 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2705 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2706 "%d, continuing...\n", section
->offset
, feat
);
2710 if (feat
>= HEADER_LAST_FEATURE
) {
2711 pr_debug("unknown feature %d, continuing...\n", feat
);
2715 if (!feat_ops
[feat
].process
)
2718 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2721 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2722 struct perf_header
*ph
, int fd
,
2727 ret
= readn(fd
, header
, sizeof(*header
));
2731 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2732 pr_debug("endian/magic failed\n");
2737 header
->size
= bswap_64(header
->size
);
2739 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2745 static int perf_header__read_pipe(struct perf_session
*session
)
2747 struct perf_header
*header
= &session
->header
;
2748 struct perf_pipe_file_header f_header
;
2750 if (perf_file_header__read_pipe(&f_header
, header
,
2751 perf_data_file__fd(session
->file
),
2752 session
->repipe
) < 0) {
2753 pr_debug("incompatible file format\n");
2760 static int read_attr(int fd
, struct perf_header
*ph
,
2761 struct perf_file_attr
*f_attr
)
2763 struct perf_event_attr
*attr
= &f_attr
->attr
;
2765 size_t our_sz
= sizeof(f_attr
->attr
);
2768 memset(f_attr
, 0, sizeof(*f_attr
));
2770 /* read minimal guaranteed structure */
2771 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2773 pr_debug("cannot read %d bytes of header attr\n",
2774 PERF_ATTR_SIZE_VER0
);
2778 /* on file perf_event_attr size */
2786 sz
= PERF_ATTR_SIZE_VER0
;
2787 } else if (sz
> our_sz
) {
2788 pr_debug("file uses a more recent and unsupported ABI"
2789 " (%zu bytes extra)\n", sz
- our_sz
);
2792 /* what we have not yet read and that we know about */
2793 left
= sz
- PERF_ATTR_SIZE_VER0
;
2796 ptr
+= PERF_ATTR_SIZE_VER0
;
2798 ret
= readn(fd
, ptr
, left
);
2800 /* read perf_file_section, ids are read in caller */
2801 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2803 return ret
<= 0 ? -1 : 0;
2806 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2807 struct pevent
*pevent
)
2809 struct event_format
*event
;
2812 /* already prepared */
2813 if (evsel
->tp_format
)
2816 if (pevent
== NULL
) {
2817 pr_debug("broken or missing trace data\n");
2821 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2822 if (event
== NULL
) {
2823 pr_debug("cannot find event format for %d\n", (int)evsel
->attr
.config
);
2828 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2829 evsel
->name
= strdup(bf
);
2830 if (evsel
->name
== NULL
)
2834 evsel
->tp_format
= event
;
2838 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2839 struct pevent
*pevent
)
2841 struct perf_evsel
*pos
;
2843 evlist__for_each_entry(evlist
, pos
) {
2844 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2845 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2852 int perf_session__read_header(struct perf_session
*session
)
2854 struct perf_data_file
*file
= session
->file
;
2855 struct perf_header
*header
= &session
->header
;
2856 struct perf_file_header f_header
;
2857 struct perf_file_attr f_attr
;
2859 int nr_attrs
, nr_ids
, i
, j
;
2860 int fd
= perf_data_file__fd(file
);
2862 session
->evlist
= perf_evlist__new();
2863 if (session
->evlist
== NULL
)
2866 session
->evlist
->env
= &header
->env
;
2867 session
->machines
.host
.env
= &header
->env
;
2868 if (perf_data_file__is_pipe(file
))
2869 return perf_header__read_pipe(session
);
2871 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2875 * Sanity check that perf.data was written cleanly; data size is
2876 * initialized to 0 and updated only if the on_exit function is run.
2877 * If data size is still 0 then the file contains only partial
2878 * information. Just warn user and process it as much as it can.
2880 if (f_header
.data
.size
== 0) {
2881 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2882 "Was the 'perf record' command properly terminated?\n",
2886 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2887 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2889 for (i
= 0; i
< nr_attrs
; i
++) {
2890 struct perf_evsel
*evsel
;
2893 if (read_attr(fd
, header
, &f_attr
) < 0)
2896 if (header
->needs_swap
) {
2897 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
2898 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
2899 perf_event__attr_swap(&f_attr
.attr
);
2902 tmp
= lseek(fd
, 0, SEEK_CUR
);
2903 evsel
= perf_evsel__new(&f_attr
.attr
);
2906 goto out_delete_evlist
;
2908 evsel
->needs_swap
= header
->needs_swap
;
2910 * Do it before so that if perf_evsel__alloc_id fails, this
2911 * entry gets purged too at perf_evlist__delete().
2913 perf_evlist__add(session
->evlist
, evsel
);
2915 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2917 * We don't have the cpu and thread maps on the header, so
2918 * for allocating the perf_sample_id table we fake 1 cpu and
2919 * hattr->ids threads.
2921 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2922 goto out_delete_evlist
;
2924 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2926 for (j
= 0; j
< nr_ids
; j
++) {
2927 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2930 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2933 lseek(fd
, tmp
, SEEK_SET
);
2936 symbol_conf
.nr_events
= nr_attrs
;
2938 perf_header__process_sections(header
, fd
, &session
->tevent
,
2939 perf_file_section__process
);
2941 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2942 session
->tevent
.pevent
))
2943 goto out_delete_evlist
;
2950 perf_evlist__delete(session
->evlist
);
2951 session
->evlist
= NULL
;
2955 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2956 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2957 perf_event__handler_t process
)
2959 union perf_event
*ev
;
2963 size
= sizeof(struct perf_event_attr
);
2964 size
= PERF_ALIGN(size
, sizeof(u64
));
2965 size
+= sizeof(struct perf_event_header
);
2966 size
+= ids
* sizeof(u64
);
2973 ev
->attr
.attr
= *attr
;
2974 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2976 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2977 ev
->attr
.header
.size
= (u16
)size
;
2979 if (ev
->attr
.header
.size
== size
)
2980 err
= process(tool
, ev
, NULL
, NULL
);
2989 static struct event_update_event
*
2990 event_update_event__new(size_t size
, u64 type
, u64 id
)
2992 struct event_update_event
*ev
;
2994 size
+= sizeof(*ev
);
2995 size
= PERF_ALIGN(size
, sizeof(u64
));
2999 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3000 ev
->header
.size
= (u16
)size
;
3008 perf_event__synthesize_event_update_unit(struct perf_tool
*tool
,
3009 struct perf_evsel
*evsel
,
3010 perf_event__handler_t process
)
3012 struct event_update_event
*ev
;
3013 size_t size
= strlen(evsel
->unit
);
3016 ev
= event_update_event__new(size
+ 1, PERF_EVENT_UPDATE__UNIT
, evsel
->id
[0]);
3020 strncpy(ev
->data
, evsel
->unit
, size
);
3021 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
3027 perf_event__synthesize_event_update_scale(struct perf_tool
*tool
,
3028 struct perf_evsel
*evsel
,
3029 perf_event__handler_t process
)
3031 struct event_update_event
*ev
;
3032 struct event_update_event_scale
*ev_data
;
3035 ev
= event_update_event__new(sizeof(*ev_data
), PERF_EVENT_UPDATE__SCALE
, evsel
->id
[0]);
3039 ev_data
= (struct event_update_event_scale
*) ev
->data
;
3040 ev_data
->scale
= evsel
->scale
;
3041 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3047 perf_event__synthesize_event_update_name(struct perf_tool
*tool
,
3048 struct perf_evsel
*evsel
,
3049 perf_event__handler_t process
)
3051 struct event_update_event
*ev
;
3052 size_t len
= strlen(evsel
->name
);
3055 ev
= event_update_event__new(len
+ 1, PERF_EVENT_UPDATE__NAME
, evsel
->id
[0]);
3059 strncpy(ev
->data
, evsel
->name
, len
);
3060 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3066 perf_event__synthesize_event_update_cpus(struct perf_tool
*tool
,
3067 struct perf_evsel
*evsel
,
3068 perf_event__handler_t process
)
3070 size_t size
= sizeof(struct event_update_event
);
3071 struct event_update_event
*ev
;
3075 if (!evsel
->own_cpus
)
3078 ev
= cpu_map_data__alloc(evsel
->own_cpus
, &size
, &type
, &max
);
3082 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3083 ev
->header
.size
= (u16
)size
;
3084 ev
->type
= PERF_EVENT_UPDATE__CPUS
;
3085 ev
->id
= evsel
->id
[0];
3087 cpu_map_data__synthesize((struct cpu_map_data
*) ev
->data
,
3091 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3096 size_t perf_event__fprintf_event_update(union perf_event
*event
, FILE *fp
)
3098 struct event_update_event
*ev
= &event
->event_update
;
3099 struct event_update_event_scale
*ev_scale
;
3100 struct event_update_event_cpus
*ev_cpus
;
3101 struct cpu_map
*map
;
3104 ret
= fprintf(fp
, "\n... id: %" PRIu64
"\n", ev
->id
);
3107 case PERF_EVENT_UPDATE__SCALE
:
3108 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3109 ret
+= fprintf(fp
, "... scale: %f\n", ev_scale
->scale
);
3111 case PERF_EVENT_UPDATE__UNIT
:
3112 ret
+= fprintf(fp
, "... unit: %s\n", ev
->data
);
3114 case PERF_EVENT_UPDATE__NAME
:
3115 ret
+= fprintf(fp
, "... name: %s\n", ev
->data
);
3117 case PERF_EVENT_UPDATE__CPUS
:
3118 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3119 ret
+= fprintf(fp
, "... ");
3121 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3123 ret
+= cpu_map__fprintf(map
, fp
);
3125 ret
+= fprintf(fp
, "failed to get cpus\n");
3128 ret
+= fprintf(fp
, "... unknown type\n");
3135 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
3136 struct perf_session
*session
,
3137 perf_event__handler_t process
)
3139 struct perf_evsel
*evsel
;
3142 evlist__for_each_entry(session
->evlist
, evsel
) {
3143 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
3144 evsel
->id
, process
);
3146 pr_debug("failed to create perf header attribute\n");
3154 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
3155 union perf_event
*event
,
3156 struct perf_evlist
**pevlist
)
3159 struct perf_evsel
*evsel
;
3160 struct perf_evlist
*evlist
= *pevlist
;
3162 if (evlist
== NULL
) {
3163 *pevlist
= evlist
= perf_evlist__new();
3168 evsel
= perf_evsel__new(&event
->attr
.attr
);
3172 perf_evlist__add(evlist
, evsel
);
3174 ids
= event
->header
.size
;
3175 ids
-= (void *)&event
->attr
.id
- (void *)event
;
3176 n_ids
= ids
/ sizeof(u64
);
3178 * We don't have the cpu and thread maps on the header, so
3179 * for allocating the perf_sample_id table we fake 1 cpu and
3180 * hattr->ids threads.
3182 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
3185 for (i
= 0; i
< n_ids
; i
++) {
3186 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
3189 symbol_conf
.nr_events
= evlist
->nr_entries
;
3194 int perf_event__process_event_update(struct perf_tool
*tool __maybe_unused
,
3195 union perf_event
*event
,
3196 struct perf_evlist
**pevlist
)
3198 struct event_update_event
*ev
= &event
->event_update
;
3199 struct event_update_event_scale
*ev_scale
;
3200 struct event_update_event_cpus
*ev_cpus
;
3201 struct perf_evlist
*evlist
;
3202 struct perf_evsel
*evsel
;
3203 struct cpu_map
*map
;
3205 if (!pevlist
|| *pevlist
== NULL
)
3210 evsel
= perf_evlist__id2evsel(evlist
, ev
->id
);
3215 case PERF_EVENT_UPDATE__UNIT
:
3216 evsel
->unit
= strdup(ev
->data
);
3218 case PERF_EVENT_UPDATE__NAME
:
3219 evsel
->name
= strdup(ev
->data
);
3221 case PERF_EVENT_UPDATE__SCALE
:
3222 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3223 evsel
->scale
= ev_scale
->scale
;
3225 case PERF_EVENT_UPDATE__CPUS
:
3226 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3228 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3230 evsel
->own_cpus
= map
;
3232 pr_err("failed to get event_update cpus\n");
3240 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
3241 struct perf_evlist
*evlist
,
3242 perf_event__handler_t process
)
3244 union perf_event ev
;
3245 struct tracing_data
*tdata
;
3246 ssize_t size
= 0, aligned_size
= 0, padding
;
3247 int err __maybe_unused
= 0;
3250 * We are going to store the size of the data followed
3251 * by the data contents. Since the fd descriptor is a pipe,
3252 * we cannot seek back to store the size of the data once
3253 * we know it. Instead we:
3255 * - write the tracing data to the temp file
3256 * - get/write the data size to pipe
3257 * - write the tracing data from the temp file
3260 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3264 memset(&ev
, 0, sizeof(ev
));
3266 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3268 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3269 padding
= aligned_size
- size
;
3270 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3271 ev
.tracing_data
.size
= aligned_size
;
3273 process(tool
, &ev
, NULL
, NULL
);
3276 * The put function will copy all the tracing data
3277 * stored in temp file to the pipe.
3279 tracing_data_put(tdata
);
3281 write_padded(fd
, NULL
, 0, padding
);
3283 return aligned_size
;
3286 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
3287 union perf_event
*event
,
3288 struct perf_session
*session
)
3290 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3291 int fd
= perf_data_file__fd(session
->file
);
3292 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
3295 /* setup for reading amidst mmap */
3296 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
3299 size_read
= trace_report(fd
, &session
->tevent
,
3301 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3303 if (readn(fd
, buf
, padding
) < 0) {
3304 pr_err("%s: reading input file", __func__
);
3307 if (session
->repipe
) {
3308 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3309 if (retw
<= 0 || retw
!= padding
) {
3310 pr_err("%s: repiping tracing data padding", __func__
);
3315 if (size_read
+ padding
!= size
) {
3316 pr_err("%s: tracing data size mismatch", __func__
);
3320 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3321 session
->tevent
.pevent
);
3323 return size_read
+ padding
;
3326 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3327 struct dso
*pos
, u16 misc
,
3328 perf_event__handler_t process
,
3329 struct machine
*machine
)
3331 union perf_event ev
;
3338 memset(&ev
, 0, sizeof(ev
));
3340 len
= pos
->long_name_len
+ 1;
3341 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3342 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3343 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3344 ev
.build_id
.header
.misc
= misc
;
3345 ev
.build_id
.pid
= machine
->pid
;
3346 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3347 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3349 err
= process(tool
, &ev
, NULL
, machine
);
3354 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3355 union perf_event
*event
,
3356 struct perf_session
*session
)
3358 __event_process_build_id(&event
->build_id
,
3359 event
->build_id
.filename
,