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"
43 #include "sane_ctype.h"
47 * must be a numerical value to let the endianness
48 * determine the memory layout. That way we are able
49 * to detect endianness when reading the perf.data file
52 * we check for legacy (PERFFILE) format.
54 static const char *__perf_magic1 = "PERFFILE";
55 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
56 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
58 #define PERF_MAGIC __perf_magic2
60 const char perf_version_string[] = PERF_VERSION;
62 struct perf_file_attr {
63 struct perf_event_attr attr;
64 struct perf_file_section ids;
68 struct perf_header *ph;
70 void *buf; /* Either buf != NULL or fd >= 0 */
73 struct perf_evsel *events;
76 void perf_header__set_feat(struct perf_header *header, int feat)
78 set_bit(feat, header->adds_features);
81 void perf_header__clear_feat(struct perf_header *header, int feat)
83 clear_bit(feat, header->adds_features);
86 bool perf_header__has_feat(const struct perf_header *header, int feat)
88 return test_bit(feat, header->adds_features);
91 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
93 ssize_t ret = writen(ff->fd, buf, size);
95 if (ret != (ssize_t)size)
96 return ret < 0 ? (int)ret : -1;
100 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
102 /* struct perf_event_header::size is u16 */
103 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
104 size_t new_size = ff->size;
107 if (size + ff->offset > max_size)
110 while (size > (new_size - ff->offset))
112 new_size = min(max_size, new_size);
114 if (ff->size < new_size) {
115 addr = realloc(ff->buf, new_size);
122 memcpy(ff->buf + ff->offset, buf, size);
128 /* Return: 0 if succeded, -ERR if failed. */
129 int do_write(struct feat_fd *ff, const void *buf, size_t size)
132 return __do_write_fd(ff, buf, size);
133 return __do_write_buf(ff, buf, size);
136 /* Return: 0 if succeded, -ERR if failed. */
137 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
139 u64 *p = (u64 *) set;
142 ret = do_write(ff, &size, sizeof(size));
146 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
147 ret = do_write(ff, p + i, sizeof(*p));
155 /* Return: 0 if succeded, -ERR if failed. */
156 int write_padded(struct feat_fd *ff, const void *bf,
157 size_t count, size_t count_aligned)
159 static const char zero_buf[NAME_ALIGN];
160 int err = do_write(ff, bf, count);
163 err = do_write(ff, zero_buf, count_aligned - count);
168 #define string_size(str) \
169 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
171 /* Return: 0 if succeded, -ERR if failed. */
172 static int do_write_string(struct feat_fd *ff, const char *str)
177 olen = strlen(str) + 1;
178 len = PERF_ALIGN(olen, NAME_ALIGN);
180 /* write len, incl. \0 */
181 ret = do_write(ff, &len, sizeof(len));
185 return write_padded(ff, str, olen, len);
188 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
190 ssize_t ret = readn(ff->fd, addr, size);
193 return ret < 0 ? (int)ret : -1;
197 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
199 if (size > (ssize_t)ff->size - ff->offset)
202 memcpy(addr, ff->buf + ff->offset, size);
209 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
212 return __do_read_fd(ff, addr, size);
213 return __do_read_buf(ff, addr, size);
216 static int do_read_u32(struct feat_fd *ff, u32 *addr)
220 ret = __do_read(ff, addr, sizeof(*addr));
224 if (ff->ph->needs_swap)
225 *addr = bswap_32(*addr);
229 static int do_read_u64(struct feat_fd *ff, u64 *addr)
233 ret = __do_read(ff, addr, sizeof(*addr));
237 if (ff->ph->needs_swap)
238 *addr = bswap_64(*addr);
242 static char *do_read_string(struct feat_fd *ff)
247 if (do_read_u32(ff, &len))
254 if (!__do_read(ff, buf, len)) {
256 * strings are padded by zeroes
257 * thus the actual strlen of buf
258 * may be less than len
267 /* Return: 0 if succeded, -ERR if failed. */
268 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
274 ret = do_read_u64(ff, &size);
278 set = bitmap_alloc(size);
284 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
285 ret = do_read_u64(ff, p + i);
297 static int write_tracing_data(struct feat_fd *ff,
298 struct perf_evlist *evlist)
300 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
303 return read_tracing_data(ff->fd, &evlist->entries);
306 static int write_build_id(struct feat_fd *ff,
307 struct perf_evlist *evlist __maybe_unused)
309 struct perf_session *session;
312 session = container_of(ff->ph, struct perf_session, header);
314 if (!perf_session__read_build_ids(session, true))
317 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
320 err = perf_session__write_buildid_table(session, ff);
322 pr_debug("failed to write buildid table\n");
325 perf_session__cache_build_ids(session);
330 static int write_hostname(struct feat_fd *ff,
331 struct perf_evlist *evlist __maybe_unused)
340 return do_write_string(ff, uts.nodename);
343 static int write_osrelease(struct feat_fd *ff,
344 struct perf_evlist *evlist __maybe_unused)
353 return do_write_string(ff, uts.release);
356 static int write_arch(struct feat_fd *ff,
357 struct perf_evlist *evlist __maybe_unused)
366 return do_write_string(ff, uts.machine);
369 static int write_version(struct feat_fd *ff,
370 struct perf_evlist *evlist __maybe_unused)
372 return do_write_string(ff, perf_version_string);
375 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
380 const char *search = cpuinfo_proc;
387 file = fopen("/proc/cpuinfo", "r");
391 while (getline(&buf, &len, file) > 0) {
392 ret = strncmp(buf, search, strlen(search));
404 p = strchr(buf, ':');
405 if (p && *(p+1) == ' ' && *(p+2))
411 /* squash extra space characters (branding string) */
418 while (*q && isspace(*q))
421 while ((*r++ = *q++));
425 ret = do_write_string(ff, s);
432 static int write_cpudesc(struct feat_fd *ff,
433 struct perf_evlist *evlist __maybe_unused)
435 const char *cpuinfo_procs[] = CPUINFO_PROC;
438 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
440 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
448 static int write_nrcpus(struct feat_fd *ff,
449 struct perf_evlist *evlist __maybe_unused)
455 nrc = cpu__max_present_cpu();
457 nr = sysconf(_SC_NPROCESSORS_ONLN);
461 nra = (u32)(nr & UINT_MAX);
463 ret = do_write(ff, &nrc, sizeof(nrc));
467 return do_write(ff, &nra, sizeof(nra));
470 static int write_event_desc(struct feat_fd *ff,
471 struct perf_evlist *evlist)
473 struct perf_evsel *evsel;
477 nre = evlist->nr_entries;
480 * write number of events
482 ret = do_write(ff, &nre, sizeof(nre));
487 * size of perf_event_attr struct
489 sz = (u32)sizeof(evsel->attr);
490 ret = do_write(ff, &sz, sizeof(sz));
494 evlist__for_each_entry(evlist, evsel) {
495 ret = do_write(ff, &evsel->attr, sz);
499 * write number of unique id per event
500 * there is one id per instance of an event
502 * copy into an nri to be independent of the
506 ret = do_write(ff, &nri, sizeof(nri));
511 * write event string as passed on cmdline
513 ret = do_write_string(ff, perf_evsel__name(evsel));
517 * write unique ids for this event
519 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
526 static int write_cmdline(struct feat_fd *ff,
527 struct perf_evlist *evlist __maybe_unused)
529 char buf[MAXPATHLEN];
533 /* actual path to perf binary */
534 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
538 /* readlink() does not add null termination */
541 /* account for binary path */
542 n = perf_env.nr_cmdline + 1;
544 ret = do_write(ff, &n, sizeof(n));
548 ret = do_write_string(ff, buf);
552 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
553 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
560 #define CORE_SIB_FMT \
561 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
562 #define THRD_SIB_FMT \
563 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
569 char **core_siblings;
570 char **thread_siblings;
573 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
576 char filename[MAXPATHLEN];
577 char *buf = NULL, *p;
583 sprintf(filename, CORE_SIB_FMT, cpu);
584 fp = fopen(filename, "r");
588 sret = getline(&buf, &len, fp);
593 p = strchr(buf, '\n');
597 for (i = 0; i < tp->core_sib; i++) {
598 if (!strcmp(buf, tp->core_siblings[i]))
601 if (i == tp->core_sib) {
602 tp->core_siblings[i] = buf;
610 sprintf(filename, THRD_SIB_FMT, cpu);
611 fp = fopen(filename, "r");
615 if (getline(&buf, &len, fp) <= 0)
618 p = strchr(buf, '\n');
622 for (i = 0; i < tp->thread_sib; i++) {
623 if (!strcmp(buf, tp->thread_siblings[i]))
626 if (i == tp->thread_sib) {
627 tp->thread_siblings[i] = buf;
639 static void free_cpu_topo(struct cpu_topo *tp)
646 for (i = 0 ; i < tp->core_sib; i++)
647 zfree(&tp->core_siblings[i]);
649 for (i = 0 ; i < tp->thread_sib; i++)
650 zfree(&tp->thread_siblings[i]);
655 static struct cpu_topo *build_cpu_topology(void)
657 struct cpu_topo *tp = NULL;
665 ncpus = cpu__max_present_cpu();
667 /* build online CPU map */
668 map = cpu_map__new(NULL);
670 pr_debug("failed to get system cpumap\n");
674 nr = (u32)(ncpus & UINT_MAX);
676 sz = nr * sizeof(char *);
677 addr = calloc(1, sizeof(*tp) + 2 * sz);
684 tp->core_siblings = addr;
686 tp->thread_siblings = addr;
688 for (i = 0; i < nr; i++) {
689 if (!cpu_map__has(map, i))
692 ret = build_cpu_topo(tp, i);
706 static int write_cpu_topology(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
713 tp = build_cpu_topology();
717 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
721 for (i = 0; i < tp->core_sib; i++) {
722 ret = do_write_string(ff, tp->core_siblings[i]);
726 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
730 for (i = 0; i < tp->thread_sib; i++) {
731 ret = do_write_string(ff, tp->thread_siblings[i]);
736 ret = perf_env__read_cpu_topology_map(&perf_env);
740 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
741 ret = do_write(ff, &perf_env.cpu[j].core_id,
742 sizeof(perf_env.cpu[j].core_id));
745 ret = do_write(ff, &perf_env.cpu[j].socket_id,
746 sizeof(perf_env.cpu[j].socket_id));
757 static int write_total_mem(struct feat_fd *ff,
758 struct perf_evlist *evlist __maybe_unused)
766 fp = fopen("/proc/meminfo", "r");
770 while (getline(&buf, &len, fp) > 0) {
771 ret = strncmp(buf, "MemTotal:", 9);
776 n = sscanf(buf, "%*s %"PRIu64, &mem);
778 ret = do_write(ff, &mem, sizeof(mem));
786 static int write_topo_node(struct feat_fd *ff, int node)
788 char str[MAXPATHLEN];
790 char *buf = NULL, *p;
793 u64 mem_total, mem_free, mem;
796 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
797 fp = fopen(str, "r");
801 while (getline(&buf, &len, fp) > 0) {
802 /* skip over invalid lines */
803 if (!strchr(buf, ':'))
805 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
807 if (!strcmp(field, "MemTotal:"))
809 if (!strcmp(field, "MemFree:"))
816 ret = do_write(ff, &mem_total, sizeof(u64));
820 ret = do_write(ff, &mem_free, sizeof(u64));
825 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
827 fp = fopen(str, "r");
831 if (getline(&buf, &len, fp) <= 0)
834 p = strchr(buf, '\n');
838 ret = do_write_string(ff, buf);
846 static int write_numa_topology(struct feat_fd *ff,
847 struct perf_evlist *evlist __maybe_unused)
852 struct cpu_map *node_map = NULL;
857 fp = fopen("/sys/devices/system/node/online", "r");
861 if (getline(&buf, &len, fp) <= 0)
864 c = strchr(buf, '\n');
868 node_map = cpu_map__new(buf);
872 nr = (u32)node_map->nr;
874 ret = do_write(ff, &nr, sizeof(nr));
878 for (i = 0; i < nr; i++) {
879 j = (u32)node_map->map[i];
880 ret = do_write(ff, &j, sizeof(j));
884 ret = write_topo_node(ff, i);
891 cpu_map__put(node_map);
898 * struct pmu_mappings {
907 static int write_pmu_mappings(struct feat_fd *ff,
908 struct perf_evlist *evlist __maybe_unused)
910 struct perf_pmu *pmu = NULL;
915 * Do a first pass to count number of pmu to avoid lseek so this
916 * works in pipe mode as well.
918 while ((pmu = perf_pmu__scan(pmu))) {
924 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
928 while ((pmu = perf_pmu__scan(pmu))) {
932 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
936 ret = do_write_string(ff, pmu->name);
947 * struct group_descs {
949 * struct group_desc {
956 static int write_group_desc(struct feat_fd *ff,
957 struct perf_evlist *evlist)
959 u32 nr_groups = evlist->nr_groups;
960 struct perf_evsel *evsel;
963 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
967 evlist__for_each_entry(evlist, evsel) {
968 if (perf_evsel__is_group_leader(evsel) &&
969 evsel->nr_members > 1) {
970 const char *name = evsel->group_name ?: "{anon_group}";
971 u32 leader_idx = evsel->idx;
972 u32 nr_members = evsel->nr_members;
974 ret = do_write_string(ff, name);
978 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
982 ret = do_write(ff, &nr_members, sizeof(nr_members));
991 * default get_cpuid(): nothing gets recorded
992 * actual implementation must be in arch/$(SRCARCH)/util/header.c
994 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
999 static int write_cpuid(struct feat_fd *ff,
1000 struct perf_evlist *evlist __maybe_unused)
1005 ret = get_cpuid(buffer, sizeof(buffer));
1011 return do_write_string(ff, buffer);
1014 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
1015 struct perf_evlist *evlist __maybe_unused)
1020 static int write_auxtrace(struct feat_fd *ff,
1021 struct perf_evlist *evlist __maybe_unused)
1023 struct perf_session *session;
1026 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
1029 session = container_of(ff->ph, struct perf_session, header);
1031 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
1033 pr_err("Failed to write auxtrace index\n");
1037 static int cpu_cache_level__sort(const void *a, const void *b)
1039 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1040 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1042 return cache_a->level - cache_b->level;
1045 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1047 if (a->level != b->level)
1050 if (a->line_size != b->line_size)
1053 if (a->sets != b->sets)
1056 if (a->ways != b->ways)
1059 if (strcmp(a->type, b->type))
1062 if (strcmp(a->size, b->size))
1065 if (strcmp(a->map, b->map))
1071 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1073 char path[PATH_MAX], file[PATH_MAX];
1077 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1078 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1080 if (stat(file, &st))
1083 scnprintf(file, PATH_MAX, "%s/level", path);
1084 if (sysfs__read_int(file, (int *) &cache->level))
1087 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1088 if (sysfs__read_int(file, (int *) &cache->line_size))
1091 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1092 if (sysfs__read_int(file, (int *) &cache->sets))
1095 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1096 if (sysfs__read_int(file, (int *) &cache->ways))
1099 scnprintf(file, PATH_MAX, "%s/type", path);
1100 if (sysfs__read_str(file, &cache->type, &len))
1103 cache->type[len] = 0;
1104 cache->type = rtrim(cache->type);
1106 scnprintf(file, PATH_MAX, "%s/size", path);
1107 if (sysfs__read_str(file, &cache->size, &len)) {
1112 cache->size[len] = 0;
1113 cache->size = rtrim(cache->size);
1115 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1116 if (sysfs__read_str(file, &cache->map, &len)) {
1122 cache->map[len] = 0;
1123 cache->map = rtrim(cache->map);
1127 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1129 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1132 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1139 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1143 nr = (u32)(ncpus & UINT_MAX);
1145 for (cpu = 0; cpu < nr; cpu++) {
1146 for (level = 0; level < 10; level++) {
1147 struct cpu_cache_level c;
1150 err = cpu_cache_level__read(&c, cpu, level);
1157 for (i = 0; i < cnt; i++) {
1158 if (cpu_cache_level__cmp(&c, &caches[i]))
1165 cpu_cache_level__free(&c);
1167 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1176 #define MAX_CACHES (MAX_NR_CPUS * 4)
1178 static int write_cache(struct feat_fd *ff,
1179 struct perf_evlist *evlist __maybe_unused)
1181 struct cpu_cache_level caches[MAX_CACHES];
1182 u32 cnt = 0, i, version = 1;
1185 ret = build_caches(caches, MAX_CACHES, &cnt);
1189 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1191 ret = do_write(ff, &version, sizeof(u32));
1195 ret = do_write(ff, &cnt, sizeof(u32));
1199 for (i = 0; i < cnt; i++) {
1200 struct cpu_cache_level *c = &caches[i];
1203 ret = do_write(ff, &c->v, sizeof(u32)); \
1214 ret = do_write_string(ff, (const char *) c->v); \
1225 for (i = 0; i < cnt; i++)
1226 cpu_cache_level__free(&caches[i]);
1230 static int write_stat(struct feat_fd *ff __maybe_unused,
1231 struct perf_evlist *evlist __maybe_unused)
1236 static int write_sample_time(struct feat_fd *ff,
1237 struct perf_evlist *evlist)
1241 ret = do_write(ff, &evlist->first_sample_time,
1242 sizeof(evlist->first_sample_time));
1246 return do_write(ff, &evlist->last_sample_time,
1247 sizeof(evlist->last_sample_time));
1251 static int memory_node__read(struct memory_node *n, unsigned long idx)
1253 unsigned int phys, size = 0;
1254 char path[PATH_MAX];
1258 #define for_each_memory(mem, dir) \
1259 while ((ent = readdir(dir))) \
1260 if (strcmp(ent->d_name, ".") && \
1261 strcmp(ent->d_name, "..") && \
1262 sscanf(ent->d_name, "memory%u", &mem) == 1)
1264 scnprintf(path, PATH_MAX,
1265 "%s/devices/system/node/node%lu",
1266 sysfs__mountpoint(), idx);
1268 dir = opendir(path);
1270 pr_warning("failed: cant' open memory sysfs data\n");
1274 for_each_memory(phys, dir) {
1275 size = max(phys, size);
1280 n->set = bitmap_alloc(size);
1291 for_each_memory(phys, dir) {
1292 set_bit(phys, n->set);
1299 static int memory_node__sort(const void *a, const void *b)
1301 const struct memory_node *na = a;
1302 const struct memory_node *nb = b;
1304 return na->node - nb->node;
1307 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1309 char path[PATH_MAX];
1315 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1316 sysfs__mountpoint());
1318 dir = opendir(path);
1320 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1325 while (!ret && (ent = readdir(dir))) {
1329 if (!strcmp(ent->d_name, ".") ||
1330 !strcmp(ent->d_name, ".."))
1333 r = sscanf(ent->d_name, "node%u", &idx);
1337 if (WARN_ONCE(cnt >= size,
1338 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1341 ret = memory_node__read(&nodes[cnt++], idx);
1348 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1353 #define MAX_MEMORY_NODES 2000
1356 * The MEM_TOPOLOGY holds physical memory map for every
1357 * node in system. The format of data is as follows:
1359 * 0 - version | for future changes
1360 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1361 * 16 - count | number of nodes
1363 * For each node we store map of physical indexes for
1366 * 32 - node id | node index
1367 * 40 - size | size of bitmap
1368 * 48 - bitmap | bitmap of memory indexes that belongs to node
1370 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1371 struct perf_evlist *evlist __maybe_unused)
1373 static struct memory_node nodes[MAX_MEMORY_NODES];
1374 u64 bsize, version = 1, i, nr;
1377 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1378 (unsigned long long *) &bsize);
1382 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1386 ret = do_write(ff, &version, sizeof(version));
1390 ret = do_write(ff, &bsize, sizeof(bsize));
1394 ret = do_write(ff, &nr, sizeof(nr));
1398 for (i = 0; i < nr; i++) {
1399 struct memory_node *n = &nodes[i];
1402 ret = do_write(ff, &n->v, sizeof(n->v)); \
1411 ret = do_write_bitmap(ff, n->set, n->size);
1420 static void print_hostname(struct feat_fd *ff, FILE *fp)
1422 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1425 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1427 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1430 static void print_arch(struct feat_fd *ff, FILE *fp)
1432 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1435 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1437 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1440 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1442 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1443 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1446 static void print_version(struct feat_fd *ff, FILE *fp)
1448 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1451 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1455 nr = ff->ph->env.nr_cmdline;
1457 fprintf(fp, "# cmdline : ");
1459 for (i = 0; i < nr; i++) {
1460 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1462 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1466 char *quote = strchr(argv_i, '\'');
1470 fprintf(fp, "%s\\\'", argv_i);
1473 fprintf(fp, "%s ", argv_i);
1480 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1482 struct perf_header *ph = ff->ph;
1483 int cpu_nr = ph->env.nr_cpus_avail;
1487 nr = ph->env.nr_sibling_cores;
1488 str = ph->env.sibling_cores;
1490 for (i = 0; i < nr; i++) {
1491 fprintf(fp, "# sibling cores : %s\n", str);
1492 str += strlen(str) + 1;
1495 nr = ph->env.nr_sibling_threads;
1496 str = ph->env.sibling_threads;
1498 for (i = 0; i < nr; i++) {
1499 fprintf(fp, "# sibling threads : %s\n", str);
1500 str += strlen(str) + 1;
1503 if (ph->env.cpu != NULL) {
1504 for (i = 0; i < cpu_nr; i++)
1505 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1506 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1508 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1511 static void free_event_desc(struct perf_evsel *events)
1513 struct perf_evsel *evsel;
1518 for (evsel = events; evsel->attr.size; evsel++) {
1519 zfree(&evsel->name);
1526 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1528 struct perf_evsel *evsel, *events = NULL;
1531 u32 nre, sz, nr, i, j;
1534 /* number of events */
1535 if (do_read_u32(ff, &nre))
1538 if (do_read_u32(ff, &sz))
1541 /* buffer to hold on file attr struct */
1546 /* the last event terminates with evsel->attr.size == 0: */
1547 events = calloc(nre + 1, sizeof(*events));
1551 msz = sizeof(evsel->attr);
1555 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1559 * must read entire on-file attr struct to
1560 * sync up with layout.
1562 if (__do_read(ff, buf, sz))
1565 if (ff->ph->needs_swap)
1566 perf_event__attr_swap(buf);
1568 memcpy(&evsel->attr, buf, msz);
1570 if (do_read_u32(ff, &nr))
1573 if (ff->ph->needs_swap)
1574 evsel->needs_swap = true;
1576 evsel->name = do_read_string(ff);
1583 id = calloc(nr, sizeof(*id));
1589 for (j = 0 ; j < nr; j++) {
1590 if (do_read_u64(ff, id))
1599 free_event_desc(events);
1604 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1605 void *priv __maybe_unused)
1607 return fprintf(fp, ", %s = %s", name, val);
1610 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1612 struct perf_evsel *evsel, *events;
1617 events = ff->events;
1619 events = read_event_desc(ff);
1622 fprintf(fp, "# event desc: not available or unable to read\n");
1626 for (evsel = events; evsel->attr.size; evsel++) {
1627 fprintf(fp, "# event : name = %s, ", evsel->name);
1630 fprintf(fp, ", id = {");
1631 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1634 fprintf(fp, " %"PRIu64, *id);
1639 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1644 free_event_desc(events);
1648 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1650 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1653 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1656 struct numa_node *n;
1658 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1659 n = &ff->ph->env.numa_nodes[i];
1661 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1662 " free = %"PRIu64" kB\n",
1663 n->node, n->mem_total, n->mem_free);
1665 fprintf(fp, "# node%u cpu list : ", n->node);
1666 cpu_map__fprintf(n->map, fp);
1670 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1672 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1675 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1677 fprintf(fp, "# contains samples with branch stack\n");
1680 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1682 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1685 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1687 fprintf(fp, "# contains stat data\n");
1690 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1694 fprintf(fp, "# CPU cache info:\n");
1695 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1697 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1701 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1703 const char *delimiter = "# pmu mappings: ";
1708 pmu_num = ff->ph->env.nr_pmu_mappings;
1710 fprintf(fp, "# pmu mappings: not available\n");
1714 str = ff->ph->env.pmu_mappings;
1717 type = strtoul(str, &tmp, 0);
1722 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1725 str += strlen(str) + 1;
1734 fprintf(fp, "# pmu mappings: unable to read\n");
1737 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1739 struct perf_session *session;
1740 struct perf_evsel *evsel;
1743 session = container_of(ff->ph, struct perf_session, header);
1745 evlist__for_each_entry(session->evlist, evsel) {
1746 if (perf_evsel__is_group_leader(evsel) &&
1747 evsel->nr_members > 1) {
1748 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1749 perf_evsel__name(evsel));
1751 nr = evsel->nr_members - 1;
1753 fprintf(fp, ",%s", perf_evsel__name(evsel));
1761 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1763 struct perf_session *session;
1767 session = container_of(ff->ph, struct perf_session, header);
1769 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1770 time_buf, sizeof(time_buf));
1771 fprintf(fp, "# time of first sample : %s\n", time_buf);
1773 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1774 time_buf, sizeof(time_buf));
1775 fprintf(fp, "# time of last sample : %s\n", time_buf);
1777 d = (double)(session->evlist->last_sample_time -
1778 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1780 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1783 static void memory_node__fprintf(struct memory_node *n,
1784 unsigned long long bsize, FILE *fp)
1786 char buf_map[100], buf_size[50];
1787 unsigned long long size;
1789 size = bsize * bitmap_weight(n->set, n->size);
1790 unit_number__scnprintf(buf_size, 50, size);
1792 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1793 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1796 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1798 struct memory_node *nodes;
1801 nodes = ff->ph->env.memory_nodes;
1802 nr = ff->ph->env.nr_memory_nodes;
1804 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1805 nr, ff->ph->env.memory_bsize);
1807 for (i = 0; i < nr; i++) {
1808 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1812 static int __event_process_build_id(struct build_id_event *bev,
1814 struct perf_session *session)
1817 struct machine *machine;
1820 enum dso_kernel_type dso_type;
1822 machine = perf_session__findnew_machine(session, bev->pid);
1826 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1829 case PERF_RECORD_MISC_KERNEL:
1830 dso_type = DSO_TYPE_KERNEL;
1832 case PERF_RECORD_MISC_GUEST_KERNEL:
1833 dso_type = DSO_TYPE_GUEST_KERNEL;
1835 case PERF_RECORD_MISC_USER:
1836 case PERF_RECORD_MISC_GUEST_USER:
1837 dso_type = DSO_TYPE_USER;
1843 dso = machine__findnew_dso(machine, filename);
1845 char sbuild_id[SBUILD_ID_SIZE];
1847 dso__set_build_id(dso, &bev->build_id);
1849 if (dso_type != DSO_TYPE_USER) {
1850 struct kmod_path m = { .name = NULL, };
1852 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1853 dso__set_module_info(dso, &m, machine);
1855 dso->kernel = dso_type;
1860 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1862 pr_debug("build id event received for %s: %s\n",
1863 dso->long_name, sbuild_id);
1872 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1873 int input, u64 offset, u64 size)
1875 struct perf_session *session = container_of(header, struct perf_session, header);
1877 struct perf_event_header header;
1878 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1881 struct build_id_event bev;
1882 char filename[PATH_MAX];
1883 u64 limit = offset + size;
1885 while (offset < limit) {
1888 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1891 if (header->needs_swap)
1892 perf_event_header__bswap(&old_bev.header);
1894 len = old_bev.header.size - sizeof(old_bev);
1895 if (readn(input, filename, len) != len)
1898 bev.header = old_bev.header;
1901 * As the pid is the missing value, we need to fill
1902 * it properly. The header.misc value give us nice hint.
1904 bev.pid = HOST_KERNEL_ID;
1905 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1906 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1907 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1909 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1910 __event_process_build_id(&bev, filename, session);
1912 offset += bev.header.size;
1918 static int perf_header__read_build_ids(struct perf_header *header,
1919 int input, u64 offset, u64 size)
1921 struct perf_session *session = container_of(header, struct perf_session, header);
1922 struct build_id_event bev;
1923 char filename[PATH_MAX];
1924 u64 limit = offset + size, orig_offset = offset;
1927 while (offset < limit) {
1930 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1933 if (header->needs_swap)
1934 perf_event_header__bswap(&bev.header);
1936 len = bev.header.size - sizeof(bev);
1937 if (readn(input, filename, len) != len)
1940 * The a1645ce1 changeset:
1942 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1944 * Added a field to struct build_id_event that broke the file
1947 * Since the kernel build-id is the first entry, process the
1948 * table using the old format if the well known
1949 * '[kernel.kallsyms]' string for the kernel build-id has the
1950 * first 4 characters chopped off (where the pid_t sits).
1952 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1953 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1955 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1958 __event_process_build_id(&bev, filename, session);
1960 offset += bev.header.size;
1967 /* Macro for features that simply need to read and store a string. */
1968 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1969 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1971 ff->ph->env.__feat_env = do_read_string(ff); \
1972 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1975 FEAT_PROCESS_STR_FUN(hostname, hostname);
1976 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1977 FEAT_PROCESS_STR_FUN(version, version);
1978 FEAT_PROCESS_STR_FUN(arch, arch);
1979 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1980 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1982 static int process_tracing_data(struct feat_fd *ff, void *data)
1984 ssize_t ret = trace_report(ff->fd, data, false);
1986 return ret < 0 ? -1 : 0;
1989 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1991 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1992 pr_debug("Failed to read buildids, continuing...\n");
1996 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1999 u32 nr_cpus_avail, nr_cpus_online;
2001 ret = do_read_u32(ff, &nr_cpus_avail);
2005 ret = do_read_u32(ff, &nr_cpus_online);
2008 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2009 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2013 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2018 ret = do_read_u64(ff, &total_mem);
2021 ff->ph->env.total_mem = (unsigned long long)total_mem;
2025 static struct perf_evsel *
2026 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2028 struct perf_evsel *evsel;
2030 evlist__for_each_entry(evlist, evsel) {
2031 if (evsel->idx == idx)
2039 perf_evlist__set_event_name(struct perf_evlist *evlist,
2040 struct perf_evsel *event)
2042 struct perf_evsel *evsel;
2047 evsel = perf_evlist__find_by_index(evlist, event->idx);
2054 evsel->name = strdup(event->name);
2058 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2060 struct perf_session *session;
2061 struct perf_evsel *evsel, *events = read_event_desc(ff);
2066 session = container_of(ff->ph, struct perf_session, header);
2068 if (session->data->is_pipe) {
2069 /* Save events for reading later by print_event_desc,
2070 * since they can't be read again in pipe mode. */
2071 ff->events = events;
2074 for (evsel = events; evsel->attr.size; evsel++)
2075 perf_evlist__set_event_name(session->evlist, evsel);
2077 if (!session->data->is_pipe)
2078 free_event_desc(events);
2083 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2085 char *str, *cmdline = NULL, **argv = NULL;
2088 if (do_read_u32(ff, &nr))
2091 ff->ph->env.nr_cmdline = nr;
2093 cmdline = zalloc(ff->size + nr + 1);
2097 argv = zalloc(sizeof(char *) * (nr + 1));
2101 for (i = 0; i < nr; i++) {
2102 str = do_read_string(ff);
2106 argv[i] = cmdline + len;
2107 memcpy(argv[i], str, strlen(str) + 1);
2108 len += strlen(str) + 1;
2111 ff->ph->env.cmdline = cmdline;
2112 ff->ph->env.cmdline_argv = (const char **) argv;
2121 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2126 int cpu_nr = ff->ph->env.nr_cpus_avail;
2128 struct perf_header *ph = ff->ph;
2129 bool do_core_id_test = true;
2131 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2135 if (do_read_u32(ff, &nr))
2138 ph->env.nr_sibling_cores = nr;
2139 size += sizeof(u32);
2140 if (strbuf_init(&sb, 128) < 0)
2143 for (i = 0; i < nr; i++) {
2144 str = do_read_string(ff);
2148 /* include a NULL character at the end */
2149 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2151 size += string_size(str);
2154 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2156 if (do_read_u32(ff, &nr))
2159 ph->env.nr_sibling_threads = nr;
2160 size += sizeof(u32);
2162 for (i = 0; i < nr; i++) {
2163 str = do_read_string(ff);
2167 /* include a NULL character at the end */
2168 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2170 size += string_size(str);
2173 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2176 * The header may be from old perf,
2177 * which doesn't include core id and socket id information.
2179 if (ff->size <= size) {
2180 zfree(&ph->env.cpu);
2184 /* On s390 the socket_id number is not related to the numbers of cpus.
2185 * The socket_id number might be higher than the numbers of cpus.
2186 * This depends on the configuration.
2187 * AArch64 is the same.
2189 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2190 || !strncmp(ph->env.arch, "aarch64", 7)))
2191 do_core_id_test = false;
2193 for (i = 0; i < (u32)cpu_nr; i++) {
2194 if (do_read_u32(ff, &nr))
2197 ph->env.cpu[i].core_id = nr;
2199 if (do_read_u32(ff, &nr))
2202 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2203 pr_debug("socket_id number is too big."
2204 "You may need to upgrade the perf tool.\n");
2208 ph->env.cpu[i].socket_id = nr;
2214 strbuf_release(&sb);
2216 zfree(&ph->env.cpu);
2220 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2222 struct numa_node *nodes, *n;
2227 if (do_read_u32(ff, &nr))
2230 nodes = zalloc(sizeof(*nodes) * nr);
2234 for (i = 0; i < nr; i++) {
2238 if (do_read_u32(ff, &n->node))
2241 if (do_read_u64(ff, &n->mem_total))
2244 if (do_read_u64(ff, &n->mem_free))
2247 str = do_read_string(ff);
2251 n->map = cpu_map__new(str);
2257 ff->ph->env.nr_numa_nodes = nr;
2258 ff->ph->env.numa_nodes = nodes;
2266 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2273 if (do_read_u32(ff, &pmu_num))
2277 pr_debug("pmu mappings not available\n");
2281 ff->ph->env.nr_pmu_mappings = pmu_num;
2282 if (strbuf_init(&sb, 128) < 0)
2286 if (do_read_u32(ff, &type))
2289 name = do_read_string(ff);
2293 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2295 /* include a NULL character at the end */
2296 if (strbuf_add(&sb, "", 1) < 0)
2299 if (!strcmp(name, "msr"))
2300 ff->ph->env.msr_pmu_type = type;
2305 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2309 strbuf_release(&sb);
2313 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2316 u32 i, nr, nr_groups;
2317 struct perf_session *session;
2318 struct perf_evsel *evsel, *leader = NULL;
2325 if (do_read_u32(ff, &nr_groups))
2328 ff->ph->env.nr_groups = nr_groups;
2330 pr_debug("group desc not available\n");
2334 desc = calloc(nr_groups, sizeof(*desc));
2338 for (i = 0; i < nr_groups; i++) {
2339 desc[i].name = do_read_string(ff);
2343 if (do_read_u32(ff, &desc[i].leader_idx))
2346 if (do_read_u32(ff, &desc[i].nr_members))
2351 * Rebuild group relationship based on the group_desc
2353 session = container_of(ff->ph, struct perf_session, header);
2354 session->evlist->nr_groups = nr_groups;
2357 evlist__for_each_entry(session->evlist, evsel) {
2358 if (evsel->idx == (int) desc[i].leader_idx) {
2359 evsel->leader = evsel;
2360 /* {anon_group} is a dummy name */
2361 if (strcmp(desc[i].name, "{anon_group}")) {
2362 evsel->group_name = desc[i].name;
2363 desc[i].name = NULL;
2365 evsel->nr_members = desc[i].nr_members;
2367 if (i >= nr_groups || nr > 0) {
2368 pr_debug("invalid group desc\n");
2373 nr = evsel->nr_members - 1;
2376 /* This is a group member */
2377 evsel->leader = leader;
2383 if (i != nr_groups || nr != 0) {
2384 pr_debug("invalid group desc\n");
2390 for (i = 0; i < nr_groups; i++)
2391 zfree(&desc[i].name);
2397 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2399 struct perf_session *session;
2402 session = container_of(ff->ph, struct perf_session, header);
2404 err = auxtrace_index__process(ff->fd, ff->size, session,
2405 ff->ph->needs_swap);
2407 pr_err("Failed to process auxtrace index\n");
2411 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2413 struct cpu_cache_level *caches;
2414 u32 cnt, i, version;
2416 if (do_read_u32(ff, &version))
2422 if (do_read_u32(ff, &cnt))
2425 caches = zalloc(sizeof(*caches) * cnt);
2429 for (i = 0; i < cnt; i++) {
2430 struct cpu_cache_level c;
2433 if (do_read_u32(ff, &c.v))\
2434 goto out_free_caches; \
2443 c.v = do_read_string(ff); \
2445 goto out_free_caches;
2455 ff->ph->env.caches = caches;
2456 ff->ph->env.caches_cnt = cnt;
2463 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2465 struct perf_session *session;
2466 u64 first_sample_time, last_sample_time;
2469 session = container_of(ff->ph, struct perf_session, header);
2471 ret = do_read_u64(ff, &first_sample_time);
2475 ret = do_read_u64(ff, &last_sample_time);
2479 session->evlist->first_sample_time = first_sample_time;
2480 session->evlist->last_sample_time = last_sample_time;
2484 static int process_mem_topology(struct feat_fd *ff,
2485 void *data __maybe_unused)
2487 struct memory_node *nodes;
2488 u64 version, i, nr, bsize;
2491 if (do_read_u64(ff, &version))
2497 if (do_read_u64(ff, &bsize))
2500 if (do_read_u64(ff, &nr))
2503 nodes = zalloc(sizeof(*nodes) * nr);
2507 for (i = 0; i < nr; i++) {
2508 struct memory_node n;
2511 if (do_read_u64(ff, &n.v)) \
2519 if (do_read_bitmap(ff, &n.set, &n.size))
2525 ff->ph->env.memory_bsize = bsize;
2526 ff->ph->env.memory_nodes = nodes;
2527 ff->ph->env.nr_memory_nodes = nr;
2536 struct feature_ops {
2537 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2538 void (*print)(struct feat_fd *ff, FILE *fp);
2539 int (*process)(struct feat_fd *ff, void *data);
2545 #define FEAT_OPR(n, func, __full_only) \
2547 .name = __stringify(n), \
2548 .write = write_##func, \
2549 .print = print_##func, \
2550 .full_only = __full_only, \
2551 .process = process_##func, \
2552 .synthesize = true \
2555 #define FEAT_OPN(n, func, __full_only) \
2557 .name = __stringify(n), \
2558 .write = write_##func, \
2559 .print = print_##func, \
2560 .full_only = __full_only, \
2561 .process = process_##func \
2564 /* feature_ops not implemented: */
2565 #define print_tracing_data NULL
2566 #define print_build_id NULL
2568 #define process_branch_stack NULL
2569 #define process_stat NULL
2572 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2573 FEAT_OPN(TRACING_DATA, tracing_data, false),
2574 FEAT_OPN(BUILD_ID, build_id, false),
2575 FEAT_OPR(HOSTNAME, hostname, false),
2576 FEAT_OPR(OSRELEASE, osrelease, false),
2577 FEAT_OPR(VERSION, version, false),
2578 FEAT_OPR(ARCH, arch, false),
2579 FEAT_OPR(NRCPUS, nrcpus, false),
2580 FEAT_OPR(CPUDESC, cpudesc, false),
2581 FEAT_OPR(CPUID, cpuid, false),
2582 FEAT_OPR(TOTAL_MEM, total_mem, false),
2583 FEAT_OPR(EVENT_DESC, event_desc, false),
2584 FEAT_OPR(CMDLINE, cmdline, false),
2585 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2586 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2587 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2588 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2589 FEAT_OPR(GROUP_DESC, group_desc, false),
2590 FEAT_OPN(AUXTRACE, auxtrace, false),
2591 FEAT_OPN(STAT, stat, false),
2592 FEAT_OPN(CACHE, cache, true),
2593 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2594 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2597 struct header_print_data {
2599 bool full; /* extended list of headers */
2602 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2603 struct perf_header *ph,
2604 int feat, int fd, void *data)
2606 struct header_print_data *hd = data;
2609 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2610 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2611 "%d, continuing...\n", section->offset, feat);
2614 if (feat >= HEADER_LAST_FEATURE) {
2615 pr_warning("unknown feature %d\n", feat);
2618 if (!feat_ops[feat].print)
2621 ff = (struct feat_fd) {
2626 if (!feat_ops[feat].full_only || hd->full)
2627 feat_ops[feat].print(&ff, hd->fp);
2629 fprintf(hd->fp, "# %s info available, use -I to display\n",
2630 feat_ops[feat].name);
2635 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2637 struct header_print_data hd;
2638 struct perf_header *header = &session->header;
2639 int fd = perf_data__fd(session->data);
2647 ret = fstat(fd, &st);
2651 stctime = st.st_ctime;
2652 fprintf(fp, "# captured on : %s", ctime(&stctime));
2654 fprintf(fp, "# header version : %u\n", header->version);
2655 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2656 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2657 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2659 perf_header__process_sections(header, fd, &hd,
2660 perf_file_section__fprintf_info);
2662 if (session->data->is_pipe)
2665 fprintf(fp, "# missing features: ");
2666 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2668 fprintf(fp, "%s ", feat_ops[bit].name);
2675 static int do_write_feat(struct feat_fd *ff, int type,
2676 struct perf_file_section **p,
2677 struct perf_evlist *evlist)
2682 if (perf_header__has_feat(ff->ph, type)) {
2683 if (!feat_ops[type].write)
2686 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2689 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2691 err = feat_ops[type].write(ff, evlist);
2693 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2695 /* undo anything written */
2696 lseek(ff->fd, (*p)->offset, SEEK_SET);
2700 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2706 static int perf_header__adds_write(struct perf_header *header,
2707 struct perf_evlist *evlist, int fd)
2711 struct perf_file_section *feat_sec, *p;
2717 ff = (struct feat_fd){
2722 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2726 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2727 if (feat_sec == NULL)
2730 sec_size = sizeof(*feat_sec) * nr_sections;
2732 sec_start = header->feat_offset;
2733 lseek(fd, sec_start + sec_size, SEEK_SET);
2735 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2736 if (do_write_feat(&ff, feat, &p, evlist))
2737 perf_header__clear_feat(header, feat);
2740 lseek(fd, sec_start, SEEK_SET);
2742 * may write more than needed due to dropped feature, but
2743 * this is okay, reader will skip the mising entries
2745 err = do_write(&ff, feat_sec, sec_size);
2747 pr_debug("failed to write feature section\n");
2752 int perf_header__write_pipe(int fd)
2754 struct perf_pipe_file_header f_header;
2758 ff = (struct feat_fd){ .fd = fd };
2760 f_header = (struct perf_pipe_file_header){
2761 .magic = PERF_MAGIC,
2762 .size = sizeof(f_header),
2765 err = do_write(&ff, &f_header, sizeof(f_header));
2767 pr_debug("failed to write perf pipe header\n");
2774 int perf_session__write_header(struct perf_session *session,
2775 struct perf_evlist *evlist,
2776 int fd, bool at_exit)
2778 struct perf_file_header f_header;
2779 struct perf_file_attr f_attr;
2780 struct perf_header *header = &session->header;
2781 struct perf_evsel *evsel;
2786 ff = (struct feat_fd){ .fd = fd};
2787 lseek(fd, sizeof(f_header), SEEK_SET);
2789 evlist__for_each_entry(session->evlist, evsel) {
2790 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2791 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2793 pr_debug("failed to write perf header\n");
2798 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2800 evlist__for_each_entry(evlist, evsel) {
2801 f_attr = (struct perf_file_attr){
2802 .attr = evsel->attr,
2804 .offset = evsel->id_offset,
2805 .size = evsel->ids * sizeof(u64),
2808 err = do_write(&ff, &f_attr, sizeof(f_attr));
2810 pr_debug("failed to write perf header attribute\n");
2815 if (!header->data_offset)
2816 header->data_offset = lseek(fd, 0, SEEK_CUR);
2817 header->feat_offset = header->data_offset + header->data_size;
2820 err = perf_header__adds_write(header, evlist, fd);
2825 f_header = (struct perf_file_header){
2826 .magic = PERF_MAGIC,
2827 .size = sizeof(f_header),
2828 .attr_size = sizeof(f_attr),
2830 .offset = attr_offset,
2831 .size = evlist->nr_entries * sizeof(f_attr),
2834 .offset = header->data_offset,
2835 .size = header->data_size,
2837 /* event_types is ignored, store zeros */
2840 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2842 lseek(fd, 0, SEEK_SET);
2843 err = do_write(&ff, &f_header, sizeof(f_header));
2845 pr_debug("failed to write perf header\n");
2848 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2853 static int perf_header__getbuffer64(struct perf_header *header,
2854 int fd, void *buf, size_t size)
2856 if (readn(fd, buf, size) <= 0)
2859 if (header->needs_swap)
2860 mem_bswap_64(buf, size);
2865 int perf_header__process_sections(struct perf_header *header, int fd,
2867 int (*process)(struct perf_file_section *section,
2868 struct perf_header *ph,
2869 int feat, int fd, void *data))
2871 struct perf_file_section *feat_sec, *sec;
2877 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2881 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2885 sec_size = sizeof(*feat_sec) * nr_sections;
2887 lseek(fd, header->feat_offset, SEEK_SET);
2889 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2893 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2894 err = process(sec++, header, feat, fd, data);
2904 static const int attr_file_abi_sizes[] = {
2905 [0] = PERF_ATTR_SIZE_VER0,
2906 [1] = PERF_ATTR_SIZE_VER1,
2907 [2] = PERF_ATTR_SIZE_VER2,
2908 [3] = PERF_ATTR_SIZE_VER3,
2909 [4] = PERF_ATTR_SIZE_VER4,
2914 * In the legacy file format, the magic number is not used to encode endianness.
2915 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2916 * on ABI revisions, we need to try all combinations for all endianness to
2917 * detect the endianness.
2919 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2921 uint64_t ref_size, attr_size;
2924 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2925 ref_size = attr_file_abi_sizes[i]
2926 + sizeof(struct perf_file_section);
2927 if (hdr_sz != ref_size) {
2928 attr_size = bswap_64(hdr_sz);
2929 if (attr_size != ref_size)
2932 ph->needs_swap = true;
2934 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2939 /* could not determine endianness */
2943 #define PERF_PIPE_HDR_VER0 16
2945 static const size_t attr_pipe_abi_sizes[] = {
2946 [0] = PERF_PIPE_HDR_VER0,
2951 * In the legacy pipe format, there is an implicit assumption that endiannesss
2952 * between host recording the samples, and host parsing the samples is the
2953 * same. This is not always the case given that the pipe output may always be
2954 * redirected into a file and analyzed on a different machine with possibly a
2955 * different endianness and perf_event ABI revsions in the perf tool itself.
2957 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2962 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2963 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2964 attr_size = bswap_64(hdr_sz);
2965 if (attr_size != hdr_sz)
2968 ph->needs_swap = true;
2970 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2976 bool is_perf_magic(u64 magic)
2978 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2979 || magic == __perf_magic2
2980 || magic == __perf_magic2_sw)
2986 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2987 bool is_pipe, struct perf_header *ph)
2991 /* check for legacy format */
2992 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2994 ph->version = PERF_HEADER_VERSION_1;
2995 pr_debug("legacy perf.data format\n");
2997 return try_all_pipe_abis(hdr_sz, ph);
2999 return try_all_file_abis(hdr_sz, ph);
3002 * the new magic number serves two purposes:
3003 * - unique number to identify actual perf.data files
3004 * - encode endianness of file
3006 ph->version = PERF_HEADER_VERSION_2;
3008 /* check magic number with one endianness */
3009 if (magic == __perf_magic2)
3012 /* check magic number with opposite endianness */
3013 if (magic != __perf_magic2_sw)
3016 ph->needs_swap = true;
3021 int perf_file_header__read(struct perf_file_header *header,
3022 struct perf_header *ph, int fd)
3026 lseek(fd, 0, SEEK_SET);
3028 ret = readn(fd, header, sizeof(*header));
3032 if (check_magic_endian(header->magic,
3033 header->attr_size, false, ph) < 0) {
3034 pr_debug("magic/endian check failed\n");
3038 if (ph->needs_swap) {
3039 mem_bswap_64(header, offsetof(struct perf_file_header,
3043 if (header->size != sizeof(*header)) {
3044 /* Support the previous format */
3045 if (header->size == offsetof(typeof(*header), adds_features))
3046 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3049 } else if (ph->needs_swap) {
3051 * feature bitmap is declared as an array of unsigned longs --
3052 * not good since its size can differ between the host that
3053 * generated the data file and the host analyzing the file.
3055 * We need to handle endianness, but we don't know the size of
3056 * the unsigned long where the file was generated. Take a best
3057 * guess at determining it: try 64-bit swap first (ie., file
3058 * created on a 64-bit host), and check if the hostname feature
3059 * bit is set (this feature bit is forced on as of fbe96f2).
3060 * If the bit is not, undo the 64-bit swap and try a 32-bit
3061 * swap. If the hostname bit is still not set (e.g., older data
3062 * file), punt and fallback to the original behavior --
3063 * clearing all feature bits and setting buildid.
3065 mem_bswap_64(&header->adds_features,
3066 BITS_TO_U64(HEADER_FEAT_BITS));
3068 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3070 mem_bswap_64(&header->adds_features,
3071 BITS_TO_U64(HEADER_FEAT_BITS));
3074 mem_bswap_32(&header->adds_features,
3075 BITS_TO_U32(HEADER_FEAT_BITS));
3078 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3079 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3080 set_bit(HEADER_BUILD_ID, header->adds_features);
3084 memcpy(&ph->adds_features, &header->adds_features,
3085 sizeof(ph->adds_features));
3087 ph->data_offset = header->data.offset;
3088 ph->data_size = header->data.size;
3089 ph->feat_offset = header->data.offset + header->data.size;
3093 static int perf_file_section__process(struct perf_file_section *section,
3094 struct perf_header *ph,
3095 int feat, int fd, void *data)
3097 struct feat_fd fdd = {
3100 .size = section->size,
3101 .offset = section->offset,
3104 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3105 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3106 "%d, continuing...\n", section->offset, feat);
3110 if (feat >= HEADER_LAST_FEATURE) {
3111 pr_debug("unknown feature %d, continuing...\n", feat);
3115 if (!feat_ops[feat].process)
3118 return feat_ops[feat].process(&fdd, data);
3121 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3122 struct perf_header *ph, int fd,
3125 struct feat_fd ff = {
3126 .fd = STDOUT_FILENO,
3131 ret = readn(fd, header, sizeof(*header));
3135 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3136 pr_debug("endian/magic failed\n");
3141 header->size = bswap_64(header->size);
3143 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3149 static int perf_header__read_pipe(struct perf_session *session)
3151 struct perf_header *header = &session->header;
3152 struct perf_pipe_file_header f_header;
3154 if (perf_file_header__read_pipe(&f_header, header,
3155 perf_data__fd(session->data),
3156 session->repipe) < 0) {
3157 pr_debug("incompatible file format\n");
3164 static int read_attr(int fd, struct perf_header *ph,
3165 struct perf_file_attr *f_attr)
3167 struct perf_event_attr *attr = &f_attr->attr;
3169 size_t our_sz = sizeof(f_attr->attr);
3172 memset(f_attr, 0, sizeof(*f_attr));
3174 /* read minimal guaranteed structure */
3175 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3177 pr_debug("cannot read %d bytes of header attr\n",
3178 PERF_ATTR_SIZE_VER0);
3182 /* on file perf_event_attr size */
3190 sz = PERF_ATTR_SIZE_VER0;
3191 } else if (sz > our_sz) {
3192 pr_debug("file uses a more recent and unsupported ABI"
3193 " (%zu bytes extra)\n", sz - our_sz);
3196 /* what we have not yet read and that we know about */
3197 left = sz - PERF_ATTR_SIZE_VER0;
3200 ptr += PERF_ATTR_SIZE_VER0;
3202 ret = readn(fd, ptr, left);
3204 /* read perf_file_section, ids are read in caller */
3205 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3207 return ret <= 0 ? -1 : 0;
3210 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
3211 struct tep_handle *pevent)
3213 struct event_format *event;
3216 /* already prepared */
3217 if (evsel->tp_format)
3220 if (pevent == NULL) {
3221 pr_debug("broken or missing trace data\n");
3225 event = tep_find_event(pevent, evsel->attr.config);
3226 if (event == NULL) {
3227 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
3232 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3233 evsel->name = strdup(bf);
3234 if (evsel->name == NULL)
3238 evsel->tp_format = event;
3242 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
3243 struct tep_handle *pevent)
3245 struct perf_evsel *pos;
3247 evlist__for_each_entry(evlist, pos) {
3248 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3249 perf_evsel__prepare_tracepoint_event(pos, pevent))
3256 int perf_session__read_header(struct perf_session *session)
3258 struct perf_data *data = session->data;
3259 struct perf_header *header = &session->header;
3260 struct perf_file_header f_header;
3261 struct perf_file_attr f_attr;
3263 int nr_attrs, nr_ids, i, j;
3264 int fd = perf_data__fd(data);
3266 session->evlist = perf_evlist__new();
3267 if (session->evlist == NULL)
3270 session->evlist->env = &header->env;
3271 session->machines.host.env = &header->env;
3272 if (perf_data__is_pipe(data))
3273 return perf_header__read_pipe(session);
3275 if (perf_file_header__read(&f_header, header, fd) < 0)
3279 * Sanity check that perf.data was written cleanly; data size is
3280 * initialized to 0 and updated only if the on_exit function is run.
3281 * If data size is still 0 then the file contains only partial
3282 * information. Just warn user and process it as much as it can.
3284 if (f_header.data.size == 0) {
3285 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3286 "Was the 'perf record' command properly terminated?\n",
3290 if (f_header.attr_size == 0) {
3291 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3292 "Was the 'perf record' command properly terminated?\n",
3297 nr_attrs = f_header.attrs.size / f_header.attr_size;
3298 lseek(fd, f_header.attrs.offset, SEEK_SET);
3300 for (i = 0; i < nr_attrs; i++) {
3301 struct perf_evsel *evsel;
3304 if (read_attr(fd, header, &f_attr) < 0)
3307 if (header->needs_swap) {
3308 f_attr.ids.size = bswap_64(f_attr.ids.size);
3309 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3310 perf_event__attr_swap(&f_attr.attr);
3313 tmp = lseek(fd, 0, SEEK_CUR);
3314 evsel = perf_evsel__new(&f_attr.attr);
3317 goto out_delete_evlist;
3319 evsel->needs_swap = header->needs_swap;
3321 * Do it before so that if perf_evsel__alloc_id fails, this
3322 * entry gets purged too at perf_evlist__delete().
3324 perf_evlist__add(session->evlist, evsel);
3326 nr_ids = f_attr.ids.size / sizeof(u64);
3328 * We don't have the cpu and thread maps on the header, so
3329 * for allocating the perf_sample_id table we fake 1 cpu and
3330 * hattr->ids threads.
3332 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3333 goto out_delete_evlist;
3335 lseek(fd, f_attr.ids.offset, SEEK_SET);
3337 for (j = 0; j < nr_ids; j++) {
3338 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3341 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
3344 lseek(fd, tmp, SEEK_SET);
3347 perf_header__process_sections(header, fd, &session->tevent,
3348 perf_file_section__process);
3350 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3351 session->tevent.pevent))
3352 goto out_delete_evlist;
3359 perf_evlist__delete(session->evlist);
3360 session->evlist = NULL;
3364 int perf_event__synthesize_attr(struct perf_tool *tool,
3365 struct perf_event_attr *attr, u32 ids, u64 *id,
3366 perf_event__handler_t process)
3368 union perf_event *ev;
3372 size = sizeof(struct perf_event_attr);
3373 size = PERF_ALIGN(size, sizeof(u64));
3374 size += sizeof(struct perf_event_header);
3375 size += ids * sizeof(u64);
3382 ev->attr.attr = *attr;
3383 memcpy(ev->attr.id, id, ids * sizeof(u64));
3385 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3386 ev->attr.header.size = (u16)size;
3388 if (ev->attr.header.size == size)
3389 err = process(tool, ev, NULL, NULL);
3398 int perf_event__synthesize_features(struct perf_tool *tool,
3399 struct perf_session *session,
3400 struct perf_evlist *evlist,
3401 perf_event__handler_t process)
3403 struct perf_header *header = &session->header;
3405 struct feature_event *fe;
3409 sz_hdr = sizeof(fe->header);
3410 sz = sizeof(union perf_event);
3411 /* get a nice alignment */
3412 sz = PERF_ALIGN(sz, page_size);
3414 memset(&ff, 0, sizeof(ff));
3416 ff.buf = malloc(sz);
3420 ff.size = sz - sz_hdr;
3422 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3423 if (!feat_ops[feat].synthesize) {
3424 pr_debug("No record header feature for header :%d\n", feat);
3428 ff.offset = sizeof(*fe);
3430 ret = feat_ops[feat].write(&ff, evlist);
3431 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3432 pr_debug("Error writing feature\n");
3435 /* ff.buf may have changed due to realloc in do_write() */
3437 memset(fe, 0, sizeof(*fe));
3440 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3441 fe->header.size = ff.offset;
3443 ret = process(tool, ff.buf, NULL, NULL);
3450 /* Send HEADER_LAST_FEATURE mark. */
3452 fe->feat_id = HEADER_LAST_FEATURE;
3453 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3454 fe->header.size = sizeof(*fe);
3456 ret = process(tool, ff.buf, NULL, NULL);
3462 int perf_event__process_feature(struct perf_tool *tool,
3463 union perf_event *event,
3464 struct perf_session *session __maybe_unused)
3466 struct feat_fd ff = { .fd = 0 };
3467 struct feature_event *fe = (struct feature_event *)event;
3468 int type = fe->header.type;
3469 u64 feat = fe->feat_id;
3471 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3472 pr_warning("invalid record type %d in pipe-mode\n", type);
3475 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3476 pr_warning("invalid record type %d in pipe-mode\n", type);
3480 if (!feat_ops[feat].process)
3483 ff.buf = (void *)fe->data;
3484 ff.size = event->header.size - sizeof(*fe);
3485 ff.ph = &session->header;
3487 if (feat_ops[feat].process(&ff, NULL))
3490 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3493 if (!feat_ops[feat].full_only ||
3494 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3495 feat_ops[feat].print(&ff, stdout);
3497 fprintf(stdout, "# %s info available, use -I to display\n",
3498 feat_ops[feat].name);
3504 static struct event_update_event *
3505 event_update_event__new(size_t size, u64 type, u64 id)
3507 struct event_update_event *ev;
3509 size += sizeof(*ev);
3510 size = PERF_ALIGN(size, sizeof(u64));
3514 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3515 ev->header.size = (u16)size;
3523 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3524 struct perf_evsel *evsel,
3525 perf_event__handler_t process)
3527 struct event_update_event *ev;
3528 size_t size = strlen(evsel->unit);
3531 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3535 strlcpy(ev->data, evsel->unit, size + 1);
3536 err = process(tool, (union perf_event *)ev, NULL, NULL);
3542 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3543 struct perf_evsel *evsel,
3544 perf_event__handler_t process)
3546 struct event_update_event *ev;
3547 struct event_update_event_scale *ev_data;
3550 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3554 ev_data = (struct event_update_event_scale *) ev->data;
3555 ev_data->scale = evsel->scale;
3556 err = process(tool, (union perf_event*) ev, NULL, NULL);
3562 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3563 struct perf_evsel *evsel,
3564 perf_event__handler_t process)
3566 struct event_update_event *ev;
3567 size_t len = strlen(evsel->name);
3570 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3574 strlcpy(ev->data, evsel->name, len + 1);
3575 err = process(tool, (union perf_event*) ev, NULL, NULL);
3581 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3582 struct perf_evsel *evsel,
3583 perf_event__handler_t process)
3585 size_t size = sizeof(struct event_update_event);
3586 struct event_update_event *ev;
3590 if (!evsel->own_cpus)
3593 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3597 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3598 ev->header.size = (u16)size;
3599 ev->type = PERF_EVENT_UPDATE__CPUS;
3600 ev->id = evsel->id[0];
3602 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3606 err = process(tool, (union perf_event*) ev, NULL, NULL);
3611 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
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 cpu_map *map;
3619 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3622 case PERF_EVENT_UPDATE__SCALE:
3623 ev_scale = (struct event_update_event_scale *) ev->data;
3624 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3626 case PERF_EVENT_UPDATE__UNIT:
3627 ret += fprintf(fp, "... unit: %s\n", ev->data);
3629 case PERF_EVENT_UPDATE__NAME:
3630 ret += fprintf(fp, "... name: %s\n", ev->data);
3632 case PERF_EVENT_UPDATE__CPUS:
3633 ev_cpus = (struct event_update_event_cpus *) ev->data;
3634 ret += fprintf(fp, "... ");
3636 map = cpu_map__new_data(&ev_cpus->cpus);
3638 ret += cpu_map__fprintf(map, fp);
3640 ret += fprintf(fp, "failed to get cpus\n");
3643 ret += fprintf(fp, "... unknown type\n");
3650 int perf_event__synthesize_attrs(struct perf_tool *tool,
3651 struct perf_session *session,
3652 perf_event__handler_t process)
3654 struct perf_evsel *evsel;
3657 evlist__for_each_entry(session->evlist, evsel) {
3658 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3659 evsel->id, process);
3661 pr_debug("failed to create perf header attribute\n");
3669 static bool has_unit(struct perf_evsel *counter)
3671 return counter->unit && *counter->unit;
3674 static bool has_scale(struct perf_evsel *counter)
3676 return counter->scale != 1;
3679 int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3680 struct perf_evlist *evsel_list,
3681 perf_event__handler_t process,
3684 struct perf_evsel *counter;
3688 * Synthesize other events stuff not carried within
3689 * attr event - unit, scale, name
3691 evlist__for_each_entry(evsel_list, counter) {
3692 if (!counter->supported)
3696 * Synthesize unit and scale only if it's defined.
3698 if (has_unit(counter)) {
3699 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3701 pr_err("Couldn't synthesize evsel unit.\n");
3706 if (has_scale(counter)) {
3707 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3709 pr_err("Couldn't synthesize evsel counter.\n");
3714 if (counter->own_cpus) {
3715 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3717 pr_err("Couldn't synthesize evsel cpus.\n");
3723 * Name is needed only for pipe output,
3724 * perf.data carries event names.
3727 err = perf_event__synthesize_event_update_name(tool, counter, process);
3729 pr_err("Couldn't synthesize evsel name.\n");
3737 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3738 union perf_event *event,
3739 struct perf_evlist **pevlist)
3742 struct perf_evsel *evsel;
3743 struct perf_evlist *evlist = *pevlist;
3745 if (evlist == NULL) {
3746 *pevlist = evlist = perf_evlist__new();
3751 evsel = perf_evsel__new(&event->attr.attr);
3755 perf_evlist__add(evlist, evsel);
3757 ids = event->header.size;
3758 ids -= (void *)&event->attr.id - (void *)event;
3759 n_ids = ids / sizeof(u64);
3761 * We don't have the cpu and thread maps on the header, so
3762 * for allocating the perf_sample_id table we fake 1 cpu and
3763 * hattr->ids threads.
3765 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3768 for (i = 0; i < n_ids; i++) {
3769 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3775 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3776 union perf_event *event,
3777 struct perf_evlist **pevlist)
3779 struct event_update_event *ev = &event->event_update;
3780 struct event_update_event_scale *ev_scale;
3781 struct event_update_event_cpus *ev_cpus;
3782 struct perf_evlist *evlist;
3783 struct perf_evsel *evsel;
3784 struct cpu_map *map;
3786 if (!pevlist || *pevlist == NULL)
3791 evsel = perf_evlist__id2evsel(evlist, ev->id);
3796 case PERF_EVENT_UPDATE__UNIT:
3797 evsel->unit = strdup(ev->data);
3799 case PERF_EVENT_UPDATE__NAME:
3800 evsel->name = strdup(ev->data);
3802 case PERF_EVENT_UPDATE__SCALE:
3803 ev_scale = (struct event_update_event_scale *) ev->data;
3804 evsel->scale = ev_scale->scale;
3806 case PERF_EVENT_UPDATE__CPUS:
3807 ev_cpus = (struct event_update_event_cpus *) ev->data;
3809 map = cpu_map__new_data(&ev_cpus->cpus);
3811 evsel->own_cpus = map;
3813 pr_err("failed to get event_update cpus\n");
3821 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3822 struct perf_evlist *evlist,
3823 perf_event__handler_t process)
3825 union perf_event ev;
3826 struct tracing_data *tdata;
3827 ssize_t size = 0, aligned_size = 0, padding;
3829 int err __maybe_unused = 0;
3832 * We are going to store the size of the data followed
3833 * by the data contents. Since the fd descriptor is a pipe,
3834 * we cannot seek back to store the size of the data once
3835 * we know it. Instead we:
3837 * - write the tracing data to the temp file
3838 * - get/write the data size to pipe
3839 * - write the tracing data from the temp file
3842 tdata = tracing_data_get(&evlist->entries, fd, true);
3846 memset(&ev, 0, sizeof(ev));
3848 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3850 aligned_size = PERF_ALIGN(size, sizeof(u64));
3851 padding = aligned_size - size;
3852 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3853 ev.tracing_data.size = aligned_size;
3855 process(tool, &ev, NULL, NULL);
3858 * The put function will copy all the tracing data
3859 * stored in temp file to the pipe.
3861 tracing_data_put(tdata);
3863 ff = (struct feat_fd){ .fd = fd };
3864 if (write_padded(&ff, NULL, 0, padding))
3867 return aligned_size;
3870 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3871 union perf_event *event,
3872 struct perf_session *session)
3874 ssize_t size_read, padding, size = event->tracing_data.size;
3875 int fd = perf_data__fd(session->data);
3876 off_t offset = lseek(fd, 0, SEEK_CUR);
3879 /* setup for reading amidst mmap */
3880 lseek(fd, offset + sizeof(struct tracing_data_event),
3883 size_read = trace_report(fd, &session->tevent,
3885 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3887 if (readn(fd, buf, padding) < 0) {
3888 pr_err("%s: reading input file", __func__);
3891 if (session->repipe) {
3892 int retw = write(STDOUT_FILENO, buf, padding);
3893 if (retw <= 0 || retw != padding) {
3894 pr_err("%s: repiping tracing data padding", __func__);
3899 if (size_read + padding != size) {
3900 pr_err("%s: tracing data size mismatch", __func__);
3904 perf_evlist__prepare_tracepoint_events(session->evlist,
3905 session->tevent.pevent);
3907 return size_read + padding;
3910 int perf_event__synthesize_build_id(struct perf_tool *tool,
3911 struct dso *pos, u16 misc,
3912 perf_event__handler_t process,
3913 struct machine *machine)
3915 union perf_event ev;
3922 memset(&ev, 0, sizeof(ev));
3924 len = pos->long_name_len + 1;
3925 len = PERF_ALIGN(len, NAME_ALIGN);
3926 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3927 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3928 ev.build_id.header.misc = misc;
3929 ev.build_id.pid = machine->pid;
3930 ev.build_id.header.size = sizeof(ev.build_id) + len;
3931 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3933 err = process(tool, &ev, NULL, machine);
3938 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3939 union perf_event *event,
3940 struct perf_session *session)
3942 __event_process_build_id(&event->build_id,
3943 event->build_id.filename,