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,
375 rc = sysfs__read_build_id("/sys/kernel/notes", &bid);
379 nsi = nsinfo__new(event->pid);
380 nsinfo__mountns_enter(nsi, &nc);
382 rc = filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1;
384 nsinfo__mountns_exit(&nc);
389 memcpy(event->build_id, bid.data, sizeof(bid.data));
390 event->build_id_size = (u8) bid.size;
391 event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
392 event->__reserved_1 = 0;
393 event->__reserved_2 = 0;
395 if (event->filename[0] == '/') {
396 pr_debug2("Failed to read build ID for %s\n",
402 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
403 union perf_event *event,
404 pid_t pid, pid_t tgid,
405 perf_event__handler_t process,
406 struct machine *machine,
409 unsigned long long t;
412 bool truncation = false;
413 unsigned long long timeout = proc_map_timeout * 1000000ULL;
415 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
416 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
418 if (machine__is_default_guest(machine))
421 snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
422 machine->root_dir, pid, pid);
424 io.fd = open(bf, O_RDONLY, 0);
427 * We raced with a task exiting - just return:
429 pr_debug("couldn't open %s\n", bf);
432 io__init(&io, io.fd, bf, sizeof(bf));
434 event->header.type = PERF_RECORD_MMAP2;
438 static const char anonstr[] = "//anon";
439 size_t size, aligned_size;
441 /* ensure null termination since stack will be reused. */
442 event->mmap2.filename[0] = '\0';
444 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
445 if (!read_proc_maps_line(&io,
454 sizeof(event->mmap2.filename),
455 event->mmap2.filename))
458 if ((rdclock() - t) > timeout) {
459 pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
460 "You may want to increase "
461 "the time limit by --proc-map-timeout\n",
462 machine->root_dir, pid, pid);
467 event->mmap2.ino_generation = 0;
470 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
472 if (machine__is_host(machine))
473 event->header.misc = PERF_RECORD_MISC_USER;
475 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
477 if ((event->mmap2.prot & PROT_EXEC) == 0) {
478 if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
481 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
486 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
488 if (!strcmp(event->mmap2.filename, ""))
489 strcpy(event->mmap2.filename, anonstr);
491 if (hugetlbfs_mnt_len &&
492 !strncmp(event->mmap2.filename, hugetlbfs_mnt,
493 hugetlbfs_mnt_len)) {
494 strcpy(event->mmap2.filename, anonstr);
495 event->mmap2.flags |= MAP_HUGETLB;
498 size = strlen(event->mmap2.filename) + 1;
499 aligned_size = PERF_ALIGN(size, sizeof(u64));
500 event->mmap2.len -= event->mmap.start;
501 event->mmap2.header.size = (sizeof(event->mmap2) -
502 (sizeof(event->mmap2.filename) - aligned_size));
503 memset(event->mmap2.filename + size, 0, machine->id_hdr_size +
504 (aligned_size - size));
505 event->mmap2.header.size += machine->id_hdr_size;
506 event->mmap2.pid = tgid;
507 event->mmap2.tid = pid;
509 if (symbol_conf.buildid_mmap2)
510 perf_record_mmap2__read_build_id(&event->mmap2, false);
512 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
525 #ifdef HAVE_FILE_HANDLE
526 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
527 union perf_event *event,
528 char *path, size_t mount_len,
529 perf_event__handler_t process,
530 struct machine *machine)
532 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
533 size_t path_len = strlen(path) - mount_len + 1;
535 struct file_handle fh;
540 while (path_len % sizeof(u64))
541 path[mount_len + path_len++] = '\0';
543 memset(&event->cgroup, 0, event_size);
545 event->cgroup.header.type = PERF_RECORD_CGROUP;
546 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
548 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
549 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
550 pr_debug("stat failed: %s\n", path);
554 event->cgroup.id = handle.cgroup_id;
555 strncpy(event->cgroup.path, path + mount_len, path_len);
556 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
558 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
559 pr_debug("process synth event failed\n");
566 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
567 union perf_event *event,
568 char *path, size_t mount_len,
569 perf_event__handler_t process,
570 struct machine *machine)
572 size_t pos = strlen(path);
577 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
578 process, machine) < 0)
583 pr_debug("failed to open directory: %s\n", path);
587 while ((dent = readdir(d)) != NULL) {
588 if (dent->d_type != DT_DIR)
590 if (!strcmp(dent->d_name, ".") ||
591 !strcmp(dent->d_name, ".."))
594 /* any sane path should be less than PATH_MAX */
595 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
598 if (path[pos - 1] != '/')
600 strcat(path, dent->d_name);
602 ret = perf_event__walk_cgroup_tree(tool, event, path,
603 mount_len, process, machine);
614 int perf_event__synthesize_cgroups(struct perf_tool *tool,
615 perf_event__handler_t process,
616 struct machine *machine)
618 union perf_event event;
619 char cgrp_root[PATH_MAX];
620 size_t mount_len; /* length of mount point in the path */
622 if (!tool || !tool->cgroup_events)
625 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
626 pr_debug("cannot find cgroup mount point\n");
630 mount_len = strlen(cgrp_root);
631 /* make sure the path starts with a slash (after mount point) */
632 strcat(cgrp_root, "/");
634 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
635 process, machine) < 0)
641 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
642 perf_event__handler_t process __maybe_unused,
643 struct machine *machine __maybe_unused)
649 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
650 struct machine *machine)
654 struct maps *maps = machine__kernel_maps(machine);
655 union perf_event *event;
656 size_t size = symbol_conf.buildid_mmap2 ?
657 sizeof(event->mmap2) : sizeof(event->mmap);
659 event = zalloc(size + machine->id_hdr_size);
661 pr_debug("Not enough memory synthesizing mmap event "
662 "for kernel modules\n");
667 * kernel uses 0 for user space maps, see kernel/perf_event.c
670 if (machine__is_host(machine))
671 event->header.misc = PERF_RECORD_MISC_KERNEL;
673 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
675 maps__for_each_entry(maps, pos) {
676 if (!__map__is_kmodule(pos))
679 if (symbol_conf.buildid_mmap2) {
680 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
681 event->mmap2.header.type = PERF_RECORD_MMAP2;
682 event->mmap2.header.size = (sizeof(event->mmap2) -
683 (sizeof(event->mmap2.filename) - size));
684 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
685 event->mmap2.header.size += machine->id_hdr_size;
686 event->mmap2.start = pos->start;
687 event->mmap2.len = pos->end - pos->start;
688 event->mmap2.pid = machine->pid;
690 memcpy(event->mmap2.filename, pos->dso->long_name,
691 pos->dso->long_name_len + 1);
693 perf_record_mmap2__read_build_id(&event->mmap2, false);
695 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
696 event->mmap.header.type = PERF_RECORD_MMAP;
697 event->mmap.header.size = (sizeof(event->mmap) -
698 (sizeof(event->mmap.filename) - size));
699 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
700 event->mmap.header.size += machine->id_hdr_size;
701 event->mmap.start = pos->start;
702 event->mmap.len = pos->end - pos->start;
703 event->mmap.pid = machine->pid;
705 memcpy(event->mmap.filename, pos->dso->long_name,
706 pos->dso->long_name_len + 1);
709 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
719 static int filter_task(const struct dirent *dirent)
721 return isdigit(dirent->d_name[0]);
724 static int __event__synthesize_thread(union perf_event *comm_event,
725 union perf_event *mmap_event,
726 union perf_event *fork_event,
727 union perf_event *namespaces_event,
728 pid_t pid, int full, perf_event__handler_t process,
729 struct perf_tool *tool, struct machine *machine,
730 bool needs_mmap, bool mmap_data)
732 char filename[PATH_MAX];
733 struct dirent **dirent;
738 /* special case: only send one comm event using passed in pid */
740 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
746 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
747 tgid, process, machine) < 0)
751 * send mmap only for thread group leader
752 * see thread__init_maps()
754 if (pid == tgid && needs_mmap &&
755 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
756 process, machine, mmap_data))
762 if (machine__is_default_guest(machine))
765 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
766 machine->root_dir, pid);
768 n = scandir(filename, &dirent, filter_task, NULL);
772 for (i = 0; i < n; i++) {
775 bool kernel_thread = false;
777 _pid = strtol(dirent[i]->d_name, &end, 10);
781 /* some threads may exit just after scan, ignore it */
782 if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
783 &tgid, &ppid, &kernel_thread) != 0)
787 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
788 ppid, process, machine) < 0)
791 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
792 tgid, process, machine) < 0)
796 * Send the prepared comm event
798 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
802 if (_pid == pid && !kernel_thread && needs_mmap) {
803 /* process the parent's maps too */
804 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
805 process, machine, mmap_data);
811 for (i = 0; i < n; i++)
818 int perf_event__synthesize_thread_map(struct perf_tool *tool,
819 struct perf_thread_map *threads,
820 perf_event__handler_t process,
821 struct machine *machine,
822 bool needs_mmap, bool mmap_data)
824 union perf_event *comm_event, *mmap_event, *fork_event;
825 union perf_event *namespaces_event;
826 int err = -1, thread, j;
828 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
829 if (comm_event == NULL)
832 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
833 if (mmap_event == NULL)
836 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
837 if (fork_event == NULL)
840 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
841 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
842 machine->id_hdr_size);
843 if (namespaces_event == NULL)
847 for (thread = 0; thread < threads->nr; ++thread) {
848 if (__event__synthesize_thread(comm_event, mmap_event,
849 fork_event, namespaces_event,
850 perf_thread_map__pid(threads, thread), 0,
851 process, tool, machine,
852 needs_mmap, mmap_data)) {
858 * comm.pid is set to thread group id by
859 * perf_event__synthesize_comm
861 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
862 bool need_leader = true;
864 /* is thread group leader in thread_map? */
865 for (j = 0; j < threads->nr; ++j) {
866 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
872 /* if not, generate events for it */
874 __event__synthesize_thread(comm_event, mmap_event,
875 fork_event, namespaces_event,
876 comm_event->comm.pid, 0,
877 process, tool, machine,
878 needs_mmap, mmap_data)) {
884 free(namespaces_event);
895 static int __perf_event__synthesize_threads(struct perf_tool *tool,
896 perf_event__handler_t process,
897 struct machine *machine,
900 struct dirent **dirent,
904 union perf_event *comm_event, *mmap_event, *fork_event;
905 union perf_event *namespaces_event;
911 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
912 if (comm_event == NULL)
915 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
916 if (mmap_event == NULL)
919 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
920 if (fork_event == NULL)
923 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
924 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
925 machine->id_hdr_size);
926 if (namespaces_event == NULL)
929 for (i = start; i < start + num; i++) {
930 if (!isdigit(dirent[i]->d_name[0]))
933 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
934 /* only interested in proper numerical dirents */
938 * We may race with exiting thread, so don't stop just because
939 * one thread couldn't be synthesized.
941 __event__synthesize_thread(comm_event, mmap_event, fork_event,
942 namespaces_event, pid, 1, process,
943 tool, machine, needs_mmap, mmap_data);
947 free(namespaces_event);
958 struct synthesize_threads_arg {
959 struct perf_tool *tool;
960 perf_event__handler_t process;
961 struct machine *machine;
964 struct dirent **dirent;
969 static void *synthesize_threads_worker(void *arg)
971 struct synthesize_threads_arg *args = arg;
973 __perf_event__synthesize_threads(args->tool, args->process,
975 args->needs_mmap, args->mmap_data,
977 args->start, args->num);
981 int perf_event__synthesize_threads(struct perf_tool *tool,
982 perf_event__handler_t process,
983 struct machine *machine,
984 bool needs_mmap, bool mmap_data,
985 unsigned int nr_threads_synthesize)
987 struct synthesize_threads_arg *args = NULL;
988 pthread_t *synthesize_threads = NULL;
989 char proc_path[PATH_MAX];
990 struct dirent **dirent;
998 if (machine__is_default_guest(machine))
1001 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
1002 n = scandir(proc_path, &dirent, filter_task, NULL);
1006 if (nr_threads_synthesize == UINT_MAX)
1007 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
1009 thread_nr = nr_threads_synthesize;
1011 if (thread_nr <= 1) {
1012 err = __perf_event__synthesize_threads(tool, process,
1014 needs_mmap, mmap_data,
1021 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
1022 if (synthesize_threads == NULL)
1025 args = calloc(sizeof(*args), thread_nr);
1029 num_per_thread = n / thread_nr;
1031 for (i = 0; i < thread_nr; i++) {
1032 args[i].tool = tool;
1033 args[i].process = process;
1034 args[i].machine = machine;
1035 args[i].needs_mmap = needs_mmap;
1036 args[i].mmap_data = mmap_data;
1037 args[i].dirent = dirent;
1039 for (i = 0; i < m; i++) {
1040 args[i].num = num_per_thread + 1;
1041 args[i].start = i * args[i].num;
1044 base = args[i-1].start + args[i-1].num;
1045 for (j = i; j < thread_nr; j++) {
1046 args[j].num = num_per_thread;
1047 args[j].start = base + (j - i) * args[i].num;
1050 for (i = 0; i < thread_nr; i++) {
1051 if (pthread_create(&synthesize_threads[i], NULL,
1052 synthesize_threads_worker, &args[i]))
1057 for (i = 0; i < thread_nr; i++)
1058 pthread_join(synthesize_threads[i], NULL);
1061 free(synthesize_threads);
1063 for (i = 0; i < n; i++)
1070 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
1071 perf_event__handler_t process __maybe_unused,
1072 struct machine *machine __maybe_unused)
1077 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1078 perf_event__handler_t process,
1079 struct machine *machine)
1081 union perf_event *event;
1082 size_t size = symbol_conf.buildid_mmap2 ?
1083 sizeof(event->mmap2) : sizeof(event->mmap);
1084 struct map *map = machine__kernel_map(machine);
1091 kmap = map__kmap(map);
1092 if (!kmap->ref_reloc_sym)
1096 * We should get this from /sys/kernel/sections/.text, but till that is
1097 * available use this, and after it is use this as a fallback for older
1100 event = zalloc(size + machine->id_hdr_size);
1101 if (event == NULL) {
1102 pr_debug("Not enough memory synthesizing mmap event "
1103 "for kernel modules\n");
1107 if (machine__is_host(machine)) {
1109 * kernel uses PERF_RECORD_MISC_USER for user space maps,
1110 * see kernel/perf_event.c __perf_event_mmap
1112 event->header.misc = PERF_RECORD_MISC_KERNEL;
1114 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1117 if (symbol_conf.buildid_mmap2) {
1118 size = snprintf(event->mmap2.filename, sizeof(event->mmap2.filename),
1119 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1120 size = PERF_ALIGN(size, sizeof(u64));
1121 event->mmap2.header.type = PERF_RECORD_MMAP2;
1122 event->mmap2.header.size = (sizeof(event->mmap2) -
1123 (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size);
1124 event->mmap2.pgoff = kmap->ref_reloc_sym->addr;
1125 event->mmap2.start = map->start;
1126 event->mmap2.len = map->end - event->mmap.start;
1127 event->mmap2.pid = machine->pid;
1129 perf_record_mmap2__read_build_id(&event->mmap2, true);
1131 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1132 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1133 size = PERF_ALIGN(size, sizeof(u64));
1134 event->mmap.header.type = PERF_RECORD_MMAP;
1135 event->mmap.header.size = (sizeof(event->mmap) -
1136 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1137 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1138 event->mmap.start = map->start;
1139 event->mmap.len = map->end - event->mmap.start;
1140 event->mmap.pid = machine->pid;
1143 err = perf_tool__process_synth_event(tool, event, machine, process);
1149 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1150 perf_event__handler_t process,
1151 struct machine *machine)
1155 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1159 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1162 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1163 struct perf_thread_map *threads,
1164 perf_event__handler_t process,
1165 struct machine *machine)
1167 union perf_event *event;
1170 size = sizeof(event->thread_map);
1171 size += threads->nr * sizeof(event->thread_map.entries[0]);
1173 event = zalloc(size);
1177 event->header.type = PERF_RECORD_THREAD_MAP;
1178 event->header.size = size;
1179 event->thread_map.nr = threads->nr;
1181 for (i = 0; i < threads->nr; i++) {
1182 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1183 char *comm = perf_thread_map__comm(threads, i);
1188 entry->pid = perf_thread_map__pid(threads, i);
1189 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1192 err = process(tool, event, NULL, machine);
1198 static void synthesize_cpus(struct cpu_map_entries *cpus,
1199 struct perf_cpu_map *map)
1201 int i, map_nr = perf_cpu_map__nr(map);
1205 for (i = 0; i < map_nr; i++)
1206 cpus->cpu[i] = perf_cpu_map__cpu(map, i).cpu;
1209 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1210 struct perf_cpu_map *map, int max)
1214 mask->nr = BITS_TO_LONGS(max);
1215 mask->long_size = sizeof(long);
1217 for (i = 0; i < perf_cpu_map__nr(map); i++)
1218 set_bit(perf_cpu_map__cpu(map, i).cpu, mask->mask);
1221 static size_t cpus_size(struct perf_cpu_map *map)
1223 return sizeof(struct cpu_map_entries) + perf_cpu_map__nr(map) * sizeof(u16);
1226 static size_t mask_size(struct perf_cpu_map *map, int *max)
1232 for (i = 0; i < perf_cpu_map__nr(map); i++) {
1233 /* bit position of the cpu is + 1 */
1234 int bit = perf_cpu_map__cpu(map, i).cpu + 1;
1240 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1243 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1245 size_t size_cpus, size_mask;
1246 bool is_dummy = perf_cpu_map__empty(map);
1249 * Both array and mask data have variable size based
1250 * on the number of cpus and their actual values.
1251 * The size of the 'struct perf_record_cpu_map_data' is:
1253 * array = size of 'struct cpu_map_entries' +
1254 * number of cpus * sizeof(u64)
1256 * mask = size of 'struct perf_record_record_cpu_map' +
1257 * maximum cpu bit converted to size of longs
1259 * and finally + the size of 'struct perf_record_cpu_map_data'.
1261 size_cpus = cpus_size(map);
1262 size_mask = mask_size(map, max);
1264 if (is_dummy || (size_cpus < size_mask)) {
1266 *type = PERF_CPU_MAP__CPUS;
1269 *type = PERF_CPU_MAP__MASK;
1272 *size += sizeof(struct perf_record_cpu_map_data);
1273 *size = PERF_ALIGN(*size, sizeof(u64));
1274 return zalloc(*size);
1277 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1283 case PERF_CPU_MAP__CPUS:
1284 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1286 case PERF_CPU_MAP__MASK:
1287 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1293 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1295 size_t size = sizeof(struct perf_record_cpu_map);
1296 struct perf_record_cpu_map *event;
1300 event = cpu_map_data__alloc(map, &size, &type, &max);
1304 event->header.type = PERF_RECORD_CPU_MAP;
1305 event->header.size = size;
1306 event->data.type = type;
1308 cpu_map_data__synthesize(&event->data, map, type, max);
1312 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1313 struct perf_cpu_map *map,
1314 perf_event__handler_t process,
1315 struct machine *machine)
1317 struct perf_record_cpu_map *event;
1320 event = cpu_map_event__new(map);
1324 err = process(tool, (union perf_event *) event, NULL, machine);
1330 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1331 struct perf_stat_config *config,
1332 perf_event__handler_t process,
1333 struct machine *machine)
1335 struct perf_record_stat_config *event;
1336 int size, i = 0, err;
1338 size = sizeof(*event);
1339 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1341 event = zalloc(size);
1345 event->header.type = PERF_RECORD_STAT_CONFIG;
1346 event->header.size = size;
1347 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1349 #define ADD(__term, __val) \
1350 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1351 event->data[i].val = __val; \
1354 ADD(AGGR_MODE, config->aggr_mode)
1355 ADD(INTERVAL, config->interval)
1356 ADD(SCALE, config->scale)
1358 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1359 "stat config terms unbalanced\n");
1362 err = process(tool, (union perf_event *) event, NULL, machine);
1368 int perf_event__synthesize_stat(struct perf_tool *tool,
1369 struct perf_cpu cpu, u32 thread, u64 id,
1370 struct perf_counts_values *count,
1371 perf_event__handler_t process,
1372 struct machine *machine)
1374 struct perf_record_stat event;
1376 event.header.type = PERF_RECORD_STAT;
1377 event.header.size = sizeof(event);
1378 event.header.misc = 0;
1381 event.cpu = cpu.cpu;
1382 event.thread = thread;
1383 event.val = count->val;
1384 event.ena = count->ena;
1385 event.run = count->run;
1387 return process(tool, (union perf_event *) &event, NULL, machine);
1390 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1391 u64 evtime, u64 type,
1392 perf_event__handler_t process,
1393 struct machine *machine)
1395 struct perf_record_stat_round event;
1397 event.header.type = PERF_RECORD_STAT_ROUND;
1398 event.header.size = sizeof(event);
1399 event.header.misc = 0;
1401 event.time = evtime;
1404 return process(tool, (union perf_event *) &event, NULL, machine);
1407 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1409 size_t sz, result = sizeof(struct perf_record_sample);
1411 if (type & PERF_SAMPLE_IDENTIFIER)
1412 result += sizeof(u64);
1414 if (type & PERF_SAMPLE_IP)
1415 result += sizeof(u64);
1417 if (type & PERF_SAMPLE_TID)
1418 result += sizeof(u64);
1420 if (type & PERF_SAMPLE_TIME)
1421 result += sizeof(u64);
1423 if (type & PERF_SAMPLE_ADDR)
1424 result += sizeof(u64);
1426 if (type & PERF_SAMPLE_ID)
1427 result += sizeof(u64);
1429 if (type & PERF_SAMPLE_STREAM_ID)
1430 result += sizeof(u64);
1432 if (type & PERF_SAMPLE_CPU)
1433 result += sizeof(u64);
1435 if (type & PERF_SAMPLE_PERIOD)
1436 result += sizeof(u64);
1438 if (type & PERF_SAMPLE_READ) {
1439 result += sizeof(u64);
1440 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1441 result += sizeof(u64);
1442 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1443 result += sizeof(u64);
1444 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1445 if (read_format & PERF_FORMAT_GROUP) {
1446 sz = sample->read.group.nr *
1447 sizeof(struct sample_read_value);
1450 result += sizeof(u64);
1454 if (type & PERF_SAMPLE_CALLCHAIN) {
1455 sz = (sample->callchain->nr + 1) * sizeof(u64);
1459 if (type & PERF_SAMPLE_RAW) {
1460 result += sizeof(u32);
1461 result += sample->raw_size;
1464 if (type & PERF_SAMPLE_BRANCH_STACK) {
1465 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1467 sz += 2 * sizeof(u64);
1471 if (type & PERF_SAMPLE_REGS_USER) {
1472 if (sample->user_regs.abi) {
1473 result += sizeof(u64);
1474 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1477 result += sizeof(u64);
1481 if (type & PERF_SAMPLE_STACK_USER) {
1482 sz = sample->user_stack.size;
1483 result += sizeof(u64);
1486 result += sizeof(u64);
1490 if (type & PERF_SAMPLE_WEIGHT_TYPE)
1491 result += sizeof(u64);
1493 if (type & PERF_SAMPLE_DATA_SRC)
1494 result += sizeof(u64);
1496 if (type & PERF_SAMPLE_TRANSACTION)
1497 result += sizeof(u64);
1499 if (type & PERF_SAMPLE_REGS_INTR) {
1500 if (sample->intr_regs.abi) {
1501 result += sizeof(u64);
1502 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1505 result += sizeof(u64);
1509 if (type & PERF_SAMPLE_PHYS_ADDR)
1510 result += sizeof(u64);
1512 if (type & PERF_SAMPLE_CGROUP)
1513 result += sizeof(u64);
1515 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1516 result += sizeof(u64);
1518 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1519 result += sizeof(u64);
1521 if (type & PERF_SAMPLE_AUX) {
1522 result += sizeof(u64);
1523 result += sample->aux_sample.size;
1529 void __weak arch_perf_synthesize_sample_weight(const struct perf_sample *data,
1530 __u64 *array, u64 type __maybe_unused)
1532 *array = data->weight;
1535 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1536 const struct perf_sample *sample)
1541 * used for cross-endian analysis. See git commit 65014ab3
1542 * for why this goofiness is needed.
1546 array = event->sample.array;
1548 if (type & PERF_SAMPLE_IDENTIFIER) {
1549 *array = sample->id;
1553 if (type & PERF_SAMPLE_IP) {
1554 *array = sample->ip;
1558 if (type & PERF_SAMPLE_TID) {
1559 u.val32[0] = sample->pid;
1560 u.val32[1] = sample->tid;
1565 if (type & PERF_SAMPLE_TIME) {
1566 *array = sample->time;
1570 if (type & PERF_SAMPLE_ADDR) {
1571 *array = sample->addr;
1575 if (type & PERF_SAMPLE_ID) {
1576 *array = sample->id;
1580 if (type & PERF_SAMPLE_STREAM_ID) {
1581 *array = sample->stream_id;
1585 if (type & PERF_SAMPLE_CPU) {
1586 u.val32[0] = sample->cpu;
1592 if (type & PERF_SAMPLE_PERIOD) {
1593 *array = sample->period;
1597 if (type & PERF_SAMPLE_READ) {
1598 if (read_format & PERF_FORMAT_GROUP)
1599 *array = sample->read.group.nr;
1601 *array = sample->read.one.value;
1604 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1605 *array = sample->read.time_enabled;
1609 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1610 *array = sample->read.time_running;
1614 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1615 if (read_format & PERF_FORMAT_GROUP) {
1616 sz = sample->read.group.nr *
1617 sizeof(struct sample_read_value);
1618 memcpy(array, sample->read.group.values, sz);
1619 array = (void *)array + sz;
1621 *array = sample->read.one.id;
1626 if (type & PERF_SAMPLE_CALLCHAIN) {
1627 sz = (sample->callchain->nr + 1) * sizeof(u64);
1628 memcpy(array, sample->callchain, sz);
1629 array = (void *)array + sz;
1632 if (type & PERF_SAMPLE_RAW) {
1633 u.val32[0] = sample->raw_size;
1635 array = (void *)array + sizeof(u32);
1637 memcpy(array, sample->raw_data, sample->raw_size);
1638 array = (void *)array + sample->raw_size;
1641 if (type & PERF_SAMPLE_BRANCH_STACK) {
1642 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1644 sz += 2 * sizeof(u64);
1645 memcpy(array, sample->branch_stack, sz);
1646 array = (void *)array + sz;
1649 if (type & PERF_SAMPLE_REGS_USER) {
1650 if (sample->user_regs.abi) {
1651 *array++ = sample->user_regs.abi;
1652 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1653 memcpy(array, sample->user_regs.regs, sz);
1654 array = (void *)array + sz;
1660 if (type & PERF_SAMPLE_STACK_USER) {
1661 sz = sample->user_stack.size;
1664 memcpy(array, sample->user_stack.data, sz);
1665 array = (void *)array + sz;
1670 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
1671 arch_perf_synthesize_sample_weight(sample, array, type);
1675 if (type & PERF_SAMPLE_DATA_SRC) {
1676 *array = sample->data_src;
1680 if (type & PERF_SAMPLE_TRANSACTION) {
1681 *array = sample->transaction;
1685 if (type & PERF_SAMPLE_REGS_INTR) {
1686 if (sample->intr_regs.abi) {
1687 *array++ = sample->intr_regs.abi;
1688 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1689 memcpy(array, sample->intr_regs.regs, sz);
1690 array = (void *)array + sz;
1696 if (type & PERF_SAMPLE_PHYS_ADDR) {
1697 *array = sample->phys_addr;
1701 if (type & PERF_SAMPLE_CGROUP) {
1702 *array = sample->cgroup;
1706 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1707 *array = sample->data_page_size;
1711 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1712 *array = sample->code_page_size;
1716 if (type & PERF_SAMPLE_AUX) {
1717 sz = sample->aux_sample.size;
1719 memcpy(array, sample->aux_sample.data, sz);
1720 array = (void *)array + sz;
1726 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1727 struct evlist *evlist, struct machine *machine)
1729 union perf_event *ev;
1730 struct evsel *evsel;
1731 size_t nr = 0, i = 0, sz, max_nr, n;
1734 pr_debug2("Synthesizing id index\n");
1736 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1737 sizeof(struct id_index_entry);
1739 evlist__for_each_entry(evlist, evsel)
1740 nr += evsel->core.ids;
1742 n = nr > max_nr ? max_nr : nr;
1743 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1748 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1749 ev->id_index.header.size = sz;
1750 ev->id_index.nr = n;
1752 evlist__for_each_entry(evlist, evsel) {
1755 for (j = 0; j < evsel->core.ids; j++) {
1756 struct id_index_entry *e;
1757 struct perf_sample_id *sid;
1760 err = process(tool, ev, NULL, machine);
1767 e = &ev->id_index.entries[i++];
1769 e->id = evsel->core.id[j];
1771 sid = evlist__id2sid(evlist, e->id);
1778 e->cpu = sid->cpu.cpu;
1783 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1784 ev->id_index.header.size = sz;
1785 ev->id_index.nr = nr;
1787 err = process(tool, ev, NULL, machine);
1794 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1795 struct target *target, struct perf_thread_map *threads,
1796 perf_event__handler_t process, bool needs_mmap,
1797 bool data_mmap, unsigned int nr_threads_synthesize)
1800 * When perf runs in non-root PID namespace, and the namespace's proc FS
1801 * is not mounted, nsinfo__is_in_root_namespace() returns false.
1802 * In this case, the proc FS is coming for the parent namespace, thus
1803 * perf tool will wrongly gather process info from its parent PID
1806 * To avoid the confusion that the perf tool runs in a child PID
1807 * namespace but it synthesizes thread info from its parent PID
1808 * namespace, returns failure with warning.
1810 if (!nsinfo__is_in_root_namespace()) {
1811 pr_err("Perf runs in non-root PID namespace but it tries to ");
1812 pr_err("gather process info from its parent PID namespace.\n");
1813 pr_err("Please mount the proc file system properly, e.g. ");
1814 pr_err("add the option '--mount-proc' for unshare command.\n");
1818 if (target__has_task(target))
1819 return perf_event__synthesize_thread_map(tool, threads, process, machine,
1820 needs_mmap, data_mmap);
1821 else if (target__has_cpu(target))
1822 return perf_event__synthesize_threads(tool, process, machine,
1823 needs_mmap, data_mmap,
1824 nr_threads_synthesize);
1825 /* command specified */
1829 int machine__synthesize_threads(struct machine *machine, struct target *target,
1830 struct perf_thread_map *threads, bool needs_mmap,
1831 bool data_mmap, unsigned int nr_threads_synthesize)
1833 return __machine__synthesize_threads(machine, NULL, target, threads,
1834 perf_event__process, needs_mmap,
1835 data_mmap, nr_threads_synthesize);
1838 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1840 struct perf_record_event_update *ev;
1842 size += sizeof(*ev);
1843 size = PERF_ALIGN(size, sizeof(u64));
1847 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1848 ev->header.size = (u16)size;
1855 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1856 perf_event__handler_t process)
1858 size_t size = strlen(evsel->unit);
1859 struct perf_record_event_update *ev;
1862 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1866 strlcpy(ev->data, evsel->unit, size + 1);
1867 err = process(tool, (union perf_event *)ev, NULL, NULL);
1872 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1873 perf_event__handler_t process)
1875 struct perf_record_event_update *ev;
1876 struct perf_record_event_update_scale *ev_data;
1879 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1883 ev_data = (struct perf_record_event_update_scale *)ev->data;
1884 ev_data->scale = evsel->scale;
1885 err = process(tool, (union perf_event *)ev, NULL, NULL);
1890 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1891 perf_event__handler_t process)
1893 struct perf_record_event_update *ev;
1894 size_t len = strlen(evsel->name);
1897 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1901 strlcpy(ev->data, evsel->name, len + 1);
1902 err = process(tool, (union perf_event *)ev, NULL, NULL);
1907 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1908 perf_event__handler_t process)
1910 size_t size = sizeof(struct perf_record_event_update);
1911 struct perf_record_event_update *ev;
1915 if (!evsel->core.own_cpus)
1918 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1922 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1923 ev->header.size = (u16)size;
1924 ev->type = PERF_EVENT_UPDATE__CPUS;
1925 ev->id = evsel->core.id[0];
1927 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1928 evsel->core.own_cpus, type, max);
1930 err = process(tool, (union perf_event *)ev, NULL, NULL);
1935 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1936 perf_event__handler_t process)
1938 struct evsel *evsel;
1941 evlist__for_each_entry(evlist, evsel) {
1942 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1943 evsel->core.id, process);
1945 pr_debug("failed to create perf header attribute\n");
1953 static bool has_unit(struct evsel *evsel)
1955 return evsel->unit && *evsel->unit;
1958 static bool has_scale(struct evsel *evsel)
1960 return evsel->scale != 1;
1963 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1964 perf_event__handler_t process, bool is_pipe)
1966 struct evsel *evsel;
1970 * Synthesize other events stuff not carried within
1971 * attr event - unit, scale, name
1973 evlist__for_each_entry(evsel_list, evsel) {
1974 if (!evsel->supported)
1978 * Synthesize unit and scale only if it's defined.
1980 if (has_unit(evsel)) {
1981 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1983 pr_err("Couldn't synthesize evsel unit.\n");
1988 if (has_scale(evsel)) {
1989 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1991 pr_err("Couldn't synthesize evsel evsel.\n");
1996 if (evsel->core.own_cpus) {
1997 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1999 pr_err("Couldn't synthesize evsel cpus.\n");
2005 * Name is needed only for pipe output,
2006 * perf.data carries event names.
2009 err = perf_event__synthesize_event_update_name(tool, evsel, process);
2011 pr_err("Couldn't synthesize evsel name.\n");
2019 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
2020 u32 ids, u64 *id, perf_event__handler_t process)
2022 union perf_event *ev;
2026 size = sizeof(struct perf_event_attr);
2027 size = PERF_ALIGN(size, sizeof(u64));
2028 size += sizeof(struct perf_event_header);
2029 size += ids * sizeof(u64);
2036 ev->attr.attr = *attr;
2037 memcpy(ev->attr.id, id, ids * sizeof(u64));
2039 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2040 ev->attr.header.size = (u16)size;
2042 if (ev->attr.header.size == size)
2043 err = process(tool, ev, NULL, NULL);
2052 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
2053 perf_event__handler_t process)
2055 union perf_event ev;
2056 struct tracing_data *tdata;
2057 ssize_t size = 0, aligned_size = 0, padding;
2061 * We are going to store the size of the data followed
2062 * by the data contents. Since the fd descriptor is a pipe,
2063 * we cannot seek back to store the size of the data once
2064 * we know it. Instead we:
2066 * - write the tracing data to the temp file
2067 * - get/write the data size to pipe
2068 * - write the tracing data from the temp file
2071 tdata = tracing_data_get(&evlist->core.entries, fd, true);
2075 memset(&ev, 0, sizeof(ev));
2077 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2079 aligned_size = PERF_ALIGN(size, sizeof(u64));
2080 padding = aligned_size - size;
2081 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2082 ev.tracing_data.size = aligned_size;
2084 process(tool, &ev, NULL, NULL);
2087 * The put function will copy all the tracing data
2088 * stored in temp file to the pipe.
2090 tracing_data_put(tdata);
2092 ff = (struct feat_fd){ .fd = fd };
2093 if (write_padded(&ff, NULL, 0, padding))
2096 return aligned_size;
2099 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2100 perf_event__handler_t process, struct machine *machine)
2102 union perf_event ev;
2108 memset(&ev, 0, sizeof(ev));
2110 len = pos->long_name_len + 1;
2111 len = PERF_ALIGN(len, NAME_ALIGN);
2112 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
2113 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2114 ev.build_id.header.misc = misc;
2115 ev.build_id.pid = machine->pid;
2116 ev.build_id.header.size = sizeof(ev.build_id) + len;
2117 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2119 return process(tool, &ev, NULL, machine);
2122 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2123 struct evlist *evlist, perf_event__handler_t process, bool attrs)
2128 err = perf_event__synthesize_attrs(tool, evlist, process);
2130 pr_err("Couldn't synthesize attrs.\n");
2135 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2136 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2138 pr_err("Couldn't synthesize thread map.\n");
2142 err = perf_event__synthesize_cpu_map(tool, evlist->core.user_requested_cpus, process, NULL);
2144 pr_err("Couldn't synthesize thread map.\n");
2148 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2150 pr_err("Couldn't synthesize config.\n");
2157 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2159 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2160 struct evlist *evlist, perf_event__handler_t process)
2162 struct perf_header *header = &session->header;
2163 struct perf_record_header_feature *fe;
2168 sz_hdr = sizeof(fe->header);
2169 sz = sizeof(union perf_event);
2170 /* get a nice alignment */
2171 sz = PERF_ALIGN(sz, page_size);
2173 memset(&ff, 0, sizeof(ff));
2175 ff.buf = malloc(sz);
2179 ff.size = sz - sz_hdr;
2180 ff.ph = &session->header;
2182 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2183 if (!feat_ops[feat].synthesize) {
2184 pr_debug("No record header feature for header :%d\n", feat);
2188 ff.offset = sizeof(*fe);
2190 ret = feat_ops[feat].write(&ff, evlist);
2191 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2192 pr_debug("Error writing feature\n");
2195 /* ff.buf may have changed due to realloc in do_write() */
2197 memset(fe, 0, sizeof(*fe));
2200 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2201 fe->header.size = ff.offset;
2203 ret = process(tool, ff.buf, NULL, NULL);
2210 /* Send HEADER_LAST_FEATURE mark. */
2212 fe->feat_id = HEADER_LAST_FEATURE;
2213 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2214 fe->header.size = sizeof(*fe);
2216 ret = process(tool, ff.buf, NULL, NULL);
2222 int perf_event__synthesize_for_pipe(struct perf_tool *tool,
2223 struct perf_session *session,
2224 struct perf_data *data,
2225 perf_event__handler_t process)
2229 struct evlist *evlist = session->evlist;
2232 * We need to synthesize events first, because some
2233 * features works on top of them (on report side).
2235 err = perf_event__synthesize_attrs(tool, evlist, process);
2237 pr_err("Couldn't synthesize attrs.\n");
2242 err = perf_event__synthesize_features(tool, session, evlist, process);
2244 pr_err("Couldn't synthesize features.\n");
2249 if (have_tracepoints(&evlist->core.entries)) {
2250 int fd = perf_data__fd(data);
2253 * FIXME err <= 0 here actually means that
2254 * there were no tracepoints so its not really
2255 * an error, just that we don't need to
2256 * synthesize anything. We really have to
2257 * return this more properly and also
2258 * propagate errors that now are calling die()
2260 err = perf_event__synthesize_tracing_data(tool, fd, evlist,
2263 pr_err("Couldn't record tracing data.\n");
2272 int parse_synth_opt(char *synth)
2280 for (q = synth; (p = strsep(&q, ",")); p = q) {
2281 if (!strcasecmp(p, "no") || !strcasecmp(p, "none"))
2284 if (!strcasecmp(p, "all"))
2285 return PERF_SYNTH_ALL;
2287 if (!strcasecmp(p, "task"))
2288 ret |= PERF_SYNTH_TASK;
2289 else if (!strcasecmp(p, "mmap"))
2290 ret |= PERF_SYNTH_TASK | PERF_SYNTH_MMAP;
2291 else if (!strcasecmp(p, "cgroup"))
2292 ret |= PERF_SYNTH_CGROUP;