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>
27 #include "trace-event.h"
37 #include <api/fs/fs.h>
40 #include "time-utils.h"
44 #include "sane_ctype.h"
48 * must be a numerical value to let the endianness
49 * determine the memory layout. That way we are able
50 * to detect endianness when reading the perf.data file
53 * we check for legacy (PERFFILE) format.
55 static const char *__perf_magic1 = "PERFFILE";
56 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
57 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
59 #define PERF_MAGIC __perf_magic2
61 const char perf_version_string[] = PERF_VERSION;
63 struct perf_file_attr {
64 struct perf_event_attr attr;
65 struct perf_file_section ids;
69 struct perf_header *ph;
71 void *buf; /* Either buf != NULL or fd >= 0 */
74 struct perf_evsel *events;
77 void perf_header__set_feat(struct perf_header *header, int feat)
79 set_bit(feat, header->adds_features);
82 void perf_header__clear_feat(struct perf_header *header, int feat)
84 clear_bit(feat, header->adds_features);
87 bool perf_header__has_feat(const struct perf_header *header, int feat)
89 return test_bit(feat, header->adds_features);
92 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
94 ssize_t ret = writen(ff->fd, buf, size);
96 if (ret != (ssize_t)size)
97 return ret < 0 ? (int)ret : -1;
101 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
103 /* struct perf_event_header::size is u16 */
104 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
105 size_t new_size = ff->size;
108 if (size + ff->offset > max_size)
111 while (size > (new_size - ff->offset))
113 new_size = min(max_size, new_size);
115 if (ff->size < new_size) {
116 addr = realloc(ff->buf, new_size);
123 memcpy(ff->buf + ff->offset, buf, size);
129 /* Return: 0 if succeded, -ERR if failed. */
130 int do_write(struct feat_fd *ff, const void *buf, size_t size)
133 return __do_write_fd(ff, buf, size);
134 return __do_write_buf(ff, buf, size);
137 /* Return: 0 if succeded, -ERR if failed. */
138 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
140 u64 *p = (u64 *) set;
143 ret = do_write(ff, &size, sizeof(size));
147 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
148 ret = do_write(ff, p + i, sizeof(*p));
156 /* Return: 0 if succeded, -ERR if failed. */
157 int write_padded(struct feat_fd *ff, const void *bf,
158 size_t count, size_t count_aligned)
160 static const char zero_buf[NAME_ALIGN];
161 int err = do_write(ff, bf, count);
164 err = do_write(ff, zero_buf, count_aligned - count);
169 #define string_size(str) \
170 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
172 /* Return: 0 if succeded, -ERR if failed. */
173 static int do_write_string(struct feat_fd *ff, const char *str)
178 olen = strlen(str) + 1;
179 len = PERF_ALIGN(olen, NAME_ALIGN);
181 /* write len, incl. \0 */
182 ret = do_write(ff, &len, sizeof(len));
186 return write_padded(ff, str, olen, len);
189 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
191 ssize_t ret = readn(ff->fd, addr, size);
194 return ret < 0 ? (int)ret : -1;
198 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
200 if (size > (ssize_t)ff->size - ff->offset)
203 memcpy(addr, ff->buf + ff->offset, size);
210 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
213 return __do_read_fd(ff, addr, size);
214 return __do_read_buf(ff, addr, size);
217 static int do_read_u32(struct feat_fd *ff, u32 *addr)
221 ret = __do_read(ff, addr, sizeof(*addr));
225 if (ff->ph->needs_swap)
226 *addr = bswap_32(*addr);
230 static int do_read_u64(struct feat_fd *ff, u64 *addr)
234 ret = __do_read(ff, addr, sizeof(*addr));
238 if (ff->ph->needs_swap)
239 *addr = bswap_64(*addr);
243 static char *do_read_string(struct feat_fd *ff)
248 if (do_read_u32(ff, &len))
255 if (!__do_read(ff, buf, len)) {
257 * strings are padded by zeroes
258 * thus the actual strlen of buf
259 * may be less than len
268 /* Return: 0 if succeded, -ERR if failed. */
269 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
275 ret = do_read_u64(ff, &size);
279 set = bitmap_alloc(size);
285 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
286 ret = do_read_u64(ff, p + i);
298 static int write_tracing_data(struct feat_fd *ff,
299 struct perf_evlist *evlist)
301 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
304 return read_tracing_data(ff->fd, &evlist->entries);
307 static int write_build_id(struct feat_fd *ff,
308 struct perf_evlist *evlist __maybe_unused)
310 struct perf_session *session;
313 session = container_of(ff->ph, struct perf_session, header);
315 if (!perf_session__read_build_ids(session, true))
318 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
321 err = perf_session__write_buildid_table(session, ff);
323 pr_debug("failed to write buildid table\n");
326 perf_session__cache_build_ids(session);
331 static int write_hostname(struct feat_fd *ff,
332 struct perf_evlist *evlist __maybe_unused)
341 return do_write_string(ff, uts.nodename);
344 static int write_osrelease(struct feat_fd *ff,
345 struct perf_evlist *evlist __maybe_unused)
354 return do_write_string(ff, uts.release);
357 static int write_arch(struct feat_fd *ff,
358 struct perf_evlist *evlist __maybe_unused)
367 return do_write_string(ff, uts.machine);
370 static int write_version(struct feat_fd *ff,
371 struct perf_evlist *evlist __maybe_unused)
373 return do_write_string(ff, perf_version_string);
376 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
381 const char *search = cpuinfo_proc;
388 file = fopen("/proc/cpuinfo", "r");
392 while (getline(&buf, &len, file) > 0) {
393 ret = strncmp(buf, search, strlen(search));
405 p = strchr(buf, ':');
406 if (p && *(p+1) == ' ' && *(p+2))
412 /* squash extra space characters (branding string) */
419 while (*q && isspace(*q))
422 while ((*r++ = *q++));
426 ret = do_write_string(ff, s);
433 static int write_cpudesc(struct feat_fd *ff,
434 struct perf_evlist *evlist __maybe_unused)
436 const char *cpuinfo_procs[] = CPUINFO_PROC;
439 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
441 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
449 static int write_nrcpus(struct feat_fd *ff,
450 struct perf_evlist *evlist __maybe_unused)
456 nrc = cpu__max_present_cpu();
458 nr = sysconf(_SC_NPROCESSORS_ONLN);
462 nra = (u32)(nr & UINT_MAX);
464 ret = do_write(ff, &nrc, sizeof(nrc));
468 return do_write(ff, &nra, sizeof(nra));
471 static int write_event_desc(struct feat_fd *ff,
472 struct perf_evlist *evlist)
474 struct perf_evsel *evsel;
478 nre = evlist->nr_entries;
481 * write number of events
483 ret = do_write(ff, &nre, sizeof(nre));
488 * size of perf_event_attr struct
490 sz = (u32)sizeof(evsel->attr);
491 ret = do_write(ff, &sz, sizeof(sz));
495 evlist__for_each_entry(evlist, evsel) {
496 ret = do_write(ff, &evsel->attr, sz);
500 * write number of unique id per event
501 * there is one id per instance of an event
503 * copy into an nri to be independent of the
507 ret = do_write(ff, &nri, sizeof(nri));
512 * write event string as passed on cmdline
514 ret = do_write_string(ff, perf_evsel__name(evsel));
518 * write unique ids for this event
520 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
527 static int write_cmdline(struct feat_fd *ff,
528 struct perf_evlist *evlist __maybe_unused)
530 char pbuf[MAXPATHLEN], *buf;
533 /* actual path to perf binary */
534 buf = perf_exe(pbuf, MAXPATHLEN);
536 /* account for binary path */
537 n = perf_env.nr_cmdline + 1;
539 ret = do_write(ff, &n, sizeof(n));
543 ret = do_write_string(ff, buf);
547 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
548 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
556 static int write_cpu_topology(struct feat_fd *ff,
557 struct perf_evlist *evlist __maybe_unused)
559 struct cpu_topology *tp;
563 tp = cpu_topology__new();
567 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
571 for (i = 0; i < tp->core_sib; i++) {
572 ret = do_write_string(ff, tp->core_siblings[i]);
576 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
580 for (i = 0; i < tp->thread_sib; i++) {
581 ret = do_write_string(ff, tp->thread_siblings[i]);
586 ret = perf_env__read_cpu_topology_map(&perf_env);
590 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
591 ret = do_write(ff, &perf_env.cpu[j].core_id,
592 sizeof(perf_env.cpu[j].core_id));
595 ret = do_write(ff, &perf_env.cpu[j].socket_id,
596 sizeof(perf_env.cpu[j].socket_id));
601 cpu_topology__delete(tp);
607 static int write_total_mem(struct feat_fd *ff,
608 struct perf_evlist *evlist __maybe_unused)
616 fp = fopen("/proc/meminfo", "r");
620 while (getline(&buf, &len, fp) > 0) {
621 ret = strncmp(buf, "MemTotal:", 9);
626 n = sscanf(buf, "%*s %"PRIu64, &mem);
628 ret = do_write(ff, &mem, sizeof(mem));
636 static int write_numa_topology(struct feat_fd *ff,
637 struct perf_evlist *evlist __maybe_unused)
639 struct numa_topology *tp;
643 tp = numa_topology__new();
647 ret = do_write(ff, &tp->nr, sizeof(u32));
651 for (i = 0; i < tp->nr; i++) {
652 struct numa_topology_node *n = &tp->nodes[i];
654 ret = do_write(ff, &n->node, sizeof(u32));
658 ret = do_write(ff, &n->mem_total, sizeof(u64));
662 ret = do_write(ff, &n->mem_free, sizeof(u64));
666 ret = do_write_string(ff, n->cpus);
674 numa_topology__delete(tp);
681 * struct pmu_mappings {
690 static int write_pmu_mappings(struct feat_fd *ff,
691 struct perf_evlist *evlist __maybe_unused)
693 struct perf_pmu *pmu = NULL;
698 * Do a first pass to count number of pmu to avoid lseek so this
699 * works in pipe mode as well.
701 while ((pmu = perf_pmu__scan(pmu))) {
707 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
711 while ((pmu = perf_pmu__scan(pmu))) {
715 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
719 ret = do_write_string(ff, pmu->name);
730 * struct group_descs {
732 * struct group_desc {
739 static int write_group_desc(struct feat_fd *ff,
740 struct perf_evlist *evlist)
742 u32 nr_groups = evlist->nr_groups;
743 struct perf_evsel *evsel;
746 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
750 evlist__for_each_entry(evlist, evsel) {
751 if (perf_evsel__is_group_leader(evsel) &&
752 evsel->nr_members > 1) {
753 const char *name = evsel->group_name ?: "{anon_group}";
754 u32 leader_idx = evsel->idx;
755 u32 nr_members = evsel->nr_members;
757 ret = do_write_string(ff, name);
761 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
765 ret = do_write(ff, &nr_members, sizeof(nr_members));
774 * Return the CPU id as a raw string.
776 * Each architecture should provide a more precise id string that
777 * can be use to match the architecture's "mapfile".
779 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
784 /* Return zero when the cpuid from the mapfile.csv matches the
785 * cpuid string generated on this platform.
786 * Otherwise return non-zero.
788 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
791 regmatch_t pmatch[1];
794 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
795 /* Warn unable to generate match particular string. */
796 pr_info("Invalid regular expression %s\n", mapcpuid);
800 match = !regexec(&re, cpuid, 1, pmatch, 0);
803 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
805 /* Verify the entire string matched. */
806 if (match_len == strlen(cpuid))
813 * default get_cpuid(): nothing gets recorded
814 * actual implementation must be in arch/$(SRCARCH)/util/header.c
816 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
821 static int write_cpuid(struct feat_fd *ff,
822 struct perf_evlist *evlist __maybe_unused)
827 ret = get_cpuid(buffer, sizeof(buffer));
831 return do_write_string(ff, buffer);
834 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
835 struct perf_evlist *evlist __maybe_unused)
840 static int write_auxtrace(struct feat_fd *ff,
841 struct perf_evlist *evlist __maybe_unused)
843 struct perf_session *session;
846 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
849 session = container_of(ff->ph, struct perf_session, header);
851 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
853 pr_err("Failed to write auxtrace index\n");
857 static int write_clockid(struct feat_fd *ff,
858 struct perf_evlist *evlist __maybe_unused)
860 return do_write(ff, &ff->ph->env.clockid_res_ns,
861 sizeof(ff->ph->env.clockid_res_ns));
864 static int cpu_cache_level__sort(const void *a, const void *b)
866 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
867 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
869 return cache_a->level - cache_b->level;
872 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
874 if (a->level != b->level)
877 if (a->line_size != b->line_size)
880 if (a->sets != b->sets)
883 if (a->ways != b->ways)
886 if (strcmp(a->type, b->type))
889 if (strcmp(a->size, b->size))
892 if (strcmp(a->map, b->map))
898 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
900 char path[PATH_MAX], file[PATH_MAX];
904 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
905 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
910 scnprintf(file, PATH_MAX, "%s/level", path);
911 if (sysfs__read_int(file, (int *) &cache->level))
914 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
915 if (sysfs__read_int(file, (int *) &cache->line_size))
918 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
919 if (sysfs__read_int(file, (int *) &cache->sets))
922 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
923 if (sysfs__read_int(file, (int *) &cache->ways))
926 scnprintf(file, PATH_MAX, "%s/type", path);
927 if (sysfs__read_str(file, &cache->type, &len))
930 cache->type[len] = 0;
931 cache->type = rtrim(cache->type);
933 scnprintf(file, PATH_MAX, "%s/size", path);
934 if (sysfs__read_str(file, &cache->size, &len)) {
939 cache->size[len] = 0;
940 cache->size = rtrim(cache->size);
942 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
943 if (sysfs__read_str(file, &cache->map, &len)) {
950 cache->map = rtrim(cache->map);
954 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
956 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
959 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
966 ncpus = sysconf(_SC_NPROCESSORS_CONF);
970 nr = (u32)(ncpus & UINT_MAX);
972 for (cpu = 0; cpu < nr; cpu++) {
973 for (level = 0; level < 10; level++) {
974 struct cpu_cache_level c;
977 err = cpu_cache_level__read(&c, cpu, level);
984 for (i = 0; i < cnt; i++) {
985 if (cpu_cache_level__cmp(&c, &caches[i]))
992 cpu_cache_level__free(&c);
994 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1003 #define MAX_CACHES 2000
1005 static int write_cache(struct feat_fd *ff,
1006 struct perf_evlist *evlist __maybe_unused)
1008 struct cpu_cache_level caches[MAX_CACHES];
1009 u32 cnt = 0, i, version = 1;
1012 ret = build_caches(caches, MAX_CACHES, &cnt);
1016 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1018 ret = do_write(ff, &version, sizeof(u32));
1022 ret = do_write(ff, &cnt, sizeof(u32));
1026 for (i = 0; i < cnt; i++) {
1027 struct cpu_cache_level *c = &caches[i];
1030 ret = do_write(ff, &c->v, sizeof(u32)); \
1041 ret = do_write_string(ff, (const char *) c->v); \
1052 for (i = 0; i < cnt; i++)
1053 cpu_cache_level__free(&caches[i]);
1057 static int write_stat(struct feat_fd *ff __maybe_unused,
1058 struct perf_evlist *evlist __maybe_unused)
1063 static int write_sample_time(struct feat_fd *ff,
1064 struct perf_evlist *evlist)
1068 ret = do_write(ff, &evlist->first_sample_time,
1069 sizeof(evlist->first_sample_time));
1073 return do_write(ff, &evlist->last_sample_time,
1074 sizeof(evlist->last_sample_time));
1078 static int memory_node__read(struct memory_node *n, unsigned long idx)
1080 unsigned int phys, size = 0;
1081 char path[PATH_MAX];
1085 #define for_each_memory(mem, dir) \
1086 while ((ent = readdir(dir))) \
1087 if (strcmp(ent->d_name, ".") && \
1088 strcmp(ent->d_name, "..") && \
1089 sscanf(ent->d_name, "memory%u", &mem) == 1)
1091 scnprintf(path, PATH_MAX,
1092 "%s/devices/system/node/node%lu",
1093 sysfs__mountpoint(), idx);
1095 dir = opendir(path);
1097 pr_warning("failed: cant' open memory sysfs data\n");
1101 for_each_memory(phys, dir) {
1102 size = max(phys, size);
1107 n->set = bitmap_alloc(size);
1118 for_each_memory(phys, dir) {
1119 set_bit(phys, n->set);
1126 static int memory_node__sort(const void *a, const void *b)
1128 const struct memory_node *na = a;
1129 const struct memory_node *nb = b;
1131 return na->node - nb->node;
1134 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1136 char path[PATH_MAX];
1142 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1143 sysfs__mountpoint());
1145 dir = opendir(path);
1147 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1152 while (!ret && (ent = readdir(dir))) {
1156 if (!strcmp(ent->d_name, ".") ||
1157 !strcmp(ent->d_name, ".."))
1160 r = sscanf(ent->d_name, "node%u", &idx);
1164 if (WARN_ONCE(cnt >= size,
1165 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1168 ret = memory_node__read(&nodes[cnt++], idx);
1175 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1180 #define MAX_MEMORY_NODES 2000
1183 * The MEM_TOPOLOGY holds physical memory map for every
1184 * node in system. The format of data is as follows:
1186 * 0 - version | for future changes
1187 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1188 * 16 - count | number of nodes
1190 * For each node we store map of physical indexes for
1193 * 32 - node id | node index
1194 * 40 - size | size of bitmap
1195 * 48 - bitmap | bitmap of memory indexes that belongs to node
1197 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1198 struct perf_evlist *evlist __maybe_unused)
1200 static struct memory_node nodes[MAX_MEMORY_NODES];
1201 u64 bsize, version = 1, i, nr;
1204 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1205 (unsigned long long *) &bsize);
1209 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1213 ret = do_write(ff, &version, sizeof(version));
1217 ret = do_write(ff, &bsize, sizeof(bsize));
1221 ret = do_write(ff, &nr, sizeof(nr));
1225 for (i = 0; i < nr; i++) {
1226 struct memory_node *n = &nodes[i];
1229 ret = do_write(ff, &n->v, sizeof(n->v)); \
1238 ret = do_write_bitmap(ff, n->set, n->size);
1247 static void print_hostname(struct feat_fd *ff, FILE *fp)
1249 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1252 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1254 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1257 static void print_arch(struct feat_fd *ff, FILE *fp)
1259 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1262 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1264 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1267 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1269 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1270 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1273 static void print_version(struct feat_fd *ff, FILE *fp)
1275 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1278 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1282 nr = ff->ph->env.nr_cmdline;
1284 fprintf(fp, "# cmdline : ");
1286 for (i = 0; i < nr; i++) {
1287 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1289 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1293 char *quote = strchr(argv_i, '\'');
1297 fprintf(fp, "%s\\\'", argv_i);
1300 fprintf(fp, "%s ", argv_i);
1307 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1309 struct perf_header *ph = ff->ph;
1310 int cpu_nr = ph->env.nr_cpus_avail;
1314 nr = ph->env.nr_sibling_cores;
1315 str = ph->env.sibling_cores;
1317 for (i = 0; i < nr; i++) {
1318 fprintf(fp, "# sibling cores : %s\n", str);
1319 str += strlen(str) + 1;
1322 nr = ph->env.nr_sibling_threads;
1323 str = ph->env.sibling_threads;
1325 for (i = 0; i < nr; i++) {
1326 fprintf(fp, "# sibling threads : %s\n", str);
1327 str += strlen(str) + 1;
1330 if (ph->env.cpu != NULL) {
1331 for (i = 0; i < cpu_nr; i++)
1332 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1333 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1335 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1338 static void print_clockid(struct feat_fd *ff, FILE *fp)
1340 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1341 ff->ph->env.clockid_res_ns * 1000);
1344 static void free_event_desc(struct perf_evsel *events)
1346 struct perf_evsel *evsel;
1351 for (evsel = events; evsel->attr.size; evsel++) {
1352 zfree(&evsel->name);
1359 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1361 struct perf_evsel *evsel, *events = NULL;
1364 u32 nre, sz, nr, i, j;
1367 /* number of events */
1368 if (do_read_u32(ff, &nre))
1371 if (do_read_u32(ff, &sz))
1374 /* buffer to hold on file attr struct */
1379 /* the last event terminates with evsel->attr.size == 0: */
1380 events = calloc(nre + 1, sizeof(*events));
1384 msz = sizeof(evsel->attr);
1388 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1392 * must read entire on-file attr struct to
1393 * sync up with layout.
1395 if (__do_read(ff, buf, sz))
1398 if (ff->ph->needs_swap)
1399 perf_event__attr_swap(buf);
1401 memcpy(&evsel->attr, buf, msz);
1403 if (do_read_u32(ff, &nr))
1406 if (ff->ph->needs_swap)
1407 evsel->needs_swap = true;
1409 evsel->name = do_read_string(ff);
1416 id = calloc(nr, sizeof(*id));
1422 for (j = 0 ; j < nr; j++) {
1423 if (do_read_u64(ff, id))
1432 free_event_desc(events);
1437 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1438 void *priv __maybe_unused)
1440 return fprintf(fp, ", %s = %s", name, val);
1443 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1445 struct perf_evsel *evsel, *events;
1450 events = ff->events;
1452 events = read_event_desc(ff);
1455 fprintf(fp, "# event desc: not available or unable to read\n");
1459 for (evsel = events; evsel->attr.size; evsel++) {
1460 fprintf(fp, "# event : name = %s, ", evsel->name);
1463 fprintf(fp, ", id = {");
1464 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1467 fprintf(fp, " %"PRIu64, *id);
1472 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1477 free_event_desc(events);
1481 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1483 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1486 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1489 struct numa_node *n;
1491 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1492 n = &ff->ph->env.numa_nodes[i];
1494 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1495 " free = %"PRIu64" kB\n",
1496 n->node, n->mem_total, n->mem_free);
1498 fprintf(fp, "# node%u cpu list : ", n->node);
1499 cpu_map__fprintf(n->map, fp);
1503 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1505 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1508 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1510 fprintf(fp, "# contains samples with branch stack\n");
1513 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1515 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1518 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1520 fprintf(fp, "# contains stat data\n");
1523 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1527 fprintf(fp, "# CPU cache info:\n");
1528 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1530 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1534 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1536 const char *delimiter = "# pmu mappings: ";
1541 pmu_num = ff->ph->env.nr_pmu_mappings;
1543 fprintf(fp, "# pmu mappings: not available\n");
1547 str = ff->ph->env.pmu_mappings;
1550 type = strtoul(str, &tmp, 0);
1555 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1558 str += strlen(str) + 1;
1567 fprintf(fp, "# pmu mappings: unable to read\n");
1570 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1572 struct perf_session *session;
1573 struct perf_evsel *evsel;
1576 session = container_of(ff->ph, struct perf_session, header);
1578 evlist__for_each_entry(session->evlist, evsel) {
1579 if (perf_evsel__is_group_leader(evsel) &&
1580 evsel->nr_members > 1) {
1581 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1582 perf_evsel__name(evsel));
1584 nr = evsel->nr_members - 1;
1586 fprintf(fp, ",%s", perf_evsel__name(evsel));
1594 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1596 struct perf_session *session;
1600 session = container_of(ff->ph, struct perf_session, header);
1602 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1603 time_buf, sizeof(time_buf));
1604 fprintf(fp, "# time of first sample : %s\n", time_buf);
1606 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1607 time_buf, sizeof(time_buf));
1608 fprintf(fp, "# time of last sample : %s\n", time_buf);
1610 d = (double)(session->evlist->last_sample_time -
1611 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1613 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1616 static void memory_node__fprintf(struct memory_node *n,
1617 unsigned long long bsize, FILE *fp)
1619 char buf_map[100], buf_size[50];
1620 unsigned long long size;
1622 size = bsize * bitmap_weight(n->set, n->size);
1623 unit_number__scnprintf(buf_size, 50, size);
1625 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1626 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1629 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1631 struct memory_node *nodes;
1634 nodes = ff->ph->env.memory_nodes;
1635 nr = ff->ph->env.nr_memory_nodes;
1637 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1638 nr, ff->ph->env.memory_bsize);
1640 for (i = 0; i < nr; i++) {
1641 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1645 static int __event_process_build_id(struct build_id_event *bev,
1647 struct perf_session *session)
1650 struct machine *machine;
1653 enum dso_kernel_type dso_type;
1655 machine = perf_session__findnew_machine(session, bev->pid);
1659 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1662 case PERF_RECORD_MISC_KERNEL:
1663 dso_type = DSO_TYPE_KERNEL;
1665 case PERF_RECORD_MISC_GUEST_KERNEL:
1666 dso_type = DSO_TYPE_GUEST_KERNEL;
1668 case PERF_RECORD_MISC_USER:
1669 case PERF_RECORD_MISC_GUEST_USER:
1670 dso_type = DSO_TYPE_USER;
1676 dso = machine__findnew_dso(machine, filename);
1678 char sbuild_id[SBUILD_ID_SIZE];
1680 dso__set_build_id(dso, &bev->build_id);
1682 if (dso_type != DSO_TYPE_USER) {
1683 struct kmod_path m = { .name = NULL, };
1685 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1686 dso__set_module_info(dso, &m, machine);
1688 dso->kernel = dso_type;
1693 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1695 pr_debug("build id event received for %s: %s\n",
1696 dso->long_name, sbuild_id);
1705 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1706 int input, u64 offset, u64 size)
1708 struct perf_session *session = container_of(header, struct perf_session, header);
1710 struct perf_event_header header;
1711 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1714 struct build_id_event bev;
1715 char filename[PATH_MAX];
1716 u64 limit = offset + size;
1718 while (offset < limit) {
1721 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1724 if (header->needs_swap)
1725 perf_event_header__bswap(&old_bev.header);
1727 len = old_bev.header.size - sizeof(old_bev);
1728 if (readn(input, filename, len) != len)
1731 bev.header = old_bev.header;
1734 * As the pid is the missing value, we need to fill
1735 * it properly. The header.misc value give us nice hint.
1737 bev.pid = HOST_KERNEL_ID;
1738 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1739 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1740 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1742 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1743 __event_process_build_id(&bev, filename, session);
1745 offset += bev.header.size;
1751 static int perf_header__read_build_ids(struct perf_header *header,
1752 int input, u64 offset, u64 size)
1754 struct perf_session *session = container_of(header, struct perf_session, header);
1755 struct build_id_event bev;
1756 char filename[PATH_MAX];
1757 u64 limit = offset + size, orig_offset = offset;
1760 while (offset < limit) {
1763 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1766 if (header->needs_swap)
1767 perf_event_header__bswap(&bev.header);
1769 len = bev.header.size - sizeof(bev);
1770 if (readn(input, filename, len) != len)
1773 * The a1645ce1 changeset:
1775 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1777 * Added a field to struct build_id_event that broke the file
1780 * Since the kernel build-id is the first entry, process the
1781 * table using the old format if the well known
1782 * '[kernel.kallsyms]' string for the kernel build-id has the
1783 * first 4 characters chopped off (where the pid_t sits).
1785 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1786 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1788 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1791 __event_process_build_id(&bev, filename, session);
1793 offset += bev.header.size;
1800 /* Macro for features that simply need to read and store a string. */
1801 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1802 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1804 ff->ph->env.__feat_env = do_read_string(ff); \
1805 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1808 FEAT_PROCESS_STR_FUN(hostname, hostname);
1809 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1810 FEAT_PROCESS_STR_FUN(version, version);
1811 FEAT_PROCESS_STR_FUN(arch, arch);
1812 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1813 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1815 static int process_tracing_data(struct feat_fd *ff, void *data)
1817 ssize_t ret = trace_report(ff->fd, data, false);
1819 return ret < 0 ? -1 : 0;
1822 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1824 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1825 pr_debug("Failed to read buildids, continuing...\n");
1829 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1832 u32 nr_cpus_avail, nr_cpus_online;
1834 ret = do_read_u32(ff, &nr_cpus_avail);
1838 ret = do_read_u32(ff, &nr_cpus_online);
1841 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1842 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
1846 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
1851 ret = do_read_u64(ff, &total_mem);
1854 ff->ph->env.total_mem = (unsigned long long)total_mem;
1858 static struct perf_evsel *
1859 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1861 struct perf_evsel *evsel;
1863 evlist__for_each_entry(evlist, evsel) {
1864 if (evsel->idx == idx)
1872 perf_evlist__set_event_name(struct perf_evlist *evlist,
1873 struct perf_evsel *event)
1875 struct perf_evsel *evsel;
1880 evsel = perf_evlist__find_by_index(evlist, event->idx);
1887 evsel->name = strdup(event->name);
1891 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
1893 struct perf_session *session;
1894 struct perf_evsel *evsel, *events = read_event_desc(ff);
1899 session = container_of(ff->ph, struct perf_session, header);
1901 if (session->data->is_pipe) {
1902 /* Save events for reading later by print_event_desc,
1903 * since they can't be read again in pipe mode. */
1904 ff->events = events;
1907 for (evsel = events; evsel->attr.size; evsel++)
1908 perf_evlist__set_event_name(session->evlist, evsel);
1910 if (!session->data->is_pipe)
1911 free_event_desc(events);
1916 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
1918 char *str, *cmdline = NULL, **argv = NULL;
1921 if (do_read_u32(ff, &nr))
1924 ff->ph->env.nr_cmdline = nr;
1926 cmdline = zalloc(ff->size + nr + 1);
1930 argv = zalloc(sizeof(char *) * (nr + 1));
1934 for (i = 0; i < nr; i++) {
1935 str = do_read_string(ff);
1939 argv[i] = cmdline + len;
1940 memcpy(argv[i], str, strlen(str) + 1);
1941 len += strlen(str) + 1;
1944 ff->ph->env.cmdline = cmdline;
1945 ff->ph->env.cmdline_argv = (const char **) argv;
1954 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
1959 int cpu_nr = ff->ph->env.nr_cpus_avail;
1961 struct perf_header *ph = ff->ph;
1962 bool do_core_id_test = true;
1964 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1968 if (do_read_u32(ff, &nr))
1971 ph->env.nr_sibling_cores = nr;
1972 size += sizeof(u32);
1973 if (strbuf_init(&sb, 128) < 0)
1976 for (i = 0; i < nr; i++) {
1977 str = do_read_string(ff);
1981 /* include a NULL character at the end */
1982 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1984 size += string_size(str);
1987 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1989 if (do_read_u32(ff, &nr))
1992 ph->env.nr_sibling_threads = nr;
1993 size += sizeof(u32);
1995 for (i = 0; i < nr; i++) {
1996 str = do_read_string(ff);
2000 /* include a NULL character at the end */
2001 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2003 size += string_size(str);
2006 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2009 * The header may be from old perf,
2010 * which doesn't include core id and socket id information.
2012 if (ff->size <= size) {
2013 zfree(&ph->env.cpu);
2017 /* On s390 the socket_id number is not related to the numbers of cpus.
2018 * The socket_id number might be higher than the numbers of cpus.
2019 * This depends on the configuration.
2021 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
2022 do_core_id_test = false;
2024 for (i = 0; i < (u32)cpu_nr; i++) {
2025 if (do_read_u32(ff, &nr))
2028 ph->env.cpu[i].core_id = nr;
2030 if (do_read_u32(ff, &nr))
2033 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2034 pr_debug("socket_id number is too big."
2035 "You may need to upgrade the perf tool.\n");
2039 ph->env.cpu[i].socket_id = nr;
2045 strbuf_release(&sb);
2047 zfree(&ph->env.cpu);
2051 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2053 struct numa_node *nodes, *n;
2058 if (do_read_u32(ff, &nr))
2061 nodes = zalloc(sizeof(*nodes) * nr);
2065 for (i = 0; i < nr; i++) {
2069 if (do_read_u32(ff, &n->node))
2072 if (do_read_u64(ff, &n->mem_total))
2075 if (do_read_u64(ff, &n->mem_free))
2078 str = do_read_string(ff);
2082 n->map = cpu_map__new(str);
2088 ff->ph->env.nr_numa_nodes = nr;
2089 ff->ph->env.numa_nodes = nodes;
2097 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2104 if (do_read_u32(ff, &pmu_num))
2108 pr_debug("pmu mappings not available\n");
2112 ff->ph->env.nr_pmu_mappings = pmu_num;
2113 if (strbuf_init(&sb, 128) < 0)
2117 if (do_read_u32(ff, &type))
2120 name = do_read_string(ff);
2124 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2126 /* include a NULL character at the end */
2127 if (strbuf_add(&sb, "", 1) < 0)
2130 if (!strcmp(name, "msr"))
2131 ff->ph->env.msr_pmu_type = type;
2136 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2140 strbuf_release(&sb);
2144 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2147 u32 i, nr, nr_groups;
2148 struct perf_session *session;
2149 struct perf_evsel *evsel, *leader = NULL;
2156 if (do_read_u32(ff, &nr_groups))
2159 ff->ph->env.nr_groups = nr_groups;
2161 pr_debug("group desc not available\n");
2165 desc = calloc(nr_groups, sizeof(*desc));
2169 for (i = 0; i < nr_groups; i++) {
2170 desc[i].name = do_read_string(ff);
2174 if (do_read_u32(ff, &desc[i].leader_idx))
2177 if (do_read_u32(ff, &desc[i].nr_members))
2182 * Rebuild group relationship based on the group_desc
2184 session = container_of(ff->ph, struct perf_session, header);
2185 session->evlist->nr_groups = nr_groups;
2188 evlist__for_each_entry(session->evlist, evsel) {
2189 if (evsel->idx == (int) desc[i].leader_idx) {
2190 evsel->leader = evsel;
2191 /* {anon_group} is a dummy name */
2192 if (strcmp(desc[i].name, "{anon_group}")) {
2193 evsel->group_name = desc[i].name;
2194 desc[i].name = NULL;
2196 evsel->nr_members = desc[i].nr_members;
2198 if (i >= nr_groups || nr > 0) {
2199 pr_debug("invalid group desc\n");
2204 nr = evsel->nr_members - 1;
2207 /* This is a group member */
2208 evsel->leader = leader;
2214 if (i != nr_groups || nr != 0) {
2215 pr_debug("invalid group desc\n");
2221 for (i = 0; i < nr_groups; i++)
2222 zfree(&desc[i].name);
2228 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2230 struct perf_session *session;
2233 session = container_of(ff->ph, struct perf_session, header);
2235 err = auxtrace_index__process(ff->fd, ff->size, session,
2236 ff->ph->needs_swap);
2238 pr_err("Failed to process auxtrace index\n");
2242 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2244 struct cpu_cache_level *caches;
2245 u32 cnt, i, version;
2247 if (do_read_u32(ff, &version))
2253 if (do_read_u32(ff, &cnt))
2256 caches = zalloc(sizeof(*caches) * cnt);
2260 for (i = 0; i < cnt; i++) {
2261 struct cpu_cache_level c;
2264 if (do_read_u32(ff, &c.v))\
2265 goto out_free_caches; \
2274 c.v = do_read_string(ff); \
2276 goto out_free_caches;
2286 ff->ph->env.caches = caches;
2287 ff->ph->env.caches_cnt = cnt;
2294 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2296 struct perf_session *session;
2297 u64 first_sample_time, last_sample_time;
2300 session = container_of(ff->ph, struct perf_session, header);
2302 ret = do_read_u64(ff, &first_sample_time);
2306 ret = do_read_u64(ff, &last_sample_time);
2310 session->evlist->first_sample_time = first_sample_time;
2311 session->evlist->last_sample_time = last_sample_time;
2315 static int process_mem_topology(struct feat_fd *ff,
2316 void *data __maybe_unused)
2318 struct memory_node *nodes;
2319 u64 version, i, nr, bsize;
2322 if (do_read_u64(ff, &version))
2328 if (do_read_u64(ff, &bsize))
2331 if (do_read_u64(ff, &nr))
2334 nodes = zalloc(sizeof(*nodes) * nr);
2338 for (i = 0; i < nr; i++) {
2339 struct memory_node n;
2342 if (do_read_u64(ff, &n.v)) \
2350 if (do_read_bitmap(ff, &n.set, &n.size))
2356 ff->ph->env.memory_bsize = bsize;
2357 ff->ph->env.memory_nodes = nodes;
2358 ff->ph->env.nr_memory_nodes = nr;
2367 static int process_clockid(struct feat_fd *ff,
2368 void *data __maybe_unused)
2370 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2376 struct feature_ops {
2377 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2378 void (*print)(struct feat_fd *ff, FILE *fp);
2379 int (*process)(struct feat_fd *ff, void *data);
2385 #define FEAT_OPR(n, func, __full_only) \
2387 .name = __stringify(n), \
2388 .write = write_##func, \
2389 .print = print_##func, \
2390 .full_only = __full_only, \
2391 .process = process_##func, \
2392 .synthesize = true \
2395 #define FEAT_OPN(n, func, __full_only) \
2397 .name = __stringify(n), \
2398 .write = write_##func, \
2399 .print = print_##func, \
2400 .full_only = __full_only, \
2401 .process = process_##func \
2404 /* feature_ops not implemented: */
2405 #define print_tracing_data NULL
2406 #define print_build_id NULL
2408 #define process_branch_stack NULL
2409 #define process_stat NULL
2412 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2413 FEAT_OPN(TRACING_DATA, tracing_data, false),
2414 FEAT_OPN(BUILD_ID, build_id, false),
2415 FEAT_OPR(HOSTNAME, hostname, false),
2416 FEAT_OPR(OSRELEASE, osrelease, false),
2417 FEAT_OPR(VERSION, version, false),
2418 FEAT_OPR(ARCH, arch, false),
2419 FEAT_OPR(NRCPUS, nrcpus, false),
2420 FEAT_OPR(CPUDESC, cpudesc, false),
2421 FEAT_OPR(CPUID, cpuid, false),
2422 FEAT_OPR(TOTAL_MEM, total_mem, false),
2423 FEAT_OPR(EVENT_DESC, event_desc, false),
2424 FEAT_OPR(CMDLINE, cmdline, false),
2425 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2426 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2427 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2428 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2429 FEAT_OPR(GROUP_DESC, group_desc, false),
2430 FEAT_OPN(AUXTRACE, auxtrace, false),
2431 FEAT_OPN(STAT, stat, false),
2432 FEAT_OPN(CACHE, cache, true),
2433 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2434 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2435 FEAT_OPR(CLOCKID, clockid, false)
2438 struct header_print_data {
2440 bool full; /* extended list of headers */
2443 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2444 struct perf_header *ph,
2445 int feat, int fd, void *data)
2447 struct header_print_data *hd = data;
2450 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2451 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2452 "%d, continuing...\n", section->offset, feat);
2455 if (feat >= HEADER_LAST_FEATURE) {
2456 pr_warning("unknown feature %d\n", feat);
2459 if (!feat_ops[feat].print)
2462 ff = (struct feat_fd) {
2467 if (!feat_ops[feat].full_only || hd->full)
2468 feat_ops[feat].print(&ff, hd->fp);
2470 fprintf(hd->fp, "# %s info available, use -I to display\n",
2471 feat_ops[feat].name);
2476 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2478 struct header_print_data hd;
2479 struct perf_header *header = &session->header;
2480 int fd = perf_data__fd(session->data);
2488 ret = fstat(fd, &st);
2492 stctime = st.st_ctime;
2493 fprintf(fp, "# captured on : %s", ctime(&stctime));
2495 fprintf(fp, "# header version : %u\n", header->version);
2496 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2497 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2498 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2500 perf_header__process_sections(header, fd, &hd,
2501 perf_file_section__fprintf_info);
2503 if (session->data->is_pipe)
2506 fprintf(fp, "# missing features: ");
2507 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2509 fprintf(fp, "%s ", feat_ops[bit].name);
2516 static int do_write_feat(struct feat_fd *ff, int type,
2517 struct perf_file_section **p,
2518 struct perf_evlist *evlist)
2523 if (perf_header__has_feat(ff->ph, type)) {
2524 if (!feat_ops[type].write)
2527 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2530 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2532 err = feat_ops[type].write(ff, evlist);
2534 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2536 /* undo anything written */
2537 lseek(ff->fd, (*p)->offset, SEEK_SET);
2541 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2547 static int perf_header__adds_write(struct perf_header *header,
2548 struct perf_evlist *evlist, int fd)
2552 struct perf_file_section *feat_sec, *p;
2558 ff = (struct feat_fd){
2563 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2567 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2568 if (feat_sec == NULL)
2571 sec_size = sizeof(*feat_sec) * nr_sections;
2573 sec_start = header->feat_offset;
2574 lseek(fd, sec_start + sec_size, SEEK_SET);
2576 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2577 if (do_write_feat(&ff, feat, &p, evlist))
2578 perf_header__clear_feat(header, feat);
2581 lseek(fd, sec_start, SEEK_SET);
2583 * may write more than needed due to dropped feature, but
2584 * this is okay, reader will skip the missing entries
2586 err = do_write(&ff, feat_sec, sec_size);
2588 pr_debug("failed to write feature section\n");
2593 int perf_header__write_pipe(int fd)
2595 struct perf_pipe_file_header f_header;
2599 ff = (struct feat_fd){ .fd = fd };
2601 f_header = (struct perf_pipe_file_header){
2602 .magic = PERF_MAGIC,
2603 .size = sizeof(f_header),
2606 err = do_write(&ff, &f_header, sizeof(f_header));
2608 pr_debug("failed to write perf pipe header\n");
2615 int perf_session__write_header(struct perf_session *session,
2616 struct perf_evlist *evlist,
2617 int fd, bool at_exit)
2619 struct perf_file_header f_header;
2620 struct perf_file_attr f_attr;
2621 struct perf_header *header = &session->header;
2622 struct perf_evsel *evsel;
2627 ff = (struct feat_fd){ .fd = fd};
2628 lseek(fd, sizeof(f_header), SEEK_SET);
2630 evlist__for_each_entry(session->evlist, evsel) {
2631 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2632 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2634 pr_debug("failed to write perf header\n");
2639 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2641 evlist__for_each_entry(evlist, evsel) {
2642 f_attr = (struct perf_file_attr){
2643 .attr = evsel->attr,
2645 .offset = evsel->id_offset,
2646 .size = evsel->ids * sizeof(u64),
2649 err = do_write(&ff, &f_attr, sizeof(f_attr));
2651 pr_debug("failed to write perf header attribute\n");
2656 if (!header->data_offset)
2657 header->data_offset = lseek(fd, 0, SEEK_CUR);
2658 header->feat_offset = header->data_offset + header->data_size;
2661 err = perf_header__adds_write(header, evlist, fd);
2666 f_header = (struct perf_file_header){
2667 .magic = PERF_MAGIC,
2668 .size = sizeof(f_header),
2669 .attr_size = sizeof(f_attr),
2671 .offset = attr_offset,
2672 .size = evlist->nr_entries * sizeof(f_attr),
2675 .offset = header->data_offset,
2676 .size = header->data_size,
2678 /* event_types is ignored, store zeros */
2681 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2683 lseek(fd, 0, SEEK_SET);
2684 err = do_write(&ff, &f_header, sizeof(f_header));
2686 pr_debug("failed to write perf header\n");
2689 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2694 static int perf_header__getbuffer64(struct perf_header *header,
2695 int fd, void *buf, size_t size)
2697 if (readn(fd, buf, size) <= 0)
2700 if (header->needs_swap)
2701 mem_bswap_64(buf, size);
2706 int perf_header__process_sections(struct perf_header *header, int fd,
2708 int (*process)(struct perf_file_section *section,
2709 struct perf_header *ph,
2710 int feat, int fd, void *data))
2712 struct perf_file_section *feat_sec, *sec;
2718 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2722 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2726 sec_size = sizeof(*feat_sec) * nr_sections;
2728 lseek(fd, header->feat_offset, SEEK_SET);
2730 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2734 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2735 err = process(sec++, header, feat, fd, data);
2745 static const int attr_file_abi_sizes[] = {
2746 [0] = PERF_ATTR_SIZE_VER0,
2747 [1] = PERF_ATTR_SIZE_VER1,
2748 [2] = PERF_ATTR_SIZE_VER2,
2749 [3] = PERF_ATTR_SIZE_VER3,
2750 [4] = PERF_ATTR_SIZE_VER4,
2755 * In the legacy file format, the magic number is not used to encode endianness.
2756 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2757 * on ABI revisions, we need to try all combinations for all endianness to
2758 * detect the endianness.
2760 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2762 uint64_t ref_size, attr_size;
2765 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2766 ref_size = attr_file_abi_sizes[i]
2767 + sizeof(struct perf_file_section);
2768 if (hdr_sz != ref_size) {
2769 attr_size = bswap_64(hdr_sz);
2770 if (attr_size != ref_size)
2773 ph->needs_swap = true;
2775 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2780 /* could not determine endianness */
2784 #define PERF_PIPE_HDR_VER0 16
2786 static const size_t attr_pipe_abi_sizes[] = {
2787 [0] = PERF_PIPE_HDR_VER0,
2792 * In the legacy pipe format, there is an implicit assumption that endiannesss
2793 * between host recording the samples, and host parsing the samples is the
2794 * same. This is not always the case given that the pipe output may always be
2795 * redirected into a file and analyzed on a different machine with possibly a
2796 * different endianness and perf_event ABI revsions in the perf tool itself.
2798 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2803 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2804 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2805 attr_size = bswap_64(hdr_sz);
2806 if (attr_size != hdr_sz)
2809 ph->needs_swap = true;
2811 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2817 bool is_perf_magic(u64 magic)
2819 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2820 || magic == __perf_magic2
2821 || magic == __perf_magic2_sw)
2827 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2828 bool is_pipe, struct perf_header *ph)
2832 /* check for legacy format */
2833 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2835 ph->version = PERF_HEADER_VERSION_1;
2836 pr_debug("legacy perf.data format\n");
2838 return try_all_pipe_abis(hdr_sz, ph);
2840 return try_all_file_abis(hdr_sz, ph);
2843 * the new magic number serves two purposes:
2844 * - unique number to identify actual perf.data files
2845 * - encode endianness of file
2847 ph->version = PERF_HEADER_VERSION_2;
2849 /* check magic number with one endianness */
2850 if (magic == __perf_magic2)
2853 /* check magic number with opposite endianness */
2854 if (magic != __perf_magic2_sw)
2857 ph->needs_swap = true;
2862 int perf_file_header__read(struct perf_file_header *header,
2863 struct perf_header *ph, int fd)
2867 lseek(fd, 0, SEEK_SET);
2869 ret = readn(fd, header, sizeof(*header));
2873 if (check_magic_endian(header->magic,
2874 header->attr_size, false, ph) < 0) {
2875 pr_debug("magic/endian check failed\n");
2879 if (ph->needs_swap) {
2880 mem_bswap_64(header, offsetof(struct perf_file_header,
2884 if (header->size != sizeof(*header)) {
2885 /* Support the previous format */
2886 if (header->size == offsetof(typeof(*header), adds_features))
2887 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2890 } else if (ph->needs_swap) {
2892 * feature bitmap is declared as an array of unsigned longs --
2893 * not good since its size can differ between the host that
2894 * generated the data file and the host analyzing the file.
2896 * We need to handle endianness, but we don't know the size of
2897 * the unsigned long where the file was generated. Take a best
2898 * guess at determining it: try 64-bit swap first (ie., file
2899 * created on a 64-bit host), and check if the hostname feature
2900 * bit is set (this feature bit is forced on as of fbe96f2).
2901 * If the bit is not, undo the 64-bit swap and try a 32-bit
2902 * swap. If the hostname bit is still not set (e.g., older data
2903 * file), punt and fallback to the original behavior --
2904 * clearing all feature bits and setting buildid.
2906 mem_bswap_64(&header->adds_features,
2907 BITS_TO_U64(HEADER_FEAT_BITS));
2909 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2911 mem_bswap_64(&header->adds_features,
2912 BITS_TO_U64(HEADER_FEAT_BITS));
2915 mem_bswap_32(&header->adds_features,
2916 BITS_TO_U32(HEADER_FEAT_BITS));
2919 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2920 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2921 set_bit(HEADER_BUILD_ID, header->adds_features);
2925 memcpy(&ph->adds_features, &header->adds_features,
2926 sizeof(ph->adds_features));
2928 ph->data_offset = header->data.offset;
2929 ph->data_size = header->data.size;
2930 ph->feat_offset = header->data.offset + header->data.size;
2934 static int perf_file_section__process(struct perf_file_section *section,
2935 struct perf_header *ph,
2936 int feat, int fd, void *data)
2938 struct feat_fd fdd = {
2941 .size = section->size,
2942 .offset = section->offset,
2945 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2946 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2947 "%d, continuing...\n", section->offset, feat);
2951 if (feat >= HEADER_LAST_FEATURE) {
2952 pr_debug("unknown feature %d, continuing...\n", feat);
2956 if (!feat_ops[feat].process)
2959 return feat_ops[feat].process(&fdd, data);
2962 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2963 struct perf_header *ph, int fd,
2966 struct feat_fd ff = {
2967 .fd = STDOUT_FILENO,
2972 ret = readn(fd, header, sizeof(*header));
2976 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2977 pr_debug("endian/magic failed\n");
2982 header->size = bswap_64(header->size);
2984 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
2990 static int perf_header__read_pipe(struct perf_session *session)
2992 struct perf_header *header = &session->header;
2993 struct perf_pipe_file_header f_header;
2995 if (perf_file_header__read_pipe(&f_header, header,
2996 perf_data__fd(session->data),
2997 session->repipe) < 0) {
2998 pr_debug("incompatible file format\n");
3005 static int read_attr(int fd, struct perf_header *ph,
3006 struct perf_file_attr *f_attr)
3008 struct perf_event_attr *attr = &f_attr->attr;
3010 size_t our_sz = sizeof(f_attr->attr);
3013 memset(f_attr, 0, sizeof(*f_attr));
3015 /* read minimal guaranteed structure */
3016 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3018 pr_debug("cannot read %d bytes of header attr\n",
3019 PERF_ATTR_SIZE_VER0);
3023 /* on file perf_event_attr size */
3031 sz = PERF_ATTR_SIZE_VER0;
3032 } else if (sz > our_sz) {
3033 pr_debug("file uses a more recent and unsupported ABI"
3034 " (%zu bytes extra)\n", sz - our_sz);
3037 /* what we have not yet read and that we know about */
3038 left = sz - PERF_ATTR_SIZE_VER0;
3041 ptr += PERF_ATTR_SIZE_VER0;
3043 ret = readn(fd, ptr, left);
3045 /* read perf_file_section, ids are read in caller */
3046 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3048 return ret <= 0 ? -1 : 0;
3051 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
3052 struct tep_handle *pevent)
3054 struct tep_event *event;
3057 /* already prepared */
3058 if (evsel->tp_format)
3061 if (pevent == NULL) {
3062 pr_debug("broken or missing trace data\n");
3066 event = tep_find_event(pevent, evsel->attr.config);
3067 if (event == NULL) {
3068 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
3073 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3074 evsel->name = strdup(bf);
3075 if (evsel->name == NULL)
3079 evsel->tp_format = event;
3083 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
3084 struct tep_handle *pevent)
3086 struct perf_evsel *pos;
3088 evlist__for_each_entry(evlist, pos) {
3089 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3090 perf_evsel__prepare_tracepoint_event(pos, pevent))
3097 int perf_session__read_header(struct perf_session *session)
3099 struct perf_data *data = session->data;
3100 struct perf_header *header = &session->header;
3101 struct perf_file_header f_header;
3102 struct perf_file_attr f_attr;
3104 int nr_attrs, nr_ids, i, j;
3105 int fd = perf_data__fd(data);
3107 session->evlist = perf_evlist__new();
3108 if (session->evlist == NULL)
3111 session->evlist->env = &header->env;
3112 session->machines.host.env = &header->env;
3113 if (perf_data__is_pipe(data))
3114 return perf_header__read_pipe(session);
3116 if (perf_file_header__read(&f_header, header, fd) < 0)
3120 * Sanity check that perf.data was written cleanly; data size is
3121 * initialized to 0 and updated only if the on_exit function is run.
3122 * If data size is still 0 then the file contains only partial
3123 * information. Just warn user and process it as much as it can.
3125 if (f_header.data.size == 0) {
3126 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3127 "Was the 'perf record' command properly terminated?\n",
3131 nr_attrs = f_header.attrs.size / f_header.attr_size;
3132 lseek(fd, f_header.attrs.offset, SEEK_SET);
3134 for (i = 0; i < nr_attrs; i++) {
3135 struct perf_evsel *evsel;
3138 if (read_attr(fd, header, &f_attr) < 0)
3141 if (header->needs_swap) {
3142 f_attr.ids.size = bswap_64(f_attr.ids.size);
3143 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3144 perf_event__attr_swap(&f_attr.attr);
3147 tmp = lseek(fd, 0, SEEK_CUR);
3148 evsel = perf_evsel__new(&f_attr.attr);
3151 goto out_delete_evlist;
3153 evsel->needs_swap = header->needs_swap;
3155 * Do it before so that if perf_evsel__alloc_id fails, this
3156 * entry gets purged too at perf_evlist__delete().
3158 perf_evlist__add(session->evlist, evsel);
3160 nr_ids = f_attr.ids.size / sizeof(u64);
3162 * We don't have the cpu and thread maps on the header, so
3163 * for allocating the perf_sample_id table we fake 1 cpu and
3164 * hattr->ids threads.
3166 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3167 goto out_delete_evlist;
3169 lseek(fd, f_attr.ids.offset, SEEK_SET);
3171 for (j = 0; j < nr_ids; j++) {
3172 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3175 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
3178 lseek(fd, tmp, SEEK_SET);
3181 perf_header__process_sections(header, fd, &session->tevent,
3182 perf_file_section__process);
3184 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3185 session->tevent.pevent))
3186 goto out_delete_evlist;
3193 perf_evlist__delete(session->evlist);
3194 session->evlist = NULL;
3198 int perf_event__synthesize_attr(struct perf_tool *tool,
3199 struct perf_event_attr *attr, u32 ids, u64 *id,
3200 perf_event__handler_t process)
3202 union perf_event *ev;
3206 size = sizeof(struct perf_event_attr);
3207 size = PERF_ALIGN(size, sizeof(u64));
3208 size += sizeof(struct perf_event_header);
3209 size += ids * sizeof(u64);
3216 ev->attr.attr = *attr;
3217 memcpy(ev->attr.id, id, ids * sizeof(u64));
3219 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3220 ev->attr.header.size = (u16)size;
3222 if (ev->attr.header.size == size)
3223 err = process(tool, ev, NULL, NULL);
3232 int perf_event__synthesize_features(struct perf_tool *tool,
3233 struct perf_session *session,
3234 struct perf_evlist *evlist,
3235 perf_event__handler_t process)
3237 struct perf_header *header = &session->header;
3239 struct feature_event *fe;
3243 sz_hdr = sizeof(fe->header);
3244 sz = sizeof(union perf_event);
3245 /* get a nice alignment */
3246 sz = PERF_ALIGN(sz, page_size);
3248 memset(&ff, 0, sizeof(ff));
3250 ff.buf = malloc(sz);
3254 ff.size = sz - sz_hdr;
3256 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3257 if (!feat_ops[feat].synthesize) {
3258 pr_debug("No record header feature for header :%d\n", feat);
3262 ff.offset = sizeof(*fe);
3264 ret = feat_ops[feat].write(&ff, evlist);
3265 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3266 pr_debug("Error writing feature\n");
3269 /* ff.buf may have changed due to realloc in do_write() */
3271 memset(fe, 0, sizeof(*fe));
3274 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3275 fe->header.size = ff.offset;
3277 ret = process(tool, ff.buf, NULL, NULL);
3284 /* Send HEADER_LAST_FEATURE mark. */
3286 fe->feat_id = HEADER_LAST_FEATURE;
3287 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3288 fe->header.size = sizeof(*fe);
3290 ret = process(tool, ff.buf, NULL, NULL);
3296 int perf_event__process_feature(struct perf_session *session,
3297 union perf_event *event)
3299 struct perf_tool *tool = session->tool;
3300 struct feat_fd ff = { .fd = 0 };
3301 struct feature_event *fe = (struct feature_event *)event;
3302 int type = fe->header.type;
3303 u64 feat = fe->feat_id;
3305 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3306 pr_warning("invalid record type %d in pipe-mode\n", type);
3309 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3310 pr_warning("invalid record type %d in pipe-mode\n", type);
3314 if (!feat_ops[feat].process)
3317 ff.buf = (void *)fe->data;
3318 ff.size = event->header.size - sizeof(event->header);
3319 ff.ph = &session->header;
3321 if (feat_ops[feat].process(&ff, NULL))
3324 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3327 if (!feat_ops[feat].full_only ||
3328 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3329 feat_ops[feat].print(&ff, stdout);
3331 fprintf(stdout, "# %s info available, use -I to display\n",
3332 feat_ops[feat].name);
3338 static struct event_update_event *
3339 event_update_event__new(size_t size, u64 type, u64 id)
3341 struct event_update_event *ev;
3343 size += sizeof(*ev);
3344 size = PERF_ALIGN(size, sizeof(u64));
3348 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3349 ev->header.size = (u16)size;
3357 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3358 struct perf_evsel *evsel,
3359 perf_event__handler_t process)
3361 struct event_update_event *ev;
3362 size_t size = strlen(evsel->unit);
3365 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3369 strlcpy(ev->data, evsel->unit, size + 1);
3370 err = process(tool, (union perf_event *)ev, NULL, NULL);
3376 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3377 struct perf_evsel *evsel,
3378 perf_event__handler_t process)
3380 struct event_update_event *ev;
3381 struct event_update_event_scale *ev_data;
3384 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3388 ev_data = (struct event_update_event_scale *) ev->data;
3389 ev_data->scale = evsel->scale;
3390 err = process(tool, (union perf_event*) ev, NULL, NULL);
3396 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3397 struct perf_evsel *evsel,
3398 perf_event__handler_t process)
3400 struct event_update_event *ev;
3401 size_t len = strlen(evsel->name);
3404 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3408 strlcpy(ev->data, evsel->name, len + 1);
3409 err = process(tool, (union perf_event*) ev, NULL, NULL);
3415 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3416 struct perf_evsel *evsel,
3417 perf_event__handler_t process)
3419 size_t size = sizeof(struct event_update_event);
3420 struct event_update_event *ev;
3424 if (!evsel->own_cpus)
3427 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3431 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3432 ev->header.size = (u16)size;
3433 ev->type = PERF_EVENT_UPDATE__CPUS;
3434 ev->id = evsel->id[0];
3436 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3440 err = process(tool, (union perf_event*) ev, NULL, NULL);
3445 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3447 struct event_update_event *ev = &event->event_update;
3448 struct event_update_event_scale *ev_scale;
3449 struct event_update_event_cpus *ev_cpus;
3450 struct cpu_map *map;
3453 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3456 case PERF_EVENT_UPDATE__SCALE:
3457 ev_scale = (struct event_update_event_scale *) ev->data;
3458 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3460 case PERF_EVENT_UPDATE__UNIT:
3461 ret += fprintf(fp, "... unit: %s\n", ev->data);
3463 case PERF_EVENT_UPDATE__NAME:
3464 ret += fprintf(fp, "... name: %s\n", ev->data);
3466 case PERF_EVENT_UPDATE__CPUS:
3467 ev_cpus = (struct event_update_event_cpus *) ev->data;
3468 ret += fprintf(fp, "... ");
3470 map = cpu_map__new_data(&ev_cpus->cpus);
3472 ret += cpu_map__fprintf(map, fp);
3474 ret += fprintf(fp, "failed to get cpus\n");
3477 ret += fprintf(fp, "... unknown type\n");
3484 int perf_event__synthesize_attrs(struct perf_tool *tool,
3485 struct perf_evlist *evlist,
3486 perf_event__handler_t process)
3488 struct perf_evsel *evsel;
3491 evlist__for_each_entry(evlist, evsel) {
3492 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3493 evsel->id, process);
3495 pr_debug("failed to create perf header attribute\n");
3503 static bool has_unit(struct perf_evsel *counter)
3505 return counter->unit && *counter->unit;
3508 static bool has_scale(struct perf_evsel *counter)
3510 return counter->scale != 1;
3513 int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3514 struct perf_evlist *evsel_list,
3515 perf_event__handler_t process,
3518 struct perf_evsel *counter;
3522 * Synthesize other events stuff not carried within
3523 * attr event - unit, scale, name
3525 evlist__for_each_entry(evsel_list, counter) {
3526 if (!counter->supported)
3530 * Synthesize unit and scale only if it's defined.
3532 if (has_unit(counter)) {
3533 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3535 pr_err("Couldn't synthesize evsel unit.\n");
3540 if (has_scale(counter)) {
3541 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3543 pr_err("Couldn't synthesize evsel counter.\n");
3548 if (counter->own_cpus) {
3549 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3551 pr_err("Couldn't synthesize evsel cpus.\n");
3557 * Name is needed only for pipe output,
3558 * perf.data carries event names.
3561 err = perf_event__synthesize_event_update_name(tool, counter, process);
3563 pr_err("Couldn't synthesize evsel name.\n");
3571 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3572 union perf_event *event,
3573 struct perf_evlist **pevlist)
3576 struct perf_evsel *evsel;
3577 struct perf_evlist *evlist = *pevlist;
3579 if (evlist == NULL) {
3580 *pevlist = evlist = perf_evlist__new();
3585 evsel = perf_evsel__new(&event->attr.attr);
3589 perf_evlist__add(evlist, evsel);
3591 ids = event->header.size;
3592 ids -= (void *)&event->attr.id - (void *)event;
3593 n_ids = ids / sizeof(u64);
3595 * We don't have the cpu and thread maps on the header, so
3596 * for allocating the perf_sample_id table we fake 1 cpu and
3597 * hattr->ids threads.
3599 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3602 for (i = 0; i < n_ids; i++) {
3603 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3609 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3610 union perf_event *event,
3611 struct perf_evlist **pevlist)
3613 struct event_update_event *ev = &event->event_update;
3614 struct event_update_event_scale *ev_scale;
3615 struct event_update_event_cpus *ev_cpus;
3616 struct perf_evlist *evlist;
3617 struct perf_evsel *evsel;
3618 struct cpu_map *map;
3620 if (!pevlist || *pevlist == NULL)
3625 evsel = perf_evlist__id2evsel(evlist, ev->id);
3630 case PERF_EVENT_UPDATE__UNIT:
3631 evsel->unit = strdup(ev->data);
3633 case PERF_EVENT_UPDATE__NAME:
3634 evsel->name = strdup(ev->data);
3636 case PERF_EVENT_UPDATE__SCALE:
3637 ev_scale = (struct event_update_event_scale *) ev->data;
3638 evsel->scale = ev_scale->scale;
3640 case PERF_EVENT_UPDATE__CPUS:
3641 ev_cpus = (struct event_update_event_cpus *) ev->data;
3643 map = cpu_map__new_data(&ev_cpus->cpus);
3645 evsel->own_cpus = map;
3647 pr_err("failed to get event_update cpus\n");
3655 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3656 struct perf_evlist *evlist,
3657 perf_event__handler_t process)
3659 union perf_event ev;
3660 struct tracing_data *tdata;
3661 ssize_t size = 0, aligned_size = 0, padding;
3663 int err __maybe_unused = 0;
3666 * We are going to store the size of the data followed
3667 * by the data contents. Since the fd descriptor is a pipe,
3668 * we cannot seek back to store the size of the data once
3669 * we know it. Instead we:
3671 * - write the tracing data to the temp file
3672 * - get/write the data size to pipe
3673 * - write the tracing data from the temp file
3676 tdata = tracing_data_get(&evlist->entries, fd, true);
3680 memset(&ev, 0, sizeof(ev));
3682 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3684 aligned_size = PERF_ALIGN(size, sizeof(u64));
3685 padding = aligned_size - size;
3686 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3687 ev.tracing_data.size = aligned_size;
3689 process(tool, &ev, NULL, NULL);
3692 * The put function will copy all the tracing data
3693 * stored in temp file to the pipe.
3695 tracing_data_put(tdata);
3697 ff = (struct feat_fd){ .fd = fd };
3698 if (write_padded(&ff, NULL, 0, padding))
3701 return aligned_size;
3704 int perf_event__process_tracing_data(struct perf_session *session,
3705 union perf_event *event)
3707 ssize_t size_read, padding, size = event->tracing_data.size;
3708 int fd = perf_data__fd(session->data);
3709 off_t offset = lseek(fd, 0, SEEK_CUR);
3712 /* setup for reading amidst mmap */
3713 lseek(fd, offset + sizeof(struct tracing_data_event),
3716 size_read = trace_report(fd, &session->tevent,
3718 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3720 if (readn(fd, buf, padding) < 0) {
3721 pr_err("%s: reading input file", __func__);
3724 if (session->repipe) {
3725 int retw = write(STDOUT_FILENO, buf, padding);
3726 if (retw <= 0 || retw != padding) {
3727 pr_err("%s: repiping tracing data padding", __func__);
3732 if (size_read + padding != size) {
3733 pr_err("%s: tracing data size mismatch", __func__);
3737 perf_evlist__prepare_tracepoint_events(session->evlist,
3738 session->tevent.pevent);
3740 return size_read + padding;
3743 int perf_event__synthesize_build_id(struct perf_tool *tool,
3744 struct dso *pos, u16 misc,
3745 perf_event__handler_t process,
3746 struct machine *machine)
3748 union perf_event ev;
3755 memset(&ev, 0, sizeof(ev));
3757 len = pos->long_name_len + 1;
3758 len = PERF_ALIGN(len, NAME_ALIGN);
3759 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3760 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3761 ev.build_id.header.misc = misc;
3762 ev.build_id.pid = machine->pid;
3763 ev.build_id.header.size = sizeof(ev.build_id) + len;
3764 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3766 err = process(tool, &ev, NULL, machine);
3771 int perf_event__process_build_id(struct perf_session *session,
3772 union perf_event *event)
3774 __event_process_build_id(&event->build_id,
3775 event->build_id.filename,