Revert "perf augmented_syscalls: Drop 'write', 'poll' for testing without self pid...
[linux/fpc-iii.git] / tools / perf / util / header.c
blob4fd45be95a433e32ac8839dd6f9b402be6d34b00
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include "util.h"
5 #include "string2.h"
6 #include <sys/param.h>
7 #include <sys/types.h>
8 #include <byteswap.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <linux/compiler.h>
13 #include <linux/list.h>
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/stringify.h>
17 #include <sys/stat.h>
18 #include <sys/utsname.h>
19 #include <linux/time64.h>
20 #include <dirent.h>
22 #include "evlist.h"
23 #include "evsel.h"
24 #include "header.h"
25 #include "memswap.h"
26 #include "../perf.h"
27 #include "trace-event.h"
28 #include "session.h"
29 #include "symbol.h"
30 #include "debug.h"
31 #include "cpumap.h"
32 #include "pmu.h"
33 #include "vdso.h"
34 #include "strbuf.h"
35 #include "build-id.h"
36 #include "data.h"
37 #include <api/fs/fs.h>
38 #include "asm/bug.h"
39 #include "tool.h"
40 #include "time-utils.h"
41 #include "units.h"
43 #include "sane_ctype.h"
46 * magic2 = "PERFILE2"
47 * must be a numerical value to let the endianness
48 * determine the memory layout. That way we are able
49 * to detect endianness when reading the perf.data file
50 * back.
52 * we check for legacy (PERFFILE) format.
54 static const char *__perf_magic1 = "PERFFILE";
55 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
56 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
58 #define PERF_MAGIC __perf_magic2
60 const char perf_version_string[] = PERF_VERSION;
62 struct perf_file_attr {
63 struct perf_event_attr attr;
64 struct perf_file_section ids;
67 struct feat_fd {
68 struct perf_header *ph;
69 int fd;
70 void *buf; /* Either buf != NULL or fd >= 0 */
71 ssize_t offset;
72 size_t size;
73 struct perf_evsel *events;
76 void perf_header__set_feat(struct perf_header *header, int feat)
78 set_bit(feat, header->adds_features);
81 void perf_header__clear_feat(struct perf_header *header, int feat)
83 clear_bit(feat, header->adds_features);
86 bool perf_header__has_feat(const struct perf_header *header, int feat)
88 return test_bit(feat, header->adds_features);
91 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
93 ssize_t ret = writen(ff->fd, buf, size);
95 if (ret != (ssize_t)size)
96 return ret < 0 ? (int)ret : -1;
97 return 0;
100 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
102 /* struct perf_event_header::size is u16 */
103 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
104 size_t new_size = ff->size;
105 void *addr;
107 if (size + ff->offset > max_size)
108 return -E2BIG;
110 while (size > (new_size - ff->offset))
111 new_size <<= 1;
112 new_size = min(max_size, new_size);
114 if (ff->size < new_size) {
115 addr = realloc(ff->buf, new_size);
116 if (!addr)
117 return -ENOMEM;
118 ff->buf = addr;
119 ff->size = new_size;
122 memcpy(ff->buf + ff->offset, buf, size);
123 ff->offset += size;
125 return 0;
128 /* Return: 0 if succeded, -ERR if failed. */
129 int do_write(struct feat_fd *ff, const void *buf, size_t size)
131 if (!ff->buf)
132 return __do_write_fd(ff, buf, size);
133 return __do_write_buf(ff, buf, size);
136 /* Return: 0 if succeded, -ERR if failed. */
137 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
139 u64 *p = (u64 *) set;
140 int i, ret;
142 ret = do_write(ff, &size, sizeof(size));
143 if (ret < 0)
144 return ret;
146 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
147 ret = do_write(ff, p + i, sizeof(*p));
148 if (ret < 0)
149 return ret;
152 return 0;
155 /* Return: 0 if succeded, -ERR if failed. */
156 int write_padded(struct feat_fd *ff, const void *bf,
157 size_t count, size_t count_aligned)
159 static const char zero_buf[NAME_ALIGN];
160 int err = do_write(ff, bf, count);
162 if (!err)
163 err = do_write(ff, zero_buf, count_aligned - count);
165 return err;
168 #define string_size(str) \
169 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
171 /* Return: 0 if succeded, -ERR if failed. */
172 static int do_write_string(struct feat_fd *ff, const char *str)
174 u32 len, olen;
175 int ret;
177 olen = strlen(str) + 1;
178 len = PERF_ALIGN(olen, NAME_ALIGN);
180 /* write len, incl. \0 */
181 ret = do_write(ff, &len, sizeof(len));
182 if (ret < 0)
183 return ret;
185 return write_padded(ff, str, olen, len);
188 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
190 ssize_t ret = readn(ff->fd, addr, size);
192 if (ret != size)
193 return ret < 0 ? (int)ret : -1;
194 return 0;
197 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
199 if (size > (ssize_t)ff->size - ff->offset)
200 return -1;
202 memcpy(addr, ff->buf + ff->offset, size);
203 ff->offset += size;
205 return 0;
209 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
211 if (!ff->buf)
212 return __do_read_fd(ff, addr, size);
213 return __do_read_buf(ff, addr, size);
216 static int do_read_u32(struct feat_fd *ff, u32 *addr)
218 int ret;
220 ret = __do_read(ff, addr, sizeof(*addr));
221 if (ret)
222 return ret;
224 if (ff->ph->needs_swap)
225 *addr = bswap_32(*addr);
226 return 0;
229 static int do_read_u64(struct feat_fd *ff, u64 *addr)
231 int ret;
233 ret = __do_read(ff, addr, sizeof(*addr));
234 if (ret)
235 return ret;
237 if (ff->ph->needs_swap)
238 *addr = bswap_64(*addr);
239 return 0;
242 static char *do_read_string(struct feat_fd *ff)
244 u32 len;
245 char *buf;
247 if (do_read_u32(ff, &len))
248 return NULL;
250 buf = malloc(len);
251 if (!buf)
252 return NULL;
254 if (!__do_read(ff, buf, len)) {
256 * strings are padded by zeroes
257 * thus the actual strlen of buf
258 * may be less than len
260 return buf;
263 free(buf);
264 return NULL;
267 /* Return: 0 if succeded, -ERR if failed. */
268 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
270 unsigned long *set;
271 u64 size, *p;
272 int i, ret;
274 ret = do_read_u64(ff, &size);
275 if (ret)
276 return ret;
278 set = bitmap_alloc(size);
279 if (!set)
280 return -ENOMEM;
282 p = (u64 *) set;
284 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
285 ret = do_read_u64(ff, p + i);
286 if (ret < 0) {
287 free(set);
288 return ret;
292 *pset = set;
293 *psize = size;
294 return 0;
297 static int write_tracing_data(struct feat_fd *ff,
298 struct perf_evlist *evlist)
300 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
301 return -1;
303 return read_tracing_data(ff->fd, &evlist->entries);
306 static int write_build_id(struct feat_fd *ff,
307 struct perf_evlist *evlist __maybe_unused)
309 struct perf_session *session;
310 int err;
312 session = container_of(ff->ph, struct perf_session, header);
314 if (!perf_session__read_build_ids(session, true))
315 return -1;
317 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
318 return -1;
320 err = perf_session__write_buildid_table(session, ff);
321 if (err < 0) {
322 pr_debug("failed to write buildid table\n");
323 return err;
325 perf_session__cache_build_ids(session);
327 return 0;
330 static int write_hostname(struct feat_fd *ff,
331 struct perf_evlist *evlist __maybe_unused)
333 struct utsname uts;
334 int ret;
336 ret = uname(&uts);
337 if (ret < 0)
338 return -1;
340 return do_write_string(ff, uts.nodename);
343 static int write_osrelease(struct feat_fd *ff,
344 struct perf_evlist *evlist __maybe_unused)
346 struct utsname uts;
347 int ret;
349 ret = uname(&uts);
350 if (ret < 0)
351 return -1;
353 return do_write_string(ff, uts.release);
356 static int write_arch(struct feat_fd *ff,
357 struct perf_evlist *evlist __maybe_unused)
359 struct utsname uts;
360 int ret;
362 ret = uname(&uts);
363 if (ret < 0)
364 return -1;
366 return do_write_string(ff, uts.machine);
369 static int write_version(struct feat_fd *ff,
370 struct perf_evlist *evlist __maybe_unused)
372 return do_write_string(ff, perf_version_string);
375 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
377 FILE *file;
378 char *buf = NULL;
379 char *s, *p;
380 const char *search = cpuinfo_proc;
381 size_t len = 0;
382 int ret = -1;
384 if (!search)
385 return -1;
387 file = fopen("/proc/cpuinfo", "r");
388 if (!file)
389 return -1;
391 while (getline(&buf, &len, file) > 0) {
392 ret = strncmp(buf, search, strlen(search));
393 if (!ret)
394 break;
397 if (ret) {
398 ret = -1;
399 goto done;
402 s = buf;
404 p = strchr(buf, ':');
405 if (p && *(p+1) == ' ' && *(p+2))
406 s = p + 2;
407 p = strchr(s, '\n');
408 if (p)
409 *p = '\0';
411 /* squash extra space characters (branding string) */
412 p = s;
413 while (*p) {
414 if (isspace(*p)) {
415 char *r = p + 1;
416 char *q = r;
417 *p = ' ';
418 while (*q && isspace(*q))
419 q++;
420 if (q != (p+1))
421 while ((*r++ = *q++));
423 p++;
425 ret = do_write_string(ff, s);
426 done:
427 free(buf);
428 fclose(file);
429 return ret;
432 static int write_cpudesc(struct feat_fd *ff,
433 struct perf_evlist *evlist __maybe_unused)
435 const char *cpuinfo_procs[] = CPUINFO_PROC;
436 unsigned int i;
438 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
439 int ret;
440 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
441 if (ret >= 0)
442 return ret;
444 return -1;
448 static int write_nrcpus(struct feat_fd *ff,
449 struct perf_evlist *evlist __maybe_unused)
451 long nr;
452 u32 nrc, nra;
453 int ret;
455 nrc = cpu__max_present_cpu();
457 nr = sysconf(_SC_NPROCESSORS_ONLN);
458 if (nr < 0)
459 return -1;
461 nra = (u32)(nr & UINT_MAX);
463 ret = do_write(ff, &nrc, sizeof(nrc));
464 if (ret < 0)
465 return ret;
467 return do_write(ff, &nra, sizeof(nra));
470 static int write_event_desc(struct feat_fd *ff,
471 struct perf_evlist *evlist)
473 struct perf_evsel *evsel;
474 u32 nre, nri, sz;
475 int ret;
477 nre = evlist->nr_entries;
480 * write number of events
482 ret = do_write(ff, &nre, sizeof(nre));
483 if (ret < 0)
484 return ret;
487 * size of perf_event_attr struct
489 sz = (u32)sizeof(evsel->attr);
490 ret = do_write(ff, &sz, sizeof(sz));
491 if (ret < 0)
492 return ret;
494 evlist__for_each_entry(evlist, evsel) {
495 ret = do_write(ff, &evsel->attr, sz);
496 if (ret < 0)
497 return ret;
499 * write number of unique id per event
500 * there is one id per instance of an event
502 * copy into an nri to be independent of the
503 * type of ids,
505 nri = evsel->ids;
506 ret = do_write(ff, &nri, sizeof(nri));
507 if (ret < 0)
508 return ret;
511 * write event string as passed on cmdline
513 ret = do_write_string(ff, perf_evsel__name(evsel));
514 if (ret < 0)
515 return ret;
517 * write unique ids for this event
519 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
520 if (ret < 0)
521 return ret;
523 return 0;
526 static int write_cmdline(struct feat_fd *ff,
527 struct perf_evlist *evlist __maybe_unused)
529 char buf[MAXPATHLEN];
530 u32 n;
531 int i, ret;
533 /* actual path to perf binary */
534 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
535 if (ret <= 0)
536 return -1;
538 /* readlink() does not add null termination */
539 buf[ret] = '\0';
541 /* account for binary path */
542 n = perf_env.nr_cmdline + 1;
544 ret = do_write(ff, &n, sizeof(n));
545 if (ret < 0)
546 return ret;
548 ret = do_write_string(ff, buf);
549 if (ret < 0)
550 return ret;
552 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
553 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
554 if (ret < 0)
555 return ret;
557 return 0;
560 #define CORE_SIB_FMT \
561 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
562 #define THRD_SIB_FMT \
563 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
565 struct cpu_topo {
566 u32 cpu_nr;
567 u32 core_sib;
568 u32 thread_sib;
569 char **core_siblings;
570 char **thread_siblings;
573 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
575 FILE *fp;
576 char filename[MAXPATHLEN];
577 char *buf = NULL, *p;
578 size_t len = 0;
579 ssize_t sret;
580 u32 i = 0;
581 int ret = -1;
583 sprintf(filename, CORE_SIB_FMT, cpu);
584 fp = fopen(filename, "r");
585 if (!fp)
586 goto try_threads;
588 sret = getline(&buf, &len, fp);
589 fclose(fp);
590 if (sret <= 0)
591 goto try_threads;
593 p = strchr(buf, '\n');
594 if (p)
595 *p = '\0';
597 for (i = 0; i < tp->core_sib; i++) {
598 if (!strcmp(buf, tp->core_siblings[i]))
599 break;
601 if (i == tp->core_sib) {
602 tp->core_siblings[i] = buf;
603 tp->core_sib++;
604 buf = NULL;
605 len = 0;
607 ret = 0;
609 try_threads:
610 sprintf(filename, THRD_SIB_FMT, cpu);
611 fp = fopen(filename, "r");
612 if (!fp)
613 goto done;
615 if (getline(&buf, &len, fp) <= 0)
616 goto done;
618 p = strchr(buf, '\n');
619 if (p)
620 *p = '\0';
622 for (i = 0; i < tp->thread_sib; i++) {
623 if (!strcmp(buf, tp->thread_siblings[i]))
624 break;
626 if (i == tp->thread_sib) {
627 tp->thread_siblings[i] = buf;
628 tp->thread_sib++;
629 buf = NULL;
631 ret = 0;
632 done:
633 if(fp)
634 fclose(fp);
635 free(buf);
636 return ret;
639 static void free_cpu_topo(struct cpu_topo *tp)
641 u32 i;
643 if (!tp)
644 return;
646 for (i = 0 ; i < tp->core_sib; i++)
647 zfree(&tp->core_siblings[i]);
649 for (i = 0 ; i < tp->thread_sib; i++)
650 zfree(&tp->thread_siblings[i]);
652 free(tp);
655 static struct cpu_topo *build_cpu_topology(void)
657 struct cpu_topo *tp = NULL;
658 void *addr;
659 u32 nr, i;
660 size_t sz;
661 long ncpus;
662 int ret = -1;
663 struct cpu_map *map;
665 ncpus = cpu__max_present_cpu();
667 /* build online CPU map */
668 map = cpu_map__new(NULL);
669 if (map == NULL) {
670 pr_debug("failed to get system cpumap\n");
671 return NULL;
674 nr = (u32)(ncpus & UINT_MAX);
676 sz = nr * sizeof(char *);
677 addr = calloc(1, sizeof(*tp) + 2 * sz);
678 if (!addr)
679 goto out_free;
681 tp = addr;
682 tp->cpu_nr = nr;
683 addr += sizeof(*tp);
684 tp->core_siblings = addr;
685 addr += sz;
686 tp->thread_siblings = addr;
688 for (i = 0; i < nr; i++) {
689 if (!cpu_map__has(map, i))
690 continue;
692 ret = build_cpu_topo(tp, i);
693 if (ret < 0)
694 break;
697 out_free:
698 cpu_map__put(map);
699 if (ret) {
700 free_cpu_topo(tp);
701 tp = NULL;
703 return tp;
706 static int write_cpu_topology(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
709 struct cpu_topo *tp;
710 u32 i;
711 int ret, j;
713 tp = build_cpu_topology();
714 if (!tp)
715 return -1;
717 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
718 if (ret < 0)
719 goto done;
721 for (i = 0; i < tp->core_sib; i++) {
722 ret = do_write_string(ff, tp->core_siblings[i]);
723 if (ret < 0)
724 goto done;
726 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
727 if (ret < 0)
728 goto done;
730 for (i = 0; i < tp->thread_sib; i++) {
731 ret = do_write_string(ff, tp->thread_siblings[i]);
732 if (ret < 0)
733 break;
736 ret = perf_env__read_cpu_topology_map(&perf_env);
737 if (ret < 0)
738 goto done;
740 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
741 ret = do_write(ff, &perf_env.cpu[j].core_id,
742 sizeof(perf_env.cpu[j].core_id));
743 if (ret < 0)
744 return ret;
745 ret = do_write(ff, &perf_env.cpu[j].socket_id,
746 sizeof(perf_env.cpu[j].socket_id));
747 if (ret < 0)
748 return ret;
750 done:
751 free_cpu_topo(tp);
752 return ret;
757 static int write_total_mem(struct feat_fd *ff,
758 struct perf_evlist *evlist __maybe_unused)
760 char *buf = NULL;
761 FILE *fp;
762 size_t len = 0;
763 int ret = -1, n;
764 uint64_t mem;
766 fp = fopen("/proc/meminfo", "r");
767 if (!fp)
768 return -1;
770 while (getline(&buf, &len, fp) > 0) {
771 ret = strncmp(buf, "MemTotal:", 9);
772 if (!ret)
773 break;
775 if (!ret) {
776 n = sscanf(buf, "%*s %"PRIu64, &mem);
777 if (n == 1)
778 ret = do_write(ff, &mem, sizeof(mem));
779 } else
780 ret = -1;
781 free(buf);
782 fclose(fp);
783 return ret;
786 static int write_topo_node(struct feat_fd *ff, int node)
788 char str[MAXPATHLEN];
789 char field[32];
790 char *buf = NULL, *p;
791 size_t len = 0;
792 FILE *fp;
793 u64 mem_total, mem_free, mem;
794 int ret = -1;
796 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
797 fp = fopen(str, "r");
798 if (!fp)
799 return -1;
801 while (getline(&buf, &len, fp) > 0) {
802 /* skip over invalid lines */
803 if (!strchr(buf, ':'))
804 continue;
805 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
806 goto done;
807 if (!strcmp(field, "MemTotal:"))
808 mem_total = mem;
809 if (!strcmp(field, "MemFree:"))
810 mem_free = mem;
813 fclose(fp);
814 fp = NULL;
816 ret = do_write(ff, &mem_total, sizeof(u64));
817 if (ret)
818 goto done;
820 ret = do_write(ff, &mem_free, sizeof(u64));
821 if (ret)
822 goto done;
824 ret = -1;
825 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
827 fp = fopen(str, "r");
828 if (!fp)
829 goto done;
831 if (getline(&buf, &len, fp) <= 0)
832 goto done;
834 p = strchr(buf, '\n');
835 if (p)
836 *p = '\0';
838 ret = do_write_string(ff, buf);
839 done:
840 free(buf);
841 if (fp)
842 fclose(fp);
843 return ret;
846 static int write_numa_topology(struct feat_fd *ff,
847 struct perf_evlist *evlist __maybe_unused)
849 char *buf = NULL;
850 size_t len = 0;
851 FILE *fp;
852 struct cpu_map *node_map = NULL;
853 char *c;
854 u32 nr, i, j;
855 int ret = -1;
857 fp = fopen("/sys/devices/system/node/online", "r");
858 if (!fp)
859 return -1;
861 if (getline(&buf, &len, fp) <= 0)
862 goto done;
864 c = strchr(buf, '\n');
865 if (c)
866 *c = '\0';
868 node_map = cpu_map__new(buf);
869 if (!node_map)
870 goto done;
872 nr = (u32)node_map->nr;
874 ret = do_write(ff, &nr, sizeof(nr));
875 if (ret < 0)
876 goto done;
878 for (i = 0; i < nr; i++) {
879 j = (u32)node_map->map[i];
880 ret = do_write(ff, &j, sizeof(j));
881 if (ret < 0)
882 break;
884 ret = write_topo_node(ff, i);
885 if (ret < 0)
886 break;
888 done:
889 free(buf);
890 fclose(fp);
891 cpu_map__put(node_map);
892 return ret;
896 * File format:
898 * struct pmu_mappings {
899 * u32 pmu_num;
900 * struct pmu_map {
901 * u32 type;
902 * char name[];
903 * }[pmu_num];
904 * };
907 static int write_pmu_mappings(struct feat_fd *ff,
908 struct perf_evlist *evlist __maybe_unused)
910 struct perf_pmu *pmu = NULL;
911 u32 pmu_num = 0;
912 int ret;
915 * Do a first pass to count number of pmu to avoid lseek so this
916 * works in pipe mode as well.
918 while ((pmu = perf_pmu__scan(pmu))) {
919 if (!pmu->name)
920 continue;
921 pmu_num++;
924 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
925 if (ret < 0)
926 return ret;
928 while ((pmu = perf_pmu__scan(pmu))) {
929 if (!pmu->name)
930 continue;
932 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
933 if (ret < 0)
934 return ret;
936 ret = do_write_string(ff, pmu->name);
937 if (ret < 0)
938 return ret;
941 return 0;
945 * File format:
947 * struct group_descs {
948 * u32 nr_groups;
949 * struct group_desc {
950 * char name[];
951 * u32 leader_idx;
952 * u32 nr_members;
953 * }[nr_groups];
954 * };
956 static int write_group_desc(struct feat_fd *ff,
957 struct perf_evlist *evlist)
959 u32 nr_groups = evlist->nr_groups;
960 struct perf_evsel *evsel;
961 int ret;
963 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
964 if (ret < 0)
965 return ret;
967 evlist__for_each_entry(evlist, evsel) {
968 if (perf_evsel__is_group_leader(evsel) &&
969 evsel->nr_members > 1) {
970 const char *name = evsel->group_name ?: "{anon_group}";
971 u32 leader_idx = evsel->idx;
972 u32 nr_members = evsel->nr_members;
974 ret = do_write_string(ff, name);
975 if (ret < 0)
976 return ret;
978 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
979 if (ret < 0)
980 return ret;
982 ret = do_write(ff, &nr_members, sizeof(nr_members));
983 if (ret < 0)
984 return ret;
987 return 0;
991 * default get_cpuid(): nothing gets recorded
992 * actual implementation must be in arch/$(SRCARCH)/util/header.c
994 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
996 return -1;
999 static int write_cpuid(struct feat_fd *ff,
1000 struct perf_evlist *evlist __maybe_unused)
1002 char buffer[64];
1003 int ret;
1005 ret = get_cpuid(buffer, sizeof(buffer));
1006 if (!ret)
1007 goto write_it;
1009 return -1;
1010 write_it:
1011 return do_write_string(ff, buffer);
1014 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
1015 struct perf_evlist *evlist __maybe_unused)
1017 return 0;
1020 static int write_auxtrace(struct feat_fd *ff,
1021 struct perf_evlist *evlist __maybe_unused)
1023 struct perf_session *session;
1024 int err;
1026 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
1027 return -1;
1029 session = container_of(ff->ph, struct perf_session, header);
1031 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
1032 if (err < 0)
1033 pr_err("Failed to write auxtrace index\n");
1034 return err;
1037 static int write_clockid(struct feat_fd *ff,
1038 struct perf_evlist *evlist __maybe_unused)
1040 return do_write(ff, &ff->ph->env.clockid_res_ns,
1041 sizeof(ff->ph->env.clockid_res_ns));
1044 static int cpu_cache_level__sort(const void *a, const void *b)
1046 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1047 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1049 return cache_a->level - cache_b->level;
1052 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1054 if (a->level != b->level)
1055 return false;
1057 if (a->line_size != b->line_size)
1058 return false;
1060 if (a->sets != b->sets)
1061 return false;
1063 if (a->ways != b->ways)
1064 return false;
1066 if (strcmp(a->type, b->type))
1067 return false;
1069 if (strcmp(a->size, b->size))
1070 return false;
1072 if (strcmp(a->map, b->map))
1073 return false;
1075 return true;
1078 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1080 char path[PATH_MAX], file[PATH_MAX];
1081 struct stat st;
1082 size_t len;
1084 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1085 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1087 if (stat(file, &st))
1088 return 1;
1090 scnprintf(file, PATH_MAX, "%s/level", path);
1091 if (sysfs__read_int(file, (int *) &cache->level))
1092 return -1;
1094 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1095 if (sysfs__read_int(file, (int *) &cache->line_size))
1096 return -1;
1098 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1099 if (sysfs__read_int(file, (int *) &cache->sets))
1100 return -1;
1102 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1103 if (sysfs__read_int(file, (int *) &cache->ways))
1104 return -1;
1106 scnprintf(file, PATH_MAX, "%s/type", path);
1107 if (sysfs__read_str(file, &cache->type, &len))
1108 return -1;
1110 cache->type[len] = 0;
1111 cache->type = rtrim(cache->type);
1113 scnprintf(file, PATH_MAX, "%s/size", path);
1114 if (sysfs__read_str(file, &cache->size, &len)) {
1115 free(cache->type);
1116 return -1;
1119 cache->size[len] = 0;
1120 cache->size = rtrim(cache->size);
1122 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1123 if (sysfs__read_str(file, &cache->map, &len)) {
1124 free(cache->map);
1125 free(cache->type);
1126 return -1;
1129 cache->map[len] = 0;
1130 cache->map = rtrim(cache->map);
1131 return 0;
1134 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1136 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1139 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1141 u32 i, cnt = 0;
1142 long ncpus;
1143 u32 nr, cpu;
1144 u16 level;
1146 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1147 if (ncpus < 0)
1148 return -1;
1150 nr = (u32)(ncpus & UINT_MAX);
1152 for (cpu = 0; cpu < nr; cpu++) {
1153 for (level = 0; level < 10; level++) {
1154 struct cpu_cache_level c;
1155 int err;
1157 err = cpu_cache_level__read(&c, cpu, level);
1158 if (err < 0)
1159 return err;
1161 if (err == 1)
1162 break;
1164 for (i = 0; i < cnt; i++) {
1165 if (cpu_cache_level__cmp(&c, &caches[i]))
1166 break;
1169 if (i == cnt)
1170 caches[cnt++] = c;
1171 else
1172 cpu_cache_level__free(&c);
1174 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1175 goto out;
1178 out:
1179 *cntp = cnt;
1180 return 0;
1183 #define MAX_CACHES 2000
1185 static int write_cache(struct feat_fd *ff,
1186 struct perf_evlist *evlist __maybe_unused)
1188 struct cpu_cache_level caches[MAX_CACHES];
1189 u32 cnt = 0, i, version = 1;
1190 int ret;
1192 ret = build_caches(caches, MAX_CACHES, &cnt);
1193 if (ret)
1194 goto out;
1196 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1198 ret = do_write(ff, &version, sizeof(u32));
1199 if (ret < 0)
1200 goto out;
1202 ret = do_write(ff, &cnt, sizeof(u32));
1203 if (ret < 0)
1204 goto out;
1206 for (i = 0; i < cnt; i++) {
1207 struct cpu_cache_level *c = &caches[i];
1209 #define _W(v) \
1210 ret = do_write(ff, &c->v, sizeof(u32)); \
1211 if (ret < 0) \
1212 goto out;
1214 _W(level)
1215 _W(line_size)
1216 _W(sets)
1217 _W(ways)
1218 #undef _W
1220 #define _W(v) \
1221 ret = do_write_string(ff, (const char *) c->v); \
1222 if (ret < 0) \
1223 goto out;
1225 _W(type)
1226 _W(size)
1227 _W(map)
1228 #undef _W
1231 out:
1232 for (i = 0; i < cnt; i++)
1233 cpu_cache_level__free(&caches[i]);
1234 return ret;
1237 static int write_stat(struct feat_fd *ff __maybe_unused,
1238 struct perf_evlist *evlist __maybe_unused)
1240 return 0;
1243 static int write_sample_time(struct feat_fd *ff,
1244 struct perf_evlist *evlist)
1246 int ret;
1248 ret = do_write(ff, &evlist->first_sample_time,
1249 sizeof(evlist->first_sample_time));
1250 if (ret < 0)
1251 return ret;
1253 return do_write(ff, &evlist->last_sample_time,
1254 sizeof(evlist->last_sample_time));
1258 static int memory_node__read(struct memory_node *n, unsigned long idx)
1260 unsigned int phys, size = 0;
1261 char path[PATH_MAX];
1262 struct dirent *ent;
1263 DIR *dir;
1265 #define for_each_memory(mem, dir) \
1266 while ((ent = readdir(dir))) \
1267 if (strcmp(ent->d_name, ".") && \
1268 strcmp(ent->d_name, "..") && \
1269 sscanf(ent->d_name, "memory%u", &mem) == 1)
1271 scnprintf(path, PATH_MAX,
1272 "%s/devices/system/node/node%lu",
1273 sysfs__mountpoint(), idx);
1275 dir = opendir(path);
1276 if (!dir) {
1277 pr_warning("failed: cant' open memory sysfs data\n");
1278 return -1;
1281 for_each_memory(phys, dir) {
1282 size = max(phys, size);
1285 size++;
1287 n->set = bitmap_alloc(size);
1288 if (!n->set) {
1289 closedir(dir);
1290 return -ENOMEM;
1293 n->node = idx;
1294 n->size = size;
1296 rewinddir(dir);
1298 for_each_memory(phys, dir) {
1299 set_bit(phys, n->set);
1302 closedir(dir);
1303 return 0;
1306 static int memory_node__sort(const void *a, const void *b)
1308 const struct memory_node *na = a;
1309 const struct memory_node *nb = b;
1311 return na->node - nb->node;
1314 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1316 char path[PATH_MAX];
1317 struct dirent *ent;
1318 DIR *dir;
1319 u64 cnt = 0;
1320 int ret = 0;
1322 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1323 sysfs__mountpoint());
1325 dir = opendir(path);
1326 if (!dir) {
1327 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1328 __func__, path);
1329 return -1;
1332 while (!ret && (ent = readdir(dir))) {
1333 unsigned int idx;
1334 int r;
1336 if (!strcmp(ent->d_name, ".") ||
1337 !strcmp(ent->d_name, ".."))
1338 continue;
1340 r = sscanf(ent->d_name, "node%u", &idx);
1341 if (r != 1)
1342 continue;
1344 if (WARN_ONCE(cnt >= size,
1345 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1346 return -1;
1348 ret = memory_node__read(&nodes[cnt++], idx);
1351 *cntp = cnt;
1352 closedir(dir);
1354 if (!ret)
1355 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1357 return ret;
1360 #define MAX_MEMORY_NODES 2000
1363 * The MEM_TOPOLOGY holds physical memory map for every
1364 * node in system. The format of data is as follows:
1366 * 0 - version | for future changes
1367 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1368 * 16 - count | number of nodes
1370 * For each node we store map of physical indexes for
1371 * each node:
1373 * 32 - node id | node index
1374 * 40 - size | size of bitmap
1375 * 48 - bitmap | bitmap of memory indexes that belongs to node
1377 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1378 struct perf_evlist *evlist __maybe_unused)
1380 static struct memory_node nodes[MAX_MEMORY_NODES];
1381 u64 bsize, version = 1, i, nr;
1382 int ret;
1384 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1385 (unsigned long long *) &bsize);
1386 if (ret)
1387 return ret;
1389 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1390 if (ret)
1391 return ret;
1393 ret = do_write(ff, &version, sizeof(version));
1394 if (ret < 0)
1395 goto out;
1397 ret = do_write(ff, &bsize, sizeof(bsize));
1398 if (ret < 0)
1399 goto out;
1401 ret = do_write(ff, &nr, sizeof(nr));
1402 if (ret < 0)
1403 goto out;
1405 for (i = 0; i < nr; i++) {
1406 struct memory_node *n = &nodes[i];
1408 #define _W(v) \
1409 ret = do_write(ff, &n->v, sizeof(n->v)); \
1410 if (ret < 0) \
1411 goto out;
1413 _W(node)
1414 _W(size)
1416 #undef _W
1418 ret = do_write_bitmap(ff, n->set, n->size);
1419 if (ret < 0)
1420 goto out;
1423 out:
1424 return ret;
1427 static void print_hostname(struct feat_fd *ff, FILE *fp)
1429 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1432 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1434 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1437 static void print_arch(struct feat_fd *ff, FILE *fp)
1439 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1442 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1444 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1447 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1449 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1450 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1453 static void print_version(struct feat_fd *ff, FILE *fp)
1455 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1458 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1460 int nr, i;
1462 nr = ff->ph->env.nr_cmdline;
1464 fprintf(fp, "# cmdline : ");
1466 for (i = 0; i < nr; i++) {
1467 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1468 if (!argv_i) {
1469 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1470 } else {
1471 char *mem = argv_i;
1472 do {
1473 char *quote = strchr(argv_i, '\'');
1474 if (!quote)
1475 break;
1476 *quote++ = '\0';
1477 fprintf(fp, "%s\\\'", argv_i);
1478 argv_i = quote;
1479 } while (1);
1480 fprintf(fp, "%s ", argv_i);
1481 free(mem);
1484 fputc('\n', fp);
1487 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1489 struct perf_header *ph = ff->ph;
1490 int cpu_nr = ph->env.nr_cpus_avail;
1491 int nr, i;
1492 char *str;
1494 nr = ph->env.nr_sibling_cores;
1495 str = ph->env.sibling_cores;
1497 for (i = 0; i < nr; i++) {
1498 fprintf(fp, "# sibling cores : %s\n", str);
1499 str += strlen(str) + 1;
1502 nr = ph->env.nr_sibling_threads;
1503 str = ph->env.sibling_threads;
1505 for (i = 0; i < nr; i++) {
1506 fprintf(fp, "# sibling threads : %s\n", str);
1507 str += strlen(str) + 1;
1510 if (ph->env.cpu != NULL) {
1511 for (i = 0; i < cpu_nr; i++)
1512 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1513 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1514 } else
1515 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1518 static void print_clockid(struct feat_fd *ff, FILE *fp)
1520 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1521 ff->ph->env.clockid_res_ns * 1000);
1524 static void free_event_desc(struct perf_evsel *events)
1526 struct perf_evsel *evsel;
1528 if (!events)
1529 return;
1531 for (evsel = events; evsel->attr.size; evsel++) {
1532 zfree(&evsel->name);
1533 zfree(&evsel->id);
1536 free(events);
1539 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1541 struct perf_evsel *evsel, *events = NULL;
1542 u64 *id;
1543 void *buf = NULL;
1544 u32 nre, sz, nr, i, j;
1545 size_t msz;
1547 /* number of events */
1548 if (do_read_u32(ff, &nre))
1549 goto error;
1551 if (do_read_u32(ff, &sz))
1552 goto error;
1554 /* buffer to hold on file attr struct */
1555 buf = malloc(sz);
1556 if (!buf)
1557 goto error;
1559 /* the last event terminates with evsel->attr.size == 0: */
1560 events = calloc(nre + 1, sizeof(*events));
1561 if (!events)
1562 goto error;
1564 msz = sizeof(evsel->attr);
1565 if (sz < msz)
1566 msz = sz;
1568 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1569 evsel->idx = i;
1572 * must read entire on-file attr struct to
1573 * sync up with layout.
1575 if (__do_read(ff, buf, sz))
1576 goto error;
1578 if (ff->ph->needs_swap)
1579 perf_event__attr_swap(buf);
1581 memcpy(&evsel->attr, buf, msz);
1583 if (do_read_u32(ff, &nr))
1584 goto error;
1586 if (ff->ph->needs_swap)
1587 evsel->needs_swap = true;
1589 evsel->name = do_read_string(ff);
1590 if (!evsel->name)
1591 goto error;
1593 if (!nr)
1594 continue;
1596 id = calloc(nr, sizeof(*id));
1597 if (!id)
1598 goto error;
1599 evsel->ids = nr;
1600 evsel->id = id;
1602 for (j = 0 ; j < nr; j++) {
1603 if (do_read_u64(ff, id))
1604 goto error;
1605 id++;
1608 out:
1609 free(buf);
1610 return events;
1611 error:
1612 free_event_desc(events);
1613 events = NULL;
1614 goto out;
1617 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1618 void *priv __maybe_unused)
1620 return fprintf(fp, ", %s = %s", name, val);
1623 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1625 struct perf_evsel *evsel, *events;
1626 u32 j;
1627 u64 *id;
1629 if (ff->events)
1630 events = ff->events;
1631 else
1632 events = read_event_desc(ff);
1634 if (!events) {
1635 fprintf(fp, "# event desc: not available or unable to read\n");
1636 return;
1639 for (evsel = events; evsel->attr.size; evsel++) {
1640 fprintf(fp, "# event : name = %s, ", evsel->name);
1642 if (evsel->ids) {
1643 fprintf(fp, ", id = {");
1644 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1645 if (j)
1646 fputc(',', fp);
1647 fprintf(fp, " %"PRIu64, *id);
1649 fprintf(fp, " }");
1652 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1654 fputc('\n', fp);
1657 free_event_desc(events);
1658 ff->events = NULL;
1661 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1663 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1666 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1668 int i;
1669 struct numa_node *n;
1671 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1672 n = &ff->ph->env.numa_nodes[i];
1674 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1675 " free = %"PRIu64" kB\n",
1676 n->node, n->mem_total, n->mem_free);
1678 fprintf(fp, "# node%u cpu list : ", n->node);
1679 cpu_map__fprintf(n->map, fp);
1683 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1685 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1688 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1690 fprintf(fp, "# contains samples with branch stack\n");
1693 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1695 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1698 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1700 fprintf(fp, "# contains stat data\n");
1703 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1705 int i;
1707 fprintf(fp, "# CPU cache info:\n");
1708 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1709 fprintf(fp, "# ");
1710 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1714 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1716 const char *delimiter = "# pmu mappings: ";
1717 char *str, *tmp;
1718 u32 pmu_num;
1719 u32 type;
1721 pmu_num = ff->ph->env.nr_pmu_mappings;
1722 if (!pmu_num) {
1723 fprintf(fp, "# pmu mappings: not available\n");
1724 return;
1727 str = ff->ph->env.pmu_mappings;
1729 while (pmu_num) {
1730 type = strtoul(str, &tmp, 0);
1731 if (*tmp != ':')
1732 goto error;
1734 str = tmp + 1;
1735 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1737 delimiter = ", ";
1738 str += strlen(str) + 1;
1739 pmu_num--;
1742 fprintf(fp, "\n");
1744 if (!pmu_num)
1745 return;
1746 error:
1747 fprintf(fp, "# pmu mappings: unable to read\n");
1750 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1752 struct perf_session *session;
1753 struct perf_evsel *evsel;
1754 u32 nr = 0;
1756 session = container_of(ff->ph, struct perf_session, header);
1758 evlist__for_each_entry(session->evlist, evsel) {
1759 if (perf_evsel__is_group_leader(evsel) &&
1760 evsel->nr_members > 1) {
1761 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1762 perf_evsel__name(evsel));
1764 nr = evsel->nr_members - 1;
1765 } else if (nr) {
1766 fprintf(fp, ",%s", perf_evsel__name(evsel));
1768 if (--nr == 0)
1769 fprintf(fp, "}\n");
1774 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1776 struct perf_session *session;
1777 char time_buf[32];
1778 double d;
1780 session = container_of(ff->ph, struct perf_session, header);
1782 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1783 time_buf, sizeof(time_buf));
1784 fprintf(fp, "# time of first sample : %s\n", time_buf);
1786 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1787 time_buf, sizeof(time_buf));
1788 fprintf(fp, "# time of last sample : %s\n", time_buf);
1790 d = (double)(session->evlist->last_sample_time -
1791 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1793 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1796 static void memory_node__fprintf(struct memory_node *n,
1797 unsigned long long bsize, FILE *fp)
1799 char buf_map[100], buf_size[50];
1800 unsigned long long size;
1802 size = bsize * bitmap_weight(n->set, n->size);
1803 unit_number__scnprintf(buf_size, 50, size);
1805 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1806 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1809 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1811 struct memory_node *nodes;
1812 int i, nr;
1814 nodes = ff->ph->env.memory_nodes;
1815 nr = ff->ph->env.nr_memory_nodes;
1817 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1818 nr, ff->ph->env.memory_bsize);
1820 for (i = 0; i < nr; i++) {
1821 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1825 static int __event_process_build_id(struct build_id_event *bev,
1826 char *filename,
1827 struct perf_session *session)
1829 int err = -1;
1830 struct machine *machine;
1831 u16 cpumode;
1832 struct dso *dso;
1833 enum dso_kernel_type dso_type;
1835 machine = perf_session__findnew_machine(session, bev->pid);
1836 if (!machine)
1837 goto out;
1839 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1841 switch (cpumode) {
1842 case PERF_RECORD_MISC_KERNEL:
1843 dso_type = DSO_TYPE_KERNEL;
1844 break;
1845 case PERF_RECORD_MISC_GUEST_KERNEL:
1846 dso_type = DSO_TYPE_GUEST_KERNEL;
1847 break;
1848 case PERF_RECORD_MISC_USER:
1849 case PERF_RECORD_MISC_GUEST_USER:
1850 dso_type = DSO_TYPE_USER;
1851 break;
1852 default:
1853 goto out;
1856 dso = machine__findnew_dso(machine, filename);
1857 if (dso != NULL) {
1858 char sbuild_id[SBUILD_ID_SIZE];
1860 dso__set_build_id(dso, &bev->build_id);
1862 if (dso_type != DSO_TYPE_USER) {
1863 struct kmod_path m = { .name = NULL, };
1865 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1866 dso__set_module_info(dso, &m, machine);
1867 else
1868 dso->kernel = dso_type;
1870 free(m.name);
1873 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1874 sbuild_id);
1875 pr_debug("build id event received for %s: %s\n",
1876 dso->long_name, sbuild_id);
1877 dso__put(dso);
1880 err = 0;
1881 out:
1882 return err;
1885 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1886 int input, u64 offset, u64 size)
1888 struct perf_session *session = container_of(header, struct perf_session, header);
1889 struct {
1890 struct perf_event_header header;
1891 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1892 char filename[0];
1893 } old_bev;
1894 struct build_id_event bev;
1895 char filename[PATH_MAX];
1896 u64 limit = offset + size;
1898 while (offset < limit) {
1899 ssize_t len;
1901 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1902 return -1;
1904 if (header->needs_swap)
1905 perf_event_header__bswap(&old_bev.header);
1907 len = old_bev.header.size - sizeof(old_bev);
1908 if (readn(input, filename, len) != len)
1909 return -1;
1911 bev.header = old_bev.header;
1914 * As the pid is the missing value, we need to fill
1915 * it properly. The header.misc value give us nice hint.
1917 bev.pid = HOST_KERNEL_ID;
1918 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1919 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1920 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1922 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1923 __event_process_build_id(&bev, filename, session);
1925 offset += bev.header.size;
1928 return 0;
1931 static int perf_header__read_build_ids(struct perf_header *header,
1932 int input, u64 offset, u64 size)
1934 struct perf_session *session = container_of(header, struct perf_session, header);
1935 struct build_id_event bev;
1936 char filename[PATH_MAX];
1937 u64 limit = offset + size, orig_offset = offset;
1938 int err = -1;
1940 while (offset < limit) {
1941 ssize_t len;
1943 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1944 goto out;
1946 if (header->needs_swap)
1947 perf_event_header__bswap(&bev.header);
1949 len = bev.header.size - sizeof(bev);
1950 if (readn(input, filename, len) != len)
1951 goto out;
1953 * The a1645ce1 changeset:
1955 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1957 * Added a field to struct build_id_event that broke the file
1958 * format.
1960 * Since the kernel build-id is the first entry, process the
1961 * table using the old format if the well known
1962 * '[kernel.kallsyms]' string for the kernel build-id has the
1963 * first 4 characters chopped off (where the pid_t sits).
1965 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1966 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1967 return -1;
1968 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1971 __event_process_build_id(&bev, filename, session);
1973 offset += bev.header.size;
1975 err = 0;
1976 out:
1977 return err;
1980 /* Macro for features that simply need to read and store a string. */
1981 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1982 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1984 ff->ph->env.__feat_env = do_read_string(ff); \
1985 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1988 FEAT_PROCESS_STR_FUN(hostname, hostname);
1989 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1990 FEAT_PROCESS_STR_FUN(version, version);
1991 FEAT_PROCESS_STR_FUN(arch, arch);
1992 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1993 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1995 static int process_tracing_data(struct feat_fd *ff, void *data)
1997 ssize_t ret = trace_report(ff->fd, data, false);
1999 return ret < 0 ? -1 : 0;
2002 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2004 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2005 pr_debug("Failed to read buildids, continuing...\n");
2006 return 0;
2009 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2011 int ret;
2012 u32 nr_cpus_avail, nr_cpus_online;
2014 ret = do_read_u32(ff, &nr_cpus_avail);
2015 if (ret)
2016 return ret;
2018 ret = do_read_u32(ff, &nr_cpus_online);
2019 if (ret)
2020 return ret;
2021 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2022 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2023 return 0;
2026 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2028 u64 total_mem;
2029 int ret;
2031 ret = do_read_u64(ff, &total_mem);
2032 if (ret)
2033 return -1;
2034 ff->ph->env.total_mem = (unsigned long long)total_mem;
2035 return 0;
2038 static struct perf_evsel *
2039 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2041 struct perf_evsel *evsel;
2043 evlist__for_each_entry(evlist, evsel) {
2044 if (evsel->idx == idx)
2045 return evsel;
2048 return NULL;
2051 static void
2052 perf_evlist__set_event_name(struct perf_evlist *evlist,
2053 struct perf_evsel *event)
2055 struct perf_evsel *evsel;
2057 if (!event->name)
2058 return;
2060 evsel = perf_evlist__find_by_index(evlist, event->idx);
2061 if (!evsel)
2062 return;
2064 if (evsel->name)
2065 return;
2067 evsel->name = strdup(event->name);
2070 static int
2071 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2073 struct perf_session *session;
2074 struct perf_evsel *evsel, *events = read_event_desc(ff);
2076 if (!events)
2077 return 0;
2079 session = container_of(ff->ph, struct perf_session, header);
2081 if (session->data->is_pipe) {
2082 /* Save events for reading later by print_event_desc,
2083 * since they can't be read again in pipe mode. */
2084 ff->events = events;
2087 for (evsel = events; evsel->attr.size; evsel++)
2088 perf_evlist__set_event_name(session->evlist, evsel);
2090 if (!session->data->is_pipe)
2091 free_event_desc(events);
2093 return 0;
2096 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2098 char *str, *cmdline = NULL, **argv = NULL;
2099 u32 nr, i, len = 0;
2101 if (do_read_u32(ff, &nr))
2102 return -1;
2104 ff->ph->env.nr_cmdline = nr;
2106 cmdline = zalloc(ff->size + nr + 1);
2107 if (!cmdline)
2108 return -1;
2110 argv = zalloc(sizeof(char *) * (nr + 1));
2111 if (!argv)
2112 goto error;
2114 for (i = 0; i < nr; i++) {
2115 str = do_read_string(ff);
2116 if (!str)
2117 goto error;
2119 argv[i] = cmdline + len;
2120 memcpy(argv[i], str, strlen(str) + 1);
2121 len += strlen(str) + 1;
2122 free(str);
2124 ff->ph->env.cmdline = cmdline;
2125 ff->ph->env.cmdline_argv = (const char **) argv;
2126 return 0;
2128 error:
2129 free(argv);
2130 free(cmdline);
2131 return -1;
2134 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2136 u32 nr, i;
2137 char *str;
2138 struct strbuf sb;
2139 int cpu_nr = ff->ph->env.nr_cpus_avail;
2140 u64 size = 0;
2141 struct perf_header *ph = ff->ph;
2142 bool do_core_id_test = true;
2144 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2145 if (!ph->env.cpu)
2146 return -1;
2148 if (do_read_u32(ff, &nr))
2149 goto free_cpu;
2151 ph->env.nr_sibling_cores = nr;
2152 size += sizeof(u32);
2153 if (strbuf_init(&sb, 128) < 0)
2154 goto free_cpu;
2156 for (i = 0; i < nr; i++) {
2157 str = do_read_string(ff);
2158 if (!str)
2159 goto error;
2161 /* include a NULL character at the end */
2162 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2163 goto error;
2164 size += string_size(str);
2165 free(str);
2167 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2169 if (do_read_u32(ff, &nr))
2170 return -1;
2172 ph->env.nr_sibling_threads = nr;
2173 size += sizeof(u32);
2175 for (i = 0; i < nr; i++) {
2176 str = do_read_string(ff);
2177 if (!str)
2178 goto error;
2180 /* include a NULL character at the end */
2181 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2182 goto error;
2183 size += string_size(str);
2184 free(str);
2186 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2189 * The header may be from old perf,
2190 * which doesn't include core id and socket id information.
2192 if (ff->size <= size) {
2193 zfree(&ph->env.cpu);
2194 return 0;
2197 /* On s390 the socket_id number is not related to the numbers of cpus.
2198 * The socket_id number might be higher than the numbers of cpus.
2199 * This depends on the configuration.
2201 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
2202 do_core_id_test = false;
2204 for (i = 0; i < (u32)cpu_nr; i++) {
2205 if (do_read_u32(ff, &nr))
2206 goto free_cpu;
2208 ph->env.cpu[i].core_id = nr;
2210 if (do_read_u32(ff, &nr))
2211 goto free_cpu;
2213 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2214 pr_debug("socket_id number is too big."
2215 "You may need to upgrade the perf tool.\n");
2216 goto free_cpu;
2219 ph->env.cpu[i].socket_id = nr;
2222 return 0;
2224 error:
2225 strbuf_release(&sb);
2226 free_cpu:
2227 zfree(&ph->env.cpu);
2228 return -1;
2231 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2233 struct numa_node *nodes, *n;
2234 u32 nr, i;
2235 char *str;
2237 /* nr nodes */
2238 if (do_read_u32(ff, &nr))
2239 return -1;
2241 nodes = zalloc(sizeof(*nodes) * nr);
2242 if (!nodes)
2243 return -ENOMEM;
2245 for (i = 0; i < nr; i++) {
2246 n = &nodes[i];
2248 /* node number */
2249 if (do_read_u32(ff, &n->node))
2250 goto error;
2252 if (do_read_u64(ff, &n->mem_total))
2253 goto error;
2255 if (do_read_u64(ff, &n->mem_free))
2256 goto error;
2258 str = do_read_string(ff);
2259 if (!str)
2260 goto error;
2262 n->map = cpu_map__new(str);
2263 if (!n->map)
2264 goto error;
2266 free(str);
2268 ff->ph->env.nr_numa_nodes = nr;
2269 ff->ph->env.numa_nodes = nodes;
2270 return 0;
2272 error:
2273 free(nodes);
2274 return -1;
2277 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2279 char *name;
2280 u32 pmu_num;
2281 u32 type;
2282 struct strbuf sb;
2284 if (do_read_u32(ff, &pmu_num))
2285 return -1;
2287 if (!pmu_num) {
2288 pr_debug("pmu mappings not available\n");
2289 return 0;
2292 ff->ph->env.nr_pmu_mappings = pmu_num;
2293 if (strbuf_init(&sb, 128) < 0)
2294 return -1;
2296 while (pmu_num) {
2297 if (do_read_u32(ff, &type))
2298 goto error;
2300 name = do_read_string(ff);
2301 if (!name)
2302 goto error;
2304 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2305 goto error;
2306 /* include a NULL character at the end */
2307 if (strbuf_add(&sb, "", 1) < 0)
2308 goto error;
2310 if (!strcmp(name, "msr"))
2311 ff->ph->env.msr_pmu_type = type;
2313 free(name);
2314 pmu_num--;
2316 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2317 return 0;
2319 error:
2320 strbuf_release(&sb);
2321 return -1;
2324 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2326 size_t ret = -1;
2327 u32 i, nr, nr_groups;
2328 struct perf_session *session;
2329 struct perf_evsel *evsel, *leader = NULL;
2330 struct group_desc {
2331 char *name;
2332 u32 leader_idx;
2333 u32 nr_members;
2334 } *desc;
2336 if (do_read_u32(ff, &nr_groups))
2337 return -1;
2339 ff->ph->env.nr_groups = nr_groups;
2340 if (!nr_groups) {
2341 pr_debug("group desc not available\n");
2342 return 0;
2345 desc = calloc(nr_groups, sizeof(*desc));
2346 if (!desc)
2347 return -1;
2349 for (i = 0; i < nr_groups; i++) {
2350 desc[i].name = do_read_string(ff);
2351 if (!desc[i].name)
2352 goto out_free;
2354 if (do_read_u32(ff, &desc[i].leader_idx))
2355 goto out_free;
2357 if (do_read_u32(ff, &desc[i].nr_members))
2358 goto out_free;
2362 * Rebuild group relationship based on the group_desc
2364 session = container_of(ff->ph, struct perf_session, header);
2365 session->evlist->nr_groups = nr_groups;
2367 i = nr = 0;
2368 evlist__for_each_entry(session->evlist, evsel) {
2369 if (evsel->idx == (int) desc[i].leader_idx) {
2370 evsel->leader = evsel;
2371 /* {anon_group} is a dummy name */
2372 if (strcmp(desc[i].name, "{anon_group}")) {
2373 evsel->group_name = desc[i].name;
2374 desc[i].name = NULL;
2376 evsel->nr_members = desc[i].nr_members;
2378 if (i >= nr_groups || nr > 0) {
2379 pr_debug("invalid group desc\n");
2380 goto out_free;
2383 leader = evsel;
2384 nr = evsel->nr_members - 1;
2385 i++;
2386 } else if (nr) {
2387 /* This is a group member */
2388 evsel->leader = leader;
2390 nr--;
2394 if (i != nr_groups || nr != 0) {
2395 pr_debug("invalid group desc\n");
2396 goto out_free;
2399 ret = 0;
2400 out_free:
2401 for (i = 0; i < nr_groups; i++)
2402 zfree(&desc[i].name);
2403 free(desc);
2405 return ret;
2408 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2410 struct perf_session *session;
2411 int err;
2413 session = container_of(ff->ph, struct perf_session, header);
2415 err = auxtrace_index__process(ff->fd, ff->size, session,
2416 ff->ph->needs_swap);
2417 if (err < 0)
2418 pr_err("Failed to process auxtrace index\n");
2419 return err;
2422 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2424 struct cpu_cache_level *caches;
2425 u32 cnt, i, version;
2427 if (do_read_u32(ff, &version))
2428 return -1;
2430 if (version != 1)
2431 return -1;
2433 if (do_read_u32(ff, &cnt))
2434 return -1;
2436 caches = zalloc(sizeof(*caches) * cnt);
2437 if (!caches)
2438 return -1;
2440 for (i = 0; i < cnt; i++) {
2441 struct cpu_cache_level c;
2443 #define _R(v) \
2444 if (do_read_u32(ff, &c.v))\
2445 goto out_free_caches; \
2447 _R(level)
2448 _R(line_size)
2449 _R(sets)
2450 _R(ways)
2451 #undef _R
2453 #define _R(v) \
2454 c.v = do_read_string(ff); \
2455 if (!c.v) \
2456 goto out_free_caches;
2458 _R(type)
2459 _R(size)
2460 _R(map)
2461 #undef _R
2463 caches[i] = c;
2466 ff->ph->env.caches = caches;
2467 ff->ph->env.caches_cnt = cnt;
2468 return 0;
2469 out_free_caches:
2470 free(caches);
2471 return -1;
2474 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2476 struct perf_session *session;
2477 u64 first_sample_time, last_sample_time;
2478 int ret;
2480 session = container_of(ff->ph, struct perf_session, header);
2482 ret = do_read_u64(ff, &first_sample_time);
2483 if (ret)
2484 return -1;
2486 ret = do_read_u64(ff, &last_sample_time);
2487 if (ret)
2488 return -1;
2490 session->evlist->first_sample_time = first_sample_time;
2491 session->evlist->last_sample_time = last_sample_time;
2492 return 0;
2495 static int process_mem_topology(struct feat_fd *ff,
2496 void *data __maybe_unused)
2498 struct memory_node *nodes;
2499 u64 version, i, nr, bsize;
2500 int ret = -1;
2502 if (do_read_u64(ff, &version))
2503 return -1;
2505 if (version != 1)
2506 return -1;
2508 if (do_read_u64(ff, &bsize))
2509 return -1;
2511 if (do_read_u64(ff, &nr))
2512 return -1;
2514 nodes = zalloc(sizeof(*nodes) * nr);
2515 if (!nodes)
2516 return -1;
2518 for (i = 0; i < nr; i++) {
2519 struct memory_node n;
2521 #define _R(v) \
2522 if (do_read_u64(ff, &n.v)) \
2523 goto out; \
2525 _R(node)
2526 _R(size)
2528 #undef _R
2530 if (do_read_bitmap(ff, &n.set, &n.size))
2531 goto out;
2533 nodes[i] = n;
2536 ff->ph->env.memory_bsize = bsize;
2537 ff->ph->env.memory_nodes = nodes;
2538 ff->ph->env.nr_memory_nodes = nr;
2539 ret = 0;
2541 out:
2542 if (ret)
2543 free(nodes);
2544 return ret;
2547 static int process_clockid(struct feat_fd *ff,
2548 void *data __maybe_unused)
2550 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2551 return -1;
2553 return 0;
2556 struct feature_ops {
2557 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2558 void (*print)(struct feat_fd *ff, FILE *fp);
2559 int (*process)(struct feat_fd *ff, void *data);
2560 const char *name;
2561 bool full_only;
2562 bool synthesize;
2565 #define FEAT_OPR(n, func, __full_only) \
2566 [HEADER_##n] = { \
2567 .name = __stringify(n), \
2568 .write = write_##func, \
2569 .print = print_##func, \
2570 .full_only = __full_only, \
2571 .process = process_##func, \
2572 .synthesize = true \
2575 #define FEAT_OPN(n, func, __full_only) \
2576 [HEADER_##n] = { \
2577 .name = __stringify(n), \
2578 .write = write_##func, \
2579 .print = print_##func, \
2580 .full_only = __full_only, \
2581 .process = process_##func \
2584 /* feature_ops not implemented: */
2585 #define print_tracing_data NULL
2586 #define print_build_id NULL
2588 #define process_branch_stack NULL
2589 #define process_stat NULL
2592 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2593 FEAT_OPN(TRACING_DATA, tracing_data, false),
2594 FEAT_OPN(BUILD_ID, build_id, false),
2595 FEAT_OPR(HOSTNAME, hostname, false),
2596 FEAT_OPR(OSRELEASE, osrelease, false),
2597 FEAT_OPR(VERSION, version, false),
2598 FEAT_OPR(ARCH, arch, false),
2599 FEAT_OPR(NRCPUS, nrcpus, false),
2600 FEAT_OPR(CPUDESC, cpudesc, false),
2601 FEAT_OPR(CPUID, cpuid, false),
2602 FEAT_OPR(TOTAL_MEM, total_mem, false),
2603 FEAT_OPR(EVENT_DESC, event_desc, false),
2604 FEAT_OPR(CMDLINE, cmdline, false),
2605 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2606 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2607 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2608 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2609 FEAT_OPR(GROUP_DESC, group_desc, false),
2610 FEAT_OPN(AUXTRACE, auxtrace, false),
2611 FEAT_OPN(STAT, stat, false),
2612 FEAT_OPN(CACHE, cache, true),
2613 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2614 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2615 FEAT_OPR(CLOCKID, clockid, false)
2618 struct header_print_data {
2619 FILE *fp;
2620 bool full; /* extended list of headers */
2623 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2624 struct perf_header *ph,
2625 int feat, int fd, void *data)
2627 struct header_print_data *hd = data;
2628 struct feat_fd ff;
2630 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2631 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2632 "%d, continuing...\n", section->offset, feat);
2633 return 0;
2635 if (feat >= HEADER_LAST_FEATURE) {
2636 pr_warning("unknown feature %d\n", feat);
2637 return 0;
2639 if (!feat_ops[feat].print)
2640 return 0;
2642 ff = (struct feat_fd) {
2643 .fd = fd,
2644 .ph = ph,
2647 if (!feat_ops[feat].full_only || hd->full)
2648 feat_ops[feat].print(&ff, hd->fp);
2649 else
2650 fprintf(hd->fp, "# %s info available, use -I to display\n",
2651 feat_ops[feat].name);
2653 return 0;
2656 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2658 struct header_print_data hd;
2659 struct perf_header *header = &session->header;
2660 int fd = perf_data__fd(session->data);
2661 struct stat st;
2662 int ret, bit;
2664 hd.fp = fp;
2665 hd.full = full;
2667 ret = fstat(fd, &st);
2668 if (ret == -1)
2669 return -1;
2671 fprintf(fp, "# captured on : %s", ctime(&st.st_ctime));
2673 fprintf(fp, "# header version : %u\n", header->version);
2674 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2675 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2676 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2678 perf_header__process_sections(header, fd, &hd,
2679 perf_file_section__fprintf_info);
2681 if (session->data->is_pipe)
2682 return 0;
2684 fprintf(fp, "# missing features: ");
2685 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2686 if (bit)
2687 fprintf(fp, "%s ", feat_ops[bit].name);
2690 fprintf(fp, "\n");
2691 return 0;
2694 static int do_write_feat(struct feat_fd *ff, int type,
2695 struct perf_file_section **p,
2696 struct perf_evlist *evlist)
2698 int err;
2699 int ret = 0;
2701 if (perf_header__has_feat(ff->ph, type)) {
2702 if (!feat_ops[type].write)
2703 return -1;
2705 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2706 return -1;
2708 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2710 err = feat_ops[type].write(ff, evlist);
2711 if (err < 0) {
2712 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2714 /* undo anything written */
2715 lseek(ff->fd, (*p)->offset, SEEK_SET);
2717 return -1;
2719 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2720 (*p)++;
2722 return ret;
2725 static int perf_header__adds_write(struct perf_header *header,
2726 struct perf_evlist *evlist, int fd)
2728 int nr_sections;
2729 struct feat_fd ff;
2730 struct perf_file_section *feat_sec, *p;
2731 int sec_size;
2732 u64 sec_start;
2733 int feat;
2734 int err;
2736 ff = (struct feat_fd){
2737 .fd = fd,
2738 .ph = header,
2741 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2742 if (!nr_sections)
2743 return 0;
2745 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2746 if (feat_sec == NULL)
2747 return -ENOMEM;
2749 sec_size = sizeof(*feat_sec) * nr_sections;
2751 sec_start = header->feat_offset;
2752 lseek(fd, sec_start + sec_size, SEEK_SET);
2754 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2755 if (do_write_feat(&ff, feat, &p, evlist))
2756 perf_header__clear_feat(header, feat);
2759 lseek(fd, sec_start, SEEK_SET);
2761 * may write more than needed due to dropped feature, but
2762 * this is okay, reader will skip the mising entries
2764 err = do_write(&ff, feat_sec, sec_size);
2765 if (err < 0)
2766 pr_debug("failed to write feature section\n");
2767 free(feat_sec);
2768 return err;
2771 int perf_header__write_pipe(int fd)
2773 struct perf_pipe_file_header f_header;
2774 struct feat_fd ff;
2775 int err;
2777 ff = (struct feat_fd){ .fd = fd };
2779 f_header = (struct perf_pipe_file_header){
2780 .magic = PERF_MAGIC,
2781 .size = sizeof(f_header),
2784 err = do_write(&ff, &f_header, sizeof(f_header));
2785 if (err < 0) {
2786 pr_debug("failed to write perf pipe header\n");
2787 return err;
2790 return 0;
2793 int perf_session__write_header(struct perf_session *session,
2794 struct perf_evlist *evlist,
2795 int fd, bool at_exit)
2797 struct perf_file_header f_header;
2798 struct perf_file_attr f_attr;
2799 struct perf_header *header = &session->header;
2800 struct perf_evsel *evsel;
2801 struct feat_fd ff;
2802 u64 attr_offset;
2803 int err;
2805 ff = (struct feat_fd){ .fd = fd};
2806 lseek(fd, sizeof(f_header), SEEK_SET);
2808 evlist__for_each_entry(session->evlist, evsel) {
2809 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2810 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2811 if (err < 0) {
2812 pr_debug("failed to write perf header\n");
2813 return err;
2817 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2819 evlist__for_each_entry(evlist, evsel) {
2820 f_attr = (struct perf_file_attr){
2821 .attr = evsel->attr,
2822 .ids = {
2823 .offset = evsel->id_offset,
2824 .size = evsel->ids * sizeof(u64),
2827 err = do_write(&ff, &f_attr, sizeof(f_attr));
2828 if (err < 0) {
2829 pr_debug("failed to write perf header attribute\n");
2830 return err;
2834 if (!header->data_offset)
2835 header->data_offset = lseek(fd, 0, SEEK_CUR);
2836 header->feat_offset = header->data_offset + header->data_size;
2838 if (at_exit) {
2839 err = perf_header__adds_write(header, evlist, fd);
2840 if (err < 0)
2841 return err;
2844 f_header = (struct perf_file_header){
2845 .magic = PERF_MAGIC,
2846 .size = sizeof(f_header),
2847 .attr_size = sizeof(f_attr),
2848 .attrs = {
2849 .offset = attr_offset,
2850 .size = evlist->nr_entries * sizeof(f_attr),
2852 .data = {
2853 .offset = header->data_offset,
2854 .size = header->data_size,
2856 /* event_types is ignored, store zeros */
2859 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2861 lseek(fd, 0, SEEK_SET);
2862 err = do_write(&ff, &f_header, sizeof(f_header));
2863 if (err < 0) {
2864 pr_debug("failed to write perf header\n");
2865 return err;
2867 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2869 return 0;
2872 static int perf_header__getbuffer64(struct perf_header *header,
2873 int fd, void *buf, size_t size)
2875 if (readn(fd, buf, size) <= 0)
2876 return -1;
2878 if (header->needs_swap)
2879 mem_bswap_64(buf, size);
2881 return 0;
2884 int perf_header__process_sections(struct perf_header *header, int fd,
2885 void *data,
2886 int (*process)(struct perf_file_section *section,
2887 struct perf_header *ph,
2888 int feat, int fd, void *data))
2890 struct perf_file_section *feat_sec, *sec;
2891 int nr_sections;
2892 int sec_size;
2893 int feat;
2894 int err;
2896 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2897 if (!nr_sections)
2898 return 0;
2900 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2901 if (!feat_sec)
2902 return -1;
2904 sec_size = sizeof(*feat_sec) * nr_sections;
2906 lseek(fd, header->feat_offset, SEEK_SET);
2908 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2909 if (err < 0)
2910 goto out_free;
2912 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2913 err = process(sec++, header, feat, fd, data);
2914 if (err < 0)
2915 goto out_free;
2917 err = 0;
2918 out_free:
2919 free(feat_sec);
2920 return err;
2923 static const int attr_file_abi_sizes[] = {
2924 [0] = PERF_ATTR_SIZE_VER0,
2925 [1] = PERF_ATTR_SIZE_VER1,
2926 [2] = PERF_ATTR_SIZE_VER2,
2927 [3] = PERF_ATTR_SIZE_VER3,
2928 [4] = PERF_ATTR_SIZE_VER4,
2933 * In the legacy file format, the magic number is not used to encode endianness.
2934 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2935 * on ABI revisions, we need to try all combinations for all endianness to
2936 * detect the endianness.
2938 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2940 uint64_t ref_size, attr_size;
2941 int i;
2943 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2944 ref_size = attr_file_abi_sizes[i]
2945 + sizeof(struct perf_file_section);
2946 if (hdr_sz != ref_size) {
2947 attr_size = bswap_64(hdr_sz);
2948 if (attr_size != ref_size)
2949 continue;
2951 ph->needs_swap = true;
2953 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2955 ph->needs_swap);
2956 return 0;
2958 /* could not determine endianness */
2959 return -1;
2962 #define PERF_PIPE_HDR_VER0 16
2964 static const size_t attr_pipe_abi_sizes[] = {
2965 [0] = PERF_PIPE_HDR_VER0,
2970 * In the legacy pipe format, there is an implicit assumption that endiannesss
2971 * between host recording the samples, and host parsing the samples is the
2972 * same. This is not always the case given that the pipe output may always be
2973 * redirected into a file and analyzed on a different machine with possibly a
2974 * different endianness and perf_event ABI revsions in the perf tool itself.
2976 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2978 u64 attr_size;
2979 int i;
2981 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2982 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2983 attr_size = bswap_64(hdr_sz);
2984 if (attr_size != hdr_sz)
2985 continue;
2987 ph->needs_swap = true;
2989 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2990 return 0;
2992 return -1;
2995 bool is_perf_magic(u64 magic)
2997 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2998 || magic == __perf_magic2
2999 || magic == __perf_magic2_sw)
3000 return true;
3002 return false;
3005 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3006 bool is_pipe, struct perf_header *ph)
3008 int ret;
3010 /* check for legacy format */
3011 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3012 if (ret == 0) {
3013 ph->version = PERF_HEADER_VERSION_1;
3014 pr_debug("legacy perf.data format\n");
3015 if (is_pipe)
3016 return try_all_pipe_abis(hdr_sz, ph);
3018 return try_all_file_abis(hdr_sz, ph);
3021 * the new magic number serves two purposes:
3022 * - unique number to identify actual perf.data files
3023 * - encode endianness of file
3025 ph->version = PERF_HEADER_VERSION_2;
3027 /* check magic number with one endianness */
3028 if (magic == __perf_magic2)
3029 return 0;
3031 /* check magic number with opposite endianness */
3032 if (magic != __perf_magic2_sw)
3033 return -1;
3035 ph->needs_swap = true;
3037 return 0;
3040 int perf_file_header__read(struct perf_file_header *header,
3041 struct perf_header *ph, int fd)
3043 ssize_t ret;
3045 lseek(fd, 0, SEEK_SET);
3047 ret = readn(fd, header, sizeof(*header));
3048 if (ret <= 0)
3049 return -1;
3051 if (check_magic_endian(header->magic,
3052 header->attr_size, false, ph) < 0) {
3053 pr_debug("magic/endian check failed\n");
3054 return -1;
3057 if (ph->needs_swap) {
3058 mem_bswap_64(header, offsetof(struct perf_file_header,
3059 adds_features));
3062 if (header->size != sizeof(*header)) {
3063 /* Support the previous format */
3064 if (header->size == offsetof(typeof(*header), adds_features))
3065 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3066 else
3067 return -1;
3068 } else if (ph->needs_swap) {
3070 * feature bitmap is declared as an array of unsigned longs --
3071 * not good since its size can differ between the host that
3072 * generated the data file and the host analyzing the file.
3074 * We need to handle endianness, but we don't know the size of
3075 * the unsigned long where the file was generated. Take a best
3076 * guess at determining it: try 64-bit swap first (ie., file
3077 * created on a 64-bit host), and check if the hostname feature
3078 * bit is set (this feature bit is forced on as of fbe96f2).
3079 * If the bit is not, undo the 64-bit swap and try a 32-bit
3080 * swap. If the hostname bit is still not set (e.g., older data
3081 * file), punt and fallback to the original behavior --
3082 * clearing all feature bits and setting buildid.
3084 mem_bswap_64(&header->adds_features,
3085 BITS_TO_U64(HEADER_FEAT_BITS));
3087 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3088 /* unswap as u64 */
3089 mem_bswap_64(&header->adds_features,
3090 BITS_TO_U64(HEADER_FEAT_BITS));
3092 /* unswap as u32 */
3093 mem_bswap_32(&header->adds_features,
3094 BITS_TO_U32(HEADER_FEAT_BITS));
3097 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3098 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3099 set_bit(HEADER_BUILD_ID, header->adds_features);
3103 memcpy(&ph->adds_features, &header->adds_features,
3104 sizeof(ph->adds_features));
3106 ph->data_offset = header->data.offset;
3107 ph->data_size = header->data.size;
3108 ph->feat_offset = header->data.offset + header->data.size;
3109 return 0;
3112 static int perf_file_section__process(struct perf_file_section *section,
3113 struct perf_header *ph,
3114 int feat, int fd, void *data)
3116 struct feat_fd fdd = {
3117 .fd = fd,
3118 .ph = ph,
3119 .size = section->size,
3120 .offset = section->offset,
3123 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3124 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3125 "%d, continuing...\n", section->offset, feat);
3126 return 0;
3129 if (feat >= HEADER_LAST_FEATURE) {
3130 pr_debug("unknown feature %d, continuing...\n", feat);
3131 return 0;
3134 if (!feat_ops[feat].process)
3135 return 0;
3137 return feat_ops[feat].process(&fdd, data);
3140 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3141 struct perf_header *ph, int fd,
3142 bool repipe)
3144 struct feat_fd ff = {
3145 .fd = STDOUT_FILENO,
3146 .ph = ph,
3148 ssize_t ret;
3150 ret = readn(fd, header, sizeof(*header));
3151 if (ret <= 0)
3152 return -1;
3154 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3155 pr_debug("endian/magic failed\n");
3156 return -1;
3159 if (ph->needs_swap)
3160 header->size = bswap_64(header->size);
3162 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3163 return -1;
3165 return 0;
3168 static int perf_header__read_pipe(struct perf_session *session)
3170 struct perf_header *header = &session->header;
3171 struct perf_pipe_file_header f_header;
3173 if (perf_file_header__read_pipe(&f_header, header,
3174 perf_data__fd(session->data),
3175 session->repipe) < 0) {
3176 pr_debug("incompatible file format\n");
3177 return -EINVAL;
3180 return 0;
3183 static int read_attr(int fd, struct perf_header *ph,
3184 struct perf_file_attr *f_attr)
3186 struct perf_event_attr *attr = &f_attr->attr;
3187 size_t sz, left;
3188 size_t our_sz = sizeof(f_attr->attr);
3189 ssize_t ret;
3191 memset(f_attr, 0, sizeof(*f_attr));
3193 /* read minimal guaranteed structure */
3194 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3195 if (ret <= 0) {
3196 pr_debug("cannot read %d bytes of header attr\n",
3197 PERF_ATTR_SIZE_VER0);
3198 return -1;
3201 /* on file perf_event_attr size */
3202 sz = attr->size;
3204 if (ph->needs_swap)
3205 sz = bswap_32(sz);
3207 if (sz == 0) {
3208 /* assume ABI0 */
3209 sz = PERF_ATTR_SIZE_VER0;
3210 } else if (sz > our_sz) {
3211 pr_debug("file uses a more recent and unsupported ABI"
3212 " (%zu bytes extra)\n", sz - our_sz);
3213 return -1;
3215 /* what we have not yet read and that we know about */
3216 left = sz - PERF_ATTR_SIZE_VER0;
3217 if (left) {
3218 void *ptr = attr;
3219 ptr += PERF_ATTR_SIZE_VER0;
3221 ret = readn(fd, ptr, left);
3223 /* read perf_file_section, ids are read in caller */
3224 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3226 return ret <= 0 ? -1 : 0;
3229 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
3230 struct tep_handle *pevent)
3232 struct tep_event_format *event;
3233 char bf[128];
3235 /* already prepared */
3236 if (evsel->tp_format)
3237 return 0;
3239 if (pevent == NULL) {
3240 pr_debug("broken or missing trace data\n");
3241 return -1;
3244 event = tep_find_event(pevent, evsel->attr.config);
3245 if (event == NULL) {
3246 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
3247 return -1;
3250 if (!evsel->name) {
3251 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3252 evsel->name = strdup(bf);
3253 if (evsel->name == NULL)
3254 return -1;
3257 evsel->tp_format = event;
3258 return 0;
3261 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
3262 struct tep_handle *pevent)
3264 struct perf_evsel *pos;
3266 evlist__for_each_entry(evlist, pos) {
3267 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3268 perf_evsel__prepare_tracepoint_event(pos, pevent))
3269 return -1;
3272 return 0;
3275 int perf_session__read_header(struct perf_session *session)
3277 struct perf_data *data = session->data;
3278 struct perf_header *header = &session->header;
3279 struct perf_file_header f_header;
3280 struct perf_file_attr f_attr;
3281 u64 f_id;
3282 int nr_attrs, nr_ids, i, j;
3283 int fd = perf_data__fd(data);
3285 session->evlist = perf_evlist__new();
3286 if (session->evlist == NULL)
3287 return -ENOMEM;
3289 session->evlist->env = &header->env;
3290 session->machines.host.env = &header->env;
3291 if (perf_data__is_pipe(data))
3292 return perf_header__read_pipe(session);
3294 if (perf_file_header__read(&f_header, header, fd) < 0)
3295 return -EINVAL;
3298 * Sanity check that perf.data was written cleanly; data size is
3299 * initialized to 0 and updated only if the on_exit function is run.
3300 * If data size is still 0 then the file contains only partial
3301 * information. Just warn user and process it as much as it can.
3303 if (f_header.data.size == 0) {
3304 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3305 "Was the 'perf record' command properly terminated?\n",
3306 data->file.path);
3309 nr_attrs = f_header.attrs.size / f_header.attr_size;
3310 lseek(fd, f_header.attrs.offset, SEEK_SET);
3312 for (i = 0; i < nr_attrs; i++) {
3313 struct perf_evsel *evsel;
3314 off_t tmp;
3316 if (read_attr(fd, header, &f_attr) < 0)
3317 goto out_errno;
3319 if (header->needs_swap) {
3320 f_attr.ids.size = bswap_64(f_attr.ids.size);
3321 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3322 perf_event__attr_swap(&f_attr.attr);
3325 tmp = lseek(fd, 0, SEEK_CUR);
3326 evsel = perf_evsel__new(&f_attr.attr);
3328 if (evsel == NULL)
3329 goto out_delete_evlist;
3331 evsel->needs_swap = header->needs_swap;
3333 * Do it before so that if perf_evsel__alloc_id fails, this
3334 * entry gets purged too at perf_evlist__delete().
3336 perf_evlist__add(session->evlist, evsel);
3338 nr_ids = f_attr.ids.size / sizeof(u64);
3340 * We don't have the cpu and thread maps on the header, so
3341 * for allocating the perf_sample_id table we fake 1 cpu and
3342 * hattr->ids threads.
3344 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3345 goto out_delete_evlist;
3347 lseek(fd, f_attr.ids.offset, SEEK_SET);
3349 for (j = 0; j < nr_ids; j++) {
3350 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3351 goto out_errno;
3353 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
3356 lseek(fd, tmp, SEEK_SET);
3359 perf_header__process_sections(header, fd, &session->tevent,
3360 perf_file_section__process);
3362 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3363 session->tevent.pevent))
3364 goto out_delete_evlist;
3366 return 0;
3367 out_errno:
3368 return -errno;
3370 out_delete_evlist:
3371 perf_evlist__delete(session->evlist);
3372 session->evlist = NULL;
3373 return -ENOMEM;
3376 int perf_event__synthesize_attr(struct perf_tool *tool,
3377 struct perf_event_attr *attr, u32 ids, u64 *id,
3378 perf_event__handler_t process)
3380 union perf_event *ev;
3381 size_t size;
3382 int err;
3384 size = sizeof(struct perf_event_attr);
3385 size = PERF_ALIGN(size, sizeof(u64));
3386 size += sizeof(struct perf_event_header);
3387 size += ids * sizeof(u64);
3389 ev = malloc(size);
3391 if (ev == NULL)
3392 return -ENOMEM;
3394 ev->attr.attr = *attr;
3395 memcpy(ev->attr.id, id, ids * sizeof(u64));
3397 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3398 ev->attr.header.size = (u16)size;
3400 if (ev->attr.header.size == size)
3401 err = process(tool, ev, NULL, NULL);
3402 else
3403 err = -E2BIG;
3405 free(ev);
3407 return err;
3410 int perf_event__synthesize_features(struct perf_tool *tool,
3411 struct perf_session *session,
3412 struct perf_evlist *evlist,
3413 perf_event__handler_t process)
3415 struct perf_header *header = &session->header;
3416 struct feat_fd ff;
3417 struct feature_event *fe;
3418 size_t sz, sz_hdr;
3419 int feat, ret;
3421 sz_hdr = sizeof(fe->header);
3422 sz = sizeof(union perf_event);
3423 /* get a nice alignment */
3424 sz = PERF_ALIGN(sz, page_size);
3426 memset(&ff, 0, sizeof(ff));
3428 ff.buf = malloc(sz);
3429 if (!ff.buf)
3430 return -ENOMEM;
3432 ff.size = sz - sz_hdr;
3434 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3435 if (!feat_ops[feat].synthesize) {
3436 pr_debug("No record header feature for header :%d\n", feat);
3437 continue;
3440 ff.offset = sizeof(*fe);
3442 ret = feat_ops[feat].write(&ff, evlist);
3443 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3444 pr_debug("Error writing feature\n");
3445 continue;
3447 /* ff.buf may have changed due to realloc in do_write() */
3448 fe = ff.buf;
3449 memset(fe, 0, sizeof(*fe));
3451 fe->feat_id = feat;
3452 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3453 fe->header.size = ff.offset;
3455 ret = process(tool, ff.buf, NULL, NULL);
3456 if (ret) {
3457 free(ff.buf);
3458 return ret;
3462 /* Send HEADER_LAST_FEATURE mark. */
3463 fe = ff.buf;
3464 fe->feat_id = HEADER_LAST_FEATURE;
3465 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3466 fe->header.size = sizeof(*fe);
3468 ret = process(tool, ff.buf, NULL, NULL);
3470 free(ff.buf);
3471 return ret;
3474 int perf_event__process_feature(struct perf_session *session,
3475 union perf_event *event)
3477 struct perf_tool *tool = session->tool;
3478 struct feat_fd ff = { .fd = 0 };
3479 struct feature_event *fe = (struct feature_event *)event;
3480 int type = fe->header.type;
3481 u64 feat = fe->feat_id;
3483 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3484 pr_warning("invalid record type %d in pipe-mode\n", type);
3485 return 0;
3487 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3488 pr_warning("invalid record type %d in pipe-mode\n", type);
3489 return -1;
3492 if (!feat_ops[feat].process)
3493 return 0;
3495 ff.buf = (void *)fe->data;
3496 ff.size = event->header.size - sizeof(event->header);
3497 ff.ph = &session->header;
3499 if (feat_ops[feat].process(&ff, NULL))
3500 return -1;
3502 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3503 return 0;
3505 if (!feat_ops[feat].full_only ||
3506 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3507 feat_ops[feat].print(&ff, stdout);
3508 } else {
3509 fprintf(stdout, "# %s info available, use -I to display\n",
3510 feat_ops[feat].name);
3513 return 0;
3516 static struct event_update_event *
3517 event_update_event__new(size_t size, u64 type, u64 id)
3519 struct event_update_event *ev;
3521 size += sizeof(*ev);
3522 size = PERF_ALIGN(size, sizeof(u64));
3524 ev = zalloc(size);
3525 if (ev) {
3526 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3527 ev->header.size = (u16)size;
3528 ev->type = type;
3529 ev->id = id;
3531 return ev;
3535 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3536 struct perf_evsel *evsel,
3537 perf_event__handler_t process)
3539 struct event_update_event *ev;
3540 size_t size = strlen(evsel->unit);
3541 int err;
3543 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3544 if (ev == NULL)
3545 return -ENOMEM;
3547 strncpy(ev->data, evsel->unit, size);
3548 err = process(tool, (union perf_event *)ev, NULL, NULL);
3549 free(ev);
3550 return err;
3554 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3555 struct perf_evsel *evsel,
3556 perf_event__handler_t process)
3558 struct event_update_event *ev;
3559 struct event_update_event_scale *ev_data;
3560 int err;
3562 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3563 if (ev == NULL)
3564 return -ENOMEM;
3566 ev_data = (struct event_update_event_scale *) ev->data;
3567 ev_data->scale = evsel->scale;
3568 err = process(tool, (union perf_event*) ev, NULL, NULL);
3569 free(ev);
3570 return err;
3574 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3575 struct perf_evsel *evsel,
3576 perf_event__handler_t process)
3578 struct event_update_event *ev;
3579 size_t len = strlen(evsel->name);
3580 int err;
3582 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3583 if (ev == NULL)
3584 return -ENOMEM;
3586 strncpy(ev->data, evsel->name, len);
3587 err = process(tool, (union perf_event*) ev, NULL, NULL);
3588 free(ev);
3589 return err;
3593 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3594 struct perf_evsel *evsel,
3595 perf_event__handler_t process)
3597 size_t size = sizeof(struct event_update_event);
3598 struct event_update_event *ev;
3599 int max, err;
3600 u16 type;
3602 if (!evsel->own_cpus)
3603 return 0;
3605 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3606 if (!ev)
3607 return -ENOMEM;
3609 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3610 ev->header.size = (u16)size;
3611 ev->type = PERF_EVENT_UPDATE__CPUS;
3612 ev->id = evsel->id[0];
3614 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3615 evsel->own_cpus,
3616 type, max);
3618 err = process(tool, (union perf_event*) ev, NULL, NULL);
3619 free(ev);
3620 return err;
3623 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3625 struct event_update_event *ev = &event->event_update;
3626 struct event_update_event_scale *ev_scale;
3627 struct event_update_event_cpus *ev_cpus;
3628 struct cpu_map *map;
3629 size_t ret;
3631 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3633 switch (ev->type) {
3634 case PERF_EVENT_UPDATE__SCALE:
3635 ev_scale = (struct event_update_event_scale *) ev->data;
3636 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3637 break;
3638 case PERF_EVENT_UPDATE__UNIT:
3639 ret += fprintf(fp, "... unit: %s\n", ev->data);
3640 break;
3641 case PERF_EVENT_UPDATE__NAME:
3642 ret += fprintf(fp, "... name: %s\n", ev->data);
3643 break;
3644 case PERF_EVENT_UPDATE__CPUS:
3645 ev_cpus = (struct event_update_event_cpus *) ev->data;
3646 ret += fprintf(fp, "... ");
3648 map = cpu_map__new_data(&ev_cpus->cpus);
3649 if (map)
3650 ret += cpu_map__fprintf(map, fp);
3651 else
3652 ret += fprintf(fp, "failed to get cpus\n");
3653 break;
3654 default:
3655 ret += fprintf(fp, "... unknown type\n");
3656 break;
3659 return ret;
3662 int perf_event__synthesize_attrs(struct perf_tool *tool,
3663 struct perf_evlist *evlist,
3664 perf_event__handler_t process)
3666 struct perf_evsel *evsel;
3667 int err = 0;
3669 evlist__for_each_entry(evlist, evsel) {
3670 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3671 evsel->id, process);
3672 if (err) {
3673 pr_debug("failed to create perf header attribute\n");
3674 return err;
3678 return err;
3681 static bool has_unit(struct perf_evsel *counter)
3683 return counter->unit && *counter->unit;
3686 static bool has_scale(struct perf_evsel *counter)
3688 return counter->scale != 1;
3691 int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3692 struct perf_evlist *evsel_list,
3693 perf_event__handler_t process,
3694 bool is_pipe)
3696 struct perf_evsel *counter;
3697 int err;
3700 * Synthesize other events stuff not carried within
3701 * attr event - unit, scale, name
3703 evlist__for_each_entry(evsel_list, counter) {
3704 if (!counter->supported)
3705 continue;
3708 * Synthesize unit and scale only if it's defined.
3710 if (has_unit(counter)) {
3711 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3712 if (err < 0) {
3713 pr_err("Couldn't synthesize evsel unit.\n");
3714 return err;
3718 if (has_scale(counter)) {
3719 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3720 if (err < 0) {
3721 pr_err("Couldn't synthesize evsel counter.\n");
3722 return err;
3726 if (counter->own_cpus) {
3727 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3728 if (err < 0) {
3729 pr_err("Couldn't synthesize evsel cpus.\n");
3730 return err;
3735 * Name is needed only for pipe output,
3736 * perf.data carries event names.
3738 if (is_pipe) {
3739 err = perf_event__synthesize_event_update_name(tool, counter, process);
3740 if (err < 0) {
3741 pr_err("Couldn't synthesize evsel name.\n");
3742 return err;
3746 return 0;
3749 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3750 union perf_event *event,
3751 struct perf_evlist **pevlist)
3753 u32 i, ids, n_ids;
3754 struct perf_evsel *evsel;
3755 struct perf_evlist *evlist = *pevlist;
3757 if (evlist == NULL) {
3758 *pevlist = evlist = perf_evlist__new();
3759 if (evlist == NULL)
3760 return -ENOMEM;
3763 evsel = perf_evsel__new(&event->attr.attr);
3764 if (evsel == NULL)
3765 return -ENOMEM;
3767 perf_evlist__add(evlist, evsel);
3769 ids = event->header.size;
3770 ids -= (void *)&event->attr.id - (void *)event;
3771 n_ids = ids / sizeof(u64);
3773 * We don't have the cpu and thread maps on the header, so
3774 * for allocating the perf_sample_id table we fake 1 cpu and
3775 * hattr->ids threads.
3777 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3778 return -ENOMEM;
3780 for (i = 0; i < n_ids; i++) {
3781 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3784 return 0;
3787 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3788 union perf_event *event,
3789 struct perf_evlist **pevlist)
3791 struct event_update_event *ev = &event->event_update;
3792 struct event_update_event_scale *ev_scale;
3793 struct event_update_event_cpus *ev_cpus;
3794 struct perf_evlist *evlist;
3795 struct perf_evsel *evsel;
3796 struct cpu_map *map;
3798 if (!pevlist || *pevlist == NULL)
3799 return -EINVAL;
3801 evlist = *pevlist;
3803 evsel = perf_evlist__id2evsel(evlist, ev->id);
3804 if (evsel == NULL)
3805 return -EINVAL;
3807 switch (ev->type) {
3808 case PERF_EVENT_UPDATE__UNIT:
3809 evsel->unit = strdup(ev->data);
3810 break;
3811 case PERF_EVENT_UPDATE__NAME:
3812 evsel->name = strdup(ev->data);
3813 break;
3814 case PERF_EVENT_UPDATE__SCALE:
3815 ev_scale = (struct event_update_event_scale *) ev->data;
3816 evsel->scale = ev_scale->scale;
3817 break;
3818 case PERF_EVENT_UPDATE__CPUS:
3819 ev_cpus = (struct event_update_event_cpus *) ev->data;
3821 map = cpu_map__new_data(&ev_cpus->cpus);
3822 if (map)
3823 evsel->own_cpus = map;
3824 else
3825 pr_err("failed to get event_update cpus\n");
3826 default:
3827 break;
3830 return 0;
3833 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3834 struct perf_evlist *evlist,
3835 perf_event__handler_t process)
3837 union perf_event ev;
3838 struct tracing_data *tdata;
3839 ssize_t size = 0, aligned_size = 0, padding;
3840 struct feat_fd ff;
3841 int err __maybe_unused = 0;
3844 * We are going to store the size of the data followed
3845 * by the data contents. Since the fd descriptor is a pipe,
3846 * we cannot seek back to store the size of the data once
3847 * we know it. Instead we:
3849 * - write the tracing data to the temp file
3850 * - get/write the data size to pipe
3851 * - write the tracing data from the temp file
3852 * to the pipe
3854 tdata = tracing_data_get(&evlist->entries, fd, true);
3855 if (!tdata)
3856 return -1;
3858 memset(&ev, 0, sizeof(ev));
3860 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3861 size = tdata->size;
3862 aligned_size = PERF_ALIGN(size, sizeof(u64));
3863 padding = aligned_size - size;
3864 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3865 ev.tracing_data.size = aligned_size;
3867 process(tool, &ev, NULL, NULL);
3870 * The put function will copy all the tracing data
3871 * stored in temp file to the pipe.
3873 tracing_data_put(tdata);
3875 ff = (struct feat_fd){ .fd = fd };
3876 if (write_padded(&ff, NULL, 0, padding))
3877 return -1;
3879 return aligned_size;
3882 int perf_event__process_tracing_data(struct perf_session *session,
3883 union perf_event *event)
3885 ssize_t size_read, padding, size = event->tracing_data.size;
3886 int fd = perf_data__fd(session->data);
3887 off_t offset = lseek(fd, 0, SEEK_CUR);
3888 char buf[BUFSIZ];
3890 /* setup for reading amidst mmap */
3891 lseek(fd, offset + sizeof(struct tracing_data_event),
3892 SEEK_SET);
3894 size_read = trace_report(fd, &session->tevent,
3895 session->repipe);
3896 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3898 if (readn(fd, buf, padding) < 0) {
3899 pr_err("%s: reading input file", __func__);
3900 return -1;
3902 if (session->repipe) {
3903 int retw = write(STDOUT_FILENO, buf, padding);
3904 if (retw <= 0 || retw != padding) {
3905 pr_err("%s: repiping tracing data padding", __func__);
3906 return -1;
3910 if (size_read + padding != size) {
3911 pr_err("%s: tracing data size mismatch", __func__);
3912 return -1;
3915 perf_evlist__prepare_tracepoint_events(session->evlist,
3916 session->tevent.pevent);
3918 return size_read + padding;
3921 int perf_event__synthesize_build_id(struct perf_tool *tool,
3922 struct dso *pos, u16 misc,
3923 perf_event__handler_t process,
3924 struct machine *machine)
3926 union perf_event ev;
3927 size_t len;
3928 int err = 0;
3930 if (!pos->hit)
3931 return err;
3933 memset(&ev, 0, sizeof(ev));
3935 len = pos->long_name_len + 1;
3936 len = PERF_ALIGN(len, NAME_ALIGN);
3937 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3938 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3939 ev.build_id.header.misc = misc;
3940 ev.build_id.pid = machine->pid;
3941 ev.build_id.header.size = sizeof(ev.build_id) + len;
3942 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3944 err = process(tool, &ev, NULL, machine);
3946 return err;
3949 int perf_event__process_build_id(struct perf_session *session,
3950 union perf_event *event)
3952 __event_process_build_id(&event->build_id,
3953 event->build_id.filename,
3954 session);
3955 return 0;