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/$(SRCARCH)/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 (dso_type
!= DSO_TYPE_USER
) {
1473 struct kmod_path m
= { .name
= NULL
, };
1475 if (!kmod_path__parse_name(&m
, filename
) && m
.kmod
)
1476 dso__set_module_info(dso
, &m
, machine
);
1478 dso
->kernel
= dso_type
;
1483 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1485 pr_debug("build id event received for %s: %s\n",
1486 dso
->long_name
, sbuild_id
);
1495 static int perf_header__read_build_ids_abi_quirk(struct perf_header
*header
,
1496 int input
, u64 offset
, u64 size
)
1498 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1500 struct perf_event_header header
;
1501 u8 build_id
[PERF_ALIGN(BUILD_ID_SIZE
, sizeof(u64
))];
1504 struct build_id_event bev
;
1505 char filename
[PATH_MAX
];
1506 u64 limit
= offset
+ size
;
1508 while (offset
< limit
) {
1511 if (readn(input
, &old_bev
, sizeof(old_bev
)) != sizeof(old_bev
))
1514 if (header
->needs_swap
)
1515 perf_event_header__bswap(&old_bev
.header
);
1517 len
= old_bev
.header
.size
- sizeof(old_bev
);
1518 if (readn(input
, filename
, len
) != len
)
1521 bev
.header
= old_bev
.header
;
1524 * As the pid is the missing value, we need to fill
1525 * it properly. The header.misc value give us nice hint.
1527 bev
.pid
= HOST_KERNEL_ID
;
1528 if (bev
.header
.misc
== PERF_RECORD_MISC_GUEST_USER
||
1529 bev
.header
.misc
== PERF_RECORD_MISC_GUEST_KERNEL
)
1530 bev
.pid
= DEFAULT_GUEST_KERNEL_ID
;
1532 memcpy(bev
.build_id
, old_bev
.build_id
, sizeof(bev
.build_id
));
1533 __event_process_build_id(&bev
, filename
, session
);
1535 offset
+= bev
.header
.size
;
1541 static int perf_header__read_build_ids(struct perf_header
*header
,
1542 int input
, u64 offset
, u64 size
)
1544 struct perf_session
*session
= container_of(header
, struct perf_session
, header
);
1545 struct build_id_event bev
;
1546 char filename
[PATH_MAX
];
1547 u64 limit
= offset
+ size
, orig_offset
= offset
;
1550 while (offset
< limit
) {
1553 if (readn(input
, &bev
, sizeof(bev
)) != sizeof(bev
))
1556 if (header
->needs_swap
)
1557 perf_event_header__bswap(&bev
.header
);
1559 len
= bev
.header
.size
- sizeof(bev
);
1560 if (readn(input
, filename
, len
) != len
)
1563 * The a1645ce1 changeset:
1565 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1567 * Added a field to struct build_id_event that broke the file
1570 * Since the kernel build-id is the first entry, process the
1571 * table using the old format if the well known
1572 * '[kernel.kallsyms]' string for the kernel build-id has the
1573 * first 4 characters chopped off (where the pid_t sits).
1575 if (memcmp(filename
, "nel.kallsyms]", 13) == 0) {
1576 if (lseek(input
, orig_offset
, SEEK_SET
) == (off_t
)-1)
1578 return perf_header__read_build_ids_abi_quirk(header
, input
, offset
, size
);
1581 __event_process_build_id(&bev
, filename
, session
);
1583 offset
+= bev
.header
.size
;
1590 static int process_tracing_data(struct perf_file_section
*section __maybe_unused
,
1591 struct perf_header
*ph __maybe_unused
,
1594 ssize_t ret
= trace_report(fd
, data
, false);
1595 return ret
< 0 ? -1 : 0;
1598 static int process_build_id(struct perf_file_section
*section
,
1599 struct perf_header
*ph
, int fd
,
1600 void *data __maybe_unused
)
1602 if (perf_header__read_build_ids(ph
, fd
, section
->offset
, section
->size
))
1603 pr_debug("Failed to read buildids, continuing...\n");
1607 static int process_hostname(struct perf_file_section
*section __maybe_unused
,
1608 struct perf_header
*ph
, int fd
,
1609 void *data __maybe_unused
)
1611 ph
->env
.hostname
= do_read_string(fd
, ph
);
1612 return ph
->env
.hostname
? 0 : -ENOMEM
;
1615 static int process_osrelease(struct perf_file_section
*section __maybe_unused
,
1616 struct perf_header
*ph
, int fd
,
1617 void *data __maybe_unused
)
1619 ph
->env
.os_release
= do_read_string(fd
, ph
);
1620 return ph
->env
.os_release
? 0 : -ENOMEM
;
1623 static int process_version(struct perf_file_section
*section __maybe_unused
,
1624 struct perf_header
*ph
, int fd
,
1625 void *data __maybe_unused
)
1627 ph
->env
.version
= do_read_string(fd
, ph
);
1628 return ph
->env
.version
? 0 : -ENOMEM
;
1631 static int process_arch(struct perf_file_section
*section __maybe_unused
,
1632 struct perf_header
*ph
, int fd
,
1633 void *data __maybe_unused
)
1635 ph
->env
.arch
= do_read_string(fd
, ph
);
1636 return ph
->env
.arch
? 0 : -ENOMEM
;
1639 static int process_nrcpus(struct perf_file_section
*section __maybe_unused
,
1640 struct perf_header
*ph
, int fd
,
1641 void *data __maybe_unused
)
1646 ret
= readn(fd
, &nr
, sizeof(nr
));
1647 if (ret
!= sizeof(nr
))
1653 ph
->env
.nr_cpus_avail
= nr
;
1655 ret
= readn(fd
, &nr
, sizeof(nr
));
1656 if (ret
!= sizeof(nr
))
1662 ph
->env
.nr_cpus_online
= nr
;
1666 static int process_cpudesc(struct perf_file_section
*section __maybe_unused
,
1667 struct perf_header
*ph
, int fd
,
1668 void *data __maybe_unused
)
1670 ph
->env
.cpu_desc
= do_read_string(fd
, ph
);
1671 return ph
->env
.cpu_desc
? 0 : -ENOMEM
;
1674 static int process_cpuid(struct perf_file_section
*section __maybe_unused
,
1675 struct perf_header
*ph
, int fd
,
1676 void *data __maybe_unused
)
1678 ph
->env
.cpuid
= do_read_string(fd
, ph
);
1679 return ph
->env
.cpuid
? 0 : -ENOMEM
;
1682 static int process_total_mem(struct perf_file_section
*section __maybe_unused
,
1683 struct perf_header
*ph
, int fd
,
1684 void *data __maybe_unused
)
1689 ret
= readn(fd
, &mem
, sizeof(mem
));
1690 if (ret
!= sizeof(mem
))
1694 mem
= bswap_64(mem
);
1696 ph
->env
.total_mem
= mem
;
1700 static struct perf_evsel
*
1701 perf_evlist__find_by_index(struct perf_evlist
*evlist
, int idx
)
1703 struct perf_evsel
*evsel
;
1705 evlist__for_each_entry(evlist
, evsel
) {
1706 if (evsel
->idx
== idx
)
1714 perf_evlist__set_event_name(struct perf_evlist
*evlist
,
1715 struct perf_evsel
*event
)
1717 struct perf_evsel
*evsel
;
1722 evsel
= perf_evlist__find_by_index(evlist
, event
->idx
);
1729 evsel
->name
= strdup(event
->name
);
1733 process_event_desc(struct perf_file_section
*section __maybe_unused
,
1734 struct perf_header
*header
, int fd
,
1735 void *data __maybe_unused
)
1737 struct perf_session
*session
;
1738 struct perf_evsel
*evsel
, *events
= read_event_desc(header
, fd
);
1743 session
= container_of(header
, struct perf_session
, header
);
1744 for (evsel
= events
; evsel
->attr
.size
; evsel
++)
1745 perf_evlist__set_event_name(session
->evlist
, evsel
);
1747 free_event_desc(events
);
1752 static int process_cmdline(struct perf_file_section
*section
,
1753 struct perf_header
*ph
, int fd
,
1754 void *data __maybe_unused
)
1757 char *str
, *cmdline
= NULL
, **argv
= NULL
;
1760 ret
= readn(fd
, &nr
, sizeof(nr
));
1761 if (ret
!= sizeof(nr
))
1767 ph
->env
.nr_cmdline
= nr
;
1769 cmdline
= zalloc(section
->size
+ nr
+ 1);
1773 argv
= zalloc(sizeof(char *) * (nr
+ 1));
1777 for (i
= 0; i
< nr
; i
++) {
1778 str
= do_read_string(fd
, ph
);
1782 argv
[i
] = cmdline
+ len
;
1783 memcpy(argv
[i
], str
, strlen(str
) + 1);
1784 len
+= strlen(str
) + 1;
1787 ph
->env
.cmdline
= cmdline
;
1788 ph
->env
.cmdline_argv
= (const char **) argv
;
1797 static int process_cpu_topology(struct perf_file_section
*section
,
1798 struct perf_header
*ph
, int fd
,
1799 void *data __maybe_unused
)
1805 int cpu_nr
= ph
->env
.nr_cpus_avail
;
1808 ph
->env
.cpu
= calloc(cpu_nr
, sizeof(*ph
->env
.cpu
));
1812 ret
= readn(fd
, &nr
, sizeof(nr
));
1813 if (ret
!= sizeof(nr
))
1819 ph
->env
.nr_sibling_cores
= nr
;
1820 size
+= sizeof(u32
);
1821 if (strbuf_init(&sb
, 128) < 0)
1824 for (i
= 0; i
< nr
; i
++) {
1825 str
= do_read_string(fd
, ph
);
1829 /* include a NULL character at the end */
1830 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1832 size
+= string_size(str
);
1835 ph
->env
.sibling_cores
= strbuf_detach(&sb
, NULL
);
1837 ret
= readn(fd
, &nr
, sizeof(nr
));
1838 if (ret
!= sizeof(nr
))
1844 ph
->env
.nr_sibling_threads
= nr
;
1845 size
+= sizeof(u32
);
1847 for (i
= 0; i
< nr
; i
++) {
1848 str
= do_read_string(fd
, ph
);
1852 /* include a NULL character at the end */
1853 if (strbuf_add(&sb
, str
, strlen(str
) + 1) < 0)
1855 size
+= string_size(str
);
1858 ph
->env
.sibling_threads
= strbuf_detach(&sb
, NULL
);
1861 * The header may be from old perf,
1862 * which doesn't include core id and socket id information.
1864 if (section
->size
<= size
) {
1865 zfree(&ph
->env
.cpu
);
1869 for (i
= 0; i
< (u32
)cpu_nr
; i
++) {
1870 ret
= readn(fd
, &nr
, sizeof(nr
));
1871 if (ret
!= sizeof(nr
))
1877 ph
->env
.cpu
[i
].core_id
= nr
;
1879 ret
= readn(fd
, &nr
, sizeof(nr
));
1880 if (ret
!= sizeof(nr
))
1886 if (nr
!= (u32
)-1 && nr
> (u32
)cpu_nr
) {
1887 pr_debug("socket_id number is too big."
1888 "You may need to upgrade the perf tool.\n");
1892 ph
->env
.cpu
[i
].socket_id
= nr
;
1898 strbuf_release(&sb
);
1900 zfree(&ph
->env
.cpu
);
1904 static int process_numa_topology(struct perf_file_section
*section __maybe_unused
,
1905 struct perf_header
*ph
, int fd
,
1906 void *data __maybe_unused
)
1908 struct numa_node
*nodes
, *n
;
1914 ret
= readn(fd
, &nr
, sizeof(nr
));
1915 if (ret
!= sizeof(nr
))
1921 nodes
= zalloc(sizeof(*nodes
) * nr
);
1925 for (i
= 0; i
< nr
; i
++) {
1929 ret
= readn(fd
, &n
->node
, sizeof(u32
));
1930 if (ret
!= sizeof(n
->node
))
1933 ret
= readn(fd
, &n
->mem_total
, sizeof(u64
));
1934 if (ret
!= sizeof(u64
))
1937 ret
= readn(fd
, &n
->mem_free
, sizeof(u64
));
1938 if (ret
!= sizeof(u64
))
1941 if (ph
->needs_swap
) {
1942 n
->node
= bswap_32(n
->node
);
1943 n
->mem_total
= bswap_64(n
->mem_total
);
1944 n
->mem_free
= bswap_64(n
->mem_free
);
1947 str
= do_read_string(fd
, ph
);
1951 n
->map
= cpu_map__new(str
);
1957 ph
->env
.nr_numa_nodes
= nr
;
1958 ph
->env
.numa_nodes
= nodes
;
1966 static int process_pmu_mappings(struct perf_file_section
*section __maybe_unused
,
1967 struct perf_header
*ph
, int fd
,
1968 void *data __maybe_unused
)
1976 ret
= readn(fd
, &pmu_num
, sizeof(pmu_num
));
1977 if (ret
!= sizeof(pmu_num
))
1981 pmu_num
= bswap_32(pmu_num
);
1984 pr_debug("pmu mappings not available\n");
1988 ph
->env
.nr_pmu_mappings
= pmu_num
;
1989 if (strbuf_init(&sb
, 128) < 0)
1993 if (readn(fd
, &type
, sizeof(type
)) != sizeof(type
))
1996 type
= bswap_32(type
);
1998 name
= do_read_string(fd
, ph
);
2002 if (strbuf_addf(&sb
, "%u:%s", type
, name
) < 0)
2004 /* include a NULL character at the end */
2005 if (strbuf_add(&sb
, "", 1) < 0)
2008 if (!strcmp(name
, "msr"))
2009 ph
->env
.msr_pmu_type
= type
;
2014 ph
->env
.pmu_mappings
= strbuf_detach(&sb
, NULL
);
2018 strbuf_release(&sb
);
2022 static int process_group_desc(struct perf_file_section
*section __maybe_unused
,
2023 struct perf_header
*ph
, int fd
,
2024 void *data __maybe_unused
)
2027 u32 i
, nr
, nr_groups
;
2028 struct perf_session
*session
;
2029 struct perf_evsel
*evsel
, *leader
= NULL
;
2036 if (readn(fd
, &nr_groups
, sizeof(nr_groups
)) != sizeof(nr_groups
))
2040 nr_groups
= bswap_32(nr_groups
);
2042 ph
->env
.nr_groups
= nr_groups
;
2044 pr_debug("group desc not available\n");
2048 desc
= calloc(nr_groups
, sizeof(*desc
));
2052 for (i
= 0; i
< nr_groups
; i
++) {
2053 desc
[i
].name
= do_read_string(fd
, ph
);
2057 if (readn(fd
, &desc
[i
].leader_idx
, sizeof(u32
)) != sizeof(u32
))
2060 if (readn(fd
, &desc
[i
].nr_members
, sizeof(u32
)) != sizeof(u32
))
2063 if (ph
->needs_swap
) {
2064 desc
[i
].leader_idx
= bswap_32(desc
[i
].leader_idx
);
2065 desc
[i
].nr_members
= bswap_32(desc
[i
].nr_members
);
2070 * Rebuild group relationship based on the group_desc
2072 session
= container_of(ph
, struct perf_session
, header
);
2073 session
->evlist
->nr_groups
= nr_groups
;
2076 evlist__for_each_entry(session
->evlist
, evsel
) {
2077 if (evsel
->idx
== (int) desc
[i
].leader_idx
) {
2078 evsel
->leader
= evsel
;
2079 /* {anon_group} is a dummy name */
2080 if (strcmp(desc
[i
].name
, "{anon_group}")) {
2081 evsel
->group_name
= desc
[i
].name
;
2082 desc
[i
].name
= NULL
;
2084 evsel
->nr_members
= desc
[i
].nr_members
;
2086 if (i
>= nr_groups
|| nr
> 0) {
2087 pr_debug("invalid group desc\n");
2092 nr
= evsel
->nr_members
- 1;
2095 /* This is a group member */
2096 evsel
->leader
= leader
;
2102 if (i
!= nr_groups
|| nr
!= 0) {
2103 pr_debug("invalid group desc\n");
2109 for (i
= 0; i
< nr_groups
; i
++)
2110 zfree(&desc
[i
].name
);
2116 static int process_auxtrace(struct perf_file_section
*section
,
2117 struct perf_header
*ph
, int fd
,
2118 void *data __maybe_unused
)
2120 struct perf_session
*session
;
2123 session
= container_of(ph
, struct perf_session
, header
);
2125 err
= auxtrace_index__process(fd
, section
->size
, session
,
2128 pr_err("Failed to process auxtrace index\n");
2132 static int process_cache(struct perf_file_section
*section __maybe_unused
,
2133 struct perf_header
*ph __maybe_unused
, int fd __maybe_unused
,
2134 void *data __maybe_unused
)
2136 struct cpu_cache_level
*caches
;
2137 u32 cnt
, i
, version
;
2139 if (readn(fd
, &version
, sizeof(version
)) != sizeof(version
))
2143 version
= bswap_32(version
);
2148 if (readn(fd
, &cnt
, sizeof(cnt
)) != sizeof(cnt
))
2152 cnt
= bswap_32(cnt
);
2154 caches
= zalloc(sizeof(*caches
) * cnt
);
2158 for (i
= 0; i
< cnt
; i
++) {
2159 struct cpu_cache_level c
;
2162 if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
2163 goto out_free_caches; \
2164 if (ph->needs_swap) \
2165 c.v = bswap_32(c.v); \
2174 c.v = do_read_string(fd, ph); \
2176 goto out_free_caches;
2186 ph
->env
.caches
= caches
;
2187 ph
->env
.caches_cnt
= cnt
;
2194 struct feature_ops
{
2195 int (*write
)(int fd
, struct perf_header
*h
, struct perf_evlist
*evlist
);
2196 void (*print
)(struct perf_header
*h
, int fd
, FILE *fp
);
2197 int (*process
)(struct perf_file_section
*section
,
2198 struct perf_header
*h
, int fd
, void *data
);
2203 #define FEAT_OPA(n, func) \
2204 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2205 #define FEAT_OPP(n, func) \
2206 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2207 .process = process_##func }
2208 #define FEAT_OPF(n, func) \
2209 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2210 .process = process_##func, .full_only = true }
2212 /* feature_ops not implemented: */
2213 #define print_tracing_data NULL
2214 #define print_build_id NULL
2216 static const struct feature_ops feat_ops
[HEADER_LAST_FEATURE
] = {
2217 FEAT_OPP(HEADER_TRACING_DATA
, tracing_data
),
2218 FEAT_OPP(HEADER_BUILD_ID
, build_id
),
2219 FEAT_OPP(HEADER_HOSTNAME
, hostname
),
2220 FEAT_OPP(HEADER_OSRELEASE
, osrelease
),
2221 FEAT_OPP(HEADER_VERSION
, version
),
2222 FEAT_OPP(HEADER_ARCH
, arch
),
2223 FEAT_OPP(HEADER_NRCPUS
, nrcpus
),
2224 FEAT_OPP(HEADER_CPUDESC
, cpudesc
),
2225 FEAT_OPP(HEADER_CPUID
, cpuid
),
2226 FEAT_OPP(HEADER_TOTAL_MEM
, total_mem
),
2227 FEAT_OPP(HEADER_EVENT_DESC
, event_desc
),
2228 FEAT_OPP(HEADER_CMDLINE
, cmdline
),
2229 FEAT_OPF(HEADER_CPU_TOPOLOGY
, cpu_topology
),
2230 FEAT_OPF(HEADER_NUMA_TOPOLOGY
, numa_topology
),
2231 FEAT_OPA(HEADER_BRANCH_STACK
, branch_stack
),
2232 FEAT_OPP(HEADER_PMU_MAPPINGS
, pmu_mappings
),
2233 FEAT_OPP(HEADER_GROUP_DESC
, group_desc
),
2234 FEAT_OPP(HEADER_AUXTRACE
, auxtrace
),
2235 FEAT_OPA(HEADER_STAT
, stat
),
2236 FEAT_OPF(HEADER_CACHE
, cache
),
2239 struct header_print_data
{
2241 bool full
; /* extended list of headers */
2244 static int perf_file_section__fprintf_info(struct perf_file_section
*section
,
2245 struct perf_header
*ph
,
2246 int feat
, int fd
, void *data
)
2248 struct header_print_data
*hd
= data
;
2250 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2251 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2252 "%d, continuing...\n", section
->offset
, feat
);
2255 if (feat
>= HEADER_LAST_FEATURE
) {
2256 pr_warning("unknown feature %d\n", feat
);
2259 if (!feat_ops
[feat
].print
)
2262 if (!feat_ops
[feat
].full_only
|| hd
->full
)
2263 feat_ops
[feat
].print(ph
, fd
, hd
->fp
);
2265 fprintf(hd
->fp
, "# %s info available, use -I to display\n",
2266 feat_ops
[feat
].name
);
2271 int perf_header__fprintf_info(struct perf_session
*session
, FILE *fp
, bool full
)
2273 struct header_print_data hd
;
2274 struct perf_header
*header
= &session
->header
;
2275 int fd
= perf_data_file__fd(session
->file
);
2282 ret
= fstat(fd
, &st
);
2286 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
2288 perf_header__process_sections(header
, fd
, &hd
,
2289 perf_file_section__fprintf_info
);
2291 if (session
->file
->is_pipe
)
2294 fprintf(fp
, "# missing features: ");
2295 for_each_clear_bit(bit
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2297 fprintf(fp
, "%s ", feat_ops
[bit
].name
);
2304 static int do_write_feat(int fd
, struct perf_header
*h
, int type
,
2305 struct perf_file_section
**p
,
2306 struct perf_evlist
*evlist
)
2311 if (perf_header__has_feat(h
, type
)) {
2312 if (!feat_ops
[type
].write
)
2315 (*p
)->offset
= lseek(fd
, 0, SEEK_CUR
);
2317 err
= feat_ops
[type
].write(fd
, h
, evlist
);
2319 pr_debug("failed to write feature %s\n", feat_ops
[type
].name
);
2321 /* undo anything written */
2322 lseek(fd
, (*p
)->offset
, SEEK_SET
);
2326 (*p
)->size
= lseek(fd
, 0, SEEK_CUR
) - (*p
)->offset
;
2332 static int perf_header__adds_write(struct perf_header
*header
,
2333 struct perf_evlist
*evlist
, int fd
)
2336 struct perf_file_section
*feat_sec
, *p
;
2342 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2346 feat_sec
= p
= calloc(nr_sections
, sizeof(*feat_sec
));
2347 if (feat_sec
== NULL
)
2350 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2352 sec_start
= header
->feat_offset
;
2353 lseek(fd
, sec_start
+ sec_size
, SEEK_SET
);
2355 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
2356 if (do_write_feat(fd
, header
, feat
, &p
, evlist
))
2357 perf_header__clear_feat(header
, feat
);
2360 lseek(fd
, sec_start
, SEEK_SET
);
2362 * may write more than needed due to dropped feature, but
2363 * this is okay, reader will skip the mising entries
2365 err
= do_write(fd
, feat_sec
, sec_size
);
2367 pr_debug("failed to write feature section\n");
2372 int perf_header__write_pipe(int fd
)
2374 struct perf_pipe_file_header f_header
;
2377 f_header
= (struct perf_pipe_file_header
){
2378 .magic
= PERF_MAGIC
,
2379 .size
= sizeof(f_header
),
2382 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2384 pr_debug("failed to write perf pipe header\n");
2391 int perf_session__write_header(struct perf_session
*session
,
2392 struct perf_evlist
*evlist
,
2393 int fd
, bool at_exit
)
2395 struct perf_file_header f_header
;
2396 struct perf_file_attr f_attr
;
2397 struct perf_header
*header
= &session
->header
;
2398 struct perf_evsel
*evsel
;
2402 lseek(fd
, sizeof(f_header
), SEEK_SET
);
2404 evlist__for_each_entry(session
->evlist
, evsel
) {
2405 evsel
->id_offset
= lseek(fd
, 0, SEEK_CUR
);
2406 err
= do_write(fd
, evsel
->id
, evsel
->ids
* sizeof(u64
));
2408 pr_debug("failed to write perf header\n");
2413 attr_offset
= lseek(fd
, 0, SEEK_CUR
);
2415 evlist__for_each_entry(evlist
, evsel
) {
2416 f_attr
= (struct perf_file_attr
){
2417 .attr
= evsel
->attr
,
2419 .offset
= evsel
->id_offset
,
2420 .size
= evsel
->ids
* sizeof(u64
),
2423 err
= do_write(fd
, &f_attr
, sizeof(f_attr
));
2425 pr_debug("failed to write perf header attribute\n");
2430 if (!header
->data_offset
)
2431 header
->data_offset
= lseek(fd
, 0, SEEK_CUR
);
2432 header
->feat_offset
= header
->data_offset
+ header
->data_size
;
2435 err
= perf_header__adds_write(header
, evlist
, fd
);
2440 f_header
= (struct perf_file_header
){
2441 .magic
= PERF_MAGIC
,
2442 .size
= sizeof(f_header
),
2443 .attr_size
= sizeof(f_attr
),
2445 .offset
= attr_offset
,
2446 .size
= evlist
->nr_entries
* sizeof(f_attr
),
2449 .offset
= header
->data_offset
,
2450 .size
= header
->data_size
,
2452 /* event_types is ignored, store zeros */
2455 memcpy(&f_header
.adds_features
, &header
->adds_features
, sizeof(header
->adds_features
));
2457 lseek(fd
, 0, SEEK_SET
);
2458 err
= do_write(fd
, &f_header
, sizeof(f_header
));
2460 pr_debug("failed to write perf header\n");
2463 lseek(fd
, header
->data_offset
+ header
->data_size
, SEEK_SET
);
2468 static int perf_header__getbuffer64(struct perf_header
*header
,
2469 int fd
, void *buf
, size_t size
)
2471 if (readn(fd
, buf
, size
) <= 0)
2474 if (header
->needs_swap
)
2475 mem_bswap_64(buf
, size
);
2480 int perf_header__process_sections(struct perf_header
*header
, int fd
,
2482 int (*process
)(struct perf_file_section
*section
,
2483 struct perf_header
*ph
,
2484 int feat
, int fd
, void *data
))
2486 struct perf_file_section
*feat_sec
, *sec
;
2492 nr_sections
= bitmap_weight(header
->adds_features
, HEADER_FEAT_BITS
);
2496 feat_sec
= sec
= calloc(nr_sections
, sizeof(*feat_sec
));
2500 sec_size
= sizeof(*feat_sec
) * nr_sections
;
2502 lseek(fd
, header
->feat_offset
, SEEK_SET
);
2504 err
= perf_header__getbuffer64(header
, fd
, feat_sec
, sec_size
);
2508 for_each_set_bit(feat
, header
->adds_features
, HEADER_LAST_FEATURE
) {
2509 err
= process(sec
++, header
, feat
, fd
, data
);
2519 static const int attr_file_abi_sizes
[] = {
2520 [0] = PERF_ATTR_SIZE_VER0
,
2521 [1] = PERF_ATTR_SIZE_VER1
,
2522 [2] = PERF_ATTR_SIZE_VER2
,
2523 [3] = PERF_ATTR_SIZE_VER3
,
2524 [4] = PERF_ATTR_SIZE_VER4
,
2529 * In the legacy file format, the magic number is not used to encode endianness.
2530 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2531 * on ABI revisions, we need to try all combinations for all endianness to
2532 * detect the endianness.
2534 static int try_all_file_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2536 uint64_t ref_size
, attr_size
;
2539 for (i
= 0 ; attr_file_abi_sizes
[i
]; i
++) {
2540 ref_size
= attr_file_abi_sizes
[i
]
2541 + sizeof(struct perf_file_section
);
2542 if (hdr_sz
!= ref_size
) {
2543 attr_size
= bswap_64(hdr_sz
);
2544 if (attr_size
!= ref_size
)
2547 ph
->needs_swap
= true;
2549 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2554 /* could not determine endianness */
2558 #define PERF_PIPE_HDR_VER0 16
2560 static const size_t attr_pipe_abi_sizes
[] = {
2561 [0] = PERF_PIPE_HDR_VER0
,
2566 * In the legacy pipe format, there is an implicit assumption that endiannesss
2567 * between host recording the samples, and host parsing the samples is the
2568 * same. This is not always the case given that the pipe output may always be
2569 * redirected into a file and analyzed on a different machine with possibly a
2570 * different endianness and perf_event ABI revsions in the perf tool itself.
2572 static int try_all_pipe_abis(uint64_t hdr_sz
, struct perf_header
*ph
)
2577 for (i
= 0 ; attr_pipe_abi_sizes
[i
]; i
++) {
2578 if (hdr_sz
!= attr_pipe_abi_sizes
[i
]) {
2579 attr_size
= bswap_64(hdr_sz
);
2580 if (attr_size
!= hdr_sz
)
2583 ph
->needs_swap
= true;
2585 pr_debug("Pipe ABI%d perf.data file detected\n", i
);
2591 bool is_perf_magic(u64 magic
)
2593 if (!memcmp(&magic
, __perf_magic1
, sizeof(magic
))
2594 || magic
== __perf_magic2
2595 || magic
== __perf_magic2_sw
)
2601 static int check_magic_endian(u64 magic
, uint64_t hdr_sz
,
2602 bool is_pipe
, struct perf_header
*ph
)
2606 /* check for legacy format */
2607 ret
= memcmp(&magic
, __perf_magic1
, sizeof(magic
));
2609 ph
->version
= PERF_HEADER_VERSION_1
;
2610 pr_debug("legacy perf.data format\n");
2612 return try_all_pipe_abis(hdr_sz
, ph
);
2614 return try_all_file_abis(hdr_sz
, ph
);
2617 * the new magic number serves two purposes:
2618 * - unique number to identify actual perf.data files
2619 * - encode endianness of file
2621 ph
->version
= PERF_HEADER_VERSION_2
;
2623 /* check magic number with one endianness */
2624 if (magic
== __perf_magic2
)
2627 /* check magic number with opposite endianness */
2628 if (magic
!= __perf_magic2_sw
)
2631 ph
->needs_swap
= true;
2636 int perf_file_header__read(struct perf_file_header
*header
,
2637 struct perf_header
*ph
, int fd
)
2641 lseek(fd
, 0, SEEK_SET
);
2643 ret
= readn(fd
, header
, sizeof(*header
));
2647 if (check_magic_endian(header
->magic
,
2648 header
->attr_size
, false, ph
) < 0) {
2649 pr_debug("magic/endian check failed\n");
2653 if (ph
->needs_swap
) {
2654 mem_bswap_64(header
, offsetof(struct perf_file_header
,
2658 if (header
->size
!= sizeof(*header
)) {
2659 /* Support the previous format */
2660 if (header
->size
== offsetof(typeof(*header
), adds_features
))
2661 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2664 } else if (ph
->needs_swap
) {
2666 * feature bitmap is declared as an array of unsigned longs --
2667 * not good since its size can differ between the host that
2668 * generated the data file and the host analyzing the file.
2670 * We need to handle endianness, but we don't know the size of
2671 * the unsigned long where the file was generated. Take a best
2672 * guess at determining it: try 64-bit swap first (ie., file
2673 * created on a 64-bit host), and check if the hostname feature
2674 * bit is set (this feature bit is forced on as of fbe96f2).
2675 * If the bit is not, undo the 64-bit swap and try a 32-bit
2676 * swap. If the hostname bit is still not set (e.g., older data
2677 * file), punt and fallback to the original behavior --
2678 * clearing all feature bits and setting buildid.
2680 mem_bswap_64(&header
->adds_features
,
2681 BITS_TO_U64(HEADER_FEAT_BITS
));
2683 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2685 mem_bswap_64(&header
->adds_features
,
2686 BITS_TO_U64(HEADER_FEAT_BITS
));
2689 mem_bswap_32(&header
->adds_features
,
2690 BITS_TO_U32(HEADER_FEAT_BITS
));
2693 if (!test_bit(HEADER_HOSTNAME
, header
->adds_features
)) {
2694 bitmap_zero(header
->adds_features
, HEADER_FEAT_BITS
);
2695 set_bit(HEADER_BUILD_ID
, header
->adds_features
);
2699 memcpy(&ph
->adds_features
, &header
->adds_features
,
2700 sizeof(ph
->adds_features
));
2702 ph
->data_offset
= header
->data
.offset
;
2703 ph
->data_size
= header
->data
.size
;
2704 ph
->feat_offset
= header
->data
.offset
+ header
->data
.size
;
2708 static int perf_file_section__process(struct perf_file_section
*section
,
2709 struct perf_header
*ph
,
2710 int feat
, int fd
, void *data
)
2712 if (lseek(fd
, section
->offset
, SEEK_SET
) == (off_t
)-1) {
2713 pr_debug("Failed to lseek to %" PRIu64
" offset for feature "
2714 "%d, continuing...\n", section
->offset
, feat
);
2718 if (feat
>= HEADER_LAST_FEATURE
) {
2719 pr_debug("unknown feature %d, continuing...\n", feat
);
2723 if (!feat_ops
[feat
].process
)
2726 return feat_ops
[feat
].process(section
, ph
, fd
, data
);
2729 static int perf_file_header__read_pipe(struct perf_pipe_file_header
*header
,
2730 struct perf_header
*ph
, int fd
,
2735 ret
= readn(fd
, header
, sizeof(*header
));
2739 if (check_magic_endian(header
->magic
, header
->size
, true, ph
) < 0) {
2740 pr_debug("endian/magic failed\n");
2745 header
->size
= bswap_64(header
->size
);
2747 if (repipe
&& do_write(STDOUT_FILENO
, header
, sizeof(*header
)) < 0)
2753 static int perf_header__read_pipe(struct perf_session
*session
)
2755 struct perf_header
*header
= &session
->header
;
2756 struct perf_pipe_file_header f_header
;
2758 if (perf_file_header__read_pipe(&f_header
, header
,
2759 perf_data_file__fd(session
->file
),
2760 session
->repipe
) < 0) {
2761 pr_debug("incompatible file format\n");
2768 static int read_attr(int fd
, struct perf_header
*ph
,
2769 struct perf_file_attr
*f_attr
)
2771 struct perf_event_attr
*attr
= &f_attr
->attr
;
2773 size_t our_sz
= sizeof(f_attr
->attr
);
2776 memset(f_attr
, 0, sizeof(*f_attr
));
2778 /* read minimal guaranteed structure */
2779 ret
= readn(fd
, attr
, PERF_ATTR_SIZE_VER0
);
2781 pr_debug("cannot read %d bytes of header attr\n",
2782 PERF_ATTR_SIZE_VER0
);
2786 /* on file perf_event_attr size */
2794 sz
= PERF_ATTR_SIZE_VER0
;
2795 } else if (sz
> our_sz
) {
2796 pr_debug("file uses a more recent and unsupported ABI"
2797 " (%zu bytes extra)\n", sz
- our_sz
);
2800 /* what we have not yet read and that we know about */
2801 left
= sz
- PERF_ATTR_SIZE_VER0
;
2804 ptr
+= PERF_ATTR_SIZE_VER0
;
2806 ret
= readn(fd
, ptr
, left
);
2808 /* read perf_file_section, ids are read in caller */
2809 ret
= readn(fd
, &f_attr
->ids
, sizeof(f_attr
->ids
));
2811 return ret
<= 0 ? -1 : 0;
2814 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel
*evsel
,
2815 struct pevent
*pevent
)
2817 struct event_format
*event
;
2820 /* already prepared */
2821 if (evsel
->tp_format
)
2824 if (pevent
== NULL
) {
2825 pr_debug("broken or missing trace data\n");
2829 event
= pevent_find_event(pevent
, evsel
->attr
.config
);
2830 if (event
== NULL
) {
2831 pr_debug("cannot find event format for %d\n", (int)evsel
->attr
.config
);
2836 snprintf(bf
, sizeof(bf
), "%s:%s", event
->system
, event
->name
);
2837 evsel
->name
= strdup(bf
);
2838 if (evsel
->name
== NULL
)
2842 evsel
->tp_format
= event
;
2846 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist
*evlist
,
2847 struct pevent
*pevent
)
2849 struct perf_evsel
*pos
;
2851 evlist__for_each_entry(evlist
, pos
) {
2852 if (pos
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2853 perf_evsel__prepare_tracepoint_event(pos
, pevent
))
2860 int perf_session__read_header(struct perf_session
*session
)
2862 struct perf_data_file
*file
= session
->file
;
2863 struct perf_header
*header
= &session
->header
;
2864 struct perf_file_header f_header
;
2865 struct perf_file_attr f_attr
;
2867 int nr_attrs
, nr_ids
, i
, j
;
2868 int fd
= perf_data_file__fd(file
);
2870 session
->evlist
= perf_evlist__new();
2871 if (session
->evlist
== NULL
)
2874 session
->evlist
->env
= &header
->env
;
2875 session
->machines
.host
.env
= &header
->env
;
2876 if (perf_data_file__is_pipe(file
))
2877 return perf_header__read_pipe(session
);
2879 if (perf_file_header__read(&f_header
, header
, fd
) < 0)
2883 * Sanity check that perf.data was written cleanly; data size is
2884 * initialized to 0 and updated only if the on_exit function is run.
2885 * If data size is still 0 then the file contains only partial
2886 * information. Just warn user and process it as much as it can.
2888 if (f_header
.data
.size
== 0) {
2889 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2890 "Was the 'perf record' command properly terminated?\n",
2894 nr_attrs
= f_header
.attrs
.size
/ f_header
.attr_size
;
2895 lseek(fd
, f_header
.attrs
.offset
, SEEK_SET
);
2897 for (i
= 0; i
< nr_attrs
; i
++) {
2898 struct perf_evsel
*evsel
;
2901 if (read_attr(fd
, header
, &f_attr
) < 0)
2904 if (header
->needs_swap
) {
2905 f_attr
.ids
.size
= bswap_64(f_attr
.ids
.size
);
2906 f_attr
.ids
.offset
= bswap_64(f_attr
.ids
.offset
);
2907 perf_event__attr_swap(&f_attr
.attr
);
2910 tmp
= lseek(fd
, 0, SEEK_CUR
);
2911 evsel
= perf_evsel__new(&f_attr
.attr
);
2914 goto out_delete_evlist
;
2916 evsel
->needs_swap
= header
->needs_swap
;
2918 * Do it before so that if perf_evsel__alloc_id fails, this
2919 * entry gets purged too at perf_evlist__delete().
2921 perf_evlist__add(session
->evlist
, evsel
);
2923 nr_ids
= f_attr
.ids
.size
/ sizeof(u64
);
2925 * We don't have the cpu and thread maps on the header, so
2926 * for allocating the perf_sample_id table we fake 1 cpu and
2927 * hattr->ids threads.
2929 if (perf_evsel__alloc_id(evsel
, 1, nr_ids
))
2930 goto out_delete_evlist
;
2932 lseek(fd
, f_attr
.ids
.offset
, SEEK_SET
);
2934 for (j
= 0; j
< nr_ids
; j
++) {
2935 if (perf_header__getbuffer64(header
, fd
, &f_id
, sizeof(f_id
)))
2938 perf_evlist__id_add(session
->evlist
, evsel
, 0, j
, f_id
);
2941 lseek(fd
, tmp
, SEEK_SET
);
2944 symbol_conf
.nr_events
= nr_attrs
;
2946 perf_header__process_sections(header
, fd
, &session
->tevent
,
2947 perf_file_section__process
);
2949 if (perf_evlist__prepare_tracepoint_events(session
->evlist
,
2950 session
->tevent
.pevent
))
2951 goto out_delete_evlist
;
2958 perf_evlist__delete(session
->evlist
);
2959 session
->evlist
= NULL
;
2963 int perf_event__synthesize_attr(struct perf_tool
*tool
,
2964 struct perf_event_attr
*attr
, u32 ids
, u64
*id
,
2965 perf_event__handler_t process
)
2967 union perf_event
*ev
;
2971 size
= sizeof(struct perf_event_attr
);
2972 size
= PERF_ALIGN(size
, sizeof(u64
));
2973 size
+= sizeof(struct perf_event_header
);
2974 size
+= ids
* sizeof(u64
);
2981 ev
->attr
.attr
= *attr
;
2982 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
2984 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
2985 ev
->attr
.header
.size
= (u16
)size
;
2987 if (ev
->attr
.header
.size
== size
)
2988 err
= process(tool
, ev
, NULL
, NULL
);
2997 static struct event_update_event
*
2998 event_update_event__new(size_t size
, u64 type
, u64 id
)
3000 struct event_update_event
*ev
;
3002 size
+= sizeof(*ev
);
3003 size
= PERF_ALIGN(size
, sizeof(u64
));
3007 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3008 ev
->header
.size
= (u16
)size
;
3016 perf_event__synthesize_event_update_unit(struct perf_tool
*tool
,
3017 struct perf_evsel
*evsel
,
3018 perf_event__handler_t process
)
3020 struct event_update_event
*ev
;
3021 size_t size
= strlen(evsel
->unit
);
3024 ev
= event_update_event__new(size
+ 1, PERF_EVENT_UPDATE__UNIT
, evsel
->id
[0]);
3028 strncpy(ev
->data
, evsel
->unit
, size
);
3029 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
3035 perf_event__synthesize_event_update_scale(struct perf_tool
*tool
,
3036 struct perf_evsel
*evsel
,
3037 perf_event__handler_t process
)
3039 struct event_update_event
*ev
;
3040 struct event_update_event_scale
*ev_data
;
3043 ev
= event_update_event__new(sizeof(*ev_data
), PERF_EVENT_UPDATE__SCALE
, evsel
->id
[0]);
3047 ev_data
= (struct event_update_event_scale
*) ev
->data
;
3048 ev_data
->scale
= evsel
->scale
;
3049 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3055 perf_event__synthesize_event_update_name(struct perf_tool
*tool
,
3056 struct perf_evsel
*evsel
,
3057 perf_event__handler_t process
)
3059 struct event_update_event
*ev
;
3060 size_t len
= strlen(evsel
->name
);
3063 ev
= event_update_event__new(len
+ 1, PERF_EVENT_UPDATE__NAME
, evsel
->id
[0]);
3067 strncpy(ev
->data
, evsel
->name
, len
);
3068 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3074 perf_event__synthesize_event_update_cpus(struct perf_tool
*tool
,
3075 struct perf_evsel
*evsel
,
3076 perf_event__handler_t process
)
3078 size_t size
= sizeof(struct event_update_event
);
3079 struct event_update_event
*ev
;
3083 if (!evsel
->own_cpus
)
3086 ev
= cpu_map_data__alloc(evsel
->own_cpus
, &size
, &type
, &max
);
3090 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
3091 ev
->header
.size
= (u16
)size
;
3092 ev
->type
= PERF_EVENT_UPDATE__CPUS
;
3093 ev
->id
= evsel
->id
[0];
3095 cpu_map_data__synthesize((struct cpu_map_data
*) ev
->data
,
3099 err
= process(tool
, (union perf_event
*) ev
, NULL
, NULL
);
3104 size_t perf_event__fprintf_event_update(union perf_event
*event
, FILE *fp
)
3106 struct event_update_event
*ev
= &event
->event_update
;
3107 struct event_update_event_scale
*ev_scale
;
3108 struct event_update_event_cpus
*ev_cpus
;
3109 struct cpu_map
*map
;
3112 ret
= fprintf(fp
, "\n... id: %" PRIu64
"\n", ev
->id
);
3115 case PERF_EVENT_UPDATE__SCALE
:
3116 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3117 ret
+= fprintf(fp
, "... scale: %f\n", ev_scale
->scale
);
3119 case PERF_EVENT_UPDATE__UNIT
:
3120 ret
+= fprintf(fp
, "... unit: %s\n", ev
->data
);
3122 case PERF_EVENT_UPDATE__NAME
:
3123 ret
+= fprintf(fp
, "... name: %s\n", ev
->data
);
3125 case PERF_EVENT_UPDATE__CPUS
:
3126 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3127 ret
+= fprintf(fp
, "... ");
3129 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3131 ret
+= cpu_map__fprintf(map
, fp
);
3133 ret
+= fprintf(fp
, "failed to get cpus\n");
3136 ret
+= fprintf(fp
, "... unknown type\n");
3143 int perf_event__synthesize_attrs(struct perf_tool
*tool
,
3144 struct perf_session
*session
,
3145 perf_event__handler_t process
)
3147 struct perf_evsel
*evsel
;
3150 evlist__for_each_entry(session
->evlist
, evsel
) {
3151 err
= perf_event__synthesize_attr(tool
, &evsel
->attr
, evsel
->ids
,
3152 evsel
->id
, process
);
3154 pr_debug("failed to create perf header attribute\n");
3162 int perf_event__process_attr(struct perf_tool
*tool __maybe_unused
,
3163 union perf_event
*event
,
3164 struct perf_evlist
**pevlist
)
3167 struct perf_evsel
*evsel
;
3168 struct perf_evlist
*evlist
= *pevlist
;
3170 if (evlist
== NULL
) {
3171 *pevlist
= evlist
= perf_evlist__new();
3176 evsel
= perf_evsel__new(&event
->attr
.attr
);
3180 perf_evlist__add(evlist
, evsel
);
3182 ids
= event
->header
.size
;
3183 ids
-= (void *)&event
->attr
.id
- (void *)event
;
3184 n_ids
= ids
/ sizeof(u64
);
3186 * We don't have the cpu and thread maps on the header, so
3187 * for allocating the perf_sample_id table we fake 1 cpu and
3188 * hattr->ids threads.
3190 if (perf_evsel__alloc_id(evsel
, 1, n_ids
))
3193 for (i
= 0; i
< n_ids
; i
++) {
3194 perf_evlist__id_add(evlist
, evsel
, 0, i
, event
->attr
.id
[i
]);
3197 symbol_conf
.nr_events
= evlist
->nr_entries
;
3202 int perf_event__process_event_update(struct perf_tool
*tool __maybe_unused
,
3203 union perf_event
*event
,
3204 struct perf_evlist
**pevlist
)
3206 struct event_update_event
*ev
= &event
->event_update
;
3207 struct event_update_event_scale
*ev_scale
;
3208 struct event_update_event_cpus
*ev_cpus
;
3209 struct perf_evlist
*evlist
;
3210 struct perf_evsel
*evsel
;
3211 struct cpu_map
*map
;
3213 if (!pevlist
|| *pevlist
== NULL
)
3218 evsel
= perf_evlist__id2evsel(evlist
, ev
->id
);
3223 case PERF_EVENT_UPDATE__UNIT
:
3224 evsel
->unit
= strdup(ev
->data
);
3226 case PERF_EVENT_UPDATE__NAME
:
3227 evsel
->name
= strdup(ev
->data
);
3229 case PERF_EVENT_UPDATE__SCALE
:
3230 ev_scale
= (struct event_update_event_scale
*) ev
->data
;
3231 evsel
->scale
= ev_scale
->scale
;
3233 case PERF_EVENT_UPDATE__CPUS
:
3234 ev_cpus
= (struct event_update_event_cpus
*) ev
->data
;
3236 map
= cpu_map__new_data(&ev_cpus
->cpus
);
3238 evsel
->own_cpus
= map
;
3240 pr_err("failed to get event_update cpus\n");
3248 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
,
3249 struct perf_evlist
*evlist
,
3250 perf_event__handler_t process
)
3252 union perf_event ev
;
3253 struct tracing_data
*tdata
;
3254 ssize_t size
= 0, aligned_size
= 0, padding
;
3255 int err __maybe_unused
= 0;
3258 * We are going to store the size of the data followed
3259 * by the data contents. Since the fd descriptor is a pipe,
3260 * we cannot seek back to store the size of the data once
3261 * we know it. Instead we:
3263 * - write the tracing data to the temp file
3264 * - get/write the data size to pipe
3265 * - write the tracing data from the temp file
3268 tdata
= tracing_data_get(&evlist
->entries
, fd
, true);
3272 memset(&ev
, 0, sizeof(ev
));
3274 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
3276 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
3277 padding
= aligned_size
- size
;
3278 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
3279 ev
.tracing_data
.size
= aligned_size
;
3281 process(tool
, &ev
, NULL
, NULL
);
3284 * The put function will copy all the tracing data
3285 * stored in temp file to the pipe.
3287 tracing_data_put(tdata
);
3289 write_padded(fd
, NULL
, 0, padding
);
3291 return aligned_size
;
3294 int perf_event__process_tracing_data(struct perf_tool
*tool __maybe_unused
,
3295 union perf_event
*event
,
3296 struct perf_session
*session
)
3298 ssize_t size_read
, padding
, size
= event
->tracing_data
.size
;
3299 int fd
= perf_data_file__fd(session
->file
);
3300 off_t offset
= lseek(fd
, 0, SEEK_CUR
);
3303 /* setup for reading amidst mmap */
3304 lseek(fd
, offset
+ sizeof(struct tracing_data_event
),
3307 size_read
= trace_report(fd
, &session
->tevent
,
3309 padding
= PERF_ALIGN(size_read
, sizeof(u64
)) - size_read
;
3311 if (readn(fd
, buf
, padding
) < 0) {
3312 pr_err("%s: reading input file", __func__
);
3315 if (session
->repipe
) {
3316 int retw
= write(STDOUT_FILENO
, buf
, padding
);
3317 if (retw
<= 0 || retw
!= padding
) {
3318 pr_err("%s: repiping tracing data padding", __func__
);
3323 if (size_read
+ padding
!= size
) {
3324 pr_err("%s: tracing data size mismatch", __func__
);
3328 perf_evlist__prepare_tracepoint_events(session
->evlist
,
3329 session
->tevent
.pevent
);
3331 return size_read
+ padding
;
3334 int perf_event__synthesize_build_id(struct perf_tool
*tool
,
3335 struct dso
*pos
, u16 misc
,
3336 perf_event__handler_t process
,
3337 struct machine
*machine
)
3339 union perf_event ev
;
3346 memset(&ev
, 0, sizeof(ev
));
3348 len
= pos
->long_name_len
+ 1;
3349 len
= PERF_ALIGN(len
, NAME_ALIGN
);
3350 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
3351 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
3352 ev
.build_id
.header
.misc
= misc
;
3353 ev
.build_id
.pid
= machine
->pid
;
3354 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
3355 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
3357 err
= process(tool
, &ev
, NULL
, machine
);
3362 int perf_event__process_build_id(struct perf_tool
*tool __maybe_unused
,
3363 union perf_event
*event
,
3364 struct perf_session
*session
)
3366 __event_process_build_id(&event
->build_id
,
3367 event
->build_id
.filename
,