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)))
752 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
756 while ((pmu = perf_pmus__scan(pmu))) {
757 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
761 ret = do_write_string(ff, pmu->name);
772 * struct group_descs {
774 * struct group_desc {
781 static int write_group_desc(struct feat_fd *ff,
782 struct evlist *evlist)
784 u32 nr_groups = evlist__nr_groups(evlist);
788 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
792 evlist__for_each_entry(evlist, evsel) {
793 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
794 const char *name = evsel->group_name ?: "{anon_group}";
795 u32 leader_idx = evsel->core.idx;
796 u32 nr_members = evsel->core.nr_members;
798 ret = do_write_string(ff, name);
802 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
806 ret = do_write(ff, &nr_members, sizeof(nr_members));
815 * Return the CPU id as a raw string.
817 * Each architecture should provide a more precise id string that
818 * can be use to match the architecture's "mapfile".
820 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
825 /* Return zero when the cpuid from the mapfile.csv matches the
826 * cpuid string generated on this platform.
827 * Otherwise return non-zero.
829 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
832 regmatch_t pmatch[1];
835 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
836 /* Warn unable to generate match particular string. */
837 pr_info("Invalid regular expression %s\n", mapcpuid);
841 match = !regexec(&re, cpuid, 1, pmatch, 0);
844 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
846 /* Verify the entire string matched. */
847 if (match_len == strlen(cpuid))
854 * default get_cpuid(): nothing gets recorded
855 * actual implementation must be in arch/$(SRCARCH)/util/header.c
857 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
859 return ENOSYS; /* Not implemented */
862 static int write_cpuid(struct feat_fd *ff,
863 struct evlist *evlist __maybe_unused)
868 ret = get_cpuid(buffer, sizeof(buffer));
872 return do_write_string(ff, buffer);
875 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
876 struct evlist *evlist __maybe_unused)
881 static int write_auxtrace(struct feat_fd *ff,
882 struct evlist *evlist __maybe_unused)
884 struct perf_session *session;
887 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
890 session = container_of(ff->ph, struct perf_session, header);
892 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
894 pr_err("Failed to write auxtrace index\n");
898 static int write_clockid(struct feat_fd *ff,
899 struct evlist *evlist __maybe_unused)
901 return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
902 sizeof(ff->ph->env.clock.clockid_res_ns));
905 static int write_clock_data(struct feat_fd *ff,
906 struct evlist *evlist __maybe_unused)
915 ret = do_write(ff, &data32, sizeof(data32));
920 data32 = ff->ph->env.clock.clockid;
922 ret = do_write(ff, &data32, sizeof(data32));
927 data64 = &ff->ph->env.clock.tod_ns;
929 ret = do_write(ff, data64, sizeof(*data64));
933 /* clockid ref time */
934 data64 = &ff->ph->env.clock.clockid_ns;
936 return do_write(ff, data64, sizeof(*data64));
939 static int write_hybrid_topology(struct feat_fd *ff,
940 struct evlist *evlist __maybe_unused)
942 struct hybrid_topology *tp;
946 tp = hybrid_topology__new();
950 ret = do_write(ff, &tp->nr, sizeof(u32));
954 for (i = 0; i < tp->nr; i++) {
955 struct hybrid_topology_node *n = &tp->nodes[i];
957 ret = do_write_string(ff, n->pmu_name);
961 ret = do_write_string(ff, n->cpus);
969 hybrid_topology__delete(tp);
973 static int write_dir_format(struct feat_fd *ff,
974 struct evlist *evlist __maybe_unused)
976 struct perf_session *session;
977 struct perf_data *data;
979 session = container_of(ff->ph, struct perf_session, header);
980 data = session->data;
982 if (WARN_ON(!perf_data__is_dir(data)))
985 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
989 * Check whether a CPU is online
992 * 1 -> if CPU is online
993 * 0 -> if CPU is offline
996 int is_cpu_online(unsigned int cpu)
1002 struct stat statbuf;
1004 snprintf(buf, sizeof(buf),
1005 "/sys/devices/system/cpu/cpu%d", cpu);
1006 if (stat(buf, &statbuf) != 0)
1010 * Check if /sys/devices/system/cpu/cpux/online file
1011 * exists. Some cases cpu0 won't have online file since
1012 * it is not expected to be turned off generally.
1013 * In kernels without CONFIG_HOTPLUG_CPU, this
1016 snprintf(buf, sizeof(buf),
1017 "/sys/devices/system/cpu/cpu%d/online", cpu);
1018 if (stat(buf, &statbuf) != 0)
1022 * Read online file using sysfs__read_str.
1023 * If read or open fails, return -1.
1024 * If read succeeds, return value from file
1025 * which gets stored in "str"
1027 snprintf(buf, sizeof(buf),
1028 "devices/system/cpu/cpu%d/online", cpu);
1030 if (sysfs__read_str(buf, &str, &strlen) < 0)
1039 #ifdef HAVE_LIBBPF_SUPPORT
1040 static int write_bpf_prog_info(struct feat_fd *ff,
1041 struct evlist *evlist __maybe_unused)
1043 struct perf_env *env = &ff->ph->env;
1044 struct rb_root *root;
1045 struct rb_node *next;
1048 down_read(&env->bpf_progs.lock);
1050 ret = do_write(ff, &env->bpf_progs.infos_cnt,
1051 sizeof(env->bpf_progs.infos_cnt));
1055 root = &env->bpf_progs.infos;
1056 next = rb_first(root);
1058 struct bpf_prog_info_node *node;
1061 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1062 next = rb_next(&node->rb_node);
1063 len = sizeof(struct perf_bpil) +
1064 node->info_linear->data_len;
1066 /* before writing to file, translate address to offset */
1067 bpil_addr_to_offs(node->info_linear);
1068 ret = do_write(ff, node->info_linear, len);
1070 * translate back to address even when do_write() fails,
1071 * so that this function never changes the data.
1073 bpil_offs_to_addr(node->info_linear);
1078 up_read(&env->bpf_progs.lock);
1082 static int write_bpf_btf(struct feat_fd *ff,
1083 struct evlist *evlist __maybe_unused)
1085 struct perf_env *env = &ff->ph->env;
1086 struct rb_root *root;
1087 struct rb_node *next;
1090 down_read(&env->bpf_progs.lock);
1092 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1093 sizeof(env->bpf_progs.btfs_cnt));
1098 root = &env->bpf_progs.btfs;
1099 next = rb_first(root);
1101 struct btf_node *node;
1103 node = rb_entry(next, struct btf_node, rb_node);
1104 next = rb_next(&node->rb_node);
1105 ret = do_write(ff, &node->id,
1106 sizeof(u32) * 2 + node->data_size);
1111 up_read(&env->bpf_progs.lock);
1114 #endif // HAVE_LIBBPF_SUPPORT
1116 static int cpu_cache_level__sort(const void *a, const void *b)
1118 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1119 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1121 return cache_a->level - cache_b->level;
1124 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1126 if (a->level != b->level)
1129 if (a->line_size != b->line_size)
1132 if (a->sets != b->sets)
1135 if (a->ways != b->ways)
1138 if (strcmp(a->type, b->type))
1141 if (strcmp(a->size, b->size))
1144 if (strcmp(a->map, b->map))
1150 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1152 char path[PATH_MAX], file[PATH_MAX];
1156 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1157 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1159 if (stat(file, &st))
1162 scnprintf(file, PATH_MAX, "%s/level", path);
1163 if (sysfs__read_int(file, (int *) &cache->level))
1166 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1167 if (sysfs__read_int(file, (int *) &cache->line_size))
1170 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1171 if (sysfs__read_int(file, (int *) &cache->sets))
1174 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1175 if (sysfs__read_int(file, (int *) &cache->ways))
1178 scnprintf(file, PATH_MAX, "%s/type", path);
1179 if (sysfs__read_str(file, &cache->type, &len))
1182 cache->type[len] = 0;
1183 cache->type = strim(cache->type);
1185 scnprintf(file, PATH_MAX, "%s/size", path);
1186 if (sysfs__read_str(file, &cache->size, &len)) {
1187 zfree(&cache->type);
1191 cache->size[len] = 0;
1192 cache->size = strim(cache->size);
1194 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1195 if (sysfs__read_str(file, &cache->map, &len)) {
1196 zfree(&cache->size);
1197 zfree(&cache->type);
1201 cache->map[len] = 0;
1202 cache->map = strim(cache->map);
1206 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1208 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1212 * Build caches levels for a particular CPU from the data in
1213 * /sys/devices/system/cpu/cpu<cpu>/cache/
1214 * The cache level data is stored in caches[] from index at
1217 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp)
1221 for (level = 0; level < MAX_CACHE_LVL; level++) {
1222 struct cpu_cache_level c;
1226 err = cpu_cache_level__read(&c, cpu, level);
1233 for (i = 0; i < *cntp; i++) {
1234 if (cpu_cache_level__cmp(&c, &caches[i]))
1242 cpu_cache_level__free(&c);
1248 static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
1250 u32 nr, cpu, cnt = 0;
1252 nr = cpu__max_cpu().cpu;
1254 for (cpu = 0; cpu < nr; cpu++) {
1255 int ret = build_caches_for_cpu(cpu, caches, &cnt);
1264 static int write_cache(struct feat_fd *ff,
1265 struct evlist *evlist __maybe_unused)
1267 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL;
1268 struct cpu_cache_level caches[max_caches];
1269 u32 cnt = 0, i, version = 1;
1272 ret = build_caches(caches, &cnt);
1276 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1278 ret = do_write(ff, &version, sizeof(u32));
1282 ret = do_write(ff, &cnt, sizeof(u32));
1286 for (i = 0; i < cnt; i++) {
1287 struct cpu_cache_level *c = &caches[i];
1290 ret = do_write(ff, &c->v, sizeof(u32)); \
1301 ret = do_write_string(ff, (const char *) c->v); \
1312 for (i = 0; i < cnt; i++)
1313 cpu_cache_level__free(&caches[i]);
1317 static int write_stat(struct feat_fd *ff __maybe_unused,
1318 struct evlist *evlist __maybe_unused)
1323 static int write_sample_time(struct feat_fd *ff,
1324 struct evlist *evlist)
1328 ret = do_write(ff, &evlist->first_sample_time,
1329 sizeof(evlist->first_sample_time));
1333 return do_write(ff, &evlist->last_sample_time,
1334 sizeof(evlist->last_sample_time));
1338 static int memory_node__read(struct memory_node *n, unsigned long idx)
1340 unsigned int phys, size = 0;
1341 char path[PATH_MAX];
1345 #define for_each_memory(mem, dir) \
1346 while ((ent = readdir(dir))) \
1347 if (strcmp(ent->d_name, ".") && \
1348 strcmp(ent->d_name, "..") && \
1349 sscanf(ent->d_name, "memory%u", &mem) == 1)
1351 scnprintf(path, PATH_MAX,
1352 "%s/devices/system/node/node%lu",
1353 sysfs__mountpoint(), idx);
1355 dir = opendir(path);
1357 pr_warning("failed: can't open memory sysfs data\n");
1361 for_each_memory(phys, dir) {
1362 size = max(phys, size);
1367 n->set = bitmap_zalloc(size);
1378 for_each_memory(phys, dir) {
1379 __set_bit(phys, n->set);
1386 static void memory_node__delete_nodes(struct memory_node *nodesp, u64 cnt)
1388 for (u64 i = 0; i < cnt; i++)
1389 bitmap_free(nodesp[i].set);
1394 static int memory_node__sort(const void *a, const void *b)
1396 const struct memory_node *na = a;
1397 const struct memory_node *nb = b;
1399 return na->node - nb->node;
1402 static int build_mem_topology(struct memory_node **nodesp, u64 *cntp)
1404 char path[PATH_MAX];
1408 size_t cnt = 0, size = 0;
1409 struct memory_node *nodes = NULL;
1411 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1412 sysfs__mountpoint());
1414 dir = opendir(path);
1416 pr_debug2("%s: couldn't read %s, does this arch have topology information?\n",
1421 while (!ret && (ent = readdir(dir))) {
1425 if (!strcmp(ent->d_name, ".") ||
1426 !strcmp(ent->d_name, ".."))
1429 r = sscanf(ent->d_name, "node%u", &idx);
1434 struct memory_node *new_nodes =
1435 reallocarray(nodes, cnt + 4, sizeof(*nodes));
1438 pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size);
1445 ret = memory_node__read(&nodes[cnt++], idx);
1452 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1454 memory_node__delete_nodes(nodes, cnt);
1460 * The MEM_TOPOLOGY holds physical memory map for every
1461 * node in system. The format of data is as follows:
1463 * 0 - version | for future changes
1464 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1465 * 16 - count | number of nodes
1467 * For each node we store map of physical indexes for
1470 * 32 - node id | node index
1471 * 40 - size | size of bitmap
1472 * 48 - bitmap | bitmap of memory indexes that belongs to node
1474 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1475 struct evlist *evlist __maybe_unused)
1477 struct memory_node *nodes = NULL;
1478 u64 bsize, version = 1, i, nr = 0;
1481 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1482 (unsigned long long *) &bsize);
1486 ret = build_mem_topology(&nodes, &nr);
1490 ret = do_write(ff, &version, sizeof(version));
1494 ret = do_write(ff, &bsize, sizeof(bsize));
1498 ret = do_write(ff, &nr, sizeof(nr));
1502 for (i = 0; i < nr; i++) {
1503 struct memory_node *n = &nodes[i];
1506 ret = do_write(ff, &n->v, sizeof(n->v)); \
1515 ret = do_write_bitmap(ff, n->set, n->size);
1521 memory_node__delete_nodes(nodes, nr);
1525 static int write_compressed(struct feat_fd *ff __maybe_unused,
1526 struct evlist *evlist __maybe_unused)
1530 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1534 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1538 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1542 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1546 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1549 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu,
1552 struct perf_pmu_caps *caps = NULL;
1555 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps));
1559 list_for_each_entry(caps, &pmu->caps, list) {
1560 ret = do_write_string(ff, caps->name);
1564 ret = do_write_string(ff, caps->value);
1570 ret = do_write_string(ff, pmu->name);
1578 static int write_cpu_pmu_caps(struct feat_fd *ff,
1579 struct evlist *evlist __maybe_unused)
1581 struct perf_pmu *cpu_pmu = perf_pmus__find("cpu");
1587 ret = perf_pmu__caps_parse(cpu_pmu);
1591 return __write_pmu_caps(ff, cpu_pmu, false);
1594 static int write_pmu_caps(struct feat_fd *ff,
1595 struct evlist *evlist __maybe_unused)
1597 struct perf_pmu *pmu = NULL;
1601 while ((pmu = perf_pmus__scan(pmu))) {
1602 if (!strcmp(pmu->name, "cpu")) {
1604 * The "cpu" PMU is special and covered by
1605 * HEADER_CPU_PMU_CAPS. Note, core PMUs are
1606 * counted/written here for ARM, s390 and Intel hybrid.
1610 if (perf_pmu__caps_parse(pmu) <= 0)
1615 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu));
1623 * Note older perf tools assume core PMUs come first, this is a property
1624 * of perf_pmus__scan.
1627 while ((pmu = perf_pmus__scan(pmu))) {
1628 if (!strcmp(pmu->name, "cpu")) {
1629 /* Skip as above. */
1632 if (perf_pmu__caps_parse(pmu) <= 0)
1634 ret = __write_pmu_caps(ff, pmu, true);
1641 static void print_hostname(struct feat_fd *ff, FILE *fp)
1643 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1646 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1648 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1651 static void print_arch(struct feat_fd *ff, FILE *fp)
1653 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1656 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1658 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1661 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1663 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1664 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1667 static void print_version(struct feat_fd *ff, FILE *fp)
1669 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1672 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1676 nr = ff->ph->env.nr_cmdline;
1678 fprintf(fp, "# cmdline : ");
1680 for (i = 0; i < nr; i++) {
1681 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1683 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1687 char *quote = strchr(argv_i, '\'');
1691 fprintf(fp, "%s\\\'", argv_i);
1694 fprintf(fp, "%s ", argv_i);
1701 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1703 struct perf_header *ph = ff->ph;
1704 int cpu_nr = ph->env.nr_cpus_avail;
1708 nr = ph->env.nr_sibling_cores;
1709 str = ph->env.sibling_cores;
1711 for (i = 0; i < nr; i++) {
1712 fprintf(fp, "# sibling sockets : %s\n", str);
1713 str += strlen(str) + 1;
1716 if (ph->env.nr_sibling_dies) {
1717 nr = ph->env.nr_sibling_dies;
1718 str = ph->env.sibling_dies;
1720 for (i = 0; i < nr; i++) {
1721 fprintf(fp, "# sibling dies : %s\n", str);
1722 str += strlen(str) + 1;
1726 nr = ph->env.nr_sibling_threads;
1727 str = ph->env.sibling_threads;
1729 for (i = 0; i < nr; i++) {
1730 fprintf(fp, "# sibling threads : %s\n", str);
1731 str += strlen(str) + 1;
1734 if (ph->env.nr_sibling_dies) {
1735 if (ph->env.cpu != NULL) {
1736 for (i = 0; i < cpu_nr; i++)
1737 fprintf(fp, "# CPU %d: Core ID %d, "
1738 "Die ID %d, Socket ID %d\n",
1739 i, ph->env.cpu[i].core_id,
1740 ph->env.cpu[i].die_id,
1741 ph->env.cpu[i].socket_id);
1743 fprintf(fp, "# Core ID, Die ID and Socket ID "
1744 "information is not available\n");
1746 if (ph->env.cpu != NULL) {
1747 for (i = 0; i < cpu_nr; i++)
1748 fprintf(fp, "# CPU %d: Core ID %d, "
1750 i, ph->env.cpu[i].core_id,
1751 ph->env.cpu[i].socket_id);
1753 fprintf(fp, "# Core ID and Socket ID "
1754 "information is not available\n");
1758 static void print_clockid(struct feat_fd *ff, FILE *fp)
1760 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1761 ff->ph->env.clock.clockid_res_ns * 1000);
1764 static void print_clock_data(struct feat_fd *ff, FILE *fp)
1766 struct timespec clockid_ns;
1767 char tstr[64], date[64];
1768 struct timeval tod_ns;
1773 if (!ff->ph->env.clock.enabled) {
1774 fprintf(fp, "# reference time disabled\n");
1778 /* Compute TOD time. */
1779 ref = ff->ph->env.clock.tod_ns;
1780 tod_ns.tv_sec = ref / NSEC_PER_SEC;
1781 ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1782 tod_ns.tv_usec = ref / NSEC_PER_USEC;
1784 /* Compute clockid time. */
1785 ref = ff->ph->env.clock.clockid_ns;
1786 clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1787 ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1788 clockid_ns.tv_nsec = ref;
1790 clockid = ff->ph->env.clock.clockid;
1792 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL)
1793 snprintf(tstr, sizeof(tstr), "<error>");
1795 strftime(date, sizeof(date), "%F %T", <ime);
1796 scnprintf(tstr, sizeof(tstr), "%s.%06d",
1797 date, (int) tod_ns.tv_usec);
1800 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1801 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1802 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec,
1803 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec,
1804 clockid_name(clockid));
1807 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp)
1810 struct hybrid_node *n;
1812 fprintf(fp, "# hybrid cpu system:\n");
1813 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) {
1814 n = &ff->ph->env.hybrid_nodes[i];
1815 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus);
1819 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1821 struct perf_session *session;
1822 struct perf_data *data;
1824 session = container_of(ff->ph, struct perf_session, header);
1825 data = session->data;
1827 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1830 #ifdef HAVE_LIBBPF_SUPPORT
1831 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1833 struct perf_env *env = &ff->ph->env;
1834 struct rb_root *root;
1835 struct rb_node *next;
1837 down_read(&env->bpf_progs.lock);
1839 root = &env->bpf_progs.infos;
1840 next = rb_first(root);
1843 struct bpf_prog_info_node *node;
1845 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1846 next = rb_next(&node->rb_node);
1848 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1852 up_read(&env->bpf_progs.lock);
1855 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1857 struct perf_env *env = &ff->ph->env;
1858 struct rb_root *root;
1859 struct rb_node *next;
1861 down_read(&env->bpf_progs.lock);
1863 root = &env->bpf_progs.btfs;
1864 next = rb_first(root);
1867 struct btf_node *node;
1869 node = rb_entry(next, struct btf_node, rb_node);
1870 next = rb_next(&node->rb_node);
1871 fprintf(fp, "# btf info of id %u\n", node->id);
1874 up_read(&env->bpf_progs.lock);
1876 #endif // HAVE_LIBBPF_SUPPORT
1878 static void free_event_desc(struct evsel *events)
1880 struct evsel *evsel;
1885 for (evsel = events; evsel->core.attr.size; evsel++) {
1886 zfree(&evsel->name);
1887 zfree(&evsel->core.id);
1893 static bool perf_attr_check(struct perf_event_attr *attr)
1895 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1896 pr_warning("Reserved bits are set unexpectedly. "
1897 "Please update perf tool.\n");
1901 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1902 pr_warning("Unknown sample type (0x%llx) is detected. "
1903 "Please update perf tool.\n",
1908 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1909 pr_warning("Unknown read format (0x%llx) is detected. "
1910 "Please update perf tool.\n",
1915 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1916 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1917 pr_warning("Unknown branch sample type (0x%llx) is detected. "
1918 "Please update perf tool.\n",
1919 attr->branch_sample_type);
1927 static struct evsel *read_event_desc(struct feat_fd *ff)
1929 struct evsel *evsel, *events = NULL;
1932 u32 nre, sz, nr, i, j;
1935 /* number of events */
1936 if (do_read_u32(ff, &nre))
1939 if (do_read_u32(ff, &sz))
1942 /* buffer to hold on file attr struct */
1947 /* the last event terminates with evsel->core.attr.size == 0: */
1948 events = calloc(nre + 1, sizeof(*events));
1952 msz = sizeof(evsel->core.attr);
1956 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1957 evsel->core.idx = i;
1960 * must read entire on-file attr struct to
1961 * sync up with layout.
1963 if (__do_read(ff, buf, sz))
1966 if (ff->ph->needs_swap)
1967 perf_event__attr_swap(buf);
1969 memcpy(&evsel->core.attr, buf, msz);
1971 if (!perf_attr_check(&evsel->core.attr))
1974 if (do_read_u32(ff, &nr))
1977 if (ff->ph->needs_swap)
1978 evsel->needs_swap = true;
1980 evsel->name = do_read_string(ff);
1987 id = calloc(nr, sizeof(*id));
1990 evsel->core.ids = nr;
1991 evsel->core.id = id;
1993 for (j = 0 ; j < nr; j++) {
1994 if (do_read_u64(ff, id))
2003 free_event_desc(events);
2008 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
2009 void *priv __maybe_unused)
2011 return fprintf(fp, ", %s = %s", name, val);
2014 static void print_event_desc(struct feat_fd *ff, FILE *fp)
2016 struct evsel *evsel, *events;
2021 events = ff->events;
2023 events = read_event_desc(ff);
2026 fprintf(fp, "# event desc: not available or unable to read\n");
2030 for (evsel = events; evsel->core.attr.size; evsel++) {
2031 fprintf(fp, "# event : name = %s, ", evsel->name);
2033 if (evsel->core.ids) {
2034 fprintf(fp, ", id = {");
2035 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
2038 fprintf(fp, " %"PRIu64, *id);
2043 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
2048 free_event_desc(events);
2052 static void print_total_mem(struct feat_fd *ff, FILE *fp)
2054 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
2057 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
2060 struct numa_node *n;
2062 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
2063 n = &ff->ph->env.numa_nodes[i];
2065 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
2066 " free = %"PRIu64" kB\n",
2067 n->node, n->mem_total, n->mem_free);
2069 fprintf(fp, "# node%u cpu list : ", n->node);
2070 cpu_map__fprintf(n->map, fp);
2074 static void print_cpuid(struct feat_fd *ff, FILE *fp)
2076 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
2079 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
2081 fprintf(fp, "# contains samples with branch stack\n");
2084 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
2086 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
2089 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
2091 fprintf(fp, "# contains stat data\n");
2094 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
2098 fprintf(fp, "# CPU cache info:\n");
2099 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
2101 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
2105 static void print_compressed(struct feat_fd *ff, FILE *fp)
2107 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
2108 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
2109 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
2112 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name)
2114 const char *delimiter = "";
2118 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name);
2122 fprintf(fp, "# %s pmu capabilities: ", pmu_name);
2123 for (i = 0; i < nr_caps; i++) {
2124 fprintf(fp, "%s%s", delimiter, caps[i]);
2131 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2133 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps,
2134 ff->ph->env.cpu_pmu_caps, (char *)"cpu");
2137 static void print_pmu_caps(struct feat_fd *ff, FILE *fp)
2139 struct pmu_caps *pmu_caps;
2141 for (int i = 0; i < ff->ph->env.nr_pmus_with_caps; i++) {
2142 pmu_caps = &ff->ph->env.pmu_caps[i];
2143 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps,
2144 pmu_caps->pmu_name);
2148 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
2150 const char *delimiter = "# pmu mappings: ";
2155 pmu_num = ff->ph->env.nr_pmu_mappings;
2157 fprintf(fp, "# pmu mappings: not available\n");
2161 str = ff->ph->env.pmu_mappings;
2164 type = strtoul(str, &tmp, 0);
2169 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
2172 str += strlen(str) + 1;
2181 fprintf(fp, "# pmu mappings: unable to read\n");
2184 static void print_group_desc(struct feat_fd *ff, FILE *fp)
2186 struct perf_session *session;
2187 struct evsel *evsel;
2190 session = container_of(ff->ph, struct perf_session, header);
2192 evlist__for_each_entry(session->evlist, evsel) {
2193 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
2194 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
2196 nr = evsel->core.nr_members - 1;
2198 fprintf(fp, ",%s", evsel__name(evsel));
2206 static void print_sample_time(struct feat_fd *ff, FILE *fp)
2208 struct perf_session *session;
2212 session = container_of(ff->ph, struct perf_session, header);
2214 timestamp__scnprintf_usec(session->evlist->first_sample_time,
2215 time_buf, sizeof(time_buf));
2216 fprintf(fp, "# time of first sample : %s\n", time_buf);
2218 timestamp__scnprintf_usec(session->evlist->last_sample_time,
2219 time_buf, sizeof(time_buf));
2220 fprintf(fp, "# time of last sample : %s\n", time_buf);
2222 d = (double)(session->evlist->last_sample_time -
2223 session->evlist->first_sample_time) / NSEC_PER_MSEC;
2225 fprintf(fp, "# sample duration : %10.3f ms\n", d);
2228 static void memory_node__fprintf(struct memory_node *n,
2229 unsigned long long bsize, FILE *fp)
2231 char buf_map[100], buf_size[50];
2232 unsigned long long size;
2234 size = bsize * bitmap_weight(n->set, n->size);
2235 unit_number__scnprintf(buf_size, 50, size);
2237 bitmap_scnprintf(n->set, n->size, buf_map, 100);
2238 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2241 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2243 struct memory_node *nodes;
2246 nodes = ff->ph->env.memory_nodes;
2247 nr = ff->ph->env.nr_memory_nodes;
2249 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2250 nr, ff->ph->env.memory_bsize);
2252 for (i = 0; i < nr; i++) {
2253 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2257 static int __event_process_build_id(struct perf_record_header_build_id *bev,
2259 struct perf_session *session)
2262 struct machine *machine;
2265 enum dso_space_type dso_space;
2267 machine = perf_session__findnew_machine(session, bev->pid);
2271 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2274 case PERF_RECORD_MISC_KERNEL:
2275 dso_space = DSO_SPACE__KERNEL;
2277 case PERF_RECORD_MISC_GUEST_KERNEL:
2278 dso_space = DSO_SPACE__KERNEL_GUEST;
2280 case PERF_RECORD_MISC_USER:
2281 case PERF_RECORD_MISC_GUEST_USER:
2282 dso_space = DSO_SPACE__USER;
2288 dso = machine__findnew_dso(machine, filename);
2290 char sbuild_id[SBUILD_ID_SIZE];
2291 struct build_id bid;
2292 size_t size = BUILD_ID_SIZE;
2294 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2297 build_id__init(&bid, bev->data, size);
2298 dso__set_build_id(dso, &bid);
2299 dso->header_build_id = 1;
2301 if (dso_space != DSO_SPACE__USER) {
2302 struct kmod_path m = { .name = NULL, };
2304 if (!kmod_path__parse_name(&m, filename) && m.kmod)
2305 dso__set_module_info(dso, &m, machine);
2307 dso->kernel = dso_space;
2311 build_id__sprintf(&dso->bid, sbuild_id);
2312 pr_debug("build id event received for %s: %s [%zu]\n",
2313 dso->long_name, sbuild_id, size);
2322 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2323 int input, u64 offset, u64 size)
2325 struct perf_session *session = container_of(header, struct perf_session, header);
2327 struct perf_event_header header;
2328 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
2331 struct perf_record_header_build_id bev;
2332 char filename[PATH_MAX];
2333 u64 limit = offset + size;
2335 while (offset < limit) {
2338 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
2341 if (header->needs_swap)
2342 perf_event_header__bswap(&old_bev.header);
2344 len = old_bev.header.size - sizeof(old_bev);
2345 if (readn(input, filename, len) != len)
2348 bev.header = old_bev.header;
2351 * As the pid is the missing value, we need to fill
2352 * it properly. The header.misc value give us nice hint.
2354 bev.pid = HOST_KERNEL_ID;
2355 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2356 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2357 bev.pid = DEFAULT_GUEST_KERNEL_ID;
2359 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2360 __event_process_build_id(&bev, filename, session);
2362 offset += bev.header.size;
2368 static int perf_header__read_build_ids(struct perf_header *header,
2369 int input, u64 offset, u64 size)
2371 struct perf_session *session = container_of(header, struct perf_session, header);
2372 struct perf_record_header_build_id bev;
2373 char filename[PATH_MAX];
2374 u64 limit = offset + size, orig_offset = offset;
2377 while (offset < limit) {
2380 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2383 if (header->needs_swap)
2384 perf_event_header__bswap(&bev.header);
2386 len = bev.header.size - sizeof(bev);
2387 if (readn(input, filename, len) != len)
2390 * The a1645ce1 changeset:
2392 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2394 * Added a field to struct perf_record_header_build_id that broke the file
2397 * Since the kernel build-id is the first entry, process the
2398 * table using the old format if the well known
2399 * '[kernel.kallsyms]' string for the kernel build-id has the
2400 * first 4 characters chopped off (where the pid_t sits).
2402 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2403 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2405 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2408 __event_process_build_id(&bev, filename, session);
2410 offset += bev.header.size;
2417 /* Macro for features that simply need to read and store a string. */
2418 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2419 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2421 free(ff->ph->env.__feat_env); \
2422 ff->ph->env.__feat_env = do_read_string(ff); \
2423 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2426 FEAT_PROCESS_STR_FUN(hostname, hostname);
2427 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2428 FEAT_PROCESS_STR_FUN(version, version);
2429 FEAT_PROCESS_STR_FUN(arch, arch);
2430 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2431 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2433 #ifdef HAVE_LIBTRACEEVENT
2434 static int process_tracing_data(struct feat_fd *ff, void *data)
2436 ssize_t ret = trace_report(ff->fd, data, false);
2438 return ret < 0 ? -1 : 0;
2442 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2444 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2445 pr_debug("Failed to read buildids, continuing...\n");
2449 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2452 u32 nr_cpus_avail, nr_cpus_online;
2454 ret = do_read_u32(ff, &nr_cpus_avail);
2458 ret = do_read_u32(ff, &nr_cpus_online);
2461 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2462 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2466 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2471 ret = do_read_u64(ff, &total_mem);
2474 ff->ph->env.total_mem = (unsigned long long)total_mem;
2478 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
2480 struct evsel *evsel;
2482 evlist__for_each_entry(evlist, evsel) {
2483 if (evsel->core.idx == idx)
2490 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
2492 struct evsel *evsel;
2497 evsel = evlist__find_by_index(evlist, event->core.idx);
2504 evsel->name = strdup(event->name);
2508 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2510 struct perf_session *session;
2511 struct evsel *evsel, *events = read_event_desc(ff);
2516 session = container_of(ff->ph, struct perf_session, header);
2518 if (session->data->is_pipe) {
2519 /* Save events for reading later by print_event_desc,
2520 * since they can't be read again in pipe mode. */
2521 ff->events = events;
2524 for (evsel = events; evsel->core.attr.size; evsel++)
2525 evlist__set_event_name(session->evlist, evsel);
2527 if (!session->data->is_pipe)
2528 free_event_desc(events);
2533 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2535 char *str, *cmdline = NULL, **argv = NULL;
2538 if (do_read_u32(ff, &nr))
2541 ff->ph->env.nr_cmdline = nr;
2543 cmdline = zalloc(ff->size + nr + 1);
2547 argv = zalloc(sizeof(char *) * (nr + 1));
2551 for (i = 0; i < nr; i++) {
2552 str = do_read_string(ff);
2556 argv[i] = cmdline + len;
2557 memcpy(argv[i], str, strlen(str) + 1);
2558 len += strlen(str) + 1;
2561 ff->ph->env.cmdline = cmdline;
2562 ff->ph->env.cmdline_argv = (const char **) argv;
2571 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2576 int cpu_nr = ff->ph->env.nr_cpus_avail;
2578 struct perf_header *ph = ff->ph;
2579 bool do_core_id_test = true;
2581 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2585 if (do_read_u32(ff, &nr))
2588 ph->env.nr_sibling_cores = nr;
2589 size += sizeof(u32);
2590 if (strbuf_init(&sb, 128) < 0)
2593 for (i = 0; i < nr; i++) {
2594 str = do_read_string(ff);
2598 /* include a NULL character at the end */
2599 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2601 size += string_size(str);
2604 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2606 if (do_read_u32(ff, &nr))
2609 ph->env.nr_sibling_threads = nr;
2610 size += sizeof(u32);
2612 for (i = 0; i < nr; i++) {
2613 str = do_read_string(ff);
2617 /* include a NULL character at the end */
2618 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2620 size += string_size(str);
2623 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2626 * The header may be from old perf,
2627 * which doesn't include core id and socket id information.
2629 if (ff->size <= size) {
2630 zfree(&ph->env.cpu);
2634 /* On s390 the socket_id number is not related to the numbers of cpus.
2635 * The socket_id number might be higher than the numbers of cpus.
2636 * This depends on the configuration.
2637 * AArch64 is the same.
2639 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2640 || !strncmp(ph->env.arch, "aarch64", 7)))
2641 do_core_id_test = false;
2643 for (i = 0; i < (u32)cpu_nr; i++) {
2644 if (do_read_u32(ff, &nr))
2647 ph->env.cpu[i].core_id = nr;
2648 size += sizeof(u32);
2650 if (do_read_u32(ff, &nr))
2653 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2654 pr_debug("socket_id number is too big."
2655 "You may need to upgrade the perf tool.\n");
2659 ph->env.cpu[i].socket_id = nr;
2660 size += sizeof(u32);
2664 * The header may be from old perf,
2665 * which doesn't include die information.
2667 if (ff->size <= size)
2670 if (do_read_u32(ff, &nr))
2673 ph->env.nr_sibling_dies = nr;
2674 size += sizeof(u32);
2676 for (i = 0; i < nr; i++) {
2677 str = do_read_string(ff);
2681 /* include a NULL character at the end */
2682 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2684 size += string_size(str);
2687 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2689 for (i = 0; i < (u32)cpu_nr; i++) {
2690 if (do_read_u32(ff, &nr))
2693 ph->env.cpu[i].die_id = nr;
2699 strbuf_release(&sb);
2701 zfree(&ph->env.cpu);
2705 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2707 struct numa_node *nodes, *n;
2712 if (do_read_u32(ff, &nr))
2715 nodes = zalloc(sizeof(*nodes) * nr);
2719 for (i = 0; i < nr; i++) {
2723 if (do_read_u32(ff, &n->node))
2726 if (do_read_u64(ff, &n->mem_total))
2729 if (do_read_u64(ff, &n->mem_free))
2732 str = do_read_string(ff);
2736 n->map = perf_cpu_map__new(str);
2742 ff->ph->env.nr_numa_nodes = nr;
2743 ff->ph->env.numa_nodes = nodes;
2751 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2758 if (do_read_u32(ff, &pmu_num))
2762 pr_debug("pmu mappings not available\n");
2766 ff->ph->env.nr_pmu_mappings = pmu_num;
2767 if (strbuf_init(&sb, 128) < 0)
2771 if (do_read_u32(ff, &type))
2774 name = do_read_string(ff);
2778 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2780 /* include a NULL character at the end */
2781 if (strbuf_add(&sb, "", 1) < 0)
2784 if (!strcmp(name, "msr"))
2785 ff->ph->env.msr_pmu_type = type;
2790 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2794 strbuf_release(&sb);
2798 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2801 u32 i, nr, nr_groups;
2802 struct perf_session *session;
2803 struct evsel *evsel, *leader = NULL;
2810 if (do_read_u32(ff, &nr_groups))
2813 ff->ph->env.nr_groups = nr_groups;
2815 pr_debug("group desc not available\n");
2819 desc = calloc(nr_groups, sizeof(*desc));
2823 for (i = 0; i < nr_groups; i++) {
2824 desc[i].name = do_read_string(ff);
2828 if (do_read_u32(ff, &desc[i].leader_idx))
2831 if (do_read_u32(ff, &desc[i].nr_members))
2836 * Rebuild group relationship based on the group_desc
2838 session = container_of(ff->ph, struct perf_session, header);
2841 evlist__for_each_entry(session->evlist, evsel) {
2842 if (i < nr_groups && evsel->core.idx == (int) desc[i].leader_idx) {
2843 evsel__set_leader(evsel, evsel);
2844 /* {anon_group} is a dummy name */
2845 if (strcmp(desc[i].name, "{anon_group}")) {
2846 evsel->group_name = desc[i].name;
2847 desc[i].name = NULL;
2849 evsel->core.nr_members = desc[i].nr_members;
2851 if (i >= nr_groups || nr > 0) {
2852 pr_debug("invalid group desc\n");
2857 nr = evsel->core.nr_members - 1;
2860 /* This is a group member */
2861 evsel__set_leader(evsel, leader);
2867 if (i != nr_groups || nr != 0) {
2868 pr_debug("invalid group desc\n");
2874 for (i = 0; i < nr_groups; i++)
2875 zfree(&desc[i].name);
2881 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2883 struct perf_session *session;
2886 session = container_of(ff->ph, struct perf_session, header);
2888 err = auxtrace_index__process(ff->fd, ff->size, session,
2889 ff->ph->needs_swap);
2891 pr_err("Failed to process auxtrace index\n");
2895 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2897 struct cpu_cache_level *caches;
2898 u32 cnt, i, version;
2900 if (do_read_u32(ff, &version))
2906 if (do_read_u32(ff, &cnt))
2909 caches = zalloc(sizeof(*caches) * cnt);
2913 for (i = 0; i < cnt; i++) {
2914 struct cpu_cache_level c;
2917 if (do_read_u32(ff, &c.v))\
2918 goto out_free_caches; \
2927 c.v = do_read_string(ff); \
2929 goto out_free_caches;
2939 ff->ph->env.caches = caches;
2940 ff->ph->env.caches_cnt = cnt;
2947 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2949 struct perf_session *session;
2950 u64 first_sample_time, last_sample_time;
2953 session = container_of(ff->ph, struct perf_session, header);
2955 ret = do_read_u64(ff, &first_sample_time);
2959 ret = do_read_u64(ff, &last_sample_time);
2963 session->evlist->first_sample_time = first_sample_time;
2964 session->evlist->last_sample_time = last_sample_time;
2968 static int process_mem_topology(struct feat_fd *ff,
2969 void *data __maybe_unused)
2971 struct memory_node *nodes;
2972 u64 version, i, nr, bsize;
2975 if (do_read_u64(ff, &version))
2981 if (do_read_u64(ff, &bsize))
2984 if (do_read_u64(ff, &nr))
2987 nodes = zalloc(sizeof(*nodes) * nr);
2991 for (i = 0; i < nr; i++) {
2992 struct memory_node n;
2995 if (do_read_u64(ff, &n.v)) \
3003 if (do_read_bitmap(ff, &n.set, &n.size))
3009 ff->ph->env.memory_bsize = bsize;
3010 ff->ph->env.memory_nodes = nodes;
3011 ff->ph->env.nr_memory_nodes = nr;
3020 static int process_clockid(struct feat_fd *ff,
3021 void *data __maybe_unused)
3023 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
3029 static int process_clock_data(struct feat_fd *ff,
3030 void *_data __maybe_unused)
3036 if (do_read_u32(ff, &data32))
3043 if (do_read_u32(ff, &data32))
3046 ff->ph->env.clock.clockid = data32;
3049 if (do_read_u64(ff, &data64))
3052 ff->ph->env.clock.tod_ns = data64;
3054 /* clockid ref time */
3055 if (do_read_u64(ff, &data64))
3058 ff->ph->env.clock.clockid_ns = data64;
3059 ff->ph->env.clock.enabled = true;
3063 static int process_hybrid_topology(struct feat_fd *ff,
3064 void *data __maybe_unused)
3066 struct hybrid_node *nodes, *n;
3070 if (do_read_u32(ff, &nr))
3073 nodes = zalloc(sizeof(*nodes) * nr);
3077 for (i = 0; i < nr; i++) {
3080 n->pmu_name = do_read_string(ff);
3084 n->cpus = do_read_string(ff);
3089 ff->ph->env.nr_hybrid_nodes = nr;
3090 ff->ph->env.hybrid_nodes = nodes;
3094 for (i = 0; i < nr; i++) {
3095 free(nodes[i].pmu_name);
3096 free(nodes[i].cpus);
3103 static int process_dir_format(struct feat_fd *ff,
3104 void *_data __maybe_unused)
3106 struct perf_session *session;
3107 struct perf_data *data;
3109 session = container_of(ff->ph, struct perf_session, header);
3110 data = session->data;
3112 if (WARN_ON(!perf_data__is_dir(data)))
3115 return do_read_u64(ff, &data->dir.version);
3118 #ifdef HAVE_LIBBPF_SUPPORT
3119 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
3121 struct bpf_prog_info_node *info_node;
3122 struct perf_env *env = &ff->ph->env;
3123 struct perf_bpil *info_linear;
3127 if (ff->ph->needs_swap) {
3128 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n");
3132 if (do_read_u32(ff, &count))
3135 down_write(&env->bpf_progs.lock);
3137 for (i = 0; i < count; ++i) {
3138 u32 info_len, data_len;
3142 if (do_read_u32(ff, &info_len))
3144 if (do_read_u32(ff, &data_len))
3147 if (info_len > sizeof(struct bpf_prog_info)) {
3148 pr_warning("detected invalid bpf_prog_info\n");
3152 info_linear = malloc(sizeof(struct perf_bpil) +
3156 info_linear->info_len = sizeof(struct bpf_prog_info);
3157 info_linear->data_len = data_len;
3158 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
3160 if (__do_read(ff, &info_linear->info, info_len))
3162 if (info_len < sizeof(struct bpf_prog_info))
3163 memset(((void *)(&info_linear->info)) + info_len, 0,
3164 sizeof(struct bpf_prog_info) - info_len);
3166 if (__do_read(ff, info_linear->data, data_len))
3169 info_node = malloc(sizeof(struct bpf_prog_info_node));
3173 /* after reading from file, translate offset to address */
3174 bpil_offs_to_addr(info_linear);
3175 info_node->info_linear = info_linear;
3176 perf_env__insert_bpf_prog_info(env, info_node);
3179 up_write(&env->bpf_progs.lock);
3184 up_write(&env->bpf_progs.lock);
3188 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
3190 struct perf_env *env = &ff->ph->env;
3191 struct btf_node *node = NULL;
3195 if (ff->ph->needs_swap) {
3196 pr_warning("interpreting btf from systems with endianness is not yet supported\n");
3200 if (do_read_u32(ff, &count))
3203 down_write(&env->bpf_progs.lock);
3205 for (i = 0; i < count; ++i) {
3208 if (do_read_u32(ff, &id))
3210 if (do_read_u32(ff, &data_size))
3213 node = malloc(sizeof(struct btf_node) + data_size);
3218 node->data_size = data_size;
3220 if (__do_read(ff, node->data, data_size))
3223 perf_env__insert_btf(env, node);
3229 up_write(&env->bpf_progs.lock);
3233 #endif // HAVE_LIBBPF_SUPPORT
3235 static int process_compressed(struct feat_fd *ff,
3236 void *data __maybe_unused)
3238 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
3241 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
3244 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
3247 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
3250 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3256 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
3257 char ***caps, unsigned int *max_branches)
3259 char *name, *value, *ptr;
3265 if (do_read_u32(ff, &nr_pmu_caps))
3271 *caps = zalloc(sizeof(char *) * nr_pmu_caps);
3275 for (i = 0; i < nr_pmu_caps; i++) {
3276 name = do_read_string(ff);
3280 value = do_read_string(ff);
3284 if (asprintf(&ptr, "%s=%s", name, value) < 0)
3289 if (!strcmp(name, "branches"))
3290 *max_branches = atoi(value);
3295 *nr_caps = nr_pmu_caps;
3304 free((*caps)[i - 1]);
3311 static int process_cpu_pmu_caps(struct feat_fd *ff,
3312 void *data __maybe_unused)
3314 int ret = __process_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps,
3315 &ff->ph->env.cpu_pmu_caps,
3316 &ff->ph->env.max_branches);
3318 if (!ret && !ff->ph->env.cpu_pmu_caps)
3319 pr_debug("cpu pmu capabilities not available\n");
3323 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
3325 struct pmu_caps *pmu_caps;
3330 if (do_read_u32(ff, &nr_pmu))
3334 pr_debug("pmu capabilities not available\n");
3338 pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
3342 for (i = 0; i < nr_pmu; i++) {
3343 ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps,
3345 &pmu_caps[i].max_branches);
3349 pmu_caps[i].pmu_name = do_read_string(ff);
3350 if (!pmu_caps[i].pmu_name) {
3354 if (!pmu_caps[i].nr_caps) {
3355 pr_debug("%s pmu capabilities not available\n",
3356 pmu_caps[i].pmu_name);
3360 ff->ph->env.nr_pmus_with_caps = nr_pmu;
3361 ff->ph->env.pmu_caps = pmu_caps;
3365 for (i = 0; i < nr_pmu; i++) {
3366 for (j = 0; j < pmu_caps[i].nr_caps; j++)
3367 free(pmu_caps[i].caps[j]);
3368 free(pmu_caps[i].caps);
3369 free(pmu_caps[i].pmu_name);
3376 #define FEAT_OPR(n, func, __full_only) \
3378 .name = __stringify(n), \
3379 .write = write_##func, \
3380 .print = print_##func, \
3381 .full_only = __full_only, \
3382 .process = process_##func, \
3383 .synthesize = true \
3386 #define FEAT_OPN(n, func, __full_only) \
3388 .name = __stringify(n), \
3389 .write = write_##func, \
3390 .print = print_##func, \
3391 .full_only = __full_only, \
3392 .process = process_##func \
3395 /* feature_ops not implemented: */
3396 #define print_tracing_data NULL
3397 #define print_build_id NULL
3399 #define process_branch_stack NULL
3400 #define process_stat NULL
3402 // Only used in util/synthetic-events.c
3403 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
3405 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
3406 #ifdef HAVE_LIBTRACEEVENT
3407 FEAT_OPN(TRACING_DATA, tracing_data, false),
3409 FEAT_OPN(BUILD_ID, build_id, false),
3410 FEAT_OPR(HOSTNAME, hostname, false),
3411 FEAT_OPR(OSRELEASE, osrelease, false),
3412 FEAT_OPR(VERSION, version, false),
3413 FEAT_OPR(ARCH, arch, false),
3414 FEAT_OPR(NRCPUS, nrcpus, false),
3415 FEAT_OPR(CPUDESC, cpudesc, false),
3416 FEAT_OPR(CPUID, cpuid, false),
3417 FEAT_OPR(TOTAL_MEM, total_mem, false),
3418 FEAT_OPR(EVENT_DESC, event_desc, false),
3419 FEAT_OPR(CMDLINE, cmdline, false),
3420 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
3421 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
3422 FEAT_OPN(BRANCH_STACK, branch_stack, false),
3423 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
3424 FEAT_OPR(GROUP_DESC, group_desc, false),
3425 FEAT_OPN(AUXTRACE, auxtrace, false),
3426 FEAT_OPN(STAT, stat, false),
3427 FEAT_OPN(CACHE, cache, true),
3428 FEAT_OPR(SAMPLE_TIME, sample_time, false),
3429 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
3430 FEAT_OPR(CLOCKID, clockid, false),
3431 FEAT_OPN(DIR_FORMAT, dir_format, false),
3432 #ifdef HAVE_LIBBPF_SUPPORT
3433 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
3434 FEAT_OPR(BPF_BTF, bpf_btf, false),
3436 FEAT_OPR(COMPRESSED, compressed, false),
3437 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false),
3438 FEAT_OPR(CLOCK_DATA, clock_data, false),
3439 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true),
3440 FEAT_OPR(PMU_CAPS, pmu_caps, false),
3443 struct header_print_data {
3445 bool full; /* extended list of headers */
3448 static int perf_file_section__fprintf_info(struct perf_file_section *section,
3449 struct perf_header *ph,
3450 int feat, int fd, void *data)
3452 struct header_print_data *hd = data;
3455 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3456 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3457 "%d, continuing...\n", section->offset, feat);
3460 if (feat >= HEADER_LAST_FEATURE) {
3461 pr_warning("unknown feature %d\n", feat);
3464 if (!feat_ops[feat].print)
3467 ff = (struct feat_fd) {
3472 if (!feat_ops[feat].full_only || hd->full)
3473 feat_ops[feat].print(&ff, hd->fp);
3475 fprintf(hd->fp, "# %s info available, use -I to display\n",
3476 feat_ops[feat].name);
3481 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3483 struct header_print_data hd;
3484 struct perf_header *header = &session->header;
3485 int fd = perf_data__fd(session->data);
3493 ret = fstat(fd, &st);
3497 stctime = st.st_mtime;
3498 fprintf(fp, "# captured on : %s", ctime(&stctime));
3500 fprintf(fp, "# header version : %u\n", header->version);
3501 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
3502 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
3503 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
3505 perf_header__process_sections(header, fd, &hd,
3506 perf_file_section__fprintf_info);
3508 if (session->data->is_pipe)
3511 fprintf(fp, "# missing features: ");
3512 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3514 fprintf(fp, "%s ", feat_ops[bit].name);
3522 struct feat_writer fw;
3526 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz)
3528 struct header_fw *h = container_of(fw, struct header_fw, fw);
3530 return do_write(h->ff, buf, sz);
3533 static int do_write_feat(struct feat_fd *ff, int type,
3534 struct perf_file_section **p,
3535 struct evlist *evlist,
3536 struct feat_copier *fc)
3541 if (perf_header__has_feat(ff->ph, type)) {
3542 if (!feat_ops[type].write)
3545 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3548 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
3551 * Hook to let perf inject copy features sections from the input
3554 if (fc && fc->copy) {
3555 struct header_fw h = {
3556 .fw.write = feat_writer_cb,
3560 /* ->copy() returns 0 if the feature was not copied */
3561 err = fc->copy(fc, type, &h.fw);
3566 err = feat_ops[type].write(ff, evlist);
3568 pr_debug("failed to write feature %s\n", feat_ops[type].name);
3570 /* undo anything written */
3571 lseek(ff->fd, (*p)->offset, SEEK_SET);
3575 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
3581 static int perf_header__adds_write(struct perf_header *header,
3582 struct evlist *evlist, int fd,
3583 struct feat_copier *fc)
3587 struct perf_file_section *feat_sec, *p;
3593 ff = (struct feat_fd){
3598 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3602 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3603 if (feat_sec == NULL)
3606 sec_size = sizeof(*feat_sec) * nr_sections;
3608 sec_start = header->feat_offset;
3609 lseek(fd, sec_start + sec_size, SEEK_SET);
3611 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3612 if (do_write_feat(&ff, feat, &p, evlist, fc))
3613 perf_header__clear_feat(header, feat);
3616 lseek(fd, sec_start, SEEK_SET);
3618 * may write more than needed due to dropped feature, but
3619 * this is okay, reader will skip the missing entries
3621 err = do_write(&ff, feat_sec, sec_size);
3623 pr_debug("failed to write feature section\n");
3628 int perf_header__write_pipe(int fd)
3630 struct perf_pipe_file_header f_header;
3634 ff = (struct feat_fd){ .fd = fd };
3636 f_header = (struct perf_pipe_file_header){
3637 .magic = PERF_MAGIC,
3638 .size = sizeof(f_header),
3641 err = do_write(&ff, &f_header, sizeof(f_header));
3643 pr_debug("failed to write perf pipe header\n");
3650 static int perf_session__do_write_header(struct perf_session *session,
3651 struct evlist *evlist,
3652 int fd, bool at_exit,
3653 struct feat_copier *fc)
3655 struct perf_file_header f_header;
3656 struct perf_file_attr f_attr;
3657 struct perf_header *header = &session->header;
3658 struct evsel *evsel;
3663 ff = (struct feat_fd){ .fd = fd};
3664 lseek(fd, sizeof(f_header), SEEK_SET);
3666 evlist__for_each_entry(session->evlist, evsel) {
3667 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3668 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3670 pr_debug("failed to write perf header\n");
3675 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3677 evlist__for_each_entry(evlist, evsel) {
3678 if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3680 * We are likely in "perf inject" and have read
3681 * from an older file. Update attr size so that
3682 * reader gets the right offset to the ids.
3684 evsel->core.attr.size = sizeof(evsel->core.attr);
3686 f_attr = (struct perf_file_attr){
3687 .attr = evsel->core.attr,
3689 .offset = evsel->id_offset,
3690 .size = evsel->core.ids * sizeof(u64),
3693 err = do_write(&ff, &f_attr, sizeof(f_attr));
3695 pr_debug("failed to write perf header attribute\n");
3700 if (!header->data_offset)
3701 header->data_offset = lseek(fd, 0, SEEK_CUR);
3702 header->feat_offset = header->data_offset + header->data_size;
3705 err = perf_header__adds_write(header, evlist, fd, fc);
3710 f_header = (struct perf_file_header){
3711 .magic = PERF_MAGIC,
3712 .size = sizeof(f_header),
3713 .attr_size = sizeof(f_attr),
3715 .offset = attr_offset,
3716 .size = evlist->core.nr_entries * sizeof(f_attr),
3719 .offset = header->data_offset,
3720 .size = header->data_size,
3722 /* event_types is ignored, store zeros */
3725 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3727 lseek(fd, 0, SEEK_SET);
3728 err = do_write(&ff, &f_header, sizeof(f_header));
3730 pr_debug("failed to write perf header\n");
3733 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3738 int perf_session__write_header(struct perf_session *session,
3739 struct evlist *evlist,
3740 int fd, bool at_exit)
3742 return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
3745 size_t perf_session__data_offset(const struct evlist *evlist)
3747 struct evsel *evsel;
3750 data_offset = sizeof(struct perf_file_header);
3751 evlist__for_each_entry(evlist, evsel) {
3752 data_offset += evsel->core.ids * sizeof(u64);
3754 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
3759 int perf_session__inject_header(struct perf_session *session,
3760 struct evlist *evlist,
3762 struct feat_copier *fc)
3764 return perf_session__do_write_header(session, evlist, fd, true, fc);
3767 static int perf_header__getbuffer64(struct perf_header *header,
3768 int fd, void *buf, size_t size)
3770 if (readn(fd, buf, size) <= 0)
3773 if (header->needs_swap)
3774 mem_bswap_64(buf, size);
3779 int perf_header__process_sections(struct perf_header *header, int fd,
3781 int (*process)(struct perf_file_section *section,
3782 struct perf_header *ph,
3783 int feat, int fd, void *data))
3785 struct perf_file_section *feat_sec, *sec;
3791 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3795 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3799 sec_size = sizeof(*feat_sec) * nr_sections;
3801 lseek(fd, header->feat_offset, SEEK_SET);
3803 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3807 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3808 err = process(sec++, header, feat, fd, data);
3818 static const int attr_file_abi_sizes[] = {
3819 [0] = PERF_ATTR_SIZE_VER0,
3820 [1] = PERF_ATTR_SIZE_VER1,
3821 [2] = PERF_ATTR_SIZE_VER2,
3822 [3] = PERF_ATTR_SIZE_VER3,
3823 [4] = PERF_ATTR_SIZE_VER4,
3828 * In the legacy file format, the magic number is not used to encode endianness.
3829 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3830 * on ABI revisions, we need to try all combinations for all endianness to
3831 * detect the endianness.
3833 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3835 uint64_t ref_size, attr_size;
3838 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3839 ref_size = attr_file_abi_sizes[i]
3840 + sizeof(struct perf_file_section);
3841 if (hdr_sz != ref_size) {
3842 attr_size = bswap_64(hdr_sz);
3843 if (attr_size != ref_size)
3846 ph->needs_swap = true;
3848 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3853 /* could not determine endianness */
3857 #define PERF_PIPE_HDR_VER0 16
3859 static const size_t attr_pipe_abi_sizes[] = {
3860 [0] = PERF_PIPE_HDR_VER0,
3865 * In the legacy pipe format, there is an implicit assumption that endianness
3866 * between host recording the samples, and host parsing the samples is the
3867 * same. This is not always the case given that the pipe output may always be
3868 * redirected into a file and analyzed on a different machine with possibly a
3869 * different endianness and perf_event ABI revisions in the perf tool itself.
3871 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3876 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3877 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3878 attr_size = bswap_64(hdr_sz);
3879 if (attr_size != hdr_sz)
3882 ph->needs_swap = true;
3884 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3890 bool is_perf_magic(u64 magic)
3892 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3893 || magic == __perf_magic2
3894 || magic == __perf_magic2_sw)
3900 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3901 bool is_pipe, struct perf_header *ph)
3905 /* check for legacy format */
3906 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3908 ph->version = PERF_HEADER_VERSION_1;
3909 pr_debug("legacy perf.data format\n");
3911 return try_all_pipe_abis(hdr_sz, ph);
3913 return try_all_file_abis(hdr_sz, ph);
3916 * the new magic number serves two purposes:
3917 * - unique number to identify actual perf.data files
3918 * - encode endianness of file
3920 ph->version = PERF_HEADER_VERSION_2;
3922 /* check magic number with one endianness */
3923 if (magic == __perf_magic2)
3926 /* check magic number with opposite endianness */
3927 if (magic != __perf_magic2_sw)
3930 ph->needs_swap = true;
3935 int perf_file_header__read(struct perf_file_header *header,
3936 struct perf_header *ph, int fd)
3940 lseek(fd, 0, SEEK_SET);
3942 ret = readn(fd, header, sizeof(*header));
3946 if (check_magic_endian(header->magic,
3947 header->attr_size, false, ph) < 0) {
3948 pr_debug("magic/endian check failed\n");
3952 if (ph->needs_swap) {
3953 mem_bswap_64(header, offsetof(struct perf_file_header,
3957 if (header->size != sizeof(*header)) {
3958 /* Support the previous format */
3959 if (header->size == offsetof(typeof(*header), adds_features))
3960 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3963 } else if (ph->needs_swap) {
3965 * feature bitmap is declared as an array of unsigned longs --
3966 * not good since its size can differ between the host that
3967 * generated the data file and the host analyzing the file.
3969 * We need to handle endianness, but we don't know the size of
3970 * the unsigned long where the file was generated. Take a best
3971 * guess at determining it: try 64-bit swap first (ie., file
3972 * created on a 64-bit host), and check if the hostname feature
3973 * bit is set (this feature bit is forced on as of fbe96f2).
3974 * If the bit is not, undo the 64-bit swap and try a 32-bit
3975 * swap. If the hostname bit is still not set (e.g., older data
3976 * file), punt and fallback to the original behavior --
3977 * clearing all feature bits and setting buildid.
3979 mem_bswap_64(&header->adds_features,
3980 BITS_TO_U64(HEADER_FEAT_BITS));
3982 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3984 mem_bswap_64(&header->adds_features,
3985 BITS_TO_U64(HEADER_FEAT_BITS));
3988 mem_bswap_32(&header->adds_features,
3989 BITS_TO_U32(HEADER_FEAT_BITS));
3992 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3993 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3994 __set_bit(HEADER_BUILD_ID, header->adds_features);
3998 memcpy(&ph->adds_features, &header->adds_features,
3999 sizeof(ph->adds_features));
4001 ph->data_offset = header->data.offset;
4002 ph->data_size = header->data.size;
4003 ph->feat_offset = header->data.offset + header->data.size;
4007 static int perf_file_section__process(struct perf_file_section *section,
4008 struct perf_header *ph,
4009 int feat, int fd, void *data)
4011 struct feat_fd fdd = {
4014 .size = section->size,
4015 .offset = section->offset,
4018 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
4019 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
4020 "%d, continuing...\n", section->offset, feat);
4024 if (feat >= HEADER_LAST_FEATURE) {
4025 pr_debug("unknown feature %d, continuing...\n", feat);
4029 if (!feat_ops[feat].process)
4032 return feat_ops[feat].process(&fdd, data);
4035 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
4036 struct perf_header *ph,
4037 struct perf_data* data,
4038 bool repipe, int repipe_fd)
4040 struct feat_fd ff = {
4046 ret = perf_data__read(data, header, sizeof(*header));
4050 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
4051 pr_debug("endian/magic failed\n");
4056 header->size = bswap_64(header->size);
4058 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
4064 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd)
4066 struct perf_header *header = &session->header;
4067 struct perf_pipe_file_header f_header;
4069 if (perf_file_header__read_pipe(&f_header, header, session->data,
4070 session->repipe, repipe_fd) < 0) {
4071 pr_debug("incompatible file format\n");
4075 return f_header.size == sizeof(f_header) ? 0 : -1;
4078 static int read_attr(int fd, struct perf_header *ph,
4079 struct perf_file_attr *f_attr)
4081 struct perf_event_attr *attr = &f_attr->attr;
4083 size_t our_sz = sizeof(f_attr->attr);
4086 memset(f_attr, 0, sizeof(*f_attr));
4088 /* read minimal guaranteed structure */
4089 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
4091 pr_debug("cannot read %d bytes of header attr\n",
4092 PERF_ATTR_SIZE_VER0);
4096 /* on file perf_event_attr size */
4104 sz = PERF_ATTR_SIZE_VER0;
4105 } else if (sz > our_sz) {
4106 pr_debug("file uses a more recent and unsupported ABI"
4107 " (%zu bytes extra)\n", sz - our_sz);
4110 /* what we have not yet read and that we know about */
4111 left = sz - PERF_ATTR_SIZE_VER0;
4114 ptr += PERF_ATTR_SIZE_VER0;
4116 ret = readn(fd, ptr, left);
4118 /* read perf_file_section, ids are read in caller */
4119 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
4121 return ret <= 0 ? -1 : 0;
4124 #ifdef HAVE_LIBTRACEEVENT
4125 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
4127 struct tep_event *event;
4130 /* already prepared */
4131 if (evsel->tp_format)
4134 if (pevent == NULL) {
4135 pr_debug("broken or missing trace data\n");
4139 event = tep_find_event(pevent, evsel->core.attr.config);
4140 if (event == NULL) {
4141 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
4146 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
4147 evsel->name = strdup(bf);
4148 if (evsel->name == NULL)
4152 evsel->tp_format = event;
4156 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
4160 evlist__for_each_entry(evlist, pos) {
4161 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
4162 evsel__prepare_tracepoint_event(pos, pevent))
4170 int perf_session__read_header(struct perf_session *session, int repipe_fd)
4172 struct perf_data *data = session->data;
4173 struct perf_header *header = &session->header;
4174 struct perf_file_header f_header;
4175 struct perf_file_attr f_attr;
4177 int nr_attrs, nr_ids, i, j, err;
4178 int fd = perf_data__fd(data);
4180 session->evlist = evlist__new();
4181 if (session->evlist == NULL)
4184 session->evlist->env = &header->env;
4185 session->machines.host.env = &header->env;
4188 * We can read 'pipe' data event from regular file,
4189 * check for the pipe header regardless of source.
4191 err = perf_header__read_pipe(session, repipe_fd);
4192 if (!err || perf_data__is_pipe(data)) {
4193 data->is_pipe = true;
4197 if (perf_file_header__read(&f_header, header, fd) < 0)
4200 if (header->needs_swap && data->in_place_update) {
4201 pr_err("In-place update not supported when byte-swapping is required\n");
4206 * Sanity check that perf.data was written cleanly; data size is
4207 * initialized to 0 and updated only if the on_exit function is run.
4208 * If data size is still 0 then the file contains only partial
4209 * information. Just warn user and process it as much as it can.
4211 if (f_header.data.size == 0) {
4212 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
4213 "Was the 'perf record' command properly terminated?\n",
4217 if (f_header.attr_size == 0) {
4218 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
4219 "Was the 'perf record' command properly terminated?\n",
4224 nr_attrs = f_header.attrs.size / f_header.attr_size;
4225 lseek(fd, f_header.attrs.offset, SEEK_SET);
4227 for (i = 0; i < nr_attrs; i++) {
4228 struct evsel *evsel;
4231 if (read_attr(fd, header, &f_attr) < 0)
4234 if (header->needs_swap) {
4235 f_attr.ids.size = bswap_64(f_attr.ids.size);
4236 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
4237 perf_event__attr_swap(&f_attr.attr);
4240 tmp = lseek(fd, 0, SEEK_CUR);
4241 evsel = evsel__new(&f_attr.attr);
4244 goto out_delete_evlist;
4246 evsel->needs_swap = header->needs_swap;
4248 * Do it before so that if perf_evsel__alloc_id fails, this
4249 * entry gets purged too at evlist__delete().
4251 evlist__add(session->evlist, evsel);
4253 nr_ids = f_attr.ids.size / sizeof(u64);
4255 * We don't have the cpu and thread maps on the header, so
4256 * for allocating the perf_sample_id table we fake 1 cpu and
4257 * hattr->ids threads.
4259 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
4260 goto out_delete_evlist;
4262 lseek(fd, f_attr.ids.offset, SEEK_SET);
4264 for (j = 0; j < nr_ids; j++) {
4265 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
4268 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
4271 lseek(fd, tmp, SEEK_SET);
4274 #ifdef HAVE_LIBTRACEEVENT
4275 perf_header__process_sections(header, fd, &session->tevent,
4276 perf_file_section__process);
4278 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
4279 goto out_delete_evlist;
4281 perf_header__process_sections(header, fd, NULL, perf_file_section__process);
4289 evlist__delete(session->evlist);
4290 session->evlist = NULL;
4294 int perf_event__process_feature(struct perf_session *session,
4295 union perf_event *event)
4297 struct perf_tool *tool = session->tool;
4298 struct feat_fd ff = { .fd = 0 };
4299 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
4300 int type = fe->header.type;
4301 u64 feat = fe->feat_id;
4304 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
4305 pr_warning("invalid record type %d in pipe-mode\n", type);
4308 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
4309 pr_warning("invalid record type %d in pipe-mode\n", type);
4313 if (!feat_ops[feat].process)
4316 ff.buf = (void *)fe->data;
4317 ff.size = event->header.size - sizeof(*fe);
4318 ff.ph = &session->header;
4320 if (feat_ops[feat].process(&ff, NULL)) {
4325 if (!feat_ops[feat].print || !tool->show_feat_hdr)
4328 if (!feat_ops[feat].full_only ||
4329 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
4330 feat_ops[feat].print(&ff, stdout);
4332 fprintf(stdout, "# %s info available, use -I to display\n",
4333 feat_ops[feat].name);
4336 free_event_desc(ff.events);
4340 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
4342 struct perf_record_event_update *ev = &event->event_update;
4343 struct perf_cpu_map *map;
4346 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
4349 case PERF_EVENT_UPDATE__SCALE:
4350 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale);
4352 case PERF_EVENT_UPDATE__UNIT:
4353 ret += fprintf(fp, "... unit: %s\n", ev->unit);
4355 case PERF_EVENT_UPDATE__NAME:
4356 ret += fprintf(fp, "... name: %s\n", ev->name);
4358 case PERF_EVENT_UPDATE__CPUS:
4359 ret += fprintf(fp, "... ");
4361 map = cpu_map__new_data(&ev->cpus.cpus);
4363 ret += cpu_map__fprintf(map, fp);
4365 ret += fprintf(fp, "failed to get cpus\n");
4368 ret += fprintf(fp, "... unknown type\n");
4375 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4376 union perf_event *event,
4377 struct evlist **pevlist)
4380 struct evsel *evsel;
4381 struct evlist *evlist = *pevlist;
4383 if (evlist == NULL) {
4384 *pevlist = evlist = evlist__new();
4389 evsel = evsel__new(&event->attr.attr);
4393 evlist__add(evlist, evsel);
4395 ids = event->header.size;
4396 ids -= (void *)&event->attr.id - (void *)event;
4397 n_ids = ids / sizeof(u64);
4399 * We don't have the cpu and thread maps on the header, so
4400 * for allocating the perf_sample_id table we fake 1 cpu and
4401 * hattr->ids threads.
4403 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
4406 for (i = 0; i < n_ids; i++) {
4407 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
4413 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4414 union perf_event *event,
4415 struct evlist **pevlist)
4417 struct perf_record_event_update *ev = &event->event_update;
4418 struct evlist *evlist;
4419 struct evsel *evsel;
4420 struct perf_cpu_map *map;
4423 perf_event__fprintf_event_update(event, stdout);
4425 if (!pevlist || *pevlist == NULL)
4430 evsel = evlist__id2evsel(evlist, ev->id);
4435 case PERF_EVENT_UPDATE__UNIT:
4436 free((char *)evsel->unit);
4437 evsel->unit = strdup(ev->unit);
4439 case PERF_EVENT_UPDATE__NAME:
4441 evsel->name = strdup(ev->name);
4443 case PERF_EVENT_UPDATE__SCALE:
4444 evsel->scale = ev->scale.scale;
4446 case PERF_EVENT_UPDATE__CPUS:
4447 map = cpu_map__new_data(&ev->cpus.cpus);
4449 perf_cpu_map__put(evsel->core.own_cpus);
4450 evsel->core.own_cpus = map;
4452 pr_err("failed to get event_update cpus\n");
4460 #ifdef HAVE_LIBTRACEEVENT
4461 int perf_event__process_tracing_data(struct perf_session *session,
4462 union perf_event *event)
4464 ssize_t size_read, padding, size = event->tracing_data.size;
4465 int fd = perf_data__fd(session->data);
4469 * The pipe fd is already in proper place and in any case
4470 * we can't move it, and we'd screw the case where we read
4471 * 'pipe' data from regular file. The trace_report reads
4472 * data from 'fd' so we need to set it directly behind the
4473 * event, where the tracing data starts.
4475 if (!perf_data__is_pipe(session->data)) {
4476 off_t offset = lseek(fd, 0, SEEK_CUR);
4478 /* setup for reading amidst mmap */
4479 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4483 size_read = trace_report(fd, &session->tevent,
4485 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4487 if (readn(fd, buf, padding) < 0) {
4488 pr_err("%s: reading input file", __func__);
4491 if (session->repipe) {
4492 int retw = write(STDOUT_FILENO, buf, padding);
4493 if (retw <= 0 || retw != padding) {
4494 pr_err("%s: repiping tracing data padding", __func__);
4499 if (size_read + padding != size) {
4500 pr_err("%s: tracing data size mismatch", __func__);
4504 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
4506 return size_read + padding;
4510 int perf_event__process_build_id(struct perf_session *session,
4511 union perf_event *event)
4513 __event_process_build_id(&event->build_id,
4514 event->build_id.filename,