1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/compiler.h>
12 #include <linux/list.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/string.h>
16 #include <linux/stringify.h>
17 #include <linux/zalloc.h>
19 #include <sys/utsname.h>
20 #include <linux/time64.h>
22 #ifdef HAVE_LIBBPF_SUPPORT
23 #include <bpf/libbpf.h>
25 #include <perf/cpumap.h>
30 #include "util/evsel_fprintf.h"
33 #include "trace-event.h"
43 #include <api/fs/fs.h>
46 #include "time-utils.h"
48 #include "util/util.h" // perf_exe()
50 #include "bpf-event.h"
51 #include "bpf-utils.h"
53 #include "pmu-hybrid.h"
55 #include <linux/ctype.h>
56 #include <internal/lib.h>
60 * must be a numerical value to let the endianness
61 * determine the memory layout. That way we are able
62 * to detect endianness when reading the perf.data file
65 * we check for legacy (PERFFILE) format.
67 static const char *__perf_magic1 = "PERFFILE";
68 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
69 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
71 #define PERF_MAGIC __perf_magic2
73 const char perf_version_string[] = PERF_VERSION;
75 struct perf_file_attr {
76 struct perf_event_attr attr;
77 struct perf_file_section ids;
80 void perf_header__set_feat(struct perf_header *header, int feat)
82 set_bit(feat, header->adds_features);
85 void perf_header__clear_feat(struct perf_header *header, int feat)
87 clear_bit(feat, header->adds_features);
90 bool perf_header__has_feat(const struct perf_header *header, int feat)
92 return test_bit(feat, header->adds_features);
95 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
97 ssize_t ret = writen(ff->fd, buf, size);
99 if (ret != (ssize_t)size)
100 return ret < 0 ? (int)ret : -1;
104 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
106 /* struct perf_event_header::size is u16 */
107 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
108 size_t new_size = ff->size;
111 if (size + ff->offset > max_size)
114 while (size > (new_size - ff->offset))
116 new_size = min(max_size, new_size);
118 if (ff->size < new_size) {
119 addr = realloc(ff->buf, new_size);
126 memcpy(ff->buf + ff->offset, buf, size);
132 /* Return: 0 if succeeded, -ERR if failed. */
133 int do_write(struct feat_fd *ff, const void *buf, size_t size)
136 return __do_write_fd(ff, buf, size);
137 return __do_write_buf(ff, buf, size);
140 /* Return: 0 if succeeded, -ERR if failed. */
141 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
143 u64 *p = (u64 *) set;
146 ret = do_write(ff, &size, sizeof(size));
150 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
151 ret = do_write(ff, p + i, sizeof(*p));
159 /* Return: 0 if succeeded, -ERR if failed. */
160 int write_padded(struct feat_fd *ff, const void *bf,
161 size_t count, size_t count_aligned)
163 static const char zero_buf[NAME_ALIGN];
164 int err = do_write(ff, bf, count);
167 err = do_write(ff, zero_buf, count_aligned - count);
172 #define string_size(str) \
173 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
175 /* Return: 0 if succeeded, -ERR if failed. */
176 static int do_write_string(struct feat_fd *ff, const char *str)
181 olen = strlen(str) + 1;
182 len = PERF_ALIGN(olen, NAME_ALIGN);
184 /* write len, incl. \0 */
185 ret = do_write(ff, &len, sizeof(len));
189 return write_padded(ff, str, olen, len);
192 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
194 ssize_t ret = readn(ff->fd, addr, size);
197 return ret < 0 ? (int)ret : -1;
201 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
203 if (size > (ssize_t)ff->size - ff->offset)
206 memcpy(addr, ff->buf + ff->offset, size);
213 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
216 return __do_read_fd(ff, addr, size);
217 return __do_read_buf(ff, addr, size);
220 static int do_read_u32(struct feat_fd *ff, u32 *addr)
224 ret = __do_read(ff, addr, sizeof(*addr));
228 if (ff->ph->needs_swap)
229 *addr = bswap_32(*addr);
233 static int do_read_u64(struct feat_fd *ff, u64 *addr)
237 ret = __do_read(ff, addr, sizeof(*addr));
241 if (ff->ph->needs_swap)
242 *addr = bswap_64(*addr);
246 static char *do_read_string(struct feat_fd *ff)
251 if (do_read_u32(ff, &len))
258 if (!__do_read(ff, buf, len)) {
260 * strings are padded by zeroes
261 * thus the actual strlen of buf
262 * may be less than len
271 /* Return: 0 if succeeded, -ERR if failed. */
272 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
278 ret = do_read_u64(ff, &size);
282 set = bitmap_zalloc(size);
288 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
289 ret = do_read_u64(ff, p + i);
301 static int write_tracing_data(struct feat_fd *ff,
302 struct evlist *evlist)
304 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
307 return read_tracing_data(ff->fd, &evlist->core.entries);
310 static int write_build_id(struct feat_fd *ff,
311 struct evlist *evlist __maybe_unused)
313 struct perf_session *session;
316 session = container_of(ff->ph, struct perf_session, header);
318 if (!perf_session__read_build_ids(session, true))
321 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
324 err = perf_session__write_buildid_table(session, ff);
326 pr_debug("failed to write buildid table\n");
329 perf_session__cache_build_ids(session);
334 static int write_hostname(struct feat_fd *ff,
335 struct evlist *evlist __maybe_unused)
344 return do_write_string(ff, uts.nodename);
347 static int write_osrelease(struct feat_fd *ff,
348 struct evlist *evlist __maybe_unused)
357 return do_write_string(ff, uts.release);
360 static int write_arch(struct feat_fd *ff,
361 struct evlist *evlist __maybe_unused)
370 return do_write_string(ff, uts.machine);
373 static int write_version(struct feat_fd *ff,
374 struct evlist *evlist __maybe_unused)
376 return do_write_string(ff, perf_version_string);
379 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
384 const char *search = cpuinfo_proc;
391 file = fopen("/proc/cpuinfo", "r");
395 while (getline(&buf, &len, file) > 0) {
396 ret = strncmp(buf, search, strlen(search));
408 p = strchr(buf, ':');
409 if (p && *(p+1) == ' ' && *(p+2))
415 /* squash extra space characters (branding string) */
420 char *q = skip_spaces(r);
423 while ((*r++ = *q++));
427 ret = do_write_string(ff, s);
434 static int write_cpudesc(struct feat_fd *ff,
435 struct evlist *evlist __maybe_unused)
437 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
438 #define CPUINFO_PROC { "cpu", }
439 #elif defined(__s390__)
440 #define CPUINFO_PROC { "vendor_id", }
441 #elif defined(__sh__)
442 #define CPUINFO_PROC { "cpu type", }
443 #elif defined(__alpha__) || defined(__mips__)
444 #define CPUINFO_PROC { "cpu model", }
445 #elif defined(__arm__)
446 #define CPUINFO_PROC { "model name", "Processor", }
447 #elif defined(__arc__)
448 #define CPUINFO_PROC { "Processor", }
449 #elif defined(__xtensa__)
450 #define CPUINFO_PROC { "core ID", }
452 #define CPUINFO_PROC { "model name", }
454 const char *cpuinfo_procs[] = CPUINFO_PROC;
458 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
460 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
468 static int write_nrcpus(struct feat_fd *ff,
469 struct evlist *evlist __maybe_unused)
475 nrc = cpu__max_present_cpu().cpu;
477 nr = sysconf(_SC_NPROCESSORS_ONLN);
481 nra = (u32)(nr & UINT_MAX);
483 ret = do_write(ff, &nrc, sizeof(nrc));
487 return do_write(ff, &nra, sizeof(nra));
490 static int write_event_desc(struct feat_fd *ff,
491 struct evlist *evlist)
497 nre = evlist->core.nr_entries;
500 * write number of events
502 ret = do_write(ff, &nre, sizeof(nre));
507 * size of perf_event_attr struct
509 sz = (u32)sizeof(evsel->core.attr);
510 ret = do_write(ff, &sz, sizeof(sz));
514 evlist__for_each_entry(evlist, evsel) {
515 ret = do_write(ff, &evsel->core.attr, sz);
519 * write number of unique id per event
520 * there is one id per instance of an event
522 * copy into an nri to be independent of the
525 nri = evsel->core.ids;
526 ret = do_write(ff, &nri, sizeof(nri));
531 * write event string as passed on cmdline
533 ret = do_write_string(ff, evsel__name(evsel));
537 * write unique ids for this event
539 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
546 static int write_cmdline(struct feat_fd *ff,
547 struct evlist *evlist __maybe_unused)
549 char pbuf[MAXPATHLEN], *buf;
552 /* actual path to perf binary */
553 buf = perf_exe(pbuf, MAXPATHLEN);
555 /* account for binary path */
556 n = perf_env.nr_cmdline + 1;
558 ret = do_write(ff, &n, sizeof(n));
562 ret = do_write_string(ff, buf);
566 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
567 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
575 static int write_cpu_topology(struct feat_fd *ff,
576 struct evlist *evlist __maybe_unused)
578 struct cpu_topology *tp;
582 tp = cpu_topology__new();
586 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists));
590 for (i = 0; i < tp->package_cpus_lists; i++) {
591 ret = do_write_string(ff, tp->package_cpus_list[i]);
595 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists));
599 for (i = 0; i < tp->core_cpus_lists; i++) {
600 ret = do_write_string(ff, tp->core_cpus_list[i]);
605 ret = perf_env__read_cpu_topology_map(&perf_env);
609 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
610 ret = do_write(ff, &perf_env.cpu[j].core_id,
611 sizeof(perf_env.cpu[j].core_id));
614 ret = do_write(ff, &perf_env.cpu[j].socket_id,
615 sizeof(perf_env.cpu[j].socket_id));
620 if (!tp->die_cpus_lists)
623 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists));
627 for (i = 0; i < tp->die_cpus_lists; i++) {
628 ret = do_write_string(ff, tp->die_cpus_list[i]);
633 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
634 ret = do_write(ff, &perf_env.cpu[j].die_id,
635 sizeof(perf_env.cpu[j].die_id));
641 cpu_topology__delete(tp);
647 static int write_total_mem(struct feat_fd *ff,
648 struct evlist *evlist __maybe_unused)
656 fp = fopen("/proc/meminfo", "r");
660 while (getline(&buf, &len, fp) > 0) {
661 ret = strncmp(buf, "MemTotal:", 9);
666 n = sscanf(buf, "%*s %"PRIu64, &mem);
668 ret = do_write(ff, &mem, sizeof(mem));
676 static int write_numa_topology(struct feat_fd *ff,
677 struct evlist *evlist __maybe_unused)
679 struct numa_topology *tp;
683 tp = numa_topology__new();
687 ret = do_write(ff, &tp->nr, sizeof(u32));
691 for (i = 0; i < tp->nr; i++) {
692 struct numa_topology_node *n = &tp->nodes[i];
694 ret = do_write(ff, &n->node, sizeof(u32));
698 ret = do_write(ff, &n->mem_total, sizeof(u64));
702 ret = do_write(ff, &n->mem_free, sizeof(u64));
706 ret = do_write_string(ff, n->cpus);
714 numa_topology__delete(tp);
721 * struct pmu_mappings {
730 static int write_pmu_mappings(struct feat_fd *ff,
731 struct evlist *evlist __maybe_unused)
733 struct perf_pmu *pmu = NULL;
738 * Do a first pass to count number of pmu to avoid lseek so this
739 * works in pipe mode as well.
741 while ((pmu = perf_pmu__scan(pmu))) {
747 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
751 while ((pmu = perf_pmu__scan(pmu))) {
755 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
759 ret = do_write_string(ff, pmu->name);
770 * struct group_descs {
772 * struct group_desc {
779 static int write_group_desc(struct feat_fd *ff,
780 struct evlist *evlist)
782 u32 nr_groups = evlist->core.nr_groups;
786 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
790 evlist__for_each_entry(evlist, evsel) {
791 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
792 const char *name = evsel->group_name ?: "{anon_group}";
793 u32 leader_idx = evsel->core.idx;
794 u32 nr_members = evsel->core.nr_members;
796 ret = do_write_string(ff, name);
800 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
804 ret = do_write(ff, &nr_members, sizeof(nr_members));
813 * Return the CPU id as a raw string.
815 * Each architecture should provide a more precise id string that
816 * can be use to match the architecture's "mapfile".
818 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
823 /* Return zero when the cpuid from the mapfile.csv matches the
824 * cpuid string generated on this platform.
825 * Otherwise return non-zero.
827 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
830 regmatch_t pmatch[1];
833 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
834 /* Warn unable to generate match particular string. */
835 pr_info("Invalid regular expression %s\n", mapcpuid);
839 match = !regexec(&re, cpuid, 1, pmatch, 0);
842 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
844 /* Verify the entire string matched. */
845 if (match_len == strlen(cpuid))
852 * default get_cpuid(): nothing gets recorded
853 * actual implementation must be in arch/$(SRCARCH)/util/header.c
855 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
857 return ENOSYS; /* Not implemented */
860 static int write_cpuid(struct feat_fd *ff,
861 struct evlist *evlist __maybe_unused)
866 ret = get_cpuid(buffer, sizeof(buffer));
870 return do_write_string(ff, buffer);
873 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
874 struct evlist *evlist __maybe_unused)
879 static int write_auxtrace(struct feat_fd *ff,
880 struct evlist *evlist __maybe_unused)
882 struct perf_session *session;
885 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
888 session = container_of(ff->ph, struct perf_session, header);
890 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
892 pr_err("Failed to write auxtrace index\n");
896 static int write_clockid(struct feat_fd *ff,
897 struct evlist *evlist __maybe_unused)
899 return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
900 sizeof(ff->ph->env.clock.clockid_res_ns));
903 static int write_clock_data(struct feat_fd *ff,
904 struct evlist *evlist __maybe_unused)
913 ret = do_write(ff, &data32, sizeof(data32));
918 data32 = ff->ph->env.clock.clockid;
920 ret = do_write(ff, &data32, sizeof(data32));
925 data64 = &ff->ph->env.clock.tod_ns;
927 ret = do_write(ff, data64, sizeof(*data64));
931 /* clockid ref time */
932 data64 = &ff->ph->env.clock.clockid_ns;
934 return do_write(ff, data64, sizeof(*data64));
937 static int write_hybrid_topology(struct feat_fd *ff,
938 struct evlist *evlist __maybe_unused)
940 struct hybrid_topology *tp;
944 tp = hybrid_topology__new();
948 ret = do_write(ff, &tp->nr, sizeof(u32));
952 for (i = 0; i < tp->nr; i++) {
953 struct hybrid_topology_node *n = &tp->nodes[i];
955 ret = do_write_string(ff, n->pmu_name);
959 ret = do_write_string(ff, n->cpus);
967 hybrid_topology__delete(tp);
971 static int write_dir_format(struct feat_fd *ff,
972 struct evlist *evlist __maybe_unused)
974 struct perf_session *session;
975 struct perf_data *data;
977 session = container_of(ff->ph, struct perf_session, header);
978 data = session->data;
980 if (WARN_ON(!perf_data__is_dir(data)))
983 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
986 #ifdef HAVE_LIBBPF_SUPPORT
987 static int write_bpf_prog_info(struct feat_fd *ff,
988 struct evlist *evlist __maybe_unused)
990 struct perf_env *env = &ff->ph->env;
991 struct rb_root *root;
992 struct rb_node *next;
995 down_read(&env->bpf_progs.lock);
997 ret = do_write(ff, &env->bpf_progs.infos_cnt,
998 sizeof(env->bpf_progs.infos_cnt));
1002 root = &env->bpf_progs.infos;
1003 next = rb_first(root);
1005 struct bpf_prog_info_node *node;
1008 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1009 next = rb_next(&node->rb_node);
1010 len = sizeof(struct perf_bpil) +
1011 node->info_linear->data_len;
1013 /* before writing to file, translate address to offset */
1014 bpil_addr_to_offs(node->info_linear);
1015 ret = do_write(ff, node->info_linear, len);
1017 * translate back to address even when do_write() fails,
1018 * so that this function never changes the data.
1020 bpil_offs_to_addr(node->info_linear);
1025 up_read(&env->bpf_progs.lock);
1029 static int write_bpf_btf(struct feat_fd *ff,
1030 struct evlist *evlist __maybe_unused)
1032 struct perf_env *env = &ff->ph->env;
1033 struct rb_root *root;
1034 struct rb_node *next;
1037 down_read(&env->bpf_progs.lock);
1039 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1040 sizeof(env->bpf_progs.btfs_cnt));
1045 root = &env->bpf_progs.btfs;
1046 next = rb_first(root);
1048 struct btf_node *node;
1050 node = rb_entry(next, struct btf_node, rb_node);
1051 next = rb_next(&node->rb_node);
1052 ret = do_write(ff, &node->id,
1053 sizeof(u32) * 2 + node->data_size);
1058 up_read(&env->bpf_progs.lock);
1061 #endif // HAVE_LIBBPF_SUPPORT
1063 static int cpu_cache_level__sort(const void *a, const void *b)
1065 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1066 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1068 return cache_a->level - cache_b->level;
1071 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1073 if (a->level != b->level)
1076 if (a->line_size != b->line_size)
1079 if (a->sets != b->sets)
1082 if (a->ways != b->ways)
1085 if (strcmp(a->type, b->type))
1088 if (strcmp(a->size, b->size))
1091 if (strcmp(a->map, b->map))
1097 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1099 char path[PATH_MAX], file[PATH_MAX];
1103 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1104 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1106 if (stat(file, &st))
1109 scnprintf(file, PATH_MAX, "%s/level", path);
1110 if (sysfs__read_int(file, (int *) &cache->level))
1113 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1114 if (sysfs__read_int(file, (int *) &cache->line_size))
1117 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1118 if (sysfs__read_int(file, (int *) &cache->sets))
1121 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1122 if (sysfs__read_int(file, (int *) &cache->ways))
1125 scnprintf(file, PATH_MAX, "%s/type", path);
1126 if (sysfs__read_str(file, &cache->type, &len))
1129 cache->type[len] = 0;
1130 cache->type = strim(cache->type);
1132 scnprintf(file, PATH_MAX, "%s/size", path);
1133 if (sysfs__read_str(file, &cache->size, &len)) {
1134 zfree(&cache->type);
1138 cache->size[len] = 0;
1139 cache->size = strim(cache->size);
1141 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1142 if (sysfs__read_str(file, &cache->map, &len)) {
1143 zfree(&cache->size);
1144 zfree(&cache->type);
1148 cache->map[len] = 0;
1149 cache->map = strim(cache->map);
1153 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1155 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1158 #define MAX_CACHE_LVL 4
1160 static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
1166 nr = cpu__max_cpu().cpu;
1168 for (cpu = 0; cpu < nr; cpu++) {
1169 for (level = 0; level < MAX_CACHE_LVL; level++) {
1170 struct cpu_cache_level c;
1173 err = cpu_cache_level__read(&c, cpu, level);
1180 for (i = 0; i < cnt; i++) {
1181 if (cpu_cache_level__cmp(&c, &caches[i]))
1188 cpu_cache_level__free(&c);
1195 static int write_cache(struct feat_fd *ff,
1196 struct evlist *evlist __maybe_unused)
1198 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL;
1199 struct cpu_cache_level caches[max_caches];
1200 u32 cnt = 0, i, version = 1;
1203 ret = build_caches(caches, &cnt);
1207 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1209 ret = do_write(ff, &version, sizeof(u32));
1213 ret = do_write(ff, &cnt, sizeof(u32));
1217 for (i = 0; i < cnt; i++) {
1218 struct cpu_cache_level *c = &caches[i];
1221 ret = do_write(ff, &c->v, sizeof(u32)); \
1232 ret = do_write_string(ff, (const char *) c->v); \
1243 for (i = 0; i < cnt; i++)
1244 cpu_cache_level__free(&caches[i]);
1248 static int write_stat(struct feat_fd *ff __maybe_unused,
1249 struct evlist *evlist __maybe_unused)
1254 static int write_sample_time(struct feat_fd *ff,
1255 struct evlist *evlist)
1259 ret = do_write(ff, &evlist->first_sample_time,
1260 sizeof(evlist->first_sample_time));
1264 return do_write(ff, &evlist->last_sample_time,
1265 sizeof(evlist->last_sample_time));
1269 static int memory_node__read(struct memory_node *n, unsigned long idx)
1271 unsigned int phys, size = 0;
1272 char path[PATH_MAX];
1276 #define for_each_memory(mem, dir) \
1277 while ((ent = readdir(dir))) \
1278 if (strcmp(ent->d_name, ".") && \
1279 strcmp(ent->d_name, "..") && \
1280 sscanf(ent->d_name, "memory%u", &mem) == 1)
1282 scnprintf(path, PATH_MAX,
1283 "%s/devices/system/node/node%lu",
1284 sysfs__mountpoint(), idx);
1286 dir = opendir(path);
1288 pr_warning("failed: can't open memory sysfs data\n");
1292 for_each_memory(phys, dir) {
1293 size = max(phys, size);
1298 n->set = bitmap_zalloc(size);
1309 for_each_memory(phys, dir) {
1310 set_bit(phys, n->set);
1317 static int memory_node__sort(const void *a, const void *b)
1319 const struct memory_node *na = a;
1320 const struct memory_node *nb = b;
1322 return na->node - nb->node;
1325 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1327 char path[PATH_MAX];
1333 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1334 sysfs__mountpoint());
1336 dir = opendir(path);
1338 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1343 while (!ret && (ent = readdir(dir))) {
1347 if (!strcmp(ent->d_name, ".") ||
1348 !strcmp(ent->d_name, ".."))
1351 r = sscanf(ent->d_name, "node%u", &idx);
1355 if (WARN_ONCE(cnt >= size,
1356 "failed to write MEM_TOPOLOGY, way too many nodes\n")) {
1361 ret = memory_node__read(&nodes[cnt++], idx);
1368 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1373 #define MAX_MEMORY_NODES 2000
1376 * The MEM_TOPOLOGY holds physical memory map for every
1377 * node in system. The format of data is as follows:
1379 * 0 - version | for future changes
1380 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1381 * 16 - count | number of nodes
1383 * For each node we store map of physical indexes for
1386 * 32 - node id | node index
1387 * 40 - size | size of bitmap
1388 * 48 - bitmap | bitmap of memory indexes that belongs to node
1390 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1391 struct evlist *evlist __maybe_unused)
1393 static struct memory_node nodes[MAX_MEMORY_NODES];
1394 u64 bsize, version = 1, i, nr;
1397 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1398 (unsigned long long *) &bsize);
1402 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1406 ret = do_write(ff, &version, sizeof(version));
1410 ret = do_write(ff, &bsize, sizeof(bsize));
1414 ret = do_write(ff, &nr, sizeof(nr));
1418 for (i = 0; i < nr; i++) {
1419 struct memory_node *n = &nodes[i];
1422 ret = do_write(ff, &n->v, sizeof(n->v)); \
1431 ret = do_write_bitmap(ff, n->set, n->size);
1440 static int write_compressed(struct feat_fd *ff __maybe_unused,
1441 struct evlist *evlist __maybe_unused)
1445 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1449 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1453 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1457 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1461 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1464 static int write_per_cpu_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu,
1467 struct perf_pmu_caps *caps = NULL;
1471 nr_caps = perf_pmu__caps_parse(pmu);
1475 ret = do_write(ff, &nr_caps, sizeof(nr_caps));
1479 list_for_each_entry(caps, &pmu->caps, list) {
1480 ret = do_write_string(ff, caps->name);
1484 ret = do_write_string(ff, caps->value);
1490 ret = do_write_string(ff, pmu->name);
1498 static int write_cpu_pmu_caps(struct feat_fd *ff,
1499 struct evlist *evlist __maybe_unused)
1501 struct perf_pmu *cpu_pmu = perf_pmu__find("cpu");
1506 return write_per_cpu_pmu_caps(ff, cpu_pmu, false);
1509 static int write_hybrid_cpu_pmu_caps(struct feat_fd *ff,
1510 struct evlist *evlist __maybe_unused)
1512 struct perf_pmu *pmu;
1513 u32 nr_pmu = perf_pmu__hybrid_pmu_num();
1519 ret = do_write(ff, &nr_pmu, sizeof(nr_pmu));
1523 perf_pmu__for_each_hybrid_pmu(pmu) {
1524 ret = write_per_cpu_pmu_caps(ff, pmu, true);
1532 static void print_hostname(struct feat_fd *ff, FILE *fp)
1534 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1537 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1539 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1542 static void print_arch(struct feat_fd *ff, FILE *fp)
1544 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1547 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1549 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1552 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1554 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1555 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1558 static void print_version(struct feat_fd *ff, FILE *fp)
1560 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1563 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1567 nr = ff->ph->env.nr_cmdline;
1569 fprintf(fp, "# cmdline : ");
1571 for (i = 0; i < nr; i++) {
1572 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1574 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1578 char *quote = strchr(argv_i, '\'');
1582 fprintf(fp, "%s\\\'", argv_i);
1585 fprintf(fp, "%s ", argv_i);
1592 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1594 struct perf_header *ph = ff->ph;
1595 int cpu_nr = ph->env.nr_cpus_avail;
1599 nr = ph->env.nr_sibling_cores;
1600 str = ph->env.sibling_cores;
1602 for (i = 0; i < nr; i++) {
1603 fprintf(fp, "# sibling sockets : %s\n", str);
1604 str += strlen(str) + 1;
1607 if (ph->env.nr_sibling_dies) {
1608 nr = ph->env.nr_sibling_dies;
1609 str = ph->env.sibling_dies;
1611 for (i = 0; i < nr; i++) {
1612 fprintf(fp, "# sibling dies : %s\n", str);
1613 str += strlen(str) + 1;
1617 nr = ph->env.nr_sibling_threads;
1618 str = ph->env.sibling_threads;
1620 for (i = 0; i < nr; i++) {
1621 fprintf(fp, "# sibling threads : %s\n", str);
1622 str += strlen(str) + 1;
1625 if (ph->env.nr_sibling_dies) {
1626 if (ph->env.cpu != NULL) {
1627 for (i = 0; i < cpu_nr; i++)
1628 fprintf(fp, "# CPU %d: Core ID %d, "
1629 "Die ID %d, Socket ID %d\n",
1630 i, ph->env.cpu[i].core_id,
1631 ph->env.cpu[i].die_id,
1632 ph->env.cpu[i].socket_id);
1634 fprintf(fp, "# Core ID, Die ID and Socket ID "
1635 "information is not available\n");
1637 if (ph->env.cpu != NULL) {
1638 for (i = 0; i < cpu_nr; i++)
1639 fprintf(fp, "# CPU %d: Core ID %d, "
1641 i, ph->env.cpu[i].core_id,
1642 ph->env.cpu[i].socket_id);
1644 fprintf(fp, "# Core ID and Socket ID "
1645 "information is not available\n");
1649 static void print_clockid(struct feat_fd *ff, FILE *fp)
1651 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1652 ff->ph->env.clock.clockid_res_ns * 1000);
1655 static void print_clock_data(struct feat_fd *ff, FILE *fp)
1657 struct timespec clockid_ns;
1658 char tstr[64], date[64];
1659 struct timeval tod_ns;
1664 if (!ff->ph->env.clock.enabled) {
1665 fprintf(fp, "# reference time disabled\n");
1669 /* Compute TOD time. */
1670 ref = ff->ph->env.clock.tod_ns;
1671 tod_ns.tv_sec = ref / NSEC_PER_SEC;
1672 ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1673 tod_ns.tv_usec = ref / NSEC_PER_USEC;
1675 /* Compute clockid time. */
1676 ref = ff->ph->env.clock.clockid_ns;
1677 clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1678 ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1679 clockid_ns.tv_nsec = ref;
1681 clockid = ff->ph->env.clock.clockid;
1683 if (localtime_r(&tod_ns.tv_sec, <ime) == NULL)
1684 snprintf(tstr, sizeof(tstr), "<error>");
1686 strftime(date, sizeof(date), "%F %T", <ime);
1687 scnprintf(tstr, sizeof(tstr), "%s.%06d",
1688 date, (int) tod_ns.tv_usec);
1691 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1692 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1693 tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec,
1694 (long) clockid_ns.tv_sec, clockid_ns.tv_nsec,
1695 clockid_name(clockid));
1698 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp)
1701 struct hybrid_node *n;
1703 fprintf(fp, "# hybrid cpu system:\n");
1704 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) {
1705 n = &ff->ph->env.hybrid_nodes[i];
1706 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus);
1710 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1712 struct perf_session *session;
1713 struct perf_data *data;
1715 session = container_of(ff->ph, struct perf_session, header);
1716 data = session->data;
1718 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1721 #ifdef HAVE_LIBBPF_SUPPORT
1722 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1724 struct perf_env *env = &ff->ph->env;
1725 struct rb_root *root;
1726 struct rb_node *next;
1728 down_read(&env->bpf_progs.lock);
1730 root = &env->bpf_progs.infos;
1731 next = rb_first(root);
1734 struct bpf_prog_info_node *node;
1736 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1737 next = rb_next(&node->rb_node);
1739 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1743 up_read(&env->bpf_progs.lock);
1746 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1748 struct perf_env *env = &ff->ph->env;
1749 struct rb_root *root;
1750 struct rb_node *next;
1752 down_read(&env->bpf_progs.lock);
1754 root = &env->bpf_progs.btfs;
1755 next = rb_first(root);
1758 struct btf_node *node;
1760 node = rb_entry(next, struct btf_node, rb_node);
1761 next = rb_next(&node->rb_node);
1762 fprintf(fp, "# btf info of id %u\n", node->id);
1765 up_read(&env->bpf_progs.lock);
1767 #endif // HAVE_LIBBPF_SUPPORT
1769 static void free_event_desc(struct evsel *events)
1771 struct evsel *evsel;
1776 for (evsel = events; evsel->core.attr.size; evsel++) {
1777 zfree(&evsel->name);
1778 zfree(&evsel->core.id);
1784 static bool perf_attr_check(struct perf_event_attr *attr)
1786 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1787 pr_warning("Reserved bits are set unexpectedly. "
1788 "Please update perf tool.\n");
1792 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1793 pr_warning("Unknown sample type (0x%llx) is detected. "
1794 "Please update perf tool.\n",
1799 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1800 pr_warning("Unknown read format (0x%llx) is detected. "
1801 "Please update perf tool.\n",
1806 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1807 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1808 pr_warning("Unknown branch sample type (0x%llx) is detected. "
1809 "Please update perf tool.\n",
1810 attr->branch_sample_type);
1818 static struct evsel *read_event_desc(struct feat_fd *ff)
1820 struct evsel *evsel, *events = NULL;
1823 u32 nre, sz, nr, i, j;
1826 /* number of events */
1827 if (do_read_u32(ff, &nre))
1830 if (do_read_u32(ff, &sz))
1833 /* buffer to hold on file attr struct */
1838 /* the last event terminates with evsel->core.attr.size == 0: */
1839 events = calloc(nre + 1, sizeof(*events));
1843 msz = sizeof(evsel->core.attr);
1847 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1848 evsel->core.idx = i;
1851 * must read entire on-file attr struct to
1852 * sync up with layout.
1854 if (__do_read(ff, buf, sz))
1857 if (ff->ph->needs_swap)
1858 perf_event__attr_swap(buf);
1860 memcpy(&evsel->core.attr, buf, msz);
1862 if (!perf_attr_check(&evsel->core.attr))
1865 if (do_read_u32(ff, &nr))
1868 if (ff->ph->needs_swap)
1869 evsel->needs_swap = true;
1871 evsel->name = do_read_string(ff);
1878 id = calloc(nr, sizeof(*id));
1881 evsel->core.ids = nr;
1882 evsel->core.id = id;
1884 for (j = 0 ; j < nr; j++) {
1885 if (do_read_u64(ff, id))
1894 free_event_desc(events);
1899 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1900 void *priv __maybe_unused)
1902 return fprintf(fp, ", %s = %s", name, val);
1905 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1907 struct evsel *evsel, *events;
1912 events = ff->events;
1914 events = read_event_desc(ff);
1917 fprintf(fp, "# event desc: not available or unable to read\n");
1921 for (evsel = events; evsel->core.attr.size; evsel++) {
1922 fprintf(fp, "# event : name = %s, ", evsel->name);
1924 if (evsel->core.ids) {
1925 fprintf(fp, ", id = {");
1926 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
1929 fprintf(fp, " %"PRIu64, *id);
1934 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
1939 free_event_desc(events);
1943 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1945 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1948 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1951 struct numa_node *n;
1953 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1954 n = &ff->ph->env.numa_nodes[i];
1956 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1957 " free = %"PRIu64" kB\n",
1958 n->node, n->mem_total, n->mem_free);
1960 fprintf(fp, "# node%u cpu list : ", n->node);
1961 cpu_map__fprintf(n->map, fp);
1965 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1967 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1970 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1972 fprintf(fp, "# contains samples with branch stack\n");
1975 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1977 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1980 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1982 fprintf(fp, "# contains stat data\n");
1985 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1989 fprintf(fp, "# CPU cache info:\n");
1990 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1992 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1996 static void print_compressed(struct feat_fd *ff, FILE *fp)
1998 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1999 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
2000 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
2003 static void print_per_cpu_pmu_caps(FILE *fp, int nr_caps, char *cpu_pmu_caps,
2006 const char *delimiter;
2007 char *str, buf[128];
2011 fprintf(fp, "# cpu pmu capabilities: not available\n");
2013 fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name);
2018 scnprintf(buf, sizeof(buf), "# cpu pmu capabilities: ");
2020 scnprintf(buf, sizeof(buf), "# %s pmu capabilities: ", pmu_name);
2026 fprintf(fp, "%s%s", delimiter, str);
2028 str += strlen(str) + 1;
2034 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2036 print_per_cpu_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps,
2037 ff->ph->env.cpu_pmu_caps, NULL);
2040 static void print_hybrid_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2042 struct hybrid_cpc_node *n;
2044 for (int i = 0; i < ff->ph->env.nr_hybrid_cpc_nodes; i++) {
2045 n = &ff->ph->env.hybrid_cpc_nodes[i];
2046 print_per_cpu_pmu_caps(fp, n->nr_cpu_pmu_caps,
2052 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
2054 const char *delimiter = "# pmu mappings: ";
2059 pmu_num = ff->ph->env.nr_pmu_mappings;
2061 fprintf(fp, "# pmu mappings: not available\n");
2065 str = ff->ph->env.pmu_mappings;
2068 type = strtoul(str, &tmp, 0);
2073 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
2076 str += strlen(str) + 1;
2085 fprintf(fp, "# pmu mappings: unable to read\n");
2088 static void print_group_desc(struct feat_fd *ff, FILE *fp)
2090 struct perf_session *session;
2091 struct evsel *evsel;
2094 session = container_of(ff->ph, struct perf_session, header);
2096 evlist__for_each_entry(session->evlist, evsel) {
2097 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
2098 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
2100 nr = evsel->core.nr_members - 1;
2102 fprintf(fp, ",%s", evsel__name(evsel));
2110 static void print_sample_time(struct feat_fd *ff, FILE *fp)
2112 struct perf_session *session;
2116 session = container_of(ff->ph, struct perf_session, header);
2118 timestamp__scnprintf_usec(session->evlist->first_sample_time,
2119 time_buf, sizeof(time_buf));
2120 fprintf(fp, "# time of first sample : %s\n", time_buf);
2122 timestamp__scnprintf_usec(session->evlist->last_sample_time,
2123 time_buf, sizeof(time_buf));
2124 fprintf(fp, "# time of last sample : %s\n", time_buf);
2126 d = (double)(session->evlist->last_sample_time -
2127 session->evlist->first_sample_time) / NSEC_PER_MSEC;
2129 fprintf(fp, "# sample duration : %10.3f ms\n", d);
2132 static void memory_node__fprintf(struct memory_node *n,
2133 unsigned long long bsize, FILE *fp)
2135 char buf_map[100], buf_size[50];
2136 unsigned long long size;
2138 size = bsize * bitmap_weight(n->set, n->size);
2139 unit_number__scnprintf(buf_size, 50, size);
2141 bitmap_scnprintf(n->set, n->size, buf_map, 100);
2142 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2145 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2147 struct memory_node *nodes;
2150 nodes = ff->ph->env.memory_nodes;
2151 nr = ff->ph->env.nr_memory_nodes;
2153 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2154 nr, ff->ph->env.memory_bsize);
2156 for (i = 0; i < nr; i++) {
2157 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2161 static int __event_process_build_id(struct perf_record_header_build_id *bev,
2163 struct perf_session *session)
2166 struct machine *machine;
2169 enum dso_space_type dso_space;
2171 machine = perf_session__findnew_machine(session, bev->pid);
2175 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2178 case PERF_RECORD_MISC_KERNEL:
2179 dso_space = DSO_SPACE__KERNEL;
2181 case PERF_RECORD_MISC_GUEST_KERNEL:
2182 dso_space = DSO_SPACE__KERNEL_GUEST;
2184 case PERF_RECORD_MISC_USER:
2185 case PERF_RECORD_MISC_GUEST_USER:
2186 dso_space = DSO_SPACE__USER;
2192 dso = machine__findnew_dso(machine, filename);
2194 char sbuild_id[SBUILD_ID_SIZE];
2195 struct build_id bid;
2196 size_t size = BUILD_ID_SIZE;
2198 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2201 build_id__init(&bid, bev->data, size);
2202 dso__set_build_id(dso, &bid);
2204 if (dso_space != DSO_SPACE__USER) {
2205 struct kmod_path m = { .name = NULL, };
2207 if (!kmod_path__parse_name(&m, filename) && m.kmod)
2208 dso__set_module_info(dso, &m, machine);
2210 dso->kernel = dso_space;
2214 build_id__sprintf(&dso->bid, sbuild_id);
2215 pr_debug("build id event received for %s: %s [%zu]\n",
2216 dso->long_name, sbuild_id, size);
2225 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2226 int input, u64 offset, u64 size)
2228 struct perf_session *session = container_of(header, struct perf_session, header);
2230 struct perf_event_header header;
2231 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
2234 struct perf_record_header_build_id bev;
2235 char filename[PATH_MAX];
2236 u64 limit = offset + size;
2238 while (offset < limit) {
2241 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
2244 if (header->needs_swap)
2245 perf_event_header__bswap(&old_bev.header);
2247 len = old_bev.header.size - sizeof(old_bev);
2248 if (readn(input, filename, len) != len)
2251 bev.header = old_bev.header;
2254 * As the pid is the missing value, we need to fill
2255 * it properly. The header.misc value give us nice hint.
2257 bev.pid = HOST_KERNEL_ID;
2258 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2259 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2260 bev.pid = DEFAULT_GUEST_KERNEL_ID;
2262 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2263 __event_process_build_id(&bev, filename, session);
2265 offset += bev.header.size;
2271 static int perf_header__read_build_ids(struct perf_header *header,
2272 int input, u64 offset, u64 size)
2274 struct perf_session *session = container_of(header, struct perf_session, header);
2275 struct perf_record_header_build_id bev;
2276 char filename[PATH_MAX];
2277 u64 limit = offset + size, orig_offset = offset;
2280 while (offset < limit) {
2283 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2286 if (header->needs_swap)
2287 perf_event_header__bswap(&bev.header);
2289 len = bev.header.size - sizeof(bev);
2290 if (readn(input, filename, len) != len)
2293 * The a1645ce1 changeset:
2295 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2297 * Added a field to struct perf_record_header_build_id that broke the file
2300 * Since the kernel build-id is the first entry, process the
2301 * table using the old format if the well known
2302 * '[kernel.kallsyms]' string for the kernel build-id has the
2303 * first 4 characters chopped off (where the pid_t sits).
2305 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2306 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2308 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2311 __event_process_build_id(&bev, filename, session);
2313 offset += bev.header.size;
2320 /* Macro for features that simply need to read and store a string. */
2321 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2322 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2324 free(ff->ph->env.__feat_env); \
2325 ff->ph->env.__feat_env = do_read_string(ff); \
2326 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2329 FEAT_PROCESS_STR_FUN(hostname, hostname);
2330 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2331 FEAT_PROCESS_STR_FUN(version, version);
2332 FEAT_PROCESS_STR_FUN(arch, arch);
2333 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2334 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2336 static int process_tracing_data(struct feat_fd *ff, void *data)
2338 ssize_t ret = trace_report(ff->fd, data, false);
2340 return ret < 0 ? -1 : 0;
2343 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2345 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2346 pr_debug("Failed to read buildids, continuing...\n");
2350 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2353 u32 nr_cpus_avail, nr_cpus_online;
2355 ret = do_read_u32(ff, &nr_cpus_avail);
2359 ret = do_read_u32(ff, &nr_cpus_online);
2362 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2363 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2367 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2372 ret = do_read_u64(ff, &total_mem);
2375 ff->ph->env.total_mem = (unsigned long long)total_mem;
2379 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
2381 struct evsel *evsel;
2383 evlist__for_each_entry(evlist, evsel) {
2384 if (evsel->core.idx == idx)
2391 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
2393 struct evsel *evsel;
2398 evsel = evlist__find_by_index(evlist, event->core.idx);
2405 evsel->name = strdup(event->name);
2409 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2411 struct perf_session *session;
2412 struct evsel *evsel, *events = read_event_desc(ff);
2417 session = container_of(ff->ph, struct perf_session, header);
2419 if (session->data->is_pipe) {
2420 /* Save events for reading later by print_event_desc,
2421 * since they can't be read again in pipe mode. */
2422 ff->events = events;
2425 for (evsel = events; evsel->core.attr.size; evsel++)
2426 evlist__set_event_name(session->evlist, evsel);
2428 if (!session->data->is_pipe)
2429 free_event_desc(events);
2434 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2436 char *str, *cmdline = NULL, **argv = NULL;
2439 if (do_read_u32(ff, &nr))
2442 ff->ph->env.nr_cmdline = nr;
2444 cmdline = zalloc(ff->size + nr + 1);
2448 argv = zalloc(sizeof(char *) * (nr + 1));
2452 for (i = 0; i < nr; i++) {
2453 str = do_read_string(ff);
2457 argv[i] = cmdline + len;
2458 memcpy(argv[i], str, strlen(str) + 1);
2459 len += strlen(str) + 1;
2462 ff->ph->env.cmdline = cmdline;
2463 ff->ph->env.cmdline_argv = (const char **) argv;
2472 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2477 int cpu_nr = ff->ph->env.nr_cpus_avail;
2479 struct perf_header *ph = ff->ph;
2480 bool do_core_id_test = true;
2482 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2486 if (do_read_u32(ff, &nr))
2489 ph->env.nr_sibling_cores = nr;
2490 size += sizeof(u32);
2491 if (strbuf_init(&sb, 128) < 0)
2494 for (i = 0; i < nr; i++) {
2495 str = do_read_string(ff);
2499 /* include a NULL character at the end */
2500 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2502 size += string_size(str);
2505 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2507 if (do_read_u32(ff, &nr))
2510 ph->env.nr_sibling_threads = nr;
2511 size += sizeof(u32);
2513 for (i = 0; i < nr; i++) {
2514 str = do_read_string(ff);
2518 /* include a NULL character at the end */
2519 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2521 size += string_size(str);
2524 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2527 * The header may be from old perf,
2528 * which doesn't include core id and socket id information.
2530 if (ff->size <= size) {
2531 zfree(&ph->env.cpu);
2535 /* On s390 the socket_id number is not related to the numbers of cpus.
2536 * The socket_id number might be higher than the numbers of cpus.
2537 * This depends on the configuration.
2538 * AArch64 is the same.
2540 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2541 || !strncmp(ph->env.arch, "aarch64", 7)))
2542 do_core_id_test = false;
2544 for (i = 0; i < (u32)cpu_nr; i++) {
2545 if (do_read_u32(ff, &nr))
2548 ph->env.cpu[i].core_id = nr;
2549 size += sizeof(u32);
2551 if (do_read_u32(ff, &nr))
2554 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2555 pr_debug("socket_id number is too big."
2556 "You may need to upgrade the perf tool.\n");
2560 ph->env.cpu[i].socket_id = nr;
2561 size += sizeof(u32);
2565 * The header may be from old perf,
2566 * which doesn't include die information.
2568 if (ff->size <= size)
2571 if (do_read_u32(ff, &nr))
2574 ph->env.nr_sibling_dies = nr;
2575 size += sizeof(u32);
2577 for (i = 0; i < nr; i++) {
2578 str = do_read_string(ff);
2582 /* include a NULL character at the end */
2583 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2585 size += string_size(str);
2588 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2590 for (i = 0; i < (u32)cpu_nr; i++) {
2591 if (do_read_u32(ff, &nr))
2594 ph->env.cpu[i].die_id = nr;
2600 strbuf_release(&sb);
2602 zfree(&ph->env.cpu);
2606 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2608 struct numa_node *nodes, *n;
2613 if (do_read_u32(ff, &nr))
2616 nodes = zalloc(sizeof(*nodes) * nr);
2620 for (i = 0; i < nr; i++) {
2624 if (do_read_u32(ff, &n->node))
2627 if (do_read_u64(ff, &n->mem_total))
2630 if (do_read_u64(ff, &n->mem_free))
2633 str = do_read_string(ff);
2637 n->map = perf_cpu_map__new(str);
2643 ff->ph->env.nr_numa_nodes = nr;
2644 ff->ph->env.numa_nodes = nodes;
2652 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2659 if (do_read_u32(ff, &pmu_num))
2663 pr_debug("pmu mappings not available\n");
2667 ff->ph->env.nr_pmu_mappings = pmu_num;
2668 if (strbuf_init(&sb, 128) < 0)
2672 if (do_read_u32(ff, &type))
2675 name = do_read_string(ff);
2679 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2681 /* include a NULL character at the end */
2682 if (strbuf_add(&sb, "", 1) < 0)
2685 if (!strcmp(name, "msr"))
2686 ff->ph->env.msr_pmu_type = type;
2691 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2695 strbuf_release(&sb);
2699 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2702 u32 i, nr, nr_groups;
2703 struct perf_session *session;
2704 struct evsel *evsel, *leader = NULL;
2711 if (do_read_u32(ff, &nr_groups))
2714 ff->ph->env.nr_groups = nr_groups;
2716 pr_debug("group desc not available\n");
2720 desc = calloc(nr_groups, sizeof(*desc));
2724 for (i = 0; i < nr_groups; i++) {
2725 desc[i].name = do_read_string(ff);
2729 if (do_read_u32(ff, &desc[i].leader_idx))
2732 if (do_read_u32(ff, &desc[i].nr_members))
2737 * Rebuild group relationship based on the group_desc
2739 session = container_of(ff->ph, struct perf_session, header);
2740 session->evlist->core.nr_groups = nr_groups;
2743 evlist__for_each_entry(session->evlist, evsel) {
2744 if (evsel->core.idx == (int) desc[i].leader_idx) {
2745 evsel__set_leader(evsel, evsel);
2746 /* {anon_group} is a dummy name */
2747 if (strcmp(desc[i].name, "{anon_group}")) {
2748 evsel->group_name = desc[i].name;
2749 desc[i].name = NULL;
2751 evsel->core.nr_members = desc[i].nr_members;
2753 if (i >= nr_groups || nr > 0) {
2754 pr_debug("invalid group desc\n");
2759 nr = evsel->core.nr_members - 1;
2762 /* This is a group member */
2763 evsel__set_leader(evsel, leader);
2769 if (i != nr_groups || nr != 0) {
2770 pr_debug("invalid group desc\n");
2776 for (i = 0; i < nr_groups; i++)
2777 zfree(&desc[i].name);
2783 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2785 struct perf_session *session;
2788 session = container_of(ff->ph, struct perf_session, header);
2790 err = auxtrace_index__process(ff->fd, ff->size, session,
2791 ff->ph->needs_swap);
2793 pr_err("Failed to process auxtrace index\n");
2797 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2799 struct cpu_cache_level *caches;
2800 u32 cnt, i, version;
2802 if (do_read_u32(ff, &version))
2808 if (do_read_u32(ff, &cnt))
2811 caches = zalloc(sizeof(*caches) * cnt);
2815 for (i = 0; i < cnt; i++) {
2816 struct cpu_cache_level c;
2819 if (do_read_u32(ff, &c.v))\
2820 goto out_free_caches; \
2829 c.v = do_read_string(ff); \
2831 goto out_free_caches;
2841 ff->ph->env.caches = caches;
2842 ff->ph->env.caches_cnt = cnt;
2849 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2851 struct perf_session *session;
2852 u64 first_sample_time, last_sample_time;
2855 session = container_of(ff->ph, struct perf_session, header);
2857 ret = do_read_u64(ff, &first_sample_time);
2861 ret = do_read_u64(ff, &last_sample_time);
2865 session->evlist->first_sample_time = first_sample_time;
2866 session->evlist->last_sample_time = last_sample_time;
2870 static int process_mem_topology(struct feat_fd *ff,
2871 void *data __maybe_unused)
2873 struct memory_node *nodes;
2874 u64 version, i, nr, bsize;
2877 if (do_read_u64(ff, &version))
2883 if (do_read_u64(ff, &bsize))
2886 if (do_read_u64(ff, &nr))
2889 nodes = zalloc(sizeof(*nodes) * nr);
2893 for (i = 0; i < nr; i++) {
2894 struct memory_node n;
2897 if (do_read_u64(ff, &n.v)) \
2905 if (do_read_bitmap(ff, &n.set, &n.size))
2911 ff->ph->env.memory_bsize = bsize;
2912 ff->ph->env.memory_nodes = nodes;
2913 ff->ph->env.nr_memory_nodes = nr;
2922 static int process_clockid(struct feat_fd *ff,
2923 void *data __maybe_unused)
2925 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
2931 static int process_clock_data(struct feat_fd *ff,
2932 void *_data __maybe_unused)
2938 if (do_read_u32(ff, &data32))
2945 if (do_read_u32(ff, &data32))
2948 ff->ph->env.clock.clockid = data32;
2951 if (do_read_u64(ff, &data64))
2954 ff->ph->env.clock.tod_ns = data64;
2956 /* clockid ref time */
2957 if (do_read_u64(ff, &data64))
2960 ff->ph->env.clock.clockid_ns = data64;
2961 ff->ph->env.clock.enabled = true;
2965 static int process_hybrid_topology(struct feat_fd *ff,
2966 void *data __maybe_unused)
2968 struct hybrid_node *nodes, *n;
2972 if (do_read_u32(ff, &nr))
2975 nodes = zalloc(sizeof(*nodes) * nr);
2979 for (i = 0; i < nr; i++) {
2982 n->pmu_name = do_read_string(ff);
2986 n->cpus = do_read_string(ff);
2991 ff->ph->env.nr_hybrid_nodes = nr;
2992 ff->ph->env.hybrid_nodes = nodes;
2996 for (i = 0; i < nr; i++) {
2997 free(nodes[i].pmu_name);
2998 free(nodes[i].cpus);
3005 static int process_dir_format(struct feat_fd *ff,
3006 void *_data __maybe_unused)
3008 struct perf_session *session;
3009 struct perf_data *data;
3011 session = container_of(ff->ph, struct perf_session, header);
3012 data = session->data;
3014 if (WARN_ON(!perf_data__is_dir(data)))
3017 return do_read_u64(ff, &data->dir.version);
3020 #ifdef HAVE_LIBBPF_SUPPORT
3021 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
3023 struct bpf_prog_info_node *info_node;
3024 struct perf_env *env = &ff->ph->env;
3025 struct perf_bpil *info_linear;
3029 if (ff->ph->needs_swap) {
3030 pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n");
3034 if (do_read_u32(ff, &count))
3037 down_write(&env->bpf_progs.lock);
3039 for (i = 0; i < count; ++i) {
3040 u32 info_len, data_len;
3044 if (do_read_u32(ff, &info_len))
3046 if (do_read_u32(ff, &data_len))
3049 if (info_len > sizeof(struct bpf_prog_info)) {
3050 pr_warning("detected invalid bpf_prog_info\n");
3054 info_linear = malloc(sizeof(struct perf_bpil) +
3058 info_linear->info_len = sizeof(struct bpf_prog_info);
3059 info_linear->data_len = data_len;
3060 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
3062 if (__do_read(ff, &info_linear->info, info_len))
3064 if (info_len < sizeof(struct bpf_prog_info))
3065 memset(((void *)(&info_linear->info)) + info_len, 0,
3066 sizeof(struct bpf_prog_info) - info_len);
3068 if (__do_read(ff, info_linear->data, data_len))
3071 info_node = malloc(sizeof(struct bpf_prog_info_node));
3075 /* after reading from file, translate offset to address */
3076 bpil_offs_to_addr(info_linear);
3077 info_node->info_linear = info_linear;
3078 perf_env__insert_bpf_prog_info(env, info_node);
3081 up_write(&env->bpf_progs.lock);
3086 up_write(&env->bpf_progs.lock);
3090 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
3092 struct perf_env *env = &ff->ph->env;
3093 struct btf_node *node = NULL;
3097 if (ff->ph->needs_swap) {
3098 pr_warning("interpreting btf from systems with endianness is not yet supported\n");
3102 if (do_read_u32(ff, &count))
3105 down_write(&env->bpf_progs.lock);
3107 for (i = 0; i < count; ++i) {
3110 if (do_read_u32(ff, &id))
3112 if (do_read_u32(ff, &data_size))
3115 node = malloc(sizeof(struct btf_node) + data_size);
3120 node->data_size = data_size;
3122 if (__do_read(ff, node->data, data_size))
3125 perf_env__insert_btf(env, node);
3131 up_write(&env->bpf_progs.lock);
3135 #endif // HAVE_LIBBPF_SUPPORT
3137 static int process_compressed(struct feat_fd *ff,
3138 void *data __maybe_unused)
3140 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
3143 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
3146 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
3149 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
3152 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3158 static int process_per_cpu_pmu_caps(struct feat_fd *ff, int *nr_cpu_pmu_caps,
3159 char **cpu_pmu_caps,
3160 unsigned int *max_branches)
3166 if (do_read_u32(ff, &nr_caps))
3170 pr_debug("cpu pmu capabilities not available\n");
3174 *nr_cpu_pmu_caps = nr_caps;
3176 if (strbuf_init(&sb, 128) < 0)
3180 name = do_read_string(ff);
3184 value = do_read_string(ff);
3188 if (strbuf_addf(&sb, "%s=%s", name, value) < 0)
3191 /* include a NULL character at the end */
3192 if (strbuf_add(&sb, "", 1) < 0)
3195 if (!strcmp(name, "branches"))
3196 *max_branches = atoi(value);
3201 *cpu_pmu_caps = strbuf_detach(&sb, NULL);
3209 strbuf_release(&sb);
3213 static int process_cpu_pmu_caps(struct feat_fd *ff,
3214 void *data __maybe_unused)
3216 return process_per_cpu_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps,
3217 &ff->ph->env.cpu_pmu_caps,
3218 &ff->ph->env.max_branches);
3221 static int process_hybrid_cpu_pmu_caps(struct feat_fd *ff,
3222 void *data __maybe_unused)
3224 struct hybrid_cpc_node *nodes;
3228 if (do_read_u32(ff, &nr_pmu))
3232 pr_debug("hybrid cpu pmu capabilities not available\n");
3236 nodes = zalloc(sizeof(*nodes) * nr_pmu);
3240 for (i = 0; i < nr_pmu; i++) {
3241 struct hybrid_cpc_node *n = &nodes[i];
3243 ret = process_per_cpu_pmu_caps(ff, &n->nr_cpu_pmu_caps,
3249 n->pmu_name = do_read_string(ff);
3256 ff->ph->env.nr_hybrid_cpc_nodes = nr_pmu;
3257 ff->ph->env.hybrid_cpc_nodes = nodes;
3261 for (i = 0; i < nr_pmu; i++) {
3262 free(nodes[i].cpu_pmu_caps);
3263 free(nodes[i].pmu_name);
3270 #define FEAT_OPR(n, func, __full_only) \
3272 .name = __stringify(n), \
3273 .write = write_##func, \
3274 .print = print_##func, \
3275 .full_only = __full_only, \
3276 .process = process_##func, \
3277 .synthesize = true \
3280 #define FEAT_OPN(n, func, __full_only) \
3282 .name = __stringify(n), \
3283 .write = write_##func, \
3284 .print = print_##func, \
3285 .full_only = __full_only, \
3286 .process = process_##func \
3289 /* feature_ops not implemented: */
3290 #define print_tracing_data NULL
3291 #define print_build_id NULL
3293 #define process_branch_stack NULL
3294 #define process_stat NULL
3296 // Only used in util/synthetic-events.c
3297 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
3299 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
3300 FEAT_OPN(TRACING_DATA, tracing_data, false),
3301 FEAT_OPN(BUILD_ID, build_id, false),
3302 FEAT_OPR(HOSTNAME, hostname, false),
3303 FEAT_OPR(OSRELEASE, osrelease, false),
3304 FEAT_OPR(VERSION, version, false),
3305 FEAT_OPR(ARCH, arch, false),
3306 FEAT_OPR(NRCPUS, nrcpus, false),
3307 FEAT_OPR(CPUDESC, cpudesc, false),
3308 FEAT_OPR(CPUID, cpuid, false),
3309 FEAT_OPR(TOTAL_MEM, total_mem, false),
3310 FEAT_OPR(EVENT_DESC, event_desc, false),
3311 FEAT_OPR(CMDLINE, cmdline, false),
3312 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
3313 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
3314 FEAT_OPN(BRANCH_STACK, branch_stack, false),
3315 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
3316 FEAT_OPR(GROUP_DESC, group_desc, false),
3317 FEAT_OPN(AUXTRACE, auxtrace, false),
3318 FEAT_OPN(STAT, stat, false),
3319 FEAT_OPN(CACHE, cache, true),
3320 FEAT_OPR(SAMPLE_TIME, sample_time, false),
3321 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
3322 FEAT_OPR(CLOCKID, clockid, false),
3323 FEAT_OPN(DIR_FORMAT, dir_format, false),
3324 #ifdef HAVE_LIBBPF_SUPPORT
3325 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
3326 FEAT_OPR(BPF_BTF, bpf_btf, false),
3328 FEAT_OPR(COMPRESSED, compressed, false),
3329 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false),
3330 FEAT_OPR(CLOCK_DATA, clock_data, false),
3331 FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true),
3332 FEAT_OPR(HYBRID_CPU_PMU_CAPS, hybrid_cpu_pmu_caps, false),
3335 struct header_print_data {
3337 bool full; /* extended list of headers */
3340 static int perf_file_section__fprintf_info(struct perf_file_section *section,
3341 struct perf_header *ph,
3342 int feat, int fd, void *data)
3344 struct header_print_data *hd = data;
3347 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3348 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3349 "%d, continuing...\n", section->offset, feat);
3352 if (feat >= HEADER_LAST_FEATURE) {
3353 pr_warning("unknown feature %d\n", feat);
3356 if (!feat_ops[feat].print)
3359 ff = (struct feat_fd) {
3364 if (!feat_ops[feat].full_only || hd->full)
3365 feat_ops[feat].print(&ff, hd->fp);
3367 fprintf(hd->fp, "# %s info available, use -I to display\n",
3368 feat_ops[feat].name);
3373 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3375 struct header_print_data hd;
3376 struct perf_header *header = &session->header;
3377 int fd = perf_data__fd(session->data);
3385 ret = fstat(fd, &st);
3389 stctime = st.st_mtime;
3390 fprintf(fp, "# captured on : %s", ctime(&stctime));
3392 fprintf(fp, "# header version : %u\n", header->version);
3393 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
3394 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
3395 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
3397 perf_header__process_sections(header, fd, &hd,
3398 perf_file_section__fprintf_info);
3400 if (session->data->is_pipe)
3403 fprintf(fp, "# missing features: ");
3404 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3406 fprintf(fp, "%s ", feat_ops[bit].name);
3413 static int do_write_feat(struct feat_fd *ff, int type,
3414 struct perf_file_section **p,
3415 struct evlist *evlist)
3420 if (perf_header__has_feat(ff->ph, type)) {
3421 if (!feat_ops[type].write)
3424 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3427 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
3429 err = feat_ops[type].write(ff, evlist);
3431 pr_debug("failed to write feature %s\n", feat_ops[type].name);
3433 /* undo anything written */
3434 lseek(ff->fd, (*p)->offset, SEEK_SET);
3438 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
3444 static int perf_header__adds_write(struct perf_header *header,
3445 struct evlist *evlist, int fd)
3449 struct perf_file_section *feat_sec, *p;
3455 ff = (struct feat_fd){
3460 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3464 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3465 if (feat_sec == NULL)
3468 sec_size = sizeof(*feat_sec) * nr_sections;
3470 sec_start = header->feat_offset;
3471 lseek(fd, sec_start + sec_size, SEEK_SET);
3473 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3474 if (do_write_feat(&ff, feat, &p, evlist))
3475 perf_header__clear_feat(header, feat);
3478 lseek(fd, sec_start, SEEK_SET);
3480 * may write more than needed due to dropped feature, but
3481 * this is okay, reader will skip the missing entries
3483 err = do_write(&ff, feat_sec, sec_size);
3485 pr_debug("failed to write feature section\n");
3490 int perf_header__write_pipe(int fd)
3492 struct perf_pipe_file_header f_header;
3496 ff = (struct feat_fd){ .fd = fd };
3498 f_header = (struct perf_pipe_file_header){
3499 .magic = PERF_MAGIC,
3500 .size = sizeof(f_header),
3503 err = do_write(&ff, &f_header, sizeof(f_header));
3505 pr_debug("failed to write perf pipe header\n");
3512 int perf_session__write_header(struct perf_session *session,
3513 struct evlist *evlist,
3514 int fd, bool at_exit)
3516 struct perf_file_header f_header;
3517 struct perf_file_attr f_attr;
3518 struct perf_header *header = &session->header;
3519 struct evsel *evsel;
3524 ff = (struct feat_fd){ .fd = fd};
3525 lseek(fd, sizeof(f_header), SEEK_SET);
3527 evlist__for_each_entry(session->evlist, evsel) {
3528 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3529 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3531 pr_debug("failed to write perf header\n");
3536 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3538 evlist__for_each_entry(evlist, evsel) {
3539 if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3541 * We are likely in "perf inject" and have read
3542 * from an older file. Update attr size so that
3543 * reader gets the right offset to the ids.
3545 evsel->core.attr.size = sizeof(evsel->core.attr);
3547 f_attr = (struct perf_file_attr){
3548 .attr = evsel->core.attr,
3550 .offset = evsel->id_offset,
3551 .size = evsel->core.ids * sizeof(u64),
3554 err = do_write(&ff, &f_attr, sizeof(f_attr));
3556 pr_debug("failed to write perf header attribute\n");
3561 if (!header->data_offset)
3562 header->data_offset = lseek(fd, 0, SEEK_CUR);
3563 header->feat_offset = header->data_offset + header->data_size;
3566 err = perf_header__adds_write(header, evlist, fd);
3571 f_header = (struct perf_file_header){
3572 .magic = PERF_MAGIC,
3573 .size = sizeof(f_header),
3574 .attr_size = sizeof(f_attr),
3576 .offset = attr_offset,
3577 .size = evlist->core.nr_entries * sizeof(f_attr),
3580 .offset = header->data_offset,
3581 .size = header->data_size,
3583 /* event_types is ignored, store zeros */
3586 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3588 lseek(fd, 0, SEEK_SET);
3589 err = do_write(&ff, &f_header, sizeof(f_header));
3591 pr_debug("failed to write perf header\n");
3594 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3599 static int perf_header__getbuffer64(struct perf_header *header,
3600 int fd, void *buf, size_t size)
3602 if (readn(fd, buf, size) <= 0)
3605 if (header->needs_swap)
3606 mem_bswap_64(buf, size);
3611 int perf_header__process_sections(struct perf_header *header, int fd,
3613 int (*process)(struct perf_file_section *section,
3614 struct perf_header *ph,
3615 int feat, int fd, void *data))
3617 struct perf_file_section *feat_sec, *sec;
3623 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3627 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3631 sec_size = sizeof(*feat_sec) * nr_sections;
3633 lseek(fd, header->feat_offset, SEEK_SET);
3635 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3639 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3640 err = process(sec++, header, feat, fd, data);
3650 static const int attr_file_abi_sizes[] = {
3651 [0] = PERF_ATTR_SIZE_VER0,
3652 [1] = PERF_ATTR_SIZE_VER1,
3653 [2] = PERF_ATTR_SIZE_VER2,
3654 [3] = PERF_ATTR_SIZE_VER3,
3655 [4] = PERF_ATTR_SIZE_VER4,
3660 * In the legacy file format, the magic number is not used to encode endianness.
3661 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3662 * on ABI revisions, we need to try all combinations for all endianness to
3663 * detect the endianness.
3665 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3667 uint64_t ref_size, attr_size;
3670 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3671 ref_size = attr_file_abi_sizes[i]
3672 + sizeof(struct perf_file_section);
3673 if (hdr_sz != ref_size) {
3674 attr_size = bswap_64(hdr_sz);
3675 if (attr_size != ref_size)
3678 ph->needs_swap = true;
3680 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3685 /* could not determine endianness */
3689 #define PERF_PIPE_HDR_VER0 16
3691 static const size_t attr_pipe_abi_sizes[] = {
3692 [0] = PERF_PIPE_HDR_VER0,
3697 * In the legacy pipe format, there is an implicit assumption that endianness
3698 * between host recording the samples, and host parsing the samples is the
3699 * same. This is not always the case given that the pipe output may always be
3700 * redirected into a file and analyzed on a different machine with possibly a
3701 * different endianness and perf_event ABI revisions in the perf tool itself.
3703 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3708 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3709 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3710 attr_size = bswap_64(hdr_sz);
3711 if (attr_size != hdr_sz)
3714 ph->needs_swap = true;
3716 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3722 bool is_perf_magic(u64 magic)
3724 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3725 || magic == __perf_magic2
3726 || magic == __perf_magic2_sw)
3732 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3733 bool is_pipe, struct perf_header *ph)
3737 /* check for legacy format */
3738 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3740 ph->version = PERF_HEADER_VERSION_1;
3741 pr_debug("legacy perf.data format\n");
3743 return try_all_pipe_abis(hdr_sz, ph);
3745 return try_all_file_abis(hdr_sz, ph);
3748 * the new magic number serves two purposes:
3749 * - unique number to identify actual perf.data files
3750 * - encode endianness of file
3752 ph->version = PERF_HEADER_VERSION_2;
3754 /* check magic number with one endianness */
3755 if (magic == __perf_magic2)
3758 /* check magic number with opposite endianness */
3759 if (magic != __perf_magic2_sw)
3762 ph->needs_swap = true;
3767 int perf_file_header__read(struct perf_file_header *header,
3768 struct perf_header *ph, int fd)
3772 lseek(fd, 0, SEEK_SET);
3774 ret = readn(fd, header, sizeof(*header));
3778 if (check_magic_endian(header->magic,
3779 header->attr_size, false, ph) < 0) {
3780 pr_debug("magic/endian check failed\n");
3784 if (ph->needs_swap) {
3785 mem_bswap_64(header, offsetof(struct perf_file_header,
3789 if (header->size != sizeof(*header)) {
3790 /* Support the previous format */
3791 if (header->size == offsetof(typeof(*header), adds_features))
3792 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3795 } else if (ph->needs_swap) {
3797 * feature bitmap is declared as an array of unsigned longs --
3798 * not good since its size can differ between the host that
3799 * generated the data file and the host analyzing the file.
3801 * We need to handle endianness, but we don't know the size of
3802 * the unsigned long where the file was generated. Take a best
3803 * guess at determining it: try 64-bit swap first (ie., file
3804 * created on a 64-bit host), and check if the hostname feature
3805 * bit is set (this feature bit is forced on as of fbe96f2).
3806 * If the bit is not, undo the 64-bit swap and try a 32-bit
3807 * swap. If the hostname bit is still not set (e.g., older data
3808 * file), punt and fallback to the original behavior --
3809 * clearing all feature bits and setting buildid.
3811 mem_bswap_64(&header->adds_features,
3812 BITS_TO_U64(HEADER_FEAT_BITS));
3814 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3816 mem_bswap_64(&header->adds_features,
3817 BITS_TO_U64(HEADER_FEAT_BITS));
3820 mem_bswap_32(&header->adds_features,
3821 BITS_TO_U32(HEADER_FEAT_BITS));
3824 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3825 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3826 set_bit(HEADER_BUILD_ID, header->adds_features);
3830 memcpy(&ph->adds_features, &header->adds_features,
3831 sizeof(ph->adds_features));
3833 ph->data_offset = header->data.offset;
3834 ph->data_size = header->data.size;
3835 ph->feat_offset = header->data.offset + header->data.size;
3839 static int perf_file_section__process(struct perf_file_section *section,
3840 struct perf_header *ph,
3841 int feat, int fd, void *data)
3843 struct feat_fd fdd = {
3846 .size = section->size,
3847 .offset = section->offset,
3850 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3851 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3852 "%d, continuing...\n", section->offset, feat);
3856 if (feat >= HEADER_LAST_FEATURE) {
3857 pr_debug("unknown feature %d, continuing...\n", feat);
3861 if (!feat_ops[feat].process)
3864 return feat_ops[feat].process(&fdd, data);
3867 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3868 struct perf_header *ph,
3869 struct perf_data* data,
3870 bool repipe, int repipe_fd)
3872 struct feat_fd ff = {
3878 ret = perf_data__read(data, header, sizeof(*header));
3882 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3883 pr_debug("endian/magic failed\n");
3888 header->size = bswap_64(header->size);
3890 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3896 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd)
3898 struct perf_header *header = &session->header;
3899 struct perf_pipe_file_header f_header;
3901 if (perf_file_header__read_pipe(&f_header, header, session->data,
3902 session->repipe, repipe_fd) < 0) {
3903 pr_debug("incompatible file format\n");
3907 return f_header.size == sizeof(f_header) ? 0 : -1;
3910 static int read_attr(int fd, struct perf_header *ph,
3911 struct perf_file_attr *f_attr)
3913 struct perf_event_attr *attr = &f_attr->attr;
3915 size_t our_sz = sizeof(f_attr->attr);
3918 memset(f_attr, 0, sizeof(*f_attr));
3920 /* read minimal guaranteed structure */
3921 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3923 pr_debug("cannot read %d bytes of header attr\n",
3924 PERF_ATTR_SIZE_VER0);
3928 /* on file perf_event_attr size */
3936 sz = PERF_ATTR_SIZE_VER0;
3937 } else if (sz > our_sz) {
3938 pr_debug("file uses a more recent and unsupported ABI"
3939 " (%zu bytes extra)\n", sz - our_sz);
3942 /* what we have not yet read and that we know about */
3943 left = sz - PERF_ATTR_SIZE_VER0;
3946 ptr += PERF_ATTR_SIZE_VER0;
3948 ret = readn(fd, ptr, left);
3950 /* read perf_file_section, ids are read in caller */
3951 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3953 return ret <= 0 ? -1 : 0;
3956 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
3958 struct tep_event *event;
3961 /* already prepared */
3962 if (evsel->tp_format)
3965 if (pevent == NULL) {
3966 pr_debug("broken or missing trace data\n");
3970 event = tep_find_event(pevent, evsel->core.attr.config);
3971 if (event == NULL) {
3972 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
3977 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3978 evsel->name = strdup(bf);
3979 if (evsel->name == NULL)
3983 evsel->tp_format = event;
3987 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
3991 evlist__for_each_entry(evlist, pos) {
3992 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
3993 evsel__prepare_tracepoint_event(pos, pevent))
4000 int perf_session__read_header(struct perf_session *session, int repipe_fd)
4002 struct perf_data *data = session->data;
4003 struct perf_header *header = &session->header;
4004 struct perf_file_header f_header;
4005 struct perf_file_attr f_attr;
4007 int nr_attrs, nr_ids, i, j, err;
4008 int fd = perf_data__fd(data);
4010 session->evlist = evlist__new();
4011 if (session->evlist == NULL)
4014 session->evlist->env = &header->env;
4015 session->machines.host.env = &header->env;
4018 * We can read 'pipe' data event from regular file,
4019 * check for the pipe header regardless of source.
4021 err = perf_header__read_pipe(session, repipe_fd);
4022 if (!err || perf_data__is_pipe(data)) {
4023 data->is_pipe = true;
4027 if (perf_file_header__read(&f_header, header, fd) < 0)
4030 if (header->needs_swap && data->in_place_update) {
4031 pr_err("In-place update not supported when byte-swapping is required\n");
4036 * Sanity check that perf.data was written cleanly; data size is
4037 * initialized to 0 and updated only if the on_exit function is run.
4038 * If data size is still 0 then the file contains only partial
4039 * information. Just warn user and process it as much as it can.
4041 if (f_header.data.size == 0) {
4042 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
4043 "Was the 'perf record' command properly terminated?\n",
4047 if (f_header.attr_size == 0) {
4048 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
4049 "Was the 'perf record' command properly terminated?\n",
4054 nr_attrs = f_header.attrs.size / f_header.attr_size;
4055 lseek(fd, f_header.attrs.offset, SEEK_SET);
4057 for (i = 0; i < nr_attrs; i++) {
4058 struct evsel *evsel;
4061 if (read_attr(fd, header, &f_attr) < 0)
4064 if (header->needs_swap) {
4065 f_attr.ids.size = bswap_64(f_attr.ids.size);
4066 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
4067 perf_event__attr_swap(&f_attr.attr);
4070 tmp = lseek(fd, 0, SEEK_CUR);
4071 evsel = evsel__new(&f_attr.attr);
4074 goto out_delete_evlist;
4076 evsel->needs_swap = header->needs_swap;
4078 * Do it before so that if perf_evsel__alloc_id fails, this
4079 * entry gets purged too at evlist__delete().
4081 evlist__add(session->evlist, evsel);
4083 nr_ids = f_attr.ids.size / sizeof(u64);
4085 * We don't have the cpu and thread maps on the header, so
4086 * for allocating the perf_sample_id table we fake 1 cpu and
4087 * hattr->ids threads.
4089 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
4090 goto out_delete_evlist;
4092 lseek(fd, f_attr.ids.offset, SEEK_SET);
4094 for (j = 0; j < nr_ids; j++) {
4095 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
4098 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
4101 lseek(fd, tmp, SEEK_SET);
4104 perf_header__process_sections(header, fd, &session->tevent,
4105 perf_file_section__process);
4107 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
4108 goto out_delete_evlist;
4115 evlist__delete(session->evlist);
4116 session->evlist = NULL;
4120 int perf_event__process_feature(struct perf_session *session,
4121 union perf_event *event)
4123 struct perf_tool *tool = session->tool;
4124 struct feat_fd ff = { .fd = 0 };
4125 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
4126 int type = fe->header.type;
4127 u64 feat = fe->feat_id;
4130 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
4131 pr_warning("invalid record type %d in pipe-mode\n", type);
4134 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
4135 pr_warning("invalid record type %d in pipe-mode\n", type);
4139 if (!feat_ops[feat].process)
4142 ff.buf = (void *)fe->data;
4143 ff.size = event->header.size - sizeof(*fe);
4144 ff.ph = &session->header;
4146 if (feat_ops[feat].process(&ff, NULL)) {
4151 if (!feat_ops[feat].print || !tool->show_feat_hdr)
4154 if (!feat_ops[feat].full_only ||
4155 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
4156 feat_ops[feat].print(&ff, stdout);
4158 fprintf(stdout, "# %s info available, use -I to display\n",
4159 feat_ops[feat].name);
4162 free_event_desc(ff.events);
4166 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
4168 struct perf_record_event_update *ev = &event->event_update;
4169 struct perf_record_event_update_scale *ev_scale;
4170 struct perf_record_event_update_cpus *ev_cpus;
4171 struct perf_cpu_map *map;
4174 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
4177 case PERF_EVENT_UPDATE__SCALE:
4178 ev_scale = (struct perf_record_event_update_scale *)ev->data;
4179 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
4181 case PERF_EVENT_UPDATE__UNIT:
4182 ret += fprintf(fp, "... unit: %s\n", ev->data);
4184 case PERF_EVENT_UPDATE__NAME:
4185 ret += fprintf(fp, "... name: %s\n", ev->data);
4187 case PERF_EVENT_UPDATE__CPUS:
4188 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
4189 ret += fprintf(fp, "... ");
4191 map = cpu_map__new_data(&ev_cpus->cpus);
4193 ret += cpu_map__fprintf(map, fp);
4195 ret += fprintf(fp, "failed to get cpus\n");
4198 ret += fprintf(fp, "... unknown type\n");
4205 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4206 union perf_event *event,
4207 struct evlist **pevlist)
4210 struct evsel *evsel;
4211 struct evlist *evlist = *pevlist;
4213 if (evlist == NULL) {
4214 *pevlist = evlist = evlist__new();
4219 evsel = evsel__new(&event->attr.attr);
4223 evlist__add(evlist, evsel);
4225 ids = event->header.size;
4226 ids -= (void *)&event->attr.id - (void *)event;
4227 n_ids = ids / sizeof(u64);
4229 * We don't have the cpu and thread maps on the header, so
4230 * for allocating the perf_sample_id table we fake 1 cpu and
4231 * hattr->ids threads.
4233 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
4236 for (i = 0; i < n_ids; i++) {
4237 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
4243 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4244 union perf_event *event,
4245 struct evlist **pevlist)
4247 struct perf_record_event_update *ev = &event->event_update;
4248 struct perf_record_event_update_scale *ev_scale;
4249 struct perf_record_event_update_cpus *ev_cpus;
4250 struct evlist *evlist;
4251 struct evsel *evsel;
4252 struct perf_cpu_map *map;
4254 if (!pevlist || *pevlist == NULL)
4259 evsel = evlist__id2evsel(evlist, ev->id);
4264 case PERF_EVENT_UPDATE__UNIT:
4265 free((char *)evsel->unit);
4266 evsel->unit = strdup(ev->data);
4268 case PERF_EVENT_UPDATE__NAME:
4270 evsel->name = strdup(ev->data);
4272 case PERF_EVENT_UPDATE__SCALE:
4273 ev_scale = (struct perf_record_event_update_scale *)ev->data;
4274 evsel->scale = ev_scale->scale;
4276 case PERF_EVENT_UPDATE__CPUS:
4277 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
4278 map = cpu_map__new_data(&ev_cpus->cpus);
4280 perf_cpu_map__put(evsel->core.own_cpus);
4281 evsel->core.own_cpus = map;
4283 pr_err("failed to get event_update cpus\n");
4291 int perf_event__process_tracing_data(struct perf_session *session,
4292 union perf_event *event)
4294 ssize_t size_read, padding, size = event->tracing_data.size;
4295 int fd = perf_data__fd(session->data);
4299 * The pipe fd is already in proper place and in any case
4300 * we can't move it, and we'd screw the case where we read
4301 * 'pipe' data from regular file. The trace_report reads
4302 * data from 'fd' so we need to set it directly behind the
4303 * event, where the tracing data starts.
4305 if (!perf_data__is_pipe(session->data)) {
4306 off_t offset = lseek(fd, 0, SEEK_CUR);
4308 /* setup for reading amidst mmap */
4309 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4313 size_read = trace_report(fd, &session->tevent,
4315 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4317 if (readn(fd, buf, padding) < 0) {
4318 pr_err("%s: reading input file", __func__);
4321 if (session->repipe) {
4322 int retw = write(STDOUT_FILENO, buf, padding);
4323 if (retw <= 0 || retw != padding) {
4324 pr_err("%s: repiping tracing data padding", __func__);
4329 if (size_read + padding != size) {
4330 pr_err("%s: tracing data size mismatch", __func__);
4334 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
4336 return size_read + padding;
4339 int perf_event__process_build_id(struct perf_session *session,
4340 union perf_event *event)
4342 __event_process_build_id(&event->build_id,
4343 event->build_id.filename,