1 // SPDX-License-Identifier: GPL-2.0-only
3 #include "util/cgroup.h"
5 #include "util/debug.h"
7 #include "util/event.h"
8 #include "util/evlist.h"
9 #include "util/machine.h"
11 #include "util/map_symbol.h"
12 #include "util/branch.h"
13 #include "util/memswap.h"
14 #include "util/namespaces.h"
15 #include "util/session.h"
16 #include "util/stat.h"
17 #include "util/symbol.h"
18 #include "util/synthetic-events.h"
19 #include "util/target.h"
20 #include "util/time-utils.h"
21 #include <linux/bitops.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/zalloc.h>
25 #include <linux/perf_event.h>
27 #include <perf/evsel.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";
428 size_t size, aligned_size;
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 aligned_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) - aligned_size));
492 memset(event->mmap2.filename + size, 0, machine->id_hdr_size +
493 (aligned_size - size));
494 event->mmap2.header.size += machine->id_hdr_size;
495 event->mmap2.pid = tgid;
496 event->mmap2.tid = pid;
498 if (symbol_conf.buildid_mmap2)
499 perf_record_mmap2__read_build_id(&event->mmap2, false);
501 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
514 #ifdef HAVE_FILE_HANDLE
515 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
516 union perf_event *event,
517 char *path, size_t mount_len,
518 perf_event__handler_t process,
519 struct machine *machine)
521 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
522 size_t path_len = strlen(path) - mount_len + 1;
524 struct file_handle fh;
529 while (path_len % sizeof(u64))
530 path[mount_len + path_len++] = '\0';
532 memset(&event->cgroup, 0, event_size);
534 event->cgroup.header.type = PERF_RECORD_CGROUP;
535 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
537 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
538 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
539 pr_debug("stat failed: %s\n", path);
543 event->cgroup.id = handle.cgroup_id;
544 strncpy(event->cgroup.path, path + mount_len, path_len);
545 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
547 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
548 pr_debug("process synth event failed\n");
555 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
556 union perf_event *event,
557 char *path, size_t mount_len,
558 perf_event__handler_t process,
559 struct machine *machine)
561 size_t pos = strlen(path);
566 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
567 process, machine) < 0)
572 pr_debug("failed to open directory: %s\n", path);
576 while ((dent = readdir(d)) != NULL) {
577 if (dent->d_type != DT_DIR)
579 if (!strcmp(dent->d_name, ".") ||
580 !strcmp(dent->d_name, ".."))
583 /* any sane path should be less than PATH_MAX */
584 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
587 if (path[pos - 1] != '/')
589 strcat(path, dent->d_name);
591 ret = perf_event__walk_cgroup_tree(tool, event, path,
592 mount_len, process, machine);
603 int perf_event__synthesize_cgroups(struct perf_tool *tool,
604 perf_event__handler_t process,
605 struct machine *machine)
607 union perf_event event;
608 char cgrp_root[PATH_MAX];
609 size_t mount_len; /* length of mount point in the path */
611 if (!tool || !tool->cgroup_events)
614 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
615 pr_debug("cannot find cgroup mount point\n");
619 mount_len = strlen(cgrp_root);
620 /* make sure the path starts with a slash (after mount point) */
621 strcat(cgrp_root, "/");
623 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
624 process, machine) < 0)
630 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
631 perf_event__handler_t process __maybe_unused,
632 struct machine *machine __maybe_unused)
638 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
639 struct machine *machine)
643 struct maps *maps = machine__kernel_maps(machine);
644 union perf_event *event;
645 size_t size = symbol_conf.buildid_mmap2 ?
646 sizeof(event->mmap2) : sizeof(event->mmap);
648 event = zalloc(size + machine->id_hdr_size);
650 pr_debug("Not enough memory synthesizing mmap event "
651 "for kernel modules\n");
656 * kernel uses 0 for user space maps, see kernel/perf_event.c
659 if (machine__is_host(machine))
660 event->header.misc = PERF_RECORD_MISC_KERNEL;
662 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
664 maps__for_each_entry(maps, pos) {
665 if (!__map__is_kmodule(pos))
668 if (symbol_conf.buildid_mmap2) {
669 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
670 event->mmap2.header.type = PERF_RECORD_MMAP2;
671 event->mmap2.header.size = (sizeof(event->mmap2) -
672 (sizeof(event->mmap2.filename) - size));
673 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
674 event->mmap2.header.size += machine->id_hdr_size;
675 event->mmap2.start = pos->start;
676 event->mmap2.len = pos->end - pos->start;
677 event->mmap2.pid = machine->pid;
679 memcpy(event->mmap2.filename, pos->dso->long_name,
680 pos->dso->long_name_len + 1);
682 perf_record_mmap2__read_build_id(&event->mmap2, false);
684 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
685 event->mmap.header.type = PERF_RECORD_MMAP;
686 event->mmap.header.size = (sizeof(event->mmap) -
687 (sizeof(event->mmap.filename) - size));
688 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
689 event->mmap.header.size += machine->id_hdr_size;
690 event->mmap.start = pos->start;
691 event->mmap.len = pos->end - pos->start;
692 event->mmap.pid = machine->pid;
694 memcpy(event->mmap.filename, pos->dso->long_name,
695 pos->dso->long_name_len + 1);
698 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
708 static int filter_task(const struct dirent *dirent)
710 return isdigit(dirent->d_name[0]);
713 static int __event__synthesize_thread(union perf_event *comm_event,
714 union perf_event *mmap_event,
715 union perf_event *fork_event,
716 union perf_event *namespaces_event,
717 pid_t pid, int full, perf_event__handler_t process,
718 struct perf_tool *tool, struct machine *machine,
719 bool needs_mmap, bool mmap_data)
721 char filename[PATH_MAX];
722 struct dirent **dirent;
727 /* special case: only send one comm event using passed in pid */
729 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
735 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
736 tgid, process, machine) < 0)
740 * send mmap only for thread group leader
741 * see thread__init_maps()
743 if (pid == tgid && needs_mmap &&
744 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
745 process, machine, mmap_data))
751 if (machine__is_default_guest(machine))
754 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
755 machine->root_dir, pid);
757 n = scandir(filename, &dirent, filter_task, NULL);
761 for (i = 0; i < n; i++) {
764 bool kernel_thread = false;
766 _pid = strtol(dirent[i]->d_name, &end, 10);
770 /* some threads may exit just after scan, ignore it */
771 if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
772 &tgid, &ppid, &kernel_thread) != 0)
776 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
777 ppid, process, machine) < 0)
780 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
781 tgid, process, machine) < 0)
785 * Send the prepared comm event
787 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
791 if (_pid == pid && !kernel_thread && needs_mmap) {
792 /* process the parent's maps too */
793 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
794 process, machine, mmap_data);
800 for (i = 0; i < n; i++)
807 int perf_event__synthesize_thread_map(struct perf_tool *tool,
808 struct perf_thread_map *threads,
809 perf_event__handler_t process,
810 struct machine *machine,
811 bool needs_mmap, bool mmap_data)
813 union perf_event *comm_event, *mmap_event, *fork_event;
814 union perf_event *namespaces_event;
815 int err = -1, thread, j;
817 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
818 if (comm_event == NULL)
821 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
822 if (mmap_event == NULL)
825 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
826 if (fork_event == NULL)
829 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
830 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
831 machine->id_hdr_size);
832 if (namespaces_event == NULL)
836 for (thread = 0; thread < threads->nr; ++thread) {
837 if (__event__synthesize_thread(comm_event, mmap_event,
838 fork_event, namespaces_event,
839 perf_thread_map__pid(threads, thread), 0,
840 process, tool, machine,
841 needs_mmap, mmap_data)) {
847 * comm.pid is set to thread group id by
848 * perf_event__synthesize_comm
850 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
851 bool need_leader = true;
853 /* is thread group leader in thread_map? */
854 for (j = 0; j < threads->nr; ++j) {
855 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
861 /* if not, generate events for it */
863 __event__synthesize_thread(comm_event, mmap_event,
864 fork_event, namespaces_event,
865 comm_event->comm.pid, 0,
866 process, tool, machine,
867 needs_mmap, mmap_data)) {
873 free(namespaces_event);
884 static int __perf_event__synthesize_threads(struct perf_tool *tool,
885 perf_event__handler_t process,
886 struct machine *machine,
889 struct dirent **dirent,
893 union perf_event *comm_event, *mmap_event, *fork_event;
894 union perf_event *namespaces_event;
900 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
901 if (comm_event == NULL)
904 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
905 if (mmap_event == NULL)
908 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
909 if (fork_event == NULL)
912 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
913 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
914 machine->id_hdr_size);
915 if (namespaces_event == NULL)
918 for (i = start; i < start + num; i++) {
919 if (!isdigit(dirent[i]->d_name[0]))
922 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
923 /* only interested in proper numerical dirents */
927 * We may race with exiting thread, so don't stop just because
928 * one thread couldn't be synthesized.
930 __event__synthesize_thread(comm_event, mmap_event, fork_event,
931 namespaces_event, pid, 1, process,
932 tool, machine, needs_mmap, mmap_data);
936 free(namespaces_event);
947 struct synthesize_threads_arg {
948 struct perf_tool *tool;
949 perf_event__handler_t process;
950 struct machine *machine;
953 struct dirent **dirent;
958 static void *synthesize_threads_worker(void *arg)
960 struct synthesize_threads_arg *args = arg;
962 __perf_event__synthesize_threads(args->tool, args->process,
964 args->needs_mmap, args->mmap_data,
966 args->start, args->num);
970 int perf_event__synthesize_threads(struct perf_tool *tool,
971 perf_event__handler_t process,
972 struct machine *machine,
973 bool needs_mmap, bool mmap_data,
974 unsigned int nr_threads_synthesize)
976 struct synthesize_threads_arg *args = NULL;
977 pthread_t *synthesize_threads = NULL;
978 char proc_path[PATH_MAX];
979 struct dirent **dirent;
987 if (machine__is_default_guest(machine))
990 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
991 n = scandir(proc_path, &dirent, filter_task, NULL);
995 if (nr_threads_synthesize == UINT_MAX)
996 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
998 thread_nr = nr_threads_synthesize;
1000 if (thread_nr <= 1) {
1001 err = __perf_event__synthesize_threads(tool, process,
1003 needs_mmap, mmap_data,
1010 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
1011 if (synthesize_threads == NULL)
1014 args = calloc(sizeof(*args), thread_nr);
1018 num_per_thread = n / thread_nr;
1020 for (i = 0; i < thread_nr; i++) {
1021 args[i].tool = tool;
1022 args[i].process = process;
1023 args[i].machine = machine;
1024 args[i].needs_mmap = needs_mmap;
1025 args[i].mmap_data = mmap_data;
1026 args[i].dirent = dirent;
1028 for (i = 0; i < m; i++) {
1029 args[i].num = num_per_thread + 1;
1030 args[i].start = i * args[i].num;
1033 base = args[i-1].start + args[i-1].num;
1034 for (j = i; j < thread_nr; j++) {
1035 args[j].num = num_per_thread;
1036 args[j].start = base + (j - i) * args[i].num;
1039 for (i = 0; i < thread_nr; i++) {
1040 if (pthread_create(&synthesize_threads[i], NULL,
1041 synthesize_threads_worker, &args[i]))
1046 for (i = 0; i < thread_nr; i++)
1047 pthread_join(synthesize_threads[i], NULL);
1050 free(synthesize_threads);
1052 for (i = 0; i < n; i++)
1059 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
1060 perf_event__handler_t process __maybe_unused,
1061 struct machine *machine __maybe_unused)
1066 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1067 perf_event__handler_t process,
1068 struct machine *machine)
1070 union perf_event *event;
1071 size_t size = symbol_conf.buildid_mmap2 ?
1072 sizeof(event->mmap2) : sizeof(event->mmap);
1073 struct map *map = machine__kernel_map(machine);
1080 kmap = map__kmap(map);
1081 if (!kmap->ref_reloc_sym)
1085 * We should get this from /sys/kernel/sections/.text, but till that is
1086 * available use this, and after it is use this as a fallback for older
1089 event = zalloc(size + machine->id_hdr_size);
1090 if (event == NULL) {
1091 pr_debug("Not enough memory synthesizing mmap event "
1092 "for kernel modules\n");
1096 if (machine__is_host(machine)) {
1098 * kernel uses PERF_RECORD_MISC_USER for user space maps,
1099 * see kernel/perf_event.c __perf_event_mmap
1101 event->header.misc = PERF_RECORD_MISC_KERNEL;
1103 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1106 if (symbol_conf.buildid_mmap2) {
1107 size = snprintf(event->mmap2.filename, sizeof(event->mmap2.filename),
1108 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1109 size = PERF_ALIGN(size, sizeof(u64));
1110 event->mmap2.header.type = PERF_RECORD_MMAP2;
1111 event->mmap2.header.size = (sizeof(event->mmap2) -
1112 (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size);
1113 event->mmap2.pgoff = kmap->ref_reloc_sym->addr;
1114 event->mmap2.start = map->start;
1115 event->mmap2.len = map->end - event->mmap.start;
1116 event->mmap2.pid = machine->pid;
1118 perf_record_mmap2__read_build_id(&event->mmap2, true);
1120 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1121 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1122 size = PERF_ALIGN(size, sizeof(u64));
1123 event->mmap.header.type = PERF_RECORD_MMAP;
1124 event->mmap.header.size = (sizeof(event->mmap) -
1125 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1126 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1127 event->mmap.start = map->start;
1128 event->mmap.len = map->end - event->mmap.start;
1129 event->mmap.pid = machine->pid;
1132 err = perf_tool__process_synth_event(tool, event, machine, process);
1138 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1139 perf_event__handler_t process,
1140 struct machine *machine)
1144 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1148 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1151 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1152 struct perf_thread_map *threads,
1153 perf_event__handler_t process,
1154 struct machine *machine)
1156 union perf_event *event;
1159 size = sizeof(event->thread_map);
1160 size += threads->nr * sizeof(event->thread_map.entries[0]);
1162 event = zalloc(size);
1166 event->header.type = PERF_RECORD_THREAD_MAP;
1167 event->header.size = size;
1168 event->thread_map.nr = threads->nr;
1170 for (i = 0; i < threads->nr; i++) {
1171 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1172 char *comm = perf_thread_map__comm(threads, i);
1177 entry->pid = perf_thread_map__pid(threads, i);
1178 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1181 err = process(tool, event, NULL, machine);
1187 static void synthesize_cpus(struct cpu_map_entries *cpus,
1188 struct perf_cpu_map *map)
1190 int i, map_nr = perf_cpu_map__nr(map);
1194 for (i = 0; i < map_nr; i++)
1195 cpus->cpu[i] = perf_cpu_map__cpu(map, i).cpu;
1198 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1199 struct perf_cpu_map *map, int max)
1203 mask->nr = BITS_TO_LONGS(max);
1204 mask->long_size = sizeof(long);
1206 for (i = 0; i < perf_cpu_map__nr(map); i++)
1207 set_bit(perf_cpu_map__cpu(map, i).cpu, mask->mask);
1210 static size_t cpus_size(struct perf_cpu_map *map)
1212 return sizeof(struct cpu_map_entries) + perf_cpu_map__nr(map) * sizeof(u16);
1215 static size_t mask_size(struct perf_cpu_map *map, int *max)
1221 for (i = 0; i < perf_cpu_map__nr(map); i++) {
1222 /* bit position of the cpu is + 1 */
1223 int bit = perf_cpu_map__cpu(map, i).cpu + 1;
1229 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1232 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1234 size_t size_cpus, size_mask;
1235 bool is_dummy = perf_cpu_map__empty(map);
1238 * Both array and mask data have variable size based
1239 * on the number of cpus and their actual values.
1240 * The size of the 'struct perf_record_cpu_map_data' is:
1242 * array = size of 'struct cpu_map_entries' +
1243 * number of cpus * sizeof(u64)
1245 * mask = size of 'struct perf_record_record_cpu_map' +
1246 * maximum cpu bit converted to size of longs
1248 * and finally + the size of 'struct perf_record_cpu_map_data'.
1250 size_cpus = cpus_size(map);
1251 size_mask = mask_size(map, max);
1253 if (is_dummy || (size_cpus < size_mask)) {
1255 *type = PERF_CPU_MAP__CPUS;
1258 *type = PERF_CPU_MAP__MASK;
1261 *size += sizeof(struct perf_record_cpu_map_data);
1262 *size = PERF_ALIGN(*size, sizeof(u64));
1263 return zalloc(*size);
1266 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1272 case PERF_CPU_MAP__CPUS:
1273 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1275 case PERF_CPU_MAP__MASK:
1276 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1282 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1284 size_t size = sizeof(struct perf_record_cpu_map);
1285 struct perf_record_cpu_map *event;
1289 event = cpu_map_data__alloc(map, &size, &type, &max);
1293 event->header.type = PERF_RECORD_CPU_MAP;
1294 event->header.size = size;
1295 event->data.type = type;
1297 cpu_map_data__synthesize(&event->data, map, type, max);
1301 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1302 struct perf_cpu_map *map,
1303 perf_event__handler_t process,
1304 struct machine *machine)
1306 struct perf_record_cpu_map *event;
1309 event = cpu_map_event__new(map);
1313 err = process(tool, (union perf_event *) event, NULL, machine);
1319 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1320 struct perf_stat_config *config,
1321 perf_event__handler_t process,
1322 struct machine *machine)
1324 struct perf_record_stat_config *event;
1325 int size, i = 0, err;
1327 size = sizeof(*event);
1328 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1330 event = zalloc(size);
1334 event->header.type = PERF_RECORD_STAT_CONFIG;
1335 event->header.size = size;
1336 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1338 #define ADD(__term, __val) \
1339 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1340 event->data[i].val = __val; \
1343 ADD(AGGR_MODE, config->aggr_mode)
1344 ADD(INTERVAL, config->interval)
1345 ADD(SCALE, config->scale)
1347 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1348 "stat config terms unbalanced\n");
1351 err = process(tool, (union perf_event *) event, NULL, machine);
1357 int perf_event__synthesize_stat(struct perf_tool *tool,
1358 struct perf_cpu cpu, u32 thread, u64 id,
1359 struct perf_counts_values *count,
1360 perf_event__handler_t process,
1361 struct machine *machine)
1363 struct perf_record_stat event;
1365 event.header.type = PERF_RECORD_STAT;
1366 event.header.size = sizeof(event);
1367 event.header.misc = 0;
1370 event.cpu = cpu.cpu;
1371 event.thread = thread;
1372 event.val = count->val;
1373 event.ena = count->ena;
1374 event.run = count->run;
1376 return process(tool, (union perf_event *) &event, NULL, machine);
1379 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1380 u64 evtime, u64 type,
1381 perf_event__handler_t process,
1382 struct machine *machine)
1384 struct perf_record_stat_round event;
1386 event.header.type = PERF_RECORD_STAT_ROUND;
1387 event.header.size = sizeof(event);
1388 event.header.misc = 0;
1390 event.time = evtime;
1393 return process(tool, (union perf_event *) &event, NULL, machine);
1396 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1398 size_t sz, result = sizeof(struct perf_record_sample);
1400 if (type & PERF_SAMPLE_IDENTIFIER)
1401 result += sizeof(u64);
1403 if (type & PERF_SAMPLE_IP)
1404 result += sizeof(u64);
1406 if (type & PERF_SAMPLE_TID)
1407 result += sizeof(u64);
1409 if (type & PERF_SAMPLE_TIME)
1410 result += sizeof(u64);
1412 if (type & PERF_SAMPLE_ADDR)
1413 result += sizeof(u64);
1415 if (type & PERF_SAMPLE_ID)
1416 result += sizeof(u64);
1418 if (type & PERF_SAMPLE_STREAM_ID)
1419 result += sizeof(u64);
1421 if (type & PERF_SAMPLE_CPU)
1422 result += sizeof(u64);
1424 if (type & PERF_SAMPLE_PERIOD)
1425 result += sizeof(u64);
1427 if (type & PERF_SAMPLE_READ) {
1428 result += sizeof(u64);
1429 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1430 result += sizeof(u64);
1431 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1432 result += sizeof(u64);
1433 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1434 if (read_format & PERF_FORMAT_GROUP) {
1435 sz = sample->read.group.nr *
1436 sizeof(struct sample_read_value);
1439 result += sizeof(u64);
1443 if (type & PERF_SAMPLE_CALLCHAIN) {
1444 sz = (sample->callchain->nr + 1) * sizeof(u64);
1448 if (type & PERF_SAMPLE_RAW) {
1449 result += sizeof(u32);
1450 result += sample->raw_size;
1453 if (type & PERF_SAMPLE_BRANCH_STACK) {
1454 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1456 sz += 2 * sizeof(u64);
1460 if (type & PERF_SAMPLE_REGS_USER) {
1461 if (sample->user_regs.abi) {
1462 result += sizeof(u64);
1463 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1466 result += sizeof(u64);
1470 if (type & PERF_SAMPLE_STACK_USER) {
1471 sz = sample->user_stack.size;
1472 result += sizeof(u64);
1475 result += sizeof(u64);
1479 if (type & PERF_SAMPLE_WEIGHT_TYPE)
1480 result += sizeof(u64);
1482 if (type & PERF_SAMPLE_DATA_SRC)
1483 result += sizeof(u64);
1485 if (type & PERF_SAMPLE_TRANSACTION)
1486 result += sizeof(u64);
1488 if (type & PERF_SAMPLE_REGS_INTR) {
1489 if (sample->intr_regs.abi) {
1490 result += sizeof(u64);
1491 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1494 result += sizeof(u64);
1498 if (type & PERF_SAMPLE_PHYS_ADDR)
1499 result += sizeof(u64);
1501 if (type & PERF_SAMPLE_CGROUP)
1502 result += sizeof(u64);
1504 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1505 result += sizeof(u64);
1507 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1508 result += sizeof(u64);
1510 if (type & PERF_SAMPLE_AUX) {
1511 result += sizeof(u64);
1512 result += sample->aux_sample.size;
1518 void __weak arch_perf_synthesize_sample_weight(const struct perf_sample *data,
1519 __u64 *array, u64 type __maybe_unused)
1521 *array = data->weight;
1524 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1525 const struct perf_sample *sample)
1530 * used for cross-endian analysis. See git commit 65014ab3
1531 * for why this goofiness is needed.
1535 array = event->sample.array;
1537 if (type & PERF_SAMPLE_IDENTIFIER) {
1538 *array = sample->id;
1542 if (type & PERF_SAMPLE_IP) {
1543 *array = sample->ip;
1547 if (type & PERF_SAMPLE_TID) {
1548 u.val32[0] = sample->pid;
1549 u.val32[1] = sample->tid;
1554 if (type & PERF_SAMPLE_TIME) {
1555 *array = sample->time;
1559 if (type & PERF_SAMPLE_ADDR) {
1560 *array = sample->addr;
1564 if (type & PERF_SAMPLE_ID) {
1565 *array = sample->id;
1569 if (type & PERF_SAMPLE_STREAM_ID) {
1570 *array = sample->stream_id;
1574 if (type & PERF_SAMPLE_CPU) {
1575 u.val32[0] = sample->cpu;
1581 if (type & PERF_SAMPLE_PERIOD) {
1582 *array = sample->period;
1586 if (type & PERF_SAMPLE_READ) {
1587 if (read_format & PERF_FORMAT_GROUP)
1588 *array = sample->read.group.nr;
1590 *array = sample->read.one.value;
1593 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1594 *array = sample->read.time_enabled;
1598 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1599 *array = sample->read.time_running;
1603 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1604 if (read_format & PERF_FORMAT_GROUP) {
1605 sz = sample->read.group.nr *
1606 sizeof(struct sample_read_value);
1607 memcpy(array, sample->read.group.values, sz);
1608 array = (void *)array + sz;
1610 *array = sample->read.one.id;
1615 if (type & PERF_SAMPLE_CALLCHAIN) {
1616 sz = (sample->callchain->nr + 1) * sizeof(u64);
1617 memcpy(array, sample->callchain, sz);
1618 array = (void *)array + sz;
1621 if (type & PERF_SAMPLE_RAW) {
1622 u.val32[0] = sample->raw_size;
1624 array = (void *)array + sizeof(u32);
1626 memcpy(array, sample->raw_data, sample->raw_size);
1627 array = (void *)array + sample->raw_size;
1630 if (type & PERF_SAMPLE_BRANCH_STACK) {
1631 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1633 sz += 2 * sizeof(u64);
1634 memcpy(array, sample->branch_stack, sz);
1635 array = (void *)array + sz;
1638 if (type & PERF_SAMPLE_REGS_USER) {
1639 if (sample->user_regs.abi) {
1640 *array++ = sample->user_regs.abi;
1641 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1642 memcpy(array, sample->user_regs.regs, sz);
1643 array = (void *)array + sz;
1649 if (type & PERF_SAMPLE_STACK_USER) {
1650 sz = sample->user_stack.size;
1653 memcpy(array, sample->user_stack.data, sz);
1654 array = (void *)array + sz;
1659 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
1660 arch_perf_synthesize_sample_weight(sample, array, type);
1664 if (type & PERF_SAMPLE_DATA_SRC) {
1665 *array = sample->data_src;
1669 if (type & PERF_SAMPLE_TRANSACTION) {
1670 *array = sample->transaction;
1674 if (type & PERF_SAMPLE_REGS_INTR) {
1675 if (sample->intr_regs.abi) {
1676 *array++ = sample->intr_regs.abi;
1677 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1678 memcpy(array, sample->intr_regs.regs, sz);
1679 array = (void *)array + sz;
1685 if (type & PERF_SAMPLE_PHYS_ADDR) {
1686 *array = sample->phys_addr;
1690 if (type & PERF_SAMPLE_CGROUP) {
1691 *array = sample->cgroup;
1695 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1696 *array = sample->data_page_size;
1700 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1701 *array = sample->code_page_size;
1705 if (type & PERF_SAMPLE_AUX) {
1706 sz = sample->aux_sample.size;
1708 memcpy(array, sample->aux_sample.data, sz);
1709 array = (void *)array + sz;
1715 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1716 struct evlist *evlist, struct machine *machine)
1718 union perf_event *ev;
1719 struct evsel *evsel;
1720 size_t nr = 0, i = 0, sz, max_nr, n;
1723 pr_debug2("Synthesizing id index\n");
1725 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1726 sizeof(struct id_index_entry);
1728 evlist__for_each_entry(evlist, evsel)
1729 nr += evsel->core.ids;
1731 n = nr > max_nr ? max_nr : nr;
1732 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1737 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1738 ev->id_index.header.size = sz;
1739 ev->id_index.nr = n;
1741 evlist__for_each_entry(evlist, evsel) {
1744 for (j = 0; j < evsel->core.ids; j++) {
1745 struct id_index_entry *e;
1746 struct perf_sample_id *sid;
1749 err = process(tool, ev, NULL, machine);
1756 e = &ev->id_index.entries[i++];
1758 e->id = evsel->core.id[j];
1760 sid = evlist__id2sid(evlist, e->id);
1767 e->cpu = sid->cpu.cpu;
1772 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1773 ev->id_index.header.size = sz;
1774 ev->id_index.nr = nr;
1776 err = process(tool, ev, NULL, machine);
1783 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1784 struct target *target, struct perf_thread_map *threads,
1785 perf_event__handler_t process, bool needs_mmap,
1786 bool data_mmap, unsigned int nr_threads_synthesize)
1789 * When perf runs in non-root PID namespace, and the namespace's proc FS
1790 * is not mounted, nsinfo__is_in_root_namespace() returns false.
1791 * In this case, the proc FS is coming for the parent namespace, thus
1792 * perf tool will wrongly gather process info from its parent PID
1795 * To avoid the confusion that the perf tool runs in a child PID
1796 * namespace but it synthesizes thread info from its parent PID
1797 * namespace, returns failure with warning.
1799 if (!nsinfo__is_in_root_namespace()) {
1800 pr_err("Perf runs in non-root PID namespace but it tries to ");
1801 pr_err("gather process info from its parent PID namespace.\n");
1802 pr_err("Please mount the proc file system properly, e.g. ");
1803 pr_err("add the option '--mount-proc' for unshare command.\n");
1807 if (target__has_task(target))
1808 return perf_event__synthesize_thread_map(tool, threads, process, machine,
1809 needs_mmap, data_mmap);
1810 else if (target__has_cpu(target))
1811 return perf_event__synthesize_threads(tool, process, machine,
1812 needs_mmap, data_mmap,
1813 nr_threads_synthesize);
1814 /* command specified */
1818 int machine__synthesize_threads(struct machine *machine, struct target *target,
1819 struct perf_thread_map *threads, bool needs_mmap,
1820 bool data_mmap, unsigned int nr_threads_synthesize)
1822 return __machine__synthesize_threads(machine, NULL, target, threads,
1823 perf_event__process, needs_mmap,
1824 data_mmap, nr_threads_synthesize);
1827 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1829 struct perf_record_event_update *ev;
1831 size += sizeof(*ev);
1832 size = PERF_ALIGN(size, sizeof(u64));
1836 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1837 ev->header.size = (u16)size;
1844 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1845 perf_event__handler_t process)
1847 size_t size = strlen(evsel->unit);
1848 struct perf_record_event_update *ev;
1851 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1855 strlcpy(ev->data, evsel->unit, size + 1);
1856 err = process(tool, (union perf_event *)ev, NULL, NULL);
1861 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1862 perf_event__handler_t process)
1864 struct perf_record_event_update *ev;
1865 struct perf_record_event_update_scale *ev_data;
1868 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1872 ev_data = (struct perf_record_event_update_scale *)ev->data;
1873 ev_data->scale = evsel->scale;
1874 err = process(tool, (union perf_event *)ev, NULL, NULL);
1879 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1880 perf_event__handler_t process)
1882 struct perf_record_event_update *ev;
1883 size_t len = strlen(evsel->name);
1886 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1890 strlcpy(ev->data, evsel->name, len + 1);
1891 err = process(tool, (union perf_event *)ev, NULL, NULL);
1896 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1897 perf_event__handler_t process)
1899 size_t size = sizeof(struct perf_record_event_update);
1900 struct perf_record_event_update *ev;
1904 if (!evsel->core.own_cpus)
1907 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1911 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1912 ev->header.size = (u16)size;
1913 ev->type = PERF_EVENT_UPDATE__CPUS;
1914 ev->id = evsel->core.id[0];
1916 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1917 evsel->core.own_cpus, type, max);
1919 err = process(tool, (union perf_event *)ev, NULL, NULL);
1924 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1925 perf_event__handler_t process)
1927 struct evsel *evsel;
1930 evlist__for_each_entry(evlist, evsel) {
1931 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1932 evsel->core.id, process);
1934 pr_debug("failed to create perf header attribute\n");
1942 static bool has_unit(struct evsel *evsel)
1944 return evsel->unit && *evsel->unit;
1947 static bool has_scale(struct evsel *evsel)
1949 return evsel->scale != 1;
1952 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1953 perf_event__handler_t process, bool is_pipe)
1955 struct evsel *evsel;
1959 * Synthesize other events stuff not carried within
1960 * attr event - unit, scale, name
1962 evlist__for_each_entry(evsel_list, evsel) {
1963 if (!evsel->supported)
1967 * Synthesize unit and scale only if it's defined.
1969 if (has_unit(evsel)) {
1970 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1972 pr_err("Couldn't synthesize evsel unit.\n");
1977 if (has_scale(evsel)) {
1978 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1980 pr_err("Couldn't synthesize evsel evsel.\n");
1985 if (evsel->core.own_cpus) {
1986 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1988 pr_err("Couldn't synthesize evsel cpus.\n");
1994 * Name is needed only for pipe output,
1995 * perf.data carries event names.
1998 err = perf_event__synthesize_event_update_name(tool, evsel, process);
2000 pr_err("Couldn't synthesize evsel name.\n");
2008 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
2009 u32 ids, u64 *id, perf_event__handler_t process)
2011 union perf_event *ev;
2015 size = sizeof(struct perf_event_attr);
2016 size = PERF_ALIGN(size, sizeof(u64));
2017 size += sizeof(struct perf_event_header);
2018 size += ids * sizeof(u64);
2025 ev->attr.attr = *attr;
2026 memcpy(ev->attr.id, id, ids * sizeof(u64));
2028 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2029 ev->attr.header.size = (u16)size;
2031 if (ev->attr.header.size == size)
2032 err = process(tool, ev, NULL, NULL);
2041 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
2042 perf_event__handler_t process)
2044 union perf_event ev;
2045 struct tracing_data *tdata;
2046 ssize_t size = 0, aligned_size = 0, padding;
2050 * We are going to store the size of the data followed
2051 * by the data contents. Since the fd descriptor is a pipe,
2052 * we cannot seek back to store the size of the data once
2053 * we know it. Instead we:
2055 * - write the tracing data to the temp file
2056 * - get/write the data size to pipe
2057 * - write the tracing data from the temp file
2060 tdata = tracing_data_get(&evlist->core.entries, fd, true);
2064 memset(&ev, 0, sizeof(ev));
2066 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2068 aligned_size = PERF_ALIGN(size, sizeof(u64));
2069 padding = aligned_size - size;
2070 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2071 ev.tracing_data.size = aligned_size;
2073 process(tool, &ev, NULL, NULL);
2076 * The put function will copy all the tracing data
2077 * stored in temp file to the pipe.
2079 tracing_data_put(tdata);
2081 ff = (struct feat_fd){ .fd = fd };
2082 if (write_padded(&ff, NULL, 0, padding))
2085 return aligned_size;
2088 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2089 perf_event__handler_t process, struct machine *machine)
2091 union perf_event ev;
2097 memset(&ev, 0, sizeof(ev));
2099 len = pos->long_name_len + 1;
2100 len = PERF_ALIGN(len, NAME_ALIGN);
2101 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
2102 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2103 ev.build_id.header.misc = misc;
2104 ev.build_id.pid = machine->pid;
2105 ev.build_id.header.size = sizeof(ev.build_id) + len;
2106 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2108 return process(tool, &ev, NULL, machine);
2111 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2112 struct evlist *evlist, perf_event__handler_t process, bool attrs)
2117 err = perf_event__synthesize_attrs(tool, evlist, process);
2119 pr_err("Couldn't synthesize attrs.\n");
2124 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2125 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2127 pr_err("Couldn't synthesize thread map.\n");
2131 err = perf_event__synthesize_cpu_map(tool, evlist->core.user_requested_cpus, process, NULL);
2133 pr_err("Couldn't synthesize thread map.\n");
2137 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2139 pr_err("Couldn't synthesize config.\n");
2146 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2148 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2149 struct evlist *evlist, perf_event__handler_t process)
2151 struct perf_header *header = &session->header;
2152 struct perf_record_header_feature *fe;
2157 sz_hdr = sizeof(fe->header);
2158 sz = sizeof(union perf_event);
2159 /* get a nice alignment */
2160 sz = PERF_ALIGN(sz, page_size);
2162 memset(&ff, 0, sizeof(ff));
2164 ff.buf = malloc(sz);
2168 ff.size = sz - sz_hdr;
2169 ff.ph = &session->header;
2171 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2172 if (!feat_ops[feat].synthesize) {
2173 pr_debug("No record header feature for header :%d\n", feat);
2177 ff.offset = sizeof(*fe);
2179 ret = feat_ops[feat].write(&ff, evlist);
2180 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2181 pr_debug("Error writing feature\n");
2184 /* ff.buf may have changed due to realloc in do_write() */
2186 memset(fe, 0, sizeof(*fe));
2189 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2190 fe->header.size = ff.offset;
2192 ret = process(tool, ff.buf, NULL, NULL);
2199 /* Send HEADER_LAST_FEATURE mark. */
2201 fe->feat_id = HEADER_LAST_FEATURE;
2202 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2203 fe->header.size = sizeof(*fe);
2205 ret = process(tool, ff.buf, NULL, NULL);
2211 int perf_event__synthesize_for_pipe(struct perf_tool *tool,
2212 struct perf_session *session,
2213 struct perf_data *data,
2214 perf_event__handler_t process)
2218 struct evlist *evlist = session->evlist;
2221 * We need to synthesize events first, because some
2222 * features works on top of them (on report side).
2224 err = perf_event__synthesize_attrs(tool, evlist, process);
2226 pr_err("Couldn't synthesize attrs.\n");
2231 err = perf_event__synthesize_features(tool, session, evlist, process);
2233 pr_err("Couldn't synthesize features.\n");
2238 if (have_tracepoints(&evlist->core.entries)) {
2239 int fd = perf_data__fd(data);
2242 * FIXME err <= 0 here actually means that
2243 * there were no tracepoints so its not really
2244 * an error, just that we don't need to
2245 * synthesize anything. We really have to
2246 * return this more properly and also
2247 * propagate errors that now are calling die()
2249 err = perf_event__synthesize_tracing_data(tool, fd, evlist,
2252 pr_err("Couldn't record tracing data.\n");
2261 int parse_synth_opt(char *synth)
2269 for (q = synth; (p = strsep(&q, ",")); p = q) {
2270 if (!strcasecmp(p, "no") || !strcasecmp(p, "none"))
2273 if (!strcasecmp(p, "all"))
2274 return PERF_SYNTH_ALL;
2276 if (!strcasecmp(p, "task"))
2277 ret |= PERF_SYNTH_TASK;
2278 else if (!strcasecmp(p, "mmap"))
2279 ret |= PERF_SYNTH_TASK | PERF_SYNTH_MMAP;
2280 else if (!strcasecmp(p, "cgroup"))
2281 ret |= PERF_SYNTH_CGROUP;