gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / tools / perf / util / synthetic-events.c
bloba661b122d9d8f25836bd12943e119c6da3c26c9f
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 <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <unistd.h>
45 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
47 unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
49 int perf_tool__process_synth_event(struct perf_tool *tool,
50 union perf_event *event,
51 struct machine *machine,
52 perf_event__handler_t process)
54 struct perf_sample synth_sample = {
55 .pid = -1,
56 .tid = -1,
57 .time = -1,
58 .stream_id = -1,
59 .cpu = -1,
60 .period = 1,
61 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
64 return process(tool, event, &synth_sample, machine);
68 * Assumes that the first 4095 bytes of /proc/pid/stat contains
69 * the comm, tgid and ppid.
71 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
72 pid_t *tgid, pid_t *ppid)
74 char filename[PATH_MAX];
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(filename, sizeof(filename), "/proc/%d/status", pid);
86 fd = open(filename, O_RDONLY);
87 if (fd < 0) {
88 pr_debug("couldn't open %s\n", filename);
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 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
278 union perf_event *event,
279 pid_t pid, pid_t tgid,
280 perf_event__handler_t process,
281 struct machine *machine,
282 bool mmap_data)
284 char filename[PATH_MAX];
285 FILE *fp;
286 unsigned long long t;
287 bool truncation = false;
288 unsigned long long timeout = proc_map_timeout * 1000000ULL;
289 int rc = 0;
290 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
291 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
293 if (machine__is_default_guest(machine))
294 return 0;
296 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
297 machine->root_dir, pid, pid);
299 fp = fopen(filename, "r");
300 if (fp == NULL) {
302 * We raced with a task exiting - just return:
304 pr_debug("couldn't open %s\n", filename);
305 return -1;
308 event->header.type = PERF_RECORD_MMAP2;
309 t = rdclock();
311 while (1) {
312 char bf[BUFSIZ];
313 char prot[5];
314 char execname[PATH_MAX];
315 char anonstr[] = "//anon";
316 unsigned int ino;
317 size_t size;
318 ssize_t n;
320 if (fgets(bf, sizeof(bf), fp) == NULL)
321 break;
323 if ((rdclock() - t) > timeout) {
324 pr_warning("Reading %s time out. "
325 "You may want to increase "
326 "the time limit by --proc-map-timeout\n",
327 filename);
328 truncation = true;
329 goto out;
332 /* ensure null termination since stack will be reused. */
333 strcpy(execname, "");
335 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
336 n = sscanf(bf, "%"PRI_lx64"-%"PRI_lx64" %s %"PRI_lx64" %x:%x %u %[^\n]\n",
337 &event->mmap2.start, &event->mmap2.len, prot,
338 &event->mmap2.pgoff, &event->mmap2.maj,
339 &event->mmap2.min,
340 &ino, execname);
343 * Anon maps don't have the execname.
345 if (n < 7)
346 continue;
348 event->mmap2.ino = (u64)ino;
349 event->mmap2.ino_generation = 0;
352 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
354 if (machine__is_host(machine))
355 event->header.misc = PERF_RECORD_MISC_USER;
356 else
357 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
359 /* map protection and flags bits */
360 event->mmap2.prot = 0;
361 event->mmap2.flags = 0;
362 if (prot[0] == 'r')
363 event->mmap2.prot |= PROT_READ;
364 if (prot[1] == 'w')
365 event->mmap2.prot |= PROT_WRITE;
366 if (prot[2] == 'x')
367 event->mmap2.prot |= PROT_EXEC;
369 if (prot[3] == 's')
370 event->mmap2.flags |= MAP_SHARED;
371 else
372 event->mmap2.flags |= MAP_PRIVATE;
374 if (prot[2] != 'x') {
375 if (!mmap_data || prot[0] != 'r')
376 continue;
378 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
381 out:
382 if (truncation)
383 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
385 if (!strcmp(execname, ""))
386 strcpy(execname, anonstr);
388 if (hugetlbfs_mnt_len &&
389 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
390 strcpy(execname, anonstr);
391 event->mmap2.flags |= MAP_HUGETLB;
394 size = strlen(execname) + 1;
395 memcpy(event->mmap2.filename, execname, size);
396 size = PERF_ALIGN(size, sizeof(u64));
397 event->mmap2.len -= event->mmap.start;
398 event->mmap2.header.size = (sizeof(event->mmap2) -
399 (sizeof(event->mmap2.filename) - size));
400 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
401 event->mmap2.header.size += machine->id_hdr_size;
402 event->mmap2.pid = tgid;
403 event->mmap2.tid = pid;
405 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
406 rc = -1;
407 break;
410 if (truncation)
411 break;
414 fclose(fp);
415 return rc;
418 #ifdef HAVE_FILE_HANDLE
419 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
420 union perf_event *event,
421 char *path, size_t mount_len,
422 perf_event__handler_t process,
423 struct machine *machine)
425 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
426 size_t path_len = strlen(path) - mount_len + 1;
427 struct {
428 struct file_handle fh;
429 uint64_t cgroup_id;
430 } handle;
431 int mount_id;
433 while (path_len % sizeof(u64))
434 path[mount_len + path_len++] = '\0';
436 memset(&event->cgroup, 0, event_size);
438 event->cgroup.header.type = PERF_RECORD_CGROUP;
439 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
441 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
442 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
443 pr_debug("stat failed: %s\n", path);
444 return -1;
447 event->cgroup.id = handle.cgroup_id;
448 strncpy(event->cgroup.path, path + mount_len, path_len);
449 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
451 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
452 pr_debug("process synth event failed\n");
453 return -1;
456 return 0;
459 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
460 union perf_event *event,
461 char *path, size_t mount_len,
462 perf_event__handler_t process,
463 struct machine *machine)
465 size_t pos = strlen(path);
466 DIR *d;
467 struct dirent *dent;
468 int ret = 0;
470 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
471 process, machine) < 0)
472 return -1;
474 d = opendir(path);
475 if (d == NULL) {
476 pr_debug("failed to open directory: %s\n", path);
477 return -1;
480 while ((dent = readdir(d)) != NULL) {
481 if (dent->d_type != DT_DIR)
482 continue;
483 if (!strcmp(dent->d_name, ".") ||
484 !strcmp(dent->d_name, ".."))
485 continue;
487 /* any sane path should be less than PATH_MAX */
488 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
489 continue;
491 if (path[pos - 1] != '/')
492 strcat(path, "/");
493 strcat(path, dent->d_name);
495 ret = perf_event__walk_cgroup_tree(tool, event, path,
496 mount_len, process, machine);
497 if (ret < 0)
498 break;
500 path[pos] = '\0';
503 closedir(d);
504 return ret;
507 int perf_event__synthesize_cgroups(struct perf_tool *tool,
508 perf_event__handler_t process,
509 struct machine *machine)
511 union perf_event event;
512 char cgrp_root[PATH_MAX];
513 size_t mount_len; /* length of mount point in the path */
515 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
516 pr_debug("cannot find cgroup mount point\n");
517 return -1;
520 mount_len = strlen(cgrp_root);
521 /* make sure the path starts with a slash (after mount point) */
522 strcat(cgrp_root, "/");
524 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
525 process, machine) < 0)
526 return -1;
528 return 0;
530 #else
531 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
532 perf_event__handler_t process __maybe_unused,
533 struct machine *machine __maybe_unused)
535 return -1;
537 #endif
539 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
540 struct machine *machine)
542 int rc = 0;
543 struct map *pos;
544 struct maps *maps = machine__kernel_maps(machine);
545 union perf_event *event = zalloc((sizeof(event->mmap) +
546 machine->id_hdr_size));
547 if (event == NULL) {
548 pr_debug("Not enough memory synthesizing mmap event "
549 "for kernel modules\n");
550 return -1;
553 event->header.type = PERF_RECORD_MMAP;
556 * kernel uses 0 for user space maps, see kernel/perf_event.c
557 * __perf_event_mmap
559 if (machine__is_host(machine))
560 event->header.misc = PERF_RECORD_MISC_KERNEL;
561 else
562 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
564 maps__for_each_entry(maps, pos) {
565 size_t size;
567 if (!__map__is_kmodule(pos))
568 continue;
570 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
571 event->mmap.header.type = PERF_RECORD_MMAP;
572 event->mmap.header.size = (sizeof(event->mmap) -
573 (sizeof(event->mmap.filename) - size));
574 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
575 event->mmap.header.size += machine->id_hdr_size;
576 event->mmap.start = pos->start;
577 event->mmap.len = pos->end - pos->start;
578 event->mmap.pid = machine->pid;
580 memcpy(event->mmap.filename, pos->dso->long_name,
581 pos->dso->long_name_len + 1);
582 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
583 rc = -1;
584 break;
588 free(event);
589 return rc;
592 static int __event__synthesize_thread(union perf_event *comm_event,
593 union perf_event *mmap_event,
594 union perf_event *fork_event,
595 union perf_event *namespaces_event,
596 pid_t pid, int full, perf_event__handler_t process,
597 struct perf_tool *tool, struct machine *machine, bool mmap_data)
599 char filename[PATH_MAX];
600 DIR *tasks;
601 struct dirent *dirent;
602 pid_t tgid, ppid;
603 int rc = 0;
605 /* special case: only send one comm event using passed in pid */
606 if (!full) {
607 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
608 process, machine);
610 if (tgid == -1)
611 return -1;
613 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
614 tgid, process, machine) < 0)
615 return -1;
618 * send mmap only for thread group leader
619 * see thread__init_maps()
621 if (pid == tgid &&
622 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
623 process, machine, mmap_data))
624 return -1;
626 return 0;
629 if (machine__is_default_guest(machine))
630 return 0;
632 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
633 machine->root_dir, pid);
635 tasks = opendir(filename);
636 if (tasks == NULL) {
637 pr_debug("couldn't open %s\n", filename);
638 return 0;
641 while ((dirent = readdir(tasks)) != NULL) {
642 char *end;
643 pid_t _pid;
645 _pid = strtol(dirent->d_name, &end, 10);
646 if (*end)
647 continue;
649 rc = -1;
650 if (perf_event__prepare_comm(comm_event, _pid, machine,
651 &tgid, &ppid) != 0)
652 break;
654 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
655 ppid, process, machine) < 0)
656 break;
658 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
659 tgid, process, machine) < 0)
660 break;
663 * Send the prepared comm event
665 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
666 break;
668 rc = 0;
669 if (_pid == pid) {
670 /* process the parent's maps too */
671 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
672 process, machine, mmap_data);
673 if (rc)
674 break;
678 closedir(tasks);
679 return rc;
682 int perf_event__synthesize_thread_map(struct perf_tool *tool,
683 struct perf_thread_map *threads,
684 perf_event__handler_t process,
685 struct machine *machine,
686 bool mmap_data)
688 union perf_event *comm_event, *mmap_event, *fork_event;
689 union perf_event *namespaces_event;
690 int err = -1, thread, j;
692 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
693 if (comm_event == NULL)
694 goto out;
696 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
697 if (mmap_event == NULL)
698 goto out_free_comm;
700 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
701 if (fork_event == NULL)
702 goto out_free_mmap;
704 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
705 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
706 machine->id_hdr_size);
707 if (namespaces_event == NULL)
708 goto out_free_fork;
710 err = 0;
711 for (thread = 0; thread < threads->nr; ++thread) {
712 if (__event__synthesize_thread(comm_event, mmap_event,
713 fork_event, namespaces_event,
714 perf_thread_map__pid(threads, thread), 0,
715 process, tool, machine,
716 mmap_data)) {
717 err = -1;
718 break;
722 * comm.pid is set to thread group id by
723 * perf_event__synthesize_comm
725 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
726 bool need_leader = true;
728 /* is thread group leader in thread_map? */
729 for (j = 0; j < threads->nr; ++j) {
730 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
731 need_leader = false;
732 break;
736 /* if not, generate events for it */
737 if (need_leader &&
738 __event__synthesize_thread(comm_event, mmap_event,
739 fork_event, namespaces_event,
740 comm_event->comm.pid, 0,
741 process, tool, machine,
742 mmap_data)) {
743 err = -1;
744 break;
748 free(namespaces_event);
749 out_free_fork:
750 free(fork_event);
751 out_free_mmap:
752 free(mmap_event);
753 out_free_comm:
754 free(comm_event);
755 out:
756 return err;
759 static int __perf_event__synthesize_threads(struct perf_tool *tool,
760 perf_event__handler_t process,
761 struct machine *machine,
762 bool mmap_data,
763 struct dirent **dirent,
764 int start,
765 int num)
767 union perf_event *comm_event, *mmap_event, *fork_event;
768 union perf_event *namespaces_event;
769 int err = -1;
770 char *end;
771 pid_t pid;
772 int i;
774 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
775 if (comm_event == NULL)
776 goto out;
778 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
779 if (mmap_event == NULL)
780 goto out_free_comm;
782 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
783 if (fork_event == NULL)
784 goto out_free_mmap;
786 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
787 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
788 machine->id_hdr_size);
789 if (namespaces_event == NULL)
790 goto out_free_fork;
792 for (i = start; i < start + num; i++) {
793 if (!isdigit(dirent[i]->d_name[0]))
794 continue;
796 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
797 /* only interested in proper numerical dirents */
798 if (*end)
799 continue;
801 * We may race with exiting thread, so don't stop just because
802 * one thread couldn't be synthesized.
804 __event__synthesize_thread(comm_event, mmap_event, fork_event,
805 namespaces_event, pid, 1, process,
806 tool, machine, mmap_data);
808 err = 0;
810 free(namespaces_event);
811 out_free_fork:
812 free(fork_event);
813 out_free_mmap:
814 free(mmap_event);
815 out_free_comm:
816 free(comm_event);
817 out:
818 return err;
821 struct synthesize_threads_arg {
822 struct perf_tool *tool;
823 perf_event__handler_t process;
824 struct machine *machine;
825 bool mmap_data;
826 struct dirent **dirent;
827 int num;
828 int start;
831 static void *synthesize_threads_worker(void *arg)
833 struct synthesize_threads_arg *args = arg;
835 __perf_event__synthesize_threads(args->tool, args->process,
836 args->machine, args->mmap_data,
837 args->dirent,
838 args->start, args->num);
839 return NULL;
842 int perf_event__synthesize_threads(struct perf_tool *tool,
843 perf_event__handler_t process,
844 struct machine *machine,
845 bool mmap_data,
846 unsigned int nr_threads_synthesize)
848 struct synthesize_threads_arg *args = NULL;
849 pthread_t *synthesize_threads = NULL;
850 char proc_path[PATH_MAX];
851 struct dirent **dirent;
852 int num_per_thread;
853 int m, n, i, j;
854 int thread_nr;
855 int base = 0;
856 int err = -1;
859 if (machine__is_default_guest(machine))
860 return 0;
862 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
863 n = scandir(proc_path, &dirent, 0, alphasort);
864 if (n < 0)
865 return err;
867 if (nr_threads_synthesize == UINT_MAX)
868 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
869 else
870 thread_nr = nr_threads_synthesize;
872 if (thread_nr <= 1) {
873 err = __perf_event__synthesize_threads(tool, process,
874 machine, mmap_data,
875 dirent, base, n);
876 goto free_dirent;
878 if (thread_nr > n)
879 thread_nr = n;
881 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
882 if (synthesize_threads == NULL)
883 goto free_dirent;
885 args = calloc(sizeof(*args), thread_nr);
886 if (args == NULL)
887 goto free_threads;
889 num_per_thread = n / thread_nr;
890 m = n % thread_nr;
891 for (i = 0; i < thread_nr; i++) {
892 args[i].tool = tool;
893 args[i].process = process;
894 args[i].machine = machine;
895 args[i].mmap_data = mmap_data;
896 args[i].dirent = dirent;
898 for (i = 0; i < m; i++) {
899 args[i].num = num_per_thread + 1;
900 args[i].start = i * args[i].num;
902 if (i != 0)
903 base = args[i-1].start + args[i-1].num;
904 for (j = i; j < thread_nr; j++) {
905 args[j].num = num_per_thread;
906 args[j].start = base + (j - i) * args[i].num;
909 for (i = 0; i < thread_nr; i++) {
910 if (pthread_create(&synthesize_threads[i], NULL,
911 synthesize_threads_worker, &args[i]))
912 goto out_join;
914 err = 0;
915 out_join:
916 for (i = 0; i < thread_nr; i++)
917 pthread_join(synthesize_threads[i], NULL);
918 free(args);
919 free_threads:
920 free(synthesize_threads);
921 free_dirent:
922 for (i = 0; i < n; i++)
923 zfree(&dirent[i]);
924 free(dirent);
926 return err;
929 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
930 perf_event__handler_t process __maybe_unused,
931 struct machine *machine __maybe_unused)
933 return 0;
936 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
937 perf_event__handler_t process,
938 struct machine *machine)
940 size_t size;
941 struct map *map = machine__kernel_map(machine);
942 struct kmap *kmap;
943 int err;
944 union perf_event *event;
946 if (map == NULL)
947 return -1;
949 kmap = map__kmap(map);
950 if (!kmap->ref_reloc_sym)
951 return -1;
954 * We should get this from /sys/kernel/sections/.text, but till that is
955 * available use this, and after it is use this as a fallback for older
956 * kernels.
958 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
959 if (event == NULL) {
960 pr_debug("Not enough memory synthesizing mmap event "
961 "for kernel modules\n");
962 return -1;
965 if (machine__is_host(machine)) {
967 * kernel uses PERF_RECORD_MISC_USER for user space maps,
968 * see kernel/perf_event.c __perf_event_mmap
970 event->header.misc = PERF_RECORD_MISC_KERNEL;
971 } else {
972 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
975 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
976 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
977 size = PERF_ALIGN(size, sizeof(u64));
978 event->mmap.header.type = PERF_RECORD_MMAP;
979 event->mmap.header.size = (sizeof(event->mmap) -
980 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
981 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
982 event->mmap.start = map->start;
983 event->mmap.len = map->end - event->mmap.start;
984 event->mmap.pid = machine->pid;
986 err = perf_tool__process_synth_event(tool, event, machine, process);
987 free(event);
989 return err;
992 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
993 perf_event__handler_t process,
994 struct machine *machine)
996 int err;
998 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
999 if (err < 0)
1000 return err;
1002 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1005 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1006 struct perf_thread_map *threads,
1007 perf_event__handler_t process,
1008 struct machine *machine)
1010 union perf_event *event;
1011 int i, err, size;
1013 size = sizeof(event->thread_map);
1014 size += threads->nr * sizeof(event->thread_map.entries[0]);
1016 event = zalloc(size);
1017 if (!event)
1018 return -ENOMEM;
1020 event->header.type = PERF_RECORD_THREAD_MAP;
1021 event->header.size = size;
1022 event->thread_map.nr = threads->nr;
1024 for (i = 0; i < threads->nr; i++) {
1025 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1026 char *comm = perf_thread_map__comm(threads, i);
1028 if (!comm)
1029 comm = (char *) "";
1031 entry->pid = perf_thread_map__pid(threads, i);
1032 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1035 err = process(tool, event, NULL, machine);
1037 free(event);
1038 return err;
1041 static void synthesize_cpus(struct cpu_map_entries *cpus,
1042 struct perf_cpu_map *map)
1044 int i;
1046 cpus->nr = map->nr;
1048 for (i = 0; i < map->nr; i++)
1049 cpus->cpu[i] = map->map[i];
1052 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1053 struct perf_cpu_map *map, int max)
1055 int i;
1057 mask->nr = BITS_TO_LONGS(max);
1058 mask->long_size = sizeof(long);
1060 for (i = 0; i < map->nr; i++)
1061 set_bit(map->map[i], mask->mask);
1064 static size_t cpus_size(struct perf_cpu_map *map)
1066 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1069 static size_t mask_size(struct perf_cpu_map *map, int *max)
1071 int i;
1073 *max = 0;
1075 for (i = 0; i < map->nr; i++) {
1076 /* bit possition of the cpu is + 1 */
1077 int bit = map->map[i] + 1;
1079 if (bit > *max)
1080 *max = bit;
1083 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1086 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1088 size_t size_cpus, size_mask;
1089 bool is_dummy = perf_cpu_map__empty(map);
1092 * Both array and mask data have variable size based
1093 * on the number of cpus and their actual values.
1094 * The size of the 'struct perf_record_cpu_map_data' is:
1096 * array = size of 'struct cpu_map_entries' +
1097 * number of cpus * sizeof(u64)
1099 * mask = size of 'struct perf_record_record_cpu_map' +
1100 * maximum cpu bit converted to size of longs
1102 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1104 size_cpus = cpus_size(map);
1105 size_mask = mask_size(map, max);
1107 if (is_dummy || (size_cpus < size_mask)) {
1108 *size += size_cpus;
1109 *type = PERF_CPU_MAP__CPUS;
1110 } else {
1111 *size += size_mask;
1112 *type = PERF_CPU_MAP__MASK;
1115 *size += sizeof(struct perf_record_cpu_map_data);
1116 *size = PERF_ALIGN(*size, sizeof(u64));
1117 return zalloc(*size);
1120 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1121 u16 type, int max)
1123 data->type = type;
1125 switch (type) {
1126 case PERF_CPU_MAP__CPUS:
1127 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1128 break;
1129 case PERF_CPU_MAP__MASK:
1130 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1131 default:
1132 break;
1136 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1138 size_t size = sizeof(struct perf_record_cpu_map);
1139 struct perf_record_cpu_map *event;
1140 int max;
1141 u16 type;
1143 event = cpu_map_data__alloc(map, &size, &type, &max);
1144 if (!event)
1145 return NULL;
1147 event->header.type = PERF_RECORD_CPU_MAP;
1148 event->header.size = size;
1149 event->data.type = type;
1151 cpu_map_data__synthesize(&event->data, map, type, max);
1152 return event;
1155 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1156 struct perf_cpu_map *map,
1157 perf_event__handler_t process,
1158 struct machine *machine)
1160 struct perf_record_cpu_map *event;
1161 int err;
1163 event = cpu_map_event__new(map);
1164 if (!event)
1165 return -ENOMEM;
1167 err = process(tool, (union perf_event *) event, NULL, machine);
1169 free(event);
1170 return err;
1173 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1174 struct perf_stat_config *config,
1175 perf_event__handler_t process,
1176 struct machine *machine)
1178 struct perf_record_stat_config *event;
1179 int size, i = 0, err;
1181 size = sizeof(*event);
1182 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1184 event = zalloc(size);
1185 if (!event)
1186 return -ENOMEM;
1188 event->header.type = PERF_RECORD_STAT_CONFIG;
1189 event->header.size = size;
1190 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1192 #define ADD(__term, __val) \
1193 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1194 event->data[i].val = __val; \
1195 i++;
1197 ADD(AGGR_MODE, config->aggr_mode)
1198 ADD(INTERVAL, config->interval)
1199 ADD(SCALE, config->scale)
1201 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1202 "stat config terms unbalanced\n");
1203 #undef ADD
1205 err = process(tool, (union perf_event *) event, NULL, machine);
1207 free(event);
1208 return err;
1211 int perf_event__synthesize_stat(struct perf_tool *tool,
1212 u32 cpu, u32 thread, u64 id,
1213 struct perf_counts_values *count,
1214 perf_event__handler_t process,
1215 struct machine *machine)
1217 struct perf_record_stat event;
1219 event.header.type = PERF_RECORD_STAT;
1220 event.header.size = sizeof(event);
1221 event.header.misc = 0;
1223 event.id = id;
1224 event.cpu = cpu;
1225 event.thread = thread;
1226 event.val = count->val;
1227 event.ena = count->ena;
1228 event.run = count->run;
1230 return process(tool, (union perf_event *) &event, NULL, machine);
1233 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1234 u64 evtime, u64 type,
1235 perf_event__handler_t process,
1236 struct machine *machine)
1238 struct perf_record_stat_round event;
1240 event.header.type = PERF_RECORD_STAT_ROUND;
1241 event.header.size = sizeof(event);
1242 event.header.misc = 0;
1244 event.time = evtime;
1245 event.type = type;
1247 return process(tool, (union perf_event *) &event, NULL, machine);
1250 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1252 size_t sz, result = sizeof(struct perf_record_sample);
1254 if (type & PERF_SAMPLE_IDENTIFIER)
1255 result += sizeof(u64);
1257 if (type & PERF_SAMPLE_IP)
1258 result += sizeof(u64);
1260 if (type & PERF_SAMPLE_TID)
1261 result += sizeof(u64);
1263 if (type & PERF_SAMPLE_TIME)
1264 result += sizeof(u64);
1266 if (type & PERF_SAMPLE_ADDR)
1267 result += sizeof(u64);
1269 if (type & PERF_SAMPLE_ID)
1270 result += sizeof(u64);
1272 if (type & PERF_SAMPLE_STREAM_ID)
1273 result += sizeof(u64);
1275 if (type & PERF_SAMPLE_CPU)
1276 result += sizeof(u64);
1278 if (type & PERF_SAMPLE_PERIOD)
1279 result += sizeof(u64);
1281 if (type & PERF_SAMPLE_READ) {
1282 result += sizeof(u64);
1283 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1284 result += sizeof(u64);
1285 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1286 result += sizeof(u64);
1287 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1288 if (read_format & PERF_FORMAT_GROUP) {
1289 sz = sample->read.group.nr *
1290 sizeof(struct sample_read_value);
1291 result += sz;
1292 } else {
1293 result += sizeof(u64);
1297 if (type & PERF_SAMPLE_CALLCHAIN) {
1298 sz = (sample->callchain->nr + 1) * sizeof(u64);
1299 result += sz;
1302 if (type & PERF_SAMPLE_RAW) {
1303 result += sizeof(u32);
1304 result += sample->raw_size;
1307 if (type & PERF_SAMPLE_BRANCH_STACK) {
1308 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1309 /* nr, hw_idx */
1310 sz += 2 * sizeof(u64);
1311 result += sz;
1314 if (type & PERF_SAMPLE_REGS_USER) {
1315 if (sample->user_regs.abi) {
1316 result += sizeof(u64);
1317 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1318 result += sz;
1319 } else {
1320 result += sizeof(u64);
1324 if (type & PERF_SAMPLE_STACK_USER) {
1325 sz = sample->user_stack.size;
1326 result += sizeof(u64);
1327 if (sz) {
1328 result += sz;
1329 result += sizeof(u64);
1333 if (type & PERF_SAMPLE_WEIGHT)
1334 result += sizeof(u64);
1336 if (type & PERF_SAMPLE_DATA_SRC)
1337 result += sizeof(u64);
1339 if (type & PERF_SAMPLE_TRANSACTION)
1340 result += sizeof(u64);
1342 if (type & PERF_SAMPLE_REGS_INTR) {
1343 if (sample->intr_regs.abi) {
1344 result += sizeof(u64);
1345 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1346 result += sz;
1347 } else {
1348 result += sizeof(u64);
1352 if (type & PERF_SAMPLE_PHYS_ADDR)
1353 result += sizeof(u64);
1355 if (type & PERF_SAMPLE_CGROUP)
1356 result += sizeof(u64);
1358 if (type & PERF_SAMPLE_AUX) {
1359 result += sizeof(u64);
1360 result += sample->aux_sample.size;
1363 return result;
1366 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1367 const struct perf_sample *sample)
1369 __u64 *array;
1370 size_t sz;
1372 * used for cross-endian analysis. See git commit 65014ab3
1373 * for why this goofiness is needed.
1375 union u64_swap u;
1377 array = event->sample.array;
1379 if (type & PERF_SAMPLE_IDENTIFIER) {
1380 *array = sample->id;
1381 array++;
1384 if (type & PERF_SAMPLE_IP) {
1385 *array = sample->ip;
1386 array++;
1389 if (type & PERF_SAMPLE_TID) {
1390 u.val32[0] = sample->pid;
1391 u.val32[1] = sample->tid;
1392 *array = u.val64;
1393 array++;
1396 if (type & PERF_SAMPLE_TIME) {
1397 *array = sample->time;
1398 array++;
1401 if (type & PERF_SAMPLE_ADDR) {
1402 *array = sample->addr;
1403 array++;
1406 if (type & PERF_SAMPLE_ID) {
1407 *array = sample->id;
1408 array++;
1411 if (type & PERF_SAMPLE_STREAM_ID) {
1412 *array = sample->stream_id;
1413 array++;
1416 if (type & PERF_SAMPLE_CPU) {
1417 u.val32[0] = sample->cpu;
1418 u.val32[1] = 0;
1419 *array = u.val64;
1420 array++;
1423 if (type & PERF_SAMPLE_PERIOD) {
1424 *array = sample->period;
1425 array++;
1428 if (type & PERF_SAMPLE_READ) {
1429 if (read_format & PERF_FORMAT_GROUP)
1430 *array = sample->read.group.nr;
1431 else
1432 *array = sample->read.one.value;
1433 array++;
1435 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1436 *array = sample->read.time_enabled;
1437 array++;
1440 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1441 *array = sample->read.time_running;
1442 array++;
1445 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1446 if (read_format & PERF_FORMAT_GROUP) {
1447 sz = sample->read.group.nr *
1448 sizeof(struct sample_read_value);
1449 memcpy(array, sample->read.group.values, sz);
1450 array = (void *)array + sz;
1451 } else {
1452 *array = sample->read.one.id;
1453 array++;
1457 if (type & PERF_SAMPLE_CALLCHAIN) {
1458 sz = (sample->callchain->nr + 1) * sizeof(u64);
1459 memcpy(array, sample->callchain, sz);
1460 array = (void *)array + sz;
1463 if (type & PERF_SAMPLE_RAW) {
1464 u.val32[0] = sample->raw_size;
1465 *array = u.val64;
1466 array = (void *)array + sizeof(u32);
1468 memcpy(array, sample->raw_data, sample->raw_size);
1469 array = (void *)array + sample->raw_size;
1472 if (type & PERF_SAMPLE_BRANCH_STACK) {
1473 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1474 /* nr, hw_idx */
1475 sz += 2 * sizeof(u64);
1476 memcpy(array, sample->branch_stack, sz);
1477 array = (void *)array + sz;
1480 if (type & PERF_SAMPLE_REGS_USER) {
1481 if (sample->user_regs.abi) {
1482 *array++ = sample->user_regs.abi;
1483 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1484 memcpy(array, sample->user_regs.regs, sz);
1485 array = (void *)array + sz;
1486 } else {
1487 *array++ = 0;
1491 if (type & PERF_SAMPLE_STACK_USER) {
1492 sz = sample->user_stack.size;
1493 *array++ = sz;
1494 if (sz) {
1495 memcpy(array, sample->user_stack.data, sz);
1496 array = (void *)array + sz;
1497 *array++ = sz;
1501 if (type & PERF_SAMPLE_WEIGHT) {
1502 *array = sample->weight;
1503 array++;
1506 if (type & PERF_SAMPLE_DATA_SRC) {
1507 *array = sample->data_src;
1508 array++;
1511 if (type & PERF_SAMPLE_TRANSACTION) {
1512 *array = sample->transaction;
1513 array++;
1516 if (type & PERF_SAMPLE_REGS_INTR) {
1517 if (sample->intr_regs.abi) {
1518 *array++ = sample->intr_regs.abi;
1519 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1520 memcpy(array, sample->intr_regs.regs, sz);
1521 array = (void *)array + sz;
1522 } else {
1523 *array++ = 0;
1527 if (type & PERF_SAMPLE_PHYS_ADDR) {
1528 *array = sample->phys_addr;
1529 array++;
1532 if (type & PERF_SAMPLE_CGROUP) {
1533 *array = sample->cgroup;
1534 array++;
1537 if (type & PERF_SAMPLE_AUX) {
1538 sz = sample->aux_sample.size;
1539 *array++ = sz;
1540 memcpy(array, sample->aux_sample.data, sz);
1541 array = (void *)array + sz;
1544 return 0;
1547 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1548 struct evlist *evlist, struct machine *machine)
1550 union perf_event *ev;
1551 struct evsel *evsel;
1552 size_t nr = 0, i = 0, sz, max_nr, n;
1553 int err;
1555 pr_debug2("Synthesizing id index\n");
1557 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1558 sizeof(struct id_index_entry);
1560 evlist__for_each_entry(evlist, evsel)
1561 nr += evsel->core.ids;
1563 n = nr > max_nr ? max_nr : nr;
1564 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1565 ev = zalloc(sz);
1566 if (!ev)
1567 return -ENOMEM;
1569 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1570 ev->id_index.header.size = sz;
1571 ev->id_index.nr = n;
1573 evlist__for_each_entry(evlist, evsel) {
1574 u32 j;
1576 for (j = 0; j < evsel->core.ids; j++) {
1577 struct id_index_entry *e;
1578 struct perf_sample_id *sid;
1580 if (i >= n) {
1581 err = process(tool, ev, NULL, machine);
1582 if (err)
1583 goto out_err;
1584 nr -= n;
1585 i = 0;
1588 e = &ev->id_index.entries[i++];
1590 e->id = evsel->core.id[j];
1592 sid = perf_evlist__id2sid(evlist, e->id);
1593 if (!sid) {
1594 free(ev);
1595 return -ENOENT;
1598 e->idx = sid->idx;
1599 e->cpu = sid->cpu;
1600 e->tid = sid->tid;
1604 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1605 ev->id_index.header.size = sz;
1606 ev->id_index.nr = nr;
1608 err = process(tool, ev, NULL, machine);
1609 out_err:
1610 free(ev);
1612 return err;
1615 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1616 struct target *target, struct perf_thread_map *threads,
1617 perf_event__handler_t process, bool data_mmap,
1618 unsigned int nr_threads_synthesize)
1620 if (target__has_task(target))
1621 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1622 else if (target__has_cpu(target))
1623 return perf_event__synthesize_threads(tool, process,
1624 machine, data_mmap,
1625 nr_threads_synthesize);
1626 /* command specified */
1627 return 0;
1630 int machine__synthesize_threads(struct machine *machine, struct target *target,
1631 struct perf_thread_map *threads, bool data_mmap,
1632 unsigned int nr_threads_synthesize)
1634 return __machine__synthesize_threads(machine, NULL, target, threads,
1635 perf_event__process, data_mmap,
1636 nr_threads_synthesize);
1639 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1641 struct perf_record_event_update *ev;
1643 size += sizeof(*ev);
1644 size = PERF_ALIGN(size, sizeof(u64));
1646 ev = zalloc(size);
1647 if (ev) {
1648 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1649 ev->header.size = (u16)size;
1650 ev->type = type;
1651 ev->id = id;
1653 return ev;
1656 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1657 perf_event__handler_t process)
1659 size_t size = strlen(evsel->unit);
1660 struct perf_record_event_update *ev;
1661 int err;
1663 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1664 if (ev == NULL)
1665 return -ENOMEM;
1667 strlcpy(ev->data, evsel->unit, size + 1);
1668 err = process(tool, (union perf_event *)ev, NULL, NULL);
1669 free(ev);
1670 return err;
1673 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1674 perf_event__handler_t process)
1676 struct perf_record_event_update *ev;
1677 struct perf_record_event_update_scale *ev_data;
1678 int err;
1680 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1681 if (ev == NULL)
1682 return -ENOMEM;
1684 ev_data = (struct perf_record_event_update_scale *)ev->data;
1685 ev_data->scale = evsel->scale;
1686 err = process(tool, (union perf_event *)ev, NULL, NULL);
1687 free(ev);
1688 return err;
1691 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1692 perf_event__handler_t process)
1694 struct perf_record_event_update *ev;
1695 size_t len = strlen(evsel->name);
1696 int err;
1698 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1699 if (ev == NULL)
1700 return -ENOMEM;
1702 strlcpy(ev->data, evsel->name, len + 1);
1703 err = process(tool, (union perf_event *)ev, NULL, NULL);
1704 free(ev);
1705 return err;
1708 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1709 perf_event__handler_t process)
1711 size_t size = sizeof(struct perf_record_event_update);
1712 struct perf_record_event_update *ev;
1713 int max, err;
1714 u16 type;
1716 if (!evsel->core.own_cpus)
1717 return 0;
1719 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1720 if (!ev)
1721 return -ENOMEM;
1723 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1724 ev->header.size = (u16)size;
1725 ev->type = PERF_EVENT_UPDATE__CPUS;
1726 ev->id = evsel->core.id[0];
1728 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1729 evsel->core.own_cpus, type, max);
1731 err = process(tool, (union perf_event *)ev, NULL, NULL);
1732 free(ev);
1733 return err;
1736 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1737 perf_event__handler_t process)
1739 struct evsel *evsel;
1740 int err = 0;
1742 evlist__for_each_entry(evlist, evsel) {
1743 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1744 evsel->core.id, process);
1745 if (err) {
1746 pr_debug("failed to create perf header attribute\n");
1747 return err;
1751 return err;
1754 static bool has_unit(struct evsel *evsel)
1756 return evsel->unit && *evsel->unit;
1759 static bool has_scale(struct evsel *evsel)
1761 return evsel->scale != 1;
1764 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1765 perf_event__handler_t process, bool is_pipe)
1767 struct evsel *evsel;
1768 int err;
1771 * Synthesize other events stuff not carried within
1772 * attr event - unit, scale, name
1774 evlist__for_each_entry(evsel_list, evsel) {
1775 if (!evsel->supported)
1776 continue;
1779 * Synthesize unit and scale only if it's defined.
1781 if (has_unit(evsel)) {
1782 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1783 if (err < 0) {
1784 pr_err("Couldn't synthesize evsel unit.\n");
1785 return err;
1789 if (has_scale(evsel)) {
1790 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1791 if (err < 0) {
1792 pr_err("Couldn't synthesize evsel evsel.\n");
1793 return err;
1797 if (evsel->core.own_cpus) {
1798 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1799 if (err < 0) {
1800 pr_err("Couldn't synthesize evsel cpus.\n");
1801 return err;
1806 * Name is needed only for pipe output,
1807 * perf.data carries event names.
1809 if (is_pipe) {
1810 err = perf_event__synthesize_event_update_name(tool, evsel, process);
1811 if (err < 0) {
1812 pr_err("Couldn't synthesize evsel name.\n");
1813 return err;
1817 return 0;
1820 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
1821 u32 ids, u64 *id, perf_event__handler_t process)
1823 union perf_event *ev;
1824 size_t size;
1825 int err;
1827 size = sizeof(struct perf_event_attr);
1828 size = PERF_ALIGN(size, sizeof(u64));
1829 size += sizeof(struct perf_event_header);
1830 size += ids * sizeof(u64);
1832 ev = zalloc(size);
1834 if (ev == NULL)
1835 return -ENOMEM;
1837 ev->attr.attr = *attr;
1838 memcpy(ev->attr.id, id, ids * sizeof(u64));
1840 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1841 ev->attr.header.size = (u16)size;
1843 if (ev->attr.header.size == size)
1844 err = process(tool, ev, NULL, NULL);
1845 else
1846 err = -E2BIG;
1848 free(ev);
1850 return err;
1853 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
1854 perf_event__handler_t process)
1856 union perf_event ev;
1857 struct tracing_data *tdata;
1858 ssize_t size = 0, aligned_size = 0, padding;
1859 struct feat_fd ff;
1862 * We are going to store the size of the data followed
1863 * by the data contents. Since the fd descriptor is a pipe,
1864 * we cannot seek back to store the size of the data once
1865 * we know it. Instead we:
1867 * - write the tracing data to the temp file
1868 * - get/write the data size to pipe
1869 * - write the tracing data from the temp file
1870 * to the pipe
1872 tdata = tracing_data_get(&evlist->core.entries, fd, true);
1873 if (!tdata)
1874 return -1;
1876 memset(&ev, 0, sizeof(ev));
1878 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
1879 size = tdata->size;
1880 aligned_size = PERF_ALIGN(size, sizeof(u64));
1881 padding = aligned_size - size;
1882 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1883 ev.tracing_data.size = aligned_size;
1885 process(tool, &ev, NULL, NULL);
1888 * The put function will copy all the tracing data
1889 * stored in temp file to the pipe.
1891 tracing_data_put(tdata);
1893 ff = (struct feat_fd){ .fd = fd };
1894 if (write_padded(&ff, NULL, 0, padding))
1895 return -1;
1897 return aligned_size;
1900 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
1901 perf_event__handler_t process, struct machine *machine)
1903 union perf_event ev;
1904 size_t len;
1906 if (!pos->hit)
1907 return 0;
1909 memset(&ev, 0, sizeof(ev));
1911 len = pos->long_name_len + 1;
1912 len = PERF_ALIGN(len, NAME_ALIGN);
1913 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1914 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1915 ev.build_id.header.misc = misc;
1916 ev.build_id.pid = machine->pid;
1917 ev.build_id.header.size = sizeof(ev.build_id) + len;
1918 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1920 return process(tool, &ev, NULL, machine);
1923 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
1924 struct evlist *evlist, perf_event__handler_t process, bool attrs)
1926 int err;
1928 if (attrs) {
1929 err = perf_event__synthesize_attrs(tool, evlist, process);
1930 if (err < 0) {
1931 pr_err("Couldn't synthesize attrs.\n");
1932 return err;
1936 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
1937 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
1938 if (err < 0) {
1939 pr_err("Couldn't synthesize thread map.\n");
1940 return err;
1943 err = perf_event__synthesize_cpu_map(tool, evlist->core.cpus, process, NULL);
1944 if (err < 0) {
1945 pr_err("Couldn't synthesize thread map.\n");
1946 return err;
1949 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
1950 if (err < 0) {
1951 pr_err("Couldn't synthesize config.\n");
1952 return err;
1955 return 0;
1958 int __weak perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
1959 struct perf_tool *tool __maybe_unused,
1960 perf_event__handler_t process __maybe_unused,
1961 struct machine *machine __maybe_unused)
1963 return 0;
1966 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
1968 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
1969 struct evlist *evlist, perf_event__handler_t process)
1971 struct perf_header *header = &session->header;
1972 struct perf_record_header_feature *fe;
1973 struct feat_fd ff;
1974 size_t sz, sz_hdr;
1975 int feat, ret;
1977 sz_hdr = sizeof(fe->header);
1978 sz = sizeof(union perf_event);
1979 /* get a nice alignment */
1980 sz = PERF_ALIGN(sz, page_size);
1982 memset(&ff, 0, sizeof(ff));
1984 ff.buf = malloc(sz);
1985 if (!ff.buf)
1986 return -ENOMEM;
1988 ff.size = sz - sz_hdr;
1989 ff.ph = &session->header;
1991 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1992 if (!feat_ops[feat].synthesize) {
1993 pr_debug("No record header feature for header :%d\n", feat);
1994 continue;
1997 ff.offset = sizeof(*fe);
1999 ret = feat_ops[feat].write(&ff, evlist);
2000 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2001 pr_debug("Error writing feature\n");
2002 continue;
2004 /* ff.buf may have changed due to realloc in do_write() */
2005 fe = ff.buf;
2006 memset(fe, 0, sizeof(*fe));
2008 fe->feat_id = feat;
2009 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2010 fe->header.size = ff.offset;
2012 ret = process(tool, ff.buf, NULL, NULL);
2013 if (ret) {
2014 free(ff.buf);
2015 return ret;
2019 /* Send HEADER_LAST_FEATURE mark. */
2020 fe = ff.buf;
2021 fe->feat_id = HEADER_LAST_FEATURE;
2022 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2023 fe->header.size = sizeof(*fe);
2025 ret = process(tool, ff.buf, NULL, NULL);
2027 free(ff.buf);
2028 return ret;