11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 #include <linux/bitops.h>
14 #include <sys/utsname.h>
20 #include "trace-event.h"
30 #include <api/fs/fs.h>
33 #include "sane_ctype.h"
37 * must be a numerical value to let the endianness
38 * determine the memory layout. That way we are able
39 * to detect endianness when reading the perf.data file
42 * we check for legacy (PERFFILE) format.
44 static const char *__perf_magic1 = "PERFFILE";
45 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
46 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
48 #define PERF_MAGIC __perf_magic2
50 const char perf_version_string[] = PERF_VERSION;
52 struct perf_file_attr {
53 struct perf_event_attr attr;
54 struct perf_file_section ids;
57 void perf_header__set_feat(struct perf_header *header, int feat)
59 set_bit(feat, header->adds_features);
62 void perf_header__clear_feat(struct perf_header *header, int feat)
64 clear_bit(feat, header->adds_features);
67 bool perf_header__has_feat(const struct perf_header *header, int feat)
69 return test_bit(feat, header->adds_features);
72 static int do_write(int fd, const void *buf, size_t size)
75 int ret = write(fd, buf, size);
87 int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
89 static const char zero_buf[NAME_ALIGN];
90 int err = do_write(fd, bf, count);
93 err = do_write(fd, zero_buf, count_aligned - count);
98 #define string_size(str) \
99 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
101 static int do_write_string(int fd, const char *str)
106 olen = strlen(str) + 1;
107 len = PERF_ALIGN(olen, NAME_ALIGN);
109 /* write len, incl. \0 */
110 ret = do_write(fd, &len, sizeof(len));
114 return write_padded(fd, str, olen, len);
117 static char *do_read_string(int fd, struct perf_header *ph)
123 sz = readn(fd, &len, sizeof(len));
124 if (sz < (ssize_t)sizeof(len))
134 ret = readn(fd, buf, len);
135 if (ret == (ssize_t)len) {
137 * strings are padded by zeroes
138 * thus the actual strlen of buf
139 * may be less than len
148 static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
149 struct perf_evlist *evlist)
151 return read_tracing_data(fd, &evlist->entries);
155 static int write_build_id(int fd, struct perf_header *h,
156 struct perf_evlist *evlist __maybe_unused)
158 struct perf_session *session;
161 session = container_of(h, struct perf_session, header);
163 if (!perf_session__read_build_ids(session, true))
166 err = perf_session__write_buildid_table(session, fd);
168 pr_debug("failed to write buildid table\n");
171 perf_session__cache_build_ids(session);
176 static int write_hostname(int fd, struct perf_header *h __maybe_unused,
177 struct perf_evlist *evlist __maybe_unused)
186 return do_write_string(fd, uts.nodename);
189 static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
190 struct perf_evlist *evlist __maybe_unused)
199 return do_write_string(fd, uts.release);
202 static int write_arch(int fd, struct perf_header *h __maybe_unused,
203 struct perf_evlist *evlist __maybe_unused)
212 return do_write_string(fd, uts.machine);
215 static int write_version(int fd, struct perf_header *h __maybe_unused,
216 struct perf_evlist *evlist __maybe_unused)
218 return do_write_string(fd, perf_version_string);
221 static int __write_cpudesc(int fd, const char *cpuinfo_proc)
226 const char *search = cpuinfo_proc;
233 file = fopen("/proc/cpuinfo", "r");
237 while (getline(&buf, &len, file) > 0) {
238 ret = strncmp(buf, search, strlen(search));
250 p = strchr(buf, ':');
251 if (p && *(p+1) == ' ' && *(p+2))
257 /* squash extra space characters (branding string) */
264 while (*q && isspace(*q))
267 while ((*r++ = *q++));
271 ret = do_write_string(fd, s);
278 static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
279 struct perf_evlist *evlist __maybe_unused)
282 #define CPUINFO_PROC {"model name", }
284 const char *cpuinfo_procs[] = CPUINFO_PROC;
287 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
289 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
297 static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
298 struct perf_evlist *evlist __maybe_unused)
304 nrc = cpu__max_present_cpu();
306 nr = sysconf(_SC_NPROCESSORS_ONLN);
310 nra = (u32)(nr & UINT_MAX);
312 ret = do_write(fd, &nrc, sizeof(nrc));
316 return do_write(fd, &nra, sizeof(nra));
319 static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
320 struct perf_evlist *evlist)
322 struct perf_evsel *evsel;
326 nre = evlist->nr_entries;
329 * write number of events
331 ret = do_write(fd, &nre, sizeof(nre));
336 * size of perf_event_attr struct
338 sz = (u32)sizeof(evsel->attr);
339 ret = do_write(fd, &sz, sizeof(sz));
343 evlist__for_each_entry(evlist, evsel) {
344 ret = do_write(fd, &evsel->attr, sz);
348 * write number of unique id per event
349 * there is one id per instance of an event
351 * copy into an nri to be independent of the
355 ret = do_write(fd, &nri, sizeof(nri));
360 * write event string as passed on cmdline
362 ret = do_write_string(fd, perf_evsel__name(evsel));
366 * write unique ids for this event
368 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
375 static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
376 struct perf_evlist *evlist __maybe_unused)
378 char buf[MAXPATHLEN];
382 /* actual path to perf binary */
383 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
387 /* readlink() does not add null termination */
390 /* account for binary path */
391 n = perf_env.nr_cmdline + 1;
393 ret = do_write(fd, &n, sizeof(n));
397 ret = do_write_string(fd, buf);
401 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
402 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
409 #define CORE_SIB_FMT \
410 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
411 #define THRD_SIB_FMT \
412 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
418 char **core_siblings;
419 char **thread_siblings;
422 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
425 char filename[MAXPATHLEN];
426 char *buf = NULL, *p;
432 sprintf(filename, CORE_SIB_FMT, cpu);
433 fp = fopen(filename, "r");
437 sret = getline(&buf, &len, fp);
442 p = strchr(buf, '\n');
446 for (i = 0; i < tp->core_sib; i++) {
447 if (!strcmp(buf, tp->core_siblings[i]))
450 if (i == tp->core_sib) {
451 tp->core_siblings[i] = buf;
459 sprintf(filename, THRD_SIB_FMT, cpu);
460 fp = fopen(filename, "r");
464 if (getline(&buf, &len, fp) <= 0)
467 p = strchr(buf, '\n');
471 for (i = 0; i < tp->thread_sib; i++) {
472 if (!strcmp(buf, tp->thread_siblings[i]))
475 if (i == tp->thread_sib) {
476 tp->thread_siblings[i] = buf;
488 static void free_cpu_topo(struct cpu_topo *tp)
495 for (i = 0 ; i < tp->core_sib; i++)
496 zfree(&tp->core_siblings[i]);
498 for (i = 0 ; i < tp->thread_sib; i++)
499 zfree(&tp->thread_siblings[i]);
504 static struct cpu_topo *build_cpu_topology(void)
506 struct cpu_topo *tp = NULL;
514 ncpus = cpu__max_present_cpu();
516 /* build online CPU map */
517 map = cpu_map__new(NULL);
519 pr_debug("failed to get system cpumap\n");
523 nr = (u32)(ncpus & UINT_MAX);
525 sz = nr * sizeof(char *);
526 addr = calloc(1, sizeof(*tp) + 2 * sz);
533 tp->core_siblings = addr;
535 tp->thread_siblings = addr;
537 for (i = 0; i < nr; i++) {
538 if (!cpu_map__has(map, i))
541 ret = build_cpu_topo(tp, i);
555 static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
556 struct perf_evlist *evlist __maybe_unused)
562 tp = build_cpu_topology();
566 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
570 for (i = 0; i < tp->core_sib; i++) {
571 ret = do_write_string(fd, tp->core_siblings[i]);
575 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
579 for (i = 0; i < tp->thread_sib; i++) {
580 ret = do_write_string(fd, tp->thread_siblings[i]);
585 ret = perf_env__read_cpu_topology_map(&perf_env);
589 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
590 ret = do_write(fd, &perf_env.cpu[j].core_id,
591 sizeof(perf_env.cpu[j].core_id));
594 ret = do_write(fd, &perf_env.cpu[j].socket_id,
595 sizeof(perf_env.cpu[j].socket_id));
606 static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
607 struct perf_evlist *evlist __maybe_unused)
615 fp = fopen("/proc/meminfo", "r");
619 while (getline(&buf, &len, fp) > 0) {
620 ret = strncmp(buf, "MemTotal:", 9);
625 n = sscanf(buf, "%*s %"PRIu64, &mem);
627 ret = do_write(fd, &mem, sizeof(mem));
635 static int write_topo_node(int fd, int node)
637 char str[MAXPATHLEN];
639 char *buf = NULL, *p;
642 u64 mem_total, mem_free, mem;
645 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
646 fp = fopen(str, "r");
650 while (getline(&buf, &len, fp) > 0) {
651 /* skip over invalid lines */
652 if (!strchr(buf, ':'))
654 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
656 if (!strcmp(field, "MemTotal:"))
658 if (!strcmp(field, "MemFree:"))
665 ret = do_write(fd, &mem_total, sizeof(u64));
669 ret = do_write(fd, &mem_free, sizeof(u64));
674 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
676 fp = fopen(str, "r");
680 if (getline(&buf, &len, fp) <= 0)
683 p = strchr(buf, '\n');
687 ret = do_write_string(fd, buf);
695 static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
696 struct perf_evlist *evlist __maybe_unused)
701 struct cpu_map *node_map = NULL;
706 fp = fopen("/sys/devices/system/node/online", "r");
710 if (getline(&buf, &len, fp) <= 0)
713 c = strchr(buf, '\n');
717 node_map = cpu_map__new(buf);
721 nr = (u32)node_map->nr;
723 ret = do_write(fd, &nr, sizeof(nr));
727 for (i = 0; i < nr; i++) {
728 j = (u32)node_map->map[i];
729 ret = do_write(fd, &j, sizeof(j));
733 ret = write_topo_node(fd, i);
740 cpu_map__put(node_map);
747 * struct pmu_mappings {
756 static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
757 struct perf_evlist *evlist __maybe_unused)
759 struct perf_pmu *pmu = NULL;
760 off_t offset = lseek(fd, 0, SEEK_CUR);
764 /* write real pmu_num later */
765 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
769 while ((pmu = perf_pmu__scan(pmu))) {
774 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
778 ret = do_write_string(fd, pmu->name);
783 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
785 lseek(fd, offset, SEEK_SET);
795 * struct group_descs {
797 * struct group_desc {
804 static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
805 struct perf_evlist *evlist)
807 u32 nr_groups = evlist->nr_groups;
808 struct perf_evsel *evsel;
811 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
815 evlist__for_each_entry(evlist, evsel) {
816 if (perf_evsel__is_group_leader(evsel) &&
817 evsel->nr_members > 1) {
818 const char *name = evsel->group_name ?: "{anon_group}";
819 u32 leader_idx = evsel->idx;
820 u32 nr_members = evsel->nr_members;
822 ret = do_write_string(fd, name);
826 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
830 ret = do_write(fd, &nr_members, sizeof(nr_members));
839 * default get_cpuid(): nothing gets recorded
840 * actual implementation must be in arch/$(ARCH)/util/header.c
842 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
847 static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
848 struct perf_evlist *evlist __maybe_unused)
853 ret = get_cpuid(buffer, sizeof(buffer));
859 return do_write_string(fd, buffer);
862 static int write_branch_stack(int fd __maybe_unused,
863 struct perf_header *h __maybe_unused,
864 struct perf_evlist *evlist __maybe_unused)
869 static int write_auxtrace(int fd, struct perf_header *h,
870 struct perf_evlist *evlist __maybe_unused)
872 struct perf_session *session;
875 session = container_of(h, struct perf_session, header);
877 err = auxtrace_index__write(fd, &session->auxtrace_index);
879 pr_err("Failed to write auxtrace index\n");
883 static int cpu_cache_level__sort(const void *a, const void *b)
885 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
886 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
888 return cache_a->level - cache_b->level;
891 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
893 if (a->level != b->level)
896 if (a->line_size != b->line_size)
899 if (a->sets != b->sets)
902 if (a->ways != b->ways)
905 if (strcmp(a->type, b->type))
908 if (strcmp(a->size, b->size))
911 if (strcmp(a->map, b->map))
917 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
919 char path[PATH_MAX], file[PATH_MAX];
923 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
924 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
929 scnprintf(file, PATH_MAX, "%s/level", path);
930 if (sysfs__read_int(file, (int *) &cache->level))
933 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
934 if (sysfs__read_int(file, (int *) &cache->line_size))
937 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
938 if (sysfs__read_int(file, (int *) &cache->sets))
941 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
942 if (sysfs__read_int(file, (int *) &cache->ways))
945 scnprintf(file, PATH_MAX, "%s/type", path);
946 if (sysfs__read_str(file, &cache->type, &len))
949 cache->type[len] = 0;
950 cache->type = rtrim(cache->type);
952 scnprintf(file, PATH_MAX, "%s/size", path);
953 if (sysfs__read_str(file, &cache->size, &len)) {
958 cache->size[len] = 0;
959 cache->size = rtrim(cache->size);
961 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
962 if (sysfs__read_str(file, &cache->map, &len)) {
969 cache->map = rtrim(cache->map);
973 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
975 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
978 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
985 ncpus = sysconf(_SC_NPROCESSORS_CONF);
989 nr = (u32)(ncpus & UINT_MAX);
991 for (cpu = 0; cpu < nr; cpu++) {
992 for (level = 0; level < 10; level++) {
993 struct cpu_cache_level c;
996 err = cpu_cache_level__read(&c, cpu, level);
1003 for (i = 0; i < cnt; i++) {
1004 if (cpu_cache_level__cmp(&c, &caches[i]))
1011 cpu_cache_level__free(&c);
1013 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1022 #define MAX_CACHES 2000
1024 static int write_cache(int fd, struct perf_header *h __maybe_unused,
1025 struct perf_evlist *evlist __maybe_unused)
1027 struct cpu_cache_level caches[MAX_CACHES];
1028 u32 cnt = 0, i, version = 1;
1031 ret = build_caches(caches, MAX_CACHES, &cnt);
1035 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1037 ret = do_write(fd, &version, sizeof(u32));
1041 ret = do_write(fd, &cnt, sizeof(u32));
1045 for (i = 0; i < cnt; i++) {
1046 struct cpu_cache_level *c = &caches[i];
1049 ret = do_write(fd, &c->v, sizeof(u32)); \
1060 ret = do_write_string(fd, (const char *) c->v); \
1071 for (i = 0; i < cnt; i++)
1072 cpu_cache_level__free(&caches[i]);
1076 static int write_stat(int fd __maybe_unused,
1077 struct perf_header *h __maybe_unused,
1078 struct perf_evlist *evlist __maybe_unused)
1083 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1086 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
1089 static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1092 fprintf(fp, "# os release : %s\n", ph->env.os_release);
1095 static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1097 fprintf(fp, "# arch : %s\n", ph->env.arch);
1100 static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1103 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
1106 static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1109 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1110 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
1113 static void print_version(struct perf_header *ph, int fd __maybe_unused,
1116 fprintf(fp, "# perf version : %s\n", ph->env.version);
1119 static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1124 nr = ph->env.nr_cmdline;
1126 fprintf(fp, "# cmdline : ");
1128 for (i = 0; i < nr; i++)
1129 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
1133 static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1138 int cpu_nr = ph->env.nr_cpus_avail;
1140 nr = ph->env.nr_sibling_cores;
1141 str = ph->env.sibling_cores;
1143 for (i = 0; i < nr; i++) {
1144 fprintf(fp, "# sibling cores : %s\n", str);
1145 str += strlen(str) + 1;
1148 nr = ph->env.nr_sibling_threads;
1149 str = ph->env.sibling_threads;
1151 for (i = 0; i < nr; i++) {
1152 fprintf(fp, "# sibling threads : %s\n", str);
1153 str += strlen(str) + 1;
1156 if (ph->env.cpu != NULL) {
1157 for (i = 0; i < cpu_nr; i++)
1158 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1159 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1161 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1164 static void free_event_desc(struct perf_evsel *events)
1166 struct perf_evsel *evsel;
1171 for (evsel = events; evsel->attr.size; evsel++) {
1172 zfree(&evsel->name);
1179 static struct perf_evsel *
1180 read_event_desc(struct perf_header *ph, int fd)
1182 struct perf_evsel *evsel, *events = NULL;
1185 u32 nre, sz, nr, i, j;
1189 /* number of events */
1190 ret = readn(fd, &nre, sizeof(nre));
1191 if (ret != (ssize_t)sizeof(nre))
1195 nre = bswap_32(nre);
1197 ret = readn(fd, &sz, sizeof(sz));
1198 if (ret != (ssize_t)sizeof(sz))
1204 /* buffer to hold on file attr struct */
1209 /* the last event terminates with evsel->attr.size == 0: */
1210 events = calloc(nre + 1, sizeof(*events));
1214 msz = sizeof(evsel->attr);
1218 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1222 * must read entire on-file attr struct to
1223 * sync up with layout.
1225 ret = readn(fd, buf, sz);
1226 if (ret != (ssize_t)sz)
1230 perf_event__attr_swap(buf);
1232 memcpy(&evsel->attr, buf, msz);
1234 ret = readn(fd, &nr, sizeof(nr));
1235 if (ret != (ssize_t)sizeof(nr))
1238 if (ph->needs_swap) {
1240 evsel->needs_swap = true;
1243 evsel->name = do_read_string(fd, ph);
1248 id = calloc(nr, sizeof(*id));
1254 for (j = 0 ; j < nr; j++) {
1255 ret = readn(fd, id, sizeof(*id));
1256 if (ret != (ssize_t)sizeof(*id))
1259 *id = bswap_64(*id);
1267 free_event_desc(events);
1272 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1273 void *priv __attribute__((unused)))
1275 return fprintf(fp, ", %s = %s", name, val);
1278 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1280 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1285 fprintf(fp, "# event desc: not available or unable to read\n");
1289 for (evsel = events; evsel->attr.size; evsel++) {
1290 fprintf(fp, "# event : name = %s, ", evsel->name);
1293 fprintf(fp, ", id = {");
1294 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1297 fprintf(fp, " %"PRIu64, *id);
1302 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1307 free_event_desc(events);
1310 static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1313 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1316 static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1320 struct numa_node *n;
1322 for (i = 0; i < ph->env.nr_numa_nodes; i++) {
1323 n = &ph->env.numa_nodes[i];
1325 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1326 " free = %"PRIu64" kB\n",
1327 n->node, n->mem_total, n->mem_free);
1329 fprintf(fp, "# node%u cpu list : ", n->node);
1330 cpu_map__fprintf(n->map, fp);
1334 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1336 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1339 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1340 int fd __maybe_unused, FILE *fp)
1342 fprintf(fp, "# contains samples with branch stack\n");
1345 static void print_auxtrace(struct perf_header *ph __maybe_unused,
1346 int fd __maybe_unused, FILE *fp)
1348 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1351 static void print_stat(struct perf_header *ph __maybe_unused,
1352 int fd __maybe_unused, FILE *fp)
1354 fprintf(fp, "# contains stat data\n");
1357 static void print_cache(struct perf_header *ph __maybe_unused,
1358 int fd __maybe_unused, FILE *fp __maybe_unused)
1362 fprintf(fp, "# CPU cache info:\n");
1363 for (i = 0; i < ph->env.caches_cnt; i++) {
1365 cpu_cache_level__fprintf(fp, &ph->env.caches[i]);
1369 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1372 const char *delimiter = "# pmu mappings: ";
1377 pmu_num = ph->env.nr_pmu_mappings;
1379 fprintf(fp, "# pmu mappings: not available\n");
1383 str = ph->env.pmu_mappings;
1386 type = strtoul(str, &tmp, 0);
1391 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1394 str += strlen(str) + 1;
1403 fprintf(fp, "# pmu mappings: unable to read\n");
1406 static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1409 struct perf_session *session;
1410 struct perf_evsel *evsel;
1413 session = container_of(ph, struct perf_session, header);
1415 evlist__for_each_entry(session->evlist, evsel) {
1416 if (perf_evsel__is_group_leader(evsel) &&
1417 evsel->nr_members > 1) {
1418 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1419 perf_evsel__name(evsel));
1421 nr = evsel->nr_members - 1;
1423 fprintf(fp, ",%s", perf_evsel__name(evsel));
1431 static int __event_process_build_id(struct build_id_event *bev,
1433 struct perf_session *session)
1436 struct machine *machine;
1439 enum dso_kernel_type dso_type;
1441 machine = perf_session__findnew_machine(session, bev->pid);
1445 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1448 case PERF_RECORD_MISC_KERNEL:
1449 dso_type = DSO_TYPE_KERNEL;
1451 case PERF_RECORD_MISC_GUEST_KERNEL:
1452 dso_type = DSO_TYPE_GUEST_KERNEL;
1454 case PERF_RECORD_MISC_USER:
1455 case PERF_RECORD_MISC_GUEST_USER:
1456 dso_type = DSO_TYPE_USER;
1462 dso = machine__findnew_dso(machine, filename);
1464 char sbuild_id[SBUILD_ID_SIZE];
1466 dso__set_build_id(dso, &bev->build_id);
1468 if (!is_kernel_module(filename, cpumode))
1469 dso->kernel = dso_type;
1471 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1473 pr_debug("build id event received for %s: %s\n",
1474 dso->long_name, sbuild_id);
1483 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1484 int input, u64 offset, u64 size)
1486 struct perf_session *session = container_of(header, struct perf_session, header);
1488 struct perf_event_header header;
1489 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1492 struct build_id_event bev;
1493 char filename[PATH_MAX];
1494 u64 limit = offset + size;
1496 while (offset < limit) {
1499 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1502 if (header->needs_swap)
1503 perf_event_header__bswap(&old_bev.header);
1505 len = old_bev.header.size - sizeof(old_bev);
1506 if (readn(input, filename, len) != len)
1509 bev.header = old_bev.header;
1512 * As the pid is the missing value, we need to fill
1513 * it properly. The header.misc value give us nice hint.
1515 bev.pid = HOST_KERNEL_ID;
1516 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1517 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1518 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1520 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1521 __event_process_build_id(&bev, filename, session);
1523 offset += bev.header.size;
1529 static int perf_header__read_build_ids(struct perf_header *header,
1530 int input, u64 offset, u64 size)
1532 struct perf_session *session = container_of(header, struct perf_session, header);
1533 struct build_id_event bev;
1534 char filename[PATH_MAX];
1535 u64 limit = offset + size, orig_offset = offset;
1538 while (offset < limit) {
1541 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1544 if (header->needs_swap)
1545 perf_event_header__bswap(&bev.header);
1547 len = bev.header.size - sizeof(bev);
1548 if (readn(input, filename, len) != len)
1551 * The a1645ce1 changeset:
1553 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1555 * Added a field to struct build_id_event that broke the file
1558 * Since the kernel build-id is the first entry, process the
1559 * table using the old format if the well known
1560 * '[kernel.kallsyms]' string for the kernel build-id has the
1561 * first 4 characters chopped off (where the pid_t sits).
1563 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1564 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1566 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1569 __event_process_build_id(&bev, filename, session);
1571 offset += bev.header.size;
1578 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1579 struct perf_header *ph __maybe_unused,
1582 ssize_t ret = trace_report(fd, data, false);
1583 return ret < 0 ? -1 : 0;
1586 static int process_build_id(struct perf_file_section *section,
1587 struct perf_header *ph, int fd,
1588 void *data __maybe_unused)
1590 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1591 pr_debug("Failed to read buildids, continuing...\n");
1595 static int process_hostname(struct perf_file_section *section __maybe_unused,
1596 struct perf_header *ph, int fd,
1597 void *data __maybe_unused)
1599 ph->env.hostname = do_read_string(fd, ph);
1600 return ph->env.hostname ? 0 : -ENOMEM;
1603 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1604 struct perf_header *ph, int fd,
1605 void *data __maybe_unused)
1607 ph->env.os_release = do_read_string(fd, ph);
1608 return ph->env.os_release ? 0 : -ENOMEM;
1611 static int process_version(struct perf_file_section *section __maybe_unused,
1612 struct perf_header *ph, int fd,
1613 void *data __maybe_unused)
1615 ph->env.version = do_read_string(fd, ph);
1616 return ph->env.version ? 0 : -ENOMEM;
1619 static int process_arch(struct perf_file_section *section __maybe_unused,
1620 struct perf_header *ph, int fd,
1621 void *data __maybe_unused)
1623 ph->env.arch = do_read_string(fd, ph);
1624 return ph->env.arch ? 0 : -ENOMEM;
1627 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1628 struct perf_header *ph, int fd,
1629 void *data __maybe_unused)
1634 ret = readn(fd, &nr, sizeof(nr));
1635 if (ret != sizeof(nr))
1641 ph->env.nr_cpus_avail = nr;
1643 ret = readn(fd, &nr, sizeof(nr));
1644 if (ret != sizeof(nr))
1650 ph->env.nr_cpus_online = nr;
1654 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1655 struct perf_header *ph, int fd,
1656 void *data __maybe_unused)
1658 ph->env.cpu_desc = do_read_string(fd, ph);
1659 return ph->env.cpu_desc ? 0 : -ENOMEM;
1662 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1663 struct perf_header *ph, int fd,
1664 void *data __maybe_unused)
1666 ph->env.cpuid = do_read_string(fd, ph);
1667 return ph->env.cpuid ? 0 : -ENOMEM;
1670 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1671 struct perf_header *ph, int fd,
1672 void *data __maybe_unused)
1677 ret = readn(fd, &mem, sizeof(mem));
1678 if (ret != sizeof(mem))
1682 mem = bswap_64(mem);
1684 ph->env.total_mem = mem;
1688 static struct perf_evsel *
1689 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1691 struct perf_evsel *evsel;
1693 evlist__for_each_entry(evlist, evsel) {
1694 if (evsel->idx == idx)
1702 perf_evlist__set_event_name(struct perf_evlist *evlist,
1703 struct perf_evsel *event)
1705 struct perf_evsel *evsel;
1710 evsel = perf_evlist__find_by_index(evlist, event->idx);
1717 evsel->name = strdup(event->name);
1721 process_event_desc(struct perf_file_section *section __maybe_unused,
1722 struct perf_header *header, int fd,
1723 void *data __maybe_unused)
1725 struct perf_session *session;
1726 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1731 session = container_of(header, struct perf_session, header);
1732 for (evsel = events; evsel->attr.size; evsel++)
1733 perf_evlist__set_event_name(session->evlist, evsel);
1735 free_event_desc(events);
1740 static int process_cmdline(struct perf_file_section *section,
1741 struct perf_header *ph, int fd,
1742 void *data __maybe_unused)
1745 char *str, *cmdline = NULL, **argv = NULL;
1748 ret = readn(fd, &nr, sizeof(nr));
1749 if (ret != sizeof(nr))
1755 ph->env.nr_cmdline = nr;
1757 cmdline = zalloc(section->size + nr + 1);
1761 argv = zalloc(sizeof(char *) * (nr + 1));
1765 for (i = 0; i < nr; i++) {
1766 str = do_read_string(fd, ph);
1770 argv[i] = cmdline + len;
1771 memcpy(argv[i], str, strlen(str) + 1);
1772 len += strlen(str) + 1;
1775 ph->env.cmdline = cmdline;
1776 ph->env.cmdline_argv = (const char **) argv;
1785 static int process_cpu_topology(struct perf_file_section *section,
1786 struct perf_header *ph, int fd,
1787 void *data __maybe_unused)
1793 int cpu_nr = ph->env.nr_cpus_avail;
1796 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1800 ret = readn(fd, &nr, sizeof(nr));
1801 if (ret != sizeof(nr))
1807 ph->env.nr_sibling_cores = nr;
1808 size += sizeof(u32);
1809 if (strbuf_init(&sb, 128) < 0)
1812 for (i = 0; i < nr; i++) {
1813 str = do_read_string(fd, ph);
1817 /* include a NULL character at the end */
1818 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1820 size += string_size(str);
1823 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1825 ret = readn(fd, &nr, sizeof(nr));
1826 if (ret != sizeof(nr))
1832 ph->env.nr_sibling_threads = nr;
1833 size += sizeof(u32);
1835 for (i = 0; i < nr; i++) {
1836 str = do_read_string(fd, ph);
1840 /* include a NULL character at the end */
1841 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1843 size += string_size(str);
1846 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1849 * The header may be from old perf,
1850 * which doesn't include core id and socket id information.
1852 if (section->size <= size) {
1853 zfree(&ph->env.cpu);
1857 for (i = 0; i < (u32)cpu_nr; i++) {
1858 ret = readn(fd, &nr, sizeof(nr));
1859 if (ret != sizeof(nr))
1865 ph->env.cpu[i].core_id = nr;
1867 ret = readn(fd, &nr, sizeof(nr));
1868 if (ret != sizeof(nr))
1874 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
1875 pr_debug("socket_id number is too big."
1876 "You may need to upgrade the perf tool.\n");
1880 ph->env.cpu[i].socket_id = nr;
1886 strbuf_release(&sb);
1888 zfree(&ph->env.cpu);
1892 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1893 struct perf_header *ph, int fd,
1894 void *data __maybe_unused)
1896 struct numa_node *nodes, *n;
1902 ret = readn(fd, &nr, sizeof(nr));
1903 if (ret != sizeof(nr))
1909 nodes = zalloc(sizeof(*nodes) * nr);
1913 for (i = 0; i < nr; i++) {
1917 ret = readn(fd, &n->node, sizeof(u32));
1918 if (ret != sizeof(n->node))
1921 ret = readn(fd, &n->mem_total, sizeof(u64));
1922 if (ret != sizeof(u64))
1925 ret = readn(fd, &n->mem_free, sizeof(u64));
1926 if (ret != sizeof(u64))
1929 if (ph->needs_swap) {
1930 n->node = bswap_32(n->node);
1931 n->mem_total = bswap_64(n->mem_total);
1932 n->mem_free = bswap_64(n->mem_free);
1935 str = do_read_string(fd, ph);
1939 n->map = cpu_map__new(str);
1945 ph->env.nr_numa_nodes = nr;
1946 ph->env.numa_nodes = nodes;
1954 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1955 struct perf_header *ph, int fd,
1956 void *data __maybe_unused)
1964 ret = readn(fd, &pmu_num, sizeof(pmu_num));
1965 if (ret != sizeof(pmu_num))
1969 pmu_num = bswap_32(pmu_num);
1972 pr_debug("pmu mappings not available\n");
1976 ph->env.nr_pmu_mappings = pmu_num;
1977 if (strbuf_init(&sb, 128) < 0)
1981 if (readn(fd, &type, sizeof(type)) != sizeof(type))
1984 type = bswap_32(type);
1986 name = do_read_string(fd, ph);
1990 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1992 /* include a NULL character at the end */
1993 if (strbuf_add(&sb, "", 1) < 0)
1996 if (!strcmp(name, "msr"))
1997 ph->env.msr_pmu_type = type;
2002 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2006 strbuf_release(&sb);
2010 static int process_group_desc(struct perf_file_section *section __maybe_unused,
2011 struct perf_header *ph, int fd,
2012 void *data __maybe_unused)
2015 u32 i, nr, nr_groups;
2016 struct perf_session *session;
2017 struct perf_evsel *evsel, *leader = NULL;
2024 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2028 nr_groups = bswap_32(nr_groups);
2030 ph->env.nr_groups = nr_groups;
2032 pr_debug("group desc not available\n");
2036 desc = calloc(nr_groups, sizeof(*desc));
2040 for (i = 0; i < nr_groups; i++) {
2041 desc[i].name = do_read_string(fd, ph);
2045 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2048 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2051 if (ph->needs_swap) {
2052 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2053 desc[i].nr_members = bswap_32(desc[i].nr_members);
2058 * Rebuild group relationship based on the group_desc
2060 session = container_of(ph, struct perf_session, header);
2061 session->evlist->nr_groups = nr_groups;
2064 evlist__for_each_entry(session->evlist, evsel) {
2065 if (evsel->idx == (int) desc[i].leader_idx) {
2066 evsel->leader = evsel;
2067 /* {anon_group} is a dummy name */
2068 if (strcmp(desc[i].name, "{anon_group}")) {
2069 evsel->group_name = desc[i].name;
2070 desc[i].name = NULL;
2072 evsel->nr_members = desc[i].nr_members;
2074 if (i >= nr_groups || nr > 0) {
2075 pr_debug("invalid group desc\n");
2080 nr = evsel->nr_members - 1;
2083 /* This is a group member */
2084 evsel->leader = leader;
2090 if (i != nr_groups || nr != 0) {
2091 pr_debug("invalid group desc\n");
2097 for (i = 0; i < nr_groups; i++)
2098 zfree(&desc[i].name);
2104 static int process_auxtrace(struct perf_file_section *section,
2105 struct perf_header *ph, int fd,
2106 void *data __maybe_unused)
2108 struct perf_session *session;
2111 session = container_of(ph, struct perf_session, header);
2113 err = auxtrace_index__process(fd, section->size, session,
2116 pr_err("Failed to process auxtrace index\n");
2120 static int process_cache(struct perf_file_section *section __maybe_unused,
2121 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2122 void *data __maybe_unused)
2124 struct cpu_cache_level *caches;
2125 u32 cnt, i, version;
2127 if (readn(fd, &version, sizeof(version)) != sizeof(version))
2131 version = bswap_32(version);
2136 if (readn(fd, &cnt, sizeof(cnt)) != sizeof(cnt))
2140 cnt = bswap_32(cnt);
2142 caches = zalloc(sizeof(*caches) * cnt);
2146 for (i = 0; i < cnt; i++) {
2147 struct cpu_cache_level c;
2150 if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
2151 goto out_free_caches; \
2152 if (ph->needs_swap) \
2153 c.v = bswap_32(c.v); \
2162 c.v = do_read_string(fd, ph); \
2164 goto out_free_caches;
2174 ph->env.caches = caches;
2175 ph->env.caches_cnt = cnt;
2182 struct feature_ops {
2183 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2184 void (*print)(struct perf_header *h, int fd, FILE *fp);
2185 int (*process)(struct perf_file_section *section,
2186 struct perf_header *h, int fd, void *data);
2191 #define FEAT_OPA(n, func) \
2192 [n] = { .name = #n, .write = write_##func, .print = print_##func }
2193 #define FEAT_OPP(n, func) \
2194 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2195 .process = process_##func }
2196 #define FEAT_OPF(n, func) \
2197 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2198 .process = process_##func, .full_only = true }
2200 /* feature_ops not implemented: */
2201 #define print_tracing_data NULL
2202 #define print_build_id NULL
2204 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2205 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
2206 FEAT_OPP(HEADER_BUILD_ID, build_id),
2207 FEAT_OPP(HEADER_HOSTNAME, hostname),
2208 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2209 FEAT_OPP(HEADER_VERSION, version),
2210 FEAT_OPP(HEADER_ARCH, arch),
2211 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2212 FEAT_OPP(HEADER_CPUDESC, cpudesc),
2213 FEAT_OPP(HEADER_CPUID, cpuid),
2214 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
2215 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
2216 FEAT_OPP(HEADER_CMDLINE, cmdline),
2217 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2218 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
2219 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
2220 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
2221 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
2222 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
2223 FEAT_OPA(HEADER_STAT, stat),
2224 FEAT_OPF(HEADER_CACHE, cache),
2227 struct header_print_data {
2229 bool full; /* extended list of headers */
2232 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2233 struct perf_header *ph,
2234 int feat, int fd, void *data)
2236 struct header_print_data *hd = data;
2238 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2239 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2240 "%d, continuing...\n", section->offset, feat);
2243 if (feat >= HEADER_LAST_FEATURE) {
2244 pr_warning("unknown feature %d\n", feat);
2247 if (!feat_ops[feat].print)
2250 if (!feat_ops[feat].full_only || hd->full)
2251 feat_ops[feat].print(ph, fd, hd->fp);
2253 fprintf(hd->fp, "# %s info available, use -I to display\n",
2254 feat_ops[feat].name);
2259 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2261 struct header_print_data hd;
2262 struct perf_header *header = &session->header;
2263 int fd = perf_data_file__fd(session->file);
2270 ret = fstat(fd, &st);
2274 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2276 perf_header__process_sections(header, fd, &hd,
2277 perf_file_section__fprintf_info);
2279 if (session->file->is_pipe)
2282 fprintf(fp, "# missing features: ");
2283 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2285 fprintf(fp, "%s ", feat_ops[bit].name);
2292 static int do_write_feat(int fd, struct perf_header *h, int type,
2293 struct perf_file_section **p,
2294 struct perf_evlist *evlist)
2299 if (perf_header__has_feat(h, type)) {
2300 if (!feat_ops[type].write)
2303 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2305 err = feat_ops[type].write(fd, h, evlist);
2307 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2309 /* undo anything written */
2310 lseek(fd, (*p)->offset, SEEK_SET);
2314 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2320 static int perf_header__adds_write(struct perf_header *header,
2321 struct perf_evlist *evlist, int fd)
2324 struct perf_file_section *feat_sec, *p;
2330 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2334 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2335 if (feat_sec == NULL)
2338 sec_size = sizeof(*feat_sec) * nr_sections;
2340 sec_start = header->feat_offset;
2341 lseek(fd, sec_start + sec_size, SEEK_SET);
2343 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2344 if (do_write_feat(fd, header, feat, &p, evlist))
2345 perf_header__clear_feat(header, feat);
2348 lseek(fd, sec_start, SEEK_SET);
2350 * may write more than needed due to dropped feature, but
2351 * this is okay, reader will skip the mising entries
2353 err = do_write(fd, feat_sec, sec_size);
2355 pr_debug("failed to write feature section\n");
2360 int perf_header__write_pipe(int fd)
2362 struct perf_pipe_file_header f_header;
2365 f_header = (struct perf_pipe_file_header){
2366 .magic = PERF_MAGIC,
2367 .size = sizeof(f_header),
2370 err = do_write(fd, &f_header, sizeof(f_header));
2372 pr_debug("failed to write perf pipe header\n");
2379 int perf_session__write_header(struct perf_session *session,
2380 struct perf_evlist *evlist,
2381 int fd, bool at_exit)
2383 struct perf_file_header f_header;
2384 struct perf_file_attr f_attr;
2385 struct perf_header *header = &session->header;
2386 struct perf_evsel *evsel;
2390 lseek(fd, sizeof(f_header), SEEK_SET);
2392 evlist__for_each_entry(session->evlist, evsel) {
2393 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2394 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2396 pr_debug("failed to write perf header\n");
2401 attr_offset = lseek(fd, 0, SEEK_CUR);
2403 evlist__for_each_entry(evlist, evsel) {
2404 f_attr = (struct perf_file_attr){
2405 .attr = evsel->attr,
2407 .offset = evsel->id_offset,
2408 .size = evsel->ids * sizeof(u64),
2411 err = do_write(fd, &f_attr, sizeof(f_attr));
2413 pr_debug("failed to write perf header attribute\n");
2418 if (!header->data_offset)
2419 header->data_offset = lseek(fd, 0, SEEK_CUR);
2420 header->feat_offset = header->data_offset + header->data_size;
2423 err = perf_header__adds_write(header, evlist, fd);
2428 f_header = (struct perf_file_header){
2429 .magic = PERF_MAGIC,
2430 .size = sizeof(f_header),
2431 .attr_size = sizeof(f_attr),
2433 .offset = attr_offset,
2434 .size = evlist->nr_entries * sizeof(f_attr),
2437 .offset = header->data_offset,
2438 .size = header->data_size,
2440 /* event_types is ignored, store zeros */
2443 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2445 lseek(fd, 0, SEEK_SET);
2446 err = do_write(fd, &f_header, sizeof(f_header));
2448 pr_debug("failed to write perf header\n");
2451 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2456 static int perf_header__getbuffer64(struct perf_header *header,
2457 int fd, void *buf, size_t size)
2459 if (readn(fd, buf, size) <= 0)
2462 if (header->needs_swap)
2463 mem_bswap_64(buf, size);
2468 int perf_header__process_sections(struct perf_header *header, int fd,
2470 int (*process)(struct perf_file_section *section,
2471 struct perf_header *ph,
2472 int feat, int fd, void *data))
2474 struct perf_file_section *feat_sec, *sec;
2480 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2484 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2488 sec_size = sizeof(*feat_sec) * nr_sections;
2490 lseek(fd, header->feat_offset, SEEK_SET);
2492 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2496 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2497 err = process(sec++, header, feat, fd, data);
2507 static const int attr_file_abi_sizes[] = {
2508 [0] = PERF_ATTR_SIZE_VER0,
2509 [1] = PERF_ATTR_SIZE_VER1,
2510 [2] = PERF_ATTR_SIZE_VER2,
2511 [3] = PERF_ATTR_SIZE_VER3,
2512 [4] = PERF_ATTR_SIZE_VER4,
2517 * In the legacy file format, the magic number is not used to encode endianness.
2518 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2519 * on ABI revisions, we need to try all combinations for all endianness to
2520 * detect the endianness.
2522 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2524 uint64_t ref_size, attr_size;
2527 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2528 ref_size = attr_file_abi_sizes[i]
2529 + sizeof(struct perf_file_section);
2530 if (hdr_sz != ref_size) {
2531 attr_size = bswap_64(hdr_sz);
2532 if (attr_size != ref_size)
2535 ph->needs_swap = true;
2537 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2542 /* could not determine endianness */
2546 #define PERF_PIPE_HDR_VER0 16
2548 static const size_t attr_pipe_abi_sizes[] = {
2549 [0] = PERF_PIPE_HDR_VER0,
2554 * In the legacy pipe format, there is an implicit assumption that endiannesss
2555 * between host recording the samples, and host parsing the samples is the
2556 * same. This is not always the case given that the pipe output may always be
2557 * redirected into a file and analyzed on a different machine with possibly a
2558 * different endianness and perf_event ABI revsions in the perf tool itself.
2560 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2565 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2566 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2567 attr_size = bswap_64(hdr_sz);
2568 if (attr_size != hdr_sz)
2571 ph->needs_swap = true;
2573 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2579 bool is_perf_magic(u64 magic)
2581 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2582 || magic == __perf_magic2
2583 || magic == __perf_magic2_sw)
2589 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2590 bool is_pipe, struct perf_header *ph)
2594 /* check for legacy format */
2595 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2597 ph->version = PERF_HEADER_VERSION_1;
2598 pr_debug("legacy perf.data format\n");
2600 return try_all_pipe_abis(hdr_sz, ph);
2602 return try_all_file_abis(hdr_sz, ph);
2605 * the new magic number serves two purposes:
2606 * - unique number to identify actual perf.data files
2607 * - encode endianness of file
2609 ph->version = PERF_HEADER_VERSION_2;
2611 /* check magic number with one endianness */
2612 if (magic == __perf_magic2)
2615 /* check magic number with opposite endianness */
2616 if (magic != __perf_magic2_sw)
2619 ph->needs_swap = true;
2624 int perf_file_header__read(struct perf_file_header *header,
2625 struct perf_header *ph, int fd)
2629 lseek(fd, 0, SEEK_SET);
2631 ret = readn(fd, header, sizeof(*header));
2635 if (check_magic_endian(header->magic,
2636 header->attr_size, false, ph) < 0) {
2637 pr_debug("magic/endian check failed\n");
2641 if (ph->needs_swap) {
2642 mem_bswap_64(header, offsetof(struct perf_file_header,
2646 if (header->size != sizeof(*header)) {
2647 /* Support the previous format */
2648 if (header->size == offsetof(typeof(*header), adds_features))
2649 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2652 } else if (ph->needs_swap) {
2654 * feature bitmap is declared as an array of unsigned longs --
2655 * not good since its size can differ between the host that
2656 * generated the data file and the host analyzing the file.
2658 * We need to handle endianness, but we don't know the size of
2659 * the unsigned long where the file was generated. Take a best
2660 * guess at determining it: try 64-bit swap first (ie., file
2661 * created on a 64-bit host), and check if the hostname feature
2662 * bit is set (this feature bit is forced on as of fbe96f2).
2663 * If the bit is not, undo the 64-bit swap and try a 32-bit
2664 * swap. If the hostname bit is still not set (e.g., older data
2665 * file), punt and fallback to the original behavior --
2666 * clearing all feature bits and setting buildid.
2668 mem_bswap_64(&header->adds_features,
2669 BITS_TO_U64(HEADER_FEAT_BITS));
2671 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2673 mem_bswap_64(&header->adds_features,
2674 BITS_TO_U64(HEADER_FEAT_BITS));
2677 mem_bswap_32(&header->adds_features,
2678 BITS_TO_U32(HEADER_FEAT_BITS));
2681 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2682 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2683 set_bit(HEADER_BUILD_ID, header->adds_features);
2687 memcpy(&ph->adds_features, &header->adds_features,
2688 sizeof(ph->adds_features));
2690 ph->data_offset = header->data.offset;
2691 ph->data_size = header->data.size;
2692 ph->feat_offset = header->data.offset + header->data.size;
2696 static int perf_file_section__process(struct perf_file_section *section,
2697 struct perf_header *ph,
2698 int feat, int fd, void *data)
2700 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2701 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2702 "%d, continuing...\n", section->offset, feat);
2706 if (feat >= HEADER_LAST_FEATURE) {
2707 pr_debug("unknown feature %d, continuing...\n", feat);
2711 if (!feat_ops[feat].process)
2714 return feat_ops[feat].process(section, ph, fd, data);
2717 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2718 struct perf_header *ph, int fd,
2723 ret = readn(fd, header, sizeof(*header));
2727 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2728 pr_debug("endian/magic failed\n");
2733 header->size = bswap_64(header->size);
2735 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2741 static int perf_header__read_pipe(struct perf_session *session)
2743 struct perf_header *header = &session->header;
2744 struct perf_pipe_file_header f_header;
2746 if (perf_file_header__read_pipe(&f_header, header,
2747 perf_data_file__fd(session->file),
2748 session->repipe) < 0) {
2749 pr_debug("incompatible file format\n");
2756 static int read_attr(int fd, struct perf_header *ph,
2757 struct perf_file_attr *f_attr)
2759 struct perf_event_attr *attr = &f_attr->attr;
2761 size_t our_sz = sizeof(f_attr->attr);
2764 memset(f_attr, 0, sizeof(*f_attr));
2766 /* read minimal guaranteed structure */
2767 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2769 pr_debug("cannot read %d bytes of header attr\n",
2770 PERF_ATTR_SIZE_VER0);
2774 /* on file perf_event_attr size */
2782 sz = PERF_ATTR_SIZE_VER0;
2783 } else if (sz > our_sz) {
2784 pr_debug("file uses a more recent and unsupported ABI"
2785 " (%zu bytes extra)\n", sz - our_sz);
2788 /* what we have not yet read and that we know about */
2789 left = sz - PERF_ATTR_SIZE_VER0;
2792 ptr += PERF_ATTR_SIZE_VER0;
2794 ret = readn(fd, ptr, left);
2796 /* read perf_file_section, ids are read in caller */
2797 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2799 return ret <= 0 ? -1 : 0;
2802 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2803 struct pevent *pevent)
2805 struct event_format *event;
2808 /* already prepared */
2809 if (evsel->tp_format)
2812 if (pevent == NULL) {
2813 pr_debug("broken or missing trace data\n");
2817 event = pevent_find_event(pevent, evsel->attr.config);
2818 if (event == NULL) {
2819 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
2824 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2825 evsel->name = strdup(bf);
2826 if (evsel->name == NULL)
2830 evsel->tp_format = event;
2834 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2835 struct pevent *pevent)
2837 struct perf_evsel *pos;
2839 evlist__for_each_entry(evlist, pos) {
2840 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2841 perf_evsel__prepare_tracepoint_event(pos, pevent))
2848 int perf_session__read_header(struct perf_session *session)
2850 struct perf_data_file *file = session->file;
2851 struct perf_header *header = &session->header;
2852 struct perf_file_header f_header;
2853 struct perf_file_attr f_attr;
2855 int nr_attrs, nr_ids, i, j;
2856 int fd = perf_data_file__fd(file);
2858 session->evlist = perf_evlist__new();
2859 if (session->evlist == NULL)
2862 session->evlist->env = &header->env;
2863 session->machines.host.env = &header->env;
2864 if (perf_data_file__is_pipe(file))
2865 return perf_header__read_pipe(session);
2867 if (perf_file_header__read(&f_header, header, fd) < 0)
2871 * Sanity check that perf.data was written cleanly; data size is
2872 * initialized to 0 and updated only if the on_exit function is run.
2873 * If data size is still 0 then the file contains only partial
2874 * information. Just warn user and process it as much as it can.
2876 if (f_header.data.size == 0) {
2877 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2878 "Was the 'perf record' command properly terminated?\n",
2882 nr_attrs = f_header.attrs.size / f_header.attr_size;
2883 lseek(fd, f_header.attrs.offset, SEEK_SET);
2885 for (i = 0; i < nr_attrs; i++) {
2886 struct perf_evsel *evsel;
2889 if (read_attr(fd, header, &f_attr) < 0)
2892 if (header->needs_swap) {
2893 f_attr.ids.size = bswap_64(f_attr.ids.size);
2894 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2895 perf_event__attr_swap(&f_attr.attr);
2898 tmp = lseek(fd, 0, SEEK_CUR);
2899 evsel = perf_evsel__new(&f_attr.attr);
2902 goto out_delete_evlist;
2904 evsel->needs_swap = header->needs_swap;
2906 * Do it before so that if perf_evsel__alloc_id fails, this
2907 * entry gets purged too at perf_evlist__delete().
2909 perf_evlist__add(session->evlist, evsel);
2911 nr_ids = f_attr.ids.size / sizeof(u64);
2913 * We don't have the cpu and thread maps on the header, so
2914 * for allocating the perf_sample_id table we fake 1 cpu and
2915 * hattr->ids threads.
2917 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2918 goto out_delete_evlist;
2920 lseek(fd, f_attr.ids.offset, SEEK_SET);
2922 for (j = 0; j < nr_ids; j++) {
2923 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2926 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2929 lseek(fd, tmp, SEEK_SET);
2932 symbol_conf.nr_events = nr_attrs;
2934 perf_header__process_sections(header, fd, &session->tevent,
2935 perf_file_section__process);
2937 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2938 session->tevent.pevent))
2939 goto out_delete_evlist;
2946 perf_evlist__delete(session->evlist);
2947 session->evlist = NULL;
2951 int perf_event__synthesize_attr(struct perf_tool *tool,
2952 struct perf_event_attr *attr, u32 ids, u64 *id,
2953 perf_event__handler_t process)
2955 union perf_event *ev;
2959 size = sizeof(struct perf_event_attr);
2960 size = PERF_ALIGN(size, sizeof(u64));
2961 size += sizeof(struct perf_event_header);
2962 size += ids * sizeof(u64);
2969 ev->attr.attr = *attr;
2970 memcpy(ev->attr.id, id, ids * sizeof(u64));
2972 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2973 ev->attr.header.size = (u16)size;
2975 if (ev->attr.header.size == size)
2976 err = process(tool, ev, NULL, NULL);
2985 static struct event_update_event *
2986 event_update_event__new(size_t size, u64 type, u64 id)
2988 struct event_update_event *ev;
2990 size += sizeof(*ev);
2991 size = PERF_ALIGN(size, sizeof(u64));
2995 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2996 ev->header.size = (u16)size;
3004 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3005 struct perf_evsel *evsel,
3006 perf_event__handler_t process)
3008 struct event_update_event *ev;
3009 size_t size = strlen(evsel->unit);
3012 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3016 strncpy(ev->data, evsel->unit, size);
3017 err = process(tool, (union perf_event *)ev, NULL, NULL);
3023 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3024 struct perf_evsel *evsel,
3025 perf_event__handler_t process)
3027 struct event_update_event *ev;
3028 struct event_update_event_scale *ev_data;
3031 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3035 ev_data = (struct event_update_event_scale *) ev->data;
3036 ev_data->scale = evsel->scale;
3037 err = process(tool, (union perf_event*) ev, NULL, NULL);
3043 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3044 struct perf_evsel *evsel,
3045 perf_event__handler_t process)
3047 struct event_update_event *ev;
3048 size_t len = strlen(evsel->name);
3051 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3055 strncpy(ev->data, evsel->name, len);
3056 err = process(tool, (union perf_event*) ev, NULL, NULL);
3062 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3063 struct perf_evsel *evsel,
3064 perf_event__handler_t process)
3066 size_t size = sizeof(struct event_update_event);
3067 struct event_update_event *ev;
3071 if (!evsel->own_cpus)
3074 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3078 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3079 ev->header.size = (u16)size;
3080 ev->type = PERF_EVENT_UPDATE__CPUS;
3081 ev->id = evsel->id[0];
3083 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3087 err = process(tool, (union perf_event*) ev, NULL, NULL);
3092 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3094 struct event_update_event *ev = &event->event_update;
3095 struct event_update_event_scale *ev_scale;
3096 struct event_update_event_cpus *ev_cpus;
3097 struct cpu_map *map;
3100 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3103 case PERF_EVENT_UPDATE__SCALE:
3104 ev_scale = (struct event_update_event_scale *) ev->data;
3105 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3107 case PERF_EVENT_UPDATE__UNIT:
3108 ret += fprintf(fp, "... unit: %s\n", ev->data);
3110 case PERF_EVENT_UPDATE__NAME:
3111 ret += fprintf(fp, "... name: %s\n", ev->data);
3113 case PERF_EVENT_UPDATE__CPUS:
3114 ev_cpus = (struct event_update_event_cpus *) ev->data;
3115 ret += fprintf(fp, "... ");
3117 map = cpu_map__new_data(&ev_cpus->cpus);
3119 ret += cpu_map__fprintf(map, fp);
3121 ret += fprintf(fp, "failed to get cpus\n");
3124 ret += fprintf(fp, "... unknown type\n");
3131 int perf_event__synthesize_attrs(struct perf_tool *tool,
3132 struct perf_session *session,
3133 perf_event__handler_t process)
3135 struct perf_evsel *evsel;
3138 evlist__for_each_entry(session->evlist, evsel) {
3139 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3140 evsel->id, process);
3142 pr_debug("failed to create perf header attribute\n");
3150 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3151 union perf_event *event,
3152 struct perf_evlist **pevlist)
3155 struct perf_evsel *evsel;
3156 struct perf_evlist *evlist = *pevlist;
3158 if (evlist == NULL) {
3159 *pevlist = evlist = perf_evlist__new();
3164 evsel = perf_evsel__new(&event->attr.attr);
3168 perf_evlist__add(evlist, evsel);
3170 ids = event->header.size;
3171 ids -= (void *)&event->attr.id - (void *)event;
3172 n_ids = ids / sizeof(u64);
3174 * We don't have the cpu and thread maps on the header, so
3175 * for allocating the perf_sample_id table we fake 1 cpu and
3176 * hattr->ids threads.
3178 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3181 for (i = 0; i < n_ids; i++) {
3182 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3185 symbol_conf.nr_events = evlist->nr_entries;
3190 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3191 union perf_event *event,
3192 struct perf_evlist **pevlist)
3194 struct event_update_event *ev = &event->event_update;
3195 struct event_update_event_scale *ev_scale;
3196 struct event_update_event_cpus *ev_cpus;
3197 struct perf_evlist *evlist;
3198 struct perf_evsel *evsel;
3199 struct cpu_map *map;
3201 if (!pevlist || *pevlist == NULL)
3206 evsel = perf_evlist__id2evsel(evlist, ev->id);
3211 case PERF_EVENT_UPDATE__UNIT:
3212 evsel->unit = strdup(ev->data);
3214 case PERF_EVENT_UPDATE__NAME:
3215 evsel->name = strdup(ev->data);
3217 case PERF_EVENT_UPDATE__SCALE:
3218 ev_scale = (struct event_update_event_scale *) ev->data;
3219 evsel->scale = ev_scale->scale;
3221 case PERF_EVENT_UPDATE__CPUS:
3222 ev_cpus = (struct event_update_event_cpus *) ev->data;
3224 map = cpu_map__new_data(&ev_cpus->cpus);
3226 evsel->own_cpus = map;
3228 pr_err("failed to get event_update cpus\n");
3236 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3237 struct perf_evlist *evlist,
3238 perf_event__handler_t process)
3240 union perf_event ev;
3241 struct tracing_data *tdata;
3242 ssize_t size = 0, aligned_size = 0, padding;
3243 int err __maybe_unused = 0;
3246 * We are going to store the size of the data followed
3247 * by the data contents. Since the fd descriptor is a pipe,
3248 * we cannot seek back to store the size of the data once
3249 * we know it. Instead we:
3251 * - write the tracing data to the temp file
3252 * - get/write the data size to pipe
3253 * - write the tracing data from the temp file
3256 tdata = tracing_data_get(&evlist->entries, fd, true);
3260 memset(&ev, 0, sizeof(ev));
3262 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3264 aligned_size = PERF_ALIGN(size, sizeof(u64));
3265 padding = aligned_size - size;
3266 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3267 ev.tracing_data.size = aligned_size;
3269 process(tool, &ev, NULL, NULL);
3272 * The put function will copy all the tracing data
3273 * stored in temp file to the pipe.
3275 tracing_data_put(tdata);
3277 write_padded(fd, NULL, 0, padding);
3279 return aligned_size;
3282 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3283 union perf_event *event,
3284 struct perf_session *session)
3286 ssize_t size_read, padding, size = event->tracing_data.size;
3287 int fd = perf_data_file__fd(session->file);
3288 off_t offset = lseek(fd, 0, SEEK_CUR);
3291 /* setup for reading amidst mmap */
3292 lseek(fd, offset + sizeof(struct tracing_data_event),
3295 size_read = trace_report(fd, &session->tevent,
3297 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3299 if (readn(fd, buf, padding) < 0) {
3300 pr_err("%s: reading input file", __func__);
3303 if (session->repipe) {
3304 int retw = write(STDOUT_FILENO, buf, padding);
3305 if (retw <= 0 || retw != padding) {
3306 pr_err("%s: repiping tracing data padding", __func__);
3311 if (size_read + padding != size) {
3312 pr_err("%s: tracing data size mismatch", __func__);
3316 perf_evlist__prepare_tracepoint_events(session->evlist,
3317 session->tevent.pevent);
3319 return size_read + padding;
3322 int perf_event__synthesize_build_id(struct perf_tool *tool,
3323 struct dso *pos, u16 misc,
3324 perf_event__handler_t process,
3325 struct machine *machine)
3327 union perf_event ev;
3334 memset(&ev, 0, sizeof(ev));
3336 len = pos->long_name_len + 1;
3337 len = PERF_ALIGN(len, NAME_ALIGN);
3338 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3339 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3340 ev.build_id.header.misc = misc;
3341 ev.build_id.pid = machine->pid;
3342 ev.build_id.header.size = sizeof(ev.build_id) + len;
3343 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3345 err = process(tool, &ev, NULL, machine);
3350 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3351 union perf_event *event,
3352 struct perf_session *session)
3354 __event_process_build_id(&event->build_id,
3355 event->build_id.filename,