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 (MAX_NR_CPUS * 4)
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.
1885 * AArch64 is the same.
1887 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
1888 || !strncmp(ph->env.arch, "aarch64", 7)))
1889 do_core_id_test = false;
1891 for (i = 0; i < (u32)cpu_nr; i++) {
1892 if (do_read_u32(ff, &nr))
1895 ph->env.cpu[i].core_id = nr;
1897 if (do_read_u32(ff, &nr))
1900 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
1901 pr_debug("socket_id number is too big."
1902 "You may need to upgrade the perf tool.\n");
1906 ph->env.cpu[i].socket_id = nr;
1912 strbuf_release(&sb);
1914 zfree(&ph->env.cpu);
1918 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
1920 struct numa_node *nodes, *n;
1925 if (do_read_u32(ff, &nr))
1928 nodes = zalloc(sizeof(*nodes) * nr);
1932 for (i = 0; i < nr; i++) {
1936 if (do_read_u32(ff, &n->node))
1939 if (do_read_u64(ff, &n->mem_total))
1942 if (do_read_u64(ff, &n->mem_free))
1945 str = do_read_string(ff);
1949 n->map = cpu_map__new(str);
1955 ff->ph->env.nr_numa_nodes = nr;
1956 ff->ph->env.numa_nodes = nodes;
1964 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
1971 if (do_read_u32(ff, &pmu_num))
1975 pr_debug("pmu mappings not available\n");
1979 ff->ph->env.nr_pmu_mappings = pmu_num;
1980 if (strbuf_init(&sb, 128) < 0)
1984 if (do_read_u32(ff, &type))
1987 name = do_read_string(ff);
1991 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1993 /* include a NULL character at the end */
1994 if (strbuf_add(&sb, "", 1) < 0)
1997 if (!strcmp(name, "msr"))
1998 ff->ph->env.msr_pmu_type = type;
2003 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2007 strbuf_release(&sb);
2011 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2014 u32 i, nr, nr_groups;
2015 struct perf_session *session;
2016 struct perf_evsel *evsel, *leader = NULL;
2023 if (do_read_u32(ff, &nr_groups))
2026 ff->ph->env.nr_groups = nr_groups;
2028 pr_debug("group desc not available\n");
2032 desc = calloc(nr_groups, sizeof(*desc));
2036 for (i = 0; i < nr_groups; i++) {
2037 desc[i].name = do_read_string(ff);
2041 if (do_read_u32(ff, &desc[i].leader_idx))
2044 if (do_read_u32(ff, &desc[i].nr_members))
2049 * Rebuild group relationship based on the group_desc
2051 session = container_of(ff->ph, struct perf_session, header);
2052 session->evlist->nr_groups = nr_groups;
2055 evlist__for_each_entry(session->evlist, evsel) {
2056 if (evsel->idx == (int) desc[i].leader_idx) {
2057 evsel->leader = evsel;
2058 /* {anon_group} is a dummy name */
2059 if (strcmp(desc[i].name, "{anon_group}")) {
2060 evsel->group_name = desc[i].name;
2061 desc[i].name = NULL;
2063 evsel->nr_members = desc[i].nr_members;
2065 if (i >= nr_groups || nr > 0) {
2066 pr_debug("invalid group desc\n");
2071 nr = evsel->nr_members - 1;
2074 /* This is a group member */
2075 evsel->leader = leader;
2081 if (i != nr_groups || nr != 0) {
2082 pr_debug("invalid group desc\n");
2088 for (i = 0; i < nr_groups; i++)
2089 zfree(&desc[i].name);
2095 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2097 struct perf_session *session;
2100 session = container_of(ff->ph, struct perf_session, header);
2102 err = auxtrace_index__process(ff->fd, ff->size, session,
2103 ff->ph->needs_swap);
2105 pr_err("Failed to process auxtrace index\n");
2109 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2111 struct cpu_cache_level *caches;
2112 u32 cnt, i, version;
2114 if (do_read_u32(ff, &version))
2120 if (do_read_u32(ff, &cnt))
2123 caches = zalloc(sizeof(*caches) * cnt);
2127 for (i = 0; i < cnt; i++) {
2128 struct cpu_cache_level c;
2131 if (do_read_u32(ff, &c.v))\
2132 goto out_free_caches; \
2141 c.v = do_read_string(ff); \
2143 goto out_free_caches;
2153 ff->ph->env.caches = caches;
2154 ff->ph->env.caches_cnt = cnt;
2161 struct feature_ops {
2162 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2163 void (*print)(struct feat_fd *ff, FILE *fp);
2164 int (*process)(struct feat_fd *ff, void *data);
2170 #define FEAT_OPR(n, func, __full_only) \
2172 .name = __stringify(n), \
2173 .write = write_##func, \
2174 .print = print_##func, \
2175 .full_only = __full_only, \
2176 .process = process_##func, \
2177 .synthesize = true \
2180 #define FEAT_OPN(n, func, __full_only) \
2182 .name = __stringify(n), \
2183 .write = write_##func, \
2184 .print = print_##func, \
2185 .full_only = __full_only, \
2186 .process = process_##func \
2189 /* feature_ops not implemented: */
2190 #define print_tracing_data NULL
2191 #define print_build_id NULL
2193 #define process_branch_stack NULL
2194 #define process_stat NULL
2197 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2198 FEAT_OPN(TRACING_DATA, tracing_data, false),
2199 FEAT_OPN(BUILD_ID, build_id, false),
2200 FEAT_OPR(HOSTNAME, hostname, false),
2201 FEAT_OPR(OSRELEASE, osrelease, false),
2202 FEAT_OPR(VERSION, version, false),
2203 FEAT_OPR(ARCH, arch, false),
2204 FEAT_OPR(NRCPUS, nrcpus, false),
2205 FEAT_OPR(CPUDESC, cpudesc, false),
2206 FEAT_OPR(CPUID, cpuid, false),
2207 FEAT_OPR(TOTAL_MEM, total_mem, false),
2208 FEAT_OPR(EVENT_DESC, event_desc, false),
2209 FEAT_OPR(CMDLINE, cmdline, false),
2210 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2211 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2212 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2213 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2214 FEAT_OPR(GROUP_DESC, group_desc, false),
2215 FEAT_OPN(AUXTRACE, auxtrace, false),
2216 FEAT_OPN(STAT, stat, false),
2217 FEAT_OPN(CACHE, cache, true),
2220 struct header_print_data {
2222 bool full; /* extended list of headers */
2225 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2226 struct perf_header *ph,
2227 int feat, int fd, void *data)
2229 struct header_print_data *hd = data;
2232 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2233 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2234 "%d, continuing...\n", section->offset, feat);
2237 if (feat >= HEADER_LAST_FEATURE) {
2238 pr_warning("unknown feature %d\n", feat);
2241 if (!feat_ops[feat].print)
2244 ff = (struct feat_fd) {
2249 if (!feat_ops[feat].full_only || hd->full)
2250 feat_ops[feat].print(&ff, hd->fp);
2252 fprintf(hd->fp, "# %s info available, use -I to display\n",
2253 feat_ops[feat].name);
2258 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2260 struct header_print_data hd;
2261 struct perf_header *header = &session->header;
2262 int fd = perf_data_file__fd(session->file);
2269 ret = fstat(fd, &st);
2273 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2275 perf_header__process_sections(header, fd, &hd,
2276 perf_file_section__fprintf_info);
2278 if (session->file->is_pipe)
2281 fprintf(fp, "# missing features: ");
2282 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2284 fprintf(fp, "%s ", feat_ops[bit].name);
2291 static int do_write_feat(struct feat_fd *ff, int type,
2292 struct perf_file_section **p,
2293 struct perf_evlist *evlist)
2298 if (perf_header__has_feat(ff->ph, type)) {
2299 if (!feat_ops[type].write)
2302 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2305 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2307 err = feat_ops[type].write(ff, evlist);
2309 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2311 /* undo anything written */
2312 lseek(ff->fd, (*p)->offset, SEEK_SET);
2316 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2322 static int perf_header__adds_write(struct perf_header *header,
2323 struct perf_evlist *evlist, int fd)
2327 struct perf_file_section *feat_sec, *p;
2333 ff = (struct feat_fd){
2338 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2342 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2343 if (feat_sec == NULL)
2346 sec_size = sizeof(*feat_sec) * nr_sections;
2348 sec_start = header->feat_offset;
2349 lseek(fd, sec_start + sec_size, SEEK_SET);
2351 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2352 if (do_write_feat(&ff, feat, &p, evlist))
2353 perf_header__clear_feat(header, feat);
2356 lseek(fd, sec_start, SEEK_SET);
2358 * may write more than needed due to dropped feature, but
2359 * this is okay, reader will skip the mising entries
2361 err = do_write(&ff, feat_sec, sec_size);
2363 pr_debug("failed to write feature section\n");
2368 int perf_header__write_pipe(int fd)
2370 struct perf_pipe_file_header f_header;
2374 ff = (struct feat_fd){ .fd = fd };
2376 f_header = (struct perf_pipe_file_header){
2377 .magic = PERF_MAGIC,
2378 .size = sizeof(f_header),
2381 err = do_write(&ff, &f_header, sizeof(f_header));
2383 pr_debug("failed to write perf pipe header\n");
2390 int perf_session__write_header(struct perf_session *session,
2391 struct perf_evlist *evlist,
2392 int fd, bool at_exit)
2394 struct perf_file_header f_header;
2395 struct perf_file_attr f_attr;
2396 struct perf_header *header = &session->header;
2397 struct perf_evsel *evsel;
2402 ff = (struct feat_fd){ .fd = fd};
2403 lseek(fd, sizeof(f_header), SEEK_SET);
2405 evlist__for_each_entry(session->evlist, evsel) {
2406 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2407 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2409 pr_debug("failed to write perf header\n");
2414 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2416 evlist__for_each_entry(evlist, evsel) {
2417 f_attr = (struct perf_file_attr){
2418 .attr = evsel->attr,
2420 .offset = evsel->id_offset,
2421 .size = evsel->ids * sizeof(u64),
2424 err = do_write(&ff, &f_attr, sizeof(f_attr));
2426 pr_debug("failed to write perf header attribute\n");
2431 if (!header->data_offset)
2432 header->data_offset = lseek(fd, 0, SEEK_CUR);
2433 header->feat_offset = header->data_offset + header->data_size;
2436 err = perf_header__adds_write(header, evlist, fd);
2441 f_header = (struct perf_file_header){
2442 .magic = PERF_MAGIC,
2443 .size = sizeof(f_header),
2444 .attr_size = sizeof(f_attr),
2446 .offset = attr_offset,
2447 .size = evlist->nr_entries * sizeof(f_attr),
2450 .offset = header->data_offset,
2451 .size = header->data_size,
2453 /* event_types is ignored, store zeros */
2456 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2458 lseek(fd, 0, SEEK_SET);
2459 err = do_write(&ff, &f_header, sizeof(f_header));
2461 pr_debug("failed to write perf header\n");
2464 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2469 static int perf_header__getbuffer64(struct perf_header *header,
2470 int fd, void *buf, size_t size)
2472 if (readn(fd, buf, size) <= 0)
2475 if (header->needs_swap)
2476 mem_bswap_64(buf, size);
2481 int perf_header__process_sections(struct perf_header *header, int fd,
2483 int (*process)(struct perf_file_section *section,
2484 struct perf_header *ph,
2485 int feat, int fd, void *data))
2487 struct perf_file_section *feat_sec, *sec;
2493 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2497 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2501 sec_size = sizeof(*feat_sec) * nr_sections;
2503 lseek(fd, header->feat_offset, SEEK_SET);
2505 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2509 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2510 err = process(sec++, header, feat, fd, data);
2520 static const int attr_file_abi_sizes[] = {
2521 [0] = PERF_ATTR_SIZE_VER0,
2522 [1] = PERF_ATTR_SIZE_VER1,
2523 [2] = PERF_ATTR_SIZE_VER2,
2524 [3] = PERF_ATTR_SIZE_VER3,
2525 [4] = PERF_ATTR_SIZE_VER4,
2530 * In the legacy file format, the magic number is not used to encode endianness.
2531 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2532 * on ABI revisions, we need to try all combinations for all endianness to
2533 * detect the endianness.
2535 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2537 uint64_t ref_size, attr_size;
2540 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2541 ref_size = attr_file_abi_sizes[i]
2542 + sizeof(struct perf_file_section);
2543 if (hdr_sz != ref_size) {
2544 attr_size = bswap_64(hdr_sz);
2545 if (attr_size != ref_size)
2548 ph->needs_swap = true;
2550 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2555 /* could not determine endianness */
2559 #define PERF_PIPE_HDR_VER0 16
2561 static const size_t attr_pipe_abi_sizes[] = {
2562 [0] = PERF_PIPE_HDR_VER0,
2567 * In the legacy pipe format, there is an implicit assumption that endiannesss
2568 * between host recording the samples, and host parsing the samples is the
2569 * same. This is not always the case given that the pipe output may always be
2570 * redirected into a file and analyzed on a different machine with possibly a
2571 * different endianness and perf_event ABI revsions in the perf tool itself.
2573 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2578 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2579 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2580 attr_size = bswap_64(hdr_sz);
2581 if (attr_size != hdr_sz)
2584 ph->needs_swap = true;
2586 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2592 bool is_perf_magic(u64 magic)
2594 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2595 || magic == __perf_magic2
2596 || magic == __perf_magic2_sw)
2602 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2603 bool is_pipe, struct perf_header *ph)
2607 /* check for legacy format */
2608 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2610 ph->version = PERF_HEADER_VERSION_1;
2611 pr_debug("legacy perf.data format\n");
2613 return try_all_pipe_abis(hdr_sz, ph);
2615 return try_all_file_abis(hdr_sz, ph);
2618 * the new magic number serves two purposes:
2619 * - unique number to identify actual perf.data files
2620 * - encode endianness of file
2622 ph->version = PERF_HEADER_VERSION_2;
2624 /* check magic number with one endianness */
2625 if (magic == __perf_magic2)
2628 /* check magic number with opposite endianness */
2629 if (magic != __perf_magic2_sw)
2632 ph->needs_swap = true;
2637 int perf_file_header__read(struct perf_file_header *header,
2638 struct perf_header *ph, int fd)
2642 lseek(fd, 0, SEEK_SET);
2644 ret = readn(fd, header, sizeof(*header));
2648 if (check_magic_endian(header->magic,
2649 header->attr_size, false, ph) < 0) {
2650 pr_debug("magic/endian check failed\n");
2654 if (ph->needs_swap) {
2655 mem_bswap_64(header, offsetof(struct perf_file_header,
2659 if (header->size != sizeof(*header)) {
2660 /* Support the previous format */
2661 if (header->size == offsetof(typeof(*header), adds_features))
2662 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2665 } else if (ph->needs_swap) {
2667 * feature bitmap is declared as an array of unsigned longs --
2668 * not good since its size can differ between the host that
2669 * generated the data file and the host analyzing the file.
2671 * We need to handle endianness, but we don't know the size of
2672 * the unsigned long where the file was generated. Take a best
2673 * guess at determining it: try 64-bit swap first (ie., file
2674 * created on a 64-bit host), and check if the hostname feature
2675 * bit is set (this feature bit is forced on as of fbe96f2).
2676 * If the bit is not, undo the 64-bit swap and try a 32-bit
2677 * swap. If the hostname bit is still not set (e.g., older data
2678 * file), punt and fallback to the original behavior --
2679 * clearing all feature bits and setting buildid.
2681 mem_bswap_64(&header->adds_features,
2682 BITS_TO_U64(HEADER_FEAT_BITS));
2684 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2686 mem_bswap_64(&header->adds_features,
2687 BITS_TO_U64(HEADER_FEAT_BITS));
2690 mem_bswap_32(&header->adds_features,
2691 BITS_TO_U32(HEADER_FEAT_BITS));
2694 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2695 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2696 set_bit(HEADER_BUILD_ID, header->adds_features);
2700 memcpy(&ph->adds_features, &header->adds_features,
2701 sizeof(ph->adds_features));
2703 ph->data_offset = header->data.offset;
2704 ph->data_size = header->data.size;
2705 ph->feat_offset = header->data.offset + header->data.size;
2709 static int perf_file_section__process(struct perf_file_section *section,
2710 struct perf_header *ph,
2711 int feat, int fd, void *data)
2713 struct feat_fd fdd = {
2716 .size = section->size,
2717 .offset = section->offset,
2720 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2721 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2722 "%d, continuing...\n", section->offset, feat);
2726 if (feat >= HEADER_LAST_FEATURE) {
2727 pr_debug("unknown feature %d, continuing...\n", feat);
2731 if (!feat_ops[feat].process)
2734 return feat_ops[feat].process(&fdd, data);
2737 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2738 struct perf_header *ph, int fd,
2741 struct feat_fd ff = {
2742 .fd = STDOUT_FILENO,
2747 ret = readn(fd, header, sizeof(*header));
2751 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2752 pr_debug("endian/magic failed\n");
2757 header->size = bswap_64(header->size);
2759 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
2765 static int perf_header__read_pipe(struct perf_session *session)
2767 struct perf_header *header = &session->header;
2768 struct perf_pipe_file_header f_header;
2770 if (perf_file_header__read_pipe(&f_header, header,
2771 perf_data_file__fd(session->file),
2772 session->repipe) < 0) {
2773 pr_debug("incompatible file format\n");
2780 static int read_attr(int fd, struct perf_header *ph,
2781 struct perf_file_attr *f_attr)
2783 struct perf_event_attr *attr = &f_attr->attr;
2785 size_t our_sz = sizeof(f_attr->attr);
2788 memset(f_attr, 0, sizeof(*f_attr));
2790 /* read minimal guaranteed structure */
2791 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2793 pr_debug("cannot read %d bytes of header attr\n",
2794 PERF_ATTR_SIZE_VER0);
2798 /* on file perf_event_attr size */
2806 sz = PERF_ATTR_SIZE_VER0;
2807 } else if (sz > our_sz) {
2808 pr_debug("file uses a more recent and unsupported ABI"
2809 " (%zu bytes extra)\n", sz - our_sz);
2812 /* what we have not yet read and that we know about */
2813 left = sz - PERF_ATTR_SIZE_VER0;
2816 ptr += PERF_ATTR_SIZE_VER0;
2818 ret = readn(fd, ptr, left);
2820 /* read perf_file_section, ids are read in caller */
2821 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2823 return ret <= 0 ? -1 : 0;
2826 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2827 struct pevent *pevent)
2829 struct event_format *event;
2832 /* already prepared */
2833 if (evsel->tp_format)
2836 if (pevent == NULL) {
2837 pr_debug("broken or missing trace data\n");
2841 event = pevent_find_event(pevent, evsel->attr.config);
2842 if (event == NULL) {
2843 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
2848 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2849 evsel->name = strdup(bf);
2850 if (evsel->name == NULL)
2854 evsel->tp_format = event;
2858 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2859 struct pevent *pevent)
2861 struct perf_evsel *pos;
2863 evlist__for_each_entry(evlist, pos) {
2864 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2865 perf_evsel__prepare_tracepoint_event(pos, pevent))
2872 int perf_session__read_header(struct perf_session *session)
2874 struct perf_data_file *file = session->file;
2875 struct perf_header *header = &session->header;
2876 struct perf_file_header f_header;
2877 struct perf_file_attr f_attr;
2879 int nr_attrs, nr_ids, i, j;
2880 int fd = perf_data_file__fd(file);
2882 session->evlist = perf_evlist__new();
2883 if (session->evlist == NULL)
2886 session->evlist->env = &header->env;
2887 session->machines.host.env = &header->env;
2888 if (perf_data_file__is_pipe(file))
2889 return perf_header__read_pipe(session);
2891 if (perf_file_header__read(&f_header, header, fd) < 0)
2895 * Sanity check that perf.data was written cleanly; data size is
2896 * initialized to 0 and updated only if the on_exit function is run.
2897 * If data size is still 0 then the file contains only partial
2898 * information. Just warn user and process it as much as it can.
2900 if (f_header.data.size == 0) {
2901 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2902 "Was the 'perf record' command properly terminated?\n",
2906 if (f_header.attr_size == 0) {
2907 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
2908 "Was the 'perf record' command properly terminated?\n",
2913 nr_attrs = f_header.attrs.size / f_header.attr_size;
2914 lseek(fd, f_header.attrs.offset, SEEK_SET);
2916 for (i = 0; i < nr_attrs; i++) {
2917 struct perf_evsel *evsel;
2920 if (read_attr(fd, header, &f_attr) < 0)
2923 if (header->needs_swap) {
2924 f_attr.ids.size = bswap_64(f_attr.ids.size);
2925 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2926 perf_event__attr_swap(&f_attr.attr);
2929 tmp = lseek(fd, 0, SEEK_CUR);
2930 evsel = perf_evsel__new(&f_attr.attr);
2933 goto out_delete_evlist;
2935 evsel->needs_swap = header->needs_swap;
2937 * Do it before so that if perf_evsel__alloc_id fails, this
2938 * entry gets purged too at perf_evlist__delete().
2940 perf_evlist__add(session->evlist, evsel);
2942 nr_ids = f_attr.ids.size / sizeof(u64);
2944 * We don't have the cpu and thread maps on the header, so
2945 * for allocating the perf_sample_id table we fake 1 cpu and
2946 * hattr->ids threads.
2948 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2949 goto out_delete_evlist;
2951 lseek(fd, f_attr.ids.offset, SEEK_SET);
2953 for (j = 0; j < nr_ids; j++) {
2954 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2957 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2960 lseek(fd, tmp, SEEK_SET);
2963 symbol_conf.nr_events = nr_attrs;
2965 perf_header__process_sections(header, fd, &session->tevent,
2966 perf_file_section__process);
2968 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2969 session->tevent.pevent))
2970 goto out_delete_evlist;
2977 perf_evlist__delete(session->evlist);
2978 session->evlist = NULL;
2982 int perf_event__synthesize_attr(struct perf_tool *tool,
2983 struct perf_event_attr *attr, u32 ids, u64 *id,
2984 perf_event__handler_t process)
2986 union perf_event *ev;
2990 size = sizeof(struct perf_event_attr);
2991 size = PERF_ALIGN(size, sizeof(u64));
2992 size += sizeof(struct perf_event_header);
2993 size += ids * sizeof(u64);
3000 ev->attr.attr = *attr;
3001 memcpy(ev->attr.id, id, ids * sizeof(u64));
3003 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3004 ev->attr.header.size = (u16)size;
3006 if (ev->attr.header.size == size)
3007 err = process(tool, ev, NULL, NULL);
3016 int perf_event__synthesize_features(struct perf_tool *tool,
3017 struct perf_session *session,
3018 struct perf_evlist *evlist,
3019 perf_event__handler_t process)
3021 struct perf_header *header = &session->header;
3023 struct feature_event *fe;
3027 sz_hdr = sizeof(fe->header);
3028 sz = sizeof(union perf_event);
3029 /* get a nice alignment */
3030 sz = PERF_ALIGN(sz, page_size);
3032 memset(&ff, 0, sizeof(ff));
3034 ff.buf = malloc(sz);
3038 ff.size = sz - sz_hdr;
3040 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3041 if (!feat_ops[feat].synthesize) {
3042 pr_debug("No record header feature for header :%d\n", feat);
3046 ff.offset = sizeof(*fe);
3048 ret = feat_ops[feat].write(&ff, evlist);
3049 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3050 pr_debug("Error writing feature\n");
3053 /* ff.buf may have changed due to realloc in do_write() */
3055 memset(fe, 0, sizeof(*fe));
3058 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3059 fe->header.size = ff.offset;
3061 ret = process(tool, ff.buf, NULL, NULL);
3071 int perf_event__process_feature(struct perf_tool *tool,
3072 union perf_event *event,
3073 struct perf_session *session __maybe_unused)
3075 struct feat_fd ff = { .fd = 0 };
3076 struct feature_event *fe = (struct feature_event *)event;
3077 int type = fe->header.type;
3078 u64 feat = fe->feat_id;
3080 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3081 pr_warning("invalid record type %d in pipe-mode\n", type);
3084 if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3085 pr_warning("invalid record type %d in pipe-mode\n", type);
3089 if (!feat_ops[feat].process)
3092 ff.buf = (void *)fe->data;
3093 ff.size = event->header.size - sizeof(*fe);
3094 ff.ph = &session->header;
3096 if (feat_ops[feat].process(&ff, NULL))
3099 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3102 if (!feat_ops[feat].full_only ||
3103 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3104 feat_ops[feat].print(&ff, stdout);
3106 fprintf(stdout, "# %s info available, use -I to display\n",
3107 feat_ops[feat].name);
3113 static struct event_update_event *
3114 event_update_event__new(size_t size, u64 type, u64 id)
3116 struct event_update_event *ev;
3118 size += sizeof(*ev);
3119 size = PERF_ALIGN(size, sizeof(u64));
3123 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3124 ev->header.size = (u16)size;
3132 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3133 struct perf_evsel *evsel,
3134 perf_event__handler_t process)
3136 struct event_update_event *ev;
3137 size_t size = strlen(evsel->unit);
3140 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3144 strlcpy(ev->data, evsel->unit, size + 1);
3145 err = process(tool, (union perf_event *)ev, NULL, NULL);
3151 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3152 struct perf_evsel *evsel,
3153 perf_event__handler_t process)
3155 struct event_update_event *ev;
3156 struct event_update_event_scale *ev_data;
3159 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3163 ev_data = (struct event_update_event_scale *) ev->data;
3164 ev_data->scale = evsel->scale;
3165 err = process(tool, (union perf_event*) ev, NULL, NULL);
3171 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3172 struct perf_evsel *evsel,
3173 perf_event__handler_t process)
3175 struct event_update_event *ev;
3176 size_t len = strlen(evsel->name);
3179 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3183 strlcpy(ev->data, evsel->name, len + 1);
3184 err = process(tool, (union perf_event*) ev, NULL, NULL);
3190 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3191 struct perf_evsel *evsel,
3192 perf_event__handler_t process)
3194 size_t size = sizeof(struct event_update_event);
3195 struct event_update_event *ev;
3199 if (!evsel->own_cpus)
3202 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3206 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3207 ev->header.size = (u16)size;
3208 ev->type = PERF_EVENT_UPDATE__CPUS;
3209 ev->id = evsel->id[0];
3211 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3215 err = process(tool, (union perf_event*) ev, NULL, NULL);
3220 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3222 struct event_update_event *ev = &event->event_update;
3223 struct event_update_event_scale *ev_scale;
3224 struct event_update_event_cpus *ev_cpus;
3225 struct cpu_map *map;
3228 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3231 case PERF_EVENT_UPDATE__SCALE:
3232 ev_scale = (struct event_update_event_scale *) ev->data;
3233 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3235 case PERF_EVENT_UPDATE__UNIT:
3236 ret += fprintf(fp, "... unit: %s\n", ev->data);
3238 case PERF_EVENT_UPDATE__NAME:
3239 ret += fprintf(fp, "... name: %s\n", ev->data);
3241 case PERF_EVENT_UPDATE__CPUS:
3242 ev_cpus = (struct event_update_event_cpus *) ev->data;
3243 ret += fprintf(fp, "... ");
3245 map = cpu_map__new_data(&ev_cpus->cpus);
3247 ret += cpu_map__fprintf(map, fp);
3249 ret += fprintf(fp, "failed to get cpus\n");
3252 ret += fprintf(fp, "... unknown type\n");
3259 int perf_event__synthesize_attrs(struct perf_tool *tool,
3260 struct perf_session *session,
3261 perf_event__handler_t process)
3263 struct perf_evsel *evsel;
3266 evlist__for_each_entry(session->evlist, evsel) {
3267 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3268 evsel->id, process);
3270 pr_debug("failed to create perf header attribute\n");
3278 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3279 union perf_event *event,
3280 struct perf_evlist **pevlist)
3283 struct perf_evsel *evsel;
3284 struct perf_evlist *evlist = *pevlist;
3286 if (evlist == NULL) {
3287 *pevlist = evlist = perf_evlist__new();
3292 evsel = perf_evsel__new(&event->attr.attr);
3296 perf_evlist__add(evlist, evsel);
3298 ids = event->header.size;
3299 ids -= (void *)&event->attr.id - (void *)event;
3300 n_ids = ids / sizeof(u64);
3302 * We don't have the cpu and thread maps on the header, so
3303 * for allocating the perf_sample_id table we fake 1 cpu and
3304 * hattr->ids threads.
3306 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3309 for (i = 0; i < n_ids; i++) {
3310 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3313 symbol_conf.nr_events = evlist->nr_entries;
3318 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3319 union perf_event *event,
3320 struct perf_evlist **pevlist)
3322 struct event_update_event *ev = &event->event_update;
3323 struct event_update_event_scale *ev_scale;
3324 struct event_update_event_cpus *ev_cpus;
3325 struct perf_evlist *evlist;
3326 struct perf_evsel *evsel;
3327 struct cpu_map *map;
3329 if (!pevlist || *pevlist == NULL)
3334 evsel = perf_evlist__id2evsel(evlist, ev->id);
3339 case PERF_EVENT_UPDATE__UNIT:
3340 evsel->unit = strdup(ev->data);
3342 case PERF_EVENT_UPDATE__NAME:
3343 evsel->name = strdup(ev->data);
3345 case PERF_EVENT_UPDATE__SCALE:
3346 ev_scale = (struct event_update_event_scale *) ev->data;
3347 evsel->scale = ev_scale->scale;
3349 case PERF_EVENT_UPDATE__CPUS:
3350 ev_cpus = (struct event_update_event_cpus *) ev->data;
3352 map = cpu_map__new_data(&ev_cpus->cpus);
3354 evsel->own_cpus = map;
3356 pr_err("failed to get event_update cpus\n");
3364 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3365 struct perf_evlist *evlist,
3366 perf_event__handler_t process)
3368 union perf_event ev;
3369 struct tracing_data *tdata;
3370 ssize_t size = 0, aligned_size = 0, padding;
3372 int err __maybe_unused = 0;
3375 * We are going to store the size of the data followed
3376 * by the data contents. Since the fd descriptor is a pipe,
3377 * we cannot seek back to store the size of the data once
3378 * we know it. Instead we:
3380 * - write the tracing data to the temp file
3381 * - get/write the data size to pipe
3382 * - write the tracing data from the temp file
3385 tdata = tracing_data_get(&evlist->entries, fd, true);
3389 memset(&ev, 0, sizeof(ev));
3391 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3393 aligned_size = PERF_ALIGN(size, sizeof(u64));
3394 padding = aligned_size - size;
3395 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3396 ev.tracing_data.size = aligned_size;
3398 process(tool, &ev, NULL, NULL);
3401 * The put function will copy all the tracing data
3402 * stored in temp file to the pipe.
3404 tracing_data_put(tdata);
3406 ff = (struct feat_fd){ .fd = fd };
3407 if (write_padded(&ff, NULL, 0, padding))
3410 return aligned_size;
3413 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3414 union perf_event *event,
3415 struct perf_session *session)
3417 ssize_t size_read, padding, size = event->tracing_data.size;
3418 int fd = perf_data_file__fd(session->file);
3419 off_t offset = lseek(fd, 0, SEEK_CUR);
3422 /* setup for reading amidst mmap */
3423 lseek(fd, offset + sizeof(struct tracing_data_event),
3426 size_read = trace_report(fd, &session->tevent,
3428 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3430 if (readn(fd, buf, padding) < 0) {
3431 pr_err("%s: reading input file", __func__);
3434 if (session->repipe) {
3435 int retw = write(STDOUT_FILENO, buf, padding);
3436 if (retw <= 0 || retw != padding) {
3437 pr_err("%s: repiping tracing data padding", __func__);
3442 if (size_read + padding != size) {
3443 pr_err("%s: tracing data size mismatch", __func__);
3447 perf_evlist__prepare_tracepoint_events(session->evlist,
3448 session->tevent.pevent);
3450 return size_read + padding;
3453 int perf_event__synthesize_build_id(struct perf_tool *tool,
3454 struct dso *pos, u16 misc,
3455 perf_event__handler_t process,
3456 struct machine *machine)
3458 union perf_event ev;
3465 memset(&ev, 0, sizeof(ev));
3467 len = pos->long_name_len + 1;
3468 len = PERF_ALIGN(len, NAME_ALIGN);
3469 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3470 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3471 ev.build_id.header.misc = misc;
3472 ev.build_id.pid = machine->pid;
3473 ev.build_id.header.size = sizeof(ev.build_id) + len;
3474 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3476 err = process(tool, &ev, NULL, machine);
3481 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3482 union perf_event *event,
3483 struct perf_session *session)
3485 __event_process_build_id(&event->build_id,
3486 event->build_id.filename,