1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/compiler.h>
12 #include <linux/list.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/string.h>
16 #include <linux/stringify.h>
17 #include <linux/zalloc.h>
19 #include <sys/utsname.h>
20 #include <linux/time64.h>
22 #include <bpf/libbpf.h>
23 #include <perf/cpumap.h>
28 #include "util/evsel_fprintf.h"
31 #include "trace-event.h"
41 #include <api/fs/fs.h>
44 #include "time-utils.h"
46 #include "util/util.h" // perf_exe()
48 #include "bpf-event.h"
50 #include <linux/ctype.h>
51 #include <internal/lib.h>
55 * must be a numerical value to let the endianness
56 * determine the memory layout. That way we are able
57 * to detect endianness when reading the perf.data file
60 * we check for legacy (PERFFILE) format.
62 static const char *__perf_magic1 = "PERFFILE";
63 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
64 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
66 #define PERF_MAGIC __perf_magic2
68 const char perf_version_string[] = PERF_VERSION;
70 struct perf_file_attr {
71 struct perf_event_attr attr;
72 struct perf_file_section ids;
75 void perf_header__set_feat(struct perf_header *header, int feat)
77 set_bit(feat, header->adds_features);
80 void perf_header__clear_feat(struct perf_header *header, int feat)
82 clear_bit(feat, header->adds_features);
85 bool perf_header__has_feat(const struct perf_header *header, int feat)
87 return test_bit(feat, header->adds_features);
90 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
92 ssize_t ret = writen(ff->fd, buf, size);
94 if (ret != (ssize_t)size)
95 return ret < 0 ? (int)ret : -1;
99 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
101 /* struct perf_event_header::size is u16 */
102 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
103 size_t new_size = ff->size;
106 if (size + ff->offset > max_size)
109 while (size > (new_size - ff->offset))
111 new_size = min(max_size, new_size);
113 if (ff->size < new_size) {
114 addr = realloc(ff->buf, new_size);
121 memcpy(ff->buf + ff->offset, buf, size);
127 /* Return: 0 if succeded, -ERR if failed. */
128 int do_write(struct feat_fd *ff, const void *buf, size_t size)
131 return __do_write_fd(ff, buf, size);
132 return __do_write_buf(ff, buf, size);
135 /* Return: 0 if succeded, -ERR if failed. */
136 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
138 u64 *p = (u64 *) set;
141 ret = do_write(ff, &size, sizeof(size));
145 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
146 ret = do_write(ff, p + i, sizeof(*p));
154 /* Return: 0 if succeded, -ERR if failed. */
155 int write_padded(struct feat_fd *ff, const void *bf,
156 size_t count, size_t count_aligned)
158 static const char zero_buf[NAME_ALIGN];
159 int err = do_write(ff, bf, count);
162 err = do_write(ff, zero_buf, count_aligned - count);
167 #define string_size(str) \
168 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
170 /* Return: 0 if succeded, -ERR if failed. */
171 static int do_write_string(struct feat_fd *ff, const char *str)
176 olen = strlen(str) + 1;
177 len = PERF_ALIGN(olen, NAME_ALIGN);
179 /* write len, incl. \0 */
180 ret = do_write(ff, &len, sizeof(len));
184 return write_padded(ff, str, olen, len);
187 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
189 ssize_t ret = readn(ff->fd, addr, size);
192 return ret < 0 ? (int)ret : -1;
196 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
198 if (size > (ssize_t)ff->size - ff->offset)
201 memcpy(addr, ff->buf + ff->offset, size);
208 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
211 return __do_read_fd(ff, addr, size);
212 return __do_read_buf(ff, addr, size);
215 static int do_read_u32(struct feat_fd *ff, u32 *addr)
219 ret = __do_read(ff, addr, sizeof(*addr));
223 if (ff->ph->needs_swap)
224 *addr = bswap_32(*addr);
228 static int do_read_u64(struct feat_fd *ff, u64 *addr)
232 ret = __do_read(ff, addr, sizeof(*addr));
236 if (ff->ph->needs_swap)
237 *addr = bswap_64(*addr);
241 static char *do_read_string(struct feat_fd *ff)
246 if (do_read_u32(ff, &len))
253 if (!__do_read(ff, buf, len)) {
255 * strings are padded by zeroes
256 * thus the actual strlen of buf
257 * may be less than len
266 /* Return: 0 if succeded, -ERR if failed. */
267 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
273 ret = do_read_u64(ff, &size);
277 set = bitmap_alloc(size);
283 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
284 ret = do_read_u64(ff, p + i);
296 static int write_tracing_data(struct feat_fd *ff,
297 struct evlist *evlist)
299 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
302 return read_tracing_data(ff->fd, &evlist->core.entries);
305 static int write_build_id(struct feat_fd *ff,
306 struct evlist *evlist __maybe_unused)
308 struct perf_session *session;
311 session = container_of(ff->ph, struct perf_session, header);
313 if (!perf_session__read_build_ids(session, true))
316 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
319 err = perf_session__write_buildid_table(session, ff);
321 pr_debug("failed to write buildid table\n");
324 perf_session__cache_build_ids(session);
329 static int write_hostname(struct feat_fd *ff,
330 struct evlist *evlist __maybe_unused)
339 return do_write_string(ff, uts.nodename);
342 static int write_osrelease(struct feat_fd *ff,
343 struct evlist *evlist __maybe_unused)
352 return do_write_string(ff, uts.release);
355 static int write_arch(struct feat_fd *ff,
356 struct evlist *evlist __maybe_unused)
365 return do_write_string(ff, uts.machine);
368 static int write_version(struct feat_fd *ff,
369 struct evlist *evlist __maybe_unused)
371 return do_write_string(ff, perf_version_string);
374 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
379 const char *search = cpuinfo_proc;
386 file = fopen("/proc/cpuinfo", "r");
390 while (getline(&buf, &len, file) > 0) {
391 ret = strncmp(buf, search, strlen(search));
403 p = strchr(buf, ':');
404 if (p && *(p+1) == ' ' && *(p+2))
410 /* squash extra space characters (branding string) */
415 char *q = skip_spaces(r);
418 while ((*r++ = *q++));
422 ret = do_write_string(ff, s);
429 static int write_cpudesc(struct feat_fd *ff,
430 struct evlist *evlist __maybe_unused)
432 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
433 #define CPUINFO_PROC { "cpu", }
434 #elif defined(__s390__)
435 #define CPUINFO_PROC { "vendor_id", }
436 #elif defined(__sh__)
437 #define CPUINFO_PROC { "cpu type", }
438 #elif defined(__alpha__) || defined(__mips__)
439 #define CPUINFO_PROC { "cpu model", }
440 #elif defined(__arm__)
441 #define CPUINFO_PROC { "model name", "Processor", }
442 #elif defined(__arc__)
443 #define CPUINFO_PROC { "Processor", }
444 #elif defined(__xtensa__)
445 #define CPUINFO_PROC { "core ID", }
447 #define CPUINFO_PROC { "model name", }
449 const char *cpuinfo_procs[] = CPUINFO_PROC;
453 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
455 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
463 static int write_nrcpus(struct feat_fd *ff,
464 struct evlist *evlist __maybe_unused)
470 nrc = cpu__max_present_cpu();
472 nr = sysconf(_SC_NPROCESSORS_ONLN);
476 nra = (u32)(nr & UINT_MAX);
478 ret = do_write(ff, &nrc, sizeof(nrc));
482 return do_write(ff, &nra, sizeof(nra));
485 static int write_event_desc(struct feat_fd *ff,
486 struct evlist *evlist)
492 nre = evlist->core.nr_entries;
495 * write number of events
497 ret = do_write(ff, &nre, sizeof(nre));
502 * size of perf_event_attr struct
504 sz = (u32)sizeof(evsel->core.attr);
505 ret = do_write(ff, &sz, sizeof(sz));
509 evlist__for_each_entry(evlist, evsel) {
510 ret = do_write(ff, &evsel->core.attr, sz);
514 * write number of unique id per event
515 * there is one id per instance of an event
517 * copy into an nri to be independent of the
520 nri = evsel->core.ids;
521 ret = do_write(ff, &nri, sizeof(nri));
526 * write event string as passed on cmdline
528 ret = do_write_string(ff, perf_evsel__name(evsel));
532 * write unique ids for this event
534 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
541 static int write_cmdline(struct feat_fd *ff,
542 struct evlist *evlist __maybe_unused)
544 char pbuf[MAXPATHLEN], *buf;
547 /* actual path to perf binary */
548 buf = perf_exe(pbuf, MAXPATHLEN);
550 /* account for binary path */
551 n = perf_env.nr_cmdline + 1;
553 ret = do_write(ff, &n, sizeof(n));
557 ret = do_write_string(ff, buf);
561 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
562 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
570 static int write_cpu_topology(struct feat_fd *ff,
571 struct evlist *evlist __maybe_unused)
573 struct cpu_topology *tp;
577 tp = cpu_topology__new();
581 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
585 for (i = 0; i < tp->core_sib; i++) {
586 ret = do_write_string(ff, tp->core_siblings[i]);
590 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
594 for (i = 0; i < tp->thread_sib; i++) {
595 ret = do_write_string(ff, tp->thread_siblings[i]);
600 ret = perf_env__read_cpu_topology_map(&perf_env);
604 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
605 ret = do_write(ff, &perf_env.cpu[j].core_id,
606 sizeof(perf_env.cpu[j].core_id));
609 ret = do_write(ff, &perf_env.cpu[j].socket_id,
610 sizeof(perf_env.cpu[j].socket_id));
618 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib));
622 for (i = 0; i < tp->die_sib; i++) {
623 ret = do_write_string(ff, tp->die_siblings[i]);
628 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
629 ret = do_write(ff, &perf_env.cpu[j].die_id,
630 sizeof(perf_env.cpu[j].die_id));
636 cpu_topology__delete(tp);
642 static int write_total_mem(struct feat_fd *ff,
643 struct evlist *evlist __maybe_unused)
651 fp = fopen("/proc/meminfo", "r");
655 while (getline(&buf, &len, fp) > 0) {
656 ret = strncmp(buf, "MemTotal:", 9);
661 n = sscanf(buf, "%*s %"PRIu64, &mem);
663 ret = do_write(ff, &mem, sizeof(mem));
671 static int write_numa_topology(struct feat_fd *ff,
672 struct evlist *evlist __maybe_unused)
674 struct numa_topology *tp;
678 tp = numa_topology__new();
682 ret = do_write(ff, &tp->nr, sizeof(u32));
686 for (i = 0; i < tp->nr; i++) {
687 struct numa_topology_node *n = &tp->nodes[i];
689 ret = do_write(ff, &n->node, sizeof(u32));
693 ret = do_write(ff, &n->mem_total, sizeof(u64));
697 ret = do_write(ff, &n->mem_free, sizeof(u64));
701 ret = do_write_string(ff, n->cpus);
709 numa_topology__delete(tp);
716 * struct pmu_mappings {
725 static int write_pmu_mappings(struct feat_fd *ff,
726 struct evlist *evlist __maybe_unused)
728 struct perf_pmu *pmu = NULL;
733 * Do a first pass to count number of pmu to avoid lseek so this
734 * works in pipe mode as well.
736 while ((pmu = perf_pmu__scan(pmu))) {
742 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
746 while ((pmu = perf_pmu__scan(pmu))) {
750 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
754 ret = do_write_string(ff, pmu->name);
765 * struct group_descs {
767 * struct group_desc {
774 static int write_group_desc(struct feat_fd *ff,
775 struct evlist *evlist)
777 u32 nr_groups = evlist->nr_groups;
781 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
785 evlist__for_each_entry(evlist, evsel) {
786 if (perf_evsel__is_group_leader(evsel) &&
787 evsel->core.nr_members > 1) {
788 const char *name = evsel->group_name ?: "{anon_group}";
789 u32 leader_idx = evsel->idx;
790 u32 nr_members = evsel->core.nr_members;
792 ret = do_write_string(ff, name);
796 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
800 ret = do_write(ff, &nr_members, sizeof(nr_members));
809 * Return the CPU id as a raw string.
811 * Each architecture should provide a more precise id string that
812 * can be use to match the architecture's "mapfile".
814 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
819 /* Return zero when the cpuid from the mapfile.csv matches the
820 * cpuid string generated on this platform.
821 * Otherwise return non-zero.
823 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
826 regmatch_t pmatch[1];
829 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
830 /* Warn unable to generate match particular string. */
831 pr_info("Invalid regular expression %s\n", mapcpuid);
835 match = !regexec(&re, cpuid, 1, pmatch, 0);
838 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
840 /* Verify the entire string matched. */
841 if (match_len == strlen(cpuid))
848 * default get_cpuid(): nothing gets recorded
849 * actual implementation must be in arch/$(SRCARCH)/util/header.c
851 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
856 static int write_cpuid(struct feat_fd *ff,
857 struct evlist *evlist __maybe_unused)
862 ret = get_cpuid(buffer, sizeof(buffer));
866 return do_write_string(ff, buffer);
869 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
870 struct evlist *evlist __maybe_unused)
875 static int write_auxtrace(struct feat_fd *ff,
876 struct evlist *evlist __maybe_unused)
878 struct perf_session *session;
881 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
884 session = container_of(ff->ph, struct perf_session, header);
886 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
888 pr_err("Failed to write auxtrace index\n");
892 static int write_clockid(struct feat_fd *ff,
893 struct evlist *evlist __maybe_unused)
895 return do_write(ff, &ff->ph->env.clockid_res_ns,
896 sizeof(ff->ph->env.clockid_res_ns));
899 static int write_dir_format(struct feat_fd *ff,
900 struct evlist *evlist __maybe_unused)
902 struct perf_session *session;
903 struct perf_data *data;
905 session = container_of(ff->ph, struct perf_session, header);
906 data = session->data;
908 if (WARN_ON(!perf_data__is_dir(data)))
911 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
914 #ifdef HAVE_LIBBPF_SUPPORT
915 static int write_bpf_prog_info(struct feat_fd *ff,
916 struct evlist *evlist __maybe_unused)
918 struct perf_env *env = &ff->ph->env;
919 struct rb_root *root;
920 struct rb_node *next;
923 down_read(&env->bpf_progs.lock);
925 ret = do_write(ff, &env->bpf_progs.infos_cnt,
926 sizeof(env->bpf_progs.infos_cnt));
930 root = &env->bpf_progs.infos;
931 next = rb_first(root);
933 struct bpf_prog_info_node *node;
936 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
937 next = rb_next(&node->rb_node);
938 len = sizeof(struct bpf_prog_info_linear) +
939 node->info_linear->data_len;
941 /* before writing to file, translate address to offset */
942 bpf_program__bpil_addr_to_offs(node->info_linear);
943 ret = do_write(ff, node->info_linear, len);
945 * translate back to address even when do_write() fails,
946 * so that this function never changes the data.
948 bpf_program__bpil_offs_to_addr(node->info_linear);
953 up_read(&env->bpf_progs.lock);
956 #else // HAVE_LIBBPF_SUPPORT
957 static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
958 struct evlist *evlist __maybe_unused)
962 #endif // HAVE_LIBBPF_SUPPORT
964 static int write_bpf_btf(struct feat_fd *ff,
965 struct evlist *evlist __maybe_unused)
967 struct perf_env *env = &ff->ph->env;
968 struct rb_root *root;
969 struct rb_node *next;
972 down_read(&env->bpf_progs.lock);
974 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
975 sizeof(env->bpf_progs.btfs_cnt));
980 root = &env->bpf_progs.btfs;
981 next = rb_first(root);
983 struct btf_node *node;
985 node = rb_entry(next, struct btf_node, rb_node);
986 next = rb_next(&node->rb_node);
987 ret = do_write(ff, &node->id,
988 sizeof(u32) * 2 + node->data_size);
993 up_read(&env->bpf_progs.lock);
997 static int cpu_cache_level__sort(const void *a, const void *b)
999 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1000 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1002 return cache_a->level - cache_b->level;
1005 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1007 if (a->level != b->level)
1010 if (a->line_size != b->line_size)
1013 if (a->sets != b->sets)
1016 if (a->ways != b->ways)
1019 if (strcmp(a->type, b->type))
1022 if (strcmp(a->size, b->size))
1025 if (strcmp(a->map, b->map))
1031 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1033 char path[PATH_MAX], file[PATH_MAX];
1037 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1038 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1040 if (stat(file, &st))
1043 scnprintf(file, PATH_MAX, "%s/level", path);
1044 if (sysfs__read_int(file, (int *) &cache->level))
1047 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1048 if (sysfs__read_int(file, (int *) &cache->line_size))
1051 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1052 if (sysfs__read_int(file, (int *) &cache->sets))
1055 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1056 if (sysfs__read_int(file, (int *) &cache->ways))
1059 scnprintf(file, PATH_MAX, "%s/type", path);
1060 if (sysfs__read_str(file, &cache->type, &len))
1063 cache->type[len] = 0;
1064 cache->type = strim(cache->type);
1066 scnprintf(file, PATH_MAX, "%s/size", path);
1067 if (sysfs__read_str(file, &cache->size, &len)) {
1068 zfree(&cache->type);
1072 cache->size[len] = 0;
1073 cache->size = strim(cache->size);
1075 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1076 if (sysfs__read_str(file, &cache->map, &len)) {
1077 zfree(&cache->size);
1078 zfree(&cache->type);
1082 cache->map[len] = 0;
1083 cache->map = strim(cache->map);
1087 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1089 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1092 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1099 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1103 nr = (u32)(ncpus & UINT_MAX);
1105 for (cpu = 0; cpu < nr; cpu++) {
1106 for (level = 0; level < 10; level++) {
1107 struct cpu_cache_level c;
1110 err = cpu_cache_level__read(&c, cpu, level);
1117 for (i = 0; i < cnt; i++) {
1118 if (cpu_cache_level__cmp(&c, &caches[i]))
1125 cpu_cache_level__free(&c);
1127 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1136 #define MAX_CACHE_LVL 4
1138 static int write_cache(struct feat_fd *ff,
1139 struct evlist *evlist __maybe_unused)
1141 u32 max_caches = cpu__max_cpu() * MAX_CACHE_LVL;
1142 struct cpu_cache_level caches[max_caches];
1143 u32 cnt = 0, i, version = 1;
1146 ret = build_caches(caches, max_caches, &cnt);
1150 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1152 ret = do_write(ff, &version, sizeof(u32));
1156 ret = do_write(ff, &cnt, sizeof(u32));
1160 for (i = 0; i < cnt; i++) {
1161 struct cpu_cache_level *c = &caches[i];
1164 ret = do_write(ff, &c->v, sizeof(u32)); \
1175 ret = do_write_string(ff, (const char *) c->v); \
1186 for (i = 0; i < cnt; i++)
1187 cpu_cache_level__free(&caches[i]);
1191 static int write_stat(struct feat_fd *ff __maybe_unused,
1192 struct evlist *evlist __maybe_unused)
1197 static int write_sample_time(struct feat_fd *ff,
1198 struct evlist *evlist)
1202 ret = do_write(ff, &evlist->first_sample_time,
1203 sizeof(evlist->first_sample_time));
1207 return do_write(ff, &evlist->last_sample_time,
1208 sizeof(evlist->last_sample_time));
1212 static int memory_node__read(struct memory_node *n, unsigned long idx)
1214 unsigned int phys, size = 0;
1215 char path[PATH_MAX];
1219 #define for_each_memory(mem, dir) \
1220 while ((ent = readdir(dir))) \
1221 if (strcmp(ent->d_name, ".") && \
1222 strcmp(ent->d_name, "..") && \
1223 sscanf(ent->d_name, "memory%u", &mem) == 1)
1225 scnprintf(path, PATH_MAX,
1226 "%s/devices/system/node/node%lu",
1227 sysfs__mountpoint(), idx);
1229 dir = opendir(path);
1231 pr_warning("failed: cant' open memory sysfs data\n");
1235 for_each_memory(phys, dir) {
1236 size = max(phys, size);
1241 n->set = bitmap_alloc(size);
1252 for_each_memory(phys, dir) {
1253 set_bit(phys, n->set);
1260 static int memory_node__sort(const void *a, const void *b)
1262 const struct memory_node *na = a;
1263 const struct memory_node *nb = b;
1265 return na->node - nb->node;
1268 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1270 char path[PATH_MAX];
1276 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1277 sysfs__mountpoint());
1279 dir = opendir(path);
1281 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1286 while (!ret && (ent = readdir(dir))) {
1290 if (!strcmp(ent->d_name, ".") ||
1291 !strcmp(ent->d_name, ".."))
1294 r = sscanf(ent->d_name, "node%u", &idx);
1298 if (WARN_ONCE(cnt >= size,
1299 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1302 ret = memory_node__read(&nodes[cnt++], idx);
1309 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1314 #define MAX_MEMORY_NODES 2000
1317 * The MEM_TOPOLOGY holds physical memory map for every
1318 * node in system. The format of data is as follows:
1320 * 0 - version | for future changes
1321 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1322 * 16 - count | number of nodes
1324 * For each node we store map of physical indexes for
1327 * 32 - node id | node index
1328 * 40 - size | size of bitmap
1329 * 48 - bitmap | bitmap of memory indexes that belongs to node
1331 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1332 struct evlist *evlist __maybe_unused)
1334 static struct memory_node nodes[MAX_MEMORY_NODES];
1335 u64 bsize, version = 1, i, nr;
1338 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1339 (unsigned long long *) &bsize);
1343 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1347 ret = do_write(ff, &version, sizeof(version));
1351 ret = do_write(ff, &bsize, sizeof(bsize));
1355 ret = do_write(ff, &nr, sizeof(nr));
1359 for (i = 0; i < nr; i++) {
1360 struct memory_node *n = &nodes[i];
1363 ret = do_write(ff, &n->v, sizeof(n->v)); \
1372 ret = do_write_bitmap(ff, n->set, n->size);
1381 static int write_compressed(struct feat_fd *ff __maybe_unused,
1382 struct evlist *evlist __maybe_unused)
1386 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1390 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1394 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1398 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1402 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1405 static void print_hostname(struct feat_fd *ff, FILE *fp)
1407 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1410 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1412 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1415 static void print_arch(struct feat_fd *ff, FILE *fp)
1417 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1420 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1422 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1425 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1427 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1428 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1431 static void print_version(struct feat_fd *ff, FILE *fp)
1433 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1436 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1440 nr = ff->ph->env.nr_cmdline;
1442 fprintf(fp, "# cmdline : ");
1444 for (i = 0; i < nr; i++) {
1445 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1447 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1451 char *quote = strchr(argv_i, '\'');
1455 fprintf(fp, "%s\\\'", argv_i);
1458 fprintf(fp, "%s ", argv_i);
1465 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1467 struct perf_header *ph = ff->ph;
1468 int cpu_nr = ph->env.nr_cpus_avail;
1472 nr = ph->env.nr_sibling_cores;
1473 str = ph->env.sibling_cores;
1475 for (i = 0; i < nr; i++) {
1476 fprintf(fp, "# sibling sockets : %s\n", str);
1477 str += strlen(str) + 1;
1480 if (ph->env.nr_sibling_dies) {
1481 nr = ph->env.nr_sibling_dies;
1482 str = ph->env.sibling_dies;
1484 for (i = 0; i < nr; i++) {
1485 fprintf(fp, "# sibling dies : %s\n", str);
1486 str += strlen(str) + 1;
1490 nr = ph->env.nr_sibling_threads;
1491 str = ph->env.sibling_threads;
1493 for (i = 0; i < nr; i++) {
1494 fprintf(fp, "# sibling threads : %s\n", str);
1495 str += strlen(str) + 1;
1498 if (ph->env.nr_sibling_dies) {
1499 if (ph->env.cpu != NULL) {
1500 for (i = 0; i < cpu_nr; i++)
1501 fprintf(fp, "# CPU %d: Core ID %d, "
1502 "Die ID %d, Socket ID %d\n",
1503 i, ph->env.cpu[i].core_id,
1504 ph->env.cpu[i].die_id,
1505 ph->env.cpu[i].socket_id);
1507 fprintf(fp, "# Core ID, Die ID and Socket ID "
1508 "information is not available\n");
1510 if (ph->env.cpu != NULL) {
1511 for (i = 0; i < cpu_nr; i++)
1512 fprintf(fp, "# CPU %d: Core ID %d, "
1514 i, ph->env.cpu[i].core_id,
1515 ph->env.cpu[i].socket_id);
1517 fprintf(fp, "# Core ID and Socket ID "
1518 "information is not available\n");
1522 static void print_clockid(struct feat_fd *ff, FILE *fp)
1524 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1525 ff->ph->env.clockid_res_ns * 1000);
1528 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1530 struct perf_session *session;
1531 struct perf_data *data;
1533 session = container_of(ff->ph, struct perf_session, header);
1534 data = session->data;
1536 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1539 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1541 struct perf_env *env = &ff->ph->env;
1542 struct rb_root *root;
1543 struct rb_node *next;
1545 down_read(&env->bpf_progs.lock);
1547 root = &env->bpf_progs.infos;
1548 next = rb_first(root);
1551 struct bpf_prog_info_node *node;
1553 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1554 next = rb_next(&node->rb_node);
1556 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1560 up_read(&env->bpf_progs.lock);
1563 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1565 struct perf_env *env = &ff->ph->env;
1566 struct rb_root *root;
1567 struct rb_node *next;
1569 down_read(&env->bpf_progs.lock);
1571 root = &env->bpf_progs.btfs;
1572 next = rb_first(root);
1575 struct btf_node *node;
1577 node = rb_entry(next, struct btf_node, rb_node);
1578 next = rb_next(&node->rb_node);
1579 fprintf(fp, "# btf info of id %u\n", node->id);
1582 up_read(&env->bpf_progs.lock);
1585 static void free_event_desc(struct evsel *events)
1587 struct evsel *evsel;
1592 for (evsel = events; evsel->core.attr.size; evsel++) {
1593 zfree(&evsel->name);
1594 zfree(&evsel->core.id);
1600 static struct evsel *read_event_desc(struct feat_fd *ff)
1602 struct evsel *evsel, *events = NULL;
1605 u32 nre, sz, nr, i, j;
1608 /* number of events */
1609 if (do_read_u32(ff, &nre))
1612 if (do_read_u32(ff, &sz))
1615 /* buffer to hold on file attr struct */
1620 /* the last event terminates with evsel->core.attr.size == 0: */
1621 events = calloc(nre + 1, sizeof(*events));
1625 msz = sizeof(evsel->core.attr);
1629 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1633 * must read entire on-file attr struct to
1634 * sync up with layout.
1636 if (__do_read(ff, buf, sz))
1639 if (ff->ph->needs_swap)
1640 perf_event__attr_swap(buf);
1642 memcpy(&evsel->core.attr, buf, msz);
1644 if (do_read_u32(ff, &nr))
1647 if (ff->ph->needs_swap)
1648 evsel->needs_swap = true;
1650 evsel->name = do_read_string(ff);
1657 id = calloc(nr, sizeof(*id));
1660 evsel->core.ids = nr;
1661 evsel->core.id = id;
1663 for (j = 0 ; j < nr; j++) {
1664 if (do_read_u64(ff, id))
1673 free_event_desc(events);
1678 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1679 void *priv __maybe_unused)
1681 return fprintf(fp, ", %s = %s", name, val);
1684 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1686 struct evsel *evsel, *events;
1691 events = ff->events;
1693 events = read_event_desc(ff);
1696 fprintf(fp, "# event desc: not available or unable to read\n");
1700 for (evsel = events; evsel->core.attr.size; evsel++) {
1701 fprintf(fp, "# event : name = %s, ", evsel->name);
1703 if (evsel->core.ids) {
1704 fprintf(fp, ", id = {");
1705 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
1708 fprintf(fp, " %"PRIu64, *id);
1713 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
1718 free_event_desc(events);
1722 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1724 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1727 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1730 struct numa_node *n;
1732 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1733 n = &ff->ph->env.numa_nodes[i];
1735 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1736 " free = %"PRIu64" kB\n",
1737 n->node, n->mem_total, n->mem_free);
1739 fprintf(fp, "# node%u cpu list : ", n->node);
1740 cpu_map__fprintf(n->map, fp);
1744 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1746 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1749 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1751 fprintf(fp, "# contains samples with branch stack\n");
1754 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1756 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1759 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1761 fprintf(fp, "# contains stat data\n");
1764 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1768 fprintf(fp, "# CPU cache info:\n");
1769 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1771 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1775 static void print_compressed(struct feat_fd *ff, FILE *fp)
1777 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1778 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1779 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1782 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1784 const char *delimiter = "# pmu mappings: ";
1789 pmu_num = ff->ph->env.nr_pmu_mappings;
1791 fprintf(fp, "# pmu mappings: not available\n");
1795 str = ff->ph->env.pmu_mappings;
1798 type = strtoul(str, &tmp, 0);
1803 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1806 str += strlen(str) + 1;
1815 fprintf(fp, "# pmu mappings: unable to read\n");
1818 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1820 struct perf_session *session;
1821 struct evsel *evsel;
1824 session = container_of(ff->ph, struct perf_session, header);
1826 evlist__for_each_entry(session->evlist, evsel) {
1827 if (perf_evsel__is_group_leader(evsel) &&
1828 evsel->core.nr_members > 1) {
1829 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1830 perf_evsel__name(evsel));
1832 nr = evsel->core.nr_members - 1;
1834 fprintf(fp, ",%s", perf_evsel__name(evsel));
1842 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1844 struct perf_session *session;
1848 session = container_of(ff->ph, struct perf_session, header);
1850 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1851 time_buf, sizeof(time_buf));
1852 fprintf(fp, "# time of first sample : %s\n", time_buf);
1854 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1855 time_buf, sizeof(time_buf));
1856 fprintf(fp, "# time of last sample : %s\n", time_buf);
1858 d = (double)(session->evlist->last_sample_time -
1859 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1861 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1864 static void memory_node__fprintf(struct memory_node *n,
1865 unsigned long long bsize, FILE *fp)
1867 char buf_map[100], buf_size[50];
1868 unsigned long long size;
1870 size = bsize * bitmap_weight(n->set, n->size);
1871 unit_number__scnprintf(buf_size, 50, size);
1873 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1874 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1877 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1879 struct memory_node *nodes;
1882 nodes = ff->ph->env.memory_nodes;
1883 nr = ff->ph->env.nr_memory_nodes;
1885 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1886 nr, ff->ph->env.memory_bsize);
1888 for (i = 0; i < nr; i++) {
1889 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1893 static int __event_process_build_id(struct perf_record_header_build_id *bev,
1895 struct perf_session *session)
1898 struct machine *machine;
1901 enum dso_kernel_type dso_type;
1903 machine = perf_session__findnew_machine(session, bev->pid);
1907 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1910 case PERF_RECORD_MISC_KERNEL:
1911 dso_type = DSO_TYPE_KERNEL;
1913 case PERF_RECORD_MISC_GUEST_KERNEL:
1914 dso_type = DSO_TYPE_GUEST_KERNEL;
1916 case PERF_RECORD_MISC_USER:
1917 case PERF_RECORD_MISC_GUEST_USER:
1918 dso_type = DSO_TYPE_USER;
1924 dso = machine__findnew_dso(machine, filename);
1926 char sbuild_id[SBUILD_ID_SIZE];
1928 dso__set_build_id(dso, &bev->build_id);
1930 if (dso_type != DSO_TYPE_USER) {
1931 struct kmod_path m = { .name = NULL, };
1933 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1934 dso__set_module_info(dso, &m, machine);
1936 dso->kernel = dso_type;
1941 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1943 pr_debug("build id event received for %s: %s\n",
1944 dso->long_name, sbuild_id);
1953 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1954 int input, u64 offset, u64 size)
1956 struct perf_session *session = container_of(header, struct perf_session, header);
1958 struct perf_event_header header;
1959 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1962 struct perf_record_header_build_id bev;
1963 char filename[PATH_MAX];
1964 u64 limit = offset + size;
1966 while (offset < limit) {
1969 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1972 if (header->needs_swap)
1973 perf_event_header__bswap(&old_bev.header);
1975 len = old_bev.header.size - sizeof(old_bev);
1976 if (readn(input, filename, len) != len)
1979 bev.header = old_bev.header;
1982 * As the pid is the missing value, we need to fill
1983 * it properly. The header.misc value give us nice hint.
1985 bev.pid = HOST_KERNEL_ID;
1986 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1987 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1988 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1990 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1991 __event_process_build_id(&bev, filename, session);
1993 offset += bev.header.size;
1999 static int perf_header__read_build_ids(struct perf_header *header,
2000 int input, u64 offset, u64 size)
2002 struct perf_session *session = container_of(header, struct perf_session, header);
2003 struct perf_record_header_build_id bev;
2004 char filename[PATH_MAX];
2005 u64 limit = offset + size, orig_offset = offset;
2008 while (offset < limit) {
2011 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2014 if (header->needs_swap)
2015 perf_event_header__bswap(&bev.header);
2017 len = bev.header.size - sizeof(bev);
2018 if (readn(input, filename, len) != len)
2021 * The a1645ce1 changeset:
2023 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2025 * Added a field to struct perf_record_header_build_id that broke the file
2028 * Since the kernel build-id is the first entry, process the
2029 * table using the old format if the well known
2030 * '[kernel.kallsyms]' string for the kernel build-id has the
2031 * first 4 characters chopped off (where the pid_t sits).
2033 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2034 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2036 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2039 __event_process_build_id(&bev, filename, session);
2041 offset += bev.header.size;
2048 /* Macro for features that simply need to read and store a string. */
2049 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2050 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2052 ff->ph->env.__feat_env = do_read_string(ff); \
2053 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2056 FEAT_PROCESS_STR_FUN(hostname, hostname);
2057 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2058 FEAT_PROCESS_STR_FUN(version, version);
2059 FEAT_PROCESS_STR_FUN(arch, arch);
2060 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2061 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2063 static int process_tracing_data(struct feat_fd *ff, void *data)
2065 ssize_t ret = trace_report(ff->fd, data, false);
2067 return ret < 0 ? -1 : 0;
2070 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2072 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2073 pr_debug("Failed to read buildids, continuing...\n");
2077 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2080 u32 nr_cpus_avail, nr_cpus_online;
2082 ret = do_read_u32(ff, &nr_cpus_avail);
2086 ret = do_read_u32(ff, &nr_cpus_online);
2089 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2090 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2094 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2099 ret = do_read_u64(ff, &total_mem);
2102 ff->ph->env.total_mem = (unsigned long long)total_mem;
2106 static struct evsel *
2107 perf_evlist__find_by_index(struct evlist *evlist, int idx)
2109 struct evsel *evsel;
2111 evlist__for_each_entry(evlist, evsel) {
2112 if (evsel->idx == idx)
2120 perf_evlist__set_event_name(struct evlist *evlist,
2121 struct evsel *event)
2123 struct evsel *evsel;
2128 evsel = perf_evlist__find_by_index(evlist, event->idx);
2135 evsel->name = strdup(event->name);
2139 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2141 struct perf_session *session;
2142 struct evsel *evsel, *events = read_event_desc(ff);
2147 session = container_of(ff->ph, struct perf_session, header);
2149 if (session->data->is_pipe) {
2150 /* Save events for reading later by print_event_desc,
2151 * since they can't be read again in pipe mode. */
2152 ff->events = events;
2155 for (evsel = events; evsel->core.attr.size; evsel++)
2156 perf_evlist__set_event_name(session->evlist, evsel);
2158 if (!session->data->is_pipe)
2159 free_event_desc(events);
2164 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2166 char *str, *cmdline = NULL, **argv = NULL;
2169 if (do_read_u32(ff, &nr))
2172 ff->ph->env.nr_cmdline = nr;
2174 cmdline = zalloc(ff->size + nr + 1);
2178 argv = zalloc(sizeof(char *) * (nr + 1));
2182 for (i = 0; i < nr; i++) {
2183 str = do_read_string(ff);
2187 argv[i] = cmdline + len;
2188 memcpy(argv[i], str, strlen(str) + 1);
2189 len += strlen(str) + 1;
2192 ff->ph->env.cmdline = cmdline;
2193 ff->ph->env.cmdline_argv = (const char **) argv;
2202 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2207 int cpu_nr = ff->ph->env.nr_cpus_avail;
2209 struct perf_header *ph = ff->ph;
2210 bool do_core_id_test = true;
2212 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2216 if (do_read_u32(ff, &nr))
2219 ph->env.nr_sibling_cores = nr;
2220 size += sizeof(u32);
2221 if (strbuf_init(&sb, 128) < 0)
2224 for (i = 0; i < nr; i++) {
2225 str = do_read_string(ff);
2229 /* include a NULL character at the end */
2230 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2232 size += string_size(str);
2235 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2237 if (do_read_u32(ff, &nr))
2240 ph->env.nr_sibling_threads = nr;
2241 size += sizeof(u32);
2243 for (i = 0; i < nr; i++) {
2244 str = do_read_string(ff);
2248 /* include a NULL character at the end */
2249 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2251 size += string_size(str);
2254 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2257 * The header may be from old perf,
2258 * which doesn't include core id and socket id information.
2260 if (ff->size <= size) {
2261 zfree(&ph->env.cpu);
2265 /* On s390 the socket_id number is not related to the numbers of cpus.
2266 * The socket_id number might be higher than the numbers of cpus.
2267 * This depends on the configuration.
2268 * AArch64 is the same.
2270 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2271 || !strncmp(ph->env.arch, "aarch64", 7)))
2272 do_core_id_test = false;
2274 for (i = 0; i < (u32)cpu_nr; i++) {
2275 if (do_read_u32(ff, &nr))
2278 ph->env.cpu[i].core_id = nr;
2279 size += sizeof(u32);
2281 if (do_read_u32(ff, &nr))
2284 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2285 pr_debug("socket_id number is too big."
2286 "You may need to upgrade the perf tool.\n");
2290 ph->env.cpu[i].socket_id = nr;
2291 size += sizeof(u32);
2295 * The header may be from old perf,
2296 * which doesn't include die information.
2298 if (ff->size <= size)
2301 if (do_read_u32(ff, &nr))
2304 ph->env.nr_sibling_dies = nr;
2305 size += sizeof(u32);
2307 for (i = 0; i < nr; i++) {
2308 str = do_read_string(ff);
2312 /* include a NULL character at the end */
2313 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2315 size += string_size(str);
2318 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2320 for (i = 0; i < (u32)cpu_nr; i++) {
2321 if (do_read_u32(ff, &nr))
2324 ph->env.cpu[i].die_id = nr;
2330 strbuf_release(&sb);
2332 zfree(&ph->env.cpu);
2336 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2338 struct numa_node *nodes, *n;
2343 if (do_read_u32(ff, &nr))
2346 nodes = zalloc(sizeof(*nodes) * nr);
2350 for (i = 0; i < nr; i++) {
2354 if (do_read_u32(ff, &n->node))
2357 if (do_read_u64(ff, &n->mem_total))
2360 if (do_read_u64(ff, &n->mem_free))
2363 str = do_read_string(ff);
2367 n->map = perf_cpu_map__new(str);
2373 ff->ph->env.nr_numa_nodes = nr;
2374 ff->ph->env.numa_nodes = nodes;
2382 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2389 if (do_read_u32(ff, &pmu_num))
2393 pr_debug("pmu mappings not available\n");
2397 ff->ph->env.nr_pmu_mappings = pmu_num;
2398 if (strbuf_init(&sb, 128) < 0)
2402 if (do_read_u32(ff, &type))
2405 name = do_read_string(ff);
2409 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2411 /* include a NULL character at the end */
2412 if (strbuf_add(&sb, "", 1) < 0)
2415 if (!strcmp(name, "msr"))
2416 ff->ph->env.msr_pmu_type = type;
2421 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2425 strbuf_release(&sb);
2429 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2432 u32 i, nr, nr_groups;
2433 struct perf_session *session;
2434 struct evsel *evsel, *leader = NULL;
2441 if (do_read_u32(ff, &nr_groups))
2444 ff->ph->env.nr_groups = nr_groups;
2446 pr_debug("group desc not available\n");
2450 desc = calloc(nr_groups, sizeof(*desc));
2454 for (i = 0; i < nr_groups; i++) {
2455 desc[i].name = do_read_string(ff);
2459 if (do_read_u32(ff, &desc[i].leader_idx))
2462 if (do_read_u32(ff, &desc[i].nr_members))
2467 * Rebuild group relationship based on the group_desc
2469 session = container_of(ff->ph, struct perf_session, header);
2470 session->evlist->nr_groups = nr_groups;
2473 evlist__for_each_entry(session->evlist, evsel) {
2474 if (evsel->idx == (int) desc[i].leader_idx) {
2475 evsel->leader = evsel;
2476 /* {anon_group} is a dummy name */
2477 if (strcmp(desc[i].name, "{anon_group}")) {
2478 evsel->group_name = desc[i].name;
2479 desc[i].name = NULL;
2481 evsel->core.nr_members = desc[i].nr_members;
2483 if (i >= nr_groups || nr > 0) {
2484 pr_debug("invalid group desc\n");
2489 nr = evsel->core.nr_members - 1;
2492 /* This is a group member */
2493 evsel->leader = leader;
2499 if (i != nr_groups || nr != 0) {
2500 pr_debug("invalid group desc\n");
2506 for (i = 0; i < nr_groups; i++)
2507 zfree(&desc[i].name);
2513 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2515 struct perf_session *session;
2518 session = container_of(ff->ph, struct perf_session, header);
2520 err = auxtrace_index__process(ff->fd, ff->size, session,
2521 ff->ph->needs_swap);
2523 pr_err("Failed to process auxtrace index\n");
2527 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2529 struct cpu_cache_level *caches;
2530 u32 cnt, i, version;
2532 if (do_read_u32(ff, &version))
2538 if (do_read_u32(ff, &cnt))
2541 caches = zalloc(sizeof(*caches) * cnt);
2545 for (i = 0; i < cnt; i++) {
2546 struct cpu_cache_level c;
2549 if (do_read_u32(ff, &c.v))\
2550 goto out_free_caches; \
2559 c.v = do_read_string(ff); \
2561 goto out_free_caches;
2571 ff->ph->env.caches = caches;
2572 ff->ph->env.caches_cnt = cnt;
2579 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2581 struct perf_session *session;
2582 u64 first_sample_time, last_sample_time;
2585 session = container_of(ff->ph, struct perf_session, header);
2587 ret = do_read_u64(ff, &first_sample_time);
2591 ret = do_read_u64(ff, &last_sample_time);
2595 session->evlist->first_sample_time = first_sample_time;
2596 session->evlist->last_sample_time = last_sample_time;
2600 static int process_mem_topology(struct feat_fd *ff,
2601 void *data __maybe_unused)
2603 struct memory_node *nodes;
2604 u64 version, i, nr, bsize;
2607 if (do_read_u64(ff, &version))
2613 if (do_read_u64(ff, &bsize))
2616 if (do_read_u64(ff, &nr))
2619 nodes = zalloc(sizeof(*nodes) * nr);
2623 for (i = 0; i < nr; i++) {
2624 struct memory_node n;
2627 if (do_read_u64(ff, &n.v)) \
2635 if (do_read_bitmap(ff, &n.set, &n.size))
2641 ff->ph->env.memory_bsize = bsize;
2642 ff->ph->env.memory_nodes = nodes;
2643 ff->ph->env.nr_memory_nodes = nr;
2652 static int process_clockid(struct feat_fd *ff,
2653 void *data __maybe_unused)
2655 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2661 static int process_dir_format(struct feat_fd *ff,
2662 void *_data __maybe_unused)
2664 struct perf_session *session;
2665 struct perf_data *data;
2667 session = container_of(ff->ph, struct perf_session, header);
2668 data = session->data;
2670 if (WARN_ON(!perf_data__is_dir(data)))
2673 return do_read_u64(ff, &data->dir.version);
2676 #ifdef HAVE_LIBBPF_SUPPORT
2677 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2679 struct bpf_prog_info_linear *info_linear;
2680 struct bpf_prog_info_node *info_node;
2681 struct perf_env *env = &ff->ph->env;
2685 if (ff->ph->needs_swap) {
2686 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2690 if (do_read_u32(ff, &count))
2693 down_write(&env->bpf_progs.lock);
2695 for (i = 0; i < count; ++i) {
2696 u32 info_len, data_len;
2700 if (do_read_u32(ff, &info_len))
2702 if (do_read_u32(ff, &data_len))
2705 if (info_len > sizeof(struct bpf_prog_info)) {
2706 pr_warning("detected invalid bpf_prog_info\n");
2710 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2714 info_linear->info_len = sizeof(struct bpf_prog_info);
2715 info_linear->data_len = data_len;
2716 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2718 if (__do_read(ff, &info_linear->info, info_len))
2720 if (info_len < sizeof(struct bpf_prog_info))
2721 memset(((void *)(&info_linear->info)) + info_len, 0,
2722 sizeof(struct bpf_prog_info) - info_len);
2724 if (__do_read(ff, info_linear->data, data_len))
2727 info_node = malloc(sizeof(struct bpf_prog_info_node));
2731 /* after reading from file, translate offset to address */
2732 bpf_program__bpil_offs_to_addr(info_linear);
2733 info_node->info_linear = info_linear;
2734 perf_env__insert_bpf_prog_info(env, info_node);
2737 up_write(&env->bpf_progs.lock);
2742 up_write(&env->bpf_progs.lock);
2745 #else // HAVE_LIBBPF_SUPPORT
2746 static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2750 #endif // HAVE_LIBBPF_SUPPORT
2752 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2754 struct perf_env *env = &ff->ph->env;
2755 struct btf_node *node = NULL;
2759 if (ff->ph->needs_swap) {
2760 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2764 if (do_read_u32(ff, &count))
2767 down_write(&env->bpf_progs.lock);
2769 for (i = 0; i < count; ++i) {
2772 if (do_read_u32(ff, &id))
2774 if (do_read_u32(ff, &data_size))
2777 node = malloc(sizeof(struct btf_node) + data_size);
2782 node->data_size = data_size;
2784 if (__do_read(ff, node->data, data_size))
2787 perf_env__insert_btf(env, node);
2793 up_write(&env->bpf_progs.lock);
2798 static int process_compressed(struct feat_fd *ff,
2799 void *data __maybe_unused)
2801 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2804 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2807 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2810 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2813 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
2819 #define FEAT_OPR(n, func, __full_only) \
2821 .name = __stringify(n), \
2822 .write = write_##func, \
2823 .print = print_##func, \
2824 .full_only = __full_only, \
2825 .process = process_##func, \
2826 .synthesize = true \
2829 #define FEAT_OPN(n, func, __full_only) \
2831 .name = __stringify(n), \
2832 .write = write_##func, \
2833 .print = print_##func, \
2834 .full_only = __full_only, \
2835 .process = process_##func \
2838 /* feature_ops not implemented: */
2839 #define print_tracing_data NULL
2840 #define print_build_id NULL
2842 #define process_branch_stack NULL
2843 #define process_stat NULL
2845 // Only used in util/synthetic-events.c
2846 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2848 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2849 FEAT_OPN(TRACING_DATA, tracing_data, false),
2850 FEAT_OPN(BUILD_ID, build_id, false),
2851 FEAT_OPR(HOSTNAME, hostname, false),
2852 FEAT_OPR(OSRELEASE, osrelease, false),
2853 FEAT_OPR(VERSION, version, false),
2854 FEAT_OPR(ARCH, arch, false),
2855 FEAT_OPR(NRCPUS, nrcpus, false),
2856 FEAT_OPR(CPUDESC, cpudesc, false),
2857 FEAT_OPR(CPUID, cpuid, false),
2858 FEAT_OPR(TOTAL_MEM, total_mem, false),
2859 FEAT_OPR(EVENT_DESC, event_desc, false),
2860 FEAT_OPR(CMDLINE, cmdline, false),
2861 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2862 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2863 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2864 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2865 FEAT_OPR(GROUP_DESC, group_desc, false),
2866 FEAT_OPN(AUXTRACE, auxtrace, false),
2867 FEAT_OPN(STAT, stat, false),
2868 FEAT_OPN(CACHE, cache, true),
2869 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2870 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2871 FEAT_OPR(CLOCKID, clockid, false),
2872 FEAT_OPN(DIR_FORMAT, dir_format, false),
2873 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
2874 FEAT_OPR(BPF_BTF, bpf_btf, false),
2875 FEAT_OPR(COMPRESSED, compressed, false),
2878 struct header_print_data {
2880 bool full; /* extended list of headers */
2883 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2884 struct perf_header *ph,
2885 int feat, int fd, void *data)
2887 struct header_print_data *hd = data;
2890 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2891 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2892 "%d, continuing...\n", section->offset, feat);
2895 if (feat >= HEADER_LAST_FEATURE) {
2896 pr_warning("unknown feature %d\n", feat);
2899 if (!feat_ops[feat].print)
2902 ff = (struct feat_fd) {
2907 if (!feat_ops[feat].full_only || hd->full)
2908 feat_ops[feat].print(&ff, hd->fp);
2910 fprintf(hd->fp, "# %s info available, use -I to display\n",
2911 feat_ops[feat].name);
2916 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2918 struct header_print_data hd;
2919 struct perf_header *header = &session->header;
2920 int fd = perf_data__fd(session->data);
2928 ret = fstat(fd, &st);
2932 stctime = st.st_ctime;
2933 fprintf(fp, "# captured on : %s", ctime(&stctime));
2935 fprintf(fp, "# header version : %u\n", header->version);
2936 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2937 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2938 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2940 perf_header__process_sections(header, fd, &hd,
2941 perf_file_section__fprintf_info);
2943 if (session->data->is_pipe)
2946 fprintf(fp, "# missing features: ");
2947 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2949 fprintf(fp, "%s ", feat_ops[bit].name);
2956 static int do_write_feat(struct feat_fd *ff, int type,
2957 struct perf_file_section **p,
2958 struct evlist *evlist)
2963 if (perf_header__has_feat(ff->ph, type)) {
2964 if (!feat_ops[type].write)
2967 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2970 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2972 err = feat_ops[type].write(ff, evlist);
2974 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2976 /* undo anything written */
2977 lseek(ff->fd, (*p)->offset, SEEK_SET);
2981 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2987 static int perf_header__adds_write(struct perf_header *header,
2988 struct evlist *evlist, int fd)
2992 struct perf_file_section *feat_sec, *p;
2998 ff = (struct feat_fd){
3003 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3007 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3008 if (feat_sec == NULL)
3011 sec_size = sizeof(*feat_sec) * nr_sections;
3013 sec_start = header->feat_offset;
3014 lseek(fd, sec_start + sec_size, SEEK_SET);
3016 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3017 if (do_write_feat(&ff, feat, &p, evlist))
3018 perf_header__clear_feat(header, feat);
3021 lseek(fd, sec_start, SEEK_SET);
3023 * may write more than needed due to dropped feature, but
3024 * this is okay, reader will skip the missing entries
3026 err = do_write(&ff, feat_sec, sec_size);
3028 pr_debug("failed to write feature section\n");
3033 int perf_header__write_pipe(int fd)
3035 struct perf_pipe_file_header f_header;
3039 ff = (struct feat_fd){ .fd = fd };
3041 f_header = (struct perf_pipe_file_header){
3042 .magic = PERF_MAGIC,
3043 .size = sizeof(f_header),
3046 err = do_write(&ff, &f_header, sizeof(f_header));
3048 pr_debug("failed to write perf pipe header\n");
3055 int perf_session__write_header(struct perf_session *session,
3056 struct evlist *evlist,
3057 int fd, bool at_exit)
3059 struct perf_file_header f_header;
3060 struct perf_file_attr f_attr;
3061 struct perf_header *header = &session->header;
3062 struct evsel *evsel;
3067 ff = (struct feat_fd){ .fd = fd};
3068 lseek(fd, sizeof(f_header), SEEK_SET);
3070 evlist__for_each_entry(session->evlist, evsel) {
3071 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3072 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3074 pr_debug("failed to write perf header\n");
3079 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3081 evlist__for_each_entry(evlist, evsel) {
3082 f_attr = (struct perf_file_attr){
3083 .attr = evsel->core.attr,
3085 .offset = evsel->id_offset,
3086 .size = evsel->core.ids * sizeof(u64),
3089 err = do_write(&ff, &f_attr, sizeof(f_attr));
3091 pr_debug("failed to write perf header attribute\n");
3096 if (!header->data_offset)
3097 header->data_offset = lseek(fd, 0, SEEK_CUR);
3098 header->feat_offset = header->data_offset + header->data_size;
3101 err = perf_header__adds_write(header, evlist, fd);
3106 f_header = (struct perf_file_header){
3107 .magic = PERF_MAGIC,
3108 .size = sizeof(f_header),
3109 .attr_size = sizeof(f_attr),
3111 .offset = attr_offset,
3112 .size = evlist->core.nr_entries * sizeof(f_attr),
3115 .offset = header->data_offset,
3116 .size = header->data_size,
3118 /* event_types is ignored, store zeros */
3121 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3123 lseek(fd, 0, SEEK_SET);
3124 err = do_write(&ff, &f_header, sizeof(f_header));
3126 pr_debug("failed to write perf header\n");
3129 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3134 static int perf_header__getbuffer64(struct perf_header *header,
3135 int fd, void *buf, size_t size)
3137 if (readn(fd, buf, size) <= 0)
3140 if (header->needs_swap)
3141 mem_bswap_64(buf, size);
3146 int perf_header__process_sections(struct perf_header *header, int fd,
3148 int (*process)(struct perf_file_section *section,
3149 struct perf_header *ph,
3150 int feat, int fd, void *data))
3152 struct perf_file_section *feat_sec, *sec;
3158 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3162 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3166 sec_size = sizeof(*feat_sec) * nr_sections;
3168 lseek(fd, header->feat_offset, SEEK_SET);
3170 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3174 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3175 err = process(sec++, header, feat, fd, data);
3185 static const int attr_file_abi_sizes[] = {
3186 [0] = PERF_ATTR_SIZE_VER0,
3187 [1] = PERF_ATTR_SIZE_VER1,
3188 [2] = PERF_ATTR_SIZE_VER2,
3189 [3] = PERF_ATTR_SIZE_VER3,
3190 [4] = PERF_ATTR_SIZE_VER4,
3195 * In the legacy file format, the magic number is not used to encode endianness.
3196 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3197 * on ABI revisions, we need to try all combinations for all endianness to
3198 * detect the endianness.
3200 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3202 uint64_t ref_size, attr_size;
3205 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3206 ref_size = attr_file_abi_sizes[i]
3207 + sizeof(struct perf_file_section);
3208 if (hdr_sz != ref_size) {
3209 attr_size = bswap_64(hdr_sz);
3210 if (attr_size != ref_size)
3213 ph->needs_swap = true;
3215 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3220 /* could not determine endianness */
3224 #define PERF_PIPE_HDR_VER0 16
3226 static const size_t attr_pipe_abi_sizes[] = {
3227 [0] = PERF_PIPE_HDR_VER0,
3232 * In the legacy pipe format, there is an implicit assumption that endiannesss
3233 * between host recording the samples, and host parsing the samples is the
3234 * same. This is not always the case given that the pipe output may always be
3235 * redirected into a file and analyzed on a different machine with possibly a
3236 * different endianness and perf_event ABI revsions in the perf tool itself.
3238 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3243 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3244 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3245 attr_size = bswap_64(hdr_sz);
3246 if (attr_size != hdr_sz)
3249 ph->needs_swap = true;
3251 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3257 bool is_perf_magic(u64 magic)
3259 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3260 || magic == __perf_magic2
3261 || magic == __perf_magic2_sw)
3267 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3268 bool is_pipe, struct perf_header *ph)
3272 /* check for legacy format */
3273 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3275 ph->version = PERF_HEADER_VERSION_1;
3276 pr_debug("legacy perf.data format\n");
3278 return try_all_pipe_abis(hdr_sz, ph);
3280 return try_all_file_abis(hdr_sz, ph);
3283 * the new magic number serves two purposes:
3284 * - unique number to identify actual perf.data files
3285 * - encode endianness of file
3287 ph->version = PERF_HEADER_VERSION_2;
3289 /* check magic number with one endianness */
3290 if (magic == __perf_magic2)
3293 /* check magic number with opposite endianness */
3294 if (magic != __perf_magic2_sw)
3297 ph->needs_swap = true;
3302 int perf_file_header__read(struct perf_file_header *header,
3303 struct perf_header *ph, int fd)
3307 lseek(fd, 0, SEEK_SET);
3309 ret = readn(fd, header, sizeof(*header));
3313 if (check_magic_endian(header->magic,
3314 header->attr_size, false, ph) < 0) {
3315 pr_debug("magic/endian check failed\n");
3319 if (ph->needs_swap) {
3320 mem_bswap_64(header, offsetof(struct perf_file_header,
3324 if (header->size != sizeof(*header)) {
3325 /* Support the previous format */
3326 if (header->size == offsetof(typeof(*header), adds_features))
3327 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3330 } else if (ph->needs_swap) {
3332 * feature bitmap is declared as an array of unsigned longs --
3333 * not good since its size can differ between the host that
3334 * generated the data file and the host analyzing the file.
3336 * We need to handle endianness, but we don't know the size of
3337 * the unsigned long where the file was generated. Take a best
3338 * guess at determining it: try 64-bit swap first (ie., file
3339 * created on a 64-bit host), and check if the hostname feature
3340 * bit is set (this feature bit is forced on as of fbe96f2).
3341 * If the bit is not, undo the 64-bit swap and try a 32-bit
3342 * swap. If the hostname bit is still not set (e.g., older data
3343 * file), punt and fallback to the original behavior --
3344 * clearing all feature bits and setting buildid.
3346 mem_bswap_64(&header->adds_features,
3347 BITS_TO_U64(HEADER_FEAT_BITS));
3349 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3351 mem_bswap_64(&header->adds_features,
3352 BITS_TO_U64(HEADER_FEAT_BITS));
3355 mem_bswap_32(&header->adds_features,
3356 BITS_TO_U32(HEADER_FEAT_BITS));
3359 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3360 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3361 set_bit(HEADER_BUILD_ID, header->adds_features);
3365 memcpy(&ph->adds_features, &header->adds_features,
3366 sizeof(ph->adds_features));
3368 ph->data_offset = header->data.offset;
3369 ph->data_size = header->data.size;
3370 ph->feat_offset = header->data.offset + header->data.size;
3374 static int perf_file_section__process(struct perf_file_section *section,
3375 struct perf_header *ph,
3376 int feat, int fd, void *data)
3378 struct feat_fd fdd = {
3381 .size = section->size,
3382 .offset = section->offset,
3385 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3386 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3387 "%d, continuing...\n", section->offset, feat);
3391 if (feat >= HEADER_LAST_FEATURE) {
3392 pr_debug("unknown feature %d, continuing...\n", feat);
3396 if (!feat_ops[feat].process)
3399 return feat_ops[feat].process(&fdd, data);
3402 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3403 struct perf_header *ph, int fd,
3406 struct feat_fd ff = {
3407 .fd = STDOUT_FILENO,
3412 ret = readn(fd, header, sizeof(*header));
3416 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3417 pr_debug("endian/magic failed\n");
3422 header->size = bswap_64(header->size);
3424 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3430 static int perf_header__read_pipe(struct perf_session *session)
3432 struct perf_header *header = &session->header;
3433 struct perf_pipe_file_header f_header;
3435 if (perf_file_header__read_pipe(&f_header, header,
3436 perf_data__fd(session->data),
3437 session->repipe) < 0) {
3438 pr_debug("incompatible file format\n");
3445 static int read_attr(int fd, struct perf_header *ph,
3446 struct perf_file_attr *f_attr)
3448 struct perf_event_attr *attr = &f_attr->attr;
3450 size_t our_sz = sizeof(f_attr->attr);
3453 memset(f_attr, 0, sizeof(*f_attr));
3455 /* read minimal guaranteed structure */
3456 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3458 pr_debug("cannot read %d bytes of header attr\n",
3459 PERF_ATTR_SIZE_VER0);
3463 /* on file perf_event_attr size */
3471 sz = PERF_ATTR_SIZE_VER0;
3472 } else if (sz > our_sz) {
3473 pr_debug("file uses a more recent and unsupported ABI"
3474 " (%zu bytes extra)\n", sz - our_sz);
3477 /* what we have not yet read and that we know about */
3478 left = sz - PERF_ATTR_SIZE_VER0;
3481 ptr += PERF_ATTR_SIZE_VER0;
3483 ret = readn(fd, ptr, left);
3485 /* read perf_file_section, ids are read in caller */
3486 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3488 return ret <= 0 ? -1 : 0;
3491 static int perf_evsel__prepare_tracepoint_event(struct evsel *evsel,
3492 struct tep_handle *pevent)
3494 struct tep_event *event;
3497 /* already prepared */
3498 if (evsel->tp_format)
3501 if (pevent == NULL) {
3502 pr_debug("broken or missing trace data\n");
3506 event = tep_find_event(pevent, evsel->core.attr.config);
3507 if (event == NULL) {
3508 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
3513 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3514 evsel->name = strdup(bf);
3515 if (evsel->name == NULL)
3519 evsel->tp_format = event;
3523 static int perf_evlist__prepare_tracepoint_events(struct evlist *evlist,
3524 struct tep_handle *pevent)
3528 evlist__for_each_entry(evlist, pos) {
3529 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
3530 perf_evsel__prepare_tracepoint_event(pos, pevent))
3537 int perf_session__read_header(struct perf_session *session)
3539 struct perf_data *data = session->data;
3540 struct perf_header *header = &session->header;
3541 struct perf_file_header f_header;
3542 struct perf_file_attr f_attr;
3544 int nr_attrs, nr_ids, i, j;
3545 int fd = perf_data__fd(data);
3547 session->evlist = evlist__new();
3548 if (session->evlist == NULL)
3551 session->evlist->env = &header->env;
3552 session->machines.host.env = &header->env;
3553 if (perf_data__is_pipe(data))
3554 return perf_header__read_pipe(session);
3556 if (perf_file_header__read(&f_header, header, fd) < 0)
3560 * Sanity check that perf.data was written cleanly; data size is
3561 * initialized to 0 and updated only if the on_exit function is run.
3562 * If data size is still 0 then the file contains only partial
3563 * information. Just warn user and process it as much as it can.
3565 if (f_header.data.size == 0) {
3566 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3567 "Was the 'perf record' command properly terminated?\n",
3571 if (f_header.attr_size == 0) {
3572 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3573 "Was the 'perf record' command properly terminated?\n",
3578 nr_attrs = f_header.attrs.size / f_header.attr_size;
3579 lseek(fd, f_header.attrs.offset, SEEK_SET);
3581 for (i = 0; i < nr_attrs; i++) {
3582 struct evsel *evsel;
3585 if (read_attr(fd, header, &f_attr) < 0)
3588 if (header->needs_swap) {
3589 f_attr.ids.size = bswap_64(f_attr.ids.size);
3590 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3591 perf_event__attr_swap(&f_attr.attr);
3594 tmp = lseek(fd, 0, SEEK_CUR);
3595 evsel = evsel__new(&f_attr.attr);
3598 goto out_delete_evlist;
3600 evsel->needs_swap = header->needs_swap;
3602 * Do it before so that if perf_evsel__alloc_id fails, this
3603 * entry gets purged too at evlist__delete().
3605 evlist__add(session->evlist, evsel);
3607 nr_ids = f_attr.ids.size / sizeof(u64);
3609 * We don't have the cpu and thread maps on the header, so
3610 * for allocating the perf_sample_id table we fake 1 cpu and
3611 * hattr->ids threads.
3613 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
3614 goto out_delete_evlist;
3616 lseek(fd, f_attr.ids.offset, SEEK_SET);
3618 for (j = 0; j < nr_ids; j++) {
3619 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3622 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
3625 lseek(fd, tmp, SEEK_SET);
3628 perf_header__process_sections(header, fd, &session->tevent,
3629 perf_file_section__process);
3631 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3632 session->tevent.pevent))
3633 goto out_delete_evlist;
3640 evlist__delete(session->evlist);
3641 session->evlist = NULL;
3645 int perf_event__process_feature(struct perf_session *session,
3646 union perf_event *event)
3648 struct perf_tool *tool = session->tool;
3649 struct feat_fd ff = { .fd = 0 };
3650 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
3651 int type = fe->header.type;
3652 u64 feat = fe->feat_id;
3654 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3655 pr_warning("invalid record type %d in pipe-mode\n", type);
3658 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3659 pr_warning("invalid record type %d in pipe-mode\n", type);
3663 if (!feat_ops[feat].process)
3666 ff.buf = (void *)fe->data;
3667 ff.size = event->header.size - sizeof(*fe);
3668 ff.ph = &session->header;
3670 if (feat_ops[feat].process(&ff, NULL))
3673 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3676 if (!feat_ops[feat].full_only ||
3677 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3678 feat_ops[feat].print(&ff, stdout);
3680 fprintf(stdout, "# %s info available, use -I to display\n",
3681 feat_ops[feat].name);
3687 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3689 struct perf_record_event_update *ev = &event->event_update;
3690 struct perf_record_event_update_scale *ev_scale;
3691 struct perf_record_event_update_cpus *ev_cpus;
3692 struct perf_cpu_map *map;
3695 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
3698 case PERF_EVENT_UPDATE__SCALE:
3699 ev_scale = (struct perf_record_event_update_scale *)ev->data;
3700 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3702 case PERF_EVENT_UPDATE__UNIT:
3703 ret += fprintf(fp, "... unit: %s\n", ev->data);
3705 case PERF_EVENT_UPDATE__NAME:
3706 ret += fprintf(fp, "... name: %s\n", ev->data);
3708 case PERF_EVENT_UPDATE__CPUS:
3709 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
3710 ret += fprintf(fp, "... ");
3712 map = cpu_map__new_data(&ev_cpus->cpus);
3714 ret += cpu_map__fprintf(map, fp);
3716 ret += fprintf(fp, "failed to get cpus\n");
3719 ret += fprintf(fp, "... unknown type\n");
3726 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3727 union perf_event *event,
3728 struct evlist **pevlist)
3731 struct evsel *evsel;
3732 struct evlist *evlist = *pevlist;
3734 if (evlist == NULL) {
3735 *pevlist = evlist = evlist__new();
3740 evsel = evsel__new(&event->attr.attr);
3744 evlist__add(evlist, evsel);
3746 ids = event->header.size;
3747 ids -= (void *)&event->attr.id - (void *)event;
3748 n_ids = ids / sizeof(u64);
3750 * We don't have the cpu and thread maps on the header, so
3751 * for allocating the perf_sample_id table we fake 1 cpu and
3752 * hattr->ids threads.
3754 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
3757 for (i = 0; i < n_ids; i++) {
3758 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
3764 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3765 union perf_event *event,
3766 struct evlist **pevlist)
3768 struct perf_record_event_update *ev = &event->event_update;
3769 struct perf_record_event_update_scale *ev_scale;
3770 struct perf_record_event_update_cpus *ev_cpus;
3771 struct evlist *evlist;
3772 struct evsel *evsel;
3773 struct perf_cpu_map *map;
3775 if (!pevlist || *pevlist == NULL)
3780 evsel = perf_evlist__id2evsel(evlist, ev->id);
3785 case PERF_EVENT_UPDATE__UNIT:
3786 evsel->unit = strdup(ev->data);
3788 case PERF_EVENT_UPDATE__NAME:
3789 evsel->name = strdup(ev->data);
3791 case PERF_EVENT_UPDATE__SCALE:
3792 ev_scale = (struct perf_record_event_update_scale *)ev->data;
3793 evsel->scale = ev_scale->scale;
3795 case PERF_EVENT_UPDATE__CPUS:
3796 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
3798 map = cpu_map__new_data(&ev_cpus->cpus);
3800 evsel->core.own_cpus = map;
3802 pr_err("failed to get event_update cpus\n");
3810 int perf_event__process_tracing_data(struct perf_session *session,
3811 union perf_event *event)
3813 ssize_t size_read, padding, size = event->tracing_data.size;
3814 int fd = perf_data__fd(session->data);
3815 off_t offset = lseek(fd, 0, SEEK_CUR);
3818 /* setup for reading amidst mmap */
3819 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
3822 size_read = trace_report(fd, &session->tevent,
3824 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3826 if (readn(fd, buf, padding) < 0) {
3827 pr_err("%s: reading input file", __func__);
3830 if (session->repipe) {
3831 int retw = write(STDOUT_FILENO, buf, padding);
3832 if (retw <= 0 || retw != padding) {
3833 pr_err("%s: repiping tracing data padding", __func__);
3838 if (size_read + padding != size) {
3839 pr_err("%s: tracing data size mismatch", __func__);
3843 perf_evlist__prepare_tracepoint_events(session->evlist,
3844 session->tevent.pevent);
3846 return size_read + padding;
3849 int perf_event__process_build_id(struct perf_session *session,
3850 union perf_event *event)
3852 __event_process_build_id(&event->build_id,
3853 event->build_id.filename,