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>
41 #include <sys/types.h>
46 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
48 unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
50 int perf_tool__process_synth_event(struct perf_tool *tool,
51 union perf_event *event,
52 struct machine *machine,
53 perf_event__handler_t process)
55 struct perf_sample synth_sample = {
62 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
65 return process(tool, event, &synth_sample, machine);
69 * Assumes that the first 4095 bytes of /proc/pid/stat contains
70 * the comm, tgid and ppid.
72 static int perf_event__get_comm_ids(pid_t pid, pid_t tid, char *comm, size_t len,
73 pid_t *tgid, pid_t *ppid, bool *kernel)
79 char *name, *tgids, *ppids, *vmpeak, *threads;
85 snprintf(bf, sizeof(bf), "/proc/%d/task/%d/status", pid, tid);
87 snprintf(bf, sizeof(bf), "/proc/%d/status", tid);
89 fd = open(bf, O_RDONLY);
91 pr_debug("couldn't open %s\n", bf);
95 n = read(fd, bf, sizeof(bf) - 1);
98 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
104 name = strstr(bf, "Name:");
105 tgids = strstr(name ?: bf, "Tgid:");
106 ppids = strstr(tgids ?: bf, "PPid:");
107 vmpeak = strstr(ppids ?: bf, "VmPeak:");
112 threads = strstr(ppids ?: bf, "Threads:");
117 name = skip_spaces(name + 5); /* strlen("Name:") */
118 nl = strchr(name, '\n');
125 memcpy(comm, name, size);
128 pr_debug("Name: string not found for pid %d\n", tid);
132 tgids += 5; /* strlen("Tgid:") */
135 pr_debug("Tgid: string not found for pid %d\n", tid);
139 ppids += 5; /* strlen("PPid:") */
142 pr_debug("PPid: string not found for pid %d\n", tid);
145 if (!vmpeak && threads)
153 static int perf_event__prepare_comm(union perf_event *event, pid_t pid, pid_t tid,
154 struct machine *machine,
155 pid_t *tgid, pid_t *ppid, bool *kernel)
161 memset(&event->comm, 0, sizeof(event->comm));
163 if (machine__is_host(machine)) {
164 if (perf_event__get_comm_ids(pid, tid, event->comm.comm,
165 sizeof(event->comm.comm),
166 tgid, ppid, kernel) != 0) {
170 *tgid = machine->pid;
176 event->comm.pid = *tgid;
177 event->comm.header.type = PERF_RECORD_COMM;
179 size = strlen(event->comm.comm) + 1;
180 size = PERF_ALIGN(size, sizeof(u64));
181 memset(event->comm.comm + size, 0, machine->id_hdr_size);
182 event->comm.header.size = (sizeof(event->comm) -
183 (sizeof(event->comm.comm) - size) +
184 machine->id_hdr_size);
185 event->comm.tid = tid;
190 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
191 union perf_event *event, pid_t pid,
192 perf_event__handler_t process,
193 struct machine *machine)
198 if (perf_event__prepare_comm(event, 0, pid, machine, &tgid, &ppid,
199 &kernel_thread) != 0)
202 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
208 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
209 struct perf_ns_link_info *ns_link_info)
214 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
215 if (stat64(proc_ns, &st) == 0) {
216 ns_link_info->dev = st.st_dev;
217 ns_link_info->ino = st.st_ino;
221 int perf_event__synthesize_namespaces(struct perf_tool *tool,
222 union perf_event *event,
223 pid_t pid, pid_t tgid,
224 perf_event__handler_t process,
225 struct machine *machine)
228 struct perf_ns_link_info *ns_link_info;
230 if (!tool || !tool->namespace_events)
233 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
234 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
235 machine->id_hdr_size));
237 event->namespaces.pid = tgid;
238 event->namespaces.tid = pid;
240 event->namespaces.nr_namespaces = NR_NAMESPACES;
242 ns_link_info = event->namespaces.link_info;
244 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
245 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
248 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
250 event->namespaces.header.size = (sizeof(event->namespaces) +
251 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
252 machine->id_hdr_size);
254 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
260 static int perf_event__synthesize_fork(struct perf_tool *tool,
261 union perf_event *event,
262 pid_t pid, pid_t tgid, pid_t ppid,
263 perf_event__handler_t process,
264 struct machine *machine)
266 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
269 * for main thread set parent to ppid from status file. For other
270 * threads set parent pid to main thread. ie., assume main thread
271 * spawns all threads in a process
274 event->fork.ppid = ppid;
275 event->fork.ptid = ppid;
277 event->fork.ppid = tgid;
278 event->fork.ptid = tgid;
280 event->fork.pid = tgid;
281 event->fork.tid = pid;
282 event->fork.header.type = PERF_RECORD_FORK;
283 event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
285 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
287 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
293 static bool read_proc_maps_line(struct io *io, __u64 *start, __u64 *end,
294 u32 *prot, u32 *flags, __u64 *offset,
297 ssize_t pathname_size, char *pathname)
301 char *start_pathname = pathname;
303 if (io__get_hex(io, start) != '-')
305 if (io__get_hex(io, end) != ' ')
308 /* map protection and flags bits */
310 ch = io__get_char(io);
315 ch = io__get_char(io);
320 ch = io__get_char(io);
325 ch = io__get_char(io);
329 *flags = MAP_PRIVATE;
332 if (io__get_char(io) != ' ')
335 if (io__get_hex(io, offset) != ' ')
338 if (io__get_hex(io, &temp) != ':')
341 if (io__get_hex(io, &temp) != ' ')
345 ch = io__get_dec(io, inode);
351 ch = io__get_char(io);
356 if (ch == '\0' || ch == '\n' ||
357 (pathname + 1 - start_pathname) >= pathname_size) {
362 ch = io__get_char(io);
366 static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
373 rc = sysfs__read_build_id("/sys/kernel/notes", &bid);
375 rc = filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1;
378 memcpy(event->build_id, bid.data, sizeof(bid.data));
379 event->build_id_size = (u8) bid.size;
380 event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
381 event->__reserved_1 = 0;
382 event->__reserved_2 = 0;
384 if (event->filename[0] == '/') {
385 pr_debug2("Failed to read build ID for %s\n",
391 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
392 union perf_event *event,
393 pid_t pid, pid_t tgid,
394 perf_event__handler_t process,
395 struct machine *machine,
398 unsigned long long t;
401 bool truncation = false;
402 unsigned long long timeout = proc_map_timeout * 1000000ULL;
404 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
405 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
407 if (machine__is_default_guest(machine))
410 snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
411 machine->root_dir, pid, pid);
413 io.fd = open(bf, O_RDONLY, 0);
416 * We raced with a task exiting - just return:
418 pr_debug("couldn't open %s\n", bf);
421 io__init(&io, io.fd, bf, sizeof(bf));
423 event->header.type = PERF_RECORD_MMAP2;
427 static const char anonstr[] = "//anon";
430 /* ensure null termination since stack will be reused. */
431 event->mmap2.filename[0] = '\0';
433 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
434 if (!read_proc_maps_line(&io,
443 sizeof(event->mmap2.filename),
444 event->mmap2.filename))
447 if ((rdclock() - t) > timeout) {
448 pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
449 "You may want to increase "
450 "the time limit by --proc-map-timeout\n",
451 machine->root_dir, pid, pid);
456 event->mmap2.ino_generation = 0;
459 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
461 if (machine__is_host(machine))
462 event->header.misc = PERF_RECORD_MISC_USER;
464 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
466 if ((event->mmap2.prot & PROT_EXEC) == 0) {
467 if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
470 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
475 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
477 if (!strcmp(event->mmap2.filename, ""))
478 strcpy(event->mmap2.filename, anonstr);
480 if (hugetlbfs_mnt_len &&
481 !strncmp(event->mmap2.filename, hugetlbfs_mnt,
482 hugetlbfs_mnt_len)) {
483 strcpy(event->mmap2.filename, anonstr);
484 event->mmap2.flags |= MAP_HUGETLB;
487 size = strlen(event->mmap2.filename) + 1;
488 size = PERF_ALIGN(size, sizeof(u64));
489 event->mmap2.len -= event->mmap.start;
490 event->mmap2.header.size = (sizeof(event->mmap2) -
491 (sizeof(event->mmap2.filename) - size));
492 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
493 event->mmap2.header.size += machine->id_hdr_size;
494 event->mmap2.pid = tgid;
495 event->mmap2.tid = pid;
497 if (symbol_conf.buildid_mmap2)
498 perf_record_mmap2__read_build_id(&event->mmap2, false);
500 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
513 #ifdef HAVE_FILE_HANDLE
514 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
515 union perf_event *event,
516 char *path, size_t mount_len,
517 perf_event__handler_t process,
518 struct machine *machine)
520 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
521 size_t path_len = strlen(path) - mount_len + 1;
523 struct file_handle fh;
528 while (path_len % sizeof(u64))
529 path[mount_len + path_len++] = '\0';
531 memset(&event->cgroup, 0, event_size);
533 event->cgroup.header.type = PERF_RECORD_CGROUP;
534 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
536 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
537 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
538 pr_debug("stat failed: %s\n", path);
542 event->cgroup.id = handle.cgroup_id;
543 strncpy(event->cgroup.path, path + mount_len, path_len);
544 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
546 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
547 pr_debug("process synth event failed\n");
554 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
555 union perf_event *event,
556 char *path, size_t mount_len,
557 perf_event__handler_t process,
558 struct machine *machine)
560 size_t pos = strlen(path);
565 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
566 process, machine) < 0)
571 pr_debug("failed to open directory: %s\n", path);
575 while ((dent = readdir(d)) != NULL) {
576 if (dent->d_type != DT_DIR)
578 if (!strcmp(dent->d_name, ".") ||
579 !strcmp(dent->d_name, ".."))
582 /* any sane path should be less than PATH_MAX */
583 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
586 if (path[pos - 1] != '/')
588 strcat(path, dent->d_name);
590 ret = perf_event__walk_cgroup_tree(tool, event, path,
591 mount_len, process, machine);
602 int perf_event__synthesize_cgroups(struct perf_tool *tool,
603 perf_event__handler_t process,
604 struct machine *machine)
606 union perf_event event;
607 char cgrp_root[PATH_MAX];
608 size_t mount_len; /* length of mount point in the path */
610 if (!tool || !tool->cgroup_events)
613 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
614 pr_debug("cannot find cgroup mount point\n");
618 mount_len = strlen(cgrp_root);
619 /* make sure the path starts with a slash (after mount point) */
620 strcat(cgrp_root, "/");
622 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
623 process, machine) < 0)
629 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
630 perf_event__handler_t process __maybe_unused,
631 struct machine *machine __maybe_unused)
637 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
638 struct machine *machine)
642 struct maps *maps = machine__kernel_maps(machine);
643 union perf_event *event;
644 size_t size = symbol_conf.buildid_mmap2 ?
645 sizeof(event->mmap2) : sizeof(event->mmap);
647 event = zalloc(size + machine->id_hdr_size);
649 pr_debug("Not enough memory synthesizing mmap event "
650 "for kernel modules\n");
655 * kernel uses 0 for user space maps, see kernel/perf_event.c
658 if (machine__is_host(machine))
659 event->header.misc = PERF_RECORD_MISC_KERNEL;
661 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
663 maps__for_each_entry(maps, pos) {
664 if (!__map__is_kmodule(pos))
667 if (symbol_conf.buildid_mmap2) {
668 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
669 event->mmap2.header.type = PERF_RECORD_MMAP2;
670 event->mmap2.header.size = (sizeof(event->mmap2) -
671 (sizeof(event->mmap2.filename) - size));
672 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
673 event->mmap2.header.size += machine->id_hdr_size;
674 event->mmap2.start = pos->start;
675 event->mmap2.len = pos->end - pos->start;
676 event->mmap2.pid = machine->pid;
678 memcpy(event->mmap2.filename, pos->dso->long_name,
679 pos->dso->long_name_len + 1);
681 perf_record_mmap2__read_build_id(&event->mmap2, false);
683 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
684 event->mmap.header.type = PERF_RECORD_MMAP;
685 event->mmap.header.size = (sizeof(event->mmap) -
686 (sizeof(event->mmap.filename) - size));
687 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
688 event->mmap.header.size += machine->id_hdr_size;
689 event->mmap.start = pos->start;
690 event->mmap.len = pos->end - pos->start;
691 event->mmap.pid = machine->pid;
693 memcpy(event->mmap.filename, pos->dso->long_name,
694 pos->dso->long_name_len + 1);
697 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
707 static int filter_task(const struct dirent *dirent)
709 return isdigit(dirent->d_name[0]);
712 static int __event__synthesize_thread(union perf_event *comm_event,
713 union perf_event *mmap_event,
714 union perf_event *fork_event,
715 union perf_event *namespaces_event,
716 pid_t pid, int full, perf_event__handler_t process,
717 struct perf_tool *tool, struct machine *machine, bool mmap_data)
719 char filename[PATH_MAX];
720 struct dirent **dirent;
725 /* special case: only send one comm event using passed in pid */
727 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
733 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
734 tgid, process, machine) < 0)
738 * send mmap only for thread group leader
739 * see thread__init_maps()
742 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
743 process, machine, mmap_data))
749 if (machine__is_default_guest(machine))
752 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
753 machine->root_dir, pid);
755 n = scandir(filename, &dirent, filter_task, alphasort);
759 for (i = 0; i < n; i++) {
764 _pid = strtol(dirent[i]->d_name, &end, 10);
769 if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
770 &tgid, &ppid, &kernel_thread) != 0)
773 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
774 ppid, process, machine) < 0)
777 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
778 tgid, process, machine) < 0)
782 * Send the prepared comm event
784 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
788 if (_pid == pid && !kernel_thread) {
789 /* process the parent's maps too */
790 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
791 process, machine, mmap_data);
797 for (i = 0; i < n; i++)
804 int perf_event__synthesize_thread_map(struct perf_tool *tool,
805 struct perf_thread_map *threads,
806 perf_event__handler_t process,
807 struct machine *machine,
810 union perf_event *comm_event, *mmap_event, *fork_event;
811 union perf_event *namespaces_event;
812 int err = -1, thread, j;
814 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
815 if (comm_event == NULL)
818 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
819 if (mmap_event == NULL)
822 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
823 if (fork_event == NULL)
826 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
827 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
828 machine->id_hdr_size);
829 if (namespaces_event == NULL)
833 for (thread = 0; thread < threads->nr; ++thread) {
834 if (__event__synthesize_thread(comm_event, mmap_event,
835 fork_event, namespaces_event,
836 perf_thread_map__pid(threads, thread), 0,
837 process, tool, machine,
844 * comm.pid is set to thread group id by
845 * perf_event__synthesize_comm
847 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
848 bool need_leader = true;
850 /* is thread group leader in thread_map? */
851 for (j = 0; j < threads->nr; ++j) {
852 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
858 /* if not, generate events for it */
860 __event__synthesize_thread(comm_event, mmap_event,
861 fork_event, namespaces_event,
862 comm_event->comm.pid, 0,
863 process, tool, machine,
870 free(namespaces_event);
881 static int __perf_event__synthesize_threads(struct perf_tool *tool,
882 perf_event__handler_t process,
883 struct machine *machine,
885 struct dirent **dirent,
889 union perf_event *comm_event, *mmap_event, *fork_event;
890 union perf_event *namespaces_event;
896 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
897 if (comm_event == NULL)
900 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
901 if (mmap_event == NULL)
904 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
905 if (fork_event == NULL)
908 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
909 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
910 machine->id_hdr_size);
911 if (namespaces_event == NULL)
914 for (i = start; i < start + num; i++) {
915 if (!isdigit(dirent[i]->d_name[0]))
918 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
919 /* only interested in proper numerical dirents */
923 * We may race with exiting thread, so don't stop just because
924 * one thread couldn't be synthesized.
926 __event__synthesize_thread(comm_event, mmap_event, fork_event,
927 namespaces_event, pid, 1, process,
928 tool, machine, mmap_data);
932 free(namespaces_event);
943 struct synthesize_threads_arg {
944 struct perf_tool *tool;
945 perf_event__handler_t process;
946 struct machine *machine;
948 struct dirent **dirent;
953 static void *synthesize_threads_worker(void *arg)
955 struct synthesize_threads_arg *args = arg;
957 __perf_event__synthesize_threads(args->tool, args->process,
958 args->machine, args->mmap_data,
960 args->start, args->num);
964 int perf_event__synthesize_threads(struct perf_tool *tool,
965 perf_event__handler_t process,
966 struct machine *machine,
968 unsigned int nr_threads_synthesize)
970 struct synthesize_threads_arg *args = NULL;
971 pthread_t *synthesize_threads = NULL;
972 char proc_path[PATH_MAX];
973 struct dirent **dirent;
981 if (machine__is_default_guest(machine))
984 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
985 n = scandir(proc_path, &dirent, filter_task, alphasort);
989 if (nr_threads_synthesize == UINT_MAX)
990 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
992 thread_nr = nr_threads_synthesize;
994 if (thread_nr <= 1) {
995 err = __perf_event__synthesize_threads(tool, process,
1003 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
1004 if (synthesize_threads == NULL)
1007 args = calloc(sizeof(*args), thread_nr);
1011 num_per_thread = n / thread_nr;
1013 for (i = 0; i < thread_nr; i++) {
1014 args[i].tool = tool;
1015 args[i].process = process;
1016 args[i].machine = machine;
1017 args[i].mmap_data = mmap_data;
1018 args[i].dirent = dirent;
1020 for (i = 0; i < m; i++) {
1021 args[i].num = num_per_thread + 1;
1022 args[i].start = i * args[i].num;
1025 base = args[i-1].start + args[i-1].num;
1026 for (j = i; j < thread_nr; j++) {
1027 args[j].num = num_per_thread;
1028 args[j].start = base + (j - i) * args[i].num;
1031 for (i = 0; i < thread_nr; i++) {
1032 if (pthread_create(&synthesize_threads[i], NULL,
1033 synthesize_threads_worker, &args[i]))
1038 for (i = 0; i < thread_nr; i++)
1039 pthread_join(synthesize_threads[i], NULL);
1042 free(synthesize_threads);
1044 for (i = 0; i < n; i++)
1051 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
1052 perf_event__handler_t process __maybe_unused,
1053 struct machine *machine __maybe_unused)
1058 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1059 perf_event__handler_t process,
1060 struct machine *machine)
1062 union perf_event *event;
1063 size_t size = symbol_conf.buildid_mmap2 ?
1064 sizeof(event->mmap2) : sizeof(event->mmap);
1065 struct map *map = machine__kernel_map(machine);
1072 kmap = map__kmap(map);
1073 if (!kmap->ref_reloc_sym)
1077 * We should get this from /sys/kernel/sections/.text, but till that is
1078 * available use this, and after it is use this as a fallback for older
1081 event = zalloc(size + machine->id_hdr_size);
1082 if (event == NULL) {
1083 pr_debug("Not enough memory synthesizing mmap event "
1084 "for kernel modules\n");
1088 if (machine__is_host(machine)) {
1090 * kernel uses PERF_RECORD_MISC_USER for user space maps,
1091 * see kernel/perf_event.c __perf_event_mmap
1093 event->header.misc = PERF_RECORD_MISC_KERNEL;
1095 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1098 if (symbol_conf.buildid_mmap2) {
1099 size = snprintf(event->mmap2.filename, sizeof(event->mmap2.filename),
1100 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1101 size = PERF_ALIGN(size, sizeof(u64));
1102 event->mmap2.header.type = PERF_RECORD_MMAP2;
1103 event->mmap2.header.size = (sizeof(event->mmap2) -
1104 (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size);
1105 event->mmap2.pgoff = kmap->ref_reloc_sym->addr;
1106 event->mmap2.start = map->start;
1107 event->mmap2.len = map->end - event->mmap.start;
1108 event->mmap2.pid = machine->pid;
1110 perf_record_mmap2__read_build_id(&event->mmap2, true);
1112 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1113 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1114 size = PERF_ALIGN(size, sizeof(u64));
1115 event->mmap.header.type = PERF_RECORD_MMAP;
1116 event->mmap.header.size = (sizeof(event->mmap) -
1117 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1118 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1119 event->mmap.start = map->start;
1120 event->mmap.len = map->end - event->mmap.start;
1121 event->mmap.pid = machine->pid;
1124 err = perf_tool__process_synth_event(tool, event, machine, process);
1130 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1131 perf_event__handler_t process,
1132 struct machine *machine)
1136 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1140 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1143 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1144 struct perf_thread_map *threads,
1145 perf_event__handler_t process,
1146 struct machine *machine)
1148 union perf_event *event;
1151 size = sizeof(event->thread_map);
1152 size += threads->nr * sizeof(event->thread_map.entries[0]);
1154 event = zalloc(size);
1158 event->header.type = PERF_RECORD_THREAD_MAP;
1159 event->header.size = size;
1160 event->thread_map.nr = threads->nr;
1162 for (i = 0; i < threads->nr; i++) {
1163 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1164 char *comm = perf_thread_map__comm(threads, i);
1169 entry->pid = perf_thread_map__pid(threads, i);
1170 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1173 err = process(tool, event, NULL, machine);
1179 static void synthesize_cpus(struct cpu_map_entries *cpus,
1180 struct perf_cpu_map *map)
1186 for (i = 0; i < map->nr; i++)
1187 cpus->cpu[i] = map->map[i];
1190 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1191 struct perf_cpu_map *map, int max)
1195 mask->nr = BITS_TO_LONGS(max);
1196 mask->long_size = sizeof(long);
1198 for (i = 0; i < map->nr; i++)
1199 set_bit(map->map[i], mask->mask);
1202 static size_t cpus_size(struct perf_cpu_map *map)
1204 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1207 static size_t mask_size(struct perf_cpu_map *map, int *max)
1213 for (i = 0; i < map->nr; i++) {
1214 /* bit possition of the cpu is + 1 */
1215 int bit = map->map[i] + 1;
1221 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1224 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1226 size_t size_cpus, size_mask;
1227 bool is_dummy = perf_cpu_map__empty(map);
1230 * Both array and mask data have variable size based
1231 * on the number of cpus and their actual values.
1232 * The size of the 'struct perf_record_cpu_map_data' is:
1234 * array = size of 'struct cpu_map_entries' +
1235 * number of cpus * sizeof(u64)
1237 * mask = size of 'struct perf_record_record_cpu_map' +
1238 * maximum cpu bit converted to size of longs
1240 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1242 size_cpus = cpus_size(map);
1243 size_mask = mask_size(map, max);
1245 if (is_dummy || (size_cpus < size_mask)) {
1247 *type = PERF_CPU_MAP__CPUS;
1250 *type = PERF_CPU_MAP__MASK;
1253 *size += sizeof(struct perf_record_cpu_map_data);
1254 *size = PERF_ALIGN(*size, sizeof(u64));
1255 return zalloc(*size);
1258 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1264 case PERF_CPU_MAP__CPUS:
1265 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1267 case PERF_CPU_MAP__MASK:
1268 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1274 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1276 size_t size = sizeof(struct perf_record_cpu_map);
1277 struct perf_record_cpu_map *event;
1281 event = cpu_map_data__alloc(map, &size, &type, &max);
1285 event->header.type = PERF_RECORD_CPU_MAP;
1286 event->header.size = size;
1287 event->data.type = type;
1289 cpu_map_data__synthesize(&event->data, map, type, max);
1293 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1294 struct perf_cpu_map *map,
1295 perf_event__handler_t process,
1296 struct machine *machine)
1298 struct perf_record_cpu_map *event;
1301 event = cpu_map_event__new(map);
1305 err = process(tool, (union perf_event *) event, NULL, machine);
1311 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1312 struct perf_stat_config *config,
1313 perf_event__handler_t process,
1314 struct machine *machine)
1316 struct perf_record_stat_config *event;
1317 int size, i = 0, err;
1319 size = sizeof(*event);
1320 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1322 event = zalloc(size);
1326 event->header.type = PERF_RECORD_STAT_CONFIG;
1327 event->header.size = size;
1328 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1330 #define ADD(__term, __val) \
1331 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1332 event->data[i].val = __val; \
1335 ADD(AGGR_MODE, config->aggr_mode)
1336 ADD(INTERVAL, config->interval)
1337 ADD(SCALE, config->scale)
1339 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1340 "stat config terms unbalanced\n");
1343 err = process(tool, (union perf_event *) event, NULL, machine);
1349 int perf_event__synthesize_stat(struct perf_tool *tool,
1350 u32 cpu, u32 thread, u64 id,
1351 struct perf_counts_values *count,
1352 perf_event__handler_t process,
1353 struct machine *machine)
1355 struct perf_record_stat event;
1357 event.header.type = PERF_RECORD_STAT;
1358 event.header.size = sizeof(event);
1359 event.header.misc = 0;
1363 event.thread = thread;
1364 event.val = count->val;
1365 event.ena = count->ena;
1366 event.run = count->run;
1368 return process(tool, (union perf_event *) &event, NULL, machine);
1371 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1372 u64 evtime, u64 type,
1373 perf_event__handler_t process,
1374 struct machine *machine)
1376 struct perf_record_stat_round event;
1378 event.header.type = PERF_RECORD_STAT_ROUND;
1379 event.header.size = sizeof(event);
1380 event.header.misc = 0;
1382 event.time = evtime;
1385 return process(tool, (union perf_event *) &event, NULL, machine);
1388 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1390 size_t sz, result = sizeof(struct perf_record_sample);
1392 if (type & PERF_SAMPLE_IDENTIFIER)
1393 result += sizeof(u64);
1395 if (type & PERF_SAMPLE_IP)
1396 result += sizeof(u64);
1398 if (type & PERF_SAMPLE_TID)
1399 result += sizeof(u64);
1401 if (type & PERF_SAMPLE_TIME)
1402 result += sizeof(u64);
1404 if (type & PERF_SAMPLE_ADDR)
1405 result += sizeof(u64);
1407 if (type & PERF_SAMPLE_ID)
1408 result += sizeof(u64);
1410 if (type & PERF_SAMPLE_STREAM_ID)
1411 result += sizeof(u64);
1413 if (type & PERF_SAMPLE_CPU)
1414 result += sizeof(u64);
1416 if (type & PERF_SAMPLE_PERIOD)
1417 result += sizeof(u64);
1419 if (type & PERF_SAMPLE_READ) {
1420 result += sizeof(u64);
1421 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1422 result += sizeof(u64);
1423 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1424 result += sizeof(u64);
1425 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1426 if (read_format & PERF_FORMAT_GROUP) {
1427 sz = sample->read.group.nr *
1428 sizeof(struct sample_read_value);
1431 result += sizeof(u64);
1435 if (type & PERF_SAMPLE_CALLCHAIN) {
1436 sz = (sample->callchain->nr + 1) * sizeof(u64);
1440 if (type & PERF_SAMPLE_RAW) {
1441 result += sizeof(u32);
1442 result += sample->raw_size;
1445 if (type & PERF_SAMPLE_BRANCH_STACK) {
1446 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1448 sz += 2 * sizeof(u64);
1452 if (type & PERF_SAMPLE_REGS_USER) {
1453 if (sample->user_regs.abi) {
1454 result += sizeof(u64);
1455 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1458 result += sizeof(u64);
1462 if (type & PERF_SAMPLE_STACK_USER) {
1463 sz = sample->user_stack.size;
1464 result += sizeof(u64);
1467 result += sizeof(u64);
1471 if (type & PERF_SAMPLE_WEIGHT_TYPE)
1472 result += sizeof(u64);
1474 if (type & PERF_SAMPLE_DATA_SRC)
1475 result += sizeof(u64);
1477 if (type & PERF_SAMPLE_TRANSACTION)
1478 result += sizeof(u64);
1480 if (type & PERF_SAMPLE_REGS_INTR) {
1481 if (sample->intr_regs.abi) {
1482 result += sizeof(u64);
1483 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1486 result += sizeof(u64);
1490 if (type & PERF_SAMPLE_PHYS_ADDR)
1491 result += sizeof(u64);
1493 if (type & PERF_SAMPLE_CGROUP)
1494 result += sizeof(u64);
1496 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1497 result += sizeof(u64);
1499 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1500 result += sizeof(u64);
1502 if (type & PERF_SAMPLE_AUX) {
1503 result += sizeof(u64);
1504 result += sample->aux_sample.size;
1510 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1511 const struct perf_sample *sample)
1516 * used for cross-endian analysis. See git commit 65014ab3
1517 * for why this goofiness is needed.
1521 array = event->sample.array;
1523 if (type & PERF_SAMPLE_IDENTIFIER) {
1524 *array = sample->id;
1528 if (type & PERF_SAMPLE_IP) {
1529 *array = sample->ip;
1533 if (type & PERF_SAMPLE_TID) {
1534 u.val32[0] = sample->pid;
1535 u.val32[1] = sample->tid;
1540 if (type & PERF_SAMPLE_TIME) {
1541 *array = sample->time;
1545 if (type & PERF_SAMPLE_ADDR) {
1546 *array = sample->addr;
1550 if (type & PERF_SAMPLE_ID) {
1551 *array = sample->id;
1555 if (type & PERF_SAMPLE_STREAM_ID) {
1556 *array = sample->stream_id;
1560 if (type & PERF_SAMPLE_CPU) {
1561 u.val32[0] = sample->cpu;
1567 if (type & PERF_SAMPLE_PERIOD) {
1568 *array = sample->period;
1572 if (type & PERF_SAMPLE_READ) {
1573 if (read_format & PERF_FORMAT_GROUP)
1574 *array = sample->read.group.nr;
1576 *array = sample->read.one.value;
1579 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1580 *array = sample->read.time_enabled;
1584 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1585 *array = sample->read.time_running;
1589 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1590 if (read_format & PERF_FORMAT_GROUP) {
1591 sz = sample->read.group.nr *
1592 sizeof(struct sample_read_value);
1593 memcpy(array, sample->read.group.values, sz);
1594 array = (void *)array + sz;
1596 *array = sample->read.one.id;
1601 if (type & PERF_SAMPLE_CALLCHAIN) {
1602 sz = (sample->callchain->nr + 1) * sizeof(u64);
1603 memcpy(array, sample->callchain, sz);
1604 array = (void *)array + sz;
1607 if (type & PERF_SAMPLE_RAW) {
1608 u.val32[0] = sample->raw_size;
1610 array = (void *)array + sizeof(u32);
1612 memcpy(array, sample->raw_data, sample->raw_size);
1613 array = (void *)array + sample->raw_size;
1616 if (type & PERF_SAMPLE_BRANCH_STACK) {
1617 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1619 sz += 2 * sizeof(u64);
1620 memcpy(array, sample->branch_stack, sz);
1621 array = (void *)array + sz;
1624 if (type & PERF_SAMPLE_REGS_USER) {
1625 if (sample->user_regs.abi) {
1626 *array++ = sample->user_regs.abi;
1627 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1628 memcpy(array, sample->user_regs.regs, sz);
1629 array = (void *)array + sz;
1635 if (type & PERF_SAMPLE_STACK_USER) {
1636 sz = sample->user_stack.size;
1639 memcpy(array, sample->user_stack.data, sz);
1640 array = (void *)array + sz;
1645 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
1646 *array = sample->weight;
1647 if (type & PERF_SAMPLE_WEIGHT_STRUCT)
1648 *array &= 0xffffffff;
1652 if (type & PERF_SAMPLE_DATA_SRC) {
1653 *array = sample->data_src;
1657 if (type & PERF_SAMPLE_TRANSACTION) {
1658 *array = sample->transaction;
1662 if (type & PERF_SAMPLE_REGS_INTR) {
1663 if (sample->intr_regs.abi) {
1664 *array++ = sample->intr_regs.abi;
1665 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1666 memcpy(array, sample->intr_regs.regs, sz);
1667 array = (void *)array + sz;
1673 if (type & PERF_SAMPLE_PHYS_ADDR) {
1674 *array = sample->phys_addr;
1678 if (type & PERF_SAMPLE_CGROUP) {
1679 *array = sample->cgroup;
1683 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1684 *array = sample->data_page_size;
1688 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1689 *array = sample->code_page_size;
1693 if (type & PERF_SAMPLE_AUX) {
1694 sz = sample->aux_sample.size;
1696 memcpy(array, sample->aux_sample.data, sz);
1697 array = (void *)array + sz;
1703 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1704 struct evlist *evlist, struct machine *machine)
1706 union perf_event *ev;
1707 struct evsel *evsel;
1708 size_t nr = 0, i = 0, sz, max_nr, n;
1711 pr_debug2("Synthesizing id index\n");
1713 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1714 sizeof(struct id_index_entry);
1716 evlist__for_each_entry(evlist, evsel)
1717 nr += evsel->core.ids;
1719 n = nr > max_nr ? max_nr : nr;
1720 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1725 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1726 ev->id_index.header.size = sz;
1727 ev->id_index.nr = n;
1729 evlist__for_each_entry(evlist, evsel) {
1732 for (j = 0; j < evsel->core.ids; j++) {
1733 struct id_index_entry *e;
1734 struct perf_sample_id *sid;
1737 err = process(tool, ev, NULL, machine);
1744 e = &ev->id_index.entries[i++];
1746 e->id = evsel->core.id[j];
1748 sid = evlist__id2sid(evlist, e->id);
1760 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1761 ev->id_index.header.size = sz;
1762 ev->id_index.nr = nr;
1764 err = process(tool, ev, NULL, machine);
1771 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1772 struct target *target, struct perf_thread_map *threads,
1773 perf_event__handler_t process, bool data_mmap,
1774 unsigned int nr_threads_synthesize)
1776 if (target__has_task(target))
1777 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1778 else if (target__has_cpu(target))
1779 return perf_event__synthesize_threads(tool, process,
1781 nr_threads_synthesize);
1782 /* command specified */
1786 int machine__synthesize_threads(struct machine *machine, struct target *target,
1787 struct perf_thread_map *threads, bool data_mmap,
1788 unsigned int nr_threads_synthesize)
1790 return __machine__synthesize_threads(machine, NULL, target, threads,
1791 perf_event__process, data_mmap,
1792 nr_threads_synthesize);
1795 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1797 struct perf_record_event_update *ev;
1799 size += sizeof(*ev);
1800 size = PERF_ALIGN(size, sizeof(u64));
1804 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1805 ev->header.size = (u16)size;
1812 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1813 perf_event__handler_t process)
1815 size_t size = strlen(evsel->unit);
1816 struct perf_record_event_update *ev;
1819 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1823 strlcpy(ev->data, evsel->unit, size + 1);
1824 err = process(tool, (union perf_event *)ev, NULL, NULL);
1829 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1830 perf_event__handler_t process)
1832 struct perf_record_event_update *ev;
1833 struct perf_record_event_update_scale *ev_data;
1836 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1840 ev_data = (struct perf_record_event_update_scale *)ev->data;
1841 ev_data->scale = evsel->scale;
1842 err = process(tool, (union perf_event *)ev, NULL, NULL);
1847 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1848 perf_event__handler_t process)
1850 struct perf_record_event_update *ev;
1851 size_t len = strlen(evsel->name);
1854 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1858 strlcpy(ev->data, evsel->name, len + 1);
1859 err = process(tool, (union perf_event *)ev, NULL, NULL);
1864 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1865 perf_event__handler_t process)
1867 size_t size = sizeof(struct perf_record_event_update);
1868 struct perf_record_event_update *ev;
1872 if (!evsel->core.own_cpus)
1875 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1879 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1880 ev->header.size = (u16)size;
1881 ev->type = PERF_EVENT_UPDATE__CPUS;
1882 ev->id = evsel->core.id[0];
1884 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1885 evsel->core.own_cpus, type, max);
1887 err = process(tool, (union perf_event *)ev, NULL, NULL);
1892 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1893 perf_event__handler_t process)
1895 struct evsel *evsel;
1898 evlist__for_each_entry(evlist, evsel) {
1899 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1900 evsel->core.id, process);
1902 pr_debug("failed to create perf header attribute\n");
1910 static bool has_unit(struct evsel *evsel)
1912 return evsel->unit && *evsel->unit;
1915 static bool has_scale(struct evsel *evsel)
1917 return evsel->scale != 1;
1920 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1921 perf_event__handler_t process, bool is_pipe)
1923 struct evsel *evsel;
1927 * Synthesize other events stuff not carried within
1928 * attr event - unit, scale, name
1930 evlist__for_each_entry(evsel_list, evsel) {
1931 if (!evsel->supported)
1935 * Synthesize unit and scale only if it's defined.
1937 if (has_unit(evsel)) {
1938 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1940 pr_err("Couldn't synthesize evsel unit.\n");
1945 if (has_scale(evsel)) {
1946 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1948 pr_err("Couldn't synthesize evsel evsel.\n");
1953 if (evsel->core.own_cpus) {
1954 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1956 pr_err("Couldn't synthesize evsel cpus.\n");
1962 * Name is needed only for pipe output,
1963 * perf.data carries event names.
1966 err = perf_event__synthesize_event_update_name(tool, evsel, process);
1968 pr_err("Couldn't synthesize evsel name.\n");
1976 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
1977 u32 ids, u64 *id, perf_event__handler_t process)
1979 union perf_event *ev;
1983 size = sizeof(struct perf_event_attr);
1984 size = PERF_ALIGN(size, sizeof(u64));
1985 size += sizeof(struct perf_event_header);
1986 size += ids * sizeof(u64);
1993 ev->attr.attr = *attr;
1994 memcpy(ev->attr.id, id, ids * sizeof(u64));
1996 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1997 ev->attr.header.size = (u16)size;
1999 if (ev->attr.header.size == size)
2000 err = process(tool, ev, NULL, NULL);
2009 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
2010 perf_event__handler_t process)
2012 union perf_event ev;
2013 struct tracing_data *tdata;
2014 ssize_t size = 0, aligned_size = 0, padding;
2018 * We are going to store the size of the data followed
2019 * by the data contents. Since the fd descriptor is a pipe,
2020 * we cannot seek back to store the size of the data once
2021 * we know it. Instead we:
2023 * - write the tracing data to the temp file
2024 * - get/write the data size to pipe
2025 * - write the tracing data from the temp file
2028 tdata = tracing_data_get(&evlist->core.entries, fd, true);
2032 memset(&ev, 0, sizeof(ev));
2034 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2036 aligned_size = PERF_ALIGN(size, sizeof(u64));
2037 padding = aligned_size - size;
2038 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2039 ev.tracing_data.size = aligned_size;
2041 process(tool, &ev, NULL, NULL);
2044 * The put function will copy all the tracing data
2045 * stored in temp file to the pipe.
2047 tracing_data_put(tdata);
2049 ff = (struct feat_fd){ .fd = fd };
2050 if (write_padded(&ff, NULL, 0, padding))
2053 return aligned_size;
2056 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2057 perf_event__handler_t process, struct machine *machine)
2059 union perf_event ev;
2065 memset(&ev, 0, sizeof(ev));
2067 len = pos->long_name_len + 1;
2068 len = PERF_ALIGN(len, NAME_ALIGN);
2069 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
2070 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2071 ev.build_id.header.misc = misc;
2072 ev.build_id.pid = machine->pid;
2073 ev.build_id.header.size = sizeof(ev.build_id) + len;
2074 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2076 return process(tool, &ev, NULL, machine);
2079 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2080 struct evlist *evlist, perf_event__handler_t process, bool attrs)
2085 err = perf_event__synthesize_attrs(tool, evlist, process);
2087 pr_err("Couldn't synthesize attrs.\n");
2092 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2093 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2095 pr_err("Couldn't synthesize thread map.\n");
2099 err = perf_event__synthesize_cpu_map(tool, evlist->core.cpus, process, NULL);
2101 pr_err("Couldn't synthesize thread map.\n");
2105 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2107 pr_err("Couldn't synthesize config.\n");
2114 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2116 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2117 struct evlist *evlist, perf_event__handler_t process)
2119 struct perf_header *header = &session->header;
2120 struct perf_record_header_feature *fe;
2125 sz_hdr = sizeof(fe->header);
2126 sz = sizeof(union perf_event);
2127 /* get a nice alignment */
2128 sz = PERF_ALIGN(sz, page_size);
2130 memset(&ff, 0, sizeof(ff));
2132 ff.buf = malloc(sz);
2136 ff.size = sz - sz_hdr;
2137 ff.ph = &session->header;
2139 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2140 if (!feat_ops[feat].synthesize) {
2141 pr_debug("No record header feature for header :%d\n", feat);
2145 ff.offset = sizeof(*fe);
2147 ret = feat_ops[feat].write(&ff, evlist);
2148 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2149 pr_debug("Error writing feature\n");
2152 /* ff.buf may have changed due to realloc in do_write() */
2154 memset(fe, 0, sizeof(*fe));
2157 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2158 fe->header.size = ff.offset;
2160 ret = process(tool, ff.buf, NULL, NULL);
2167 /* Send HEADER_LAST_FEATURE mark. */
2169 fe->feat_id = HEADER_LAST_FEATURE;
2170 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2171 fe->header.size = sizeof(*fe);
2173 ret = process(tool, ff.buf, NULL, NULL);