1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/compiler.h>
13 #include <linux/list.h>
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/string.h>
17 #include <linux/stringify.h>
18 #include <linux/zalloc.h>
20 #include <sys/utsname.h>
21 #include <linux/time64.h>
23 #ifdef HAVE_LIBBPF_SUPPORT
24 #include <bpf/libbpf.h>
26 #include <perf/cpumap.h>
27 #include <tools/libc_compat.h> // reallocarray
32 #include "util/evsel_fprintf.h"
35 #include "trace-event.h"
46 #include <api/fs/fs.h>
49 #include "time-utils.h"
51 #include "util/util.h" // perf_exe()
53 #include "bpf-event.h"
54 #include "bpf-utils.h"
57 #include <linux/ctype.h>
58 #include <internal/lib.h>
60 #ifdef HAVE_LIBTRACEEVENT
61 #include <traceevent/event-parse.h>
66 * must be a numerical value to let the endianness
67 * determine the memory layout. That way we are able
68 * to detect endianness when reading the perf.data file
71 * we check for legacy (PERFFILE) format.
73 static const char *__perf_magic1 = "PERFFILE";
74 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
75 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
77 #define PERF_MAGIC __perf_magic2
79 const char perf_version_string[] = PERF_VERSION;
81 struct perf_file_attr {
82 struct perf_event_attr attr;
83 struct perf_file_section ids;
86 void perf_header__set_feat(struct perf_header *header, int feat)
88 __set_bit(feat, header->adds_features);
91 void perf_header__clear_feat(struct perf_header *header, int feat)
93 __clear_bit(feat, header->adds_features);
96 bool perf_header__has_feat(const struct perf_header *header, int feat)
98 return test_bit(feat, header->adds_features);
101 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
103 ssize_t ret = writen(ff->fd, buf, size);
105 if (ret != (ssize_t)size)
106 return ret < 0 ? (int)ret : -1;
110 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
112 /* struct perf_event_header::size is u16 */
113 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
114 size_t new_size = ff->size;
117 if (size + ff->offset > max_size)
120 while (size > (new_size - ff->offset))
122 new_size = min(max_size, new_size);
124 if (ff->size < new_size) {
125 addr = realloc(ff->buf, new_size);
132 memcpy(ff->buf + ff->offset, buf, size);
138 /* Return: 0 if succeeded, -ERR if failed. */
139 int do_write(struct feat_fd *ff, const void *buf, size_t size)
142 return __do_write_fd(ff, buf, size);
143 return __do_write_buf(ff, buf, size);
146 /* Return: 0 if succeeded, -ERR if failed. */
147 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
149 u64 *p = (u64 *) set;
152 ret = do_write(ff, &size, sizeof(size));
156 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
157 ret = do_write(ff, p + i, sizeof(*p));
165 /* Return: 0 if succeeded, -ERR if failed. */
166 int write_padded(struct feat_fd *ff, const void *bf,
167 size_t count, size_t count_aligned)
169 static const char zero_buf[NAME_ALIGN];
170 int err = do_write(ff, bf, count);
173 err = do_write(ff, zero_buf, count_aligned - count);
178 #define string_size(str) \
179 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
181 /* Return: 0 if succeeded, -ERR if failed. */
182 static int do_write_string(struct feat_fd *ff, const char *str)
187 olen = strlen(str) + 1;
188 len = PERF_ALIGN(olen, NAME_ALIGN);
190 /* write len, incl. \0 */
191 ret = do_write(ff, &len, sizeof(len));
195 return write_padded(ff, str, olen, len);
198 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
200 ssize_t ret = readn(ff->fd, addr, size);
203 return ret < 0 ? (int)ret : -1;
207 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
209 if (size > (ssize_t)ff->size - ff->offset)
212 memcpy(addr, ff->buf + ff->offset, size);
219 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
222 return __do_read_fd(ff, addr, size);
223 return __do_read_buf(ff, addr, size);
226 static int do_read_u32(struct feat_fd *ff, u32 *addr)
230 ret = __do_read(ff, addr, sizeof(*addr));
234 if (ff->ph->needs_swap)
235 *addr = bswap_32(*addr);
239 static int do_read_u64(struct feat_fd *ff, u64 *addr)
243 ret = __do_read(ff, addr, sizeof(*addr));
247 if (ff->ph->needs_swap)
248 *addr = bswap_64(*addr);
252 static char *do_read_string(struct feat_fd *ff)
257 if (do_read_u32(ff, &len))
264 if (!__do_read(ff, buf, len)) {
266 * strings are padded by zeroes
267 * thus the actual strlen of buf
268 * may be less than len
277 /* Return: 0 if succeeded, -ERR if failed. */
278 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
284 ret = do_read_u64(ff, &size);
288 set = bitmap_zalloc(size);
294 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
295 ret = do_read_u64(ff, p + i);
307 #ifdef HAVE_LIBTRACEEVENT
308 static int write_tracing_data(struct feat_fd *ff,
309 struct evlist *evlist)
311 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
314 return read_tracing_data(ff->fd, &evlist->core.entries);
318 static int write_build_id(struct feat_fd *ff,
319 struct evlist *evlist __maybe_unused)
321 struct perf_session *session;
324 session = container_of(ff->ph, struct perf_session, header);
326 if (!perf_session__read_build_ids(session, true))
329 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
332 err = perf_session__write_buildid_table(session, ff);
334 pr_debug("failed to write buildid table\n");
337 perf_session__cache_build_ids(session);
342 static int write_hostname(struct feat_fd *ff,
343 struct evlist *evlist __maybe_unused)
352 return do_write_string(ff, uts.nodename);
355 static int write_osrelease(struct feat_fd *ff,
356 struct evlist *evlist __maybe_unused)
365 return do_write_string(ff, uts.release);
368 static int write_arch(struct feat_fd *ff,
369 struct evlist *evlist __maybe_unused)
378 return do_write_string(ff, uts.machine);
381 static int write_version(struct feat_fd *ff,
382 struct evlist *evlist __maybe_unused)
384 return do_write_string(ff, perf_version_string);
387 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
392 const char *search = cpuinfo_proc;
399 file = fopen("/proc/cpuinfo", "r");
403 while (getline(&buf, &len, file) > 0) {
404 ret = strncmp(buf, search, strlen(search));
416 p = strchr(buf, ':');
417 if (p && *(p+1) == ' ' && *(p+2))
423 /* squash extra space characters (branding string) */
428 char *q = skip_spaces(r);
431 while ((*r++ = *q++));
435 ret = do_write_string(ff, s);
442 static int write_cpudesc(struct feat_fd *ff,
443 struct evlist *evlist __maybe_unused)
445 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
446 #define CPUINFO_PROC { "cpu", }
447 #elif defined(__s390__)
448 #define CPUINFO_PROC { "vendor_id", }
449 #elif defined(__sh__)
450 #define CPUINFO_PROC { "cpu type", }
451 #elif defined(__alpha__) || defined(__mips__)
452 #define CPUINFO_PROC { "cpu model", }
453 #elif defined(__arm__)
454 #define CPUINFO_PROC { "model name", "Processor", }
455 #elif defined(__arc__)
456 #define CPUINFO_PROC { "Processor", }
457 #elif defined(__xtensa__)
458 #define CPUINFO_PROC { "core ID", }
460 #define CPUINFO_PROC { "model name", }
462 const char *cpuinfo_procs[] = CPUINFO_PROC;
466 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
468 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
476 static int write_nrcpus(struct feat_fd *ff,
477 struct evlist *evlist __maybe_unused)
483 nrc = cpu__max_present_cpu().cpu;
485 nr = sysconf(_SC_NPROCESSORS_ONLN);
489 nra = (u32)(nr & UINT_MAX);
491 ret = do_write(ff, &nrc, sizeof(nrc));
495 return do_write(ff, &nra, sizeof(nra));
498 static int write_event_desc(struct feat_fd *ff,
499 struct evlist *evlist)
505 nre = evlist->core.nr_entries;
508 * write number of events
510 ret = do_write(ff, &nre, sizeof(nre));
515 * size of perf_event_attr struct
517 sz = (u32)sizeof(evsel->core.attr);
518 ret = do_write(ff, &sz, sizeof(sz));
522 evlist__for_each_entry(evlist, evsel) {
523 ret = do_write(ff, &evsel->core.attr, sz);
527 * write number of unique id per event
528 * there is one id per instance of an event
530 * copy into an nri to be independent of the
533 nri = evsel->core.ids;
534 ret = do_write(ff, &nri, sizeof(nri));
539 * write event string as passed on cmdline
541 ret = do_write_string(ff, evsel__name(evsel));
545 * write unique ids for this event
547 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
554 static int write_cmdline(struct feat_fd *ff,
555 struct evlist *evlist __maybe_unused)
557 char pbuf[MAXPATHLEN], *buf;
560 /* actual path to perf binary */
561 buf = perf_exe(pbuf, MAXPATHLEN);
563 /* account for binary path */
564 n = perf_env.nr_cmdline + 1;
566 ret = do_write(ff, &n, sizeof(n));
570 ret = do_write_string(ff, buf);
574 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
575 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
583 static int write_cpu_topology(struct feat_fd *ff,
584 struct evlist *evlist __maybe_unused)
586 struct cpu_topology *tp;
590 tp = cpu_topology__new();
594 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists));
598 for (i = 0; i < tp->package_cpus_lists; i++) {
599 ret = do_write_string(ff, tp->package_cpus_list[i]);
603 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists));
607 for (i = 0; i < tp->core_cpus_lists; i++) {
608 ret = do_write_string(ff, tp->core_cpus_list[i]);
613 ret = perf_env__read_cpu_topology_map(&perf_env);
617 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
618 ret = do_write(ff, &perf_env.cpu[j].core_id,
619 sizeof(perf_env.cpu[j].core_id));
622 ret = do_write(ff, &perf_env.cpu[j].socket_id,
623 sizeof(perf_env.cpu[j].socket_id));
628 if (!tp->die_cpus_lists)
631 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists));
635 for (i = 0; i < tp->die_cpus_lists; i++) {
636 ret = do_write_string(ff, tp->die_cpus_list[i]);
641 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
642 ret = do_write(ff, &perf_env.cpu[j].die_id,
643 sizeof(perf_env.cpu[j].die_id));
649 cpu_topology__delete(tp);
655 static int write_total_mem(struct feat_fd *ff,
656 struct evlist *evlist __maybe_unused)
664 fp = fopen("/proc/meminfo", "r");
668 while (getline(&buf, &len, fp) > 0) {
669 ret = strncmp(buf, "MemTotal:", 9);
674 n = sscanf(buf, "%*s %"PRIu64, &mem);
676 ret = do_write(ff, &mem, sizeof(mem));
684 static int write_numa_topology(struct feat_fd *ff,
685 struct evlist *evlist __maybe_unused)
687 struct numa_topology *tp;
691 tp = numa_topology__new();
695 ret = do_write(ff, &tp->nr, sizeof(u32));
699 for (i = 0; i < tp->nr; i++) {
700 struct numa_topology_node *n = &tp->nodes[i];
702 ret = do_write(ff, &n->node, sizeof(u32));
706 ret = do_write(ff, &n->mem_total, sizeof(u64));
710 ret = do_write(ff, &n->mem_free, sizeof(u64));
714 ret = do_write_string(ff, n->cpus);
722 numa_topology__delete(tp);
729 * struct pmu_mappings {
738 static int write_pmu_mappings(struct feat_fd *ff,
739 struct evlist *evlist __maybe_unused)
741 struct perf_pmu *pmu = NULL;
746 * Do a first pass to count number of pmu to avoid lseek so this
747 * works in pipe mode as well.
749 while ((pmu = perf_pmus__scan(pmu))) {
755 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
759 while ((pmu = perf_pmus__scan(pmu))) {
763 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
767 ret = do_write_string(ff, pmu->name);
778 * struct group_descs {
780 * struct group_desc {
787 static int write_group_desc(struct feat_fd *ff,
788 struct evlist *evlist)
790 u32 nr_groups = evlist__nr_groups(evlist);
794 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
798 evlist__for_each_entry(evlist, evsel) {
799 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
800 const char *name = evsel->group_name ?: "{anon_group}";
801 u32 leader_idx = evsel->core.idx;
802 u32 nr_members = evsel->core.nr_members;
804 ret = do_write_string(ff, name);
808 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
812 ret = do_write(ff, &nr_members, sizeof(nr_members));
821 * Return the CPU id as a raw string.
823 * Each architecture should provide a more precise id string that
824 * can be use to match the architecture's "mapfile".
826 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
831 /* Return zero when the cpuid from the mapfile.csv matches the
832 * cpuid string generated on this platform.
833 * Otherwise return non-zero.
835 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
838 regmatch_t pmatch[1];
841 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
842 /* Warn unable to generate match particular string. */
843 pr_info("Invalid regular expression %s\n", mapcpuid);
847 match = !regexec(&re, cpuid, 1, pmatch, 0);
850 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
852 /* Verify the entire string matched. */
853 if (match_len == strlen(cpuid))
860 * default get_cpuid(): nothing gets recorded
861 * actual implementation must be in arch/$(SRCARCH)/util/header.c
863 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
865 return ENOSYS; /* Not implemented */
868 static int write_cpuid(struct feat_fd *ff,
869 struct evlist *evlist __maybe_unused)
874 ret = get_cpuid(buffer, sizeof(buffer));
878 return do_write_string(ff, buffer);
881 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
882 struct evlist *evlist __maybe_unused)
887 static int write_auxtrace(struct feat_fd *ff,
888 struct evlist *evlist __maybe_unused)
890 struct perf_session *session;
893 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
896 session = container_of(ff->ph, struct perf_session, header);
898 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
900 pr_err("Failed to write auxtrace index\n");
904 static int write_clockid(struct feat_fd *ff,
905 struct evlist *evlist __maybe_unused)
907 return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
908 sizeof(ff->ph->env.clock.clockid_res_ns));
911 static int write_clock_data(struct feat_fd *ff,
912 struct evlist *evlist __maybe_unused)
921 ret = do_write(ff, &data32, sizeof(data32));
926 data32 = ff->ph->env.clock.clockid;
928 ret = do_write(ff, &data32, sizeof(data32));
933 data64 = &ff->ph->env.clock.tod_ns;
935 ret = do_write(ff, data64, sizeof(*data64));
939 /* clockid ref time */
940 data64 = &ff->ph->env.clock.clockid_ns;
942 return do_write(ff, data64, sizeof(*data64));
945 static int write_hybrid_topology(struct feat_fd *ff,
946 struct evlist *evlist __maybe_unused)
948 struct hybrid_topology *tp;
952 tp = hybrid_topology__new();
956 ret = do_write(ff, &tp->nr, sizeof(u32));
960 for (i = 0; i < tp->nr; i++) {
961 struct hybrid_topology_node *n = &tp->nodes[i];
963 ret = do_write_string(ff, n->pmu_name);
967 ret = do_write_string(ff, n->cpus);
975 hybrid_topology__delete(tp);
979 static int write_dir_format(struct feat_fd *ff,
980 struct evlist *evlist __maybe_unused)
982 struct perf_session *session;
983 struct perf_data *data;
985 session = container_of(ff->ph, struct perf_session, header);
986 data = session->data;
988 if (WARN_ON(!perf_data__is_dir(data)))
991 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
995 * Check whether a CPU is online
998 * 1 -> if CPU is online
999 * 0 -> if CPU is offline
1002 int is_cpu_online(unsigned int cpu)
1008 struct stat statbuf;
1010 snprintf(buf, sizeof(buf),
1011 "/sys/devices/system/cpu/cpu%d", cpu);
1012 if (stat(buf, &statbuf) != 0)
1016 * Check if /sys/devices/system/cpu/cpux/online file
1017 * exists. Some cases cpu0 won't have online file since
1018 * it is not expected to be turned off generally.
1019 * In kernels without CONFIG_HOTPLUG_CPU, this
1022 snprintf(buf, sizeof(buf),
1023 "/sys/devices/system/cpu/cpu%d/online", cpu);
1024 if (stat(buf, &statbuf) != 0)
1028 * Read online file using sysfs__read_str.
1029 * If read or open fails, return -1.
1030 * If read succeeds, return value from file
1031 * which gets stored in "str"
1033 snprintf(buf, sizeof(buf),
1034 "devices/system/cpu/cpu%d/online", cpu);
1036 if (sysfs__read_str(buf, &str, &strlen) < 0)
1045 #ifdef HAVE_LIBBPF_SUPPORT
1046 static int write_bpf_prog_info(struct feat_fd *ff,
1047 struct evlist *evlist __maybe_unused)
1049 struct perf_env *env = &ff->ph->env;
1050 struct rb_root *root;
1051 struct rb_node *next;
1054 down_read(&env->bpf_progs.lock);
1056 ret = do_write(ff, &env->bpf_progs.infos_cnt,
1057 sizeof(env->bpf_progs.infos_cnt));
1061 root = &env->bpf_progs.infos;
1062 next = rb_first(root);
1064 struct bpf_prog_info_node *node;
1067 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1068 next = rb_next(&node->rb_node);
1069 len = sizeof(struct perf_bpil) +
1070 node->info_linear->data_len;
1072 /* before writing to file, translate address to offset */
1073 bpil_addr_to_offs(node->info_linear);
1074 ret = do_write(ff, node->info_linear, len);
1076 * translate back to address even when do_write() fails,
1077 * so that this function never changes the data.
1079 bpil_offs_to_addr(node->info_linear);
1084 up_read(&env->bpf_progs.lock);
1088 static int write_bpf_btf(struct feat_fd *ff,
1089 struct evlist *evlist __maybe_unused)
1091 struct perf_env *env = &ff->ph->env;
1092 struct rb_root *root;
1093 struct rb_node *next;
1096 down_read(&env->bpf_progs.lock);
1098 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1099 sizeof(env->bpf_progs.btfs_cnt));
1104 root = &env->bpf_progs.btfs;
1105 next = rb_first(root);
1107 struct btf_node *node;
1109 node = rb_entry(next, struct btf_node, rb_node);
1110 next = rb_next(&node->rb_node);
1111 ret = do_write(ff, &node->id,
1112 sizeof(u32) * 2 + node->data_size);
1117 up_read(&env->bpf_progs.lock);
1120 #endif // HAVE_LIBBPF_SUPPORT
1122 static int cpu_cache_level__sort(const void *a, const void *b)
1124 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1125 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1127 return cache_a->level - cache_b->level;
1130 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1132 if (a->level != b->level)
1135 if (a->line_size != b->line_size)
1138 if (a->sets != b->sets)
1141 if (a->ways != b->ways)
1144 if (strcmp(a->type, b->type))
1147 if (strcmp(a->size, b->size))
1150 if (strcmp(a->map, b->map))
1156 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1158 char path[PATH_MAX], file[PATH_MAX];
1162 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1163 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1165 if (stat(file, &st))
1168 scnprintf(file, PATH_MAX, "%s/level", path);
1169 if (sysfs__read_int(file, (int *) &cache->level))
1172 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1173 if (sysfs__read_int(file, (int *) &cache->line_size))
1176 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1177 if (sysfs__read_int(file, (int *) &cache->sets))
1180 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1181 if (sysfs__read_int(file, (int *) &cache->ways))
1184 scnprintf(file, PATH_MAX, "%s/type", path);
1185 if (sysfs__read_str(file, &cache->type, &len))
1188 cache->type[len] = 0;
1189 cache->type = strim(cache->type);
1191 scnprintf(file, PATH_MAX, "%s/size", path);
1192 if (sysfs__read_str(file, &cache->size, &len)) {
1193 zfree(&cache->type);
1197 cache->size[len] = 0;
1198 cache->size = strim(cache->size);
1200 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1201 if (sysfs__read_str(file, &cache->map, &len)) {
1202 zfree(&cache->size);
1203 zfree(&cache->type);
1207 cache->map[len] = 0;
1208 cache->map = strim(cache->map);
1212 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1214 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1218 * Build caches levels for a particular CPU from the data in
1219 * /sys/devices/system/cpu/cpu<cpu>/cache/
1220 * The cache level data is stored in caches[] from index at
1223 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp)
1227 for (level = 0; level < MAX_CACHE_LVL; level++) {
1228 struct cpu_cache_level c;
1232 err = cpu_cache_level__read(&c, cpu, level);
1239 for (i = 0; i < *cntp; i++) {
1240 if (cpu_cache_level__cmp(&c, &caches[i]))
1248 cpu_cache_level__free(&c);
1254 static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
1256 u32 nr, cpu, cnt = 0;
1258 nr = cpu__max_cpu().cpu;
1260 for (cpu = 0; cpu < nr; cpu++) {
1261 int ret = build_caches_for_cpu(cpu, caches, &cnt);
1270 static int write_cache(struct feat_fd *ff,
1271 struct evlist *evlist __maybe_unused)
1273 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL;
1274 struct cpu_cache_level caches[max_caches];
1275 u32 cnt = 0, i, version = 1;
1278 ret = build_caches(caches, &cnt);
1282 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1284 ret = do_write(ff, &version, sizeof(u32));
1288 ret = do_write(ff, &cnt, sizeof(u32));
1292 for (i = 0; i < cnt; i++) {
1293 struct cpu_cache_level *c = &caches[i];
1296 ret = do_write(ff, &c->v, sizeof(u32)); \
1307 ret = do_write_string(ff, (const char *) c->v); \
1318 for (i = 0; i < cnt; i++)
1319 cpu_cache_level__free(&caches[i]);
1323 static int write_stat(struct feat_fd *ff __maybe_unused,
1324 struct evlist *evlist __maybe_unused)
1329 static int write_sample_time(struct feat_fd *ff,
1330 struct evlist *evlist)
1334 ret = do_write(ff, &evlist->first_sample_time,
1335 sizeof(evlist->first_sample_time));
1339 return do_write(ff, &evlist->last_sample_time,
1340 sizeof(evlist->last_sample_time));
1344 static int memory_node__read(struct memory_node *n, unsigned long idx)
1346 unsigned int phys, size = 0;
1347 char path[PATH_MAX];
1351 #define for_each_memory(mem, dir) \
1352 while ((ent = readdir(dir))) \
1353 if (strcmp(ent->d_name, ".") && \
1354 strcmp(ent->d_name, "..") && \
1355 sscanf(ent->d_name, "memory%u", &mem) == 1)
1357 scnprintf(path, PATH_MAX,
1358 "%s/devices/system/node/node%lu",
1359 sysfs__mountpoint(), idx);
1361 dir = opendir(path);
1363 pr_warning("failed: can't open memory sysfs data\n");
1367 for_each_memory(phys, dir) {
1368 size = max(phys, size);
1373 n->set = bitmap_zalloc(size);
1384 for_each_memory(phys, dir) {
1385 __set_bit(phys, n->set);
1392 static void memory_node__delete_nodes(struct memory_node *nodesp, u64 cnt)
1394 for (u64 i = 0; i < cnt; i++)
1395 bitmap_free(nodesp[i].set);
1400 static int memory_node__sort(const void *a, const void *b)
1402 const struct memory_node *na = a;
1403 const struct memory_node *nb = b;
1405 return na->node - nb->node;
1408 static int build_mem_topology(struct memory_node **nodesp, u64 *cntp)
1410 char path[PATH_MAX];
1414 size_t cnt = 0, size = 0;
1415 struct memory_node *nodes = NULL;
1417 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1418 sysfs__mountpoint());
1420 dir = opendir(path);
1422 pr_debug2("%s: couldn't read %s, does this arch have topology information?\n",
1427 while (!ret && (ent = readdir(dir))) {
1431 if (!strcmp(ent->d_name, ".") ||
1432 !strcmp(ent->d_name, ".."))
1435 r = sscanf(ent->d_name, "node%u", &idx);
1440 struct memory_node *new_nodes =
1441 reallocarray(nodes, cnt + 4, sizeof(*nodes));
1444 pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size);
1451 ret = memory_node__read(&nodes[cnt++], idx);
1458 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1460 memory_node__delete_nodes(nodes, cnt);
1466 * The MEM_TOPOLOGY holds physical memory map for every
1467 * node in system. The format of data is as follows:
1469 * 0 - version | for future changes
1470 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1471 * 16 - count | number of nodes
1473 * For each node we store map of physical indexes for
1476 * 32 - node id | node index
1477 * 40 - size | size of bitmap
1478 * 48 - bitmap | bitmap of memory indexes that belongs to node
1480 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1481 struct evlist *evlist __maybe_unused)
1483 struct memory_node *nodes = NULL;
1484 u64 bsize, version = 1, i, nr = 0;
1487 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1488 (unsigned long long *) &bsize);
1492 ret = build_mem_topology(&nodes, &nr);
1496 ret = do_write(ff, &version, sizeof(version));
1500 ret = do_write(ff, &bsize, sizeof(bsize));
1504 ret = do_write(ff, &nr, sizeof(nr));
1508 for (i = 0; i < nr; i++) {
1509 struct memory_node *n = &nodes[i];
1512 ret = do_write(ff, &n->v, sizeof(n->v)); \
1521 ret = do_write_bitmap(ff, n->set, n->size);
1527 memory_node__delete_nodes(nodes, nr);
1531 static int write_compressed(struct feat_fd *ff __maybe_unused,
1532 struct evlist *evlist __maybe_unused)
1536 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1540 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1544 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1548 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1552 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1555 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu,
1558 struct perf_pmu_caps *caps = NULL;
1561 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps));
1565 list_for_each_entry(caps, &pmu->caps, list) {
1566 ret = do_write_string(ff, caps->name);
1570 ret = do_write_string(ff, caps->value);
1576 ret = do_write_string(ff, pmu->name);
1584 static int write_cpu_pmu_caps(struct feat_fd *ff,
1585 struct evlist *evlist __maybe_unused)
1587 struct perf_pmu *cpu_pmu = perf_pmus__find("cpu");
1593 ret = perf_pmu__caps_parse(cpu_pmu);
1597 return __write_pmu_caps(ff, cpu_pmu, false);
1600 static int write_pmu_caps(struct feat_fd *ff,
1601 struct evlist *evlist __maybe_unused)
1603 struct perf_pmu *pmu = NULL;
1607 while ((pmu = perf_pmus__scan(pmu))) {
1608 if (!pmu->name || !strcmp(pmu->name, "cpu") ||
1609 perf_pmu__caps_parse(pmu) <= 0)
1614 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu));
1622 * Write hybrid pmu caps first to maintain compatibility with
1625 if (perf_pmus__num_core_pmus() > 1) {
1627 while ((pmu = perf_pmus__scan_core(pmu))) {
1628 ret = __write_pmu_caps(ff, pmu, true);
1635 while ((pmu = perf_pmus__scan(pmu))) {
1636 if (pmu->is_core || !pmu->nr_caps)
1639 ret = __write_pmu_caps(ff, pmu, true);
1646 static void print_hostname(struct feat_fd *ff, FILE *fp)
1648 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1651 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1653 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1656 static void print_arch(struct feat_fd *ff, FILE *fp)
1658 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1661 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1663 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1666 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1668 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1669 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1672 static void print_version(struct feat_fd *ff, FILE *fp)
1674 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1677 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1681 nr = ff->ph->env.nr_cmdline;
1683 fprintf(fp, "# cmdline : ");
1685 for (i = 0; i < nr; i++) {
1686 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1688 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1692 char *quote = strchr(argv_i, '\'');
1696 fprintf(fp, "%s\\\'", argv_i);
1699 fprintf(fp, "%s ", argv_i);
1706 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1708 struct perf_header *ph = ff->ph;
1709 int cpu_nr = ph->env.nr_cpus_avail;
1713 nr = ph->env.nr_sibling_cores;
1714 str = ph->env.sibling_cores;
1716 for (i = 0; i < nr; i++) {
1717 fprintf(fp, "# sibling sockets : %s\n", str);
1718 str += strlen(str) + 1;
1721 if (ph->env.nr_sibling_dies) {
1722 nr = ph->env.nr_sibling_dies;
1723 str = ph->env.sibling_dies;
1725 for (i = 0; i < nr; i++) {
1726 fprintf(fp, "# sibling dies : %s\n", str);
1727 str += strlen(str) + 1;
1731 nr = ph->env.nr_sibling_threads;
1732 str = ph->env.sibling_threads;
1734 for (i = 0; i < nr; i++) {
1735 fprintf(fp, "# sibling threads : %s\n", str);
1736 str += strlen(str) + 1;
1739 if (ph->env.nr_sibling_dies) {
1740 if (ph->env.cpu != NULL) {
1741 for (i = 0; i < cpu_nr; i++)
1742 fprintf(fp, "# CPU %d: Core ID %d, "
1743 "Die ID %d, Socket ID %d\n",
1744 i, ph->env.cpu[i].core_id,
1745 ph->env.cpu[i].die_id,
1746 ph->env.cpu[i].socket_id);
1748 fprintf(fp, "# Core ID, Die ID and Socket ID "
1749 "information is not available\n");
1751 if (ph->env.cpu != NULL) {
1752 for (i = 0; i < cpu_nr; i++)
1753 fprintf(fp, "# CPU %d: Core ID %d, "
1755 i, ph->env.cpu[i].core_id,
1756 ph->env.cpu[i].socket_id);
1758 fprintf(fp, "# Core ID and Socket ID "
1759 "information is not available\n");
1763 static void print_clockid(struct feat_fd *ff, FILE *fp)
1765 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1766 ff->ph->env.clock.clockid_res_ns * 1000);
1769 static void print_clock_data(struct feat_fd *ff, FILE *fp)
1771 struct timespec clockid_ns;
1772 char tstr[64], date[64];
1773 struct timeval tod_ns;
1778 if (!ff->ph->env.clock.enabled) {
1779 fprintf(fp, "# reference time disabled\n");
1783 /* Compute TOD time. */
1784 ref = ff->ph->env.clock.tod_ns;
1785 tod_ns.tv_sec = ref / NSEC_PER_SEC;
1786 ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1787 tod_ns.tv_usec = ref / NSEC_PER_USEC;
1789 /* Compute clockid time. */
1790 ref = ff->ph->env.clock.clockid_ns;
1791 clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1792 ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1793 clockid_ns.tv_nsec = ref;
1795 clockid = ff->ph->env.clock.clockid;
1797 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL)
1798 snprintf(tstr, sizeof(tstr), "<error>");
1800 strftime(date, sizeof(date), "%F %T", <ime);
1801 scnprintf(tstr, sizeof(tstr), "%s.%06d",
1802 date, (int) tod_ns.tv_usec);
1805 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1806 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1807 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec,
1808 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec,
1809 clockid_name(clockid));
1812 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp)
1815 struct hybrid_node *n;
1817 fprintf(fp, "# hybrid cpu system:\n");
1818 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) {
1819 n = &ff->ph->env.hybrid_nodes[i];
1820 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus);
1824 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1826 struct perf_session *session;
1827 struct perf_data *data;
1829 session = container_of(ff->ph, struct perf_session, header);
1830 data = session->data;
1832 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1835 #ifdef HAVE_LIBBPF_SUPPORT
1836 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1838 struct perf_env *env = &ff->ph->env;
1839 struct rb_root *root;
1840 struct rb_node *next;
1842 down_read(&env->bpf_progs.lock);
1844 root = &env->bpf_progs.infos;
1845 next = rb_first(root);
1848 struct bpf_prog_info_node *node;
1850 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1851 next = rb_next(&node->rb_node);
1853 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1857 up_read(&env->bpf_progs.lock);
1860 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1862 struct perf_env *env = &ff->ph->env;
1863 struct rb_root *root;
1864 struct rb_node *next;
1866 down_read(&env->bpf_progs.lock);
1868 root = &env->bpf_progs.btfs;
1869 next = rb_first(root);
1872 struct btf_node *node;
1874 node = rb_entry(next, struct btf_node, rb_node);
1875 next = rb_next(&node->rb_node);
1876 fprintf(fp, "# btf info of id %u\n", node->id);
1879 up_read(&env->bpf_progs.lock);
1881 #endif // HAVE_LIBBPF_SUPPORT
1883 static void free_event_desc(struct evsel *events)
1885 struct evsel *evsel;
1890 for (evsel = events; evsel->core.attr.size; evsel++) {
1891 zfree(&evsel->name);
1892 zfree(&evsel->core.id);
1898 static bool perf_attr_check(struct perf_event_attr *attr)
1900 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1901 pr_warning("Reserved bits are set unexpectedly. "
1902 "Please update perf tool.\n");
1906 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1907 pr_warning("Unknown sample type (0x%llx) is detected. "
1908 "Please update perf tool.\n",
1913 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1914 pr_warning("Unknown read format (0x%llx) is detected. "
1915 "Please update perf tool.\n",
1920 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1921 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1922 pr_warning("Unknown branch sample type (0x%llx) is detected. "
1923 "Please update perf tool.\n",
1924 attr->branch_sample_type);
1932 static struct evsel *read_event_desc(struct feat_fd *ff)
1934 struct evsel *evsel, *events = NULL;
1937 u32 nre, sz, nr, i, j;
1940 /* number of events */
1941 if (do_read_u32(ff, &nre))
1944 if (do_read_u32(ff, &sz))
1947 /* buffer to hold on file attr struct */
1952 /* the last event terminates with evsel->core.attr.size == 0: */
1953 events = calloc(nre + 1, sizeof(*events));
1957 msz = sizeof(evsel->core.attr);
1961 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1962 evsel->core.idx = i;
1965 * must read entire on-file attr struct to
1966 * sync up with layout.
1968 if (__do_read(ff, buf, sz))
1971 if (ff->ph->needs_swap)
1972 perf_event__attr_swap(buf);
1974 memcpy(&evsel->core.attr, buf, msz);
1976 if (!perf_attr_check(&evsel->core.attr))
1979 if (do_read_u32(ff, &nr))
1982 if (ff->ph->needs_swap)
1983 evsel->needs_swap = true;
1985 evsel->name = do_read_string(ff);
1992 id = calloc(nr, sizeof(*id));
1995 evsel->core.ids = nr;
1996 evsel->core.id = id;
1998 for (j = 0 ; j < nr; j++) {
1999 if (do_read_u64(ff, id))
2008 free_event_desc(events);
2013 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
2014 void *priv __maybe_unused)
2016 return fprintf(fp, ", %s = %s", name, val);
2019 static void print_event_desc(struct feat_fd *ff, FILE *fp)
2021 struct evsel *evsel, *events;
2026 events = ff->events;
2028 events = read_event_desc(ff);
2031 fprintf(fp, "# event desc: not available or unable to read\n");
2035 for (evsel = events; evsel->core.attr.size; evsel++) {
2036 fprintf(fp, "# event : name = %s, ", evsel->name);
2038 if (evsel->core.ids) {
2039 fprintf(fp, ", id = {");
2040 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
2043 fprintf(fp, " %"PRIu64, *id);
2048 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
2053 free_event_desc(events);
2057 static void print_total_mem(struct feat_fd *ff, FILE *fp)
2059 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
2062 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
2065 struct numa_node *n;
2067 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
2068 n = &ff->ph->env.numa_nodes[i];
2070 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
2071 " free = %"PRIu64" kB\n",
2072 n->node, n->mem_total, n->mem_free);
2074 fprintf(fp, "# node%u cpu list : ", n->node);
2075 cpu_map__fprintf(n->map, fp);
2079 static void print_cpuid(struct feat_fd *ff, FILE *fp)
2081 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
2084 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
2086 fprintf(fp, "# contains samples with branch stack\n");
2089 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
2091 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
2094 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
2096 fprintf(fp, "# contains stat data\n");
2099 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
2103 fprintf(fp, "# CPU cache info:\n");
2104 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
2106 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
2110 static void print_compressed(struct feat_fd *ff, FILE *fp)
2112 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
2113 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
2114 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
2117 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name)
2119 const char *delimiter = "";
2123 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name);
2127 fprintf(fp, "# %s pmu capabilities: ", pmu_name);
2128 for (i = 0; i < nr_caps; i++) {
2129 fprintf(fp, "%s%s", delimiter, caps[i]);
2136 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2138 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps,
2139 ff->ph->env.cpu_pmu_caps, (char *)"cpu");
2142 static void print_pmu_caps(struct feat_fd *ff, FILE *fp)
2144 struct pmu_caps *pmu_caps;
2146 for (int i = 0; i < ff->ph->env.nr_pmus_with_caps; i++) {
2147 pmu_caps = &ff->ph->env.pmu_caps[i];
2148 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps,
2149 pmu_caps->pmu_name);
2153 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
2155 const char *delimiter = "# pmu mappings: ";
2160 pmu_num = ff->ph->env.nr_pmu_mappings;
2162 fprintf(fp, "# pmu mappings: not available\n");
2166 str = ff->ph->env.pmu_mappings;
2169 type = strtoul(str, &tmp, 0);
2174 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
2177 str += strlen(str) + 1;
2186 fprintf(fp, "# pmu mappings: unable to read\n");
2189 static void print_group_desc(struct feat_fd *ff, FILE *fp)
2191 struct perf_session *session;
2192 struct evsel *evsel;
2195 session = container_of(ff->ph, struct perf_session, header);
2197 evlist__for_each_entry(session->evlist, evsel) {
2198 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
2199 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
2201 nr = evsel->core.nr_members - 1;
2203 fprintf(fp, ",%s", evsel__name(evsel));
2211 static void print_sample_time(struct feat_fd *ff, FILE *fp)
2213 struct perf_session *session;
2217 session = container_of(ff->ph, struct perf_session, header);
2219 timestamp__scnprintf_usec(session->evlist->first_sample_time,
2220 time_buf, sizeof(time_buf));
2221 fprintf(fp, "# time of first sample : %s\n", time_buf);
2223 timestamp__scnprintf_usec(session->evlist->last_sample_time,
2224 time_buf, sizeof(time_buf));
2225 fprintf(fp, "# time of last sample : %s\n", time_buf);
2227 d = (double)(session->evlist->last_sample_time -
2228 session->evlist->first_sample_time) / NSEC_PER_MSEC;
2230 fprintf(fp, "# sample duration : %10.3f ms\n", d);
2233 static void memory_node__fprintf(struct memory_node *n,
2234 unsigned long long bsize, FILE *fp)
2236 char buf_map[100], buf_size[50];
2237 unsigned long long size;
2239 size = bsize * bitmap_weight(n->set, n->size);
2240 unit_number__scnprintf(buf_size, 50, size);
2242 bitmap_scnprintf(n->set, n->size, buf_map, 100);
2243 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2246 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2248 struct memory_node *nodes;
2251 nodes = ff->ph->env.memory_nodes;
2252 nr = ff->ph->env.nr_memory_nodes;
2254 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2255 nr, ff->ph->env.memory_bsize);
2257 for (i = 0; i < nr; i++) {
2258 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2262 static int __event_process_build_id(struct perf_record_header_build_id *bev,
2264 struct perf_session *session)
2267 struct machine *machine;
2270 enum dso_space_type dso_space;
2272 machine = perf_session__findnew_machine(session, bev->pid);
2276 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2279 case PERF_RECORD_MISC_KERNEL:
2280 dso_space = DSO_SPACE__KERNEL;
2282 case PERF_RECORD_MISC_GUEST_KERNEL:
2283 dso_space = DSO_SPACE__KERNEL_GUEST;
2285 case PERF_RECORD_MISC_USER:
2286 case PERF_RECORD_MISC_GUEST_USER:
2287 dso_space = DSO_SPACE__USER;
2293 dso = machine__findnew_dso(machine, filename);
2295 char sbuild_id[SBUILD_ID_SIZE];
2296 struct build_id bid;
2297 size_t size = BUILD_ID_SIZE;
2299 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2302 build_id__init(&bid, bev->data, size);
2303 dso__set_build_id(dso, &bid);
2304 dso->header_build_id = 1;
2306 if (dso_space != DSO_SPACE__USER) {
2307 struct kmod_path m = { .name = NULL, };
2309 if (!kmod_path__parse_name(&m, filename) && m.kmod)
2310 dso__set_module_info(dso, &m, machine);
2312 dso->kernel = dso_space;
2316 build_id__sprintf(&dso->bid, sbuild_id);
2317 pr_debug("build id event received for %s: %s [%zu]\n",
2318 dso->long_name, sbuild_id, size);
2327 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2328 int input, u64 offset, u64 size)
2330 struct perf_session *session = container_of(header, struct perf_session, header);
2332 struct perf_event_header header;
2333 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
2336 struct perf_record_header_build_id bev;
2337 char filename[PATH_MAX];
2338 u64 limit = offset + size;
2340 while (offset < limit) {
2343 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
2346 if (header->needs_swap)
2347 perf_event_header__bswap(&old_bev.header);
2349 len = old_bev.header.size - sizeof(old_bev);
2350 if (readn(input, filename, len) != len)
2353 bev.header = old_bev.header;
2356 * As the pid is the missing value, we need to fill
2357 * it properly. The header.misc value give us nice hint.
2359 bev.pid = HOST_KERNEL_ID;
2360 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2361 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2362 bev.pid = DEFAULT_GUEST_KERNEL_ID;
2364 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2365 __event_process_build_id(&bev, filename, session);
2367 offset += bev.header.size;
2373 static int perf_header__read_build_ids(struct perf_header *header,
2374 int input, u64 offset, u64 size)
2376 struct perf_session *session = container_of(header, struct perf_session, header);
2377 struct perf_record_header_build_id bev;
2378 char filename[PATH_MAX];
2379 u64 limit = offset + size, orig_offset = offset;
2382 while (offset < limit) {
2385 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2388 if (header->needs_swap)
2389 perf_event_header__bswap(&bev.header);
2391 len = bev.header.size - sizeof(bev);
2392 if (readn(input, filename, len) != len)
2395 * The a1645ce1 changeset:
2397 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2399 * Added a field to struct perf_record_header_build_id that broke the file
2402 * Since the kernel build-id is the first entry, process the
2403 * table using the old format if the well known
2404 * '[kernel.kallsyms]' string for the kernel build-id has the
2405 * first 4 characters chopped off (where the pid_t sits).
2407 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2408 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2410 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2413 __event_process_build_id(&bev, filename, session);
2415 offset += bev.header.size;
2422 /* Macro for features that simply need to read and store a string. */
2423 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2424 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2426 free(ff->ph->env.__feat_env); \
2427 ff->ph->env.__feat_env = do_read_string(ff); \
2428 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2431 FEAT_PROCESS_STR_FUN(hostname, hostname);
2432 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2433 FEAT_PROCESS_STR_FUN(version, version);
2434 FEAT_PROCESS_STR_FUN(arch, arch);
2435 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2436 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2438 #ifdef HAVE_LIBTRACEEVENT
2439 static int process_tracing_data(struct feat_fd *ff, void *data)
2441 ssize_t ret = trace_report(ff->fd, data, false);
2443 return ret < 0 ? -1 : 0;
2447 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2449 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2450 pr_debug("Failed to read buildids, continuing...\n");
2454 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2457 u32 nr_cpus_avail, nr_cpus_online;
2459 ret = do_read_u32(ff, &nr_cpus_avail);
2463 ret = do_read_u32(ff, &nr_cpus_online);
2466 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2467 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2471 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2476 ret = do_read_u64(ff, &total_mem);
2479 ff->ph->env.total_mem = (unsigned long long)total_mem;
2483 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
2485 struct evsel *evsel;
2487 evlist__for_each_entry(evlist, evsel) {
2488 if (evsel->core.idx == idx)
2495 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
2497 struct evsel *evsel;
2502 evsel = evlist__find_by_index(evlist, event->core.idx);
2509 evsel->name = strdup(event->name);
2513 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2515 struct perf_session *session;
2516 struct evsel *evsel, *events = read_event_desc(ff);
2521 session = container_of(ff->ph, struct perf_session, header);
2523 if (session->data->is_pipe) {
2524 /* Save events for reading later by print_event_desc,
2525 * since they can't be read again in pipe mode. */
2526 ff->events = events;
2529 for (evsel = events; evsel->core.attr.size; evsel++)
2530 evlist__set_event_name(session->evlist, evsel);
2532 if (!session->data->is_pipe)
2533 free_event_desc(events);
2538 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2540 char *str, *cmdline = NULL, **argv = NULL;
2543 if (do_read_u32(ff, &nr))
2546 ff->ph->env.nr_cmdline = nr;
2548 cmdline = zalloc(ff->size + nr + 1);
2552 argv = zalloc(sizeof(char *) * (nr + 1));
2556 for (i = 0; i < nr; i++) {
2557 str = do_read_string(ff);
2561 argv[i] = cmdline + len;
2562 memcpy(argv[i], str, strlen(str) + 1);
2563 len += strlen(str) + 1;
2566 ff->ph->env.cmdline = cmdline;
2567 ff->ph->env.cmdline_argv = (const char **) argv;
2576 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2581 int cpu_nr = ff->ph->env.nr_cpus_avail;
2583 struct perf_header *ph = ff->ph;
2584 bool do_core_id_test = true;
2586 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2590 if (do_read_u32(ff, &nr))
2593 ph->env.nr_sibling_cores = nr;
2594 size += sizeof(u32);
2595 if (strbuf_init(&sb, 128) < 0)
2598 for (i = 0; i < nr; i++) {
2599 str = do_read_string(ff);
2603 /* include a NULL character at the end */
2604 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2606 size += string_size(str);
2609 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2611 if (do_read_u32(ff, &nr))
2614 ph->env.nr_sibling_threads = nr;
2615 size += sizeof(u32);
2617 for (i = 0; i < nr; i++) {
2618 str = do_read_string(ff);
2622 /* include a NULL character at the end */
2623 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2625 size += string_size(str);
2628 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2631 * The header may be from old perf,
2632 * which doesn't include core id and socket id information.
2634 if (ff->size <= size) {
2635 zfree(&ph->env.cpu);
2639 /* On s390 the socket_id number is not related to the numbers of cpus.
2640 * The socket_id number might be higher than the numbers of cpus.
2641 * This depends on the configuration.
2642 * AArch64 is the same.
2644 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2645 || !strncmp(ph->env.arch, "aarch64", 7)))
2646 do_core_id_test = false;
2648 for (i = 0; i < (u32)cpu_nr; i++) {
2649 if (do_read_u32(ff, &nr))
2652 ph->env.cpu[i].core_id = nr;
2653 size += sizeof(u32);
2655 if (do_read_u32(ff, &nr))
2658 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2659 pr_debug("socket_id number is too big."
2660 "You may need to upgrade the perf tool.\n");
2664 ph->env.cpu[i].socket_id = nr;
2665 size += sizeof(u32);
2669 * The header may be from old perf,
2670 * which doesn't include die information.
2672 if (ff->size <= size)
2675 if (do_read_u32(ff, &nr))
2678 ph->env.nr_sibling_dies = nr;
2679 size += sizeof(u32);
2681 for (i = 0; i < nr; i++) {
2682 str = do_read_string(ff);
2686 /* include a NULL character at the end */
2687 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2689 size += string_size(str);
2692 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2694 for (i = 0; i < (u32)cpu_nr; i++) {
2695 if (do_read_u32(ff, &nr))
2698 ph->env.cpu[i].die_id = nr;
2704 strbuf_release(&sb);
2706 zfree(&ph->env.cpu);
2710 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2712 struct numa_node *nodes, *n;
2717 if (do_read_u32(ff, &nr))
2720 nodes = zalloc(sizeof(*nodes) * nr);
2724 for (i = 0; i < nr; i++) {
2728 if (do_read_u32(ff, &n->node))
2731 if (do_read_u64(ff, &n->mem_total))
2734 if (do_read_u64(ff, &n->mem_free))
2737 str = do_read_string(ff);
2741 n->map = perf_cpu_map__new(str);
2747 ff->ph->env.nr_numa_nodes = nr;
2748 ff->ph->env.numa_nodes = nodes;
2756 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2763 if (do_read_u32(ff, &pmu_num))
2767 pr_debug("pmu mappings not available\n");
2771 ff->ph->env.nr_pmu_mappings = pmu_num;
2772 if (strbuf_init(&sb, 128) < 0)
2776 if (do_read_u32(ff, &type))
2779 name = do_read_string(ff);
2783 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2785 /* include a NULL character at the end */
2786 if (strbuf_add(&sb, "", 1) < 0)
2789 if (!strcmp(name, "msr"))
2790 ff->ph->env.msr_pmu_type = type;
2795 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2799 strbuf_release(&sb);
2803 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2806 u32 i, nr, nr_groups;
2807 struct perf_session *session;
2808 struct evsel *evsel, *leader = NULL;
2815 if (do_read_u32(ff, &nr_groups))
2818 ff->ph->env.nr_groups = nr_groups;
2820 pr_debug("group desc not available\n");
2824 desc = calloc(nr_groups, sizeof(*desc));
2828 for (i = 0; i < nr_groups; i++) {
2829 desc[i].name = do_read_string(ff);
2833 if (do_read_u32(ff, &desc[i].leader_idx))
2836 if (do_read_u32(ff, &desc[i].nr_members))
2841 * Rebuild group relationship based on the group_desc
2843 session = container_of(ff->ph, struct perf_session, header);
2846 evlist__for_each_entry(session->evlist, evsel) {
2847 if (i < nr_groups && evsel->core.idx == (int) desc[i].leader_idx) {
2848 evsel__set_leader(evsel, evsel);
2849 /* {anon_group} is a dummy name */
2850 if (strcmp(desc[i].name, "{anon_group}")) {
2851 evsel->group_name = desc[i].name;
2852 desc[i].name = NULL;
2854 evsel->core.nr_members = desc[i].nr_members;
2856 if (i >= nr_groups || nr > 0) {
2857 pr_debug("invalid group desc\n");
2862 nr = evsel->core.nr_members - 1;
2865 /* This is a group member */
2866 evsel__set_leader(evsel, leader);
2872 if (i != nr_groups || nr != 0) {
2873 pr_debug("invalid group desc\n");
2879 for (i = 0; i < nr_groups; i++)
2880 zfree(&desc[i].name);
2886 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2888 struct perf_session *session;
2891 session = container_of(ff->ph, struct perf_session, header);
2893 err = auxtrace_index__process(ff->fd, ff->size, session,
2894 ff->ph->needs_swap);
2896 pr_err("Failed to process auxtrace index\n");
2900 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2902 struct cpu_cache_level *caches;
2903 u32 cnt, i, version;
2905 if (do_read_u32(ff, &version))
2911 if (do_read_u32(ff, &cnt))
2914 caches = zalloc(sizeof(*caches) * cnt);
2918 for (i = 0; i < cnt; i++) {
2919 struct cpu_cache_level c;
2922 if (do_read_u32(ff, &c.v))\
2923 goto out_free_caches; \
2932 c.v = do_read_string(ff); \
2934 goto out_free_caches;
2944 ff->ph->env.caches = caches;
2945 ff->ph->env.caches_cnt = cnt;
2952 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2954 struct perf_session *session;
2955 u64 first_sample_time, last_sample_time;
2958 session = container_of(ff->ph, struct perf_session, header);
2960 ret = do_read_u64(ff, &first_sample_time);
2964 ret = do_read_u64(ff, &last_sample_time);
2968 session->evlist->first_sample_time = first_sample_time;
2969 session->evlist->last_sample_time = last_sample_time;
2973 static int process_mem_topology(struct feat_fd *ff,
2974 void *data __maybe_unused)
2976 struct memory_node *nodes;
2977 u64 version, i, nr, bsize;
2980 if (do_read_u64(ff, &version))
2986 if (do_read_u64(ff, &bsize))
2989 if (do_read_u64(ff, &nr))
2992 nodes = zalloc(sizeof(*nodes) * nr);
2996 for (i = 0; i < nr; i++) {
2997 struct memory_node n;
3000 if (do_read_u64(ff, &n.v)) \
3008 if (do_read_bitmap(ff, &n.set, &n.size))
3014 ff->ph->env.memory_bsize = bsize;
3015 ff->ph->env.memory_nodes = nodes;
3016 ff->ph->env.nr_memory_nodes = nr;
3025 static int process_clockid(struct feat_fd *ff,
3026 void *data __maybe_unused)
3028 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
3034 static int process_clock_data(struct feat_fd *ff,
3035 void *_data __maybe_unused)
3041 if (do_read_u32(ff, &data32))
3048 if (do_read_u32(ff, &data32))
3051 ff->ph->env.clock.clockid = data32;
3054 if (do_read_u64(ff, &data64))
3057 ff->ph->env.clock.tod_ns = data64;
3059 /* clockid ref time */
3060 if (do_read_u64(ff, &data64))
3063 ff->ph->env.clock.clockid_ns = data64;
3064 ff->ph->env.clock.enabled = true;
3068 static int process_hybrid_topology(struct feat_fd *ff,
3069 void *data __maybe_unused)
3071 struct hybrid_node *nodes, *n;
3075 if (do_read_u32(ff, &nr))
3078 nodes = zalloc(sizeof(*nodes) * nr);
3082 for (i = 0; i < nr; i++) {
3085 n->pmu_name = do_read_string(ff);
3089 n->cpus = do_read_string(ff);
3094 ff->ph->env.nr_hybrid_nodes = nr;
3095 ff->ph->env.hybrid_nodes = nodes;
3099 for (i = 0; i < nr; i++) {
3100 free(nodes[i].pmu_name);
3101 free(nodes[i].cpus);
3108 static int process_dir_format(struct feat_fd *ff,
3109 void *_data __maybe_unused)
3111 struct perf_session *session;
3112 struct perf_data *data;
3114 session = container_of(ff->ph, struct perf_session, header);
3115 data = session->data;
3117 if (WARN_ON(!perf_data__is_dir(data)))
3120 return do_read_u64(ff, &data->dir.version);
3123 #ifdef HAVE_LIBBPF_SUPPORT
3124 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
3126 struct bpf_prog_info_node *info_node;
3127 struct perf_env *env = &ff->ph->env;
3128 struct perf_bpil *info_linear;
3132 if (ff->ph->needs_swap) {
3133 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n");
3137 if (do_read_u32(ff, &count))
3140 down_write(&env->bpf_progs.lock);
3142 for (i = 0; i < count; ++i) {
3143 u32 info_len, data_len;
3147 if (do_read_u32(ff, &info_len))
3149 if (do_read_u32(ff, &data_len))
3152 if (info_len > sizeof(struct bpf_prog_info)) {
3153 pr_warning("detected invalid bpf_prog_info\n");
3157 info_linear = malloc(sizeof(struct perf_bpil) +
3161 info_linear->info_len = sizeof(struct bpf_prog_info);
3162 info_linear->data_len = data_len;
3163 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
3165 if (__do_read(ff, &info_linear->info, info_len))
3167 if (info_len < sizeof(struct bpf_prog_info))
3168 memset(((void *)(&info_linear->info)) + info_len, 0,
3169 sizeof(struct bpf_prog_info) - info_len);
3171 if (__do_read(ff, info_linear->data, data_len))
3174 info_node = malloc(sizeof(struct bpf_prog_info_node));
3178 /* after reading from file, translate offset to address */
3179 bpil_offs_to_addr(info_linear);
3180 info_node->info_linear = info_linear;
3181 perf_env__insert_bpf_prog_info(env, info_node);
3184 up_write(&env->bpf_progs.lock);
3189 up_write(&env->bpf_progs.lock);
3193 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
3195 struct perf_env *env = &ff->ph->env;
3196 struct btf_node *node = NULL;
3200 if (ff->ph->needs_swap) {
3201 pr_warning("interpreting btf from systems with endianness is not yet supported\n");
3205 if (do_read_u32(ff, &count))
3208 down_write(&env->bpf_progs.lock);
3210 for (i = 0; i < count; ++i) {
3213 if (do_read_u32(ff, &id))
3215 if (do_read_u32(ff, &data_size))
3218 node = malloc(sizeof(struct btf_node) + data_size);
3223 node->data_size = data_size;
3225 if (__do_read(ff, node->data, data_size))
3228 perf_env__insert_btf(env, node);
3234 up_write(&env->bpf_progs.lock);
3238 #endif // HAVE_LIBBPF_SUPPORT
3240 static int process_compressed(struct feat_fd *ff,
3241 void *data __maybe_unused)
3243 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
3246 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
3249 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
3252 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
3255 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3261 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
3262 char ***caps, unsigned int *max_branches)
3264 char *name, *value, *ptr;
3270 if (do_read_u32(ff, &nr_pmu_caps))
3276 *caps = zalloc(sizeof(char *) * nr_pmu_caps);
3280 for (i = 0; i < nr_pmu_caps; i++) {
3281 name = do_read_string(ff);
3285 value = do_read_string(ff);
3289 if (asprintf(&ptr, "%s=%s", name, value) < 0)
3294 if (!strcmp(name, "branches"))
3295 *max_branches = atoi(value);
3300 *nr_caps = nr_pmu_caps;
3309 free((*caps)[i - 1]);
3316 static int process_cpu_pmu_caps(struct feat_fd *ff,
3317 void *data __maybe_unused)
3319 int ret = __process_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps,
3320 &ff->ph->env.cpu_pmu_caps,
3321 &ff->ph->env.max_branches);
3323 if (!ret && !ff->ph->env.cpu_pmu_caps)
3324 pr_debug("cpu pmu capabilities not available\n");
3328 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
3330 struct pmu_caps *pmu_caps;
3335 if (do_read_u32(ff, &nr_pmu))
3339 pr_debug("pmu capabilities not available\n");
3343 pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
3347 for (i = 0; i < nr_pmu; i++) {
3348 ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps,
3350 &pmu_caps[i].max_branches);
3354 pmu_caps[i].pmu_name = do_read_string(ff);
3355 if (!pmu_caps[i].pmu_name) {
3359 if (!pmu_caps[i].nr_caps) {
3360 pr_debug("%s pmu capabilities not available\n",
3361 pmu_caps[i].pmu_name);
3365 ff->ph->env.nr_pmus_with_caps = nr_pmu;
3366 ff->ph->env.pmu_caps = pmu_caps;
3370 for (i = 0; i < nr_pmu; i++) {
3371 for (j = 0; j < pmu_caps[i].nr_caps; j++)
3372 free(pmu_caps[i].caps[j]);
3373 free(pmu_caps[i].caps);
3374 free(pmu_caps[i].pmu_name);
3381 #define FEAT_OPR(n, func, __full_only) \
3383 .name = __stringify(n), \
3384 .write = write_##func, \
3385 .print = print_##func, \
3386 .full_only = __full_only, \
3387 .process = process_##func, \
3388 .synthesize = true \
3391 #define FEAT_OPN(n, func, __full_only) \
3393 .name = __stringify(n), \
3394 .write = write_##func, \
3395 .print = print_##func, \
3396 .full_only = __full_only, \
3397 .process = process_##func \
3400 /* feature_ops not implemented: */
3401 #define print_tracing_data NULL
3402 #define print_build_id NULL
3404 #define process_branch_stack NULL
3405 #define process_stat NULL
3407 // Only used in util/synthetic-events.c
3408 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
3410 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
3411 #ifdef HAVE_LIBTRACEEVENT
3412 FEAT_OPN(TRACING_DATA, tracing_data, false),
3414 FEAT_OPN(BUILD_ID, build_id, false),
3415 FEAT_OPR(HOSTNAME, hostname, false),
3416 FEAT_OPR(OSRELEASE, osrelease, false),
3417 FEAT_OPR(VERSION, version, false),
3418 FEAT_OPR(ARCH, arch, false),
3419 FEAT_OPR(NRCPUS, nrcpus, false),
3420 FEAT_OPR(CPUDESC, cpudesc, false),
3421 FEAT_OPR(CPUID, cpuid, false),
3422 FEAT_OPR(TOTAL_MEM, total_mem, false),
3423 FEAT_OPR(EVENT_DESC, event_desc, false),
3424 FEAT_OPR(CMDLINE, cmdline, false),
3425 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
3426 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
3427 FEAT_OPN(BRANCH_STACK, branch_stack, false),
3428 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
3429 FEAT_OPR(GROUP_DESC, group_desc, false),
3430 FEAT_OPN(AUXTRACE, auxtrace, false),
3431 FEAT_OPN(STAT, stat, false),
3432 FEAT_OPN(CACHE, cache, true),
3433 FEAT_OPR(SAMPLE_TIME, sample_time, false),
3434 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
3435 FEAT_OPR(CLOCKID, clockid, false),
3436 FEAT_OPN(DIR_FORMAT, dir_format, false),
3437 #ifdef HAVE_LIBBPF_SUPPORT
3438 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
3439 FEAT_OPR(BPF_BTF, bpf_btf, false),
3441 FEAT_OPR(COMPRESSED, compressed, false),
3442 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false),
3443 FEAT_OPR(CLOCK_DATA, clock_data, false),
3444 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true),
3445 FEAT_OPR(PMU_CAPS, pmu_caps, false),
3448 struct header_print_data {
3450 bool full; /* extended list of headers */
3453 static int perf_file_section__fprintf_info(struct perf_file_section *section,
3454 struct perf_header *ph,
3455 int feat, int fd, void *data)
3457 struct header_print_data *hd = data;
3460 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3461 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3462 "%d, continuing...\n", section->offset, feat);
3465 if (feat >= HEADER_LAST_FEATURE) {
3466 pr_warning("unknown feature %d\n", feat);
3469 if (!feat_ops[feat].print)
3472 ff = (struct feat_fd) {
3477 if (!feat_ops[feat].full_only || hd->full)
3478 feat_ops[feat].print(&ff, hd->fp);
3480 fprintf(hd->fp, "# %s info available, use -I to display\n",
3481 feat_ops[feat].name);
3486 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3488 struct header_print_data hd;
3489 struct perf_header *header = &session->header;
3490 int fd = perf_data__fd(session->data);
3498 ret = fstat(fd, &st);
3502 stctime = st.st_mtime;
3503 fprintf(fp, "# captured on : %s", ctime(&stctime));
3505 fprintf(fp, "# header version : %u\n", header->version);
3506 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
3507 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
3508 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
3510 perf_header__process_sections(header, fd, &hd,
3511 perf_file_section__fprintf_info);
3513 if (session->data->is_pipe)
3516 fprintf(fp, "# missing features: ");
3517 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3519 fprintf(fp, "%s ", feat_ops[bit].name);
3527 struct feat_writer fw;
3531 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz)
3533 struct header_fw *h = container_of(fw, struct header_fw, fw);
3535 return do_write(h->ff, buf, sz);
3538 static int do_write_feat(struct feat_fd *ff, int type,
3539 struct perf_file_section **p,
3540 struct evlist *evlist,
3541 struct feat_copier *fc)
3546 if (perf_header__has_feat(ff->ph, type)) {
3547 if (!feat_ops[type].write)
3550 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3553 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
3556 * Hook to let perf inject copy features sections from the input
3559 if (fc && fc->copy) {
3560 struct header_fw h = {
3561 .fw.write = feat_writer_cb,
3565 /* ->copy() returns 0 if the feature was not copied */
3566 err = fc->copy(fc, type, &h.fw);
3571 err = feat_ops[type].write(ff, evlist);
3573 pr_debug("failed to write feature %s\n", feat_ops[type].name);
3575 /* undo anything written */
3576 lseek(ff->fd, (*p)->offset, SEEK_SET);
3580 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
3586 static int perf_header__adds_write(struct perf_header *header,
3587 struct evlist *evlist, int fd,
3588 struct feat_copier *fc)
3592 struct perf_file_section *feat_sec, *p;
3598 ff = (struct feat_fd){
3603 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3607 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3608 if (feat_sec == NULL)
3611 sec_size = sizeof(*feat_sec) * nr_sections;
3613 sec_start = header->feat_offset;
3614 lseek(fd, sec_start + sec_size, SEEK_SET);
3616 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3617 if (do_write_feat(&ff, feat, &p, evlist, fc))
3618 perf_header__clear_feat(header, feat);
3621 lseek(fd, sec_start, SEEK_SET);
3623 * may write more than needed due to dropped feature, but
3624 * this is okay, reader will skip the missing entries
3626 err = do_write(&ff, feat_sec, sec_size);
3628 pr_debug("failed to write feature section\n");
3633 int perf_header__write_pipe(int fd)
3635 struct perf_pipe_file_header f_header;
3639 ff = (struct feat_fd){ .fd = fd };
3641 f_header = (struct perf_pipe_file_header){
3642 .magic = PERF_MAGIC,
3643 .size = sizeof(f_header),
3646 err = do_write(&ff, &f_header, sizeof(f_header));
3648 pr_debug("failed to write perf pipe header\n");
3655 static int perf_session__do_write_header(struct perf_session *session,
3656 struct evlist *evlist,
3657 int fd, bool at_exit,
3658 struct feat_copier *fc)
3660 struct perf_file_header f_header;
3661 struct perf_file_attr f_attr;
3662 struct perf_header *header = &session->header;
3663 struct evsel *evsel;
3668 ff = (struct feat_fd){ .fd = fd};
3669 lseek(fd, sizeof(f_header), SEEK_SET);
3671 evlist__for_each_entry(session->evlist, evsel) {
3672 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3673 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3675 pr_debug("failed to write perf header\n");
3680 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3682 evlist__for_each_entry(evlist, evsel) {
3683 if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3685 * We are likely in "perf inject" and have read
3686 * from an older file. Update attr size so that
3687 * reader gets the right offset to the ids.
3689 evsel->core.attr.size = sizeof(evsel->core.attr);
3691 f_attr = (struct perf_file_attr){
3692 .attr = evsel->core.attr,
3694 .offset = evsel->id_offset,
3695 .size = evsel->core.ids * sizeof(u64),
3698 err = do_write(&ff, &f_attr, sizeof(f_attr));
3700 pr_debug("failed to write perf header attribute\n");
3705 if (!header->data_offset)
3706 header->data_offset = lseek(fd, 0, SEEK_CUR);
3707 header->feat_offset = header->data_offset + header->data_size;
3710 err = perf_header__adds_write(header, evlist, fd, fc);
3715 f_header = (struct perf_file_header){
3716 .magic = PERF_MAGIC,
3717 .size = sizeof(f_header),
3718 .attr_size = sizeof(f_attr),
3720 .offset = attr_offset,
3721 .size = evlist->core.nr_entries * sizeof(f_attr),
3724 .offset = header->data_offset,
3725 .size = header->data_size,
3727 /* event_types is ignored, store zeros */
3730 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3732 lseek(fd, 0, SEEK_SET);
3733 err = do_write(&ff, &f_header, sizeof(f_header));
3735 pr_debug("failed to write perf header\n");
3738 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3743 int perf_session__write_header(struct perf_session *session,
3744 struct evlist *evlist,
3745 int fd, bool at_exit)
3747 return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
3750 size_t perf_session__data_offset(const struct evlist *evlist)
3752 struct evsel *evsel;
3755 data_offset = sizeof(struct perf_file_header);
3756 evlist__for_each_entry(evlist, evsel) {
3757 data_offset += evsel->core.ids * sizeof(u64);
3759 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
3764 int perf_session__inject_header(struct perf_session *session,
3765 struct evlist *evlist,
3767 struct feat_copier *fc)
3769 return perf_session__do_write_header(session, evlist, fd, true, fc);
3772 static int perf_header__getbuffer64(struct perf_header *header,
3773 int fd, void *buf, size_t size)
3775 if (readn(fd, buf, size) <= 0)
3778 if (header->needs_swap)
3779 mem_bswap_64(buf, size);
3784 int perf_header__process_sections(struct perf_header *header, int fd,
3786 int (*process)(struct perf_file_section *section,
3787 struct perf_header *ph,
3788 int feat, int fd, void *data))
3790 struct perf_file_section *feat_sec, *sec;
3796 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3800 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3804 sec_size = sizeof(*feat_sec) * nr_sections;
3806 lseek(fd, header->feat_offset, SEEK_SET);
3808 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3812 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3813 err = process(sec++, header, feat, fd, data);
3823 static const int attr_file_abi_sizes[] = {
3824 [0] = PERF_ATTR_SIZE_VER0,
3825 [1] = PERF_ATTR_SIZE_VER1,
3826 [2] = PERF_ATTR_SIZE_VER2,
3827 [3] = PERF_ATTR_SIZE_VER3,
3828 [4] = PERF_ATTR_SIZE_VER4,
3833 * In the legacy file format, the magic number is not used to encode endianness.
3834 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3835 * on ABI revisions, we need to try all combinations for all endianness to
3836 * detect the endianness.
3838 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3840 uint64_t ref_size, attr_size;
3843 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3844 ref_size = attr_file_abi_sizes[i]
3845 + sizeof(struct perf_file_section);
3846 if (hdr_sz != ref_size) {
3847 attr_size = bswap_64(hdr_sz);
3848 if (attr_size != ref_size)
3851 ph->needs_swap = true;
3853 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3858 /* could not determine endianness */
3862 #define PERF_PIPE_HDR_VER0 16
3864 static const size_t attr_pipe_abi_sizes[] = {
3865 [0] = PERF_PIPE_HDR_VER0,
3870 * In the legacy pipe format, there is an implicit assumption that endianness
3871 * between host recording the samples, and host parsing the samples is the
3872 * same. This is not always the case given that the pipe output may always be
3873 * redirected into a file and analyzed on a different machine with possibly a
3874 * different endianness and perf_event ABI revisions in the perf tool itself.
3876 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3881 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3882 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3883 attr_size = bswap_64(hdr_sz);
3884 if (attr_size != hdr_sz)
3887 ph->needs_swap = true;
3889 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3895 bool is_perf_magic(u64 magic)
3897 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3898 || magic == __perf_magic2
3899 || magic == __perf_magic2_sw)
3905 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3906 bool is_pipe, struct perf_header *ph)
3910 /* check for legacy format */
3911 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3913 ph->version = PERF_HEADER_VERSION_1;
3914 pr_debug("legacy perf.data format\n");
3916 return try_all_pipe_abis(hdr_sz, ph);
3918 return try_all_file_abis(hdr_sz, ph);
3921 * the new magic number serves two purposes:
3922 * - unique number to identify actual perf.data files
3923 * - encode endianness of file
3925 ph->version = PERF_HEADER_VERSION_2;
3927 /* check magic number with one endianness */
3928 if (magic == __perf_magic2)
3931 /* check magic number with opposite endianness */
3932 if (magic != __perf_magic2_sw)
3935 ph->needs_swap = true;
3940 int perf_file_header__read(struct perf_file_header *header,
3941 struct perf_header *ph, int fd)
3945 lseek(fd, 0, SEEK_SET);
3947 ret = readn(fd, header, sizeof(*header));
3951 if (check_magic_endian(header->magic,
3952 header->attr_size, false, ph) < 0) {
3953 pr_debug("magic/endian check failed\n");
3957 if (ph->needs_swap) {
3958 mem_bswap_64(header, offsetof(struct perf_file_header,
3962 if (header->size != sizeof(*header)) {
3963 /* Support the previous format */
3964 if (header->size == offsetof(typeof(*header), adds_features))
3965 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3968 } else if (ph->needs_swap) {
3970 * feature bitmap is declared as an array of unsigned longs --
3971 * not good since its size can differ between the host that
3972 * generated the data file and the host analyzing the file.
3974 * We need to handle endianness, but we don't know the size of
3975 * the unsigned long where the file was generated. Take a best
3976 * guess at determining it: try 64-bit swap first (ie., file
3977 * created on a 64-bit host), and check if the hostname feature
3978 * bit is set (this feature bit is forced on as of fbe96f2).
3979 * If the bit is not, undo the 64-bit swap and try a 32-bit
3980 * swap. If the hostname bit is still not set (e.g., older data
3981 * file), punt and fallback to the original behavior --
3982 * clearing all feature bits and setting buildid.
3984 mem_bswap_64(&header->adds_features,
3985 BITS_TO_U64(HEADER_FEAT_BITS));
3987 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3989 mem_bswap_64(&header->adds_features,
3990 BITS_TO_U64(HEADER_FEAT_BITS));
3993 mem_bswap_32(&header->adds_features,
3994 BITS_TO_U32(HEADER_FEAT_BITS));
3997 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3998 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3999 __set_bit(HEADER_BUILD_ID, header->adds_features);
4003 memcpy(&ph->adds_features, &header->adds_features,
4004 sizeof(ph->adds_features));
4006 ph->data_offset = header->data.offset;
4007 ph->data_size = header->data.size;
4008 ph->feat_offset = header->data.offset + header->data.size;
4012 static int perf_file_section__process(struct perf_file_section *section,
4013 struct perf_header *ph,
4014 int feat, int fd, void *data)
4016 struct feat_fd fdd = {
4019 .size = section->size,
4020 .offset = section->offset,
4023 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
4024 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
4025 "%d, continuing...\n", section->offset, feat);
4029 if (feat >= HEADER_LAST_FEATURE) {
4030 pr_debug("unknown feature %d, continuing...\n", feat);
4034 if (!feat_ops[feat].process)
4037 return feat_ops[feat].process(&fdd, data);
4040 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
4041 struct perf_header *ph,
4042 struct perf_data* data,
4043 bool repipe, int repipe_fd)
4045 struct feat_fd ff = {
4051 ret = perf_data__read(data, header, sizeof(*header));
4055 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
4056 pr_debug("endian/magic failed\n");
4061 header->size = bswap_64(header->size);
4063 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
4069 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd)
4071 struct perf_header *header = &session->header;
4072 struct perf_pipe_file_header f_header;
4074 if (perf_file_header__read_pipe(&f_header, header, session->data,
4075 session->repipe, repipe_fd) < 0) {
4076 pr_debug("incompatible file format\n");
4080 return f_header.size == sizeof(f_header) ? 0 : -1;
4083 static int read_attr(int fd, struct perf_header *ph,
4084 struct perf_file_attr *f_attr)
4086 struct perf_event_attr *attr = &f_attr->attr;
4088 size_t our_sz = sizeof(f_attr->attr);
4091 memset(f_attr, 0, sizeof(*f_attr));
4093 /* read minimal guaranteed structure */
4094 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
4096 pr_debug("cannot read %d bytes of header attr\n",
4097 PERF_ATTR_SIZE_VER0);
4101 /* on file perf_event_attr size */
4109 sz = PERF_ATTR_SIZE_VER0;
4110 } else if (sz > our_sz) {
4111 pr_debug("file uses a more recent and unsupported ABI"
4112 " (%zu bytes extra)\n", sz - our_sz);
4115 /* what we have not yet read and that we know about */
4116 left = sz - PERF_ATTR_SIZE_VER0;
4119 ptr += PERF_ATTR_SIZE_VER0;
4121 ret = readn(fd, ptr, left);
4123 /* read perf_file_section, ids are read in caller */
4124 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
4126 return ret <= 0 ? -1 : 0;
4129 #ifdef HAVE_LIBTRACEEVENT
4130 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
4132 struct tep_event *event;
4135 /* already prepared */
4136 if (evsel->tp_format)
4139 if (pevent == NULL) {
4140 pr_debug("broken or missing trace data\n");
4144 event = tep_find_event(pevent, evsel->core.attr.config);
4145 if (event == NULL) {
4146 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
4151 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
4152 evsel->name = strdup(bf);
4153 if (evsel->name == NULL)
4157 evsel->tp_format = event;
4161 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
4165 evlist__for_each_entry(evlist, pos) {
4166 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
4167 evsel__prepare_tracepoint_event(pos, pevent))
4175 int perf_session__read_header(struct perf_session *session, int repipe_fd)
4177 struct perf_data *data = session->data;
4178 struct perf_header *header = &session->header;
4179 struct perf_file_header f_header;
4180 struct perf_file_attr f_attr;
4182 int nr_attrs, nr_ids, i, j, err;
4183 int fd = perf_data__fd(data);
4185 session->evlist = evlist__new();
4186 if (session->evlist == NULL)
4189 session->evlist->env = &header->env;
4190 session->machines.host.env = &header->env;
4193 * We can read 'pipe' data event from regular file,
4194 * check for the pipe header regardless of source.
4196 err = perf_header__read_pipe(session, repipe_fd);
4197 if (!err || perf_data__is_pipe(data)) {
4198 data->is_pipe = true;
4202 if (perf_file_header__read(&f_header, header, fd) < 0)
4205 if (header->needs_swap && data->in_place_update) {
4206 pr_err("In-place update not supported when byte-swapping is required\n");
4211 * Sanity check that perf.data was written cleanly; data size is
4212 * initialized to 0 and updated only if the on_exit function is run.
4213 * If data size is still 0 then the file contains only partial
4214 * information. Just warn user and process it as much as it can.
4216 if (f_header.data.size == 0) {
4217 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
4218 "Was the 'perf record' command properly terminated?\n",
4222 if (f_header.attr_size == 0) {
4223 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
4224 "Was the 'perf record' command properly terminated?\n",
4229 nr_attrs = f_header.attrs.size / f_header.attr_size;
4230 lseek(fd, f_header.attrs.offset, SEEK_SET);
4232 for (i = 0; i < nr_attrs; i++) {
4233 struct evsel *evsel;
4236 if (read_attr(fd, header, &f_attr) < 0)
4239 if (header->needs_swap) {
4240 f_attr.ids.size = bswap_64(f_attr.ids.size);
4241 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
4242 perf_event__attr_swap(&f_attr.attr);
4245 tmp = lseek(fd, 0, SEEK_CUR);
4246 evsel = evsel__new(&f_attr.attr);
4249 goto out_delete_evlist;
4251 evsel->needs_swap = header->needs_swap;
4253 * Do it before so that if perf_evsel__alloc_id fails, this
4254 * entry gets purged too at evlist__delete().
4256 evlist__add(session->evlist, evsel);
4258 nr_ids = f_attr.ids.size / sizeof(u64);
4260 * We don't have the cpu and thread maps on the header, so
4261 * for allocating the perf_sample_id table we fake 1 cpu and
4262 * hattr->ids threads.
4264 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
4265 goto out_delete_evlist;
4267 lseek(fd, f_attr.ids.offset, SEEK_SET);
4269 for (j = 0; j < nr_ids; j++) {
4270 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
4273 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
4276 lseek(fd, tmp, SEEK_SET);
4279 #ifdef HAVE_LIBTRACEEVENT
4280 perf_header__process_sections(header, fd, &session->tevent,
4281 perf_file_section__process);
4283 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
4284 goto out_delete_evlist;
4286 perf_header__process_sections(header, fd, NULL, perf_file_section__process);
4294 evlist__delete(session->evlist);
4295 session->evlist = NULL;
4299 int perf_event__process_feature(struct perf_session *session,
4300 union perf_event *event)
4302 struct perf_tool *tool = session->tool;
4303 struct feat_fd ff = { .fd = 0 };
4304 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
4305 int type = fe->header.type;
4306 u64 feat = fe->feat_id;
4309 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
4310 pr_warning("invalid record type %d in pipe-mode\n", type);
4313 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
4314 pr_warning("invalid record type %d in pipe-mode\n", type);
4318 if (!feat_ops[feat].process)
4321 ff.buf = (void *)fe->data;
4322 ff.size = event->header.size - sizeof(*fe);
4323 ff.ph = &session->header;
4325 if (feat_ops[feat].process(&ff, NULL)) {
4330 if (!feat_ops[feat].print || !tool->show_feat_hdr)
4333 if (!feat_ops[feat].full_only ||
4334 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
4335 feat_ops[feat].print(&ff, stdout);
4337 fprintf(stdout, "# %s info available, use -I to display\n",
4338 feat_ops[feat].name);
4341 free_event_desc(ff.events);
4345 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
4347 struct perf_record_event_update *ev = &event->event_update;
4348 struct perf_cpu_map *map;
4351 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
4354 case PERF_EVENT_UPDATE__SCALE:
4355 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale);
4357 case PERF_EVENT_UPDATE__UNIT:
4358 ret += fprintf(fp, "... unit: %s\n", ev->unit);
4360 case PERF_EVENT_UPDATE__NAME:
4361 ret += fprintf(fp, "... name: %s\n", ev->name);
4363 case PERF_EVENT_UPDATE__CPUS:
4364 ret += fprintf(fp, "... ");
4366 map = cpu_map__new_data(&ev->cpus.cpus);
4368 ret += cpu_map__fprintf(map, fp);
4370 ret += fprintf(fp, "failed to get cpus\n");
4373 ret += fprintf(fp, "... unknown type\n");
4380 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4381 union perf_event *event,
4382 struct evlist **pevlist)
4385 struct evsel *evsel;
4386 struct evlist *evlist = *pevlist;
4388 if (evlist == NULL) {
4389 *pevlist = evlist = evlist__new();
4394 evsel = evsel__new(&event->attr.attr);
4398 evlist__add(evlist, evsel);
4400 ids = event->header.size;
4401 ids -= (void *)&event->attr.id - (void *)event;
4402 n_ids = ids / sizeof(u64);
4404 * We don't have the cpu and thread maps on the header, so
4405 * for allocating the perf_sample_id table we fake 1 cpu and
4406 * hattr->ids threads.
4408 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
4411 for (i = 0; i < n_ids; i++) {
4412 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
4418 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4419 union perf_event *event,
4420 struct evlist **pevlist)
4422 struct perf_record_event_update *ev = &event->event_update;
4423 struct evlist *evlist;
4424 struct evsel *evsel;
4425 struct perf_cpu_map *map;
4428 perf_event__fprintf_event_update(event, stdout);
4430 if (!pevlist || *pevlist == NULL)
4435 evsel = evlist__id2evsel(evlist, ev->id);
4440 case PERF_EVENT_UPDATE__UNIT:
4441 free((char *)evsel->unit);
4442 evsel->unit = strdup(ev->unit);
4444 case PERF_EVENT_UPDATE__NAME:
4446 evsel->name = strdup(ev->name);
4448 case PERF_EVENT_UPDATE__SCALE:
4449 evsel->scale = ev->scale.scale;
4451 case PERF_EVENT_UPDATE__CPUS:
4452 map = cpu_map__new_data(&ev->cpus.cpus);
4454 perf_cpu_map__put(evsel->core.own_cpus);
4455 evsel->core.own_cpus = map;
4457 pr_err("failed to get event_update cpus\n");
4465 #ifdef HAVE_LIBTRACEEVENT
4466 int perf_event__process_tracing_data(struct perf_session *session,
4467 union perf_event *event)
4469 ssize_t size_read, padding, size = event->tracing_data.size;
4470 int fd = perf_data__fd(session->data);
4474 * The pipe fd is already in proper place and in any case
4475 * we can't move it, and we'd screw the case where we read
4476 * 'pipe' data from regular file. The trace_report reads
4477 * data from 'fd' so we need to set it directly behind the
4478 * event, where the tracing data starts.
4480 if (!perf_data__is_pipe(session->data)) {
4481 off_t offset = lseek(fd, 0, SEEK_CUR);
4483 /* setup for reading amidst mmap */
4484 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4488 size_read = trace_report(fd, &session->tevent,
4490 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4492 if (readn(fd, buf, padding) < 0) {
4493 pr_err("%s: reading input file", __func__);
4496 if (session->repipe) {
4497 int retw = write(STDOUT_FILENO, buf, padding);
4498 if (retw <= 0 || retw != padding) {
4499 pr_err("%s: repiping tracing data padding", __func__);
4504 if (size_read + padding != size) {
4505 pr_err("%s: tracing data size mismatch", __func__);
4509 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
4511 return size_read + padding;
4515 int perf_event__process_build_id(struct perf_session *session,
4516 union perf_event *event)
4518 __event_process_build_id(&event->build_id,
4519 event->build_id.filename,