1 // SPDX-License-Identifier: GPL-2.0-only
3 #include "util/debug.h"
5 #include "util/event.h"
6 #include "util/evlist.h"
7 #include "util/machine.h"
9 #include "util/map_symbol.h"
10 #include "util/branch.h"
11 #include "util/memswap.h"
12 #include "util/namespaces.h"
13 #include "util/session.h"
14 #include "util/stat.h"
15 #include "util/symbol.h"
16 #include "util/synthetic-events.h"
17 #include "util/target.h"
18 #include "util/time-utils.h"
19 #include "util/cgroup.h"
20 #include <linux/bitops.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/zalloc.h>
24 #include <linux/perf_event.h>
26 #include <perf/evsel.h>
27 #include <internal/cpumap.h>
28 #include <perf/cpumap.h>
29 #include <internal/lib.h> // page_size
30 #include <internal/threadmap.h>
31 #include <perf/threadmap.h>
32 #include <symbol/kallsyms.h>
38 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
39 #include <api/fs/fs.h>
40 #include <sys/types.h>
45 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
47 unsigned int proc_map_timeout
= DEFAULT_PROC_MAP_PARSE_TIMEOUT
;
49 int perf_tool__process_synth_event(struct perf_tool
*tool
,
50 union perf_event
*event
,
51 struct machine
*machine
,
52 perf_event__handler_t process
)
54 struct perf_sample synth_sample
= {
61 .cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
,
64 return process(tool
, event
, &synth_sample
, machine
);
68 * Assumes that the first 4095 bytes of /proc/pid/stat contains
69 * the comm, tgid and ppid.
71 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
72 pid_t
*tgid
, pid_t
*ppid
)
74 char filename
[PATH_MAX
];
79 char *name
, *tgids
, *ppids
;
84 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
86 fd
= open(filename
, O_RDONLY
);
88 pr_debug("couldn't open %s\n", filename
);
92 n
= read(fd
, bf
, sizeof(bf
) - 1);
95 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
101 name
= strstr(bf
, "Name:");
102 tgids
= strstr(bf
, "Tgid:");
103 ppids
= strstr(bf
, "PPid:");
108 name
= skip_spaces(name
+ 5); /* strlen("Name:") */
109 nl
= strchr(name
, '\n');
116 memcpy(comm
, name
, size
);
119 pr_debug("Name: string not found for pid %d\n", pid
);
123 tgids
+= 5; /* strlen("Tgid:") */
126 pr_debug("Tgid: string not found for pid %d\n", pid
);
130 ppids
+= 5; /* strlen("PPid:") */
133 pr_debug("PPid: string not found for pid %d\n", pid
);
139 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
140 struct machine
*machine
,
141 pid_t
*tgid
, pid_t
*ppid
)
147 memset(&event
->comm
, 0, sizeof(event
->comm
));
149 if (machine__is_host(machine
)) {
150 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
151 sizeof(event
->comm
.comm
),
156 *tgid
= machine
->pid
;
162 event
->comm
.pid
= *tgid
;
163 event
->comm
.header
.type
= PERF_RECORD_COMM
;
165 size
= strlen(event
->comm
.comm
) + 1;
166 size
= PERF_ALIGN(size
, sizeof(u64
));
167 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
168 event
->comm
.header
.size
= (sizeof(event
->comm
) -
169 (sizeof(event
->comm
.comm
) - size
) +
170 machine
->id_hdr_size
);
171 event
->comm
.tid
= pid
;
176 pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
177 union perf_event
*event
, pid_t pid
,
178 perf_event__handler_t process
,
179 struct machine
*machine
)
183 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
186 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
192 static void perf_event__get_ns_link_info(pid_t pid
, const char *ns
,
193 struct perf_ns_link_info
*ns_link_info
)
198 sprintf(proc_ns
, "/proc/%u/ns/%s", pid
, ns
);
199 if (stat64(proc_ns
, &st
) == 0) {
200 ns_link_info
->dev
= st
.st_dev
;
201 ns_link_info
->ino
= st
.st_ino
;
205 int perf_event__synthesize_namespaces(struct perf_tool
*tool
,
206 union perf_event
*event
,
207 pid_t pid
, pid_t tgid
,
208 perf_event__handler_t process
,
209 struct machine
*machine
)
212 struct perf_ns_link_info
*ns_link_info
;
214 if (!tool
|| !tool
->namespace_events
)
217 memset(&event
->namespaces
, 0, (sizeof(event
->namespaces
) +
218 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
219 machine
->id_hdr_size
));
221 event
->namespaces
.pid
= tgid
;
222 event
->namespaces
.tid
= pid
;
224 event
->namespaces
.nr_namespaces
= NR_NAMESPACES
;
226 ns_link_info
= event
->namespaces
.link_info
;
228 for (idx
= 0; idx
< event
->namespaces
.nr_namespaces
; idx
++)
229 perf_event__get_ns_link_info(pid
, perf_ns__name(idx
),
232 event
->namespaces
.header
.type
= PERF_RECORD_NAMESPACES
;
234 event
->namespaces
.header
.size
= (sizeof(event
->namespaces
) +
235 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
236 machine
->id_hdr_size
);
238 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
244 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
245 union perf_event
*event
,
246 pid_t pid
, pid_t tgid
, pid_t ppid
,
247 perf_event__handler_t process
,
248 struct machine
*machine
)
250 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
253 * for main thread set parent to ppid from status file. For other
254 * threads set parent pid to main thread. ie., assume main thread
255 * spawns all threads in a process
258 event
->fork
.ppid
= ppid
;
259 event
->fork
.ptid
= ppid
;
261 event
->fork
.ppid
= tgid
;
262 event
->fork
.ptid
= tgid
;
264 event
->fork
.pid
= tgid
;
265 event
->fork
.tid
= pid
;
266 event
->fork
.header
.type
= PERF_RECORD_FORK
;
267 event
->fork
.header
.misc
= PERF_RECORD_MISC_FORK_EXEC
;
269 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
271 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
277 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
278 union perf_event
*event
,
279 pid_t pid
, pid_t tgid
,
280 perf_event__handler_t process
,
281 struct machine
*machine
,
284 char filename
[PATH_MAX
];
286 unsigned long long t
;
287 bool truncation
= false;
288 unsigned long long timeout
= proc_map_timeout
* 1000000ULL;
290 const char *hugetlbfs_mnt
= hugetlbfs__mountpoint();
291 int hugetlbfs_mnt_len
= hugetlbfs_mnt
? strlen(hugetlbfs_mnt
) : 0;
293 if (machine__is_default_guest(machine
))
296 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task/%d/maps",
297 machine
->root_dir
, pid
, pid
);
299 fp
= fopen(filename
, "r");
302 * We raced with a task exiting - just return:
304 pr_debug("couldn't open %s\n", filename
);
308 event
->header
.type
= PERF_RECORD_MMAP2
;
314 char execname
[PATH_MAX
];
315 char anonstr
[] = "//anon";
320 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
323 if ((rdclock() - t
) > timeout
) {
324 pr_warning("Reading %s time out. "
325 "You may want to increase "
326 "the time limit by --proc-map-timeout\n",
332 /* ensure null termination since stack will be reused. */
333 strcpy(execname
, "");
335 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
336 n
= sscanf(bf
, "%"PRI_lx64
"-%"PRI_lx64
" %s %"PRI_lx64
" %x:%x %u %[^\n]\n",
337 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
338 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
343 * Anon maps don't have the execname.
348 event
->mmap2
.ino
= (u64
)ino
;
349 event
->mmap2
.ino_generation
= 0;
352 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
354 if (machine__is_host(machine
))
355 event
->header
.misc
= PERF_RECORD_MISC_USER
;
357 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
359 /* map protection and flags bits */
360 event
->mmap2
.prot
= 0;
361 event
->mmap2
.flags
= 0;
363 event
->mmap2
.prot
|= PROT_READ
;
365 event
->mmap2
.prot
|= PROT_WRITE
;
367 event
->mmap2
.prot
|= PROT_EXEC
;
370 event
->mmap2
.flags
|= MAP_SHARED
;
372 event
->mmap2
.flags
|= MAP_PRIVATE
;
374 if (prot
[2] != 'x') {
375 if (!mmap_data
|| prot
[0] != 'r')
378 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
383 event
->header
.misc
|= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
;
385 if (!strcmp(execname
, ""))
386 strcpy(execname
, anonstr
);
388 if (hugetlbfs_mnt_len
&&
389 !strncmp(execname
, hugetlbfs_mnt
, hugetlbfs_mnt_len
)) {
390 strcpy(execname
, anonstr
);
391 event
->mmap2
.flags
|= MAP_HUGETLB
;
394 size
= strlen(execname
) + 1;
395 memcpy(event
->mmap2
.filename
, execname
, size
);
396 size
= PERF_ALIGN(size
, sizeof(u64
));
397 event
->mmap2
.len
-= event
->mmap
.start
;
398 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
399 (sizeof(event
->mmap2
.filename
) - size
));
400 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
401 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
402 event
->mmap2
.pid
= tgid
;
403 event
->mmap2
.tid
= pid
;
405 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
418 #ifdef HAVE_FILE_HANDLE
419 static int perf_event__synthesize_cgroup(struct perf_tool
*tool
,
420 union perf_event
*event
,
421 char *path
, size_t mount_len
,
422 perf_event__handler_t process
,
423 struct machine
*machine
)
425 size_t event_size
= sizeof(event
->cgroup
) - sizeof(event
->cgroup
.path
);
426 size_t path_len
= strlen(path
) - mount_len
+ 1;
428 struct file_handle fh
;
433 while (path_len
% sizeof(u64
))
434 path
[mount_len
+ path_len
++] = '\0';
436 memset(&event
->cgroup
, 0, event_size
);
438 event
->cgroup
.header
.type
= PERF_RECORD_CGROUP
;
439 event
->cgroup
.header
.size
= event_size
+ path_len
+ machine
->id_hdr_size
;
441 handle
.fh
.handle_bytes
= sizeof(handle
.cgroup_id
);
442 if (name_to_handle_at(AT_FDCWD
, path
, &handle
.fh
, &mount_id
, 0) < 0) {
443 pr_debug("stat failed: %s\n", path
);
447 event
->cgroup
.id
= handle
.cgroup_id
;
448 strncpy(event
->cgroup
.path
, path
+ mount_len
, path_len
);
449 memset(event
->cgroup
.path
+ path_len
, 0, machine
->id_hdr_size
);
451 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) < 0) {
452 pr_debug("process synth event failed\n");
459 static int perf_event__walk_cgroup_tree(struct perf_tool
*tool
,
460 union perf_event
*event
,
461 char *path
, size_t mount_len
,
462 perf_event__handler_t process
,
463 struct machine
*machine
)
465 size_t pos
= strlen(path
);
470 if (perf_event__synthesize_cgroup(tool
, event
, path
, mount_len
,
471 process
, machine
) < 0)
476 pr_debug("failed to open directory: %s\n", path
);
480 while ((dent
= readdir(d
)) != NULL
) {
481 if (dent
->d_type
!= DT_DIR
)
483 if (!strcmp(dent
->d_name
, ".") ||
484 !strcmp(dent
->d_name
, ".."))
487 /* any sane path should be less than PATH_MAX */
488 if (strlen(path
) + strlen(dent
->d_name
) + 1 >= PATH_MAX
)
491 if (path
[pos
- 1] != '/')
493 strcat(path
, dent
->d_name
);
495 ret
= perf_event__walk_cgroup_tree(tool
, event
, path
,
496 mount_len
, process
, machine
);
507 int perf_event__synthesize_cgroups(struct perf_tool
*tool
,
508 perf_event__handler_t process
,
509 struct machine
*machine
)
511 union perf_event event
;
512 char cgrp_root
[PATH_MAX
];
513 size_t mount_len
; /* length of mount point in the path */
515 if (cgroupfs_find_mountpoint(cgrp_root
, PATH_MAX
, "perf_event") < 0) {
516 pr_debug("cannot find cgroup mount point\n");
520 mount_len
= strlen(cgrp_root
);
521 /* make sure the path starts with a slash (after mount point) */
522 strcat(cgrp_root
, "/");
524 if (perf_event__walk_cgroup_tree(tool
, &event
, cgrp_root
, mount_len
,
525 process
, machine
) < 0)
531 int perf_event__synthesize_cgroups(struct perf_tool
*tool __maybe_unused
,
532 perf_event__handler_t process __maybe_unused
,
533 struct machine
*machine __maybe_unused
)
539 int perf_event__synthesize_modules(struct perf_tool
*tool
, perf_event__handler_t process
,
540 struct machine
*machine
)
544 struct maps
*maps
= machine__kernel_maps(machine
);
545 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
546 machine
->id_hdr_size
));
548 pr_debug("Not enough memory synthesizing mmap event "
549 "for kernel modules\n");
553 event
->header
.type
= PERF_RECORD_MMAP
;
556 * kernel uses 0 for user space maps, see kernel/perf_event.c
559 if (machine__is_host(machine
))
560 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
562 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
564 maps__for_each_entry(maps
, pos
) {
567 if (!__map__is_kmodule(pos
))
570 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
571 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
572 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
573 (sizeof(event
->mmap
.filename
) - size
));
574 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
575 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
576 event
->mmap
.start
= pos
->start
;
577 event
->mmap
.len
= pos
->end
- pos
->start
;
578 event
->mmap
.pid
= machine
->pid
;
580 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
581 pos
->dso
->long_name_len
+ 1);
582 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
592 static int __event__synthesize_thread(union perf_event
*comm_event
,
593 union perf_event
*mmap_event
,
594 union perf_event
*fork_event
,
595 union perf_event
*namespaces_event
,
596 pid_t pid
, int full
, perf_event__handler_t process
,
597 struct perf_tool
*tool
, struct machine
*machine
, bool mmap_data
)
599 char filename
[PATH_MAX
];
601 struct dirent
*dirent
;
605 /* special case: only send one comm event using passed in pid */
607 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
613 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, pid
,
614 tgid
, process
, machine
) < 0)
618 * send mmap only for thread group leader
619 * see thread__init_maps()
622 perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
623 process
, machine
, mmap_data
))
629 if (machine__is_default_guest(machine
))
632 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
633 machine
->root_dir
, pid
);
635 tasks
= opendir(filename
);
637 pr_debug("couldn't open %s\n", filename
);
641 while ((dirent
= readdir(tasks
)) != NULL
) {
645 _pid
= strtol(dirent
->d_name
, &end
, 10);
650 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
654 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
655 ppid
, process
, machine
) < 0)
658 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, _pid
,
659 tgid
, process
, machine
) < 0)
663 * Send the prepared comm event
665 if (perf_tool__process_synth_event(tool
, comm_event
, machine
, process
) != 0)
670 /* process the parent's maps too */
671 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
672 process
, machine
, mmap_data
);
682 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
683 struct perf_thread_map
*threads
,
684 perf_event__handler_t process
,
685 struct machine
*machine
,
688 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
689 union perf_event
*namespaces_event
;
690 int err
= -1, thread
, j
;
692 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
693 if (comm_event
== NULL
)
696 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
697 if (mmap_event
== NULL
)
700 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
701 if (fork_event
== NULL
)
704 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
705 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
706 machine
->id_hdr_size
);
707 if (namespaces_event
== NULL
)
711 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
712 if (__event__synthesize_thread(comm_event
, mmap_event
,
713 fork_event
, namespaces_event
,
714 perf_thread_map__pid(threads
, thread
), 0,
715 process
, tool
, machine
,
722 * comm.pid is set to thread group id by
723 * perf_event__synthesize_comm
725 if ((int) comm_event
->comm
.pid
!= perf_thread_map__pid(threads
, thread
)) {
726 bool need_leader
= true;
728 /* is thread group leader in thread_map? */
729 for (j
= 0; j
< threads
->nr
; ++j
) {
730 if ((int) comm_event
->comm
.pid
== perf_thread_map__pid(threads
, j
)) {
736 /* if not, generate events for it */
738 __event__synthesize_thread(comm_event
, mmap_event
,
739 fork_event
, namespaces_event
,
740 comm_event
->comm
.pid
, 0,
741 process
, tool
, machine
,
748 free(namespaces_event
);
759 static int __perf_event__synthesize_threads(struct perf_tool
*tool
,
760 perf_event__handler_t process
,
761 struct machine
*machine
,
763 struct dirent
**dirent
,
767 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
768 union perf_event
*namespaces_event
;
774 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
775 if (comm_event
== NULL
)
778 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
779 if (mmap_event
== NULL
)
782 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
783 if (fork_event
== NULL
)
786 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
787 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
788 machine
->id_hdr_size
);
789 if (namespaces_event
== NULL
)
792 for (i
= start
; i
< start
+ num
; i
++) {
793 if (!isdigit(dirent
[i
]->d_name
[0]))
796 pid
= (pid_t
)strtol(dirent
[i
]->d_name
, &end
, 10);
797 /* only interested in proper numerical dirents */
801 * We may race with exiting thread, so don't stop just because
802 * one thread couldn't be synthesized.
804 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
,
805 namespaces_event
, pid
, 1, process
,
806 tool
, machine
, mmap_data
);
810 free(namespaces_event
);
821 struct synthesize_threads_arg
{
822 struct perf_tool
*tool
;
823 perf_event__handler_t process
;
824 struct machine
*machine
;
826 struct dirent
**dirent
;
831 static void *synthesize_threads_worker(void *arg
)
833 struct synthesize_threads_arg
*args
= arg
;
835 __perf_event__synthesize_threads(args
->tool
, args
->process
,
836 args
->machine
, args
->mmap_data
,
838 args
->start
, args
->num
);
842 int perf_event__synthesize_threads(struct perf_tool
*tool
,
843 perf_event__handler_t process
,
844 struct machine
*machine
,
846 unsigned int nr_threads_synthesize
)
848 struct synthesize_threads_arg
*args
= NULL
;
849 pthread_t
*synthesize_threads
= NULL
;
850 char proc_path
[PATH_MAX
];
851 struct dirent
**dirent
;
859 if (machine__is_default_guest(machine
))
862 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
863 n
= scandir(proc_path
, &dirent
, 0, alphasort
);
867 if (nr_threads_synthesize
== UINT_MAX
)
868 thread_nr
= sysconf(_SC_NPROCESSORS_ONLN
);
870 thread_nr
= nr_threads_synthesize
;
872 if (thread_nr
<= 1) {
873 err
= __perf_event__synthesize_threads(tool
, process
,
881 synthesize_threads
= calloc(sizeof(pthread_t
), thread_nr
);
882 if (synthesize_threads
== NULL
)
885 args
= calloc(sizeof(*args
), thread_nr
);
889 num_per_thread
= n
/ thread_nr
;
891 for (i
= 0; i
< thread_nr
; i
++) {
893 args
[i
].process
= process
;
894 args
[i
].machine
= machine
;
895 args
[i
].mmap_data
= mmap_data
;
896 args
[i
].dirent
= dirent
;
898 for (i
= 0; i
< m
; i
++) {
899 args
[i
].num
= num_per_thread
+ 1;
900 args
[i
].start
= i
* args
[i
].num
;
903 base
= args
[i
-1].start
+ args
[i
-1].num
;
904 for (j
= i
; j
< thread_nr
; j
++) {
905 args
[j
].num
= num_per_thread
;
906 args
[j
].start
= base
+ (j
- i
) * args
[i
].num
;
909 for (i
= 0; i
< thread_nr
; i
++) {
910 if (pthread_create(&synthesize_threads
[i
], NULL
,
911 synthesize_threads_worker
, &args
[i
]))
916 for (i
= 0; i
< thread_nr
; i
++)
917 pthread_join(synthesize_threads
[i
], NULL
);
920 free(synthesize_threads
);
922 for (i
= 0; i
< n
; i
++)
929 int __weak
perf_event__synthesize_extra_kmaps(struct perf_tool
*tool __maybe_unused
,
930 perf_event__handler_t process __maybe_unused
,
931 struct machine
*machine __maybe_unused
)
936 static int __perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
937 perf_event__handler_t process
,
938 struct machine
*machine
)
941 struct map
*map
= machine__kernel_map(machine
);
944 union perf_event
*event
;
949 kmap
= map__kmap(map
);
950 if (!kmap
->ref_reloc_sym
)
954 * We should get this from /sys/kernel/sections/.text, but till that is
955 * available use this, and after it is use this as a fallback for older
958 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
960 pr_debug("Not enough memory synthesizing mmap event "
961 "for kernel modules\n");
965 if (machine__is_host(machine
)) {
967 * kernel uses PERF_RECORD_MISC_USER for user space maps,
968 * see kernel/perf_event.c __perf_event_mmap
970 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
972 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
975 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
976 "%s%s", machine
->mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
977 size
= PERF_ALIGN(size
, sizeof(u64
));
978 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
979 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
980 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
981 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
982 event
->mmap
.start
= map
->start
;
983 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
984 event
->mmap
.pid
= machine
->pid
;
986 err
= perf_tool__process_synth_event(tool
, event
, machine
, process
);
992 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
993 perf_event__handler_t process
,
994 struct machine
*machine
)
998 err
= __perf_event__synthesize_kernel_mmap(tool
, process
, machine
);
1002 return perf_event__synthesize_extra_kmaps(tool
, process
, machine
);
1005 int perf_event__synthesize_thread_map2(struct perf_tool
*tool
,
1006 struct perf_thread_map
*threads
,
1007 perf_event__handler_t process
,
1008 struct machine
*machine
)
1010 union perf_event
*event
;
1013 size
= sizeof(event
->thread_map
);
1014 size
+= threads
->nr
* sizeof(event
->thread_map
.entries
[0]);
1016 event
= zalloc(size
);
1020 event
->header
.type
= PERF_RECORD_THREAD_MAP
;
1021 event
->header
.size
= size
;
1022 event
->thread_map
.nr
= threads
->nr
;
1024 for (i
= 0; i
< threads
->nr
; i
++) {
1025 struct perf_record_thread_map_entry
*entry
= &event
->thread_map
.entries
[i
];
1026 char *comm
= perf_thread_map__comm(threads
, i
);
1031 entry
->pid
= perf_thread_map__pid(threads
, i
);
1032 strncpy((char *) &entry
->comm
, comm
, sizeof(entry
->comm
));
1035 err
= process(tool
, event
, NULL
, machine
);
1041 static void synthesize_cpus(struct cpu_map_entries
*cpus
,
1042 struct perf_cpu_map
*map
)
1048 for (i
= 0; i
< map
->nr
; i
++)
1049 cpus
->cpu
[i
] = map
->map
[i
];
1052 static void synthesize_mask(struct perf_record_record_cpu_map
*mask
,
1053 struct perf_cpu_map
*map
, int max
)
1057 mask
->nr
= BITS_TO_LONGS(max
);
1058 mask
->long_size
= sizeof(long);
1060 for (i
= 0; i
< map
->nr
; i
++)
1061 set_bit(map
->map
[i
], mask
->mask
);
1064 static size_t cpus_size(struct perf_cpu_map
*map
)
1066 return sizeof(struct cpu_map_entries
) + map
->nr
* sizeof(u16
);
1069 static size_t mask_size(struct perf_cpu_map
*map
, int *max
)
1075 for (i
= 0; i
< map
->nr
; i
++) {
1076 /* bit possition of the cpu is + 1 */
1077 int bit
= map
->map
[i
] + 1;
1083 return sizeof(struct perf_record_record_cpu_map
) + BITS_TO_LONGS(*max
) * sizeof(long);
1086 void *cpu_map_data__alloc(struct perf_cpu_map
*map
, size_t *size
, u16
*type
, int *max
)
1088 size_t size_cpus
, size_mask
;
1089 bool is_dummy
= perf_cpu_map__empty(map
);
1092 * Both array and mask data have variable size based
1093 * on the number of cpus and their actual values.
1094 * The size of the 'struct perf_record_cpu_map_data' is:
1096 * array = size of 'struct cpu_map_entries' +
1097 * number of cpus * sizeof(u64)
1099 * mask = size of 'struct perf_record_record_cpu_map' +
1100 * maximum cpu bit converted to size of longs
1102 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1104 size_cpus
= cpus_size(map
);
1105 size_mask
= mask_size(map
, max
);
1107 if (is_dummy
|| (size_cpus
< size_mask
)) {
1109 *type
= PERF_CPU_MAP__CPUS
;
1112 *type
= PERF_CPU_MAP__MASK
;
1115 *size
+= sizeof(struct perf_record_cpu_map_data
);
1116 *size
= PERF_ALIGN(*size
, sizeof(u64
));
1117 return zalloc(*size
);
1120 void cpu_map_data__synthesize(struct perf_record_cpu_map_data
*data
, struct perf_cpu_map
*map
,
1126 case PERF_CPU_MAP__CPUS
:
1127 synthesize_cpus((struct cpu_map_entries
*) data
->data
, map
);
1129 case PERF_CPU_MAP__MASK
:
1130 synthesize_mask((struct perf_record_record_cpu_map
*)data
->data
, map
, max
);
1136 static struct perf_record_cpu_map
*cpu_map_event__new(struct perf_cpu_map
*map
)
1138 size_t size
= sizeof(struct perf_record_cpu_map
);
1139 struct perf_record_cpu_map
*event
;
1143 event
= cpu_map_data__alloc(map
, &size
, &type
, &max
);
1147 event
->header
.type
= PERF_RECORD_CPU_MAP
;
1148 event
->header
.size
= size
;
1149 event
->data
.type
= type
;
1151 cpu_map_data__synthesize(&event
->data
, map
, type
, max
);
1155 int perf_event__synthesize_cpu_map(struct perf_tool
*tool
,
1156 struct perf_cpu_map
*map
,
1157 perf_event__handler_t process
,
1158 struct machine
*machine
)
1160 struct perf_record_cpu_map
*event
;
1163 event
= cpu_map_event__new(map
);
1167 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1173 int perf_event__synthesize_stat_config(struct perf_tool
*tool
,
1174 struct perf_stat_config
*config
,
1175 perf_event__handler_t process
,
1176 struct machine
*machine
)
1178 struct perf_record_stat_config
*event
;
1179 int size
, i
= 0, err
;
1181 size
= sizeof(*event
);
1182 size
+= (PERF_STAT_CONFIG_TERM__MAX
* sizeof(event
->data
[0]));
1184 event
= zalloc(size
);
1188 event
->header
.type
= PERF_RECORD_STAT_CONFIG
;
1189 event
->header
.size
= size
;
1190 event
->nr
= PERF_STAT_CONFIG_TERM__MAX
;
1192 #define ADD(__term, __val) \
1193 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1194 event->data[i].val = __val; \
1197 ADD(AGGR_MODE
, config
->aggr_mode
)
1198 ADD(INTERVAL
, config
->interval
)
1199 ADD(SCALE
, config
->scale
)
1201 WARN_ONCE(i
!= PERF_STAT_CONFIG_TERM__MAX
,
1202 "stat config terms unbalanced\n");
1205 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1211 int perf_event__synthesize_stat(struct perf_tool
*tool
,
1212 u32 cpu
, u32 thread
, u64 id
,
1213 struct perf_counts_values
*count
,
1214 perf_event__handler_t process
,
1215 struct machine
*machine
)
1217 struct perf_record_stat event
;
1219 event
.header
.type
= PERF_RECORD_STAT
;
1220 event
.header
.size
= sizeof(event
);
1221 event
.header
.misc
= 0;
1225 event
.thread
= thread
;
1226 event
.val
= count
->val
;
1227 event
.ena
= count
->ena
;
1228 event
.run
= count
->run
;
1230 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1233 int perf_event__synthesize_stat_round(struct perf_tool
*tool
,
1234 u64 evtime
, u64 type
,
1235 perf_event__handler_t process
,
1236 struct machine
*machine
)
1238 struct perf_record_stat_round event
;
1240 event
.header
.type
= PERF_RECORD_STAT_ROUND
;
1241 event
.header
.size
= sizeof(event
);
1242 event
.header
.misc
= 0;
1244 event
.time
= evtime
;
1247 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1250 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
, u64 read_format
)
1252 size_t sz
, result
= sizeof(struct perf_record_sample
);
1254 if (type
& PERF_SAMPLE_IDENTIFIER
)
1255 result
+= sizeof(u64
);
1257 if (type
& PERF_SAMPLE_IP
)
1258 result
+= sizeof(u64
);
1260 if (type
& PERF_SAMPLE_TID
)
1261 result
+= sizeof(u64
);
1263 if (type
& PERF_SAMPLE_TIME
)
1264 result
+= sizeof(u64
);
1266 if (type
& PERF_SAMPLE_ADDR
)
1267 result
+= sizeof(u64
);
1269 if (type
& PERF_SAMPLE_ID
)
1270 result
+= sizeof(u64
);
1272 if (type
& PERF_SAMPLE_STREAM_ID
)
1273 result
+= sizeof(u64
);
1275 if (type
& PERF_SAMPLE_CPU
)
1276 result
+= sizeof(u64
);
1278 if (type
& PERF_SAMPLE_PERIOD
)
1279 result
+= sizeof(u64
);
1281 if (type
& PERF_SAMPLE_READ
) {
1282 result
+= sizeof(u64
);
1283 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1284 result
+= sizeof(u64
);
1285 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1286 result
+= sizeof(u64
);
1287 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1288 if (read_format
& PERF_FORMAT_GROUP
) {
1289 sz
= sample
->read
.group
.nr
*
1290 sizeof(struct sample_read_value
);
1293 result
+= sizeof(u64
);
1297 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1298 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1302 if (type
& PERF_SAMPLE_RAW
) {
1303 result
+= sizeof(u32
);
1304 result
+= sample
->raw_size
;
1307 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1308 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1310 sz
+= 2 * sizeof(u64
);
1314 if (type
& PERF_SAMPLE_REGS_USER
) {
1315 if (sample
->user_regs
.abi
) {
1316 result
+= sizeof(u64
);
1317 sz
= hweight64(sample
->user_regs
.mask
) * sizeof(u64
);
1320 result
+= sizeof(u64
);
1324 if (type
& PERF_SAMPLE_STACK_USER
) {
1325 sz
= sample
->user_stack
.size
;
1326 result
+= sizeof(u64
);
1329 result
+= sizeof(u64
);
1333 if (type
& PERF_SAMPLE_WEIGHT
)
1334 result
+= sizeof(u64
);
1336 if (type
& PERF_SAMPLE_DATA_SRC
)
1337 result
+= sizeof(u64
);
1339 if (type
& PERF_SAMPLE_TRANSACTION
)
1340 result
+= sizeof(u64
);
1342 if (type
& PERF_SAMPLE_REGS_INTR
) {
1343 if (sample
->intr_regs
.abi
) {
1344 result
+= sizeof(u64
);
1345 sz
= hweight64(sample
->intr_regs
.mask
) * sizeof(u64
);
1348 result
+= sizeof(u64
);
1352 if (type
& PERF_SAMPLE_PHYS_ADDR
)
1353 result
+= sizeof(u64
);
1355 if (type
& PERF_SAMPLE_CGROUP
)
1356 result
+= sizeof(u64
);
1358 if (type
& PERF_SAMPLE_AUX
) {
1359 result
+= sizeof(u64
);
1360 result
+= sample
->aux_sample
.size
;
1366 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
, u64 read_format
,
1367 const struct perf_sample
*sample
)
1372 * used for cross-endian analysis. See git commit 65014ab3
1373 * for why this goofiness is needed.
1377 array
= event
->sample
.array
;
1379 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1380 *array
= sample
->id
;
1384 if (type
& PERF_SAMPLE_IP
) {
1385 *array
= sample
->ip
;
1389 if (type
& PERF_SAMPLE_TID
) {
1390 u
.val32
[0] = sample
->pid
;
1391 u
.val32
[1] = sample
->tid
;
1396 if (type
& PERF_SAMPLE_TIME
) {
1397 *array
= sample
->time
;
1401 if (type
& PERF_SAMPLE_ADDR
) {
1402 *array
= sample
->addr
;
1406 if (type
& PERF_SAMPLE_ID
) {
1407 *array
= sample
->id
;
1411 if (type
& PERF_SAMPLE_STREAM_ID
) {
1412 *array
= sample
->stream_id
;
1416 if (type
& PERF_SAMPLE_CPU
) {
1417 u
.val32
[0] = sample
->cpu
;
1423 if (type
& PERF_SAMPLE_PERIOD
) {
1424 *array
= sample
->period
;
1428 if (type
& PERF_SAMPLE_READ
) {
1429 if (read_format
& PERF_FORMAT_GROUP
)
1430 *array
= sample
->read
.group
.nr
;
1432 *array
= sample
->read
.one
.value
;
1435 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1436 *array
= sample
->read
.time_enabled
;
1440 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1441 *array
= sample
->read
.time_running
;
1445 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1446 if (read_format
& PERF_FORMAT_GROUP
) {
1447 sz
= sample
->read
.group
.nr
*
1448 sizeof(struct sample_read_value
);
1449 memcpy(array
, sample
->read
.group
.values
, sz
);
1450 array
= (void *)array
+ sz
;
1452 *array
= sample
->read
.one
.id
;
1457 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1458 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1459 memcpy(array
, sample
->callchain
, sz
);
1460 array
= (void *)array
+ sz
;
1463 if (type
& PERF_SAMPLE_RAW
) {
1464 u
.val32
[0] = sample
->raw_size
;
1466 array
= (void *)array
+ sizeof(u32
);
1468 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
1469 array
= (void *)array
+ sample
->raw_size
;
1472 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1473 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1475 sz
+= 2 * sizeof(u64
);
1476 memcpy(array
, sample
->branch_stack
, sz
);
1477 array
= (void *)array
+ sz
;
1480 if (type
& PERF_SAMPLE_REGS_USER
) {
1481 if (sample
->user_regs
.abi
) {
1482 *array
++ = sample
->user_regs
.abi
;
1483 sz
= hweight64(sample
->user_regs
.mask
) * sizeof(u64
);
1484 memcpy(array
, sample
->user_regs
.regs
, sz
);
1485 array
= (void *)array
+ sz
;
1491 if (type
& PERF_SAMPLE_STACK_USER
) {
1492 sz
= sample
->user_stack
.size
;
1495 memcpy(array
, sample
->user_stack
.data
, sz
);
1496 array
= (void *)array
+ sz
;
1501 if (type
& PERF_SAMPLE_WEIGHT
) {
1502 *array
= sample
->weight
;
1506 if (type
& PERF_SAMPLE_DATA_SRC
) {
1507 *array
= sample
->data_src
;
1511 if (type
& PERF_SAMPLE_TRANSACTION
) {
1512 *array
= sample
->transaction
;
1516 if (type
& PERF_SAMPLE_REGS_INTR
) {
1517 if (sample
->intr_regs
.abi
) {
1518 *array
++ = sample
->intr_regs
.abi
;
1519 sz
= hweight64(sample
->intr_regs
.mask
) * sizeof(u64
);
1520 memcpy(array
, sample
->intr_regs
.regs
, sz
);
1521 array
= (void *)array
+ sz
;
1527 if (type
& PERF_SAMPLE_PHYS_ADDR
) {
1528 *array
= sample
->phys_addr
;
1532 if (type
& PERF_SAMPLE_CGROUP
) {
1533 *array
= sample
->cgroup
;
1537 if (type
& PERF_SAMPLE_AUX
) {
1538 sz
= sample
->aux_sample
.size
;
1540 memcpy(array
, sample
->aux_sample
.data
, sz
);
1541 array
= (void *)array
+ sz
;
1547 int perf_event__synthesize_id_index(struct perf_tool
*tool
, perf_event__handler_t process
,
1548 struct evlist
*evlist
, struct machine
*machine
)
1550 union perf_event
*ev
;
1551 struct evsel
*evsel
;
1552 size_t nr
= 0, i
= 0, sz
, max_nr
, n
;
1555 pr_debug2("Synthesizing id index\n");
1557 max_nr
= (UINT16_MAX
- sizeof(struct perf_record_id_index
)) /
1558 sizeof(struct id_index_entry
);
1560 evlist__for_each_entry(evlist
, evsel
)
1561 nr
+= evsel
->core
.ids
;
1563 n
= nr
> max_nr
? max_nr
: nr
;
1564 sz
= sizeof(struct perf_record_id_index
) + n
* sizeof(struct id_index_entry
);
1569 ev
->id_index
.header
.type
= PERF_RECORD_ID_INDEX
;
1570 ev
->id_index
.header
.size
= sz
;
1571 ev
->id_index
.nr
= n
;
1573 evlist__for_each_entry(evlist
, evsel
) {
1576 for (j
= 0; j
< evsel
->core
.ids
; j
++) {
1577 struct id_index_entry
*e
;
1578 struct perf_sample_id
*sid
;
1581 err
= process(tool
, ev
, NULL
, machine
);
1588 e
= &ev
->id_index
.entries
[i
++];
1590 e
->id
= evsel
->core
.id
[j
];
1592 sid
= perf_evlist__id2sid(evlist
, e
->id
);
1604 sz
= sizeof(struct perf_record_id_index
) + nr
* sizeof(struct id_index_entry
);
1605 ev
->id_index
.header
.size
= sz
;
1606 ev
->id_index
.nr
= nr
;
1608 err
= process(tool
, ev
, NULL
, machine
);
1615 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
1616 struct target
*target
, struct perf_thread_map
*threads
,
1617 perf_event__handler_t process
, bool data_mmap
,
1618 unsigned int nr_threads_synthesize
)
1620 if (target__has_task(target
))
1621 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
);
1622 else if (target__has_cpu(target
))
1623 return perf_event__synthesize_threads(tool
, process
,
1625 nr_threads_synthesize
);
1626 /* command specified */
1630 int machine__synthesize_threads(struct machine
*machine
, struct target
*target
,
1631 struct perf_thread_map
*threads
, bool data_mmap
,
1632 unsigned int nr_threads_synthesize
)
1634 return __machine__synthesize_threads(machine
, NULL
, target
, threads
,
1635 perf_event__process
, data_mmap
,
1636 nr_threads_synthesize
);
1639 static struct perf_record_event_update
*event_update_event__new(size_t size
, u64 type
, u64 id
)
1641 struct perf_record_event_update
*ev
;
1643 size
+= sizeof(*ev
);
1644 size
= PERF_ALIGN(size
, sizeof(u64
));
1648 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
1649 ev
->header
.size
= (u16
)size
;
1656 int perf_event__synthesize_event_update_unit(struct perf_tool
*tool
, struct evsel
*evsel
,
1657 perf_event__handler_t process
)
1659 size_t size
= strlen(evsel
->unit
);
1660 struct perf_record_event_update
*ev
;
1663 ev
= event_update_event__new(size
+ 1, PERF_EVENT_UPDATE__UNIT
, evsel
->core
.id
[0]);
1667 strlcpy(ev
->data
, evsel
->unit
, size
+ 1);
1668 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
1673 int perf_event__synthesize_event_update_scale(struct perf_tool
*tool
, struct evsel
*evsel
,
1674 perf_event__handler_t process
)
1676 struct perf_record_event_update
*ev
;
1677 struct perf_record_event_update_scale
*ev_data
;
1680 ev
= event_update_event__new(sizeof(*ev_data
), PERF_EVENT_UPDATE__SCALE
, evsel
->core
.id
[0]);
1684 ev_data
= (struct perf_record_event_update_scale
*)ev
->data
;
1685 ev_data
->scale
= evsel
->scale
;
1686 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
1691 int perf_event__synthesize_event_update_name(struct perf_tool
*tool
, struct evsel
*evsel
,
1692 perf_event__handler_t process
)
1694 struct perf_record_event_update
*ev
;
1695 size_t len
= strlen(evsel
->name
);
1698 ev
= event_update_event__new(len
+ 1, PERF_EVENT_UPDATE__NAME
, evsel
->core
.id
[0]);
1702 strlcpy(ev
->data
, evsel
->name
, len
+ 1);
1703 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
1708 int perf_event__synthesize_event_update_cpus(struct perf_tool
*tool
, struct evsel
*evsel
,
1709 perf_event__handler_t process
)
1711 size_t size
= sizeof(struct perf_record_event_update
);
1712 struct perf_record_event_update
*ev
;
1716 if (!evsel
->core
.own_cpus
)
1719 ev
= cpu_map_data__alloc(evsel
->core
.own_cpus
, &size
, &type
, &max
);
1723 ev
->header
.type
= PERF_RECORD_EVENT_UPDATE
;
1724 ev
->header
.size
= (u16
)size
;
1725 ev
->type
= PERF_EVENT_UPDATE__CPUS
;
1726 ev
->id
= evsel
->core
.id
[0];
1728 cpu_map_data__synthesize((struct perf_record_cpu_map_data
*)ev
->data
,
1729 evsel
->core
.own_cpus
, type
, max
);
1731 err
= process(tool
, (union perf_event
*)ev
, NULL
, NULL
);
1736 int perf_event__synthesize_attrs(struct perf_tool
*tool
, struct evlist
*evlist
,
1737 perf_event__handler_t process
)
1739 struct evsel
*evsel
;
1742 evlist__for_each_entry(evlist
, evsel
) {
1743 err
= perf_event__synthesize_attr(tool
, &evsel
->core
.attr
, evsel
->core
.ids
,
1744 evsel
->core
.id
, process
);
1746 pr_debug("failed to create perf header attribute\n");
1754 static bool has_unit(struct evsel
*evsel
)
1756 return evsel
->unit
&& *evsel
->unit
;
1759 static bool has_scale(struct evsel
*evsel
)
1761 return evsel
->scale
!= 1;
1764 int perf_event__synthesize_extra_attr(struct perf_tool
*tool
, struct evlist
*evsel_list
,
1765 perf_event__handler_t process
, bool is_pipe
)
1767 struct evsel
*evsel
;
1771 * Synthesize other events stuff not carried within
1772 * attr event - unit, scale, name
1774 evlist__for_each_entry(evsel_list
, evsel
) {
1775 if (!evsel
->supported
)
1779 * Synthesize unit and scale only if it's defined.
1781 if (has_unit(evsel
)) {
1782 err
= perf_event__synthesize_event_update_unit(tool
, evsel
, process
);
1784 pr_err("Couldn't synthesize evsel unit.\n");
1789 if (has_scale(evsel
)) {
1790 err
= perf_event__synthesize_event_update_scale(tool
, evsel
, process
);
1792 pr_err("Couldn't synthesize evsel evsel.\n");
1797 if (evsel
->core
.own_cpus
) {
1798 err
= perf_event__synthesize_event_update_cpus(tool
, evsel
, process
);
1800 pr_err("Couldn't synthesize evsel cpus.\n");
1806 * Name is needed only for pipe output,
1807 * perf.data carries event names.
1810 err
= perf_event__synthesize_event_update_name(tool
, evsel
, process
);
1812 pr_err("Couldn't synthesize evsel name.\n");
1820 int perf_event__synthesize_attr(struct perf_tool
*tool
, struct perf_event_attr
*attr
,
1821 u32 ids
, u64
*id
, perf_event__handler_t process
)
1823 union perf_event
*ev
;
1827 size
= sizeof(struct perf_event_attr
);
1828 size
= PERF_ALIGN(size
, sizeof(u64
));
1829 size
+= sizeof(struct perf_event_header
);
1830 size
+= ids
* sizeof(u64
);
1837 ev
->attr
.attr
= *attr
;
1838 memcpy(ev
->attr
.id
, id
, ids
* sizeof(u64
));
1840 ev
->attr
.header
.type
= PERF_RECORD_HEADER_ATTR
;
1841 ev
->attr
.header
.size
= (u16
)size
;
1843 if (ev
->attr
.header
.size
== size
)
1844 err
= process(tool
, ev
, NULL
, NULL
);
1853 int perf_event__synthesize_tracing_data(struct perf_tool
*tool
, int fd
, struct evlist
*evlist
,
1854 perf_event__handler_t process
)
1856 union perf_event ev
;
1857 struct tracing_data
*tdata
;
1858 ssize_t size
= 0, aligned_size
= 0, padding
;
1862 * We are going to store the size of the data followed
1863 * by the data contents. Since the fd descriptor is a pipe,
1864 * we cannot seek back to store the size of the data once
1865 * we know it. Instead we:
1867 * - write the tracing data to the temp file
1868 * - get/write the data size to pipe
1869 * - write the tracing data from the temp file
1872 tdata
= tracing_data_get(&evlist
->core
.entries
, fd
, true);
1876 memset(&ev
, 0, sizeof(ev
));
1878 ev
.tracing_data
.header
.type
= PERF_RECORD_HEADER_TRACING_DATA
;
1880 aligned_size
= PERF_ALIGN(size
, sizeof(u64
));
1881 padding
= aligned_size
- size
;
1882 ev
.tracing_data
.header
.size
= sizeof(ev
.tracing_data
);
1883 ev
.tracing_data
.size
= aligned_size
;
1885 process(tool
, &ev
, NULL
, NULL
);
1888 * The put function will copy all the tracing data
1889 * stored in temp file to the pipe.
1891 tracing_data_put(tdata
);
1893 ff
= (struct feat_fd
){ .fd
= fd
};
1894 if (write_padded(&ff
, NULL
, 0, padding
))
1897 return aligned_size
;
1900 int perf_event__synthesize_build_id(struct perf_tool
*tool
, struct dso
*pos
, u16 misc
,
1901 perf_event__handler_t process
, struct machine
*machine
)
1903 union perf_event ev
;
1909 memset(&ev
, 0, sizeof(ev
));
1911 len
= pos
->long_name_len
+ 1;
1912 len
= PERF_ALIGN(len
, NAME_ALIGN
);
1913 memcpy(&ev
.build_id
.build_id
, pos
->build_id
, sizeof(pos
->build_id
));
1914 ev
.build_id
.header
.type
= PERF_RECORD_HEADER_BUILD_ID
;
1915 ev
.build_id
.header
.misc
= misc
;
1916 ev
.build_id
.pid
= machine
->pid
;
1917 ev
.build_id
.header
.size
= sizeof(ev
.build_id
) + len
;
1918 memcpy(&ev
.build_id
.filename
, pos
->long_name
, pos
->long_name_len
);
1920 return process(tool
, &ev
, NULL
, machine
);
1923 int perf_event__synthesize_stat_events(struct perf_stat_config
*config
, struct perf_tool
*tool
,
1924 struct evlist
*evlist
, perf_event__handler_t process
, bool attrs
)
1929 err
= perf_event__synthesize_attrs(tool
, evlist
, process
);
1931 pr_err("Couldn't synthesize attrs.\n");
1936 err
= perf_event__synthesize_extra_attr(tool
, evlist
, process
, attrs
);
1937 err
= perf_event__synthesize_thread_map2(tool
, evlist
->core
.threads
, process
, NULL
);
1939 pr_err("Couldn't synthesize thread map.\n");
1943 err
= perf_event__synthesize_cpu_map(tool
, evlist
->core
.cpus
, process
, NULL
);
1945 pr_err("Couldn't synthesize thread map.\n");
1949 err
= perf_event__synthesize_stat_config(tool
, config
, process
, NULL
);
1951 pr_err("Couldn't synthesize config.\n");
1958 int __weak
perf_event__synth_time_conv(const struct perf_event_mmap_page
*pc __maybe_unused
,
1959 struct perf_tool
*tool __maybe_unused
,
1960 perf_event__handler_t process __maybe_unused
,
1961 struct machine
*machine __maybe_unused
)
1966 extern const struct perf_header_feature_ops feat_ops
[HEADER_LAST_FEATURE
];
1968 int perf_event__synthesize_features(struct perf_tool
*tool
, struct perf_session
*session
,
1969 struct evlist
*evlist
, perf_event__handler_t process
)
1971 struct perf_header
*header
= &session
->header
;
1972 struct perf_record_header_feature
*fe
;
1977 sz_hdr
= sizeof(fe
->header
);
1978 sz
= sizeof(union perf_event
);
1979 /* get a nice alignment */
1980 sz
= PERF_ALIGN(sz
, page_size
);
1982 memset(&ff
, 0, sizeof(ff
));
1984 ff
.buf
= malloc(sz
);
1988 ff
.size
= sz
- sz_hdr
;
1989 ff
.ph
= &session
->header
;
1991 for_each_set_bit(feat
, header
->adds_features
, HEADER_FEAT_BITS
) {
1992 if (!feat_ops
[feat
].synthesize
) {
1993 pr_debug("No record header feature for header :%d\n", feat
);
1997 ff
.offset
= sizeof(*fe
);
1999 ret
= feat_ops
[feat
].write(&ff
, evlist
);
2000 if (ret
|| ff
.offset
<= (ssize_t
)sizeof(*fe
)) {
2001 pr_debug("Error writing feature\n");
2004 /* ff.buf may have changed due to realloc in do_write() */
2006 memset(fe
, 0, sizeof(*fe
));
2009 fe
->header
.type
= PERF_RECORD_HEADER_FEATURE
;
2010 fe
->header
.size
= ff
.offset
;
2012 ret
= process(tool
, ff
.buf
, NULL
, NULL
);
2019 /* Send HEADER_LAST_FEATURE mark. */
2021 fe
->feat_id
= HEADER_LAST_FEATURE
;
2022 fe
->header
.type
= PERF_RECORD_HEADER_FEATURE
;
2023 fe
->header
.size
= sizeof(*fe
);
2025 ret
= process(tool
, ff
.buf
, NULL
, NULL
);