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/stringify.h>
18 #include <sys/utsname.h>
19 #include <linux/time64.h>
21 #include <bpf/libbpf.h>
28 #include "trace-event.h"
38 #include <api/fs/fs.h>
41 #include "time-utils.h"
44 #include "bpf-event.h"
46 #include "sane_ctype.h"
50 * must be a numerical value to let the endianness
51 * determine the memory layout. That way we are able
52 * to detect endianness when reading the perf.data file
55 * we check for legacy (PERFFILE) format.
57 static const char *__perf_magic1 = "PERFFILE";
58 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
59 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
61 #define PERF_MAGIC __perf_magic2
63 const char perf_version_string[] = PERF_VERSION;
65 struct perf_file_attr {
66 struct perf_event_attr attr;
67 struct perf_file_section ids;
71 struct perf_header *ph;
73 void *buf; /* Either buf != NULL or fd >= 0 */
76 struct perf_evsel *events;
79 void perf_header__set_feat(struct perf_header *header, int feat)
81 set_bit(feat, header->adds_features);
84 void perf_header__clear_feat(struct perf_header *header, int feat)
86 clear_bit(feat, header->adds_features);
89 bool perf_header__has_feat(const struct perf_header *header, int feat)
91 return test_bit(feat, header->adds_features);
94 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
96 ssize_t ret = writen(ff->fd, buf, size);
98 if (ret != (ssize_t)size)
99 return ret < 0 ? (int)ret : -1;
103 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
105 /* struct perf_event_header::size is u16 */
106 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
107 size_t new_size = ff->size;
110 if (size + ff->offset > max_size)
113 while (size > (new_size - ff->offset))
115 new_size = min(max_size, new_size);
117 if (ff->size < new_size) {
118 addr = realloc(ff->buf, new_size);
125 memcpy(ff->buf + ff->offset, buf, size);
131 /* Return: 0 if succeded, -ERR if failed. */
132 int do_write(struct feat_fd *ff, const void *buf, size_t size)
135 return __do_write_fd(ff, buf, size);
136 return __do_write_buf(ff, buf, size);
139 /* Return: 0 if succeded, -ERR if failed. */
140 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
142 u64 *p = (u64 *) set;
145 ret = do_write(ff, &size, sizeof(size));
149 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
150 ret = do_write(ff, p + i, sizeof(*p));
158 /* Return: 0 if succeded, -ERR if failed. */
159 int write_padded(struct feat_fd *ff, const void *bf,
160 size_t count, size_t count_aligned)
162 static const char zero_buf[NAME_ALIGN];
163 int err = do_write(ff, bf, count);
166 err = do_write(ff, zero_buf, count_aligned - count);
171 #define string_size(str) \
172 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
174 /* Return: 0 if succeded, -ERR if failed. */
175 static int do_write_string(struct feat_fd *ff, const char *str)
180 olen = strlen(str) + 1;
181 len = PERF_ALIGN(olen, NAME_ALIGN);
183 /* write len, incl. \0 */
184 ret = do_write(ff, &len, sizeof(len));
188 return write_padded(ff, str, olen, len);
191 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
193 ssize_t ret = readn(ff->fd, addr, size);
196 return ret < 0 ? (int)ret : -1;
200 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
202 if (size > (ssize_t)ff->size - ff->offset)
205 memcpy(addr, ff->buf + ff->offset, size);
212 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
215 return __do_read_fd(ff, addr, size);
216 return __do_read_buf(ff, addr, size);
219 static int do_read_u32(struct feat_fd *ff, u32 *addr)
223 ret = __do_read(ff, addr, sizeof(*addr));
227 if (ff->ph->needs_swap)
228 *addr = bswap_32(*addr);
232 static int do_read_u64(struct feat_fd *ff, u64 *addr)
236 ret = __do_read(ff, addr, sizeof(*addr));
240 if (ff->ph->needs_swap)
241 *addr = bswap_64(*addr);
245 static char *do_read_string(struct feat_fd *ff)
250 if (do_read_u32(ff, &len))
257 if (!__do_read(ff, buf, len)) {
259 * strings are padded by zeroes
260 * thus the actual strlen of buf
261 * may be less than len
270 /* Return: 0 if succeded, -ERR if failed. */
271 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
277 ret = do_read_u64(ff, &size);
281 set = bitmap_alloc(size);
287 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
288 ret = do_read_u64(ff, p + i);
300 static int write_tracing_data(struct feat_fd *ff,
301 struct perf_evlist *evlist)
303 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
306 return read_tracing_data(ff->fd, &evlist->entries);
309 static int write_build_id(struct feat_fd *ff,
310 struct perf_evlist *evlist __maybe_unused)
312 struct perf_session *session;
315 session = container_of(ff->ph, struct perf_session, header);
317 if (!perf_session__read_build_ids(session, true))
320 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
323 err = perf_session__write_buildid_table(session, ff);
325 pr_debug("failed to write buildid table\n");
328 perf_session__cache_build_ids(session);
333 static int write_hostname(struct feat_fd *ff,
334 struct perf_evlist *evlist __maybe_unused)
343 return do_write_string(ff, uts.nodename);
346 static int write_osrelease(struct feat_fd *ff,
347 struct perf_evlist *evlist __maybe_unused)
356 return do_write_string(ff, uts.release);
359 static int write_arch(struct feat_fd *ff,
360 struct perf_evlist *evlist __maybe_unused)
369 return do_write_string(ff, uts.machine);
372 static int write_version(struct feat_fd *ff,
373 struct perf_evlist *evlist __maybe_unused)
375 return do_write_string(ff, perf_version_string);
378 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
383 const char *search = cpuinfo_proc;
390 file = fopen("/proc/cpuinfo", "r");
394 while (getline(&buf, &len, file) > 0) {
395 ret = strncmp(buf, search, strlen(search));
407 p = strchr(buf, ':');
408 if (p && *(p+1) == ' ' && *(p+2))
414 /* squash extra space characters (branding string) */
421 while (*q && isspace(*q))
424 while ((*r++ = *q++));
428 ret = do_write_string(ff, s);
435 static int write_cpudesc(struct feat_fd *ff,
436 struct perf_evlist *evlist __maybe_unused)
438 const char *cpuinfo_procs[] = CPUINFO_PROC;
441 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
443 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
451 static int write_nrcpus(struct feat_fd *ff,
452 struct perf_evlist *evlist __maybe_unused)
458 nrc = cpu__max_present_cpu();
460 nr = sysconf(_SC_NPROCESSORS_ONLN);
464 nra = (u32)(nr & UINT_MAX);
466 ret = do_write(ff, &nrc, sizeof(nrc));
470 return do_write(ff, &nra, sizeof(nra));
473 static int write_event_desc(struct feat_fd *ff,
474 struct perf_evlist *evlist)
476 struct perf_evsel *evsel;
480 nre = evlist->nr_entries;
483 * write number of events
485 ret = do_write(ff, &nre, sizeof(nre));
490 * size of perf_event_attr struct
492 sz = (u32)sizeof(evsel->attr);
493 ret = do_write(ff, &sz, sizeof(sz));
497 evlist__for_each_entry(evlist, evsel) {
498 ret = do_write(ff, &evsel->attr, sz);
502 * write number of unique id per event
503 * there is one id per instance of an event
505 * copy into an nri to be independent of the
509 ret = do_write(ff, &nri, sizeof(nri));
514 * write event string as passed on cmdline
516 ret = do_write_string(ff, perf_evsel__name(evsel));
520 * write unique ids for this event
522 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
529 static int write_cmdline(struct feat_fd *ff,
530 struct perf_evlist *evlist __maybe_unused)
532 char pbuf[MAXPATHLEN], *buf;
535 /* actual path to perf binary */
536 buf = perf_exe(pbuf, MAXPATHLEN);
538 /* account for binary path */
539 n = perf_env.nr_cmdline + 1;
541 ret = do_write(ff, &n, sizeof(n));
545 ret = do_write_string(ff, buf);
549 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
550 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
558 static int write_cpu_topology(struct feat_fd *ff,
559 struct perf_evlist *evlist __maybe_unused)
561 struct cpu_topology *tp;
565 tp = cpu_topology__new();
569 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
573 for (i = 0; i < tp->core_sib; i++) {
574 ret = do_write_string(ff, tp->core_siblings[i]);
578 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
582 for (i = 0; i < tp->thread_sib; i++) {
583 ret = do_write_string(ff, tp->thread_siblings[i]);
588 ret = perf_env__read_cpu_topology_map(&perf_env);
592 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
593 ret = do_write(ff, &perf_env.cpu[j].core_id,
594 sizeof(perf_env.cpu[j].core_id));
597 ret = do_write(ff, &perf_env.cpu[j].socket_id,
598 sizeof(perf_env.cpu[j].socket_id));
606 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib));
610 for (i = 0; i < tp->die_sib; i++) {
611 ret = do_write_string(ff, tp->die_siblings[i]);
616 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
617 ret = do_write(ff, &perf_env.cpu[j].die_id,
618 sizeof(perf_env.cpu[j].die_id));
624 cpu_topology__delete(tp);
630 static int write_total_mem(struct feat_fd *ff,
631 struct perf_evlist *evlist __maybe_unused)
639 fp = fopen("/proc/meminfo", "r");
643 while (getline(&buf, &len, fp) > 0) {
644 ret = strncmp(buf, "MemTotal:", 9);
649 n = sscanf(buf, "%*s %"PRIu64, &mem);
651 ret = do_write(ff, &mem, sizeof(mem));
659 static int write_numa_topology(struct feat_fd *ff,
660 struct perf_evlist *evlist __maybe_unused)
662 struct numa_topology *tp;
666 tp = numa_topology__new();
670 ret = do_write(ff, &tp->nr, sizeof(u32));
674 for (i = 0; i < tp->nr; i++) {
675 struct numa_topology_node *n = &tp->nodes[i];
677 ret = do_write(ff, &n->node, sizeof(u32));
681 ret = do_write(ff, &n->mem_total, sizeof(u64));
685 ret = do_write(ff, &n->mem_free, sizeof(u64));
689 ret = do_write_string(ff, n->cpus);
697 numa_topology__delete(tp);
704 * struct pmu_mappings {
713 static int write_pmu_mappings(struct feat_fd *ff,
714 struct perf_evlist *evlist __maybe_unused)
716 struct perf_pmu *pmu = NULL;
721 * Do a first pass to count number of pmu to avoid lseek so this
722 * works in pipe mode as well.
724 while ((pmu = perf_pmu__scan(pmu))) {
730 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
734 while ((pmu = perf_pmu__scan(pmu))) {
738 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
742 ret = do_write_string(ff, pmu->name);
753 * struct group_descs {
755 * struct group_desc {
762 static int write_group_desc(struct feat_fd *ff,
763 struct perf_evlist *evlist)
765 u32 nr_groups = evlist->nr_groups;
766 struct perf_evsel *evsel;
769 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
773 evlist__for_each_entry(evlist, evsel) {
774 if (perf_evsel__is_group_leader(evsel) &&
775 evsel->nr_members > 1) {
776 const char *name = evsel->group_name ?: "{anon_group}";
777 u32 leader_idx = evsel->idx;
778 u32 nr_members = evsel->nr_members;
780 ret = do_write_string(ff, name);
784 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
788 ret = do_write(ff, &nr_members, sizeof(nr_members));
797 * Return the CPU id as a raw string.
799 * Each architecture should provide a more precise id string that
800 * can be use to match the architecture's "mapfile".
802 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
807 /* Return zero when the cpuid from the mapfile.csv matches the
808 * cpuid string generated on this platform.
809 * Otherwise return non-zero.
811 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
814 regmatch_t pmatch[1];
817 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
818 /* Warn unable to generate match particular string. */
819 pr_info("Invalid regular expression %s\n", mapcpuid);
823 match = !regexec(&re, cpuid, 1, pmatch, 0);
826 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
828 /* Verify the entire string matched. */
829 if (match_len == strlen(cpuid))
836 * default get_cpuid(): nothing gets recorded
837 * actual implementation must be in arch/$(SRCARCH)/util/header.c
839 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
844 static int write_cpuid(struct feat_fd *ff,
845 struct perf_evlist *evlist __maybe_unused)
850 ret = get_cpuid(buffer, sizeof(buffer));
854 return do_write_string(ff, buffer);
857 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
858 struct perf_evlist *evlist __maybe_unused)
863 static int write_auxtrace(struct feat_fd *ff,
864 struct perf_evlist *evlist __maybe_unused)
866 struct perf_session *session;
869 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
872 session = container_of(ff->ph, struct perf_session, header);
874 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
876 pr_err("Failed to write auxtrace index\n");
880 static int write_clockid(struct feat_fd *ff,
881 struct perf_evlist *evlist __maybe_unused)
883 return do_write(ff, &ff->ph->env.clockid_res_ns,
884 sizeof(ff->ph->env.clockid_res_ns));
887 static int write_dir_format(struct feat_fd *ff,
888 struct perf_evlist *evlist __maybe_unused)
890 struct perf_session *session;
891 struct perf_data *data;
893 session = container_of(ff->ph, struct perf_session, header);
894 data = session->data;
896 if (WARN_ON(!perf_data__is_dir(data)))
899 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
902 #ifdef HAVE_LIBBPF_SUPPORT
903 static int write_bpf_prog_info(struct feat_fd *ff,
904 struct perf_evlist *evlist __maybe_unused)
906 struct perf_env *env = &ff->ph->env;
907 struct rb_root *root;
908 struct rb_node *next;
911 down_read(&env->bpf_progs.lock);
913 ret = do_write(ff, &env->bpf_progs.infos_cnt,
914 sizeof(env->bpf_progs.infos_cnt));
918 root = &env->bpf_progs.infos;
919 next = rb_first(root);
921 struct bpf_prog_info_node *node;
924 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
925 next = rb_next(&node->rb_node);
926 len = sizeof(struct bpf_prog_info_linear) +
927 node->info_linear->data_len;
929 /* before writing to file, translate address to offset */
930 bpf_program__bpil_addr_to_offs(node->info_linear);
931 ret = do_write(ff, node->info_linear, len);
933 * translate back to address even when do_write() fails,
934 * so that this function never changes the data.
936 bpf_program__bpil_offs_to_addr(node->info_linear);
941 up_read(&env->bpf_progs.lock);
944 #else // HAVE_LIBBPF_SUPPORT
945 static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
946 struct perf_evlist *evlist __maybe_unused)
950 #endif // HAVE_LIBBPF_SUPPORT
952 static int write_bpf_btf(struct feat_fd *ff,
953 struct perf_evlist *evlist __maybe_unused)
955 struct perf_env *env = &ff->ph->env;
956 struct rb_root *root;
957 struct rb_node *next;
960 down_read(&env->bpf_progs.lock);
962 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
963 sizeof(env->bpf_progs.btfs_cnt));
968 root = &env->bpf_progs.btfs;
969 next = rb_first(root);
971 struct btf_node *node;
973 node = rb_entry(next, struct btf_node, rb_node);
974 next = rb_next(&node->rb_node);
975 ret = do_write(ff, &node->id,
976 sizeof(u32) * 2 + node->data_size);
981 up_read(&env->bpf_progs.lock);
985 static int cpu_cache_level__sort(const void *a, const void *b)
987 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
988 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
990 return cache_a->level - cache_b->level;
993 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
995 if (a->level != b->level)
998 if (a->line_size != b->line_size)
1001 if (a->sets != b->sets)
1004 if (a->ways != b->ways)
1007 if (strcmp(a->type, b->type))
1010 if (strcmp(a->size, b->size))
1013 if (strcmp(a->map, b->map))
1019 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1021 char path[PATH_MAX], file[PATH_MAX];
1025 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1026 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1028 if (stat(file, &st))
1031 scnprintf(file, PATH_MAX, "%s/level", path);
1032 if (sysfs__read_int(file, (int *) &cache->level))
1035 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1036 if (sysfs__read_int(file, (int *) &cache->line_size))
1039 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1040 if (sysfs__read_int(file, (int *) &cache->sets))
1043 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1044 if (sysfs__read_int(file, (int *) &cache->ways))
1047 scnprintf(file, PATH_MAX, "%s/type", path);
1048 if (sysfs__read_str(file, &cache->type, &len))
1051 cache->type[len] = 0;
1052 cache->type = rtrim(cache->type);
1054 scnprintf(file, PATH_MAX, "%s/size", path);
1055 if (sysfs__read_str(file, &cache->size, &len)) {
1060 cache->size[len] = 0;
1061 cache->size = rtrim(cache->size);
1063 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1064 if (sysfs__read_str(file, &cache->map, &len)) {
1070 cache->map[len] = 0;
1071 cache->map = rtrim(cache->map);
1075 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1077 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1080 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1087 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1091 nr = (u32)(ncpus & UINT_MAX);
1093 for (cpu = 0; cpu < nr; cpu++) {
1094 for (level = 0; level < 10; level++) {
1095 struct cpu_cache_level c;
1098 err = cpu_cache_level__read(&c, cpu, level);
1105 for (i = 0; i < cnt; i++) {
1106 if (cpu_cache_level__cmp(&c, &caches[i]))
1113 cpu_cache_level__free(&c);
1115 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1124 #define MAX_CACHES 2000
1126 static int write_cache(struct feat_fd *ff,
1127 struct perf_evlist *evlist __maybe_unused)
1129 struct cpu_cache_level caches[MAX_CACHES];
1130 u32 cnt = 0, i, version = 1;
1133 ret = build_caches(caches, MAX_CACHES, &cnt);
1137 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1139 ret = do_write(ff, &version, sizeof(u32));
1143 ret = do_write(ff, &cnt, sizeof(u32));
1147 for (i = 0; i < cnt; i++) {
1148 struct cpu_cache_level *c = &caches[i];
1151 ret = do_write(ff, &c->v, sizeof(u32)); \
1162 ret = do_write_string(ff, (const char *) c->v); \
1173 for (i = 0; i < cnt; i++)
1174 cpu_cache_level__free(&caches[i]);
1178 static int write_stat(struct feat_fd *ff __maybe_unused,
1179 struct perf_evlist *evlist __maybe_unused)
1184 static int write_sample_time(struct feat_fd *ff,
1185 struct perf_evlist *evlist)
1189 ret = do_write(ff, &evlist->first_sample_time,
1190 sizeof(evlist->first_sample_time));
1194 return do_write(ff, &evlist->last_sample_time,
1195 sizeof(evlist->last_sample_time));
1199 static int memory_node__read(struct memory_node *n, unsigned long idx)
1201 unsigned int phys, size = 0;
1202 char path[PATH_MAX];
1206 #define for_each_memory(mem, dir) \
1207 while ((ent = readdir(dir))) \
1208 if (strcmp(ent->d_name, ".") && \
1209 strcmp(ent->d_name, "..") && \
1210 sscanf(ent->d_name, "memory%u", &mem) == 1)
1212 scnprintf(path, PATH_MAX,
1213 "%s/devices/system/node/node%lu",
1214 sysfs__mountpoint(), idx);
1216 dir = opendir(path);
1218 pr_warning("failed: cant' open memory sysfs data\n");
1222 for_each_memory(phys, dir) {
1223 size = max(phys, size);
1228 n->set = bitmap_alloc(size);
1239 for_each_memory(phys, dir) {
1240 set_bit(phys, n->set);
1247 static int memory_node__sort(const void *a, const void *b)
1249 const struct memory_node *na = a;
1250 const struct memory_node *nb = b;
1252 return na->node - nb->node;
1255 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1257 char path[PATH_MAX];
1263 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1264 sysfs__mountpoint());
1266 dir = opendir(path);
1268 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1273 while (!ret && (ent = readdir(dir))) {
1277 if (!strcmp(ent->d_name, ".") ||
1278 !strcmp(ent->d_name, ".."))
1281 r = sscanf(ent->d_name, "node%u", &idx);
1285 if (WARN_ONCE(cnt >= size,
1286 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1289 ret = memory_node__read(&nodes[cnt++], idx);
1296 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1301 #define MAX_MEMORY_NODES 2000
1304 * The MEM_TOPOLOGY holds physical memory map for every
1305 * node in system. The format of data is as follows:
1307 * 0 - version | for future changes
1308 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1309 * 16 - count | number of nodes
1311 * For each node we store map of physical indexes for
1314 * 32 - node id | node index
1315 * 40 - size | size of bitmap
1316 * 48 - bitmap | bitmap of memory indexes that belongs to node
1318 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1319 struct perf_evlist *evlist __maybe_unused)
1321 static struct memory_node nodes[MAX_MEMORY_NODES];
1322 u64 bsize, version = 1, i, nr;
1325 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1326 (unsigned long long *) &bsize);
1330 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1334 ret = do_write(ff, &version, sizeof(version));
1338 ret = do_write(ff, &bsize, sizeof(bsize));
1342 ret = do_write(ff, &nr, sizeof(nr));
1346 for (i = 0; i < nr; i++) {
1347 struct memory_node *n = &nodes[i];
1350 ret = do_write(ff, &n->v, sizeof(n->v)); \
1359 ret = do_write_bitmap(ff, n->set, n->size);
1368 static int write_compressed(struct feat_fd *ff __maybe_unused,
1369 struct perf_evlist *evlist __maybe_unused)
1373 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1377 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1381 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1385 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1389 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1392 static void print_hostname(struct feat_fd *ff, FILE *fp)
1394 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1397 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1399 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1402 static void print_arch(struct feat_fd *ff, FILE *fp)
1404 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1407 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1409 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1412 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1414 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1415 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1418 static void print_version(struct feat_fd *ff, FILE *fp)
1420 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1423 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1427 nr = ff->ph->env.nr_cmdline;
1429 fprintf(fp, "# cmdline : ");
1431 for (i = 0; i < nr; i++) {
1432 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1434 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1438 char *quote = strchr(argv_i, '\'');
1442 fprintf(fp, "%s\\\'", argv_i);
1445 fprintf(fp, "%s ", argv_i);
1452 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1454 struct perf_header *ph = ff->ph;
1455 int cpu_nr = ph->env.nr_cpus_avail;
1459 nr = ph->env.nr_sibling_cores;
1460 str = ph->env.sibling_cores;
1462 for (i = 0; i < nr; i++) {
1463 fprintf(fp, "# sibling sockets : %s\n", str);
1464 str += strlen(str) + 1;
1467 if (ph->env.nr_sibling_dies) {
1468 nr = ph->env.nr_sibling_dies;
1469 str = ph->env.sibling_dies;
1471 for (i = 0; i < nr; i++) {
1472 fprintf(fp, "# sibling dies : %s\n", str);
1473 str += strlen(str) + 1;
1477 nr = ph->env.nr_sibling_threads;
1478 str = ph->env.sibling_threads;
1480 for (i = 0; i < nr; i++) {
1481 fprintf(fp, "# sibling threads : %s\n", str);
1482 str += strlen(str) + 1;
1485 if (ph->env.nr_sibling_dies) {
1486 if (ph->env.cpu != NULL) {
1487 for (i = 0; i < cpu_nr; i++)
1488 fprintf(fp, "# CPU %d: Core ID %d, "
1489 "Die ID %d, Socket ID %d\n",
1490 i, ph->env.cpu[i].core_id,
1491 ph->env.cpu[i].die_id,
1492 ph->env.cpu[i].socket_id);
1494 fprintf(fp, "# Core ID, Die ID and Socket ID "
1495 "information is not available\n");
1497 if (ph->env.cpu != NULL) {
1498 for (i = 0; i < cpu_nr; i++)
1499 fprintf(fp, "# CPU %d: Core ID %d, "
1501 i, ph->env.cpu[i].core_id,
1502 ph->env.cpu[i].socket_id);
1504 fprintf(fp, "# Core ID and Socket ID "
1505 "information is not available\n");
1509 static void print_clockid(struct feat_fd *ff, FILE *fp)
1511 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1512 ff->ph->env.clockid_res_ns * 1000);
1515 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1517 struct perf_session *session;
1518 struct perf_data *data;
1520 session = container_of(ff->ph, struct perf_session, header);
1521 data = session->data;
1523 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1526 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1528 struct perf_env *env = &ff->ph->env;
1529 struct rb_root *root;
1530 struct rb_node *next;
1532 down_read(&env->bpf_progs.lock);
1534 root = &env->bpf_progs.infos;
1535 next = rb_first(root);
1538 struct bpf_prog_info_node *node;
1540 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1541 next = rb_next(&node->rb_node);
1543 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1547 up_read(&env->bpf_progs.lock);
1550 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1552 struct perf_env *env = &ff->ph->env;
1553 struct rb_root *root;
1554 struct rb_node *next;
1556 down_read(&env->bpf_progs.lock);
1558 root = &env->bpf_progs.btfs;
1559 next = rb_first(root);
1562 struct btf_node *node;
1564 node = rb_entry(next, struct btf_node, rb_node);
1565 next = rb_next(&node->rb_node);
1566 fprintf(fp, "# btf info of id %u\n", node->id);
1569 up_read(&env->bpf_progs.lock);
1572 static void free_event_desc(struct perf_evsel *events)
1574 struct perf_evsel *evsel;
1579 for (evsel = events; evsel->attr.size; evsel++) {
1580 zfree(&evsel->name);
1587 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1589 struct perf_evsel *evsel, *events = NULL;
1592 u32 nre, sz, nr, i, j;
1595 /* number of events */
1596 if (do_read_u32(ff, &nre))
1599 if (do_read_u32(ff, &sz))
1602 /* buffer to hold on file attr struct */
1607 /* the last event terminates with evsel->attr.size == 0: */
1608 events = calloc(nre + 1, sizeof(*events));
1612 msz = sizeof(evsel->attr);
1616 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1620 * must read entire on-file attr struct to
1621 * sync up with layout.
1623 if (__do_read(ff, buf, sz))
1626 if (ff->ph->needs_swap)
1627 perf_event__attr_swap(buf);
1629 memcpy(&evsel->attr, buf, msz);
1631 if (do_read_u32(ff, &nr))
1634 if (ff->ph->needs_swap)
1635 evsel->needs_swap = true;
1637 evsel->name = do_read_string(ff);
1644 id = calloc(nr, sizeof(*id));
1650 for (j = 0 ; j < nr; j++) {
1651 if (do_read_u64(ff, id))
1660 free_event_desc(events);
1665 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1666 void *priv __maybe_unused)
1668 return fprintf(fp, ", %s = %s", name, val);
1671 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1673 struct perf_evsel *evsel, *events;
1678 events = ff->events;
1680 events = read_event_desc(ff);
1683 fprintf(fp, "# event desc: not available or unable to read\n");
1687 for (evsel = events; evsel->attr.size; evsel++) {
1688 fprintf(fp, "# event : name = %s, ", evsel->name);
1691 fprintf(fp, ", id = {");
1692 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1695 fprintf(fp, " %"PRIu64, *id);
1700 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1705 free_event_desc(events);
1709 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1711 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1714 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1717 struct numa_node *n;
1719 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1720 n = &ff->ph->env.numa_nodes[i];
1722 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1723 " free = %"PRIu64" kB\n",
1724 n->node, n->mem_total, n->mem_free);
1726 fprintf(fp, "# node%u cpu list : ", n->node);
1727 cpu_map__fprintf(n->map, fp);
1731 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1733 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1736 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1738 fprintf(fp, "# contains samples with branch stack\n");
1741 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1743 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1746 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1748 fprintf(fp, "# contains stat data\n");
1751 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1755 fprintf(fp, "# CPU cache info:\n");
1756 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1758 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1762 static void print_compressed(struct feat_fd *ff, FILE *fp)
1764 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1765 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1766 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1769 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1771 const char *delimiter = "# pmu mappings: ";
1776 pmu_num = ff->ph->env.nr_pmu_mappings;
1778 fprintf(fp, "# pmu mappings: not available\n");
1782 str = ff->ph->env.pmu_mappings;
1785 type = strtoul(str, &tmp, 0);
1790 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1793 str += strlen(str) + 1;
1802 fprintf(fp, "# pmu mappings: unable to read\n");
1805 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1807 struct perf_session *session;
1808 struct perf_evsel *evsel;
1811 session = container_of(ff->ph, struct perf_session, header);
1813 evlist__for_each_entry(session->evlist, evsel) {
1814 if (perf_evsel__is_group_leader(evsel) &&
1815 evsel->nr_members > 1) {
1816 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1817 perf_evsel__name(evsel));
1819 nr = evsel->nr_members - 1;
1821 fprintf(fp, ",%s", perf_evsel__name(evsel));
1829 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1831 struct perf_session *session;
1835 session = container_of(ff->ph, struct perf_session, header);
1837 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1838 time_buf, sizeof(time_buf));
1839 fprintf(fp, "# time of first sample : %s\n", time_buf);
1841 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1842 time_buf, sizeof(time_buf));
1843 fprintf(fp, "# time of last sample : %s\n", time_buf);
1845 d = (double)(session->evlist->last_sample_time -
1846 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1848 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1851 static void memory_node__fprintf(struct memory_node *n,
1852 unsigned long long bsize, FILE *fp)
1854 char buf_map[100], buf_size[50];
1855 unsigned long long size;
1857 size = bsize * bitmap_weight(n->set, n->size);
1858 unit_number__scnprintf(buf_size, 50, size);
1860 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1861 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1864 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1866 struct memory_node *nodes;
1869 nodes = ff->ph->env.memory_nodes;
1870 nr = ff->ph->env.nr_memory_nodes;
1872 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1873 nr, ff->ph->env.memory_bsize);
1875 for (i = 0; i < nr; i++) {
1876 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1880 static int __event_process_build_id(struct build_id_event *bev,
1882 struct perf_session *session)
1885 struct machine *machine;
1888 enum dso_kernel_type dso_type;
1890 machine = perf_session__findnew_machine(session, bev->pid);
1894 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1897 case PERF_RECORD_MISC_KERNEL:
1898 dso_type = DSO_TYPE_KERNEL;
1900 case PERF_RECORD_MISC_GUEST_KERNEL:
1901 dso_type = DSO_TYPE_GUEST_KERNEL;
1903 case PERF_RECORD_MISC_USER:
1904 case PERF_RECORD_MISC_GUEST_USER:
1905 dso_type = DSO_TYPE_USER;
1911 dso = machine__findnew_dso(machine, filename);
1913 char sbuild_id[SBUILD_ID_SIZE];
1915 dso__set_build_id(dso, &bev->build_id);
1917 if (dso_type != DSO_TYPE_USER) {
1918 struct kmod_path m = { .name = NULL, };
1920 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1921 dso__set_module_info(dso, &m, machine);
1923 dso->kernel = dso_type;
1928 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1930 pr_debug("build id event received for %s: %s\n",
1931 dso->long_name, sbuild_id);
1940 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1941 int input, u64 offset, u64 size)
1943 struct perf_session *session = container_of(header, struct perf_session, header);
1945 struct perf_event_header header;
1946 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1949 struct build_id_event bev;
1950 char filename[PATH_MAX];
1951 u64 limit = offset + size;
1953 while (offset < limit) {
1956 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1959 if (header->needs_swap)
1960 perf_event_header__bswap(&old_bev.header);
1962 len = old_bev.header.size - sizeof(old_bev);
1963 if (readn(input, filename, len) != len)
1966 bev.header = old_bev.header;
1969 * As the pid is the missing value, we need to fill
1970 * it properly. The header.misc value give us nice hint.
1972 bev.pid = HOST_KERNEL_ID;
1973 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1974 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1975 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1977 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1978 __event_process_build_id(&bev, filename, session);
1980 offset += bev.header.size;
1986 static int perf_header__read_build_ids(struct perf_header *header,
1987 int input, u64 offset, u64 size)
1989 struct perf_session *session = container_of(header, struct perf_session, header);
1990 struct build_id_event bev;
1991 char filename[PATH_MAX];
1992 u64 limit = offset + size, orig_offset = offset;
1995 while (offset < limit) {
1998 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2001 if (header->needs_swap)
2002 perf_event_header__bswap(&bev.header);
2004 len = bev.header.size - sizeof(bev);
2005 if (readn(input, filename, len) != len)
2008 * The a1645ce1 changeset:
2010 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2012 * Added a field to struct build_id_event that broke the file
2015 * Since the kernel build-id is the first entry, process the
2016 * table using the old format if the well known
2017 * '[kernel.kallsyms]' string for the kernel build-id has the
2018 * first 4 characters chopped off (where the pid_t sits).
2020 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2021 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2023 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2026 __event_process_build_id(&bev, filename, session);
2028 offset += bev.header.size;
2035 /* Macro for features that simply need to read and store a string. */
2036 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2037 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2039 ff->ph->env.__feat_env = do_read_string(ff); \
2040 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2043 FEAT_PROCESS_STR_FUN(hostname, hostname);
2044 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2045 FEAT_PROCESS_STR_FUN(version, version);
2046 FEAT_PROCESS_STR_FUN(arch, arch);
2047 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2048 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2050 static int process_tracing_data(struct feat_fd *ff, void *data)
2052 ssize_t ret = trace_report(ff->fd, data, false);
2054 return ret < 0 ? -1 : 0;
2057 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2059 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2060 pr_debug("Failed to read buildids, continuing...\n");
2064 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2067 u32 nr_cpus_avail, nr_cpus_online;
2069 ret = do_read_u32(ff, &nr_cpus_avail);
2073 ret = do_read_u32(ff, &nr_cpus_online);
2076 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2077 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2081 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2086 ret = do_read_u64(ff, &total_mem);
2089 ff->ph->env.total_mem = (unsigned long long)total_mem;
2093 static struct perf_evsel *
2094 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2096 struct perf_evsel *evsel;
2098 evlist__for_each_entry(evlist, evsel) {
2099 if (evsel->idx == idx)
2107 perf_evlist__set_event_name(struct perf_evlist *evlist,
2108 struct perf_evsel *event)
2110 struct perf_evsel *evsel;
2115 evsel = perf_evlist__find_by_index(evlist, event->idx);
2122 evsel->name = strdup(event->name);
2126 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2128 struct perf_session *session;
2129 struct perf_evsel *evsel, *events = read_event_desc(ff);
2134 session = container_of(ff->ph, struct perf_session, header);
2136 if (session->data->is_pipe) {
2137 /* Save events for reading later by print_event_desc,
2138 * since they can't be read again in pipe mode. */
2139 ff->events = events;
2142 for (evsel = events; evsel->attr.size; evsel++)
2143 perf_evlist__set_event_name(session->evlist, evsel);
2145 if (!session->data->is_pipe)
2146 free_event_desc(events);
2151 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2153 char *str, *cmdline = NULL, **argv = NULL;
2156 if (do_read_u32(ff, &nr))
2159 ff->ph->env.nr_cmdline = nr;
2161 cmdline = zalloc(ff->size + nr + 1);
2165 argv = zalloc(sizeof(char *) * (nr + 1));
2169 for (i = 0; i < nr; i++) {
2170 str = do_read_string(ff);
2174 argv[i] = cmdline + len;
2175 memcpy(argv[i], str, strlen(str) + 1);
2176 len += strlen(str) + 1;
2179 ff->ph->env.cmdline = cmdline;
2180 ff->ph->env.cmdline_argv = (const char **) argv;
2189 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2194 int cpu_nr = ff->ph->env.nr_cpus_avail;
2196 struct perf_header *ph = ff->ph;
2197 bool do_core_id_test = true;
2199 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2203 if (do_read_u32(ff, &nr))
2206 ph->env.nr_sibling_cores = nr;
2207 size += sizeof(u32);
2208 if (strbuf_init(&sb, 128) < 0)
2211 for (i = 0; i < nr; i++) {
2212 str = do_read_string(ff);
2216 /* include a NULL character at the end */
2217 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2219 size += string_size(str);
2222 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2224 if (do_read_u32(ff, &nr))
2227 ph->env.nr_sibling_threads = nr;
2228 size += sizeof(u32);
2230 for (i = 0; i < nr; i++) {
2231 str = do_read_string(ff);
2235 /* include a NULL character at the end */
2236 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2238 size += string_size(str);
2241 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2244 * The header may be from old perf,
2245 * which doesn't include core id and socket id information.
2247 if (ff->size <= size) {
2248 zfree(&ph->env.cpu);
2252 /* On s390 the socket_id number is not related to the numbers of cpus.
2253 * The socket_id number might be higher than the numbers of cpus.
2254 * This depends on the configuration.
2256 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
2257 do_core_id_test = false;
2259 for (i = 0; i < (u32)cpu_nr; i++) {
2260 if (do_read_u32(ff, &nr))
2263 ph->env.cpu[i].core_id = nr;
2264 size += sizeof(u32);
2266 if (do_read_u32(ff, &nr))
2269 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2270 pr_debug("socket_id number is too big."
2271 "You may need to upgrade the perf tool.\n");
2275 ph->env.cpu[i].socket_id = nr;
2276 size += sizeof(u32);
2280 * The header may be from old perf,
2281 * which doesn't include die information.
2283 if (ff->size <= size)
2286 if (do_read_u32(ff, &nr))
2289 ph->env.nr_sibling_dies = nr;
2290 size += sizeof(u32);
2292 for (i = 0; i < nr; i++) {
2293 str = do_read_string(ff);
2297 /* include a NULL character at the end */
2298 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2300 size += string_size(str);
2303 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2305 for (i = 0; i < (u32)cpu_nr; i++) {
2306 if (do_read_u32(ff, &nr))
2309 ph->env.cpu[i].die_id = nr;
2315 strbuf_release(&sb);
2317 zfree(&ph->env.cpu);
2321 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2323 struct numa_node *nodes, *n;
2328 if (do_read_u32(ff, &nr))
2331 nodes = zalloc(sizeof(*nodes) * nr);
2335 for (i = 0; i < nr; i++) {
2339 if (do_read_u32(ff, &n->node))
2342 if (do_read_u64(ff, &n->mem_total))
2345 if (do_read_u64(ff, &n->mem_free))
2348 str = do_read_string(ff);
2352 n->map = cpu_map__new(str);
2358 ff->ph->env.nr_numa_nodes = nr;
2359 ff->ph->env.numa_nodes = nodes;
2367 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2374 if (do_read_u32(ff, &pmu_num))
2378 pr_debug("pmu mappings not available\n");
2382 ff->ph->env.nr_pmu_mappings = pmu_num;
2383 if (strbuf_init(&sb, 128) < 0)
2387 if (do_read_u32(ff, &type))
2390 name = do_read_string(ff);
2394 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2396 /* include a NULL character at the end */
2397 if (strbuf_add(&sb, "", 1) < 0)
2400 if (!strcmp(name, "msr"))
2401 ff->ph->env.msr_pmu_type = type;
2406 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2410 strbuf_release(&sb);
2414 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2417 u32 i, nr, nr_groups;
2418 struct perf_session *session;
2419 struct perf_evsel *evsel, *leader = NULL;
2426 if (do_read_u32(ff, &nr_groups))
2429 ff->ph->env.nr_groups = nr_groups;
2431 pr_debug("group desc not available\n");
2435 desc = calloc(nr_groups, sizeof(*desc));
2439 for (i = 0; i < nr_groups; i++) {
2440 desc[i].name = do_read_string(ff);
2444 if (do_read_u32(ff, &desc[i].leader_idx))
2447 if (do_read_u32(ff, &desc[i].nr_members))
2452 * Rebuild group relationship based on the group_desc
2454 session = container_of(ff->ph, struct perf_session, header);
2455 session->evlist->nr_groups = nr_groups;
2458 evlist__for_each_entry(session->evlist, evsel) {
2459 if (evsel->idx == (int) desc[i].leader_idx) {
2460 evsel->leader = evsel;
2461 /* {anon_group} is a dummy name */
2462 if (strcmp(desc[i].name, "{anon_group}")) {
2463 evsel->group_name = desc[i].name;
2464 desc[i].name = NULL;
2466 evsel->nr_members = desc[i].nr_members;
2468 if (i >= nr_groups || nr > 0) {
2469 pr_debug("invalid group desc\n");
2474 nr = evsel->nr_members - 1;
2477 /* This is a group member */
2478 evsel->leader = leader;
2484 if (i != nr_groups || nr != 0) {
2485 pr_debug("invalid group desc\n");
2491 for (i = 0; i < nr_groups; i++)
2492 zfree(&desc[i].name);
2498 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2500 struct perf_session *session;
2503 session = container_of(ff->ph, struct perf_session, header);
2505 err = auxtrace_index__process(ff->fd, ff->size, session,
2506 ff->ph->needs_swap);
2508 pr_err("Failed to process auxtrace index\n");
2512 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2514 struct cpu_cache_level *caches;
2515 u32 cnt, i, version;
2517 if (do_read_u32(ff, &version))
2523 if (do_read_u32(ff, &cnt))
2526 caches = zalloc(sizeof(*caches) * cnt);
2530 for (i = 0; i < cnt; i++) {
2531 struct cpu_cache_level c;
2534 if (do_read_u32(ff, &c.v))\
2535 goto out_free_caches; \
2544 c.v = do_read_string(ff); \
2546 goto out_free_caches;
2556 ff->ph->env.caches = caches;
2557 ff->ph->env.caches_cnt = cnt;
2564 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2566 struct perf_session *session;
2567 u64 first_sample_time, last_sample_time;
2570 session = container_of(ff->ph, struct perf_session, header);
2572 ret = do_read_u64(ff, &first_sample_time);
2576 ret = do_read_u64(ff, &last_sample_time);
2580 session->evlist->first_sample_time = first_sample_time;
2581 session->evlist->last_sample_time = last_sample_time;
2585 static int process_mem_topology(struct feat_fd *ff,
2586 void *data __maybe_unused)
2588 struct memory_node *nodes;
2589 u64 version, i, nr, bsize;
2592 if (do_read_u64(ff, &version))
2598 if (do_read_u64(ff, &bsize))
2601 if (do_read_u64(ff, &nr))
2604 nodes = zalloc(sizeof(*nodes) * nr);
2608 for (i = 0; i < nr; i++) {
2609 struct memory_node n;
2612 if (do_read_u64(ff, &n.v)) \
2620 if (do_read_bitmap(ff, &n.set, &n.size))
2626 ff->ph->env.memory_bsize = bsize;
2627 ff->ph->env.memory_nodes = nodes;
2628 ff->ph->env.nr_memory_nodes = nr;
2637 static int process_clockid(struct feat_fd *ff,
2638 void *data __maybe_unused)
2640 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2646 static int process_dir_format(struct feat_fd *ff,
2647 void *_data __maybe_unused)
2649 struct perf_session *session;
2650 struct perf_data *data;
2652 session = container_of(ff->ph, struct perf_session, header);
2653 data = session->data;
2655 if (WARN_ON(!perf_data__is_dir(data)))
2658 return do_read_u64(ff, &data->dir.version);
2661 #ifdef HAVE_LIBBPF_SUPPORT
2662 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2664 struct bpf_prog_info_linear *info_linear;
2665 struct bpf_prog_info_node *info_node;
2666 struct perf_env *env = &ff->ph->env;
2670 if (ff->ph->needs_swap) {
2671 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2675 if (do_read_u32(ff, &count))
2678 down_write(&env->bpf_progs.lock);
2680 for (i = 0; i < count; ++i) {
2681 u32 info_len, data_len;
2685 if (do_read_u32(ff, &info_len))
2687 if (do_read_u32(ff, &data_len))
2690 if (info_len > sizeof(struct bpf_prog_info)) {
2691 pr_warning("detected invalid bpf_prog_info\n");
2695 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2699 info_linear->info_len = sizeof(struct bpf_prog_info);
2700 info_linear->data_len = data_len;
2701 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2703 if (__do_read(ff, &info_linear->info, info_len))
2705 if (info_len < sizeof(struct bpf_prog_info))
2706 memset(((void *)(&info_linear->info)) + info_len, 0,
2707 sizeof(struct bpf_prog_info) - info_len);
2709 if (__do_read(ff, info_linear->data, data_len))
2712 info_node = malloc(sizeof(struct bpf_prog_info_node));
2716 /* after reading from file, translate offset to address */
2717 bpf_program__bpil_offs_to_addr(info_linear);
2718 info_node->info_linear = info_linear;
2719 perf_env__insert_bpf_prog_info(env, info_node);
2722 up_write(&env->bpf_progs.lock);
2727 up_write(&env->bpf_progs.lock);
2730 #else // HAVE_LIBBPF_SUPPORT
2731 static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2735 #endif // HAVE_LIBBPF_SUPPORT
2737 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2739 struct perf_env *env = &ff->ph->env;
2740 struct btf_node *node = NULL;
2744 if (ff->ph->needs_swap) {
2745 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2749 if (do_read_u32(ff, &count))
2752 down_write(&env->bpf_progs.lock);
2754 for (i = 0; i < count; ++i) {
2757 if (do_read_u32(ff, &id))
2759 if (do_read_u32(ff, &data_size))
2762 node = malloc(sizeof(struct btf_node) + data_size);
2767 node->data_size = data_size;
2769 if (__do_read(ff, node->data, data_size))
2772 perf_env__insert_btf(env, node);
2778 up_write(&env->bpf_progs.lock);
2783 static int process_compressed(struct feat_fd *ff,
2784 void *data __maybe_unused)
2786 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2789 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2792 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2795 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2798 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
2804 struct feature_ops {
2805 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2806 void (*print)(struct feat_fd *ff, FILE *fp);
2807 int (*process)(struct feat_fd *ff, void *data);
2813 #define FEAT_OPR(n, func, __full_only) \
2815 .name = __stringify(n), \
2816 .write = write_##func, \
2817 .print = print_##func, \
2818 .full_only = __full_only, \
2819 .process = process_##func, \
2820 .synthesize = true \
2823 #define FEAT_OPN(n, func, __full_only) \
2825 .name = __stringify(n), \
2826 .write = write_##func, \
2827 .print = print_##func, \
2828 .full_only = __full_only, \
2829 .process = process_##func \
2832 /* feature_ops not implemented: */
2833 #define print_tracing_data NULL
2834 #define print_build_id NULL
2836 #define process_branch_stack NULL
2837 #define process_stat NULL
2840 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2841 FEAT_OPN(TRACING_DATA, tracing_data, false),
2842 FEAT_OPN(BUILD_ID, build_id, false),
2843 FEAT_OPR(HOSTNAME, hostname, false),
2844 FEAT_OPR(OSRELEASE, osrelease, false),
2845 FEAT_OPR(VERSION, version, false),
2846 FEAT_OPR(ARCH, arch, false),
2847 FEAT_OPR(NRCPUS, nrcpus, false),
2848 FEAT_OPR(CPUDESC, cpudesc, false),
2849 FEAT_OPR(CPUID, cpuid, false),
2850 FEAT_OPR(TOTAL_MEM, total_mem, false),
2851 FEAT_OPR(EVENT_DESC, event_desc, false),
2852 FEAT_OPR(CMDLINE, cmdline, false),
2853 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2854 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2855 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2856 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2857 FEAT_OPR(GROUP_DESC, group_desc, false),
2858 FEAT_OPN(AUXTRACE, auxtrace, false),
2859 FEAT_OPN(STAT, stat, false),
2860 FEAT_OPN(CACHE, cache, true),
2861 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2862 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2863 FEAT_OPR(CLOCKID, clockid, false),
2864 FEAT_OPN(DIR_FORMAT, dir_format, false),
2865 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
2866 FEAT_OPR(BPF_BTF, bpf_btf, false),
2867 FEAT_OPR(COMPRESSED, compressed, false),
2870 struct header_print_data {
2872 bool full; /* extended list of headers */
2875 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2876 struct perf_header *ph,
2877 int feat, int fd, void *data)
2879 struct header_print_data *hd = data;
2882 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2883 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2884 "%d, continuing...\n", section->offset, feat);
2887 if (feat >= HEADER_LAST_FEATURE) {
2888 pr_warning("unknown feature %d\n", feat);
2891 if (!feat_ops[feat].print)
2894 ff = (struct feat_fd) {
2899 if (!feat_ops[feat].full_only || hd->full)
2900 feat_ops[feat].print(&ff, hd->fp);
2902 fprintf(hd->fp, "# %s info available, use -I to display\n",
2903 feat_ops[feat].name);
2908 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2910 struct header_print_data hd;
2911 struct perf_header *header = &session->header;
2912 int fd = perf_data__fd(session->data);
2920 ret = fstat(fd, &st);
2924 stctime = st.st_ctime;
2925 fprintf(fp, "# captured on : %s", ctime(&stctime));
2927 fprintf(fp, "# header version : %u\n", header->version);
2928 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2929 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2930 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2932 perf_header__process_sections(header, fd, &hd,
2933 perf_file_section__fprintf_info);
2935 if (session->data->is_pipe)
2938 fprintf(fp, "# missing features: ");
2939 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2941 fprintf(fp, "%s ", feat_ops[bit].name);
2948 static int do_write_feat(struct feat_fd *ff, int type,
2949 struct perf_file_section **p,
2950 struct perf_evlist *evlist)
2955 if (perf_header__has_feat(ff->ph, type)) {
2956 if (!feat_ops[type].write)
2959 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2962 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2964 err = feat_ops[type].write(ff, evlist);
2966 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2968 /* undo anything written */
2969 lseek(ff->fd, (*p)->offset, SEEK_SET);
2973 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2979 static int perf_header__adds_write(struct perf_header *header,
2980 struct perf_evlist *evlist, int fd)
2984 struct perf_file_section *feat_sec, *p;
2990 ff = (struct feat_fd){
2995 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2999 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3000 if (feat_sec == NULL)
3003 sec_size = sizeof(*feat_sec) * nr_sections;
3005 sec_start = header->feat_offset;
3006 lseek(fd, sec_start + sec_size, SEEK_SET);
3008 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3009 if (do_write_feat(&ff, feat, &p, evlist))
3010 perf_header__clear_feat(header, feat);
3013 lseek(fd, sec_start, SEEK_SET);
3015 * may write more than needed due to dropped feature, but
3016 * this is okay, reader will skip the missing entries
3018 err = do_write(&ff, feat_sec, sec_size);
3020 pr_debug("failed to write feature section\n");
3025 int perf_header__write_pipe(int fd)
3027 struct perf_pipe_file_header f_header;
3031 ff = (struct feat_fd){ .fd = fd };
3033 f_header = (struct perf_pipe_file_header){
3034 .magic = PERF_MAGIC,
3035 .size = sizeof(f_header),
3038 err = do_write(&ff, &f_header, sizeof(f_header));
3040 pr_debug("failed to write perf pipe header\n");
3047 int perf_session__write_header(struct perf_session *session,
3048 struct perf_evlist *evlist,
3049 int fd, bool at_exit)
3051 struct perf_file_header f_header;
3052 struct perf_file_attr f_attr;
3053 struct perf_header *header = &session->header;
3054 struct perf_evsel *evsel;
3059 ff = (struct feat_fd){ .fd = fd};
3060 lseek(fd, sizeof(f_header), SEEK_SET);
3062 evlist__for_each_entry(session->evlist, evsel) {
3063 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3064 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
3066 pr_debug("failed to write perf header\n");
3071 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3073 evlist__for_each_entry(evlist, evsel) {
3074 f_attr = (struct perf_file_attr){
3075 .attr = evsel->attr,
3077 .offset = evsel->id_offset,
3078 .size = evsel->ids * sizeof(u64),
3081 err = do_write(&ff, &f_attr, sizeof(f_attr));
3083 pr_debug("failed to write perf header attribute\n");
3088 if (!header->data_offset)
3089 header->data_offset = lseek(fd, 0, SEEK_CUR);
3090 header->feat_offset = header->data_offset + header->data_size;
3093 err = perf_header__adds_write(header, evlist, fd);
3098 f_header = (struct perf_file_header){
3099 .magic = PERF_MAGIC,
3100 .size = sizeof(f_header),
3101 .attr_size = sizeof(f_attr),
3103 .offset = attr_offset,
3104 .size = evlist->nr_entries * sizeof(f_attr),
3107 .offset = header->data_offset,
3108 .size = header->data_size,
3110 /* event_types is ignored, store zeros */
3113 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3115 lseek(fd, 0, SEEK_SET);
3116 err = do_write(&ff, &f_header, sizeof(f_header));
3118 pr_debug("failed to write perf header\n");
3121 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3126 static int perf_header__getbuffer64(struct perf_header *header,
3127 int fd, void *buf, size_t size)
3129 if (readn(fd, buf, size) <= 0)
3132 if (header->needs_swap)
3133 mem_bswap_64(buf, size);
3138 int perf_header__process_sections(struct perf_header *header, int fd,
3140 int (*process)(struct perf_file_section *section,
3141 struct perf_header *ph,
3142 int feat, int fd, void *data))
3144 struct perf_file_section *feat_sec, *sec;
3150 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3154 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3158 sec_size = sizeof(*feat_sec) * nr_sections;
3160 lseek(fd, header->feat_offset, SEEK_SET);
3162 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3166 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3167 err = process(sec++, header, feat, fd, data);
3177 static const int attr_file_abi_sizes[] = {
3178 [0] = PERF_ATTR_SIZE_VER0,
3179 [1] = PERF_ATTR_SIZE_VER1,
3180 [2] = PERF_ATTR_SIZE_VER2,
3181 [3] = PERF_ATTR_SIZE_VER3,
3182 [4] = PERF_ATTR_SIZE_VER4,
3187 * In the legacy file format, the magic number is not used to encode endianness.
3188 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3189 * on ABI revisions, we need to try all combinations for all endianness to
3190 * detect the endianness.
3192 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3194 uint64_t ref_size, attr_size;
3197 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3198 ref_size = attr_file_abi_sizes[i]
3199 + sizeof(struct perf_file_section);
3200 if (hdr_sz != ref_size) {
3201 attr_size = bswap_64(hdr_sz);
3202 if (attr_size != ref_size)
3205 ph->needs_swap = true;
3207 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3212 /* could not determine endianness */
3216 #define PERF_PIPE_HDR_VER0 16
3218 static const size_t attr_pipe_abi_sizes[] = {
3219 [0] = PERF_PIPE_HDR_VER0,
3224 * In the legacy pipe format, there is an implicit assumption that endiannesss
3225 * between host recording the samples, and host parsing the samples is the
3226 * same. This is not always the case given that the pipe output may always be
3227 * redirected into a file and analyzed on a different machine with possibly a
3228 * different endianness and perf_event ABI revsions in the perf tool itself.
3230 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3235 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3236 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3237 attr_size = bswap_64(hdr_sz);
3238 if (attr_size != hdr_sz)
3241 ph->needs_swap = true;
3243 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3249 bool is_perf_magic(u64 magic)
3251 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3252 || magic == __perf_magic2
3253 || magic == __perf_magic2_sw)
3259 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3260 bool is_pipe, struct perf_header *ph)
3264 /* check for legacy format */
3265 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3267 ph->version = PERF_HEADER_VERSION_1;
3268 pr_debug("legacy perf.data format\n");
3270 return try_all_pipe_abis(hdr_sz, ph);
3272 return try_all_file_abis(hdr_sz, ph);
3275 * the new magic number serves two purposes:
3276 * - unique number to identify actual perf.data files
3277 * - encode endianness of file
3279 ph->version = PERF_HEADER_VERSION_2;
3281 /* check magic number with one endianness */
3282 if (magic == __perf_magic2)
3285 /* check magic number with opposite endianness */
3286 if (magic != __perf_magic2_sw)
3289 ph->needs_swap = true;
3294 int perf_file_header__read(struct perf_file_header *header,
3295 struct perf_header *ph, int fd)
3299 lseek(fd, 0, SEEK_SET);
3301 ret = readn(fd, header, sizeof(*header));
3305 if (check_magic_endian(header->magic,
3306 header->attr_size, false, ph) < 0) {
3307 pr_debug("magic/endian check failed\n");
3311 if (ph->needs_swap) {
3312 mem_bswap_64(header, offsetof(struct perf_file_header,
3316 if (header->size != sizeof(*header)) {
3317 /* Support the previous format */
3318 if (header->size == offsetof(typeof(*header), adds_features))
3319 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3322 } else if (ph->needs_swap) {
3324 * feature bitmap is declared as an array of unsigned longs --
3325 * not good since its size can differ between the host that
3326 * generated the data file and the host analyzing the file.
3328 * We need to handle endianness, but we don't know the size of
3329 * the unsigned long where the file was generated. Take a best
3330 * guess at determining it: try 64-bit swap first (ie., file
3331 * created on a 64-bit host), and check if the hostname feature
3332 * bit is set (this feature bit is forced on as of fbe96f2).
3333 * If the bit is not, undo the 64-bit swap and try a 32-bit
3334 * swap. If the hostname bit is still not set (e.g., older data
3335 * file), punt and fallback to the original behavior --
3336 * clearing all feature bits and setting buildid.
3338 mem_bswap_64(&header->adds_features,
3339 BITS_TO_U64(HEADER_FEAT_BITS));
3341 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3343 mem_bswap_64(&header->adds_features,
3344 BITS_TO_U64(HEADER_FEAT_BITS));
3347 mem_bswap_32(&header->adds_features,
3348 BITS_TO_U32(HEADER_FEAT_BITS));
3351 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3352 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3353 set_bit(HEADER_BUILD_ID, header->adds_features);
3357 memcpy(&ph->adds_features, &header->adds_features,
3358 sizeof(ph->adds_features));
3360 ph->data_offset = header->data.offset;
3361 ph->data_size = header->data.size;
3362 ph->feat_offset = header->data.offset + header->data.size;
3366 static int perf_file_section__process(struct perf_file_section *section,
3367 struct perf_header *ph,
3368 int feat, int fd, void *data)
3370 struct feat_fd fdd = {
3373 .size = section->size,
3374 .offset = section->offset,
3377 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3378 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3379 "%d, continuing...\n", section->offset, feat);
3383 if (feat >= HEADER_LAST_FEATURE) {
3384 pr_debug("unknown feature %d, continuing...\n", feat);
3388 if (!feat_ops[feat].process)
3391 return feat_ops[feat].process(&fdd, data);
3394 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3395 struct perf_header *ph, int fd,
3398 struct feat_fd ff = {
3399 .fd = STDOUT_FILENO,
3404 ret = readn(fd, header, sizeof(*header));
3408 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3409 pr_debug("endian/magic failed\n");
3414 header->size = bswap_64(header->size);
3416 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3422 static int perf_header__read_pipe(struct perf_session *session)
3424 struct perf_header *header = &session->header;
3425 struct perf_pipe_file_header f_header;
3427 if (perf_file_header__read_pipe(&f_header, header,
3428 perf_data__fd(session->data),
3429 session->repipe) < 0) {
3430 pr_debug("incompatible file format\n");
3437 static int read_attr(int fd, struct perf_header *ph,
3438 struct perf_file_attr *f_attr)
3440 struct perf_event_attr *attr = &f_attr->attr;
3442 size_t our_sz = sizeof(f_attr->attr);
3445 memset(f_attr, 0, sizeof(*f_attr));
3447 /* read minimal guaranteed structure */
3448 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3450 pr_debug("cannot read %d bytes of header attr\n",
3451 PERF_ATTR_SIZE_VER0);
3455 /* on file perf_event_attr size */
3463 sz = PERF_ATTR_SIZE_VER0;
3464 } else if (sz > our_sz) {
3465 pr_debug("file uses a more recent and unsupported ABI"
3466 " (%zu bytes extra)\n", sz - our_sz);
3469 /* what we have not yet read and that we know about */
3470 left = sz - PERF_ATTR_SIZE_VER0;
3473 ptr += PERF_ATTR_SIZE_VER0;
3475 ret = readn(fd, ptr, left);
3477 /* read perf_file_section, ids are read in caller */
3478 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3480 return ret <= 0 ? -1 : 0;
3483 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
3484 struct tep_handle *pevent)
3486 struct tep_event *event;
3489 /* already prepared */
3490 if (evsel->tp_format)
3493 if (pevent == NULL) {
3494 pr_debug("broken or missing trace data\n");
3498 event = tep_find_event(pevent, evsel->attr.config);
3499 if (event == NULL) {
3500 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
3505 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3506 evsel->name = strdup(bf);
3507 if (evsel->name == NULL)
3511 evsel->tp_format = event;
3515 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
3516 struct tep_handle *pevent)
3518 struct perf_evsel *pos;
3520 evlist__for_each_entry(evlist, pos) {
3521 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3522 perf_evsel__prepare_tracepoint_event(pos, pevent))
3529 int perf_session__read_header(struct perf_session *session)
3531 struct perf_data *data = session->data;
3532 struct perf_header *header = &session->header;
3533 struct perf_file_header f_header;
3534 struct perf_file_attr f_attr;
3536 int nr_attrs, nr_ids, i, j;
3537 int fd = perf_data__fd(data);
3539 session->evlist = perf_evlist__new();
3540 if (session->evlist == NULL)
3543 session->evlist->env = &header->env;
3544 session->machines.host.env = &header->env;
3545 if (perf_data__is_pipe(data))
3546 return perf_header__read_pipe(session);
3548 if (perf_file_header__read(&f_header, header, fd) < 0)
3552 * Sanity check that perf.data was written cleanly; data size is
3553 * initialized to 0 and updated only if the on_exit function is run.
3554 * If data size is still 0 then the file contains only partial
3555 * information. Just warn user and process it as much as it can.
3557 if (f_header.data.size == 0) {
3558 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3559 "Was the 'perf record' command properly terminated?\n",
3563 nr_attrs = f_header.attrs.size / f_header.attr_size;
3564 lseek(fd, f_header.attrs.offset, SEEK_SET);
3566 for (i = 0; i < nr_attrs; i++) {
3567 struct perf_evsel *evsel;
3570 if (read_attr(fd, header, &f_attr) < 0)
3573 if (header->needs_swap) {
3574 f_attr.ids.size = bswap_64(f_attr.ids.size);
3575 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3576 perf_event__attr_swap(&f_attr.attr);
3579 tmp = lseek(fd, 0, SEEK_CUR);
3580 evsel = perf_evsel__new(&f_attr.attr);
3583 goto out_delete_evlist;
3585 evsel->needs_swap = header->needs_swap;
3587 * Do it before so that if perf_evsel__alloc_id fails, this
3588 * entry gets purged too at perf_evlist__delete().
3590 perf_evlist__add(session->evlist, evsel);
3592 nr_ids = f_attr.ids.size / sizeof(u64);
3594 * We don't have the cpu and thread maps on the header, so
3595 * for allocating the perf_sample_id table we fake 1 cpu and
3596 * hattr->ids threads.
3598 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3599 goto out_delete_evlist;
3601 lseek(fd, f_attr.ids.offset, SEEK_SET);
3603 for (j = 0; j < nr_ids; j++) {
3604 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3607 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
3610 lseek(fd, tmp, SEEK_SET);
3613 perf_header__process_sections(header, fd, &session->tevent,
3614 perf_file_section__process);
3616 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3617 session->tevent.pevent))
3618 goto out_delete_evlist;
3625 perf_evlist__delete(session->evlist);
3626 session->evlist = NULL;
3630 int perf_event__synthesize_attr(struct perf_tool *tool,
3631 struct perf_event_attr *attr, u32 ids, u64 *id,
3632 perf_event__handler_t process)
3634 union perf_event *ev;
3638 size = sizeof(struct perf_event_attr);
3639 size = PERF_ALIGN(size, sizeof(u64));
3640 size += sizeof(struct perf_event_header);
3641 size += ids * sizeof(u64);
3648 ev->attr.attr = *attr;
3649 memcpy(ev->attr.id, id, ids * sizeof(u64));
3651 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3652 ev->attr.header.size = (u16)size;
3654 if (ev->attr.header.size == size)
3655 err = process(tool, ev, NULL, NULL);
3664 int perf_event__synthesize_features(struct perf_tool *tool,
3665 struct perf_session *session,
3666 struct perf_evlist *evlist,
3667 perf_event__handler_t process)
3669 struct perf_header *header = &session->header;
3671 struct feature_event *fe;
3675 sz_hdr = sizeof(fe->header);
3676 sz = sizeof(union perf_event);
3677 /* get a nice alignment */
3678 sz = PERF_ALIGN(sz, page_size);
3680 memset(&ff, 0, sizeof(ff));
3682 ff.buf = malloc(sz);
3686 ff.size = sz - sz_hdr;
3688 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3689 if (!feat_ops[feat].synthesize) {
3690 pr_debug("No record header feature for header :%d\n", feat);
3694 ff.offset = sizeof(*fe);
3696 ret = feat_ops[feat].write(&ff, evlist);
3697 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3698 pr_debug("Error writing feature\n");
3701 /* ff.buf may have changed due to realloc in do_write() */
3703 memset(fe, 0, sizeof(*fe));
3706 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3707 fe->header.size = ff.offset;
3709 ret = process(tool, ff.buf, NULL, NULL);
3716 /* Send HEADER_LAST_FEATURE mark. */
3718 fe->feat_id = HEADER_LAST_FEATURE;
3719 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3720 fe->header.size = sizeof(*fe);
3722 ret = process(tool, ff.buf, NULL, NULL);
3728 int perf_event__process_feature(struct perf_session *session,
3729 union perf_event *event)
3731 struct perf_tool *tool = session->tool;
3732 struct feat_fd ff = { .fd = 0 };
3733 struct feature_event *fe = (struct feature_event *)event;
3734 int type = fe->header.type;
3735 u64 feat = fe->feat_id;
3737 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3738 pr_warning("invalid record type %d in pipe-mode\n", type);
3741 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3742 pr_warning("invalid record type %d in pipe-mode\n", type);
3746 if (!feat_ops[feat].process)
3749 ff.buf = (void *)fe->data;
3750 ff.size = event->header.size - sizeof(event->header);
3751 ff.ph = &session->header;
3753 if (feat_ops[feat].process(&ff, NULL))
3756 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3759 if (!feat_ops[feat].full_only ||
3760 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3761 feat_ops[feat].print(&ff, stdout);
3763 fprintf(stdout, "# %s info available, use -I to display\n",
3764 feat_ops[feat].name);
3770 static struct event_update_event *
3771 event_update_event__new(size_t size, u64 type, u64 id)
3773 struct event_update_event *ev;
3775 size += sizeof(*ev);
3776 size = PERF_ALIGN(size, sizeof(u64));
3780 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3781 ev->header.size = (u16)size;
3789 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3790 struct perf_evsel *evsel,
3791 perf_event__handler_t process)
3793 struct event_update_event *ev;
3794 size_t size = strlen(evsel->unit);
3797 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3801 strlcpy(ev->data, evsel->unit, size + 1);
3802 err = process(tool, (union perf_event *)ev, NULL, NULL);
3808 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3809 struct perf_evsel *evsel,
3810 perf_event__handler_t process)
3812 struct event_update_event *ev;
3813 struct event_update_event_scale *ev_data;
3816 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3820 ev_data = (struct event_update_event_scale *) ev->data;
3821 ev_data->scale = evsel->scale;
3822 err = process(tool, (union perf_event*) ev, NULL, NULL);
3828 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3829 struct perf_evsel *evsel,
3830 perf_event__handler_t process)
3832 struct event_update_event *ev;
3833 size_t len = strlen(evsel->name);
3836 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3840 strlcpy(ev->data, evsel->name, len + 1);
3841 err = process(tool, (union perf_event*) ev, NULL, NULL);
3847 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3848 struct perf_evsel *evsel,
3849 perf_event__handler_t process)
3851 size_t size = sizeof(struct event_update_event);
3852 struct event_update_event *ev;
3856 if (!evsel->own_cpus)
3859 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3863 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3864 ev->header.size = (u16)size;
3865 ev->type = PERF_EVENT_UPDATE__CPUS;
3866 ev->id = evsel->id[0];
3868 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3872 err = process(tool, (union perf_event*) ev, NULL, NULL);
3877 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3879 struct event_update_event *ev = &event->event_update;
3880 struct event_update_event_scale *ev_scale;
3881 struct event_update_event_cpus *ev_cpus;
3882 struct cpu_map *map;
3885 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3888 case PERF_EVENT_UPDATE__SCALE:
3889 ev_scale = (struct event_update_event_scale *) ev->data;
3890 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3892 case PERF_EVENT_UPDATE__UNIT:
3893 ret += fprintf(fp, "... unit: %s\n", ev->data);
3895 case PERF_EVENT_UPDATE__NAME:
3896 ret += fprintf(fp, "... name: %s\n", ev->data);
3898 case PERF_EVENT_UPDATE__CPUS:
3899 ev_cpus = (struct event_update_event_cpus *) ev->data;
3900 ret += fprintf(fp, "... ");
3902 map = cpu_map__new_data(&ev_cpus->cpus);
3904 ret += cpu_map__fprintf(map, fp);
3906 ret += fprintf(fp, "failed to get cpus\n");
3909 ret += fprintf(fp, "... unknown type\n");
3916 int perf_event__synthesize_attrs(struct perf_tool *tool,
3917 struct perf_evlist *evlist,
3918 perf_event__handler_t process)
3920 struct perf_evsel *evsel;
3923 evlist__for_each_entry(evlist, evsel) {
3924 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3925 evsel->id, process);
3927 pr_debug("failed to create perf header attribute\n");
3935 static bool has_unit(struct perf_evsel *counter)
3937 return counter->unit && *counter->unit;
3940 static bool has_scale(struct perf_evsel *counter)
3942 return counter->scale != 1;
3945 int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3946 struct perf_evlist *evsel_list,
3947 perf_event__handler_t process,
3950 struct perf_evsel *counter;
3954 * Synthesize other events stuff not carried within
3955 * attr event - unit, scale, name
3957 evlist__for_each_entry(evsel_list, counter) {
3958 if (!counter->supported)
3962 * Synthesize unit and scale only if it's defined.
3964 if (has_unit(counter)) {
3965 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3967 pr_err("Couldn't synthesize evsel unit.\n");
3972 if (has_scale(counter)) {
3973 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3975 pr_err("Couldn't synthesize evsel counter.\n");
3980 if (counter->own_cpus) {
3981 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3983 pr_err("Couldn't synthesize evsel cpus.\n");
3989 * Name is needed only for pipe output,
3990 * perf.data carries event names.
3993 err = perf_event__synthesize_event_update_name(tool, counter, process);
3995 pr_err("Couldn't synthesize evsel name.\n");
4003 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4004 union perf_event *event,
4005 struct perf_evlist **pevlist)
4008 struct perf_evsel *evsel;
4009 struct perf_evlist *evlist = *pevlist;
4011 if (evlist == NULL) {
4012 *pevlist = evlist = perf_evlist__new();
4017 evsel = perf_evsel__new(&event->attr.attr);
4021 perf_evlist__add(evlist, evsel);
4023 ids = event->header.size;
4024 ids -= (void *)&event->attr.id - (void *)event;
4025 n_ids = ids / sizeof(u64);
4027 * We don't have the cpu and thread maps on the header, so
4028 * for allocating the perf_sample_id table we fake 1 cpu and
4029 * hattr->ids threads.
4031 if (perf_evsel__alloc_id(evsel, 1, n_ids))
4034 for (i = 0; i < n_ids; i++) {
4035 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
4041 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4042 union perf_event *event,
4043 struct perf_evlist **pevlist)
4045 struct event_update_event *ev = &event->event_update;
4046 struct event_update_event_scale *ev_scale;
4047 struct event_update_event_cpus *ev_cpus;
4048 struct perf_evlist *evlist;
4049 struct perf_evsel *evsel;
4050 struct cpu_map *map;
4052 if (!pevlist || *pevlist == NULL)
4057 evsel = perf_evlist__id2evsel(evlist, ev->id);
4062 case PERF_EVENT_UPDATE__UNIT:
4063 evsel->unit = strdup(ev->data);
4065 case PERF_EVENT_UPDATE__NAME:
4066 evsel->name = strdup(ev->data);
4068 case PERF_EVENT_UPDATE__SCALE:
4069 ev_scale = (struct event_update_event_scale *) ev->data;
4070 evsel->scale = ev_scale->scale;
4072 case PERF_EVENT_UPDATE__CPUS:
4073 ev_cpus = (struct event_update_event_cpus *) ev->data;
4075 map = cpu_map__new_data(&ev_cpus->cpus);
4077 evsel->own_cpus = map;
4079 pr_err("failed to get event_update cpus\n");
4087 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
4088 struct perf_evlist *evlist,
4089 perf_event__handler_t process)
4091 union perf_event ev;
4092 struct tracing_data *tdata;
4093 ssize_t size = 0, aligned_size = 0, padding;
4095 int err __maybe_unused = 0;
4098 * We are going to store the size of the data followed
4099 * by the data contents. Since the fd descriptor is a pipe,
4100 * we cannot seek back to store the size of the data once
4101 * we know it. Instead we:
4103 * - write the tracing data to the temp file
4104 * - get/write the data size to pipe
4105 * - write the tracing data from the temp file
4108 tdata = tracing_data_get(&evlist->entries, fd, true);
4112 memset(&ev, 0, sizeof(ev));
4114 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
4116 aligned_size = PERF_ALIGN(size, sizeof(u64));
4117 padding = aligned_size - size;
4118 ev.tracing_data.header.size = sizeof(ev.tracing_data);
4119 ev.tracing_data.size = aligned_size;
4121 process(tool, &ev, NULL, NULL);
4124 * The put function will copy all the tracing data
4125 * stored in temp file to the pipe.
4127 tracing_data_put(tdata);
4129 ff = (struct feat_fd){ .fd = fd };
4130 if (write_padded(&ff, NULL, 0, padding))
4133 return aligned_size;
4136 int perf_event__process_tracing_data(struct perf_session *session,
4137 union perf_event *event)
4139 ssize_t size_read, padding, size = event->tracing_data.size;
4140 int fd = perf_data__fd(session->data);
4141 off_t offset = lseek(fd, 0, SEEK_CUR);
4144 /* setup for reading amidst mmap */
4145 lseek(fd, offset + sizeof(struct tracing_data_event),
4148 size_read = trace_report(fd, &session->tevent,
4150 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4152 if (readn(fd, buf, padding) < 0) {
4153 pr_err("%s: reading input file", __func__);
4156 if (session->repipe) {
4157 int retw = write(STDOUT_FILENO, buf, padding);
4158 if (retw <= 0 || retw != padding) {
4159 pr_err("%s: repiping tracing data padding", __func__);
4164 if (size_read + padding != size) {
4165 pr_err("%s: tracing data size mismatch", __func__);
4169 perf_evlist__prepare_tracepoint_events(session->evlist,
4170 session->tevent.pevent);
4172 return size_read + padding;
4175 int perf_event__synthesize_build_id(struct perf_tool *tool,
4176 struct dso *pos, u16 misc,
4177 perf_event__handler_t process,
4178 struct machine *machine)
4180 union perf_event ev;
4187 memset(&ev, 0, sizeof(ev));
4189 len = pos->long_name_len + 1;
4190 len = PERF_ALIGN(len, NAME_ALIGN);
4191 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
4192 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
4193 ev.build_id.header.misc = misc;
4194 ev.build_id.pid = machine->pid;
4195 ev.build_id.header.size = sizeof(ev.build_id) + len;
4196 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
4198 err = process(tool, &ev, NULL, machine);
4203 int perf_event__process_build_id(struct perf_session *session,
4204 union perf_event *event)
4206 __event_process_build_id(&event->build_id,
4207 event->build_id.filename,