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/types.h>
19 #include <sys/utsname.h>
27 #include "trace-event.h"
37 #include <api/fs/fs.h>
41 #include "sane_ctype.h"
45 * must be a numerical value to let the endianness
46 * determine the memory layout. That way we are able
47 * to detect endianness when reading the perf.data file
50 * we check for legacy (PERFFILE) format.
52 static const char *__perf_magic1 = "PERFFILE";
53 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
54 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
56 #define PERF_MAGIC __perf_magic2
58 const char perf_version_string[] = PERF_VERSION;
60 struct perf_file_attr {
61 struct perf_event_attr attr;
62 struct perf_file_section ids;
66 struct perf_header *ph;
68 void *buf; /* Either buf != NULL or fd >= 0 */
71 struct perf_evsel *events;
74 void perf_header__set_feat(struct perf_header *header, int feat)
76 set_bit(feat, header->adds_features);
79 void perf_header__clear_feat(struct perf_header *header, int feat)
81 clear_bit(feat, header->adds_features);
84 bool perf_header__has_feat(const struct perf_header *header, int feat)
86 return test_bit(feat, header->adds_features);
89 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
91 ssize_t ret = writen(ff->fd, buf, size);
93 if (ret != (ssize_t)size)
94 return ret < 0 ? (int)ret : -1;
98 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
100 /* struct perf_event_header::size is u16 */
101 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
102 size_t new_size = ff->size;
105 if (size + ff->offset > max_size)
108 while (size > (new_size - ff->offset))
110 new_size = min(max_size, new_size);
112 if (ff->size < new_size) {
113 addr = realloc(ff->buf, new_size);
120 memcpy(ff->buf + ff->offset, buf, size);
126 /* Return: 0 if succeded, -ERR if failed. */
127 int do_write(struct feat_fd *ff, const void *buf, size_t size)
130 return __do_write_fd(ff, buf, size);
131 return __do_write_buf(ff, buf, size);
134 /* Return: 0 if succeded, -ERR if failed. */
135 int write_padded(struct feat_fd *ff, const void *bf,
136 size_t count, size_t count_aligned)
138 static const char zero_buf[NAME_ALIGN];
139 int err = do_write(ff, bf, count);
142 err = do_write(ff, zero_buf, count_aligned - count);
147 #define string_size(str) \
148 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
150 /* Return: 0 if succeded, -ERR if failed. */
151 static int do_write_string(struct feat_fd *ff, const char *str)
156 olen = strlen(str) + 1;
157 len = PERF_ALIGN(olen, NAME_ALIGN);
159 /* write len, incl. \0 */
160 ret = do_write(ff, &len, sizeof(len));
164 return write_padded(ff, str, olen, len);
167 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
169 ssize_t ret = readn(ff->fd, addr, size);
172 return ret < 0 ? (int)ret : -1;
176 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
178 if (size > (ssize_t)ff->size - ff->offset)
181 memcpy(addr, ff->buf + ff->offset, size);
188 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
191 return __do_read_fd(ff, addr, size);
192 return __do_read_buf(ff, addr, size);
195 static int do_read_u32(struct feat_fd *ff, u32 *addr)
199 ret = __do_read(ff, addr, sizeof(*addr));
203 if (ff->ph->needs_swap)
204 *addr = bswap_32(*addr);
208 static int do_read_u64(struct feat_fd *ff, u64 *addr)
212 ret = __do_read(ff, addr, sizeof(*addr));
216 if (ff->ph->needs_swap)
217 *addr = bswap_64(*addr);
221 static char *do_read_string(struct feat_fd *ff)
226 if (do_read_u32(ff, &len))
233 if (!__do_read(ff, buf, len)) {
235 * strings are padded by zeroes
236 * thus the actual strlen of buf
237 * may be less than len
246 static int write_tracing_data(struct feat_fd *ff,
247 struct perf_evlist *evlist)
249 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
252 return read_tracing_data(ff->fd, &evlist->entries);
255 static int write_build_id(struct feat_fd *ff,
256 struct perf_evlist *evlist __maybe_unused)
258 struct perf_session *session;
261 session = container_of(ff->ph, struct perf_session, header);
263 if (!perf_session__read_build_ids(session, true))
266 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
269 err = perf_session__write_buildid_table(session, ff);
271 pr_debug("failed to write buildid table\n");
274 perf_session__cache_build_ids(session);
279 static int write_hostname(struct feat_fd *ff,
280 struct perf_evlist *evlist __maybe_unused)
289 return do_write_string(ff, uts.nodename);
292 static int write_osrelease(struct feat_fd *ff,
293 struct perf_evlist *evlist __maybe_unused)
302 return do_write_string(ff, uts.release);
305 static int write_arch(struct feat_fd *ff,
306 struct perf_evlist *evlist __maybe_unused)
315 return do_write_string(ff, uts.machine);
318 static int write_version(struct feat_fd *ff,
319 struct perf_evlist *evlist __maybe_unused)
321 return do_write_string(ff, perf_version_string);
324 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
329 const char *search = cpuinfo_proc;
336 file = fopen("/proc/cpuinfo", "r");
340 while (getline(&buf, &len, file) > 0) {
341 ret = strncmp(buf, search, strlen(search));
353 p = strchr(buf, ':');
354 if (p && *(p+1) == ' ' && *(p+2))
360 /* squash extra space characters (branding string) */
367 while (*q && isspace(*q))
370 while ((*r++ = *q++));
374 ret = do_write_string(ff, s);
381 static int write_cpudesc(struct feat_fd *ff,
382 struct perf_evlist *evlist __maybe_unused)
384 const char *cpuinfo_procs[] = CPUINFO_PROC;
387 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
389 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
397 static int write_nrcpus(struct feat_fd *ff,
398 struct perf_evlist *evlist __maybe_unused)
404 nrc = cpu__max_present_cpu();
406 nr = sysconf(_SC_NPROCESSORS_ONLN);
410 nra = (u32)(nr & UINT_MAX);
412 ret = do_write(ff, &nrc, sizeof(nrc));
416 return do_write(ff, &nra, sizeof(nra));
419 static int write_event_desc(struct feat_fd *ff,
420 struct perf_evlist *evlist)
422 struct perf_evsel *evsel;
426 nre = evlist->nr_entries;
429 * write number of events
431 ret = do_write(ff, &nre, sizeof(nre));
436 * size of perf_event_attr struct
438 sz = (u32)sizeof(evsel->attr);
439 ret = do_write(ff, &sz, sizeof(sz));
443 evlist__for_each_entry(evlist, evsel) {
444 ret = do_write(ff, &evsel->attr, sz);
448 * write number of unique id per event
449 * there is one id per instance of an event
451 * copy into an nri to be independent of the
455 ret = do_write(ff, &nri, sizeof(nri));
460 * write event string as passed on cmdline
462 ret = do_write_string(ff, perf_evsel__name(evsel));
466 * write unique ids for this event
468 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
475 static int write_cmdline(struct feat_fd *ff,
476 struct perf_evlist *evlist __maybe_unused)
478 char buf[MAXPATHLEN];
482 /* actual path to perf binary */
483 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
487 /* readlink() does not add null termination */
490 /* account for binary path */
491 n = perf_env.nr_cmdline + 1;
493 ret = do_write(ff, &n, sizeof(n));
497 ret = do_write_string(ff, buf);
501 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
502 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
509 #define CORE_SIB_FMT \
510 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
511 #define THRD_SIB_FMT \
512 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
518 char **core_siblings;
519 char **thread_siblings;
522 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
525 char filename[MAXPATHLEN];
526 char *buf = NULL, *p;
532 sprintf(filename, CORE_SIB_FMT, cpu);
533 fp = fopen(filename, "r");
537 sret = getline(&buf, &len, fp);
542 p = strchr(buf, '\n');
546 for (i = 0; i < tp->core_sib; i++) {
547 if (!strcmp(buf, tp->core_siblings[i]))
550 if (i == tp->core_sib) {
551 tp->core_siblings[i] = buf;
559 sprintf(filename, THRD_SIB_FMT, cpu);
560 fp = fopen(filename, "r");
564 if (getline(&buf, &len, fp) <= 0)
567 p = strchr(buf, '\n');
571 for (i = 0; i < tp->thread_sib; i++) {
572 if (!strcmp(buf, tp->thread_siblings[i]))
575 if (i == tp->thread_sib) {
576 tp->thread_siblings[i] = buf;
588 static void free_cpu_topo(struct cpu_topo *tp)
595 for (i = 0 ; i < tp->core_sib; i++)
596 zfree(&tp->core_siblings[i]);
598 for (i = 0 ; i < tp->thread_sib; i++)
599 zfree(&tp->thread_siblings[i]);
604 static struct cpu_topo *build_cpu_topology(void)
606 struct cpu_topo *tp = NULL;
614 ncpus = cpu__max_present_cpu();
616 /* build online CPU map */
617 map = cpu_map__new(NULL);
619 pr_debug("failed to get system cpumap\n");
623 nr = (u32)(ncpus & UINT_MAX);
625 sz = nr * sizeof(char *);
626 addr = calloc(1, sizeof(*tp) + 2 * sz);
633 tp->core_siblings = addr;
635 tp->thread_siblings = addr;
637 for (i = 0; i < nr; i++) {
638 if (!cpu_map__has(map, i))
641 ret = build_cpu_topo(tp, i);
655 static int write_cpu_topology(struct feat_fd *ff,
656 struct perf_evlist *evlist __maybe_unused)
662 tp = build_cpu_topology();
666 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
670 for (i = 0; i < tp->core_sib; i++) {
671 ret = do_write_string(ff, tp->core_siblings[i]);
675 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
679 for (i = 0; i < tp->thread_sib; i++) {
680 ret = do_write_string(ff, tp->thread_siblings[i]);
685 ret = perf_env__read_cpu_topology_map(&perf_env);
689 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
690 ret = do_write(ff, &perf_env.cpu[j].core_id,
691 sizeof(perf_env.cpu[j].core_id));
694 ret = do_write(ff, &perf_env.cpu[j].socket_id,
695 sizeof(perf_env.cpu[j].socket_id));
706 static int write_total_mem(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
715 fp = fopen("/proc/meminfo", "r");
719 while (getline(&buf, &len, fp) > 0) {
720 ret = strncmp(buf, "MemTotal:", 9);
725 n = sscanf(buf, "%*s %"PRIu64, &mem);
727 ret = do_write(ff, &mem, sizeof(mem));
735 static int write_topo_node(struct feat_fd *ff, int node)
737 char str[MAXPATHLEN];
739 char *buf = NULL, *p;
742 u64 mem_total, mem_free, mem;
745 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
746 fp = fopen(str, "r");
750 while (getline(&buf, &len, fp) > 0) {
751 /* skip over invalid lines */
752 if (!strchr(buf, ':'))
754 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
756 if (!strcmp(field, "MemTotal:"))
758 if (!strcmp(field, "MemFree:"))
765 ret = do_write(ff, &mem_total, sizeof(u64));
769 ret = do_write(ff, &mem_free, sizeof(u64));
774 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
776 fp = fopen(str, "r");
780 if (getline(&buf, &len, fp) <= 0)
783 p = strchr(buf, '\n');
787 ret = do_write_string(ff, buf);
795 static int write_numa_topology(struct feat_fd *ff,
796 struct perf_evlist *evlist __maybe_unused)
801 struct cpu_map *node_map = NULL;
806 fp = fopen("/sys/devices/system/node/online", "r");
810 if (getline(&buf, &len, fp) <= 0)
813 c = strchr(buf, '\n');
817 node_map = cpu_map__new(buf);
821 nr = (u32)node_map->nr;
823 ret = do_write(ff, &nr, sizeof(nr));
827 for (i = 0; i < nr; i++) {
828 j = (u32)node_map->map[i];
829 ret = do_write(ff, &j, sizeof(j));
833 ret = write_topo_node(ff, i);
840 cpu_map__put(node_map);
847 * struct pmu_mappings {
856 static int write_pmu_mappings(struct feat_fd *ff,
857 struct perf_evlist *evlist __maybe_unused)
859 struct perf_pmu *pmu = NULL;
864 * Do a first pass to count number of pmu to avoid lseek so this
865 * works in pipe mode as well.
867 while ((pmu = perf_pmu__scan(pmu))) {
873 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
877 while ((pmu = perf_pmu__scan(pmu))) {
881 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
885 ret = do_write_string(ff, pmu->name);
896 * struct group_descs {
898 * struct group_desc {
905 static int write_group_desc(struct feat_fd *ff,
906 struct perf_evlist *evlist)
908 u32 nr_groups = evlist->nr_groups;
909 struct perf_evsel *evsel;
912 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
916 evlist__for_each_entry(evlist, evsel) {
917 if (perf_evsel__is_group_leader(evsel) &&
918 evsel->nr_members > 1) {
919 const char *name = evsel->group_name ?: "{anon_group}";
920 u32 leader_idx = evsel->idx;
921 u32 nr_members = evsel->nr_members;
923 ret = do_write_string(ff, name);
927 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
931 ret = do_write(ff, &nr_members, sizeof(nr_members));
940 * default get_cpuid(): nothing gets recorded
941 * actual implementation must be in arch/$(SRCARCH)/util/header.c
943 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
948 static int write_cpuid(struct feat_fd *ff,
949 struct perf_evlist *evlist __maybe_unused)
954 ret = get_cpuid(buffer, sizeof(buffer));
960 return do_write_string(ff, buffer);
963 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
964 struct perf_evlist *evlist __maybe_unused)
969 static int write_auxtrace(struct feat_fd *ff,
970 struct perf_evlist *evlist __maybe_unused)
972 struct perf_session *session;
975 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
978 session = container_of(ff->ph, struct perf_session, header);
980 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
982 pr_err("Failed to write auxtrace index\n");
986 static int cpu_cache_level__sort(const void *a, const void *b)
988 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
989 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
991 return cache_a->level - cache_b->level;
994 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
996 if (a->level != b->level)
999 if (a->line_size != b->line_size)
1002 if (a->sets != b->sets)
1005 if (a->ways != b->ways)
1008 if (strcmp(a->type, b->type))
1011 if (strcmp(a->size, b->size))
1014 if (strcmp(a->map, b->map))
1020 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1022 char path[PATH_MAX], file[PATH_MAX];
1026 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1027 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1029 if (stat(file, &st))
1032 scnprintf(file, PATH_MAX, "%s/level", path);
1033 if (sysfs__read_int(file, (int *) &cache->level))
1036 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1037 if (sysfs__read_int(file, (int *) &cache->line_size))
1040 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1041 if (sysfs__read_int(file, (int *) &cache->sets))
1044 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1045 if (sysfs__read_int(file, (int *) &cache->ways))
1048 scnprintf(file, PATH_MAX, "%s/type", path);
1049 if (sysfs__read_str(file, &cache->type, &len))
1052 cache->type[len] = 0;
1053 cache->type = rtrim(cache->type);
1055 scnprintf(file, PATH_MAX, "%s/size", path);
1056 if (sysfs__read_str(file, &cache->size, &len)) {
1061 cache->size[len] = 0;
1062 cache->size = rtrim(cache->size);
1064 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1065 if (sysfs__read_str(file, &cache->map, &len)) {
1071 cache->map[len] = 0;
1072 cache->map = rtrim(cache->map);
1076 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1078 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1081 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1088 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1092 nr = (u32)(ncpus & UINT_MAX);
1094 for (cpu = 0; cpu < nr; cpu++) {
1095 for (level = 0; level < 10; level++) {
1096 struct cpu_cache_level c;
1099 err = cpu_cache_level__read(&c, cpu, level);
1106 for (i = 0; i < cnt; i++) {
1107 if (cpu_cache_level__cmp(&c, &caches[i]))
1114 cpu_cache_level__free(&c);
1116 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1125 #define MAX_CACHES 2000
1127 static int write_cache(struct feat_fd *ff,
1128 struct perf_evlist *evlist __maybe_unused)
1130 struct cpu_cache_level caches[MAX_CACHES];
1131 u32 cnt = 0, i, version = 1;
1134 ret = build_caches(caches, MAX_CACHES, &cnt);
1138 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1140 ret = do_write(ff, &version, sizeof(u32));
1144 ret = do_write(ff, &cnt, sizeof(u32));
1148 for (i = 0; i < cnt; i++) {
1149 struct cpu_cache_level *c = &caches[i];
1152 ret = do_write(ff, &c->v, sizeof(u32)); \
1163 ret = do_write_string(ff, (const char *) c->v); \
1174 for (i = 0; i < cnt; i++)
1175 cpu_cache_level__free(&caches[i]);
1179 static int write_stat(struct feat_fd *ff __maybe_unused,
1180 struct perf_evlist *evlist __maybe_unused)
1185 static void print_hostname(struct feat_fd *ff, FILE *fp)
1187 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1190 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1192 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1195 static void print_arch(struct feat_fd *ff, FILE *fp)
1197 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1200 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1202 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1205 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1207 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1208 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1211 static void print_version(struct feat_fd *ff, FILE *fp)
1213 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1216 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1220 nr = ff->ph->env.nr_cmdline;
1222 fprintf(fp, "# cmdline : ");
1224 for (i = 0; i < nr; i++)
1225 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1229 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1231 struct perf_header *ph = ff->ph;
1232 int cpu_nr = ph->env.nr_cpus_avail;
1236 nr = ph->env.nr_sibling_cores;
1237 str = ph->env.sibling_cores;
1239 for (i = 0; i < nr; i++) {
1240 fprintf(fp, "# sibling cores : %s\n", str);
1241 str += strlen(str) + 1;
1244 nr = ph->env.nr_sibling_threads;
1245 str = ph->env.sibling_threads;
1247 for (i = 0; i < nr; i++) {
1248 fprintf(fp, "# sibling threads : %s\n", str);
1249 str += strlen(str) + 1;
1252 if (ph->env.cpu != NULL) {
1253 for (i = 0; i < cpu_nr; i++)
1254 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1255 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1257 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1260 static void free_event_desc(struct perf_evsel *events)
1262 struct perf_evsel *evsel;
1267 for (evsel = events; evsel->attr.size; evsel++) {
1268 zfree(&evsel->name);
1275 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1277 struct perf_evsel *evsel, *events = NULL;
1280 u32 nre, sz, nr, i, j;
1283 /* number of events */
1284 if (do_read_u32(ff, &nre))
1287 if (do_read_u32(ff, &sz))
1290 /* buffer to hold on file attr struct */
1295 /* the last event terminates with evsel->attr.size == 0: */
1296 events = calloc(nre + 1, sizeof(*events));
1300 msz = sizeof(evsel->attr);
1304 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1308 * must read entire on-file attr struct to
1309 * sync up with layout.
1311 if (__do_read(ff, buf, sz))
1314 if (ff->ph->needs_swap)
1315 perf_event__attr_swap(buf);
1317 memcpy(&evsel->attr, buf, msz);
1319 if (do_read_u32(ff, &nr))
1322 if (ff->ph->needs_swap)
1323 evsel->needs_swap = true;
1325 evsel->name = do_read_string(ff);
1332 id = calloc(nr, sizeof(*id));
1338 for (j = 0 ; j < nr; j++) {
1339 if (do_read_u64(ff, id))
1348 free_event_desc(events);
1353 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1354 void *priv __maybe_unused)
1356 return fprintf(fp, ", %s = %s", name, val);
1359 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1361 struct perf_evsel *evsel, *events;
1366 events = ff->events;
1368 events = read_event_desc(ff);
1371 fprintf(fp, "# event desc: not available or unable to read\n");
1375 for (evsel = events; evsel->attr.size; evsel++) {
1376 fprintf(fp, "# event : name = %s, ", evsel->name);
1379 fprintf(fp, ", id = {");
1380 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1383 fprintf(fp, " %"PRIu64, *id);
1388 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1393 free_event_desc(events);
1397 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1399 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1402 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1405 struct numa_node *n;
1407 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1408 n = &ff->ph->env.numa_nodes[i];
1410 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1411 " free = %"PRIu64" kB\n",
1412 n->node, n->mem_total, n->mem_free);
1414 fprintf(fp, "# node%u cpu list : ", n->node);
1415 cpu_map__fprintf(n->map, fp);
1419 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1421 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1424 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1426 fprintf(fp, "# contains samples with branch stack\n");
1429 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1431 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1434 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1436 fprintf(fp, "# contains stat data\n");
1439 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1443 fprintf(fp, "# CPU cache info:\n");
1444 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1446 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1450 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1452 const char *delimiter = "# pmu mappings: ";
1457 pmu_num = ff->ph->env.nr_pmu_mappings;
1459 fprintf(fp, "# pmu mappings: not available\n");
1463 str = ff->ph->env.pmu_mappings;
1466 type = strtoul(str, &tmp, 0);
1471 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1474 str += strlen(str) + 1;
1483 fprintf(fp, "# pmu mappings: unable to read\n");
1486 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1488 struct perf_session *session;
1489 struct perf_evsel *evsel;
1492 session = container_of(ff->ph, struct perf_session, header);
1494 evlist__for_each_entry(session->evlist, evsel) {
1495 if (perf_evsel__is_group_leader(evsel) &&
1496 evsel->nr_members > 1) {
1497 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1498 perf_evsel__name(evsel));
1500 nr = evsel->nr_members - 1;
1502 fprintf(fp, ",%s", perf_evsel__name(evsel));
1510 static int __event_process_build_id(struct build_id_event *bev,
1512 struct perf_session *session)
1515 struct machine *machine;
1518 enum dso_kernel_type dso_type;
1520 machine = perf_session__findnew_machine(session, bev->pid);
1524 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1527 case PERF_RECORD_MISC_KERNEL:
1528 dso_type = DSO_TYPE_KERNEL;
1530 case PERF_RECORD_MISC_GUEST_KERNEL:
1531 dso_type = DSO_TYPE_GUEST_KERNEL;
1533 case PERF_RECORD_MISC_USER:
1534 case PERF_RECORD_MISC_GUEST_USER:
1535 dso_type = DSO_TYPE_USER;
1541 dso = machine__findnew_dso(machine, filename);
1543 char sbuild_id[SBUILD_ID_SIZE];
1545 dso__set_build_id(dso, &bev->build_id);
1547 if (dso_type != DSO_TYPE_USER) {
1548 struct kmod_path m = { .name = NULL, };
1550 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1551 dso__set_module_info(dso, &m, machine);
1553 dso->kernel = dso_type;
1558 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1560 pr_debug("build id event received for %s: %s\n",
1561 dso->long_name, sbuild_id);
1570 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1571 int input, u64 offset, u64 size)
1573 struct perf_session *session = container_of(header, struct perf_session, header);
1575 struct perf_event_header header;
1576 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1579 struct build_id_event bev;
1580 char filename[PATH_MAX];
1581 u64 limit = offset + size;
1583 while (offset < limit) {
1586 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1589 if (header->needs_swap)
1590 perf_event_header__bswap(&old_bev.header);
1592 len = old_bev.header.size - sizeof(old_bev);
1593 if (readn(input, filename, len) != len)
1596 bev.header = old_bev.header;
1599 * As the pid is the missing value, we need to fill
1600 * it properly. The header.misc value give us nice hint.
1602 bev.pid = HOST_KERNEL_ID;
1603 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1604 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1605 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1607 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1608 __event_process_build_id(&bev, filename, session);
1610 offset += bev.header.size;
1616 static int perf_header__read_build_ids(struct perf_header *header,
1617 int input, u64 offset, u64 size)
1619 struct perf_session *session = container_of(header, struct perf_session, header);
1620 struct build_id_event bev;
1621 char filename[PATH_MAX];
1622 u64 limit = offset + size, orig_offset = offset;
1625 while (offset < limit) {
1628 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1631 if (header->needs_swap)
1632 perf_event_header__bswap(&bev.header);
1634 len = bev.header.size - sizeof(bev);
1635 if (readn(input, filename, len) != len)
1638 * The a1645ce1 changeset:
1640 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1642 * Added a field to struct build_id_event that broke the file
1645 * Since the kernel build-id is the first entry, process the
1646 * table using the old format if the well known
1647 * '[kernel.kallsyms]' string for the kernel build-id has the
1648 * first 4 characters chopped off (where the pid_t sits).
1650 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1651 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1653 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1656 __event_process_build_id(&bev, filename, session);
1658 offset += bev.header.size;
1665 /* Macro for features that simply need to read and store a string. */
1666 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1667 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1669 ff->ph->env.__feat_env = do_read_string(ff); \
1670 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1673 FEAT_PROCESS_STR_FUN(hostname, hostname);
1674 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1675 FEAT_PROCESS_STR_FUN(version, version);
1676 FEAT_PROCESS_STR_FUN(arch, arch);
1677 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1678 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1680 static int process_tracing_data(struct feat_fd *ff, void *data)
1682 ssize_t ret = trace_report(ff->fd, data, false);
1684 return ret < 0 ? -1 : 0;
1687 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1689 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1690 pr_debug("Failed to read buildids, continuing...\n");
1694 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1697 u32 nr_cpus_avail, nr_cpus_online;
1699 ret = do_read_u32(ff, &nr_cpus_avail);
1703 ret = do_read_u32(ff, &nr_cpus_online);
1706 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1707 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
1711 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
1716 ret = do_read_u64(ff, &total_mem);
1719 ff->ph->env.total_mem = (unsigned long long)total_mem;
1723 static struct perf_evsel *
1724 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1726 struct perf_evsel *evsel;
1728 evlist__for_each_entry(evlist, evsel) {
1729 if (evsel->idx == idx)
1737 perf_evlist__set_event_name(struct perf_evlist *evlist,
1738 struct perf_evsel *event)
1740 struct perf_evsel *evsel;
1745 evsel = perf_evlist__find_by_index(evlist, event->idx);
1752 evsel->name = strdup(event->name);
1756 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
1758 struct perf_session *session;
1759 struct perf_evsel *evsel, *events = read_event_desc(ff);
1764 session = container_of(ff->ph, struct perf_session, header);
1766 if (session->file->is_pipe) {
1767 /* Save events for reading later by print_event_desc,
1768 * since they can't be read again in pipe mode. */
1769 ff->events = events;
1772 for (evsel = events; evsel->attr.size; evsel++)
1773 perf_evlist__set_event_name(session->evlist, evsel);
1775 if (!session->file->is_pipe)
1776 free_event_desc(events);
1781 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
1783 char *str, *cmdline = NULL, **argv = NULL;
1786 if (do_read_u32(ff, &nr))
1789 ff->ph->env.nr_cmdline = nr;
1791 cmdline = zalloc(ff->size + nr + 1);
1795 argv = zalloc(sizeof(char *) * (nr + 1));
1799 for (i = 0; i < nr; i++) {
1800 str = do_read_string(ff);
1804 argv[i] = cmdline + len;
1805 memcpy(argv[i], str, strlen(str) + 1);
1806 len += strlen(str) + 1;
1809 ff->ph->env.cmdline = cmdline;
1810 ff->ph->env.cmdline_argv = (const char **) argv;
1819 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
1824 int cpu_nr = ff->ph->env.nr_cpus_avail;
1826 struct perf_header *ph = ff->ph;
1827 bool do_core_id_test = true;
1829 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1833 if (do_read_u32(ff, &nr))
1836 ph->env.nr_sibling_cores = nr;
1837 size += sizeof(u32);
1838 if (strbuf_init(&sb, 128) < 0)
1841 for (i = 0; i < nr; i++) {
1842 str = do_read_string(ff);
1846 /* include a NULL character at the end */
1847 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1849 size += string_size(str);
1852 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1854 if (do_read_u32(ff, &nr))
1857 ph->env.nr_sibling_threads = nr;
1858 size += sizeof(u32);
1860 for (i = 0; i < nr; i++) {
1861 str = do_read_string(ff);
1865 /* include a NULL character at the end */
1866 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1868 size += string_size(str);
1871 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1874 * The header may be from old perf,
1875 * which doesn't include core id and socket id information.
1877 if (ff->size <= size) {
1878 zfree(&ph->env.cpu);
1882 /* On s390 the socket_id number is not related to the numbers of cpus.
1883 * The socket_id number might be higher than the numbers of cpus.
1884 * This depends on the configuration.
1886 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
1887 do_core_id_test = false;
1889 for (i = 0; i < (u32)cpu_nr; i++) {
1890 if (do_read_u32(ff, &nr))
1893 ph->env.cpu[i].core_id = nr;
1895 if (do_read_u32(ff, &nr))
1898 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
1899 pr_debug("socket_id number is too big."
1900 "You may need to upgrade the perf tool.\n");
1904 ph->env.cpu[i].socket_id = nr;
1910 strbuf_release(&sb);
1912 zfree(&ph->env.cpu);
1916 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
1918 struct numa_node *nodes, *n;
1923 if (do_read_u32(ff, &nr))
1926 nodes = zalloc(sizeof(*nodes) * nr);
1930 for (i = 0; i < nr; i++) {
1934 if (do_read_u32(ff, &n->node))
1937 if (do_read_u64(ff, &n->mem_total))
1940 if (do_read_u64(ff, &n->mem_free))
1943 str = do_read_string(ff);
1947 n->map = cpu_map__new(str);
1953 ff->ph->env.nr_numa_nodes = nr;
1954 ff->ph->env.numa_nodes = nodes;
1962 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
1969 if (do_read_u32(ff, &pmu_num))
1973 pr_debug("pmu mappings not available\n");
1977 ff->ph->env.nr_pmu_mappings = pmu_num;
1978 if (strbuf_init(&sb, 128) < 0)
1982 if (do_read_u32(ff, &type))
1985 name = do_read_string(ff);
1989 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1991 /* include a NULL character at the end */
1992 if (strbuf_add(&sb, "", 1) < 0)
1995 if (!strcmp(name, "msr"))
1996 ff->ph->env.msr_pmu_type = type;
2001 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2005 strbuf_release(&sb);
2009 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2012 u32 i, nr, nr_groups;
2013 struct perf_session *session;
2014 struct perf_evsel *evsel, *leader = NULL;
2021 if (do_read_u32(ff, &nr_groups))
2024 ff->ph->env.nr_groups = nr_groups;
2026 pr_debug("group desc not available\n");
2030 desc = calloc(nr_groups, sizeof(*desc));
2034 for (i = 0; i < nr_groups; i++) {
2035 desc[i].name = do_read_string(ff);
2039 if (do_read_u32(ff, &desc[i].leader_idx))
2042 if (do_read_u32(ff, &desc[i].nr_members))
2047 * Rebuild group relationship based on the group_desc
2049 session = container_of(ff->ph, struct perf_session, header);
2050 session->evlist->nr_groups = nr_groups;
2053 evlist__for_each_entry(session->evlist, evsel) {
2054 if (evsel->idx == (int) desc[i].leader_idx) {
2055 evsel->leader = evsel;
2056 /* {anon_group} is a dummy name */
2057 if (strcmp(desc[i].name, "{anon_group}")) {
2058 evsel->group_name = desc[i].name;
2059 desc[i].name = NULL;
2061 evsel->nr_members = desc[i].nr_members;
2063 if (i >= nr_groups || nr > 0) {
2064 pr_debug("invalid group desc\n");
2069 nr = evsel->nr_members - 1;
2072 /* This is a group member */
2073 evsel->leader = leader;
2079 if (i != nr_groups || nr != 0) {
2080 pr_debug("invalid group desc\n");
2086 for (i = 0; i < nr_groups; i++)
2087 zfree(&desc[i].name);
2093 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2095 struct perf_session *session;
2098 session = container_of(ff->ph, struct perf_session, header);
2100 err = auxtrace_index__process(ff->fd, ff->size, session,
2101 ff->ph->needs_swap);
2103 pr_err("Failed to process auxtrace index\n");
2107 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2109 struct cpu_cache_level *caches;
2110 u32 cnt, i, version;
2112 if (do_read_u32(ff, &version))
2118 if (do_read_u32(ff, &cnt))
2121 caches = zalloc(sizeof(*caches) * cnt);
2125 for (i = 0; i < cnt; i++) {
2126 struct cpu_cache_level c;
2129 if (do_read_u32(ff, &c.v))\
2130 goto out_free_caches; \
2139 c.v = do_read_string(ff); \
2141 goto out_free_caches;
2151 ff->ph->env.caches = caches;
2152 ff->ph->env.caches_cnt = cnt;
2159 struct feature_ops {
2160 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2161 void (*print)(struct feat_fd *ff, FILE *fp);
2162 int (*process)(struct feat_fd *ff, void *data);
2168 #define FEAT_OPR(n, func, __full_only) \
2170 .name = __stringify(n), \
2171 .write = write_##func, \
2172 .print = print_##func, \
2173 .full_only = __full_only, \
2174 .process = process_##func, \
2175 .synthesize = true \
2178 #define FEAT_OPN(n, func, __full_only) \
2180 .name = __stringify(n), \
2181 .write = write_##func, \
2182 .print = print_##func, \
2183 .full_only = __full_only, \
2184 .process = process_##func \
2187 /* feature_ops not implemented: */
2188 #define print_tracing_data NULL
2189 #define print_build_id NULL
2191 #define process_branch_stack NULL
2192 #define process_stat NULL
2195 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2196 FEAT_OPN(TRACING_DATA, tracing_data, false),
2197 FEAT_OPN(BUILD_ID, build_id, false),
2198 FEAT_OPR(HOSTNAME, hostname, false),
2199 FEAT_OPR(OSRELEASE, osrelease, false),
2200 FEAT_OPR(VERSION, version, false),
2201 FEAT_OPR(ARCH, arch, false),
2202 FEAT_OPR(NRCPUS, nrcpus, false),
2203 FEAT_OPR(CPUDESC, cpudesc, false),
2204 FEAT_OPR(CPUID, cpuid, false),
2205 FEAT_OPR(TOTAL_MEM, total_mem, false),
2206 FEAT_OPR(EVENT_DESC, event_desc, false),
2207 FEAT_OPR(CMDLINE, cmdline, false),
2208 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2209 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2210 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2211 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2212 FEAT_OPN(GROUP_DESC, group_desc, false),
2213 FEAT_OPN(AUXTRACE, auxtrace, false),
2214 FEAT_OPN(STAT, stat, false),
2215 FEAT_OPN(CACHE, cache, true),
2218 struct header_print_data {
2220 bool full; /* extended list of headers */
2223 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2224 struct perf_header *ph,
2225 int feat, int fd, void *data)
2227 struct header_print_data *hd = data;
2230 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2231 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2232 "%d, continuing...\n", section->offset, feat);
2235 if (feat >= HEADER_LAST_FEATURE) {
2236 pr_warning("unknown feature %d\n", feat);
2239 if (!feat_ops[feat].print)
2242 ff = (struct feat_fd) {
2247 if (!feat_ops[feat].full_only || hd->full)
2248 feat_ops[feat].print(&ff, hd->fp);
2250 fprintf(hd->fp, "# %s info available, use -I to display\n",
2251 feat_ops[feat].name);
2256 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2258 struct header_print_data hd;
2259 struct perf_header *header = &session->header;
2260 int fd = perf_data_file__fd(session->file);
2267 ret = fstat(fd, &st);
2271 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2273 perf_header__process_sections(header, fd, &hd,
2274 perf_file_section__fprintf_info);
2276 if (session->file->is_pipe)
2279 fprintf(fp, "# missing features: ");
2280 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2282 fprintf(fp, "%s ", feat_ops[bit].name);
2289 static int do_write_feat(struct feat_fd *ff, int type,
2290 struct perf_file_section **p,
2291 struct perf_evlist *evlist)
2296 if (perf_header__has_feat(ff->ph, type)) {
2297 if (!feat_ops[type].write)
2300 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2303 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2305 err = feat_ops[type].write(ff, evlist);
2307 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2309 /* undo anything written */
2310 lseek(ff->fd, (*p)->offset, SEEK_SET);
2314 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2320 static int perf_header__adds_write(struct perf_header *header,
2321 struct perf_evlist *evlist, int fd)
2325 struct perf_file_section *feat_sec, *p;
2331 ff = (struct feat_fd){
2336 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2340 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2341 if (feat_sec == NULL)
2344 sec_size = sizeof(*feat_sec) * nr_sections;
2346 sec_start = header->feat_offset;
2347 lseek(fd, sec_start + sec_size, SEEK_SET);
2349 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2350 if (do_write_feat(&ff, feat, &p, evlist))
2351 perf_header__clear_feat(header, feat);
2354 lseek(fd, sec_start, SEEK_SET);
2356 * may write more than needed due to dropped feature, but
2357 * this is okay, reader will skip the mising entries
2359 err = do_write(&ff, feat_sec, sec_size);
2361 pr_debug("failed to write feature section\n");
2366 int perf_header__write_pipe(int fd)
2368 struct perf_pipe_file_header f_header;
2372 ff = (struct feat_fd){ .fd = fd };
2374 f_header = (struct perf_pipe_file_header){
2375 .magic = PERF_MAGIC,
2376 .size = sizeof(f_header),
2379 err = do_write(&ff, &f_header, sizeof(f_header));
2381 pr_debug("failed to write perf pipe header\n");
2388 int perf_session__write_header(struct perf_session *session,
2389 struct perf_evlist *evlist,
2390 int fd, bool at_exit)
2392 struct perf_file_header f_header;
2393 struct perf_file_attr f_attr;
2394 struct perf_header *header = &session->header;
2395 struct perf_evsel *evsel;
2400 ff = (struct feat_fd){ .fd = fd};
2401 lseek(fd, sizeof(f_header), SEEK_SET);
2403 evlist__for_each_entry(session->evlist, evsel) {
2404 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2405 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2407 pr_debug("failed to write perf header\n");
2412 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2414 evlist__for_each_entry(evlist, evsel) {
2415 f_attr = (struct perf_file_attr){
2416 .attr = evsel->attr,
2418 .offset = evsel->id_offset,
2419 .size = evsel->ids * sizeof(u64),
2422 err = do_write(&ff, &f_attr, sizeof(f_attr));
2424 pr_debug("failed to write perf header attribute\n");
2429 if (!header->data_offset)
2430 header->data_offset = lseek(fd, 0, SEEK_CUR);
2431 header->feat_offset = header->data_offset + header->data_size;
2434 err = perf_header__adds_write(header, evlist, fd);
2439 f_header = (struct perf_file_header){
2440 .magic = PERF_MAGIC,
2441 .size = sizeof(f_header),
2442 .attr_size = sizeof(f_attr),
2444 .offset = attr_offset,
2445 .size = evlist->nr_entries * sizeof(f_attr),
2448 .offset = header->data_offset,
2449 .size = header->data_size,
2451 /* event_types is ignored, store zeros */
2454 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2456 lseek(fd, 0, SEEK_SET);
2457 err = do_write(&ff, &f_header, sizeof(f_header));
2459 pr_debug("failed to write perf header\n");
2462 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2467 static int perf_header__getbuffer64(struct perf_header *header,
2468 int fd, void *buf, size_t size)
2470 if (readn(fd, buf, size) <= 0)
2473 if (header->needs_swap)
2474 mem_bswap_64(buf, size);
2479 int perf_header__process_sections(struct perf_header *header, int fd,
2481 int (*process)(struct perf_file_section *section,
2482 struct perf_header *ph,
2483 int feat, int fd, void *data))
2485 struct perf_file_section *feat_sec, *sec;
2491 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2495 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2499 sec_size = sizeof(*feat_sec) * nr_sections;
2501 lseek(fd, header->feat_offset, SEEK_SET);
2503 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2507 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2508 err = process(sec++, header, feat, fd, data);
2518 static const int attr_file_abi_sizes[] = {
2519 [0] = PERF_ATTR_SIZE_VER0,
2520 [1] = PERF_ATTR_SIZE_VER1,
2521 [2] = PERF_ATTR_SIZE_VER2,
2522 [3] = PERF_ATTR_SIZE_VER3,
2523 [4] = PERF_ATTR_SIZE_VER4,
2528 * In the legacy file format, the magic number is not used to encode endianness.
2529 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2530 * on ABI revisions, we need to try all combinations for all endianness to
2531 * detect the endianness.
2533 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2535 uint64_t ref_size, attr_size;
2538 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2539 ref_size = attr_file_abi_sizes[i]
2540 + sizeof(struct perf_file_section);
2541 if (hdr_sz != ref_size) {
2542 attr_size = bswap_64(hdr_sz);
2543 if (attr_size != ref_size)
2546 ph->needs_swap = true;
2548 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2553 /* could not determine endianness */
2557 #define PERF_PIPE_HDR_VER0 16
2559 static const size_t attr_pipe_abi_sizes[] = {
2560 [0] = PERF_PIPE_HDR_VER0,
2565 * In the legacy pipe format, there is an implicit assumption that endiannesss
2566 * between host recording the samples, and host parsing the samples is the
2567 * same. This is not always the case given that the pipe output may always be
2568 * redirected into a file and analyzed on a different machine with possibly a
2569 * different endianness and perf_event ABI revsions in the perf tool itself.
2571 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2576 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2577 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2578 attr_size = bswap_64(hdr_sz);
2579 if (attr_size != hdr_sz)
2582 ph->needs_swap = true;
2584 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2590 bool is_perf_magic(u64 magic)
2592 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2593 || magic == __perf_magic2
2594 || magic == __perf_magic2_sw)
2600 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2601 bool is_pipe, struct perf_header *ph)
2605 /* check for legacy format */
2606 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2608 ph->version = PERF_HEADER_VERSION_1;
2609 pr_debug("legacy perf.data format\n");
2611 return try_all_pipe_abis(hdr_sz, ph);
2613 return try_all_file_abis(hdr_sz, ph);
2616 * the new magic number serves two purposes:
2617 * - unique number to identify actual perf.data files
2618 * - encode endianness of file
2620 ph->version = PERF_HEADER_VERSION_2;
2622 /* check magic number with one endianness */
2623 if (magic == __perf_magic2)
2626 /* check magic number with opposite endianness */
2627 if (magic != __perf_magic2_sw)
2630 ph->needs_swap = true;
2635 int perf_file_header__read(struct perf_file_header *header,
2636 struct perf_header *ph, int fd)
2640 lseek(fd, 0, SEEK_SET);
2642 ret = readn(fd, header, sizeof(*header));
2646 if (check_magic_endian(header->magic,
2647 header->attr_size, false, ph) < 0) {
2648 pr_debug("magic/endian check failed\n");
2652 if (ph->needs_swap) {
2653 mem_bswap_64(header, offsetof(struct perf_file_header,
2657 if (header->size != sizeof(*header)) {
2658 /* Support the previous format */
2659 if (header->size == offsetof(typeof(*header), adds_features))
2660 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2663 } else if (ph->needs_swap) {
2665 * feature bitmap is declared as an array of unsigned longs --
2666 * not good since its size can differ between the host that
2667 * generated the data file and the host analyzing the file.
2669 * We need to handle endianness, but we don't know the size of
2670 * the unsigned long where the file was generated. Take a best
2671 * guess at determining it: try 64-bit swap first (ie., file
2672 * created on a 64-bit host), and check if the hostname feature
2673 * bit is set (this feature bit is forced on as of fbe96f2).
2674 * If the bit is not, undo the 64-bit swap and try a 32-bit
2675 * swap. If the hostname bit is still not set (e.g., older data
2676 * file), punt and fallback to the original behavior --
2677 * clearing all feature bits and setting buildid.
2679 mem_bswap_64(&header->adds_features,
2680 BITS_TO_U64(HEADER_FEAT_BITS));
2682 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2684 mem_bswap_64(&header->adds_features,
2685 BITS_TO_U64(HEADER_FEAT_BITS));
2688 mem_bswap_32(&header->adds_features,
2689 BITS_TO_U32(HEADER_FEAT_BITS));
2692 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2693 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2694 set_bit(HEADER_BUILD_ID, header->adds_features);
2698 memcpy(&ph->adds_features, &header->adds_features,
2699 sizeof(ph->adds_features));
2701 ph->data_offset = header->data.offset;
2702 ph->data_size = header->data.size;
2703 ph->feat_offset = header->data.offset + header->data.size;
2707 static int perf_file_section__process(struct perf_file_section *section,
2708 struct perf_header *ph,
2709 int feat, int fd, void *data)
2711 struct feat_fd fdd = {
2714 .size = section->size,
2715 .offset = section->offset,
2718 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2719 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2720 "%d, continuing...\n", section->offset, feat);
2724 if (feat >= HEADER_LAST_FEATURE) {
2725 pr_debug("unknown feature %d, continuing...\n", feat);
2729 if (!feat_ops[feat].process)
2732 return feat_ops[feat].process(&fdd, data);
2735 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2736 struct perf_header *ph, int fd,
2739 struct feat_fd ff = {
2740 .fd = STDOUT_FILENO,
2745 ret = readn(fd, header, sizeof(*header));
2749 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2750 pr_debug("endian/magic failed\n");
2755 header->size = bswap_64(header->size);
2757 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
2763 static int perf_header__read_pipe(struct perf_session *session)
2765 struct perf_header *header = &session->header;
2766 struct perf_pipe_file_header f_header;
2768 if (perf_file_header__read_pipe(&f_header, header,
2769 perf_data_file__fd(session->file),
2770 session->repipe) < 0) {
2771 pr_debug("incompatible file format\n");
2778 static int read_attr(int fd, struct perf_header *ph,
2779 struct perf_file_attr *f_attr)
2781 struct perf_event_attr *attr = &f_attr->attr;
2783 size_t our_sz = sizeof(f_attr->attr);
2786 memset(f_attr, 0, sizeof(*f_attr));
2788 /* read minimal guaranteed structure */
2789 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2791 pr_debug("cannot read %d bytes of header attr\n",
2792 PERF_ATTR_SIZE_VER0);
2796 /* on file perf_event_attr size */
2804 sz = PERF_ATTR_SIZE_VER0;
2805 } else if (sz > our_sz) {
2806 pr_debug("file uses a more recent and unsupported ABI"
2807 " (%zu bytes extra)\n", sz - our_sz);
2810 /* what we have not yet read and that we know about */
2811 left = sz - PERF_ATTR_SIZE_VER0;
2814 ptr += PERF_ATTR_SIZE_VER0;
2816 ret = readn(fd, ptr, left);
2818 /* read perf_file_section, ids are read in caller */
2819 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2821 return ret <= 0 ? -1 : 0;
2824 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2825 struct pevent *pevent)
2827 struct event_format *event;
2830 /* already prepared */
2831 if (evsel->tp_format)
2834 if (pevent == NULL) {
2835 pr_debug("broken or missing trace data\n");
2839 event = pevent_find_event(pevent, evsel->attr.config);
2840 if (event == NULL) {
2841 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
2846 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2847 evsel->name = strdup(bf);
2848 if (evsel->name == NULL)
2852 evsel->tp_format = event;
2856 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2857 struct pevent *pevent)
2859 struct perf_evsel *pos;
2861 evlist__for_each_entry(evlist, pos) {
2862 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2863 perf_evsel__prepare_tracepoint_event(pos, pevent))
2870 int perf_session__read_header(struct perf_session *session)
2872 struct perf_data_file *file = session->file;
2873 struct perf_header *header = &session->header;
2874 struct perf_file_header f_header;
2875 struct perf_file_attr f_attr;
2877 int nr_attrs, nr_ids, i, j;
2878 int fd = perf_data_file__fd(file);
2880 session->evlist = perf_evlist__new();
2881 if (session->evlist == NULL)
2884 session->evlist->env = &header->env;
2885 session->machines.host.env = &header->env;
2886 if (perf_data_file__is_pipe(file))
2887 return perf_header__read_pipe(session);
2889 if (perf_file_header__read(&f_header, header, fd) < 0)
2893 * Sanity check that perf.data was written cleanly; data size is
2894 * initialized to 0 and updated only if the on_exit function is run.
2895 * If data size is still 0 then the file contains only partial
2896 * information. Just warn user and process it as much as it can.
2898 if (f_header.data.size == 0) {
2899 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2900 "Was the 'perf record' command properly terminated?\n",
2904 nr_attrs = f_header.attrs.size / f_header.attr_size;
2905 lseek(fd, f_header.attrs.offset, SEEK_SET);
2907 for (i = 0; i < nr_attrs; i++) {
2908 struct perf_evsel *evsel;
2911 if (read_attr(fd, header, &f_attr) < 0)
2914 if (header->needs_swap) {
2915 f_attr.ids.size = bswap_64(f_attr.ids.size);
2916 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2917 perf_event__attr_swap(&f_attr.attr);
2920 tmp = lseek(fd, 0, SEEK_CUR);
2921 evsel = perf_evsel__new(&f_attr.attr);
2924 goto out_delete_evlist;
2926 evsel->needs_swap = header->needs_swap;
2928 * Do it before so that if perf_evsel__alloc_id fails, this
2929 * entry gets purged too at perf_evlist__delete().
2931 perf_evlist__add(session->evlist, evsel);
2933 nr_ids = f_attr.ids.size / sizeof(u64);
2935 * We don't have the cpu and thread maps on the header, so
2936 * for allocating the perf_sample_id table we fake 1 cpu and
2937 * hattr->ids threads.
2939 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2940 goto out_delete_evlist;
2942 lseek(fd, f_attr.ids.offset, SEEK_SET);
2944 for (j = 0; j < nr_ids; j++) {
2945 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2948 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2951 lseek(fd, tmp, SEEK_SET);
2954 symbol_conf.nr_events = nr_attrs;
2956 perf_header__process_sections(header, fd, &session->tevent,
2957 perf_file_section__process);
2959 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2960 session->tevent.pevent))
2961 goto out_delete_evlist;
2968 perf_evlist__delete(session->evlist);
2969 session->evlist = NULL;
2973 int perf_event__synthesize_attr(struct perf_tool *tool,
2974 struct perf_event_attr *attr, u32 ids, u64 *id,
2975 perf_event__handler_t process)
2977 union perf_event *ev;
2981 size = sizeof(struct perf_event_attr);
2982 size = PERF_ALIGN(size, sizeof(u64));
2983 size += sizeof(struct perf_event_header);
2984 size += ids * sizeof(u64);
2991 ev->attr.attr = *attr;
2992 memcpy(ev->attr.id, id, ids * sizeof(u64));
2994 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2995 ev->attr.header.size = (u16)size;
2997 if (ev->attr.header.size == size)
2998 err = process(tool, ev, NULL, NULL);
3007 int perf_event__synthesize_features(struct perf_tool *tool,
3008 struct perf_session *session,
3009 struct perf_evlist *evlist,
3010 perf_event__handler_t process)
3012 struct perf_header *header = &session->header;
3014 struct feature_event *fe;
3018 sz_hdr = sizeof(fe->header);
3019 sz = sizeof(union perf_event);
3020 /* get a nice alignment */
3021 sz = PERF_ALIGN(sz, page_size);
3023 memset(&ff, 0, sizeof(ff));
3025 ff.buf = malloc(sz);
3029 ff.size = sz - sz_hdr;
3031 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3032 if (!feat_ops[feat].synthesize) {
3033 pr_debug("No record header feature for header :%d\n", feat);
3037 ff.offset = sizeof(*fe);
3039 ret = feat_ops[feat].write(&ff, evlist);
3040 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3041 pr_debug("Error writing feature\n");
3044 /* ff.buf may have changed due to realloc in do_write() */
3046 memset(fe, 0, sizeof(*fe));
3049 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3050 fe->header.size = ff.offset;
3052 ret = process(tool, ff.buf, NULL, NULL);
3062 int perf_event__process_feature(struct perf_tool *tool,
3063 union perf_event *event,
3064 struct perf_session *session __maybe_unused)
3066 struct feat_fd ff = { .fd = 0 };
3067 struct feature_event *fe = (struct feature_event *)event;
3068 int type = fe->header.type;
3069 u64 feat = fe->feat_id;
3071 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3072 pr_warning("invalid record type %d in pipe-mode\n", type);
3075 if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3076 pr_warning("invalid record type %d in pipe-mode\n", type);
3080 if (!feat_ops[feat].process)
3083 ff.buf = (void *)fe->data;
3084 ff.size = event->header.size - sizeof(event->header);
3085 ff.ph = &session->header;
3087 if (feat_ops[feat].process(&ff, NULL))
3090 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3093 if (!feat_ops[feat].full_only ||
3094 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3095 feat_ops[feat].print(&ff, stdout);
3097 fprintf(stdout, "# %s info available, use -I to display\n",
3098 feat_ops[feat].name);
3104 static struct event_update_event *
3105 event_update_event__new(size_t size, u64 type, u64 id)
3107 struct event_update_event *ev;
3109 size += sizeof(*ev);
3110 size = PERF_ALIGN(size, sizeof(u64));
3114 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3115 ev->header.size = (u16)size;
3123 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3124 struct perf_evsel *evsel,
3125 perf_event__handler_t process)
3127 struct event_update_event *ev;
3128 size_t size = strlen(evsel->unit);
3131 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3135 strncpy(ev->data, evsel->unit, size);
3136 err = process(tool, (union perf_event *)ev, NULL, NULL);
3142 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3143 struct perf_evsel *evsel,
3144 perf_event__handler_t process)
3146 struct event_update_event *ev;
3147 struct event_update_event_scale *ev_data;
3150 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3154 ev_data = (struct event_update_event_scale *) ev->data;
3155 ev_data->scale = evsel->scale;
3156 err = process(tool, (union perf_event*) ev, NULL, NULL);
3162 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3163 struct perf_evsel *evsel,
3164 perf_event__handler_t process)
3166 struct event_update_event *ev;
3167 size_t len = strlen(evsel->name);
3170 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3174 strncpy(ev->data, evsel->name, len);
3175 err = process(tool, (union perf_event*) ev, NULL, NULL);
3181 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3182 struct perf_evsel *evsel,
3183 perf_event__handler_t process)
3185 size_t size = sizeof(struct event_update_event);
3186 struct event_update_event *ev;
3190 if (!evsel->own_cpus)
3193 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3197 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3198 ev->header.size = (u16)size;
3199 ev->type = PERF_EVENT_UPDATE__CPUS;
3200 ev->id = evsel->id[0];
3202 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3206 err = process(tool, (union perf_event*) ev, NULL, NULL);
3211 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3213 struct event_update_event *ev = &event->event_update;
3214 struct event_update_event_scale *ev_scale;
3215 struct event_update_event_cpus *ev_cpus;
3216 struct cpu_map *map;
3219 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3222 case PERF_EVENT_UPDATE__SCALE:
3223 ev_scale = (struct event_update_event_scale *) ev->data;
3224 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3226 case PERF_EVENT_UPDATE__UNIT:
3227 ret += fprintf(fp, "... unit: %s\n", ev->data);
3229 case PERF_EVENT_UPDATE__NAME:
3230 ret += fprintf(fp, "... name: %s\n", ev->data);
3232 case PERF_EVENT_UPDATE__CPUS:
3233 ev_cpus = (struct event_update_event_cpus *) ev->data;
3234 ret += fprintf(fp, "... ");
3236 map = cpu_map__new_data(&ev_cpus->cpus);
3238 ret += cpu_map__fprintf(map, fp);
3240 ret += fprintf(fp, "failed to get cpus\n");
3243 ret += fprintf(fp, "... unknown type\n");
3250 int perf_event__synthesize_attrs(struct perf_tool *tool,
3251 struct perf_session *session,
3252 perf_event__handler_t process)
3254 struct perf_evsel *evsel;
3257 evlist__for_each_entry(session->evlist, evsel) {
3258 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3259 evsel->id, process);
3261 pr_debug("failed to create perf header attribute\n");
3269 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3270 union perf_event *event,
3271 struct perf_evlist **pevlist)
3274 struct perf_evsel *evsel;
3275 struct perf_evlist *evlist = *pevlist;
3277 if (evlist == NULL) {
3278 *pevlist = evlist = perf_evlist__new();
3283 evsel = perf_evsel__new(&event->attr.attr);
3287 perf_evlist__add(evlist, evsel);
3289 ids = event->header.size;
3290 ids -= (void *)&event->attr.id - (void *)event;
3291 n_ids = ids / sizeof(u64);
3293 * We don't have the cpu and thread maps on the header, so
3294 * for allocating the perf_sample_id table we fake 1 cpu and
3295 * hattr->ids threads.
3297 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3300 for (i = 0; i < n_ids; i++) {
3301 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3304 symbol_conf.nr_events = evlist->nr_entries;
3309 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3310 union perf_event *event,
3311 struct perf_evlist **pevlist)
3313 struct event_update_event *ev = &event->event_update;
3314 struct event_update_event_scale *ev_scale;
3315 struct event_update_event_cpus *ev_cpus;
3316 struct perf_evlist *evlist;
3317 struct perf_evsel *evsel;
3318 struct cpu_map *map;
3320 if (!pevlist || *pevlist == NULL)
3325 evsel = perf_evlist__id2evsel(evlist, ev->id);
3330 case PERF_EVENT_UPDATE__UNIT:
3331 evsel->unit = strdup(ev->data);
3333 case PERF_EVENT_UPDATE__NAME:
3334 evsel->name = strdup(ev->data);
3336 case PERF_EVENT_UPDATE__SCALE:
3337 ev_scale = (struct event_update_event_scale *) ev->data;
3338 evsel->scale = ev_scale->scale;
3340 case PERF_EVENT_UPDATE__CPUS:
3341 ev_cpus = (struct event_update_event_cpus *) ev->data;
3343 map = cpu_map__new_data(&ev_cpus->cpus);
3345 evsel->own_cpus = map;
3347 pr_err("failed to get event_update cpus\n");
3355 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3356 struct perf_evlist *evlist,
3357 perf_event__handler_t process)
3359 union perf_event ev;
3360 struct tracing_data *tdata;
3361 ssize_t size = 0, aligned_size = 0, padding;
3363 int err __maybe_unused = 0;
3366 * We are going to store the size of the data followed
3367 * by the data contents. Since the fd descriptor is a pipe,
3368 * we cannot seek back to store the size of the data once
3369 * we know it. Instead we:
3371 * - write the tracing data to the temp file
3372 * - get/write the data size to pipe
3373 * - write the tracing data from the temp file
3376 tdata = tracing_data_get(&evlist->entries, fd, true);
3380 memset(&ev, 0, sizeof(ev));
3382 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3384 aligned_size = PERF_ALIGN(size, sizeof(u64));
3385 padding = aligned_size - size;
3386 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3387 ev.tracing_data.size = aligned_size;
3389 process(tool, &ev, NULL, NULL);
3392 * The put function will copy all the tracing data
3393 * stored in temp file to the pipe.
3395 tracing_data_put(tdata);
3397 ff = (struct feat_fd){ .fd = fd };
3398 if (write_padded(&ff, NULL, 0, padding))
3401 return aligned_size;
3404 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3405 union perf_event *event,
3406 struct perf_session *session)
3408 ssize_t size_read, padding, size = event->tracing_data.size;
3409 int fd = perf_data_file__fd(session->file);
3410 off_t offset = lseek(fd, 0, SEEK_CUR);
3413 /* setup for reading amidst mmap */
3414 lseek(fd, offset + sizeof(struct tracing_data_event),
3417 size_read = trace_report(fd, &session->tevent,
3419 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3421 if (readn(fd, buf, padding) < 0) {
3422 pr_err("%s: reading input file", __func__);
3425 if (session->repipe) {
3426 int retw = write(STDOUT_FILENO, buf, padding);
3427 if (retw <= 0 || retw != padding) {
3428 pr_err("%s: repiping tracing data padding", __func__);
3433 if (size_read + padding != size) {
3434 pr_err("%s: tracing data size mismatch", __func__);
3438 perf_evlist__prepare_tracepoint_events(session->evlist,
3439 session->tevent.pevent);
3441 return size_read + padding;
3444 int perf_event__synthesize_build_id(struct perf_tool *tool,
3445 struct dso *pos, u16 misc,
3446 perf_event__handler_t process,
3447 struct machine *machine)
3449 union perf_event ev;
3456 memset(&ev, 0, sizeof(ev));
3458 len = pos->long_name_len + 1;
3459 len = PERF_ALIGN(len, NAME_ALIGN);
3460 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3461 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3462 ev.build_id.header.misc = misc;
3463 ev.build_id.pid = machine->pid;
3464 ev.build_id.header.size = sizeof(ev.build_id) + len;
3465 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3467 err = process(tool, &ev, NULL, machine);
3472 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3473 union perf_event *event,
3474 struct perf_session *session)
3476 __event_process_build_id(&event->build_id,
3477 event->build_id.filename,