blob: 3c0d74fc1ff26a7b52c7a627cf87561bcae9c06a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// 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>
21
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"
42
43#include "sane_ctype.h"
44
45/*
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.
51 *
52 * we check for legacy (PERFFILE) format.
53 */
54static const char *__perf_magic1 = "PERFFILE";
55static const u64 __perf_magic2 = 0x32454c4946524550ULL;
56static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
57
58#define PERF_MAGIC __perf_magic2
59
60const char perf_version_string[] = PERF_VERSION;
61
62struct perf_file_attr {
63 struct perf_event_attr attr;
64 struct perf_file_section ids;
65};
66
67struct 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;
74};
75
76void perf_header__set_feat(struct perf_header *header, int feat)
77{
78 set_bit(feat, header->adds_features);
79}
80
81void perf_header__clear_feat(struct perf_header *header, int feat)
82{
83 clear_bit(feat, header->adds_features);
84}
85
86bool perf_header__has_feat(const struct perf_header *header, int feat)
87{
88 return test_bit(feat, header->adds_features);
89}
90
91static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
92{
93 ssize_t ret = writen(ff->fd, buf, size);
94
95 if (ret != (ssize_t)size)
96 return ret < 0 ? (int)ret : -1;
97 return 0;
98}
99
100static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
101{
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;
106
107 if (size + ff->offset > max_size)
108 return -E2BIG;
109
110 while (size > (new_size - ff->offset))
111 new_size <<= 1;
112 new_size = min(max_size, new_size);
113
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;
120 }
121
122 memcpy(ff->buf + ff->offset, buf, size);
123 ff->offset += size;
124
125 return 0;
126}
127
128/* Return: 0 if succeded, -ERR if failed. */
129int do_write(struct feat_fd *ff, const void *buf, size_t size)
130{
131 if (!ff->buf)
132 return __do_write_fd(ff, buf, size);
133 return __do_write_buf(ff, buf, size);
134}
135
136/* Return: 0 if succeded, -ERR if failed. */
137static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
138{
139 u64 *p = (u64 *) set;
140 int i, ret;
141
142 ret = do_write(ff, &size, sizeof(size));
143 if (ret < 0)
144 return ret;
145
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;
150 }
151
152 return 0;
153}
154
155/* Return: 0 if succeded, -ERR if failed. */
156int write_padded(struct feat_fd *ff, const void *bf,
157 size_t count, size_t count_aligned)
158{
159 static const char zero_buf[NAME_ALIGN];
160 int err = do_write(ff, bf, count);
161
162 if (!err)
163 err = do_write(ff, zero_buf, count_aligned - count);
164
165 return err;
166}
167
168#define string_size(str) \
169 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
170
171/* Return: 0 if succeded, -ERR if failed. */
172static int do_write_string(struct feat_fd *ff, const char *str)
173{
174 u32 len, olen;
175 int ret;
176
177 olen = strlen(str) + 1;
178 len = PERF_ALIGN(olen, NAME_ALIGN);
179
180 /* write len, incl. \0 */
181 ret = do_write(ff, &len, sizeof(len));
182 if (ret < 0)
183 return ret;
184
185 return write_padded(ff, str, olen, len);
186}
187
188static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
189{
190 ssize_t ret = readn(ff->fd, addr, size);
191
192 if (ret != size)
193 return ret < 0 ? (int)ret : -1;
194 return 0;
195}
196
197static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
198{
199 if (size > (ssize_t)ff->size - ff->offset)
200 return -1;
201
202 memcpy(addr, ff->buf + ff->offset, size);
203 ff->offset += size;
204
205 return 0;
206
207}
208
209static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
210{
211 if (!ff->buf)
212 return __do_read_fd(ff, addr, size);
213 return __do_read_buf(ff, addr, size);
214}
215
216static int do_read_u32(struct feat_fd *ff, u32 *addr)
217{
218 int ret;
219
220 ret = __do_read(ff, addr, sizeof(*addr));
221 if (ret)
222 return ret;
223
224 if (ff->ph->needs_swap)
225 *addr = bswap_32(*addr);
226 return 0;
227}
228
229static int do_read_u64(struct feat_fd *ff, u64 *addr)
230{
231 int ret;
232
233 ret = __do_read(ff, addr, sizeof(*addr));
234 if (ret)
235 return ret;
236
237 if (ff->ph->needs_swap)
238 *addr = bswap_64(*addr);
239 return 0;
240}
241
242static char *do_read_string(struct feat_fd *ff)
243{
244 u32 len;
245 char *buf;
246
247 if (do_read_u32(ff, &len))
248 return NULL;
249
250 buf = malloc(len);
251 if (!buf)
252 return NULL;
253
254 if (!__do_read(ff, buf, len)) {
255 /*
256 * strings are padded by zeroes
257 * thus the actual strlen of buf
258 * may be less than len
259 */
260 return buf;
261 }
262
263 free(buf);
264 return NULL;
265}
266
267/* Return: 0 if succeded, -ERR if failed. */
268static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
269{
270 unsigned long *set;
271 u64 size, *p;
272 int i, ret;
273
274 ret = do_read_u64(ff, &size);
275 if (ret)
276 return ret;
277
278 set = bitmap_alloc(size);
279 if (!set)
280 return -ENOMEM;
281
282 p = (u64 *) set;
283
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;
289 }
290 }
291
292 *pset = set;
293 *psize = size;
294 return 0;
295}
296
297static int write_tracing_data(struct feat_fd *ff,
298 struct perf_evlist *evlist)
299{
300 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
301 return -1;
302
303 return read_tracing_data(ff->fd, &evlist->entries);
304}
305
306static int write_build_id(struct feat_fd *ff,
307 struct perf_evlist *evlist __maybe_unused)
308{
309 struct perf_session *session;
310 int err;
311
312 session = container_of(ff->ph, struct perf_session, header);
313
314 if (!perf_session__read_build_ids(session, true))
315 return -1;
316
317 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
318 return -1;
319
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;
324 }
325 perf_session__cache_build_ids(session);
326
327 return 0;
328}
329
330static int write_hostname(struct feat_fd *ff,
331 struct perf_evlist *evlist __maybe_unused)
332{
333 struct utsname uts;
334 int ret;
335
336 ret = uname(&uts);
337 if (ret < 0)
338 return -1;
339
340 return do_write_string(ff, uts.nodename);
341}
342
343static int write_osrelease(struct feat_fd *ff,
344 struct perf_evlist *evlist __maybe_unused)
345{
346 struct utsname uts;
347 int ret;
348
349 ret = uname(&uts);
350 if (ret < 0)
351 return -1;
352
353 return do_write_string(ff, uts.release);
354}
355
356static int write_arch(struct feat_fd *ff,
357 struct perf_evlist *evlist __maybe_unused)
358{
359 struct utsname uts;
360 int ret;
361
362 ret = uname(&uts);
363 if (ret < 0)
364 return -1;
365
366 return do_write_string(ff, uts.machine);
367}
368
369static int write_version(struct feat_fd *ff,
370 struct perf_evlist *evlist __maybe_unused)
371{
372 return do_write_string(ff, perf_version_string);
373}
374
375static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
376{
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;
383
384 if (!search)
385 return -1;
386
387 file = fopen("/proc/cpuinfo", "r");
388 if (!file)
389 return -1;
390
391 while (getline(&buf, &len, file) > 0) {
392 ret = strncmp(buf, search, strlen(search));
393 if (!ret)
394 break;
395 }
396
397 if (ret) {
398 ret = -1;
399 goto done;
400 }
401
402 s = buf;
403
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';
410
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++));
422 }
423 p++;
424 }
425 ret = do_write_string(ff, s);
426done:
427 free(buf);
428 fclose(file);
429 return ret;
430}
431
432static int write_cpudesc(struct feat_fd *ff,
433 struct perf_evlist *evlist __maybe_unused)
434{
435 const char *cpuinfo_procs[] = CPUINFO_PROC;
436 unsigned int i;
437
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;
443 }
444 return -1;
445}
446
447
448static int write_nrcpus(struct feat_fd *ff,
449 struct perf_evlist *evlist __maybe_unused)
450{
451 long nr;
452 u32 nrc, nra;
453 int ret;
454
455 nrc = cpu__max_present_cpu();
456
457 nr = sysconf(_SC_NPROCESSORS_ONLN);
458 if (nr < 0)
459 return -1;
460
461 nra = (u32)(nr & UINT_MAX);
462
463 ret = do_write(ff, &nrc, sizeof(nrc));
464 if (ret < 0)
465 return ret;
466
467 return do_write(ff, &nra, sizeof(nra));
468}
469
470static int write_event_desc(struct feat_fd *ff,
471 struct perf_evlist *evlist)
472{
473 struct perf_evsel *evsel;
474 u32 nre, nri, sz;
475 int ret;
476
477 nre = evlist->nr_entries;
478
479 /*
480 * write number of events
481 */
482 ret = do_write(ff, &nre, sizeof(nre));
483 if (ret < 0)
484 return ret;
485
486 /*
487 * size of perf_event_attr struct
488 */
489 sz = (u32)sizeof(evsel->attr);
490 ret = do_write(ff, &sz, sizeof(sz));
491 if (ret < 0)
492 return ret;
493
494 evlist__for_each_entry(evlist, evsel) {
495 ret = do_write(ff, &evsel->attr, sz);
496 if (ret < 0)
497 return ret;
498 /*
499 * write number of unique id per event
500 * there is one id per instance of an event
501 *
502 * copy into an nri to be independent of the
503 * type of ids,
504 */
505 nri = evsel->ids;
506 ret = do_write(ff, &nri, sizeof(nri));
507 if (ret < 0)
508 return ret;
509
510 /*
511 * write event string as passed on cmdline
512 */
513 ret = do_write_string(ff, perf_evsel__name(evsel));
514 if (ret < 0)
515 return ret;
516 /*
517 * write unique ids for this event
518 */
519 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
520 if (ret < 0)
521 return ret;
522 }
523 return 0;
524}
525
526static int write_cmdline(struct feat_fd *ff,
527 struct perf_evlist *evlist __maybe_unused)
528{
529 char buf[MAXPATHLEN];
530 u32 n;
531 int i, ret;
532
533 /* actual path to perf binary */
534 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
535 if (ret <= 0)
536 return -1;
537
538 /* readlink() does not add null termination */
539 buf[ret] = '\0';
540
541 /* account for binary path */
542 n = perf_env.nr_cmdline + 1;
543
544 ret = do_write(ff, &n, sizeof(n));
545 if (ret < 0)
546 return ret;
547
548 ret = do_write_string(ff, buf);
549 if (ret < 0)
550 return ret;
551
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;
556 }
557 return 0;
558}
559
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"
564
565struct cpu_topo {
566 u32 cpu_nr;
567 u32 core_sib;
568 u32 thread_sib;
569 char **core_siblings;
570 char **thread_siblings;
571};
572
573static int build_cpu_topo(struct cpu_topo *tp, int cpu)
574{
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;
582
583 sprintf(filename, CORE_SIB_FMT, cpu);
584 fp = fopen(filename, "r");
585 if (!fp)
586 goto try_threads;
587
588 sret = getline(&buf, &len, fp);
589 fclose(fp);
590 if (sret <= 0)
591 goto try_threads;
592
593 p = strchr(buf, '\n');
594 if (p)
595 *p = '\0';
596
597 for (i = 0; i < tp->core_sib; i++) {
598 if (!strcmp(buf, tp->core_siblings[i]))
599 break;
600 }
601 if (i == tp->core_sib) {
602 tp->core_siblings[i] = buf;
603 tp->core_sib++;
604 buf = NULL;
605 len = 0;
606 }
607 ret = 0;
608
609try_threads:
610 sprintf(filename, THRD_SIB_FMT, cpu);
611 fp = fopen(filename, "r");
612 if (!fp)
613 goto done;
614
615 if (getline(&buf, &len, fp) <= 0)
616 goto done;
617
618 p = strchr(buf, '\n');
619 if (p)
620 *p = '\0';
621
622 for (i = 0; i < tp->thread_sib; i++) {
623 if (!strcmp(buf, tp->thread_siblings[i]))
624 break;
625 }
626 if (i == tp->thread_sib) {
627 tp->thread_siblings[i] = buf;
628 tp->thread_sib++;
629 buf = NULL;
630 }
631 ret = 0;
632done:
633 if(fp)
634 fclose(fp);
635 free(buf);
636 return ret;
637}
638
639static void free_cpu_topo(struct cpu_topo *tp)
640{
641 u32 i;
642
643 if (!tp)
644 return;
645
646 for (i = 0 ; i < tp->core_sib; i++)
647 zfree(&tp->core_siblings[i]);
648
649 for (i = 0 ; i < tp->thread_sib; i++)
650 zfree(&tp->thread_siblings[i]);
651
652 free(tp);
653}
654
655static struct cpu_topo *build_cpu_topology(void)
656{
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;
664
665 ncpus = cpu__max_present_cpu();
666
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;
672 }
673
674 nr = (u32)(ncpus & UINT_MAX);
675
676 sz = nr * sizeof(char *);
677 addr = calloc(1, sizeof(*tp) + 2 * sz);
678 if (!addr)
679 goto out_free;
680
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;
687
688 for (i = 0; i < nr; i++) {
689 if (!cpu_map__has(map, i))
690 continue;
691
692 ret = build_cpu_topo(tp, i);
693 if (ret < 0)
694 break;
695 }
696
697out_free:
698 cpu_map__put(map);
699 if (ret) {
700 free_cpu_topo(tp);
701 tp = NULL;
702 }
703 return tp;
704}
705
706static int write_cpu_topology(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
708{
709 struct cpu_topo *tp;
710 u32 i;
711 int ret, j;
712
713 tp = build_cpu_topology();
714 if (!tp)
715 return -1;
716
717 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
718 if (ret < 0)
719 goto done;
720
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;
725 }
726 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
727 if (ret < 0)
728 goto done;
729
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;
734 }
735
736 ret = perf_env__read_cpu_topology_map(&perf_env);
737 if (ret < 0)
738 goto done;
739
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;
749 }
750done:
751 free_cpu_topo(tp);
752 return ret;
753}
754
755
756
757static int write_total_mem(struct feat_fd *ff,
758 struct perf_evlist *evlist __maybe_unused)
759{
760 char *buf = NULL;
761 FILE *fp;
762 size_t len = 0;
763 int ret = -1, n;
764 uint64_t mem;
765
766 fp = fopen("/proc/meminfo", "r");
767 if (!fp)
768 return -1;
769
770 while (getline(&buf, &len, fp) > 0) {
771 ret = strncmp(buf, "MemTotal:", 9);
772 if (!ret)
773 break;
774 }
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;
784}
785
786static int write_topo_node(struct feat_fd *ff, int node)
787{
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;
795
796 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
797 fp = fopen(str, "r");
798 if (!fp)
799 return -1;
800
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;
811 }
812
813 fclose(fp);
814 fp = NULL;
815
816 ret = do_write(ff, &mem_total, sizeof(u64));
817 if (ret)
818 goto done;
819
820 ret = do_write(ff, &mem_free, sizeof(u64));
821 if (ret)
822 goto done;
823
824 ret = -1;
825 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
826
827 fp = fopen(str, "r");
828 if (!fp)
829 goto done;
830
831 if (getline(&buf, &len, fp) <= 0)
832 goto done;
833
834 p = strchr(buf, '\n');
835 if (p)
836 *p = '\0';
837
838 ret = do_write_string(ff, buf);
839done:
840 free(buf);
841 if (fp)
842 fclose(fp);
843 return ret;
844}
845
846static int write_numa_topology(struct feat_fd *ff,
847 struct perf_evlist *evlist __maybe_unused)
848{
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;
856
857 fp = fopen("/sys/devices/system/node/online", "r");
858 if (!fp)
859 return -1;
860
861 if (getline(&buf, &len, fp) <= 0)
862 goto done;
863
864 c = strchr(buf, '\n');
865 if (c)
866 *c = '\0';
867
868 node_map = cpu_map__new(buf);
869 if (!node_map)
870 goto done;
871
872 nr = (u32)node_map->nr;
873
874 ret = do_write(ff, &nr, sizeof(nr));
875 if (ret < 0)
876 goto done;
877
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;
883
884 ret = write_topo_node(ff, i);
885 if (ret < 0)
886 break;
887 }
888done:
889 free(buf);
890 fclose(fp);
891 cpu_map__put(node_map);
892 return ret;
893}
894
895/*
896 * File format:
897 *
898 * struct pmu_mappings {
899 * u32 pmu_num;
900 * struct pmu_map {
901 * u32 type;
902 * char name[];
903 * }[pmu_num];
904 * };
905 */
906
907static int write_pmu_mappings(struct feat_fd *ff,
908 struct perf_evlist *evlist __maybe_unused)
909{
910 struct perf_pmu *pmu = NULL;
911 u32 pmu_num = 0;
912 int ret;
913
914 /*
915 * Do a first pass to count number of pmu to avoid lseek so this
916 * works in pipe mode as well.
917 */
918 while ((pmu = perf_pmu__scan(pmu))) {
919 if (!pmu->name)
920 continue;
921 pmu_num++;
922 }
923
924 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
925 if (ret < 0)
926 return ret;
927
928 while ((pmu = perf_pmu__scan(pmu))) {
929 if (!pmu->name)
930 continue;
931
932 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
933 if (ret < 0)
934 return ret;
935
936 ret = do_write_string(ff, pmu->name);
937 if (ret < 0)
938 return ret;
939 }
940
941 return 0;
942}
943
944/*
945 * File format:
946 *
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 * };
955 */
956static int write_group_desc(struct feat_fd *ff,
957 struct perf_evlist *evlist)
958{
959 u32 nr_groups = evlist->nr_groups;
960 struct perf_evsel *evsel;
961 int ret;
962
963 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
964 if (ret < 0)
965 return ret;
966
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;
973
974 ret = do_write_string(ff, name);
975 if (ret < 0)
976 return ret;
977
978 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
979 if (ret < 0)
980 return ret;
981
982 ret = do_write(ff, &nr_members, sizeof(nr_members));
983 if (ret < 0)
984 return ret;
985 }
986 }
987 return 0;
988}
989
990/*
991 * default get_cpuid(): nothing gets recorded
992 * actual implementation must be in arch/$(SRCARCH)/util/header.c
993 */
994int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
995{
996 return -1;
997}
998
999static int write_cpuid(struct feat_fd *ff,
1000 struct perf_evlist *evlist __maybe_unused)
1001{
1002 char buffer[64];
1003 int ret;
1004
1005 ret = get_cpuid(buffer, sizeof(buffer));
1006 if (!ret)
1007 goto write_it;
1008
1009 return -1;
1010write_it:
1011 return do_write_string(ff, buffer);
1012}
1013
1014static int write_branch_stack(struct feat_fd *ff __maybe_unused,
1015 struct perf_evlist *evlist __maybe_unused)
1016{
1017 return 0;
1018}
1019
1020static int write_auxtrace(struct feat_fd *ff,
1021 struct perf_evlist *evlist __maybe_unused)
1022{
1023 struct perf_session *session;
1024 int err;
1025
1026 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
1027 return -1;
1028
1029 session = container_of(ff->ph, struct perf_session, header);
1030
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;
1035}
1036
1037static int cpu_cache_level__sort(const void *a, const void *b)
1038{
1039 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1040 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1041
1042 return cache_a->level - cache_b->level;
1043}
1044
1045static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1046{
1047 if (a->level != b->level)
1048 return false;
1049
1050 if (a->line_size != b->line_size)
1051 return false;
1052
1053 if (a->sets != b->sets)
1054 return false;
1055
1056 if (a->ways != b->ways)
1057 return false;
1058
1059 if (strcmp(a->type, b->type))
1060 return false;
1061
1062 if (strcmp(a->size, b->size))
1063 return false;
1064
1065 if (strcmp(a->map, b->map))
1066 return false;
1067
1068 return true;
1069}
1070
1071static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1072{
1073 char path[PATH_MAX], file[PATH_MAX];
1074 struct stat st;
1075 size_t len;
1076
1077 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1078 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1079
1080 if (stat(file, &st))
1081 return 1;
1082
1083 scnprintf(file, PATH_MAX, "%s/level", path);
1084 if (sysfs__read_int(file, (int *) &cache->level))
1085 return -1;
1086
1087 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1088 if (sysfs__read_int(file, (int *) &cache->line_size))
1089 return -1;
1090
1091 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1092 if (sysfs__read_int(file, (int *) &cache->sets))
1093 return -1;
1094
1095 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1096 if (sysfs__read_int(file, (int *) &cache->ways))
1097 return -1;
1098
1099 scnprintf(file, PATH_MAX, "%s/type", path);
1100 if (sysfs__read_str(file, &cache->type, &len))
1101 return -1;
1102
1103 cache->type[len] = 0;
1104 cache->type = rtrim(cache->type);
1105
1106 scnprintf(file, PATH_MAX, "%s/size", path);
1107 if (sysfs__read_str(file, &cache->size, &len)) {
1108 free(cache->type);
1109 return -1;
1110 }
1111
1112 cache->size[len] = 0;
1113 cache->size = rtrim(cache->size);
1114
1115 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1116 if (sysfs__read_str(file, &cache->map, &len)) {
1117 free(cache->size);
1118 free(cache->type);
1119 return -1;
1120 }
1121
1122 cache->map[len] = 0;
1123 cache->map = rtrim(cache->map);
1124 return 0;
1125}
1126
1127static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1128{
1129 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1130}
1131
1132static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1133{
1134 u32 i, cnt = 0;
1135 long ncpus;
1136 u32 nr, cpu;
1137 u16 level;
1138
1139 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1140 if (ncpus < 0)
1141 return -1;
1142
1143 nr = (u32)(ncpus & UINT_MAX);
1144
1145 for (cpu = 0; cpu < nr; cpu++) {
1146 for (level = 0; level < 10; level++) {
1147 struct cpu_cache_level c;
1148 int err;
1149
1150 err = cpu_cache_level__read(&c, cpu, level);
1151 if (err < 0)
1152 return err;
1153
1154 if (err == 1)
1155 break;
1156
1157 for (i = 0; i < cnt; i++) {
1158 if (cpu_cache_level__cmp(&c, &caches[i]))
1159 break;
1160 }
1161
1162 if (i == cnt)
1163 caches[cnt++] = c;
1164 else
1165 cpu_cache_level__free(&c);
1166
1167 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1168 goto out;
1169 }
1170 }
1171 out:
1172 *cntp = cnt;
1173 return 0;
1174}
1175
1176#define MAX_CACHES (MAX_NR_CPUS * 4)
1177
1178static int write_cache(struct feat_fd *ff,
1179 struct perf_evlist *evlist __maybe_unused)
1180{
1181 struct cpu_cache_level caches[MAX_CACHES];
1182 u32 cnt = 0, i, version = 1;
1183 int ret;
1184
1185 ret = build_caches(caches, MAX_CACHES, &cnt);
1186 if (ret)
1187 goto out;
1188
1189 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1190
1191 ret = do_write(ff, &version, sizeof(u32));
1192 if (ret < 0)
1193 goto out;
1194
1195 ret = do_write(ff, &cnt, sizeof(u32));
1196 if (ret < 0)
1197 goto out;
1198
1199 for (i = 0; i < cnt; i++) {
1200 struct cpu_cache_level *c = &caches[i];
1201
1202 #define _W(v) \
1203 ret = do_write(ff, &c->v, sizeof(u32)); \
1204 if (ret < 0) \
1205 goto out;
1206
1207 _W(level)
1208 _W(line_size)
1209 _W(sets)
1210 _W(ways)
1211 #undef _W
1212
1213 #define _W(v) \
1214 ret = do_write_string(ff, (const char *) c->v); \
1215 if (ret < 0) \
1216 goto out;
1217
1218 _W(type)
1219 _W(size)
1220 _W(map)
1221 #undef _W
1222 }
1223
1224out:
1225 for (i = 0; i < cnt; i++)
1226 cpu_cache_level__free(&caches[i]);
1227 return ret;
1228}
1229
1230static int write_stat(struct feat_fd *ff __maybe_unused,
1231 struct perf_evlist *evlist __maybe_unused)
1232{
1233 return 0;
1234}
1235
1236static int write_sample_time(struct feat_fd *ff,
1237 struct perf_evlist *evlist)
1238{
1239 int ret;
1240
1241 ret = do_write(ff, &evlist->first_sample_time,
1242 sizeof(evlist->first_sample_time));
1243 if (ret < 0)
1244 return ret;
1245
1246 return do_write(ff, &evlist->last_sample_time,
1247 sizeof(evlist->last_sample_time));
1248}
1249
1250
1251static int memory_node__read(struct memory_node *n, unsigned long idx)
1252{
1253 unsigned int phys, size = 0;
1254 char path[PATH_MAX];
1255 struct dirent *ent;
1256 DIR *dir;
1257
1258#define for_each_memory(mem, dir) \
1259 while ((ent = readdir(dir))) \
1260 if (strcmp(ent->d_name, ".") && \
1261 strcmp(ent->d_name, "..") && \
1262 sscanf(ent->d_name, "memory%u", &mem) == 1)
1263
1264 scnprintf(path, PATH_MAX,
1265 "%s/devices/system/node/node%lu",
1266 sysfs__mountpoint(), idx);
1267
1268 dir = opendir(path);
1269 if (!dir) {
1270 pr_warning("failed: cant' open memory sysfs data\n");
1271 return -1;
1272 }
1273
1274 for_each_memory(phys, dir) {
1275 size = max(phys, size);
1276 }
1277
1278 size++;
1279
1280 n->set = bitmap_alloc(size);
1281 if (!n->set) {
1282 closedir(dir);
1283 return -ENOMEM;
1284 }
1285
1286 n->node = idx;
1287 n->size = size;
1288
1289 rewinddir(dir);
1290
1291 for_each_memory(phys, dir) {
1292 set_bit(phys, n->set);
1293 }
1294
1295 closedir(dir);
1296 return 0;
1297}
1298
1299static int memory_node__sort(const void *a, const void *b)
1300{
1301 const struct memory_node *na = a;
1302 const struct memory_node *nb = b;
1303
1304 return na->node - nb->node;
1305}
1306
1307static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1308{
1309 char path[PATH_MAX];
1310 struct dirent *ent;
1311 DIR *dir;
1312 u64 cnt = 0;
1313 int ret = 0;
1314
1315 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1316 sysfs__mountpoint());
1317
1318 dir = opendir(path);
1319 if (!dir) {
1320 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1321 __func__, path);
1322 return -1;
1323 }
1324
1325 while (!ret && (ent = readdir(dir))) {
1326 unsigned int idx;
1327 int r;
1328
1329 if (!strcmp(ent->d_name, ".") ||
1330 !strcmp(ent->d_name, ".."))
1331 continue;
1332
1333 r = sscanf(ent->d_name, "node%u", &idx);
1334 if (r != 1)
1335 continue;
1336
1337 if (WARN_ONCE(cnt >= size,
1338 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1339 return -1;
1340
1341 ret = memory_node__read(&nodes[cnt++], idx);
1342 }
1343
1344 *cntp = cnt;
1345 closedir(dir);
1346
1347 if (!ret)
1348 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1349
1350 return ret;
1351}
1352
1353#define MAX_MEMORY_NODES 2000
1354
1355/*
1356 * The MEM_TOPOLOGY holds physical memory map for every
1357 * node in system. The format of data is as follows:
1358 *
1359 * 0 - version | for future changes
1360 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1361 * 16 - count | number of nodes
1362 *
1363 * For each node we store map of physical indexes for
1364 * each node:
1365 *
1366 * 32 - node id | node index
1367 * 40 - size | size of bitmap
1368 * 48 - bitmap | bitmap of memory indexes that belongs to node
1369 */
1370static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1371 struct perf_evlist *evlist __maybe_unused)
1372{
1373 static struct memory_node nodes[MAX_MEMORY_NODES];
1374 u64 bsize, version = 1, i, nr;
1375 int ret;
1376
1377 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1378 (unsigned long long *) &bsize);
1379 if (ret)
1380 return ret;
1381
1382 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1383 if (ret)
1384 return ret;
1385
1386 ret = do_write(ff, &version, sizeof(version));
1387 if (ret < 0)
1388 goto out;
1389
1390 ret = do_write(ff, &bsize, sizeof(bsize));
1391 if (ret < 0)
1392 goto out;
1393
1394 ret = do_write(ff, &nr, sizeof(nr));
1395 if (ret < 0)
1396 goto out;
1397
1398 for (i = 0; i < nr; i++) {
1399 struct memory_node *n = &nodes[i];
1400
1401 #define _W(v) \
1402 ret = do_write(ff, &n->v, sizeof(n->v)); \
1403 if (ret < 0) \
1404 goto out;
1405
1406 _W(node)
1407 _W(size)
1408
1409 #undef _W
1410
1411 ret = do_write_bitmap(ff, n->set, n->size);
1412 if (ret < 0)
1413 goto out;
1414 }
1415
1416out:
1417 return ret;
1418}
1419
1420static void print_hostname(struct feat_fd *ff, FILE *fp)
1421{
1422 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1423}
1424
1425static void print_osrelease(struct feat_fd *ff, FILE *fp)
1426{
1427 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1428}
1429
1430static void print_arch(struct feat_fd *ff, FILE *fp)
1431{
1432 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1433}
1434
1435static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1436{
1437 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1438}
1439
1440static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1441{
1442 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1443 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1444}
1445
1446static void print_version(struct feat_fd *ff, FILE *fp)
1447{
1448 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1449}
1450
1451static void print_cmdline(struct feat_fd *ff, FILE *fp)
1452{
1453 int nr, i;
1454
1455 nr = ff->ph->env.nr_cmdline;
1456
1457 fprintf(fp, "# cmdline : ");
1458
1459 for (i = 0; i < nr; i++) {
1460 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1461 if (!argv_i) {
1462 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1463 } else {
1464 char *mem = argv_i;
1465 do {
1466 char *quote = strchr(argv_i, '\'');
1467 if (!quote)
1468 break;
1469 *quote++ = '\0';
1470 fprintf(fp, "%s\\\'", argv_i);
1471 argv_i = quote;
1472 } while (1);
1473 fprintf(fp, "%s ", argv_i);
1474 free(mem);
1475 }
1476 }
1477 fputc('\n', fp);
1478}
1479
1480static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1481{
1482 struct perf_header *ph = ff->ph;
1483 int cpu_nr = ph->env.nr_cpus_avail;
1484 int nr, i;
1485 char *str;
1486
1487 nr = ph->env.nr_sibling_cores;
1488 str = ph->env.sibling_cores;
1489
1490 for (i = 0; i < nr; i++) {
1491 fprintf(fp, "# sibling cores : %s\n", str);
1492 str += strlen(str) + 1;
1493 }
1494
1495 nr = ph->env.nr_sibling_threads;
1496 str = ph->env.sibling_threads;
1497
1498 for (i = 0; i < nr; i++) {
1499 fprintf(fp, "# sibling threads : %s\n", str);
1500 str += strlen(str) + 1;
1501 }
1502
1503 if (ph->env.cpu != NULL) {
1504 for (i = 0; i < cpu_nr; i++)
1505 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1506 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1507 } else
1508 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1509}
1510
1511static void free_event_desc(struct perf_evsel *events)
1512{
1513 struct perf_evsel *evsel;
1514
1515 if (!events)
1516 return;
1517
1518 for (evsel = events; evsel->attr.size; evsel++) {
1519 zfree(&evsel->name);
1520 zfree(&evsel->id);
1521 }
1522
1523 free(events);
1524}
1525
1526static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1527{
1528 struct perf_evsel *evsel, *events = NULL;
1529 u64 *id;
1530 void *buf = NULL;
1531 u32 nre, sz, nr, i, j;
1532 size_t msz;
1533
1534 /* number of events */
1535 if (do_read_u32(ff, &nre))
1536 goto error;
1537
1538 if (do_read_u32(ff, &sz))
1539 goto error;
1540
1541 /* buffer to hold on file attr struct */
1542 buf = malloc(sz);
1543 if (!buf)
1544 goto error;
1545
1546 /* the last event terminates with evsel->attr.size == 0: */
1547 events = calloc(nre + 1, sizeof(*events));
1548 if (!events)
1549 goto error;
1550
1551 msz = sizeof(evsel->attr);
1552 if (sz < msz)
1553 msz = sz;
1554
1555 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1556 evsel->idx = i;
1557
1558 /*
1559 * must read entire on-file attr struct to
1560 * sync up with layout.
1561 */
1562 if (__do_read(ff, buf, sz))
1563 goto error;
1564
1565 if (ff->ph->needs_swap)
1566 perf_event__attr_swap(buf);
1567
1568 memcpy(&evsel->attr, buf, msz);
1569
1570 if (do_read_u32(ff, &nr))
1571 goto error;
1572
1573 if (ff->ph->needs_swap)
1574 evsel->needs_swap = true;
1575
1576 evsel->name = do_read_string(ff);
1577 if (!evsel->name)
1578 goto error;
1579
1580 if (!nr)
1581 continue;
1582
1583 id = calloc(nr, sizeof(*id));
1584 if (!id)
1585 goto error;
1586 evsel->ids = nr;
1587 evsel->id = id;
1588
1589 for (j = 0 ; j < nr; j++) {
1590 if (do_read_u64(ff, id))
1591 goto error;
1592 id++;
1593 }
1594 }
1595out:
1596 free(buf);
1597 return events;
1598error:
1599 free_event_desc(events);
1600 events = NULL;
1601 goto out;
1602}
1603
1604static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1605 void *priv __maybe_unused)
1606{
1607 return fprintf(fp, ", %s = %s", name, val);
1608}
1609
1610static void print_event_desc(struct feat_fd *ff, FILE *fp)
1611{
1612 struct perf_evsel *evsel, *events;
1613 u32 j;
1614 u64 *id;
1615
1616 if (ff->events)
1617 events = ff->events;
1618 else
1619 events = read_event_desc(ff);
1620
1621 if (!events) {
1622 fprintf(fp, "# event desc: not available or unable to read\n");
1623 return;
1624 }
1625
1626 for (evsel = events; evsel->attr.size; evsel++) {
1627 fprintf(fp, "# event : name = %s, ", evsel->name);
1628
1629 if (evsel->ids) {
1630 fprintf(fp, ", id = {");
1631 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1632 if (j)
1633 fputc(',', fp);
1634 fprintf(fp, " %"PRIu64, *id);
1635 }
1636 fprintf(fp, " }");
1637 }
1638
1639 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1640
1641 fputc('\n', fp);
1642 }
1643
1644 free_event_desc(events);
1645 ff->events = NULL;
1646}
1647
1648static void print_total_mem(struct feat_fd *ff, FILE *fp)
1649{
1650 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1651}
1652
1653static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1654{
1655 int i;
1656 struct numa_node *n;
1657
1658 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1659 n = &ff->ph->env.numa_nodes[i];
1660
1661 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1662 " free = %"PRIu64" kB\n",
1663 n->node, n->mem_total, n->mem_free);
1664
1665 fprintf(fp, "# node%u cpu list : ", n->node);
1666 cpu_map__fprintf(n->map, fp);
1667 }
1668}
1669
1670static void print_cpuid(struct feat_fd *ff, FILE *fp)
1671{
1672 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1673}
1674
1675static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1676{
1677 fprintf(fp, "# contains samples with branch stack\n");
1678}
1679
1680static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1681{
1682 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1683}
1684
1685static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1686{
1687 fprintf(fp, "# contains stat data\n");
1688}
1689
1690static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1691{
1692 int i;
1693
1694 fprintf(fp, "# CPU cache info:\n");
1695 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1696 fprintf(fp, "# ");
1697 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1698 }
1699}
1700
1701static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1702{
1703 const char *delimiter = "# pmu mappings: ";
1704 char *str, *tmp;
1705 u32 pmu_num;
1706 u32 type;
1707
1708 pmu_num = ff->ph->env.nr_pmu_mappings;
1709 if (!pmu_num) {
1710 fprintf(fp, "# pmu mappings: not available\n");
1711 return;
1712 }
1713
1714 str = ff->ph->env.pmu_mappings;
1715
1716 while (pmu_num) {
1717 type = strtoul(str, &tmp, 0);
1718 if (*tmp != ':')
1719 goto error;
1720
1721 str = tmp + 1;
1722 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1723
1724 delimiter = ", ";
1725 str += strlen(str) + 1;
1726 pmu_num--;
1727 }
1728
1729 fprintf(fp, "\n");
1730
1731 if (!pmu_num)
1732 return;
1733error:
1734 fprintf(fp, "# pmu mappings: unable to read\n");
1735}
1736
1737static void print_group_desc(struct feat_fd *ff, FILE *fp)
1738{
1739 struct perf_session *session;
1740 struct perf_evsel *evsel;
1741 u32 nr = 0;
1742
1743 session = container_of(ff->ph, struct perf_session, header);
1744
1745 evlist__for_each_entry(session->evlist, evsel) {
1746 if (perf_evsel__is_group_leader(evsel) &&
1747 evsel->nr_members > 1) {
1748 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1749 perf_evsel__name(evsel));
1750
1751 nr = evsel->nr_members - 1;
1752 } else if (nr) {
1753 fprintf(fp, ",%s", perf_evsel__name(evsel));
1754
1755 if (--nr == 0)
1756 fprintf(fp, "}\n");
1757 }
1758 }
1759}
1760
1761static void print_sample_time(struct feat_fd *ff, FILE *fp)
1762{
1763 struct perf_session *session;
1764 char time_buf[32];
1765 double d;
1766
1767 session = container_of(ff->ph, struct perf_session, header);
1768
1769 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1770 time_buf, sizeof(time_buf));
1771 fprintf(fp, "# time of first sample : %s\n", time_buf);
1772
1773 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1774 time_buf, sizeof(time_buf));
1775 fprintf(fp, "# time of last sample : %s\n", time_buf);
1776
1777 d = (double)(session->evlist->last_sample_time -
1778 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1779
1780 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1781}
1782
1783static void memory_node__fprintf(struct memory_node *n,
1784 unsigned long long bsize, FILE *fp)
1785{
1786 char buf_map[100], buf_size[50];
1787 unsigned long long size;
1788
1789 size = bsize * bitmap_weight(n->set, n->size);
1790 unit_number__scnprintf(buf_size, 50, size);
1791
1792 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1793 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1794}
1795
1796static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1797{
1798 struct memory_node *nodes;
1799 int i, nr;
1800
1801 nodes = ff->ph->env.memory_nodes;
1802 nr = ff->ph->env.nr_memory_nodes;
1803
1804 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1805 nr, ff->ph->env.memory_bsize);
1806
1807 for (i = 0; i < nr; i++) {
1808 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1809 }
1810}
1811
1812static int __event_process_build_id(struct build_id_event *bev,
1813 char *filename,
1814 struct perf_session *session)
1815{
1816 int err = -1;
1817 struct machine *machine;
1818 u16 cpumode;
1819 struct dso *dso;
1820 enum dso_kernel_type dso_type;
1821
1822 machine = perf_session__findnew_machine(session, bev->pid);
1823 if (!machine)
1824 goto out;
1825
1826 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1827
1828 switch (cpumode) {
1829 case PERF_RECORD_MISC_KERNEL:
1830 dso_type = DSO_TYPE_KERNEL;
1831 break;
1832 case PERF_RECORD_MISC_GUEST_KERNEL:
1833 dso_type = DSO_TYPE_GUEST_KERNEL;
1834 break;
1835 case PERF_RECORD_MISC_USER:
1836 case PERF_RECORD_MISC_GUEST_USER:
1837 dso_type = DSO_TYPE_USER;
1838 break;
1839 default:
1840 goto out;
1841 }
1842
1843 dso = machine__findnew_dso(machine, filename);
1844 if (dso != NULL) {
1845 char sbuild_id[SBUILD_ID_SIZE];
1846
1847 dso__set_build_id(dso, &bev->build_id);
1848
1849 if (dso_type != DSO_TYPE_USER) {
1850 struct kmod_path m = { .name = NULL, };
1851
1852 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1853 dso__set_module_info(dso, &m, machine);
1854 else
1855 dso->kernel = dso_type;
1856
1857 free(m.name);
1858 }
1859
1860 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1861 sbuild_id);
1862 pr_debug("build id event received for %s: %s\n",
1863 dso->long_name, sbuild_id);
1864 dso__put(dso);
1865 }
1866
1867 err = 0;
1868out:
1869 return err;
1870}
1871
1872static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1873 int input, u64 offset, u64 size)
1874{
1875 struct perf_session *session = container_of(header, struct perf_session, header);
1876 struct {
1877 struct perf_event_header header;
1878 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1879 char filename[0];
1880 } old_bev;
1881 struct build_id_event bev;
1882 char filename[PATH_MAX];
1883 u64 limit = offset + size;
1884
1885 while (offset < limit) {
1886 ssize_t len;
1887
1888 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1889 return -1;
1890
1891 if (header->needs_swap)
1892 perf_event_header__bswap(&old_bev.header);
1893
1894 len = old_bev.header.size - sizeof(old_bev);
1895 if (readn(input, filename, len) != len)
1896 return -1;
1897
1898 bev.header = old_bev.header;
1899
1900 /*
1901 * As the pid is the missing value, we need to fill
1902 * it properly. The header.misc value give us nice hint.
1903 */
1904 bev.pid = HOST_KERNEL_ID;
1905 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1906 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1907 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1908
1909 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1910 __event_process_build_id(&bev, filename, session);
1911
1912 offset += bev.header.size;
1913 }
1914
1915 return 0;
1916}
1917
1918static int perf_header__read_build_ids(struct perf_header *header,
1919 int input, u64 offset, u64 size)
1920{
1921 struct perf_session *session = container_of(header, struct perf_session, header);
1922 struct build_id_event bev;
1923 char filename[PATH_MAX];
1924 u64 limit = offset + size, orig_offset = offset;
1925 int err = -1;
1926
1927 while (offset < limit) {
1928 ssize_t len;
1929
1930 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1931 goto out;
1932
1933 if (header->needs_swap)
1934 perf_event_header__bswap(&bev.header);
1935
1936 len = bev.header.size - sizeof(bev);
1937 if (readn(input, filename, len) != len)
1938 goto out;
1939 /*
1940 * The a1645ce1 changeset:
1941 *
1942 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1943 *
1944 * Added a field to struct build_id_event that broke the file
1945 * format.
1946 *
1947 * Since the kernel build-id is the first entry, process the
1948 * table using the old format if the well known
1949 * '[kernel.kallsyms]' string for the kernel build-id has the
1950 * first 4 characters chopped off (where the pid_t sits).
1951 */
1952 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1953 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1954 return -1;
1955 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1956 }
1957
1958 __event_process_build_id(&bev, filename, session);
1959
1960 offset += bev.header.size;
1961 }
1962 err = 0;
1963out:
1964 return err;
1965}
1966
1967/* Macro for features that simply need to read and store a string. */
1968#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1969static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1970{\
1971 ff->ph->env.__feat_env = do_read_string(ff); \
1972 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1973}
1974
1975FEAT_PROCESS_STR_FUN(hostname, hostname);
1976FEAT_PROCESS_STR_FUN(osrelease, os_release);
1977FEAT_PROCESS_STR_FUN(version, version);
1978FEAT_PROCESS_STR_FUN(arch, arch);
1979FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1980FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1981
1982static int process_tracing_data(struct feat_fd *ff, void *data)
1983{
1984 ssize_t ret = trace_report(ff->fd, data, false);
1985
1986 return ret < 0 ? -1 : 0;
1987}
1988
1989static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1990{
1991 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1992 pr_debug("Failed to read buildids, continuing...\n");
1993 return 0;
1994}
1995
1996static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1997{
1998 int ret;
1999 u32 nr_cpus_avail, nr_cpus_online;
2000
2001 ret = do_read_u32(ff, &nr_cpus_avail);
2002 if (ret)
2003 return ret;
2004
2005 ret = do_read_u32(ff, &nr_cpus_online);
2006 if (ret)
2007 return ret;
2008 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2009 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2010 return 0;
2011}
2012
2013static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2014{
2015 u64 total_mem;
2016 int ret;
2017
2018 ret = do_read_u64(ff, &total_mem);
2019 if (ret)
2020 return -1;
2021 ff->ph->env.total_mem = (unsigned long long)total_mem;
2022 return 0;
2023}
2024
2025static struct perf_evsel *
2026perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2027{
2028 struct perf_evsel *evsel;
2029
2030 evlist__for_each_entry(evlist, evsel) {
2031 if (evsel->idx == idx)
2032 return evsel;
2033 }
2034
2035 return NULL;
2036}
2037
2038static void
2039perf_evlist__set_event_name(struct perf_evlist *evlist,
2040 struct perf_evsel *event)
2041{
2042 struct perf_evsel *evsel;
2043
2044 if (!event->name)
2045 return;
2046
2047 evsel = perf_evlist__find_by_index(evlist, event->idx);
2048 if (!evsel)
2049 return;
2050
2051 if (evsel->name)
2052 return;
2053
2054 evsel->name = strdup(event->name);
2055}
2056
2057static int
2058process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2059{
2060 struct perf_session *session;
2061 struct perf_evsel *evsel, *events = read_event_desc(ff);
2062
2063 if (!events)
2064 return 0;
2065
2066 session = container_of(ff->ph, struct perf_session, header);
2067
2068 if (session->data->is_pipe) {
2069 /* Save events for reading later by print_event_desc,
2070 * since they can't be read again in pipe mode. */
2071 ff->events = events;
2072 }
2073
2074 for (evsel = events; evsel->attr.size; evsel++)
2075 perf_evlist__set_event_name(session->evlist, evsel);
2076
2077 if (!session->data->is_pipe)
2078 free_event_desc(events);
2079
2080 return 0;
2081}
2082
2083static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2084{
2085 char *str, *cmdline = NULL, **argv = NULL;
2086 u32 nr, i, len = 0;
2087
2088 if (do_read_u32(ff, &nr))
2089 return -1;
2090
2091 ff->ph->env.nr_cmdline = nr;
2092
2093 cmdline = zalloc(ff->size + nr + 1);
2094 if (!cmdline)
2095 return -1;
2096
2097 argv = zalloc(sizeof(char *) * (nr + 1));
2098 if (!argv)
2099 goto error;
2100
2101 for (i = 0; i < nr; i++) {
2102 str = do_read_string(ff);
2103 if (!str)
2104 goto error;
2105
2106 argv[i] = cmdline + len;
2107 memcpy(argv[i], str, strlen(str) + 1);
2108 len += strlen(str) + 1;
2109 free(str);
2110 }
2111 ff->ph->env.cmdline = cmdline;
2112 ff->ph->env.cmdline_argv = (const char **) argv;
2113 return 0;
2114
2115error:
2116 free(argv);
2117 free(cmdline);
2118 return -1;
2119}
2120
2121static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2122{
2123 u32 nr, i;
2124 char *str;
2125 struct strbuf sb;
2126 int cpu_nr = ff->ph->env.nr_cpus_avail;
2127 u64 size = 0;
2128 struct perf_header *ph = ff->ph;
2129 bool do_core_id_test = true;
2130
2131 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2132 if (!ph->env.cpu)
2133 return -1;
2134
2135 if (do_read_u32(ff, &nr))
2136 goto free_cpu;
2137
2138 ph->env.nr_sibling_cores = nr;
2139 size += sizeof(u32);
2140 if (strbuf_init(&sb, 128) < 0)
2141 goto free_cpu;
2142
2143 for (i = 0; i < nr; i++) {
2144 str = do_read_string(ff);
2145 if (!str)
2146 goto error;
2147
2148 /* include a NULL character at the end */
2149 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2150 goto error;
2151 size += string_size(str);
2152 free(str);
2153 }
2154 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2155
2156 if (do_read_u32(ff, &nr))
2157 return -1;
2158
2159 ph->env.nr_sibling_threads = nr;
2160 size += sizeof(u32);
2161
2162 for (i = 0; i < nr; i++) {
2163 str = do_read_string(ff);
2164 if (!str)
2165 goto error;
2166
2167 /* include a NULL character at the end */
2168 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2169 goto error;
2170 size += string_size(str);
2171 free(str);
2172 }
2173 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2174
2175 /*
2176 * The header may be from old perf,
2177 * which doesn't include core id and socket id information.
2178 */
2179 if (ff->size <= size) {
2180 zfree(&ph->env.cpu);
2181 return 0;
2182 }
2183
2184 /* On s390 the socket_id number is not related to the numbers of cpus.
2185 * The socket_id number might be higher than the numbers of cpus.
2186 * This depends on the configuration.
2187 * AArch64 is the same.
2188 */
2189 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2190 || !strncmp(ph->env.arch, "aarch64", 7)))
2191 do_core_id_test = false;
2192
2193 for (i = 0; i < (u32)cpu_nr; i++) {
2194 if (do_read_u32(ff, &nr))
2195 goto free_cpu;
2196
2197 ph->env.cpu[i].core_id = nr;
2198
2199 if (do_read_u32(ff, &nr))
2200 goto free_cpu;
2201
2202 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2203 pr_debug("socket_id number is too big."
2204 "You may need to upgrade the perf tool.\n");
2205 goto free_cpu;
2206 }
2207
2208 ph->env.cpu[i].socket_id = nr;
2209 }
2210
2211 return 0;
2212
2213error:
2214 strbuf_release(&sb);
2215free_cpu:
2216 zfree(&ph->env.cpu);
2217 return -1;
2218}
2219
2220static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2221{
2222 struct numa_node *nodes, *n;
2223 u32 nr, i;
2224 char *str;
2225
2226 /* nr nodes */
2227 if (do_read_u32(ff, &nr))
2228 return -1;
2229
2230 nodes = zalloc(sizeof(*nodes) * nr);
2231 if (!nodes)
2232 return -ENOMEM;
2233
2234 for (i = 0; i < nr; i++) {
2235 n = &nodes[i];
2236
2237 /* node number */
2238 if (do_read_u32(ff, &n->node))
2239 goto error;
2240
2241 if (do_read_u64(ff, &n->mem_total))
2242 goto error;
2243
2244 if (do_read_u64(ff, &n->mem_free))
2245 goto error;
2246
2247 str = do_read_string(ff);
2248 if (!str)
2249 goto error;
2250
2251 n->map = cpu_map__new(str);
2252 if (!n->map)
2253 goto error;
2254
2255 free(str);
2256 }
2257 ff->ph->env.nr_numa_nodes = nr;
2258 ff->ph->env.numa_nodes = nodes;
2259 return 0;
2260
2261error:
2262 free(nodes);
2263 return -1;
2264}
2265
2266static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2267{
2268 char *name;
2269 u32 pmu_num;
2270 u32 type;
2271 struct strbuf sb;
2272
2273 if (do_read_u32(ff, &pmu_num))
2274 return -1;
2275
2276 if (!pmu_num) {
2277 pr_debug("pmu mappings not available\n");
2278 return 0;
2279 }
2280
2281 ff->ph->env.nr_pmu_mappings = pmu_num;
2282 if (strbuf_init(&sb, 128) < 0)
2283 return -1;
2284
2285 while (pmu_num) {
2286 if (do_read_u32(ff, &type))
2287 goto error;
2288
2289 name = do_read_string(ff);
2290 if (!name)
2291 goto error;
2292
2293 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2294 goto error;
2295 /* include a NULL character at the end */
2296 if (strbuf_add(&sb, "", 1) < 0)
2297 goto error;
2298
2299 if (!strcmp(name, "msr"))
2300 ff->ph->env.msr_pmu_type = type;
2301
2302 free(name);
2303 pmu_num--;
2304 }
2305 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2306 return 0;
2307
2308error:
2309 strbuf_release(&sb);
2310 return -1;
2311}
2312
2313static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2314{
2315 size_t ret = -1;
2316 u32 i, nr, nr_groups;
2317 struct perf_session *session;
2318 struct perf_evsel *evsel, *leader = NULL;
2319 struct group_desc {
2320 char *name;
2321 u32 leader_idx;
2322 u32 nr_members;
2323 } *desc;
2324
2325 if (do_read_u32(ff, &nr_groups))
2326 return -1;
2327
2328 ff->ph->env.nr_groups = nr_groups;
2329 if (!nr_groups) {
2330 pr_debug("group desc not available\n");
2331 return 0;
2332 }
2333
2334 desc = calloc(nr_groups, sizeof(*desc));
2335 if (!desc)
2336 return -1;
2337
2338 for (i = 0; i < nr_groups; i++) {
2339 desc[i].name = do_read_string(ff);
2340 if (!desc[i].name)
2341 goto out_free;
2342
2343 if (do_read_u32(ff, &desc[i].leader_idx))
2344 goto out_free;
2345
2346 if (do_read_u32(ff, &desc[i].nr_members))
2347 goto out_free;
2348 }
2349
2350 /*
2351 * Rebuild group relationship based on the group_desc
2352 */
2353 session = container_of(ff->ph, struct perf_session, header);
2354 session->evlist->nr_groups = nr_groups;
2355
2356 i = nr = 0;
2357 evlist__for_each_entry(session->evlist, evsel) {
2358 if (evsel->idx == (int) desc[i].leader_idx) {
2359 evsel->leader = evsel;
2360 /* {anon_group} is a dummy name */
2361 if (strcmp(desc[i].name, "{anon_group}")) {
2362 evsel->group_name = desc[i].name;
2363 desc[i].name = NULL;
2364 }
2365 evsel->nr_members = desc[i].nr_members;
2366
2367 if (i >= nr_groups || nr > 0) {
2368 pr_debug("invalid group desc\n");
2369 goto out_free;
2370 }
2371
2372 leader = evsel;
2373 nr = evsel->nr_members - 1;
2374 i++;
2375 } else if (nr) {
2376 /* This is a group member */
2377 evsel->leader = leader;
2378
2379 nr--;
2380 }
2381 }
2382
2383 if (i != nr_groups || nr != 0) {
2384 pr_debug("invalid group desc\n");
2385 goto out_free;
2386 }
2387
2388 ret = 0;
2389out_free:
2390 for (i = 0; i < nr_groups; i++)
2391 zfree(&desc[i].name);
2392 free(desc);
2393
2394 return ret;
2395}
2396
2397static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2398{
2399 struct perf_session *session;
2400 int err;
2401
2402 session = container_of(ff->ph, struct perf_session, header);
2403
2404 err = auxtrace_index__process(ff->fd, ff->size, session,
2405 ff->ph->needs_swap);
2406 if (err < 0)
2407 pr_err("Failed to process auxtrace index\n");
2408 return err;
2409}
2410
2411static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2412{
2413 struct cpu_cache_level *caches;
2414 u32 cnt, i, version;
2415
2416 if (do_read_u32(ff, &version))
2417 return -1;
2418
2419 if (version != 1)
2420 return -1;
2421
2422 if (do_read_u32(ff, &cnt))
2423 return -1;
2424
2425 caches = zalloc(sizeof(*caches) * cnt);
2426 if (!caches)
2427 return -1;
2428
2429 for (i = 0; i < cnt; i++) {
2430 struct cpu_cache_level c;
2431
2432 #define _R(v) \
2433 if (do_read_u32(ff, &c.v))\
2434 goto out_free_caches; \
2435
2436 _R(level)
2437 _R(line_size)
2438 _R(sets)
2439 _R(ways)
2440 #undef _R
2441
2442 #define _R(v) \
2443 c.v = do_read_string(ff); \
2444 if (!c.v) \
2445 goto out_free_caches;
2446
2447 _R(type)
2448 _R(size)
2449 _R(map)
2450 #undef _R
2451
2452 caches[i] = c;
2453 }
2454
2455 ff->ph->env.caches = caches;
2456 ff->ph->env.caches_cnt = cnt;
2457 return 0;
2458out_free_caches:
2459 free(caches);
2460 return -1;
2461}
2462
2463static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2464{
2465 struct perf_session *session;
2466 u64 first_sample_time, last_sample_time;
2467 int ret;
2468
2469 session = container_of(ff->ph, struct perf_session, header);
2470
2471 ret = do_read_u64(ff, &first_sample_time);
2472 if (ret)
2473 return -1;
2474
2475 ret = do_read_u64(ff, &last_sample_time);
2476 if (ret)
2477 return -1;
2478
2479 session->evlist->first_sample_time = first_sample_time;
2480 session->evlist->last_sample_time = last_sample_time;
2481 return 0;
2482}
2483
2484static int process_mem_topology(struct feat_fd *ff,
2485 void *data __maybe_unused)
2486{
2487 struct memory_node *nodes;
2488 u64 version, i, nr, bsize;
2489 int ret = -1;
2490
2491 if (do_read_u64(ff, &version))
2492 return -1;
2493
2494 if (version != 1)
2495 return -1;
2496
2497 if (do_read_u64(ff, &bsize))
2498 return -1;
2499
2500 if (do_read_u64(ff, &nr))
2501 return -1;
2502
2503 nodes = zalloc(sizeof(*nodes) * nr);
2504 if (!nodes)
2505 return -1;
2506
2507 for (i = 0; i < nr; i++) {
2508 struct memory_node n;
2509
2510 #define _R(v) \
2511 if (do_read_u64(ff, &n.v)) \
2512 goto out; \
2513
2514 _R(node)
2515 _R(size)
2516
2517 #undef _R
2518
2519 if (do_read_bitmap(ff, &n.set, &n.size))
2520 goto out;
2521
2522 nodes[i] = n;
2523 }
2524
2525 ff->ph->env.memory_bsize = bsize;
2526 ff->ph->env.memory_nodes = nodes;
2527 ff->ph->env.nr_memory_nodes = nr;
2528 ret = 0;
2529
2530out:
2531 if (ret)
2532 free(nodes);
2533 return ret;
2534}
2535
2536struct feature_ops {
2537 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2538 void (*print)(struct feat_fd *ff, FILE *fp);
2539 int (*process)(struct feat_fd *ff, void *data);
2540 const char *name;
2541 bool full_only;
2542 bool synthesize;
2543};
2544
2545#define FEAT_OPR(n, func, __full_only) \
2546 [HEADER_##n] = { \
2547 .name = __stringify(n), \
2548 .write = write_##func, \
2549 .print = print_##func, \
2550 .full_only = __full_only, \
2551 .process = process_##func, \
2552 .synthesize = true \
2553 }
2554
2555#define FEAT_OPN(n, func, __full_only) \
2556 [HEADER_##n] = { \
2557 .name = __stringify(n), \
2558 .write = write_##func, \
2559 .print = print_##func, \
2560 .full_only = __full_only, \
2561 .process = process_##func \
2562 }
2563
2564/* feature_ops not implemented: */
2565#define print_tracing_data NULL
2566#define print_build_id NULL
2567
2568#define process_branch_stack NULL
2569#define process_stat NULL
2570
2571
2572static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2573 FEAT_OPN(TRACING_DATA, tracing_data, false),
2574 FEAT_OPN(BUILD_ID, build_id, false),
2575 FEAT_OPR(HOSTNAME, hostname, false),
2576 FEAT_OPR(OSRELEASE, osrelease, false),
2577 FEAT_OPR(VERSION, version, false),
2578 FEAT_OPR(ARCH, arch, false),
2579 FEAT_OPR(NRCPUS, nrcpus, false),
2580 FEAT_OPR(CPUDESC, cpudesc, false),
2581 FEAT_OPR(CPUID, cpuid, false),
2582 FEAT_OPR(TOTAL_MEM, total_mem, false),
2583 FEAT_OPR(EVENT_DESC, event_desc, false),
2584 FEAT_OPR(CMDLINE, cmdline, false),
2585 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2586 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2587 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2588 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2589 FEAT_OPR(GROUP_DESC, group_desc, false),
2590 FEAT_OPN(AUXTRACE, auxtrace, false),
2591 FEAT_OPN(STAT, stat, false),
2592 FEAT_OPN(CACHE, cache, true),
2593 FEAT_OPR(SAMPLE_TIME, sample_time, false),
2594 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
2595};
2596
2597struct header_print_data {
2598 FILE *fp;
2599 bool full; /* extended list of headers */
2600};
2601
2602static int perf_file_section__fprintf_info(struct perf_file_section *section,
2603 struct perf_header *ph,
2604 int feat, int fd, void *data)
2605{
2606 struct header_print_data *hd = data;
2607 struct feat_fd ff;
2608
2609 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2610 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2611 "%d, continuing...\n", section->offset, feat);
2612 return 0;
2613 }
2614 if (feat >= HEADER_LAST_FEATURE) {
2615 pr_warning("unknown feature %d\n", feat);
2616 return 0;
2617 }
2618 if (!feat_ops[feat].print)
2619 return 0;
2620
2621 ff = (struct feat_fd) {
2622 .fd = fd,
2623 .ph = ph,
2624 };
2625
2626 if (!feat_ops[feat].full_only || hd->full)
2627 feat_ops[feat].print(&ff, hd->fp);
2628 else
2629 fprintf(hd->fp, "# %s info available, use -I to display\n",
2630 feat_ops[feat].name);
2631
2632 return 0;
2633}
2634
2635int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2636{
2637 struct header_print_data hd;
2638 struct perf_header *header = &session->header;
2639 int fd = perf_data__fd(session->data);
2640 struct stat st;
2641 time_t stctime;
2642 int ret, bit;
2643
2644 hd.fp = fp;
2645 hd.full = full;
2646
2647 ret = fstat(fd, &st);
2648 if (ret == -1)
2649 return -1;
2650
2651 stctime = st.st_ctime;
2652 fprintf(fp, "# captured on : %s", ctime(&stctime));
2653
2654 fprintf(fp, "# header version : %u\n", header->version);
2655 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2656 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2657 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
2658
2659 perf_header__process_sections(header, fd, &hd,
2660 perf_file_section__fprintf_info);
2661
2662 if (session->data->is_pipe)
2663 return 0;
2664
2665 fprintf(fp, "# missing features: ");
2666 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2667 if (bit)
2668 fprintf(fp, "%s ", feat_ops[bit].name);
2669 }
2670
2671 fprintf(fp, "\n");
2672 return 0;
2673}
2674
2675static int do_write_feat(struct feat_fd *ff, int type,
2676 struct perf_file_section **p,
2677 struct perf_evlist *evlist)
2678{
2679 int err;
2680 int ret = 0;
2681
2682 if (perf_header__has_feat(ff->ph, type)) {
2683 if (!feat_ops[type].write)
2684 return -1;
2685
2686 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2687 return -1;
2688
2689 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2690
2691 err = feat_ops[type].write(ff, evlist);
2692 if (err < 0) {
2693 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2694
2695 /* undo anything written */
2696 lseek(ff->fd, (*p)->offset, SEEK_SET);
2697
2698 return -1;
2699 }
2700 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2701 (*p)++;
2702 }
2703 return ret;
2704}
2705
2706static int perf_header__adds_write(struct perf_header *header,
2707 struct perf_evlist *evlist, int fd)
2708{
2709 int nr_sections;
2710 struct feat_fd ff;
2711 struct perf_file_section *feat_sec, *p;
2712 int sec_size;
2713 u64 sec_start;
2714 int feat;
2715 int err;
2716
2717 ff = (struct feat_fd){
2718 .fd = fd,
2719 .ph = header,
2720 };
2721
2722 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2723 if (!nr_sections)
2724 return 0;
2725
2726 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2727 if (feat_sec == NULL)
2728 return -ENOMEM;
2729
2730 sec_size = sizeof(*feat_sec) * nr_sections;
2731
2732 sec_start = header->feat_offset;
2733 lseek(fd, sec_start + sec_size, SEEK_SET);
2734
2735 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2736 if (do_write_feat(&ff, feat, &p, evlist))
2737 perf_header__clear_feat(header, feat);
2738 }
2739
2740 lseek(fd, sec_start, SEEK_SET);
2741 /*
2742 * may write more than needed due to dropped feature, but
2743 * this is okay, reader will skip the mising entries
2744 */
2745 err = do_write(&ff, feat_sec, sec_size);
2746 if (err < 0)
2747 pr_debug("failed to write feature section\n");
2748 free(feat_sec);
2749 return err;
2750}
2751
2752int perf_header__write_pipe(int fd)
2753{
2754 struct perf_pipe_file_header f_header;
2755 struct feat_fd ff;
2756 int err;
2757
2758 ff = (struct feat_fd){ .fd = fd };
2759
2760 f_header = (struct perf_pipe_file_header){
2761 .magic = PERF_MAGIC,
2762 .size = sizeof(f_header),
2763 };
2764
2765 err = do_write(&ff, &f_header, sizeof(f_header));
2766 if (err < 0) {
2767 pr_debug("failed to write perf pipe header\n");
2768 return err;
2769 }
2770
2771 return 0;
2772}
2773
2774int perf_session__write_header(struct perf_session *session,
2775 struct perf_evlist *evlist,
2776 int fd, bool at_exit)
2777{
2778 struct perf_file_header f_header;
2779 struct perf_file_attr f_attr;
2780 struct perf_header *header = &session->header;
2781 struct perf_evsel *evsel;
2782 struct feat_fd ff;
2783 u64 attr_offset;
2784 int err;
2785
2786 ff = (struct feat_fd){ .fd = fd};
2787 lseek(fd, sizeof(f_header), SEEK_SET);
2788
2789 evlist__for_each_entry(session->evlist, evsel) {
2790 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2791 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2792 if (err < 0) {
2793 pr_debug("failed to write perf header\n");
2794 return err;
2795 }
2796 }
2797
2798 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2799
2800 evlist__for_each_entry(evlist, evsel) {
2801 f_attr = (struct perf_file_attr){
2802 .attr = evsel->attr,
2803 .ids = {
2804 .offset = evsel->id_offset,
2805 .size = evsel->ids * sizeof(u64),
2806 }
2807 };
2808 err = do_write(&ff, &f_attr, sizeof(f_attr));
2809 if (err < 0) {
2810 pr_debug("failed to write perf header attribute\n");
2811 return err;
2812 }
2813 }
2814
2815 if (!header->data_offset)
2816 header->data_offset = lseek(fd, 0, SEEK_CUR);
2817 header->feat_offset = header->data_offset + header->data_size;
2818
2819 if (at_exit) {
2820 err = perf_header__adds_write(header, evlist, fd);
2821 if (err < 0)
2822 return err;
2823 }
2824
2825 f_header = (struct perf_file_header){
2826 .magic = PERF_MAGIC,
2827 .size = sizeof(f_header),
2828 .attr_size = sizeof(f_attr),
2829 .attrs = {
2830 .offset = attr_offset,
2831 .size = evlist->nr_entries * sizeof(f_attr),
2832 },
2833 .data = {
2834 .offset = header->data_offset,
2835 .size = header->data_size,
2836 },
2837 /* event_types is ignored, store zeros */
2838 };
2839
2840 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2841
2842 lseek(fd, 0, SEEK_SET);
2843 err = do_write(&ff, &f_header, sizeof(f_header));
2844 if (err < 0) {
2845 pr_debug("failed to write perf header\n");
2846 return err;
2847 }
2848 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2849
2850 return 0;
2851}
2852
2853static int perf_header__getbuffer64(struct perf_header *header,
2854 int fd, void *buf, size_t size)
2855{
2856 if (readn(fd, buf, size) <= 0)
2857 return -1;
2858
2859 if (header->needs_swap)
2860 mem_bswap_64(buf, size);
2861
2862 return 0;
2863}
2864
2865int perf_header__process_sections(struct perf_header *header, int fd,
2866 void *data,
2867 int (*process)(struct perf_file_section *section,
2868 struct perf_header *ph,
2869 int feat, int fd, void *data))
2870{
2871 struct perf_file_section *feat_sec, *sec;
2872 int nr_sections;
2873 int sec_size;
2874 int feat;
2875 int err;
2876
2877 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2878 if (!nr_sections)
2879 return 0;
2880
2881 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2882 if (!feat_sec)
2883 return -1;
2884
2885 sec_size = sizeof(*feat_sec) * nr_sections;
2886
2887 lseek(fd, header->feat_offset, SEEK_SET);
2888
2889 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2890 if (err < 0)
2891 goto out_free;
2892
2893 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2894 err = process(sec++, header, feat, fd, data);
2895 if (err < 0)
2896 goto out_free;
2897 }
2898 err = 0;
2899out_free:
2900 free(feat_sec);
2901 return err;
2902}
2903
2904static const int attr_file_abi_sizes[] = {
2905 [0] = PERF_ATTR_SIZE_VER0,
2906 [1] = PERF_ATTR_SIZE_VER1,
2907 [2] = PERF_ATTR_SIZE_VER2,
2908 [3] = PERF_ATTR_SIZE_VER3,
2909 [4] = PERF_ATTR_SIZE_VER4,
2910 0,
2911};
2912
2913/*
2914 * In the legacy file format, the magic number is not used to encode endianness.
2915 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2916 * on ABI revisions, we need to try all combinations for all endianness to
2917 * detect the endianness.
2918 */
2919static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2920{
2921 uint64_t ref_size, attr_size;
2922 int i;
2923
2924 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2925 ref_size = attr_file_abi_sizes[i]
2926 + sizeof(struct perf_file_section);
2927 if (hdr_sz != ref_size) {
2928 attr_size = bswap_64(hdr_sz);
2929 if (attr_size != ref_size)
2930 continue;
2931
2932 ph->needs_swap = true;
2933 }
2934 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2935 i,
2936 ph->needs_swap);
2937 return 0;
2938 }
2939 /* could not determine endianness */
2940 return -1;
2941}
2942
2943#define PERF_PIPE_HDR_VER0 16
2944
2945static const size_t attr_pipe_abi_sizes[] = {
2946 [0] = PERF_PIPE_HDR_VER0,
2947 0,
2948};
2949
2950/*
2951 * In the legacy pipe format, there is an implicit assumption that endiannesss
2952 * between host recording the samples, and host parsing the samples is the
2953 * same. This is not always the case given that the pipe output may always be
2954 * redirected into a file and analyzed on a different machine with possibly a
2955 * different endianness and perf_event ABI revsions in the perf tool itself.
2956 */
2957static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2958{
2959 u64 attr_size;
2960 int i;
2961
2962 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2963 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2964 attr_size = bswap_64(hdr_sz);
2965 if (attr_size != hdr_sz)
2966 continue;
2967
2968 ph->needs_swap = true;
2969 }
2970 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2971 return 0;
2972 }
2973 return -1;
2974}
2975
2976bool is_perf_magic(u64 magic)
2977{
2978 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2979 || magic == __perf_magic2
2980 || magic == __perf_magic2_sw)
2981 return true;
2982
2983 return false;
2984}
2985
2986static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2987 bool is_pipe, struct perf_header *ph)
2988{
2989 int ret;
2990
2991 /* check for legacy format */
2992 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2993 if (ret == 0) {
2994 ph->version = PERF_HEADER_VERSION_1;
2995 pr_debug("legacy perf.data format\n");
2996 if (is_pipe)
2997 return try_all_pipe_abis(hdr_sz, ph);
2998
2999 return try_all_file_abis(hdr_sz, ph);
3000 }
3001 /*
3002 * the new magic number serves two purposes:
3003 * - unique number to identify actual perf.data files
3004 * - encode endianness of file
3005 */
3006 ph->version = PERF_HEADER_VERSION_2;
3007
3008 /* check magic number with one endianness */
3009 if (magic == __perf_magic2)
3010 return 0;
3011
3012 /* check magic number with opposite endianness */
3013 if (magic != __perf_magic2_sw)
3014 return -1;
3015
3016 ph->needs_swap = true;
3017
3018 return 0;
3019}
3020
3021int perf_file_header__read(struct perf_file_header *header,
3022 struct perf_header *ph, int fd)
3023{
3024 ssize_t ret;
3025
3026 lseek(fd, 0, SEEK_SET);
3027
3028 ret = readn(fd, header, sizeof(*header));
3029 if (ret <= 0)
3030 return -1;
3031
3032 if (check_magic_endian(header->magic,
3033 header->attr_size, false, ph) < 0) {
3034 pr_debug("magic/endian check failed\n");
3035 return -1;
3036 }
3037
3038 if (ph->needs_swap) {
3039 mem_bswap_64(header, offsetof(struct perf_file_header,
3040 adds_features));
3041 }
3042
3043 if (header->size != sizeof(*header)) {
3044 /* Support the previous format */
3045 if (header->size == offsetof(typeof(*header), adds_features))
3046 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3047 else
3048 return -1;
3049 } else if (ph->needs_swap) {
3050 /*
3051 * feature bitmap is declared as an array of unsigned longs --
3052 * not good since its size can differ between the host that
3053 * generated the data file and the host analyzing the file.
3054 *
3055 * We need to handle endianness, but we don't know the size of
3056 * the unsigned long where the file was generated. Take a best
3057 * guess at determining it: try 64-bit swap first (ie., file
3058 * created on a 64-bit host), and check if the hostname feature
3059 * bit is set (this feature bit is forced on as of fbe96f2).
3060 * If the bit is not, undo the 64-bit swap and try a 32-bit
3061 * swap. If the hostname bit is still not set (e.g., older data
3062 * file), punt and fallback to the original behavior --
3063 * clearing all feature bits and setting buildid.
3064 */
3065 mem_bswap_64(&header->adds_features,
3066 BITS_TO_U64(HEADER_FEAT_BITS));
3067
3068 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3069 /* unswap as u64 */
3070 mem_bswap_64(&header->adds_features,
3071 BITS_TO_U64(HEADER_FEAT_BITS));
3072
3073 /* unswap as u32 */
3074 mem_bswap_32(&header->adds_features,
3075 BITS_TO_U32(HEADER_FEAT_BITS));
3076 }
3077
3078 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3079 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3080 set_bit(HEADER_BUILD_ID, header->adds_features);
3081 }
3082 }
3083
3084 memcpy(&ph->adds_features, &header->adds_features,
3085 sizeof(ph->adds_features));
3086
3087 ph->data_offset = header->data.offset;
3088 ph->data_size = header->data.size;
3089 ph->feat_offset = header->data.offset + header->data.size;
3090 return 0;
3091}
3092
3093static int perf_file_section__process(struct perf_file_section *section,
3094 struct perf_header *ph,
3095 int feat, int fd, void *data)
3096{
3097 struct feat_fd fdd = {
3098 .fd = fd,
3099 .ph = ph,
3100 .size = section->size,
3101 .offset = section->offset,
3102 };
3103
3104 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3105 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3106 "%d, continuing...\n", section->offset, feat);
3107 return 0;
3108 }
3109
3110 if (feat >= HEADER_LAST_FEATURE) {
3111 pr_debug("unknown feature %d, continuing...\n", feat);
3112 return 0;
3113 }
3114
3115 if (!feat_ops[feat].process)
3116 return 0;
3117
3118 return feat_ops[feat].process(&fdd, data);
3119}
3120
3121static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3122 struct perf_header *ph, int fd,
3123 bool repipe)
3124{
3125 struct feat_fd ff = {
3126 .fd = STDOUT_FILENO,
3127 .ph = ph,
3128 };
3129 ssize_t ret;
3130
3131 ret = readn(fd, header, sizeof(*header));
3132 if (ret <= 0)
3133 return -1;
3134
3135 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3136 pr_debug("endian/magic failed\n");
3137 return -1;
3138 }
3139
3140 if (ph->needs_swap)
3141 header->size = bswap_64(header->size);
3142
3143 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3144 return -1;
3145
3146 return 0;
3147}
3148
3149static int perf_header__read_pipe(struct perf_session *session)
3150{
3151 struct perf_header *header = &session->header;
3152 struct perf_pipe_file_header f_header;
3153
3154 if (perf_file_header__read_pipe(&f_header, header,
3155 perf_data__fd(session->data),
3156 session->repipe) < 0) {
3157 pr_debug("incompatible file format\n");
3158 return -EINVAL;
3159 }
3160
3161 return 0;
3162}
3163
3164static int read_attr(int fd, struct perf_header *ph,
3165 struct perf_file_attr *f_attr)
3166{
3167 struct perf_event_attr *attr = &f_attr->attr;
3168 size_t sz, left;
3169 size_t our_sz = sizeof(f_attr->attr);
3170 ssize_t ret;
3171
3172 memset(f_attr, 0, sizeof(*f_attr));
3173
3174 /* read minimal guaranteed structure */
3175 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3176 if (ret <= 0) {
3177 pr_debug("cannot read %d bytes of header attr\n",
3178 PERF_ATTR_SIZE_VER0);
3179 return -1;
3180 }
3181
3182 /* on file perf_event_attr size */
3183 sz = attr->size;
3184
3185 if (ph->needs_swap)
3186 sz = bswap_32(sz);
3187
3188 if (sz == 0) {
3189 /* assume ABI0 */
3190 sz = PERF_ATTR_SIZE_VER0;
3191 } else if (sz > our_sz) {
3192 pr_debug("file uses a more recent and unsupported ABI"
3193 " (%zu bytes extra)\n", sz - our_sz);
3194 return -1;
3195 }
3196 /* what we have not yet read and that we know about */
3197 left = sz - PERF_ATTR_SIZE_VER0;
3198 if (left) {
3199 void *ptr = attr;
3200 ptr += PERF_ATTR_SIZE_VER0;
3201
3202 ret = readn(fd, ptr, left);
3203 }
3204 /* read perf_file_section, ids are read in caller */
3205 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3206
3207 return ret <= 0 ? -1 : 0;
3208}
3209
3210static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
3211 struct tep_handle *pevent)
3212{
3213 struct event_format *event;
3214 char bf[128];
3215
3216 /* already prepared */
3217 if (evsel->tp_format)
3218 return 0;
3219
3220 if (pevent == NULL) {
3221 pr_debug("broken or missing trace data\n");
3222 return -1;
3223 }
3224
3225 event = tep_find_event(pevent, evsel->attr.config);
3226 if (event == NULL) {
3227 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
3228 return -1;
3229 }
3230
3231 if (!evsel->name) {
3232 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3233 evsel->name = strdup(bf);
3234 if (evsel->name == NULL)
3235 return -1;
3236 }
3237
3238 evsel->tp_format = event;
3239 return 0;
3240}
3241
3242static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
3243 struct tep_handle *pevent)
3244{
3245 struct perf_evsel *pos;
3246
3247 evlist__for_each_entry(evlist, pos) {
3248 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3249 perf_evsel__prepare_tracepoint_event(pos, pevent))
3250 return -1;
3251 }
3252
3253 return 0;
3254}
3255
3256int perf_session__read_header(struct perf_session *session)
3257{
3258 struct perf_data *data = session->data;
3259 struct perf_header *header = &session->header;
3260 struct perf_file_header f_header;
3261 struct perf_file_attr f_attr;
3262 u64 f_id;
3263 int nr_attrs, nr_ids, i, j;
3264 int fd = perf_data__fd(data);
3265
3266 session->evlist = perf_evlist__new();
3267 if (session->evlist == NULL)
3268 return -ENOMEM;
3269
3270 session->evlist->env = &header->env;
3271 session->machines.host.env = &header->env;
3272 if (perf_data__is_pipe(data))
3273 return perf_header__read_pipe(session);
3274
3275 if (perf_file_header__read(&f_header, header, fd) < 0)
3276 return -EINVAL;
3277
3278 /*
3279 * Sanity check that perf.data was written cleanly; data size is
3280 * initialized to 0 and updated only if the on_exit function is run.
3281 * If data size is still 0 then the file contains only partial
3282 * information. Just warn user and process it as much as it can.
3283 */
3284 if (f_header.data.size == 0) {
3285 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3286 "Was the 'perf record' command properly terminated?\n",
3287 data->file.path);
3288 }
3289
3290 if (f_header.attr_size == 0) {
3291 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3292 "Was the 'perf record' command properly terminated?\n",
3293 data->file.path);
3294 return -EINVAL;
3295 }
3296
3297 nr_attrs = f_header.attrs.size / f_header.attr_size;
3298 lseek(fd, f_header.attrs.offset, SEEK_SET);
3299
3300 for (i = 0; i < nr_attrs; i++) {
3301 struct perf_evsel *evsel;
3302 off_t tmp;
3303
3304 if (read_attr(fd, header, &f_attr) < 0)
3305 goto out_errno;
3306
3307 if (header->needs_swap) {
3308 f_attr.ids.size = bswap_64(f_attr.ids.size);
3309 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
3310 perf_event__attr_swap(&f_attr.attr);
3311 }
3312
3313 tmp = lseek(fd, 0, SEEK_CUR);
3314 evsel = perf_evsel__new(&f_attr.attr);
3315
3316 if (evsel == NULL)
3317 goto out_delete_evlist;
3318
3319 evsel->needs_swap = header->needs_swap;
3320 /*
3321 * Do it before so that if perf_evsel__alloc_id fails, this
3322 * entry gets purged too at perf_evlist__delete().
3323 */
3324 perf_evlist__add(session->evlist, evsel);
3325
3326 nr_ids = f_attr.ids.size / sizeof(u64);
3327 /*
3328 * We don't have the cpu and thread maps on the header, so
3329 * for allocating the perf_sample_id table we fake 1 cpu and
3330 * hattr->ids threads.
3331 */
3332 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3333 goto out_delete_evlist;
3334
3335 lseek(fd, f_attr.ids.offset, SEEK_SET);
3336
3337 for (j = 0; j < nr_ids; j++) {
3338 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
3339 goto out_errno;
3340
3341 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
3342 }
3343
3344 lseek(fd, tmp, SEEK_SET);
3345 }
3346
3347 perf_header__process_sections(header, fd, &session->tevent,
3348 perf_file_section__process);
3349
3350 if (perf_evlist__prepare_tracepoint_events(session->evlist,
3351 session->tevent.pevent))
3352 goto out_delete_evlist;
3353
3354 return 0;
3355out_errno:
3356 return -errno;
3357
3358out_delete_evlist:
3359 perf_evlist__delete(session->evlist);
3360 session->evlist = NULL;
3361 return -ENOMEM;
3362}
3363
3364int perf_event__synthesize_attr(struct perf_tool *tool,
3365 struct perf_event_attr *attr, u32 ids, u64 *id,
3366 perf_event__handler_t process)
3367{
3368 union perf_event *ev;
3369 size_t size;
3370 int err;
3371
3372 size = sizeof(struct perf_event_attr);
3373 size = PERF_ALIGN(size, sizeof(u64));
3374 size += sizeof(struct perf_event_header);
3375 size += ids * sizeof(u64);
3376
3377 ev = zalloc(size);
3378
3379 if (ev == NULL)
3380 return -ENOMEM;
3381
3382 ev->attr.attr = *attr;
3383 memcpy(ev->attr.id, id, ids * sizeof(u64));
3384
3385 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3386 ev->attr.header.size = (u16)size;
3387
3388 if (ev->attr.header.size == size)
3389 err = process(tool, ev, NULL, NULL);
3390 else
3391 err = -E2BIG;
3392
3393 free(ev);
3394
3395 return err;
3396}
3397
3398int perf_event__synthesize_features(struct perf_tool *tool,
3399 struct perf_session *session,
3400 struct perf_evlist *evlist,
3401 perf_event__handler_t process)
3402{
3403 struct perf_header *header = &session->header;
3404 struct feat_fd ff;
3405 struct feature_event *fe;
3406 size_t sz, sz_hdr;
3407 int feat, ret;
3408
3409 sz_hdr = sizeof(fe->header);
3410 sz = sizeof(union perf_event);
3411 /* get a nice alignment */
3412 sz = PERF_ALIGN(sz, page_size);
3413
3414 memset(&ff, 0, sizeof(ff));
3415
3416 ff.buf = malloc(sz);
3417 if (!ff.buf)
3418 return -ENOMEM;
3419
3420 ff.size = sz - sz_hdr;
3421
3422 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3423 if (!feat_ops[feat].synthesize) {
3424 pr_debug("No record header feature for header :%d\n", feat);
3425 continue;
3426 }
3427
3428 ff.offset = sizeof(*fe);
3429
3430 ret = feat_ops[feat].write(&ff, evlist);
3431 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3432 pr_debug("Error writing feature\n");
3433 continue;
3434 }
3435 /* ff.buf may have changed due to realloc in do_write() */
3436 fe = ff.buf;
3437 memset(fe, 0, sizeof(*fe));
3438
3439 fe->feat_id = feat;
3440 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3441 fe->header.size = ff.offset;
3442
3443 ret = process(tool, ff.buf, NULL, NULL);
3444 if (ret) {
3445 free(ff.buf);
3446 return ret;
3447 }
3448 }
3449
3450 /* Send HEADER_LAST_FEATURE mark. */
3451 fe = ff.buf;
3452 fe->feat_id = HEADER_LAST_FEATURE;
3453 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3454 fe->header.size = sizeof(*fe);
3455
3456 ret = process(tool, ff.buf, NULL, NULL);
3457
3458 free(ff.buf);
3459 return ret;
3460}
3461
3462int perf_event__process_feature(struct perf_tool *tool,
3463 union perf_event *event,
3464 struct perf_session *session __maybe_unused)
3465{
3466 struct feat_fd ff = { .fd = 0 };
3467 struct feature_event *fe = (struct feature_event *)event;
3468 int type = fe->header.type;
3469 u64 feat = fe->feat_id;
3470
3471 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3472 pr_warning("invalid record type %d in pipe-mode\n", type);
3473 return 0;
3474 }
3475 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
3476 pr_warning("invalid record type %d in pipe-mode\n", type);
3477 return -1;
3478 }
3479
3480 if (!feat_ops[feat].process)
3481 return 0;
3482
3483 ff.buf = (void *)fe->data;
3484 ff.size = event->header.size - sizeof(*fe);
3485 ff.ph = &session->header;
3486
3487 if (feat_ops[feat].process(&ff, NULL))
3488 return -1;
3489
3490 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3491 return 0;
3492
3493 if (!feat_ops[feat].full_only ||
3494 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3495 feat_ops[feat].print(&ff, stdout);
3496 } else {
3497 fprintf(stdout, "# %s info available, use -I to display\n",
3498 feat_ops[feat].name);
3499 }
3500
3501 return 0;
3502}
3503
3504static struct event_update_event *
3505event_update_event__new(size_t size, u64 type, u64 id)
3506{
3507 struct event_update_event *ev;
3508
3509 size += sizeof(*ev);
3510 size = PERF_ALIGN(size, sizeof(u64));
3511
3512 ev = zalloc(size);
3513 if (ev) {
3514 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3515 ev->header.size = (u16)size;
3516 ev->type = type;
3517 ev->id = id;
3518 }
3519 return ev;
3520}
3521
3522int
3523perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3524 struct perf_evsel *evsel,
3525 perf_event__handler_t process)
3526{
3527 struct event_update_event *ev;
3528 size_t size = strlen(evsel->unit);
3529 int err;
3530
3531 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3532 if (ev == NULL)
3533 return -ENOMEM;
3534
3535 strlcpy(ev->data, evsel->unit, size + 1);
3536 err = process(tool, (union perf_event *)ev, NULL, NULL);
3537 free(ev);
3538 return err;
3539}
3540
3541int
3542perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3543 struct perf_evsel *evsel,
3544 perf_event__handler_t process)
3545{
3546 struct event_update_event *ev;
3547 struct event_update_event_scale *ev_data;
3548 int err;
3549
3550 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3551 if (ev == NULL)
3552 return -ENOMEM;
3553
3554 ev_data = (struct event_update_event_scale *) ev->data;
3555 ev_data->scale = evsel->scale;
3556 err = process(tool, (union perf_event*) ev, NULL, NULL);
3557 free(ev);
3558 return err;
3559}
3560
3561int
3562perf_event__synthesize_event_update_name(struct perf_tool *tool,
3563 struct perf_evsel *evsel,
3564 perf_event__handler_t process)
3565{
3566 struct event_update_event *ev;
3567 size_t len = strlen(evsel->name);
3568 int err;
3569
3570 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3571 if (ev == NULL)
3572 return -ENOMEM;
3573
3574 strlcpy(ev->data, evsel->name, len + 1);
3575 err = process(tool, (union perf_event*) ev, NULL, NULL);
3576 free(ev);
3577 return err;
3578}
3579
3580int
3581perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3582 struct perf_evsel *evsel,
3583 perf_event__handler_t process)
3584{
3585 size_t size = sizeof(struct event_update_event);
3586 struct event_update_event *ev;
3587 int max, err;
3588 u16 type;
3589
3590 if (!evsel->own_cpus)
3591 return 0;
3592
3593 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3594 if (!ev)
3595 return -ENOMEM;
3596
3597 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3598 ev->header.size = (u16)size;
3599 ev->type = PERF_EVENT_UPDATE__CPUS;
3600 ev->id = evsel->id[0];
3601
3602 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3603 evsel->own_cpus,
3604 type, max);
3605
3606 err = process(tool, (union perf_event*) ev, NULL, NULL);
3607 free(ev);
3608 return err;
3609}
3610
3611size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3612{
3613 struct event_update_event *ev = &event->event_update;
3614 struct event_update_event_scale *ev_scale;
3615 struct event_update_event_cpus *ev_cpus;
3616 struct cpu_map *map;
3617 size_t ret;
3618
3619 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3620
3621 switch (ev->type) {
3622 case PERF_EVENT_UPDATE__SCALE:
3623 ev_scale = (struct event_update_event_scale *) ev->data;
3624 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3625 break;
3626 case PERF_EVENT_UPDATE__UNIT:
3627 ret += fprintf(fp, "... unit: %s\n", ev->data);
3628 break;
3629 case PERF_EVENT_UPDATE__NAME:
3630 ret += fprintf(fp, "... name: %s\n", ev->data);
3631 break;
3632 case PERF_EVENT_UPDATE__CPUS:
3633 ev_cpus = (struct event_update_event_cpus *) ev->data;
3634 ret += fprintf(fp, "... ");
3635
3636 map = cpu_map__new_data(&ev_cpus->cpus);
3637 if (map)
3638 ret += cpu_map__fprintf(map, fp);
3639 else
3640 ret += fprintf(fp, "failed to get cpus\n");
3641 break;
3642 default:
3643 ret += fprintf(fp, "... unknown type\n");
3644 break;
3645 }
3646
3647 return ret;
3648}
3649
3650int perf_event__synthesize_attrs(struct perf_tool *tool,
3651 struct perf_session *session,
3652 perf_event__handler_t process)
3653{
3654 struct perf_evsel *evsel;
3655 int err = 0;
3656
3657 evlist__for_each_entry(session->evlist, evsel) {
3658 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3659 evsel->id, process);
3660 if (err) {
3661 pr_debug("failed to create perf header attribute\n");
3662 return err;
3663 }
3664 }
3665
3666 return err;
3667}
3668
3669static bool has_unit(struct perf_evsel *counter)
3670{
3671 return counter->unit && *counter->unit;
3672}
3673
3674static bool has_scale(struct perf_evsel *counter)
3675{
3676 return counter->scale != 1;
3677}
3678
3679int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3680 struct perf_evlist *evsel_list,
3681 perf_event__handler_t process,
3682 bool is_pipe)
3683{
3684 struct perf_evsel *counter;
3685 int err;
3686
3687 /*
3688 * Synthesize other events stuff not carried within
3689 * attr event - unit, scale, name
3690 */
3691 evlist__for_each_entry(evsel_list, counter) {
3692 if (!counter->supported)
3693 continue;
3694
3695 /*
3696 * Synthesize unit and scale only if it's defined.
3697 */
3698 if (has_unit(counter)) {
3699 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3700 if (err < 0) {
3701 pr_err("Couldn't synthesize evsel unit.\n");
3702 return err;
3703 }
3704 }
3705
3706 if (has_scale(counter)) {
3707 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3708 if (err < 0) {
3709 pr_err("Couldn't synthesize evsel counter.\n");
3710 return err;
3711 }
3712 }
3713
3714 if (counter->own_cpus) {
3715 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3716 if (err < 0) {
3717 pr_err("Couldn't synthesize evsel cpus.\n");
3718 return err;
3719 }
3720 }
3721
3722 /*
3723 * Name is needed only for pipe output,
3724 * perf.data carries event names.
3725 */
3726 if (is_pipe) {
3727 err = perf_event__synthesize_event_update_name(tool, counter, process);
3728 if (err < 0) {
3729 pr_err("Couldn't synthesize evsel name.\n");
3730 return err;
3731 }
3732 }
3733 }
3734 return 0;
3735}
3736
3737int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3738 union perf_event *event,
3739 struct perf_evlist **pevlist)
3740{
3741 u32 i, ids, n_ids;
3742 struct perf_evsel *evsel;
3743 struct perf_evlist *evlist = *pevlist;
3744
3745 if (evlist == NULL) {
3746 *pevlist = evlist = perf_evlist__new();
3747 if (evlist == NULL)
3748 return -ENOMEM;
3749 }
3750
3751 evsel = perf_evsel__new(&event->attr.attr);
3752 if (evsel == NULL)
3753 return -ENOMEM;
3754
3755 perf_evlist__add(evlist, evsel);
3756
3757 ids = event->header.size;
3758 ids -= (void *)&event->attr.id - (void *)event;
3759 n_ids = ids / sizeof(u64);
3760 /*
3761 * We don't have the cpu and thread maps on the header, so
3762 * for allocating the perf_sample_id table we fake 1 cpu and
3763 * hattr->ids threads.
3764 */
3765 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3766 return -ENOMEM;
3767
3768 for (i = 0; i < n_ids; i++) {
3769 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3770 }
3771
3772 return 0;
3773}
3774
3775int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3776 union perf_event *event,
3777 struct perf_evlist **pevlist)
3778{
3779 struct event_update_event *ev = &event->event_update;
3780 struct event_update_event_scale *ev_scale;
3781 struct event_update_event_cpus *ev_cpus;
3782 struct perf_evlist *evlist;
3783 struct perf_evsel *evsel;
3784 struct cpu_map *map;
3785
3786 if (!pevlist || *pevlist == NULL)
3787 return -EINVAL;
3788
3789 evlist = *pevlist;
3790
3791 evsel = perf_evlist__id2evsel(evlist, ev->id);
3792 if (evsel == NULL)
3793 return -EINVAL;
3794
3795 switch (ev->type) {
3796 case PERF_EVENT_UPDATE__UNIT:
3797 evsel->unit = strdup(ev->data);
3798 break;
3799 case PERF_EVENT_UPDATE__NAME:
3800 evsel->name = strdup(ev->data);
3801 break;
3802 case PERF_EVENT_UPDATE__SCALE:
3803 ev_scale = (struct event_update_event_scale *) ev->data;
3804 evsel->scale = ev_scale->scale;
3805 break;
3806 case PERF_EVENT_UPDATE__CPUS:
3807 ev_cpus = (struct event_update_event_cpus *) ev->data;
3808
3809 map = cpu_map__new_data(&ev_cpus->cpus);
3810 if (map)
3811 evsel->own_cpus = map;
3812 else
3813 pr_err("failed to get event_update cpus\n");
3814 default:
3815 break;
3816 }
3817
3818 return 0;
3819}
3820
3821int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3822 struct perf_evlist *evlist,
3823 perf_event__handler_t process)
3824{
3825 union perf_event ev;
3826 struct tracing_data *tdata;
3827 ssize_t size = 0, aligned_size = 0, padding;
3828 struct feat_fd ff;
3829 int err __maybe_unused = 0;
3830
3831 /*
3832 * We are going to store the size of the data followed
3833 * by the data contents. Since the fd descriptor is a pipe,
3834 * we cannot seek back to store the size of the data once
3835 * we know it. Instead we:
3836 *
3837 * - write the tracing data to the temp file
3838 * - get/write the data size to pipe
3839 * - write the tracing data from the temp file
3840 * to the pipe
3841 */
3842 tdata = tracing_data_get(&evlist->entries, fd, true);
3843 if (!tdata)
3844 return -1;
3845
3846 memset(&ev, 0, sizeof(ev));
3847
3848 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3849 size = tdata->size;
3850 aligned_size = PERF_ALIGN(size, sizeof(u64));
3851 padding = aligned_size - size;
3852 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3853 ev.tracing_data.size = aligned_size;
3854
3855 process(tool, &ev, NULL, NULL);
3856
3857 /*
3858 * The put function will copy all the tracing data
3859 * stored in temp file to the pipe.
3860 */
3861 tracing_data_put(tdata);
3862
3863 ff = (struct feat_fd){ .fd = fd };
3864 if (write_padded(&ff, NULL, 0, padding))
3865 return -1;
3866
3867 return aligned_size;
3868}
3869
3870int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3871 union perf_event *event,
3872 struct perf_session *session)
3873{
3874 ssize_t size_read, padding, size = event->tracing_data.size;
3875 int fd = perf_data__fd(session->data);
3876 off_t offset = lseek(fd, 0, SEEK_CUR);
3877 char buf[BUFSIZ];
3878
3879 /* setup for reading amidst mmap */
3880 lseek(fd, offset + sizeof(struct tracing_data_event),
3881 SEEK_SET);
3882
3883 size_read = trace_report(fd, &session->tevent,
3884 session->repipe);
3885 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3886
3887 if (readn(fd, buf, padding) < 0) {
3888 pr_err("%s: reading input file", __func__);
3889 return -1;
3890 }
3891 if (session->repipe) {
3892 int retw = write(STDOUT_FILENO, buf, padding);
3893 if (retw <= 0 || retw != padding) {
3894 pr_err("%s: repiping tracing data padding", __func__);
3895 return -1;
3896 }
3897 }
3898
3899 if (size_read + padding != size) {
3900 pr_err("%s: tracing data size mismatch", __func__);
3901 return -1;
3902 }
3903
3904 perf_evlist__prepare_tracepoint_events(session->evlist,
3905 session->tevent.pevent);
3906
3907 return size_read + padding;
3908}
3909
3910int perf_event__synthesize_build_id(struct perf_tool *tool,
3911 struct dso *pos, u16 misc,
3912 perf_event__handler_t process,
3913 struct machine *machine)
3914{
3915 union perf_event ev;
3916 size_t len;
3917 int err = 0;
3918
3919 if (!pos->hit)
3920 return err;
3921
3922 memset(&ev, 0, sizeof(ev));
3923
3924 len = pos->long_name_len + 1;
3925 len = PERF_ALIGN(len, NAME_ALIGN);
3926 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3927 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3928 ev.build_id.header.misc = misc;
3929 ev.build_id.pid = machine->pid;
3930 ev.build_id.header.size = sizeof(ev.build_id) + len;
3931 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3932
3933 err = process(tool, &ev, NULL, machine);
3934
3935 return err;
3936}
3937
3938int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3939 union perf_event *event,
3940 struct perf_session *session)
3941{
3942 __event_process_build_id(&event->build_id,
3943 event->build_id.filename,
3944 session);
3945 return 0;
3946}