WIP FPC-III support
[linux/fpc-iii.git] / tools / perf / util / synthetic-events.c
blob2947e3f3c6d9d03c154c719eabd224669bc676b4
1 // SPDX-License-Identifier: GPL-2.0-only
3 #include "util/debug.h"
4 #include "util/dso.h"
5 #include "util/event.h"
6 #include "util/evlist.h"
7 #include "util/machine.h"
8 #include "util/map.h"
9 #include "util/map_symbol.h"
10 #include "util/branch.h"
11 #include "util/memswap.h"
12 #include "util/namespaces.h"
13 #include "util/session.h"
14 #include "util/stat.h"
15 #include "util/symbol.h"
16 #include "util/synthetic-events.h"
17 #include "util/target.h"
18 #include "util/time-utils.h"
19 #include "util/cgroup.h"
20 #include <linux/bitops.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/zalloc.h>
24 #include <linux/perf_event.h>
25 #include <asm/bug.h>
26 #include <perf/evsel.h>
27 #include <internal/cpumap.h>
28 #include <perf/cpumap.h>
29 #include <internal/lib.h> // page_size
30 #include <internal/threadmap.h>
31 #include <perf/threadmap.h>
32 #include <symbol/kallsyms.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #include <inttypes.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
39 #include <api/fs/fs.h>
40 #include <api/io.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.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 = {
56 .pid = -1,
57 .tid = -1,
58 .time = -1,
59 .stream_id = -1,
60 .cpu = -1,
61 .period = 1,
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, char *comm, size_t len,
73 pid_t *tgid, pid_t *ppid)
75 char bf[4096];
76 int fd;
77 size_t size = 0;
78 ssize_t n;
79 char *name, *tgids, *ppids;
81 *tgid = -1;
82 *ppid = -1;
84 snprintf(bf, sizeof(bf), "/proc/%d/status", pid);
86 fd = open(bf, O_RDONLY);
87 if (fd < 0) {
88 pr_debug("couldn't open %s\n", bf);
89 return -1;
92 n = read(fd, bf, sizeof(bf) - 1);
93 close(fd);
94 if (n <= 0) {
95 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
96 pid);
97 return -1;
99 bf[n] = '\0';
101 name = strstr(bf, "Name:");
102 tgids = strstr(bf, "Tgid:");
103 ppids = strstr(bf, "PPid:");
105 if (name) {
106 char *nl;
108 name = skip_spaces(name + 5); /* strlen("Name:") */
109 nl = strchr(name, '\n');
110 if (nl)
111 *nl = '\0';
113 size = strlen(name);
114 if (size >= len)
115 size = len - 1;
116 memcpy(comm, name, size);
117 comm[size] = '\0';
118 } else {
119 pr_debug("Name: string not found for pid %d\n", pid);
122 if (tgids) {
123 tgids += 5; /* strlen("Tgid:") */
124 *tgid = atoi(tgids);
125 } else {
126 pr_debug("Tgid: string not found for pid %d\n", pid);
129 if (ppids) {
130 ppids += 5; /* strlen("PPid:") */
131 *ppid = atoi(ppids);
132 } else {
133 pr_debug("PPid: string not found for pid %d\n", pid);
136 return 0;
139 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
140 struct machine *machine,
141 pid_t *tgid, pid_t *ppid)
143 size_t size;
145 *ppid = -1;
147 memset(&event->comm, 0, sizeof(event->comm));
149 if (machine__is_host(machine)) {
150 if (perf_event__get_comm_ids(pid, event->comm.comm,
151 sizeof(event->comm.comm),
152 tgid, ppid) != 0) {
153 return -1;
155 } else {
156 *tgid = machine->pid;
159 if (*tgid < 0)
160 return -1;
162 event->comm.pid = *tgid;
163 event->comm.header.type = PERF_RECORD_COMM;
165 size = strlen(event->comm.comm) + 1;
166 size = PERF_ALIGN(size, sizeof(u64));
167 memset(event->comm.comm + size, 0, machine->id_hdr_size);
168 event->comm.header.size = (sizeof(event->comm) -
169 (sizeof(event->comm.comm) - size) +
170 machine->id_hdr_size);
171 event->comm.tid = pid;
173 return 0;
176 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
177 union perf_event *event, pid_t pid,
178 perf_event__handler_t process,
179 struct machine *machine)
181 pid_t tgid, ppid;
183 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
184 return -1;
186 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
187 return -1;
189 return tgid;
192 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
193 struct perf_ns_link_info *ns_link_info)
195 struct stat64 st;
196 char proc_ns[128];
198 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
199 if (stat64(proc_ns, &st) == 0) {
200 ns_link_info->dev = st.st_dev;
201 ns_link_info->ino = st.st_ino;
205 int perf_event__synthesize_namespaces(struct perf_tool *tool,
206 union perf_event *event,
207 pid_t pid, pid_t tgid,
208 perf_event__handler_t process,
209 struct machine *machine)
211 u32 idx;
212 struct perf_ns_link_info *ns_link_info;
214 if (!tool || !tool->namespace_events)
215 return 0;
217 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
218 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
219 machine->id_hdr_size));
221 event->namespaces.pid = tgid;
222 event->namespaces.tid = pid;
224 event->namespaces.nr_namespaces = NR_NAMESPACES;
226 ns_link_info = event->namespaces.link_info;
228 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
229 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
230 &ns_link_info[idx]);
232 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
234 event->namespaces.header.size = (sizeof(event->namespaces) +
235 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
236 machine->id_hdr_size);
238 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
239 return -1;
241 return 0;
244 static int perf_event__synthesize_fork(struct perf_tool *tool,
245 union perf_event *event,
246 pid_t pid, pid_t tgid, pid_t ppid,
247 perf_event__handler_t process,
248 struct machine *machine)
250 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
253 * for main thread set parent to ppid from status file. For other
254 * threads set parent pid to main thread. ie., assume main thread
255 * spawns all threads in a process
257 if (tgid == pid) {
258 event->fork.ppid = ppid;
259 event->fork.ptid = ppid;
260 } else {
261 event->fork.ppid = tgid;
262 event->fork.ptid = tgid;
264 event->fork.pid = tgid;
265 event->fork.tid = pid;
266 event->fork.header.type = PERF_RECORD_FORK;
267 event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
269 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
271 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
272 return -1;
274 return 0;
277 static bool read_proc_maps_line(struct io *io, __u64 *start, __u64 *end,
278 u32 *prot, u32 *flags, __u64 *offset,
279 u32 *maj, u32 *min,
280 __u64 *inode,
281 ssize_t pathname_size, char *pathname)
283 __u64 temp;
284 int ch;
285 char *start_pathname = pathname;
287 if (io__get_hex(io, start) != '-')
288 return false;
289 if (io__get_hex(io, end) != ' ')
290 return false;
292 /* map protection and flags bits */
293 *prot = 0;
294 ch = io__get_char(io);
295 if (ch == 'r')
296 *prot |= PROT_READ;
297 else if (ch != '-')
298 return false;
299 ch = io__get_char(io);
300 if (ch == 'w')
301 *prot |= PROT_WRITE;
302 else if (ch != '-')
303 return false;
304 ch = io__get_char(io);
305 if (ch == 'x')
306 *prot |= PROT_EXEC;
307 else if (ch != '-')
308 return false;
309 ch = io__get_char(io);
310 if (ch == 's')
311 *flags = MAP_SHARED;
312 else if (ch == 'p')
313 *flags = MAP_PRIVATE;
314 else
315 return false;
316 if (io__get_char(io) != ' ')
317 return false;
319 if (io__get_hex(io, offset) != ' ')
320 return false;
322 if (io__get_hex(io, &temp) != ':')
323 return false;
324 *maj = temp;
325 if (io__get_hex(io, &temp) != ' ')
326 return false;
327 *min = temp;
329 ch = io__get_dec(io, inode);
330 if (ch != ' ') {
331 *pathname = '\0';
332 return ch == '\n';
334 do {
335 ch = io__get_char(io);
336 } while (ch == ' ');
337 while (true) {
338 if (ch < 0)
339 return false;
340 if (ch == '\0' || ch == '\n' ||
341 (pathname + 1 - start_pathname) >= pathname_size) {
342 *pathname = '\0';
343 return true;
345 *pathname++ = ch;
346 ch = io__get_char(io);
350 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
351 union perf_event *event,
352 pid_t pid, pid_t tgid,
353 perf_event__handler_t process,
354 struct machine *machine,
355 bool mmap_data)
357 unsigned long long t;
358 char bf[BUFSIZ];
359 struct io io;
360 bool truncation = false;
361 unsigned long long timeout = proc_map_timeout * 1000000ULL;
362 int rc = 0;
363 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
364 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
366 if (machine__is_default_guest(machine))
367 return 0;
369 snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
370 machine->root_dir, pid, pid);
372 io.fd = open(bf, O_RDONLY, 0);
373 if (io.fd < 0) {
375 * We raced with a task exiting - just return:
377 pr_debug("couldn't open %s\n", bf);
378 return -1;
380 io__init(&io, io.fd, bf, sizeof(bf));
382 event->header.type = PERF_RECORD_MMAP2;
383 t = rdclock();
385 while (!io.eof) {
386 static const char anonstr[] = "//anon";
387 size_t size;
389 /* ensure null termination since stack will be reused. */
390 event->mmap2.filename[0] = '\0';
392 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
393 if (!read_proc_maps_line(&io,
394 &event->mmap2.start,
395 &event->mmap2.len,
396 &event->mmap2.prot,
397 &event->mmap2.flags,
398 &event->mmap2.pgoff,
399 &event->mmap2.maj,
400 &event->mmap2.min,
401 &event->mmap2.ino,
402 sizeof(event->mmap2.filename),
403 event->mmap2.filename))
404 continue;
406 if ((rdclock() - t) > timeout) {
407 pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
408 "You may want to increase "
409 "the time limit by --proc-map-timeout\n",
410 machine->root_dir, pid, pid);
411 truncation = true;
412 goto out;
415 event->mmap2.ino_generation = 0;
418 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
420 if (machine__is_host(machine))
421 event->header.misc = PERF_RECORD_MISC_USER;
422 else
423 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
425 if ((event->mmap2.prot & PROT_EXEC) == 0) {
426 if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
427 continue;
429 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
432 out:
433 if (truncation)
434 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
436 if (!strcmp(event->mmap2.filename, ""))
437 strcpy(event->mmap2.filename, anonstr);
439 if (hugetlbfs_mnt_len &&
440 !strncmp(event->mmap2.filename, hugetlbfs_mnt,
441 hugetlbfs_mnt_len)) {
442 strcpy(event->mmap2.filename, anonstr);
443 event->mmap2.flags |= MAP_HUGETLB;
446 size = strlen(event->mmap2.filename) + 1;
447 size = PERF_ALIGN(size, sizeof(u64));
448 event->mmap2.len -= event->mmap.start;
449 event->mmap2.header.size = (sizeof(event->mmap2) -
450 (sizeof(event->mmap2.filename) - size));
451 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
452 event->mmap2.header.size += machine->id_hdr_size;
453 event->mmap2.pid = tgid;
454 event->mmap2.tid = pid;
456 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
457 rc = -1;
458 break;
461 if (truncation)
462 break;
465 close(io.fd);
466 return rc;
469 #ifdef HAVE_FILE_HANDLE
470 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
471 union perf_event *event,
472 char *path, size_t mount_len,
473 perf_event__handler_t process,
474 struct machine *machine)
476 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
477 size_t path_len = strlen(path) - mount_len + 1;
478 struct {
479 struct file_handle fh;
480 uint64_t cgroup_id;
481 } handle;
482 int mount_id;
484 while (path_len % sizeof(u64))
485 path[mount_len + path_len++] = '\0';
487 memset(&event->cgroup, 0, event_size);
489 event->cgroup.header.type = PERF_RECORD_CGROUP;
490 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
492 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
493 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
494 pr_debug("stat failed: %s\n", path);
495 return -1;
498 event->cgroup.id = handle.cgroup_id;
499 strncpy(event->cgroup.path, path + mount_len, path_len);
500 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
502 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
503 pr_debug("process synth event failed\n");
504 return -1;
507 return 0;
510 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
511 union perf_event *event,
512 char *path, size_t mount_len,
513 perf_event__handler_t process,
514 struct machine *machine)
516 size_t pos = strlen(path);
517 DIR *d;
518 struct dirent *dent;
519 int ret = 0;
521 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
522 process, machine) < 0)
523 return -1;
525 d = opendir(path);
526 if (d == NULL) {
527 pr_debug("failed to open directory: %s\n", path);
528 return -1;
531 while ((dent = readdir(d)) != NULL) {
532 if (dent->d_type != DT_DIR)
533 continue;
534 if (!strcmp(dent->d_name, ".") ||
535 !strcmp(dent->d_name, ".."))
536 continue;
538 /* any sane path should be less than PATH_MAX */
539 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
540 continue;
542 if (path[pos - 1] != '/')
543 strcat(path, "/");
544 strcat(path, dent->d_name);
546 ret = perf_event__walk_cgroup_tree(tool, event, path,
547 mount_len, process, machine);
548 if (ret < 0)
549 break;
551 path[pos] = '\0';
554 closedir(d);
555 return ret;
558 int perf_event__synthesize_cgroups(struct perf_tool *tool,
559 perf_event__handler_t process,
560 struct machine *machine)
562 union perf_event event;
563 char cgrp_root[PATH_MAX];
564 size_t mount_len; /* length of mount point in the path */
566 if (!tool || !tool->cgroup_events)
567 return 0;
569 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
570 pr_debug("cannot find cgroup mount point\n");
571 return -1;
574 mount_len = strlen(cgrp_root);
575 /* make sure the path starts with a slash (after mount point) */
576 strcat(cgrp_root, "/");
578 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
579 process, machine) < 0)
580 return -1;
582 return 0;
584 #else
585 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
586 perf_event__handler_t process __maybe_unused,
587 struct machine *machine __maybe_unused)
589 return -1;
591 #endif
593 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
594 struct machine *machine)
596 int rc = 0;
597 struct map *pos;
598 struct maps *maps = machine__kernel_maps(machine);
599 union perf_event *event = zalloc((sizeof(event->mmap) +
600 machine->id_hdr_size));
601 if (event == NULL) {
602 pr_debug("Not enough memory synthesizing mmap event "
603 "for kernel modules\n");
604 return -1;
607 event->header.type = PERF_RECORD_MMAP;
610 * kernel uses 0 for user space maps, see kernel/perf_event.c
611 * __perf_event_mmap
613 if (machine__is_host(machine))
614 event->header.misc = PERF_RECORD_MISC_KERNEL;
615 else
616 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
618 maps__for_each_entry(maps, pos) {
619 size_t size;
621 if (!__map__is_kmodule(pos))
622 continue;
624 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
625 event->mmap.header.type = PERF_RECORD_MMAP;
626 event->mmap.header.size = (sizeof(event->mmap) -
627 (sizeof(event->mmap.filename) - size));
628 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
629 event->mmap.header.size += machine->id_hdr_size;
630 event->mmap.start = pos->start;
631 event->mmap.len = pos->end - pos->start;
632 event->mmap.pid = machine->pid;
634 memcpy(event->mmap.filename, pos->dso->long_name,
635 pos->dso->long_name_len + 1);
636 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
637 rc = -1;
638 break;
642 free(event);
643 return rc;
646 static int __event__synthesize_thread(union perf_event *comm_event,
647 union perf_event *mmap_event,
648 union perf_event *fork_event,
649 union perf_event *namespaces_event,
650 pid_t pid, int full, perf_event__handler_t process,
651 struct perf_tool *tool, struct machine *machine, bool mmap_data)
653 char filename[PATH_MAX];
654 DIR *tasks;
655 struct dirent *dirent;
656 pid_t tgid, ppid;
657 int rc = 0;
659 /* special case: only send one comm event using passed in pid */
660 if (!full) {
661 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
662 process, machine);
664 if (tgid == -1)
665 return -1;
667 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
668 tgid, process, machine) < 0)
669 return -1;
672 * send mmap only for thread group leader
673 * see thread__init_maps()
675 if (pid == tgid &&
676 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
677 process, machine, mmap_data))
678 return -1;
680 return 0;
683 if (machine__is_default_guest(machine))
684 return 0;
686 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
687 machine->root_dir, pid);
689 tasks = opendir(filename);
690 if (tasks == NULL) {
691 pr_debug("couldn't open %s\n", filename);
692 return 0;
695 while ((dirent = readdir(tasks)) != NULL) {
696 char *end;
697 pid_t _pid;
699 _pid = strtol(dirent->d_name, &end, 10);
700 if (*end)
701 continue;
703 rc = -1;
704 if (perf_event__prepare_comm(comm_event, _pid, machine,
705 &tgid, &ppid) != 0)
706 break;
708 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
709 ppid, process, machine) < 0)
710 break;
712 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
713 tgid, process, machine) < 0)
714 break;
717 * Send the prepared comm event
719 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
720 break;
722 rc = 0;
723 if (_pid == pid) {
724 /* process the parent's maps too */
725 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
726 process, machine, mmap_data);
727 if (rc)
728 break;
732 closedir(tasks);
733 return rc;
736 int perf_event__synthesize_thread_map(struct perf_tool *tool,
737 struct perf_thread_map *threads,
738 perf_event__handler_t process,
739 struct machine *machine,
740 bool mmap_data)
742 union perf_event *comm_event, *mmap_event, *fork_event;
743 union perf_event *namespaces_event;
744 int err = -1, thread, j;
746 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
747 if (comm_event == NULL)
748 goto out;
750 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
751 if (mmap_event == NULL)
752 goto out_free_comm;
754 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
755 if (fork_event == NULL)
756 goto out_free_mmap;
758 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
759 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
760 machine->id_hdr_size);
761 if (namespaces_event == NULL)
762 goto out_free_fork;
764 err = 0;
765 for (thread = 0; thread < threads->nr; ++thread) {
766 if (__event__synthesize_thread(comm_event, mmap_event,
767 fork_event, namespaces_event,
768 perf_thread_map__pid(threads, thread), 0,
769 process, tool, machine,
770 mmap_data)) {
771 err = -1;
772 break;
776 * comm.pid is set to thread group id by
777 * perf_event__synthesize_comm
779 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
780 bool need_leader = true;
782 /* is thread group leader in thread_map? */
783 for (j = 0; j < threads->nr; ++j) {
784 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
785 need_leader = false;
786 break;
790 /* if not, generate events for it */
791 if (need_leader &&
792 __event__synthesize_thread(comm_event, mmap_event,
793 fork_event, namespaces_event,
794 comm_event->comm.pid, 0,
795 process, tool, machine,
796 mmap_data)) {
797 err = -1;
798 break;
802 free(namespaces_event);
803 out_free_fork:
804 free(fork_event);
805 out_free_mmap:
806 free(mmap_event);
807 out_free_comm:
808 free(comm_event);
809 out:
810 return err;
813 static int __perf_event__synthesize_threads(struct perf_tool *tool,
814 perf_event__handler_t process,
815 struct machine *machine,
816 bool mmap_data,
817 struct dirent **dirent,
818 int start,
819 int num)
821 union perf_event *comm_event, *mmap_event, *fork_event;
822 union perf_event *namespaces_event;
823 int err = -1;
824 char *end;
825 pid_t pid;
826 int i;
828 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
829 if (comm_event == NULL)
830 goto out;
832 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
833 if (mmap_event == NULL)
834 goto out_free_comm;
836 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
837 if (fork_event == NULL)
838 goto out_free_mmap;
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)
844 goto out_free_fork;
846 for (i = start; i < start + num; i++) {
847 if (!isdigit(dirent[i]->d_name[0]))
848 continue;
850 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
851 /* only interested in proper numerical dirents */
852 if (*end)
853 continue;
855 * We may race with exiting thread, so don't stop just because
856 * one thread couldn't be synthesized.
858 __event__synthesize_thread(comm_event, mmap_event, fork_event,
859 namespaces_event, pid, 1, process,
860 tool, machine, mmap_data);
862 err = 0;
864 free(namespaces_event);
865 out_free_fork:
866 free(fork_event);
867 out_free_mmap:
868 free(mmap_event);
869 out_free_comm:
870 free(comm_event);
871 out:
872 return err;
875 struct synthesize_threads_arg {
876 struct perf_tool *tool;
877 perf_event__handler_t process;
878 struct machine *machine;
879 bool mmap_data;
880 struct dirent **dirent;
881 int num;
882 int start;
885 static void *synthesize_threads_worker(void *arg)
887 struct synthesize_threads_arg *args = arg;
889 __perf_event__synthesize_threads(args->tool, args->process,
890 args->machine, args->mmap_data,
891 args->dirent,
892 args->start, args->num);
893 return NULL;
896 int perf_event__synthesize_threads(struct perf_tool *tool,
897 perf_event__handler_t process,
898 struct machine *machine,
899 bool mmap_data,
900 unsigned int nr_threads_synthesize)
902 struct synthesize_threads_arg *args = NULL;
903 pthread_t *synthesize_threads = NULL;
904 char proc_path[PATH_MAX];
905 struct dirent **dirent;
906 int num_per_thread;
907 int m, n, i, j;
908 int thread_nr;
909 int base = 0;
910 int err = -1;
913 if (machine__is_default_guest(machine))
914 return 0;
916 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
917 n = scandir(proc_path, &dirent, 0, alphasort);
918 if (n < 0)
919 return err;
921 if (nr_threads_synthesize == UINT_MAX)
922 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
923 else
924 thread_nr = nr_threads_synthesize;
926 if (thread_nr <= 1) {
927 err = __perf_event__synthesize_threads(tool, process,
928 machine, mmap_data,
929 dirent, base, n);
930 goto free_dirent;
932 if (thread_nr > n)
933 thread_nr = n;
935 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
936 if (synthesize_threads == NULL)
937 goto free_dirent;
939 args = calloc(sizeof(*args), thread_nr);
940 if (args == NULL)
941 goto free_threads;
943 num_per_thread = n / thread_nr;
944 m = n % thread_nr;
945 for (i = 0; i < thread_nr; i++) {
946 args[i].tool = tool;
947 args[i].process = process;
948 args[i].machine = machine;
949 args[i].mmap_data = mmap_data;
950 args[i].dirent = dirent;
952 for (i = 0; i < m; i++) {
953 args[i].num = num_per_thread + 1;
954 args[i].start = i * args[i].num;
956 if (i != 0)
957 base = args[i-1].start + args[i-1].num;
958 for (j = i; j < thread_nr; j++) {
959 args[j].num = num_per_thread;
960 args[j].start = base + (j - i) * args[i].num;
963 for (i = 0; i < thread_nr; i++) {
964 if (pthread_create(&synthesize_threads[i], NULL,
965 synthesize_threads_worker, &args[i]))
966 goto out_join;
968 err = 0;
969 out_join:
970 for (i = 0; i < thread_nr; i++)
971 pthread_join(synthesize_threads[i], NULL);
972 free(args);
973 free_threads:
974 free(synthesize_threads);
975 free_dirent:
976 for (i = 0; i < n; i++)
977 zfree(&dirent[i]);
978 free(dirent);
980 return err;
983 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
984 perf_event__handler_t process __maybe_unused,
985 struct machine *machine __maybe_unused)
987 return 0;
990 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
991 perf_event__handler_t process,
992 struct machine *machine)
994 size_t size;
995 struct map *map = machine__kernel_map(machine);
996 struct kmap *kmap;
997 int err;
998 union perf_event *event;
1000 if (map == NULL)
1001 return -1;
1003 kmap = map__kmap(map);
1004 if (!kmap->ref_reloc_sym)
1005 return -1;
1008 * We should get this from /sys/kernel/sections/.text, but till that is
1009 * available use this, and after it is use this as a fallback for older
1010 * kernels.
1012 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
1013 if (event == NULL) {
1014 pr_debug("Not enough memory synthesizing mmap event "
1015 "for kernel modules\n");
1016 return -1;
1019 if (machine__is_host(machine)) {
1021 * kernel uses PERF_RECORD_MISC_USER for user space maps,
1022 * see kernel/perf_event.c __perf_event_mmap
1024 event->header.misc = PERF_RECORD_MISC_KERNEL;
1025 } else {
1026 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1029 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1030 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1031 size = PERF_ALIGN(size, sizeof(u64));
1032 event->mmap.header.type = PERF_RECORD_MMAP;
1033 event->mmap.header.size = (sizeof(event->mmap) -
1034 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1035 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1036 event->mmap.start = map->start;
1037 event->mmap.len = map->end - event->mmap.start;
1038 event->mmap.pid = machine->pid;
1040 err = perf_tool__process_synth_event(tool, event, machine, process);
1041 free(event);
1043 return err;
1046 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1047 perf_event__handler_t process,
1048 struct machine *machine)
1050 int err;
1052 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1053 if (err < 0)
1054 return err;
1056 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1059 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1060 struct perf_thread_map *threads,
1061 perf_event__handler_t process,
1062 struct machine *machine)
1064 union perf_event *event;
1065 int i, err, size;
1067 size = sizeof(event->thread_map);
1068 size += threads->nr * sizeof(event->thread_map.entries[0]);
1070 event = zalloc(size);
1071 if (!event)
1072 return -ENOMEM;
1074 event->header.type = PERF_RECORD_THREAD_MAP;
1075 event->header.size = size;
1076 event->thread_map.nr = threads->nr;
1078 for (i = 0; i < threads->nr; i++) {
1079 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1080 char *comm = perf_thread_map__comm(threads, i);
1082 if (!comm)
1083 comm = (char *) "";
1085 entry->pid = perf_thread_map__pid(threads, i);
1086 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1089 err = process(tool, event, NULL, machine);
1091 free(event);
1092 return err;
1095 static void synthesize_cpus(struct cpu_map_entries *cpus,
1096 struct perf_cpu_map *map)
1098 int i;
1100 cpus->nr = map->nr;
1102 for (i = 0; i < map->nr; i++)
1103 cpus->cpu[i] = map->map[i];
1106 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1107 struct perf_cpu_map *map, int max)
1109 int i;
1111 mask->nr = BITS_TO_LONGS(max);
1112 mask->long_size = sizeof(long);
1114 for (i = 0; i < map->nr; i++)
1115 set_bit(map->map[i], mask->mask);
1118 static size_t cpus_size(struct perf_cpu_map *map)
1120 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1123 static size_t mask_size(struct perf_cpu_map *map, int *max)
1125 int i;
1127 *max = 0;
1129 for (i = 0; i < map->nr; i++) {
1130 /* bit possition of the cpu is + 1 */
1131 int bit = map->map[i] + 1;
1133 if (bit > *max)
1134 *max = bit;
1137 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1140 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1142 size_t size_cpus, size_mask;
1143 bool is_dummy = perf_cpu_map__empty(map);
1146 * Both array and mask data have variable size based
1147 * on the number of cpus and their actual values.
1148 * The size of the 'struct perf_record_cpu_map_data' is:
1150 * array = size of 'struct cpu_map_entries' +
1151 * number of cpus * sizeof(u64)
1153 * mask = size of 'struct perf_record_record_cpu_map' +
1154 * maximum cpu bit converted to size of longs
1156 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1158 size_cpus = cpus_size(map);
1159 size_mask = mask_size(map, max);
1161 if (is_dummy || (size_cpus < size_mask)) {
1162 *size += size_cpus;
1163 *type = PERF_CPU_MAP__CPUS;
1164 } else {
1165 *size += size_mask;
1166 *type = PERF_CPU_MAP__MASK;
1169 *size += sizeof(struct perf_record_cpu_map_data);
1170 *size = PERF_ALIGN(*size, sizeof(u64));
1171 return zalloc(*size);
1174 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1175 u16 type, int max)
1177 data->type = type;
1179 switch (type) {
1180 case PERF_CPU_MAP__CPUS:
1181 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1182 break;
1183 case PERF_CPU_MAP__MASK:
1184 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1185 default:
1186 break;
1190 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1192 size_t size = sizeof(struct perf_record_cpu_map);
1193 struct perf_record_cpu_map *event;
1194 int max;
1195 u16 type;
1197 event = cpu_map_data__alloc(map, &size, &type, &max);
1198 if (!event)
1199 return NULL;
1201 event->header.type = PERF_RECORD_CPU_MAP;
1202 event->header.size = size;
1203 event->data.type = type;
1205 cpu_map_data__synthesize(&event->data, map, type, max);
1206 return event;
1209 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1210 struct perf_cpu_map *map,
1211 perf_event__handler_t process,
1212 struct machine *machine)
1214 struct perf_record_cpu_map *event;
1215 int err;
1217 event = cpu_map_event__new(map);
1218 if (!event)
1219 return -ENOMEM;
1221 err = process(tool, (union perf_event *) event, NULL, machine);
1223 free(event);
1224 return err;
1227 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1228 struct perf_stat_config *config,
1229 perf_event__handler_t process,
1230 struct machine *machine)
1232 struct perf_record_stat_config *event;
1233 int size, i = 0, err;
1235 size = sizeof(*event);
1236 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1238 event = zalloc(size);
1239 if (!event)
1240 return -ENOMEM;
1242 event->header.type = PERF_RECORD_STAT_CONFIG;
1243 event->header.size = size;
1244 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1246 #define ADD(__term, __val) \
1247 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1248 event->data[i].val = __val; \
1249 i++;
1251 ADD(AGGR_MODE, config->aggr_mode)
1252 ADD(INTERVAL, config->interval)
1253 ADD(SCALE, config->scale)
1255 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1256 "stat config terms unbalanced\n");
1257 #undef ADD
1259 err = process(tool, (union perf_event *) event, NULL, machine);
1261 free(event);
1262 return err;
1265 int perf_event__synthesize_stat(struct perf_tool *tool,
1266 u32 cpu, u32 thread, u64 id,
1267 struct perf_counts_values *count,
1268 perf_event__handler_t process,
1269 struct machine *machine)
1271 struct perf_record_stat event;
1273 event.header.type = PERF_RECORD_STAT;
1274 event.header.size = sizeof(event);
1275 event.header.misc = 0;
1277 event.id = id;
1278 event.cpu = cpu;
1279 event.thread = thread;
1280 event.val = count->val;
1281 event.ena = count->ena;
1282 event.run = count->run;
1284 return process(tool, (union perf_event *) &event, NULL, machine);
1287 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1288 u64 evtime, u64 type,
1289 perf_event__handler_t process,
1290 struct machine *machine)
1292 struct perf_record_stat_round event;
1294 event.header.type = PERF_RECORD_STAT_ROUND;
1295 event.header.size = sizeof(event);
1296 event.header.misc = 0;
1298 event.time = evtime;
1299 event.type = type;
1301 return process(tool, (union perf_event *) &event, NULL, machine);
1304 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1306 size_t sz, result = sizeof(struct perf_record_sample);
1308 if (type & PERF_SAMPLE_IDENTIFIER)
1309 result += sizeof(u64);
1311 if (type & PERF_SAMPLE_IP)
1312 result += sizeof(u64);
1314 if (type & PERF_SAMPLE_TID)
1315 result += sizeof(u64);
1317 if (type & PERF_SAMPLE_TIME)
1318 result += sizeof(u64);
1320 if (type & PERF_SAMPLE_ADDR)
1321 result += sizeof(u64);
1323 if (type & PERF_SAMPLE_ID)
1324 result += sizeof(u64);
1326 if (type & PERF_SAMPLE_STREAM_ID)
1327 result += sizeof(u64);
1329 if (type & PERF_SAMPLE_CPU)
1330 result += sizeof(u64);
1332 if (type & PERF_SAMPLE_PERIOD)
1333 result += sizeof(u64);
1335 if (type & PERF_SAMPLE_READ) {
1336 result += sizeof(u64);
1337 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1338 result += sizeof(u64);
1339 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1340 result += sizeof(u64);
1341 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1342 if (read_format & PERF_FORMAT_GROUP) {
1343 sz = sample->read.group.nr *
1344 sizeof(struct sample_read_value);
1345 result += sz;
1346 } else {
1347 result += sizeof(u64);
1351 if (type & PERF_SAMPLE_CALLCHAIN) {
1352 sz = (sample->callchain->nr + 1) * sizeof(u64);
1353 result += sz;
1356 if (type & PERF_SAMPLE_RAW) {
1357 result += sizeof(u32);
1358 result += sample->raw_size;
1361 if (type & PERF_SAMPLE_BRANCH_STACK) {
1362 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1363 /* nr, hw_idx */
1364 sz += 2 * sizeof(u64);
1365 result += sz;
1368 if (type & PERF_SAMPLE_REGS_USER) {
1369 if (sample->user_regs.abi) {
1370 result += sizeof(u64);
1371 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1372 result += sz;
1373 } else {
1374 result += sizeof(u64);
1378 if (type & PERF_SAMPLE_STACK_USER) {
1379 sz = sample->user_stack.size;
1380 result += sizeof(u64);
1381 if (sz) {
1382 result += sz;
1383 result += sizeof(u64);
1387 if (type & PERF_SAMPLE_WEIGHT)
1388 result += sizeof(u64);
1390 if (type & PERF_SAMPLE_DATA_SRC)
1391 result += sizeof(u64);
1393 if (type & PERF_SAMPLE_TRANSACTION)
1394 result += sizeof(u64);
1396 if (type & PERF_SAMPLE_REGS_INTR) {
1397 if (sample->intr_regs.abi) {
1398 result += sizeof(u64);
1399 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1400 result += sz;
1401 } else {
1402 result += sizeof(u64);
1406 if (type & PERF_SAMPLE_PHYS_ADDR)
1407 result += sizeof(u64);
1409 if (type & PERF_SAMPLE_CGROUP)
1410 result += sizeof(u64);
1412 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1413 result += sizeof(u64);
1415 if (type & PERF_SAMPLE_AUX) {
1416 result += sizeof(u64);
1417 result += sample->aux_sample.size;
1420 return result;
1423 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1424 const struct perf_sample *sample)
1426 __u64 *array;
1427 size_t sz;
1429 * used for cross-endian analysis. See git commit 65014ab3
1430 * for why this goofiness is needed.
1432 union u64_swap u;
1434 array = event->sample.array;
1436 if (type & PERF_SAMPLE_IDENTIFIER) {
1437 *array = sample->id;
1438 array++;
1441 if (type & PERF_SAMPLE_IP) {
1442 *array = sample->ip;
1443 array++;
1446 if (type & PERF_SAMPLE_TID) {
1447 u.val32[0] = sample->pid;
1448 u.val32[1] = sample->tid;
1449 *array = u.val64;
1450 array++;
1453 if (type & PERF_SAMPLE_TIME) {
1454 *array = sample->time;
1455 array++;
1458 if (type & PERF_SAMPLE_ADDR) {
1459 *array = sample->addr;
1460 array++;
1463 if (type & PERF_SAMPLE_ID) {
1464 *array = sample->id;
1465 array++;
1468 if (type & PERF_SAMPLE_STREAM_ID) {
1469 *array = sample->stream_id;
1470 array++;
1473 if (type & PERF_SAMPLE_CPU) {
1474 u.val32[0] = sample->cpu;
1475 u.val32[1] = 0;
1476 *array = u.val64;
1477 array++;
1480 if (type & PERF_SAMPLE_PERIOD) {
1481 *array = sample->period;
1482 array++;
1485 if (type & PERF_SAMPLE_READ) {
1486 if (read_format & PERF_FORMAT_GROUP)
1487 *array = sample->read.group.nr;
1488 else
1489 *array = sample->read.one.value;
1490 array++;
1492 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1493 *array = sample->read.time_enabled;
1494 array++;
1497 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1498 *array = sample->read.time_running;
1499 array++;
1502 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1503 if (read_format & PERF_FORMAT_GROUP) {
1504 sz = sample->read.group.nr *
1505 sizeof(struct sample_read_value);
1506 memcpy(array, sample->read.group.values, sz);
1507 array = (void *)array + sz;
1508 } else {
1509 *array = sample->read.one.id;
1510 array++;
1514 if (type & PERF_SAMPLE_CALLCHAIN) {
1515 sz = (sample->callchain->nr + 1) * sizeof(u64);
1516 memcpy(array, sample->callchain, sz);
1517 array = (void *)array + sz;
1520 if (type & PERF_SAMPLE_RAW) {
1521 u.val32[0] = sample->raw_size;
1522 *array = u.val64;
1523 array = (void *)array + sizeof(u32);
1525 memcpy(array, sample->raw_data, sample->raw_size);
1526 array = (void *)array + sample->raw_size;
1529 if (type & PERF_SAMPLE_BRANCH_STACK) {
1530 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1531 /* nr, hw_idx */
1532 sz += 2 * sizeof(u64);
1533 memcpy(array, sample->branch_stack, sz);
1534 array = (void *)array + sz;
1537 if (type & PERF_SAMPLE_REGS_USER) {
1538 if (sample->user_regs.abi) {
1539 *array++ = sample->user_regs.abi;
1540 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1541 memcpy(array, sample->user_regs.regs, sz);
1542 array = (void *)array + sz;
1543 } else {
1544 *array++ = 0;
1548 if (type & PERF_SAMPLE_STACK_USER) {
1549 sz = sample->user_stack.size;
1550 *array++ = sz;
1551 if (sz) {
1552 memcpy(array, sample->user_stack.data, sz);
1553 array = (void *)array + sz;
1554 *array++ = sz;
1558 if (type & PERF_SAMPLE_WEIGHT) {
1559 *array = sample->weight;
1560 array++;
1563 if (type & PERF_SAMPLE_DATA_SRC) {
1564 *array = sample->data_src;
1565 array++;
1568 if (type & PERF_SAMPLE_TRANSACTION) {
1569 *array = sample->transaction;
1570 array++;
1573 if (type & PERF_SAMPLE_REGS_INTR) {
1574 if (sample->intr_regs.abi) {
1575 *array++ = sample->intr_regs.abi;
1576 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1577 memcpy(array, sample->intr_regs.regs, sz);
1578 array = (void *)array + sz;
1579 } else {
1580 *array++ = 0;
1584 if (type & PERF_SAMPLE_PHYS_ADDR) {
1585 *array = sample->phys_addr;
1586 array++;
1589 if (type & PERF_SAMPLE_CGROUP) {
1590 *array = sample->cgroup;
1591 array++;
1594 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1595 *array = sample->data_page_size;
1596 array++;
1599 if (type & PERF_SAMPLE_AUX) {
1600 sz = sample->aux_sample.size;
1601 *array++ = sz;
1602 memcpy(array, sample->aux_sample.data, sz);
1603 array = (void *)array + sz;
1606 return 0;
1609 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1610 struct evlist *evlist, struct machine *machine)
1612 union perf_event *ev;
1613 struct evsel *evsel;
1614 size_t nr = 0, i = 0, sz, max_nr, n;
1615 int err;
1617 pr_debug2("Synthesizing id index\n");
1619 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1620 sizeof(struct id_index_entry);
1622 evlist__for_each_entry(evlist, evsel)
1623 nr += evsel->core.ids;
1625 n = nr > max_nr ? max_nr : nr;
1626 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1627 ev = zalloc(sz);
1628 if (!ev)
1629 return -ENOMEM;
1631 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1632 ev->id_index.header.size = sz;
1633 ev->id_index.nr = n;
1635 evlist__for_each_entry(evlist, evsel) {
1636 u32 j;
1638 for (j = 0; j < evsel->core.ids; j++) {
1639 struct id_index_entry *e;
1640 struct perf_sample_id *sid;
1642 if (i >= n) {
1643 err = process(tool, ev, NULL, machine);
1644 if (err)
1645 goto out_err;
1646 nr -= n;
1647 i = 0;
1650 e = &ev->id_index.entries[i++];
1652 e->id = evsel->core.id[j];
1654 sid = evlist__id2sid(evlist, e->id);
1655 if (!sid) {
1656 free(ev);
1657 return -ENOENT;
1660 e->idx = sid->idx;
1661 e->cpu = sid->cpu;
1662 e->tid = sid->tid;
1666 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1667 ev->id_index.header.size = sz;
1668 ev->id_index.nr = nr;
1670 err = process(tool, ev, NULL, machine);
1671 out_err:
1672 free(ev);
1674 return err;
1677 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1678 struct target *target, struct perf_thread_map *threads,
1679 perf_event__handler_t process, bool data_mmap,
1680 unsigned int nr_threads_synthesize)
1682 if (target__has_task(target))
1683 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1684 else if (target__has_cpu(target))
1685 return perf_event__synthesize_threads(tool, process,
1686 machine, data_mmap,
1687 nr_threads_synthesize);
1688 /* command specified */
1689 return 0;
1692 int machine__synthesize_threads(struct machine *machine, struct target *target,
1693 struct perf_thread_map *threads, bool data_mmap,
1694 unsigned int nr_threads_synthesize)
1696 return __machine__synthesize_threads(machine, NULL, target, threads,
1697 perf_event__process, data_mmap,
1698 nr_threads_synthesize);
1701 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1703 struct perf_record_event_update *ev;
1705 size += sizeof(*ev);
1706 size = PERF_ALIGN(size, sizeof(u64));
1708 ev = zalloc(size);
1709 if (ev) {
1710 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1711 ev->header.size = (u16)size;
1712 ev->type = type;
1713 ev->id = id;
1715 return ev;
1718 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1719 perf_event__handler_t process)
1721 size_t size = strlen(evsel->unit);
1722 struct perf_record_event_update *ev;
1723 int err;
1725 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1726 if (ev == NULL)
1727 return -ENOMEM;
1729 strlcpy(ev->data, evsel->unit, size + 1);
1730 err = process(tool, (union perf_event *)ev, NULL, NULL);
1731 free(ev);
1732 return err;
1735 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1736 perf_event__handler_t process)
1738 struct perf_record_event_update *ev;
1739 struct perf_record_event_update_scale *ev_data;
1740 int err;
1742 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1743 if (ev == NULL)
1744 return -ENOMEM;
1746 ev_data = (struct perf_record_event_update_scale *)ev->data;
1747 ev_data->scale = evsel->scale;
1748 err = process(tool, (union perf_event *)ev, NULL, NULL);
1749 free(ev);
1750 return err;
1753 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1754 perf_event__handler_t process)
1756 struct perf_record_event_update *ev;
1757 size_t len = strlen(evsel->name);
1758 int err;
1760 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1761 if (ev == NULL)
1762 return -ENOMEM;
1764 strlcpy(ev->data, evsel->name, len + 1);
1765 err = process(tool, (union perf_event *)ev, NULL, NULL);
1766 free(ev);
1767 return err;
1770 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1771 perf_event__handler_t process)
1773 size_t size = sizeof(struct perf_record_event_update);
1774 struct perf_record_event_update *ev;
1775 int max, err;
1776 u16 type;
1778 if (!evsel->core.own_cpus)
1779 return 0;
1781 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1782 if (!ev)
1783 return -ENOMEM;
1785 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1786 ev->header.size = (u16)size;
1787 ev->type = PERF_EVENT_UPDATE__CPUS;
1788 ev->id = evsel->core.id[0];
1790 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1791 evsel->core.own_cpus, type, max);
1793 err = process(tool, (union perf_event *)ev, NULL, NULL);
1794 free(ev);
1795 return err;
1798 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1799 perf_event__handler_t process)
1801 struct evsel *evsel;
1802 int err = 0;
1804 evlist__for_each_entry(evlist, evsel) {
1805 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1806 evsel->core.id, process);
1807 if (err) {
1808 pr_debug("failed to create perf header attribute\n");
1809 return err;
1813 return err;
1816 static bool has_unit(struct evsel *evsel)
1818 return evsel->unit && *evsel->unit;
1821 static bool has_scale(struct evsel *evsel)
1823 return evsel->scale != 1;
1826 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1827 perf_event__handler_t process, bool is_pipe)
1829 struct evsel *evsel;
1830 int err;
1833 * Synthesize other events stuff not carried within
1834 * attr event - unit, scale, name
1836 evlist__for_each_entry(evsel_list, evsel) {
1837 if (!evsel->supported)
1838 continue;
1841 * Synthesize unit and scale only if it's defined.
1843 if (has_unit(evsel)) {
1844 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1845 if (err < 0) {
1846 pr_err("Couldn't synthesize evsel unit.\n");
1847 return err;
1851 if (has_scale(evsel)) {
1852 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1853 if (err < 0) {
1854 pr_err("Couldn't synthesize evsel evsel.\n");
1855 return err;
1859 if (evsel->core.own_cpus) {
1860 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1861 if (err < 0) {
1862 pr_err("Couldn't synthesize evsel cpus.\n");
1863 return err;
1868 * Name is needed only for pipe output,
1869 * perf.data carries event names.
1871 if (is_pipe) {
1872 err = perf_event__synthesize_event_update_name(tool, evsel, process);
1873 if (err < 0) {
1874 pr_err("Couldn't synthesize evsel name.\n");
1875 return err;
1879 return 0;
1882 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
1883 u32 ids, u64 *id, perf_event__handler_t process)
1885 union perf_event *ev;
1886 size_t size;
1887 int err;
1889 size = sizeof(struct perf_event_attr);
1890 size = PERF_ALIGN(size, sizeof(u64));
1891 size += sizeof(struct perf_event_header);
1892 size += ids * sizeof(u64);
1894 ev = zalloc(size);
1896 if (ev == NULL)
1897 return -ENOMEM;
1899 ev->attr.attr = *attr;
1900 memcpy(ev->attr.id, id, ids * sizeof(u64));
1902 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1903 ev->attr.header.size = (u16)size;
1905 if (ev->attr.header.size == size)
1906 err = process(tool, ev, NULL, NULL);
1907 else
1908 err = -E2BIG;
1910 free(ev);
1912 return err;
1915 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
1916 perf_event__handler_t process)
1918 union perf_event ev;
1919 struct tracing_data *tdata;
1920 ssize_t size = 0, aligned_size = 0, padding;
1921 struct feat_fd ff;
1924 * We are going to store the size of the data followed
1925 * by the data contents. Since the fd descriptor is a pipe,
1926 * we cannot seek back to store the size of the data once
1927 * we know it. Instead we:
1929 * - write the tracing data to the temp file
1930 * - get/write the data size to pipe
1931 * - write the tracing data from the temp file
1932 * to the pipe
1934 tdata = tracing_data_get(&evlist->core.entries, fd, true);
1935 if (!tdata)
1936 return -1;
1938 memset(&ev, 0, sizeof(ev));
1940 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
1941 size = tdata->size;
1942 aligned_size = PERF_ALIGN(size, sizeof(u64));
1943 padding = aligned_size - size;
1944 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1945 ev.tracing_data.size = aligned_size;
1947 process(tool, &ev, NULL, NULL);
1950 * The put function will copy all the tracing data
1951 * stored in temp file to the pipe.
1953 tracing_data_put(tdata);
1955 ff = (struct feat_fd){ .fd = fd };
1956 if (write_padded(&ff, NULL, 0, padding))
1957 return -1;
1959 return aligned_size;
1962 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
1963 perf_event__handler_t process, struct machine *machine)
1965 union perf_event ev;
1966 size_t len;
1968 if (!pos->hit)
1969 return 0;
1971 memset(&ev, 0, sizeof(ev));
1973 len = pos->long_name_len + 1;
1974 len = PERF_ALIGN(len, NAME_ALIGN);
1975 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
1976 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1977 ev.build_id.header.misc = misc;
1978 ev.build_id.pid = machine->pid;
1979 ev.build_id.header.size = sizeof(ev.build_id) + len;
1980 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1982 return process(tool, &ev, NULL, machine);
1985 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
1986 struct evlist *evlist, perf_event__handler_t process, bool attrs)
1988 int err;
1990 if (attrs) {
1991 err = perf_event__synthesize_attrs(tool, evlist, process);
1992 if (err < 0) {
1993 pr_err("Couldn't synthesize attrs.\n");
1994 return err;
1998 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
1999 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2000 if (err < 0) {
2001 pr_err("Couldn't synthesize thread map.\n");
2002 return err;
2005 err = perf_event__synthesize_cpu_map(tool, evlist->core.cpus, process, NULL);
2006 if (err < 0) {
2007 pr_err("Couldn't synthesize thread map.\n");
2008 return err;
2011 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2012 if (err < 0) {
2013 pr_err("Couldn't synthesize config.\n");
2014 return err;
2017 return 0;
2020 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2022 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2023 struct evlist *evlist, perf_event__handler_t process)
2025 struct perf_header *header = &session->header;
2026 struct perf_record_header_feature *fe;
2027 struct feat_fd ff;
2028 size_t sz, sz_hdr;
2029 int feat, ret;
2031 sz_hdr = sizeof(fe->header);
2032 sz = sizeof(union perf_event);
2033 /* get a nice alignment */
2034 sz = PERF_ALIGN(sz, page_size);
2036 memset(&ff, 0, sizeof(ff));
2038 ff.buf = malloc(sz);
2039 if (!ff.buf)
2040 return -ENOMEM;
2042 ff.size = sz - sz_hdr;
2043 ff.ph = &session->header;
2045 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2046 if (!feat_ops[feat].synthesize) {
2047 pr_debug("No record header feature for header :%d\n", feat);
2048 continue;
2051 ff.offset = sizeof(*fe);
2053 ret = feat_ops[feat].write(&ff, evlist);
2054 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2055 pr_debug("Error writing feature\n");
2056 continue;
2058 /* ff.buf may have changed due to realloc in do_write() */
2059 fe = ff.buf;
2060 memset(fe, 0, sizeof(*fe));
2062 fe->feat_id = feat;
2063 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2064 fe->header.size = ff.offset;
2066 ret = process(tool, ff.buf, NULL, NULL);
2067 if (ret) {
2068 free(ff.buf);
2069 return ret;
2073 /* Send HEADER_LAST_FEATURE mark. */
2074 fe = ff.buf;
2075 fe->feat_id = HEADER_LAST_FEATURE;
2076 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2077 fe->header.size = sizeof(*fe);
2079 ret = process(tool, ff.buf, NULL, NULL);
2081 free(ff.buf);
2082 return ret;