aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/event.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/event.c')
-rw-r--r--tools/perf/util/event.c1109
1 files changed, 3 insertions, 1106 deletions
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index f4afbb858ebb..fc1e5a991008 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -1,16 +1,16 @@
1#include <dirent.h>
2#include <errno.h> 1#include <errno.h>
3#include <fcntl.h> 2#include <fcntl.h>
4#include <inttypes.h> 3#include <inttypes.h>
5#include <linux/kernel.h> 4#include <linux/kernel.h>
6#include <linux/types.h> 5#include <linux/types.h>
6#include <perf/cpumap.h>
7#include <sys/types.h> 7#include <sys/types.h>
8#include <sys/stat.h> 8#include <sys/stat.h>
9#include <unistd.h> 9#include <unistd.h>
10#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ 10#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
11#include <api/fs/fs.h>
12#include <linux/perf_event.h> 11#include <linux/perf_event.h>
13#include <linux/zalloc.h> 12#include <linux/zalloc.h>
13#include "cpumap.h"
14#include "dso.h" 14#include "dso.h"
15#include "event.h" 15#include "event.h"
16#include "debug.h" 16#include "debug.h"
@@ -24,6 +24,7 @@
24#include "time-utils.h" 24#include "time-utils.h"
25#include <linux/ctype.h> 25#include <linux/ctype.h>
26#include "map.h" 26#include "map.h"
27#include "util/namespaces.h"
27#include "symbol.h" 28#include "symbol.h"
28#include "symbol/kallsyms.h" 29#include "symbol/kallsyms.h"
29#include "asm/bug.h" 30#include "asm/bug.h"
@@ -33,8 +34,6 @@
33#include "tool.h" 34#include "tool.h"
34#include "../perf.h" 35#include "../perf.h"
35 36
36#define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
37
38static const char *perf_event__names[] = { 37static const char *perf_event__names[] = {
39 [0] = "TOTAL", 38 [0] = "TOTAL",
40 [PERF_RECORD_MMAP] = "MMAP", 39 [PERF_RECORD_MMAP] = "MMAP",
@@ -75,18 +74,6 @@ static const char *perf_event__names[] = {
75 [PERF_RECORD_COMPRESSED] = "COMPRESSED", 74 [PERF_RECORD_COMPRESSED] = "COMPRESSED",
76}; 75};
77 76
78static const char *perf_ns__names[] = {
79 [NET_NS_INDEX] = "net",
80 [UTS_NS_INDEX] = "uts",
81 [IPC_NS_INDEX] = "ipc",
82 [PID_NS_INDEX] = "pid",
83 [USER_NS_INDEX] = "user",
84 [MNT_NS_INDEX] = "mnt",
85 [CGROUP_NS_INDEX] = "cgroup",
86};
87
88unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
89
90const char *perf_event__name(unsigned int id) 77const char *perf_event__name(unsigned int id)
91{ 78{
92 if (id >= ARRAY_SIZE(perf_event__names)) 79 if (id >= ARRAY_SIZE(perf_event__names))
@@ -96,775 +83,6 @@ const char *perf_event__name(unsigned int id)
96 return perf_event__names[id]; 83 return perf_event__names[id];
97} 84}
98 85
99static const char *perf_ns__name(unsigned int id)
100{
101 if (id >= ARRAY_SIZE(perf_ns__names))
102 return "UNKNOWN";
103 return perf_ns__names[id];
104}
105
106int perf_tool__process_synth_event(struct perf_tool *tool,
107 union perf_event *event,
108 struct machine *machine,
109 perf_event__handler_t process)
110{
111 struct perf_sample synth_sample = {
112 .pid = -1,
113 .tid = -1,
114 .time = -1,
115 .stream_id = -1,
116 .cpu = -1,
117 .period = 1,
118 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
119 };
120
121 return process(tool, event, &synth_sample, machine);
122};
123
124/*
125 * Assumes that the first 4095 bytes of /proc/pid/stat contains
126 * the comm, tgid and ppid.
127 */
128static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
129 pid_t *tgid, pid_t *ppid)
130{
131 char filename[PATH_MAX];
132 char bf[4096];
133 int fd;
134 size_t size = 0;
135 ssize_t n;
136 char *name, *tgids, *ppids;
137
138 *tgid = -1;
139 *ppid = -1;
140
141 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
142
143 fd = open(filename, O_RDONLY);
144 if (fd < 0) {
145 pr_debug("couldn't open %s\n", filename);
146 return -1;
147 }
148
149 n = read(fd, bf, sizeof(bf) - 1);
150 close(fd);
151 if (n <= 0) {
152 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
153 pid);
154 return -1;
155 }
156 bf[n] = '\0';
157
158 name = strstr(bf, "Name:");
159 tgids = strstr(bf, "Tgid:");
160 ppids = strstr(bf, "PPid:");
161
162 if (name) {
163 char *nl;
164
165 name = skip_spaces(name + 5); /* strlen("Name:") */
166 nl = strchr(name, '\n');
167 if (nl)
168 *nl = '\0';
169
170 size = strlen(name);
171 if (size >= len)
172 size = len - 1;
173 memcpy(comm, name, size);
174 comm[size] = '\0';
175 } else {
176 pr_debug("Name: string not found for pid %d\n", pid);
177 }
178
179 if (tgids) {
180 tgids += 5; /* strlen("Tgid:") */
181 *tgid = atoi(tgids);
182 } else {
183 pr_debug("Tgid: string not found for pid %d\n", pid);
184 }
185
186 if (ppids) {
187 ppids += 5; /* strlen("PPid:") */
188 *ppid = atoi(ppids);
189 } else {
190 pr_debug("PPid: string not found for pid %d\n", pid);
191 }
192
193 return 0;
194}
195
196static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
197 struct machine *machine,
198 pid_t *tgid, pid_t *ppid)
199{
200 size_t size;
201
202 *ppid = -1;
203
204 memset(&event->comm, 0, sizeof(event->comm));
205
206 if (machine__is_host(machine)) {
207 if (perf_event__get_comm_ids(pid, event->comm.comm,
208 sizeof(event->comm.comm),
209 tgid, ppid) != 0) {
210 return -1;
211 }
212 } else {
213 *tgid = machine->pid;
214 }
215
216 if (*tgid < 0)
217 return -1;
218
219 event->comm.pid = *tgid;
220 event->comm.header.type = PERF_RECORD_COMM;
221
222 size = strlen(event->comm.comm) + 1;
223 size = PERF_ALIGN(size, sizeof(u64));
224 memset(event->comm.comm + size, 0, machine->id_hdr_size);
225 event->comm.header.size = (sizeof(event->comm) -
226 (sizeof(event->comm.comm) - size) +
227 machine->id_hdr_size);
228 event->comm.tid = pid;
229
230 return 0;
231}
232
233pid_t perf_event__synthesize_comm(struct perf_tool *tool,
234 union perf_event *event, pid_t pid,
235 perf_event__handler_t process,
236 struct machine *machine)
237{
238 pid_t tgid, ppid;
239
240 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
241 return -1;
242
243 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
244 return -1;
245
246 return tgid;
247}
248
249static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
250 struct perf_ns_link_info *ns_link_info)
251{
252 struct stat64 st;
253 char proc_ns[128];
254
255 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
256 if (stat64(proc_ns, &st) == 0) {
257 ns_link_info->dev = st.st_dev;
258 ns_link_info->ino = st.st_ino;
259 }
260}
261
262int perf_event__synthesize_namespaces(struct perf_tool *tool,
263 union perf_event *event,
264 pid_t pid, pid_t tgid,
265 perf_event__handler_t process,
266 struct machine *machine)
267{
268 u32 idx;
269 struct perf_ns_link_info *ns_link_info;
270
271 if (!tool || !tool->namespace_events)
272 return 0;
273
274 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
275 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
276 machine->id_hdr_size));
277
278 event->namespaces.pid = tgid;
279 event->namespaces.tid = pid;
280
281 event->namespaces.nr_namespaces = NR_NAMESPACES;
282
283 ns_link_info = event->namespaces.link_info;
284
285 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
286 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
287 &ns_link_info[idx]);
288
289 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
290
291 event->namespaces.header.size = (sizeof(event->namespaces) +
292 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
293 machine->id_hdr_size);
294
295 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
296 return -1;
297
298 return 0;
299}
300
301static int perf_event__synthesize_fork(struct perf_tool *tool,
302 union perf_event *event,
303 pid_t pid, pid_t tgid, pid_t ppid,
304 perf_event__handler_t process,
305 struct machine *machine)
306{
307 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
308
309 /*
310 * for main thread set parent to ppid from status file. For other
311 * threads set parent pid to main thread. ie., assume main thread
312 * spawns all threads in a process
313 */
314 if (tgid == pid) {
315 event->fork.ppid = ppid;
316 event->fork.ptid = ppid;
317 } else {
318 event->fork.ppid = tgid;
319 event->fork.ptid = tgid;
320 }
321 event->fork.pid = tgid;
322 event->fork.tid = pid;
323 event->fork.header.type = PERF_RECORD_FORK;
324 event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
325
326 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
327
328 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
329 return -1;
330
331 return 0;
332}
333
334int perf_event__synthesize_mmap_events(struct perf_tool *tool,
335 union perf_event *event,
336 pid_t pid, pid_t tgid,
337 perf_event__handler_t process,
338 struct machine *machine,
339 bool mmap_data)
340{
341 char filename[PATH_MAX];
342 FILE *fp;
343 unsigned long long t;
344 bool truncation = false;
345 unsigned long long timeout = proc_map_timeout * 1000000ULL;
346 int rc = 0;
347 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
348 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
349
350 if (machine__is_default_guest(machine))
351 return 0;
352
353 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
354 machine->root_dir, pid, pid);
355
356 fp = fopen(filename, "r");
357 if (fp == NULL) {
358 /*
359 * We raced with a task exiting - just return:
360 */
361 pr_debug("couldn't open %s\n", filename);
362 return -1;
363 }
364
365 event->header.type = PERF_RECORD_MMAP2;
366 t = rdclock();
367
368 while (1) {
369 char bf[BUFSIZ];
370 char prot[5];
371 char execname[PATH_MAX];
372 char anonstr[] = "//anon";
373 unsigned int ino;
374 size_t size;
375 ssize_t n;
376
377 if (fgets(bf, sizeof(bf), fp) == NULL)
378 break;
379
380 if ((rdclock() - t) > timeout) {
381 pr_warning("Reading %s time out. "
382 "You may want to increase "
383 "the time limit by --proc-map-timeout\n",
384 filename);
385 truncation = true;
386 goto out;
387 }
388
389 /* ensure null termination since stack will be reused. */
390 strcpy(execname, "");
391
392 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
393 n = sscanf(bf, "%"PRI_lx64"-%"PRI_lx64" %s %"PRI_lx64" %x:%x %u %[^\n]\n",
394 &event->mmap2.start, &event->mmap2.len, prot,
395 &event->mmap2.pgoff, &event->mmap2.maj,
396 &event->mmap2.min,
397 &ino, execname);
398
399 /*
400 * Anon maps don't have the execname.
401 */
402 if (n < 7)
403 continue;
404
405 event->mmap2.ino = (u64)ino;
406
407 /*
408 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
409 */
410 if (machine__is_host(machine))
411 event->header.misc = PERF_RECORD_MISC_USER;
412 else
413 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
414
415 /* map protection and flags bits */
416 event->mmap2.prot = 0;
417 event->mmap2.flags = 0;
418 if (prot[0] == 'r')
419 event->mmap2.prot |= PROT_READ;
420 if (prot[1] == 'w')
421 event->mmap2.prot |= PROT_WRITE;
422 if (prot[2] == 'x')
423 event->mmap2.prot |= PROT_EXEC;
424
425 if (prot[3] == 's')
426 event->mmap2.flags |= MAP_SHARED;
427 else
428 event->mmap2.flags |= MAP_PRIVATE;
429
430 if (prot[2] != 'x') {
431 if (!mmap_data || prot[0] != 'r')
432 continue;
433
434 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
435 }
436
437out:
438 if (truncation)
439 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
440
441 if (!strcmp(execname, ""))
442 strcpy(execname, anonstr);
443
444 if (hugetlbfs_mnt_len &&
445 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
446 strcpy(execname, anonstr);
447 event->mmap2.flags |= MAP_HUGETLB;
448 }
449
450 size = strlen(execname) + 1;
451 memcpy(event->mmap2.filename, execname, size);
452 size = PERF_ALIGN(size, sizeof(u64));
453 event->mmap2.len -= event->mmap.start;
454 event->mmap2.header.size = (sizeof(event->mmap2) -
455 (sizeof(event->mmap2.filename) - size));
456 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
457 event->mmap2.header.size += machine->id_hdr_size;
458 event->mmap2.pid = tgid;
459 event->mmap2.tid = pid;
460
461 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
462 rc = -1;
463 break;
464 }
465
466 if (truncation)
467 break;
468 }
469
470 fclose(fp);
471 return rc;
472}
473
474int perf_event__synthesize_modules(struct perf_tool *tool,
475 perf_event__handler_t process,
476 struct machine *machine)
477{
478 int rc = 0;
479 struct map *pos;
480 struct maps *maps = machine__kernel_maps(machine);
481 union perf_event *event = zalloc((sizeof(event->mmap) +
482 machine->id_hdr_size));
483 if (event == NULL) {
484 pr_debug("Not enough memory synthesizing mmap event "
485 "for kernel modules\n");
486 return -1;
487 }
488
489 event->header.type = PERF_RECORD_MMAP;
490
491 /*
492 * kernel uses 0 for user space maps, see kernel/perf_event.c
493 * __perf_event_mmap
494 */
495 if (machine__is_host(machine))
496 event->header.misc = PERF_RECORD_MISC_KERNEL;
497 else
498 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
499
500 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
501 size_t size;
502
503 if (!__map__is_kmodule(pos))
504 continue;
505
506 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
507 event->mmap.header.type = PERF_RECORD_MMAP;
508 event->mmap.header.size = (sizeof(event->mmap) -
509 (sizeof(event->mmap.filename) - size));
510 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
511 event->mmap.header.size += machine->id_hdr_size;
512 event->mmap.start = pos->start;
513 event->mmap.len = pos->end - pos->start;
514 event->mmap.pid = machine->pid;
515
516 memcpy(event->mmap.filename, pos->dso->long_name,
517 pos->dso->long_name_len + 1);
518 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
519 rc = -1;
520 break;
521 }
522 }
523
524 free(event);
525 return rc;
526}
527
528static int __event__synthesize_thread(union perf_event *comm_event,
529 union perf_event *mmap_event,
530 union perf_event *fork_event,
531 union perf_event *namespaces_event,
532 pid_t pid, int full,
533 perf_event__handler_t process,
534 struct perf_tool *tool,
535 struct machine *machine,
536 bool mmap_data)
537{
538 char filename[PATH_MAX];
539 DIR *tasks;
540 struct dirent *dirent;
541 pid_t tgid, ppid;
542 int rc = 0;
543
544 /* special case: only send one comm event using passed in pid */
545 if (!full) {
546 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
547 process, machine);
548
549 if (tgid == -1)
550 return -1;
551
552 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
553 tgid, process, machine) < 0)
554 return -1;
555
556 /*
557 * send mmap only for thread group leader
558 * see thread__init_map_groups
559 */
560 if (pid == tgid &&
561 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
562 process, machine, mmap_data))
563 return -1;
564
565 return 0;
566 }
567
568 if (machine__is_default_guest(machine))
569 return 0;
570
571 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
572 machine->root_dir, pid);
573
574 tasks = opendir(filename);
575 if (tasks == NULL) {
576 pr_debug("couldn't open %s\n", filename);
577 return 0;
578 }
579
580 while ((dirent = readdir(tasks)) != NULL) {
581 char *end;
582 pid_t _pid;
583
584 _pid = strtol(dirent->d_name, &end, 10);
585 if (*end)
586 continue;
587
588 rc = -1;
589 if (perf_event__prepare_comm(comm_event, _pid, machine,
590 &tgid, &ppid) != 0)
591 break;
592
593 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
594 ppid, process, machine) < 0)
595 break;
596
597 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
598 tgid, process, machine) < 0)
599 break;
600
601 /*
602 * Send the prepared comm event
603 */
604 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
605 break;
606
607 rc = 0;
608 if (_pid == pid) {
609 /* process the parent's maps too */
610 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
611 process, machine, mmap_data);
612 if (rc)
613 break;
614 }
615 }
616
617 closedir(tasks);
618 return rc;
619}
620
621int perf_event__synthesize_thread_map(struct perf_tool *tool,
622 struct perf_thread_map *threads,
623 perf_event__handler_t process,
624 struct machine *machine,
625 bool mmap_data)
626{
627 union perf_event *comm_event, *mmap_event, *fork_event;
628 union perf_event *namespaces_event;
629 int err = -1, thread, j;
630
631 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
632 if (comm_event == NULL)
633 goto out;
634
635 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
636 if (mmap_event == NULL)
637 goto out_free_comm;
638
639 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
640 if (fork_event == NULL)
641 goto out_free_mmap;
642
643 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
644 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
645 machine->id_hdr_size);
646 if (namespaces_event == NULL)
647 goto out_free_fork;
648
649 err = 0;
650 for (thread = 0; thread < threads->nr; ++thread) {
651 if (__event__synthesize_thread(comm_event, mmap_event,
652 fork_event, namespaces_event,
653 perf_thread_map__pid(threads, thread), 0,
654 process, tool, machine,
655 mmap_data)) {
656 err = -1;
657 break;
658 }
659
660 /*
661 * comm.pid is set to thread group id by
662 * perf_event__synthesize_comm
663 */
664 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
665 bool need_leader = true;
666
667 /* is thread group leader in thread_map? */
668 for (j = 0; j < threads->nr; ++j) {
669 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
670 need_leader = false;
671 break;
672 }
673 }
674
675 /* if not, generate events for it */
676 if (need_leader &&
677 __event__synthesize_thread(comm_event, mmap_event,
678 fork_event, namespaces_event,
679 comm_event->comm.pid, 0,
680 process, tool, machine,
681 mmap_data)) {
682 err = -1;
683 break;
684 }
685 }
686 }
687 free(namespaces_event);
688out_free_fork:
689 free(fork_event);
690out_free_mmap:
691 free(mmap_event);
692out_free_comm:
693 free(comm_event);
694out:
695 return err;
696}
697
698static int __perf_event__synthesize_threads(struct perf_tool *tool,
699 perf_event__handler_t process,
700 struct machine *machine,
701 bool mmap_data,
702 struct dirent **dirent,
703 int start,
704 int num)
705{
706 union perf_event *comm_event, *mmap_event, *fork_event;
707 union perf_event *namespaces_event;
708 int err = -1;
709 char *end;
710 pid_t pid;
711 int i;
712
713 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
714 if (comm_event == NULL)
715 goto out;
716
717 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
718 if (mmap_event == NULL)
719 goto out_free_comm;
720
721 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
722 if (fork_event == NULL)
723 goto out_free_mmap;
724
725 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
726 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
727 machine->id_hdr_size);
728 if (namespaces_event == NULL)
729 goto out_free_fork;
730
731 for (i = start; i < start + num; i++) {
732 if (!isdigit(dirent[i]->d_name[0]))
733 continue;
734
735 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
736 /* only interested in proper numerical dirents */
737 if (*end)
738 continue;
739 /*
740 * We may race with exiting thread, so don't stop just because
741 * one thread couldn't be synthesized.
742 */
743 __event__synthesize_thread(comm_event, mmap_event, fork_event,
744 namespaces_event, pid, 1, process,
745 tool, machine, mmap_data);
746 }
747 err = 0;
748
749 free(namespaces_event);
750out_free_fork:
751 free(fork_event);
752out_free_mmap:
753 free(mmap_event);
754out_free_comm:
755 free(comm_event);
756out:
757 return err;
758}
759
760struct synthesize_threads_arg {
761 struct perf_tool *tool;
762 perf_event__handler_t process;
763 struct machine *machine;
764 bool mmap_data;
765 struct dirent **dirent;
766 int num;
767 int start;
768};
769
770static void *synthesize_threads_worker(void *arg)
771{
772 struct synthesize_threads_arg *args = arg;
773
774 __perf_event__synthesize_threads(args->tool, args->process,
775 args->machine, args->mmap_data,
776 args->dirent,
777 args->start, args->num);
778 return NULL;
779}
780
781int perf_event__synthesize_threads(struct perf_tool *tool,
782 perf_event__handler_t process,
783 struct machine *machine,
784 bool mmap_data,
785 unsigned int nr_threads_synthesize)
786{
787 struct synthesize_threads_arg *args = NULL;
788 pthread_t *synthesize_threads = NULL;
789 char proc_path[PATH_MAX];
790 struct dirent **dirent;
791 int num_per_thread;
792 int m, n, i, j;
793 int thread_nr;
794 int base = 0;
795 int err = -1;
796
797
798 if (machine__is_default_guest(machine))
799 return 0;
800
801 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
802 n = scandir(proc_path, &dirent, 0, alphasort);
803 if (n < 0)
804 return err;
805
806 if (nr_threads_synthesize == UINT_MAX)
807 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
808 else
809 thread_nr = nr_threads_synthesize;
810
811 if (thread_nr <= 1) {
812 err = __perf_event__synthesize_threads(tool, process,
813 machine, mmap_data,
814 dirent, base, n);
815 goto free_dirent;
816 }
817 if (thread_nr > n)
818 thread_nr = n;
819
820 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
821 if (synthesize_threads == NULL)
822 goto free_dirent;
823
824 args = calloc(sizeof(*args), thread_nr);
825 if (args == NULL)
826 goto free_threads;
827
828 num_per_thread = n / thread_nr;
829 m = n % thread_nr;
830 for (i = 0; i < thread_nr; i++) {
831 args[i].tool = tool;
832 args[i].process = process;
833 args[i].machine = machine;
834 args[i].mmap_data = mmap_data;
835 args[i].dirent = dirent;
836 }
837 for (i = 0; i < m; i++) {
838 args[i].num = num_per_thread + 1;
839 args[i].start = i * args[i].num;
840 }
841 if (i != 0)
842 base = args[i-1].start + args[i-1].num;
843 for (j = i; j < thread_nr; j++) {
844 args[j].num = num_per_thread;
845 args[j].start = base + (j - i) * args[i].num;
846 }
847
848 for (i = 0; i < thread_nr; i++) {
849 if (pthread_create(&synthesize_threads[i], NULL,
850 synthesize_threads_worker, &args[i]))
851 goto out_join;
852 }
853 err = 0;
854out_join:
855 for (i = 0; i < thread_nr; i++)
856 pthread_join(synthesize_threads[i], NULL);
857 free(args);
858free_threads:
859 free(synthesize_threads);
860free_dirent:
861 for (i = 0; i < n; i++)
862 zfree(&dirent[i]);
863 free(dirent);
864
865 return err;
866}
867
868struct process_symbol_args { 86struct process_symbol_args {
869 const char *name; 87 const char *name;
870 u64 start; 88 u64 start;
@@ -899,327 +117,6 @@ int kallsyms__get_function_start(const char *kallsyms_filename,
899 return 0; 117 return 0;
900} 118}
901 119
902int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
903 perf_event__handler_t process __maybe_unused,
904 struct machine *machine __maybe_unused)
905{
906 return 0;
907}
908
909static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
910 perf_event__handler_t process,
911 struct machine *machine)
912{
913 size_t size;
914 struct map *map = machine__kernel_map(machine);
915 struct kmap *kmap;
916 int err;
917 union perf_event *event;
918
919 if (map == NULL)
920 return -1;
921
922 kmap = map__kmap(map);
923 if (!kmap->ref_reloc_sym)
924 return -1;
925
926 /*
927 * We should get this from /sys/kernel/sections/.text, but till that is
928 * available use this, and after it is use this as a fallback for older
929 * kernels.
930 */
931 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
932 if (event == NULL) {
933 pr_debug("Not enough memory synthesizing mmap event "
934 "for kernel modules\n");
935 return -1;
936 }
937
938 if (machine__is_host(machine)) {
939 /*
940 * kernel uses PERF_RECORD_MISC_USER for user space maps,
941 * see kernel/perf_event.c __perf_event_mmap
942 */
943 event->header.misc = PERF_RECORD_MISC_KERNEL;
944 } else {
945 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
946 }
947
948 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
949 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
950 size = PERF_ALIGN(size, sizeof(u64));
951 event->mmap.header.type = PERF_RECORD_MMAP;
952 event->mmap.header.size = (sizeof(event->mmap) -
953 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
954 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
955 event->mmap.start = map->start;
956 event->mmap.len = map->end - event->mmap.start;
957 event->mmap.pid = machine->pid;
958
959 err = perf_tool__process_synth_event(tool, event, machine, process);
960 free(event);
961
962 return err;
963}
964
965int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
966 perf_event__handler_t process,
967 struct machine *machine)
968{
969 int err;
970
971 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
972 if (err < 0)
973 return err;
974
975 return perf_event__synthesize_extra_kmaps(tool, process, machine);
976}
977
978int perf_event__synthesize_thread_map2(struct perf_tool *tool,
979 struct perf_thread_map *threads,
980 perf_event__handler_t process,
981 struct machine *machine)
982{
983 union perf_event *event;
984 int i, err, size;
985
986 size = sizeof(event->thread_map);
987 size += threads->nr * sizeof(event->thread_map.entries[0]);
988
989 event = zalloc(size);
990 if (!event)
991 return -ENOMEM;
992
993 event->header.type = PERF_RECORD_THREAD_MAP;
994 event->header.size = size;
995 event->thread_map.nr = threads->nr;
996
997 for (i = 0; i < threads->nr; i++) {
998 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
999 char *comm = perf_thread_map__comm(threads, i);
1000
1001 if (!comm)
1002 comm = (char *) "";
1003
1004 entry->pid = perf_thread_map__pid(threads, i);
1005 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1006 }
1007
1008 err = process(tool, event, NULL, machine);
1009
1010 free(event);
1011 return err;
1012}
1013
1014static void synthesize_cpus(struct cpu_map_entries *cpus,
1015 struct perf_cpu_map *map)
1016{
1017 int i;
1018
1019 cpus->nr = map->nr;
1020
1021 for (i = 0; i < map->nr; i++)
1022 cpus->cpu[i] = map->map[i];
1023}
1024
1025static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1026 struct perf_cpu_map *map, int max)
1027{
1028 int i;
1029
1030 mask->nr = BITS_TO_LONGS(max);
1031 mask->long_size = sizeof(long);
1032
1033 for (i = 0; i < map->nr; i++)
1034 set_bit(map->map[i], mask->mask);
1035}
1036
1037static size_t cpus_size(struct perf_cpu_map *map)
1038{
1039 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1040}
1041
1042static size_t mask_size(struct perf_cpu_map *map, int *max)
1043{
1044 int i;
1045
1046 *max = 0;
1047
1048 for (i = 0; i < map->nr; i++) {
1049 /* bit possition of the cpu is + 1 */
1050 int bit = map->map[i] + 1;
1051
1052 if (bit > *max)
1053 *max = bit;
1054 }
1055
1056 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1057}
1058
1059void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1060{
1061 size_t size_cpus, size_mask;
1062 bool is_dummy = perf_cpu_map__empty(map);
1063
1064 /*
1065 * Both array and mask data have variable size based
1066 * on the number of cpus and their actual values.
1067 * The size of the 'struct perf_record_cpu_map_data' is:
1068 *
1069 * array = size of 'struct cpu_map_entries' +
1070 * number of cpus * sizeof(u64)
1071 *
1072 * mask = size of 'struct perf_record_record_cpu_map' +
1073 * maximum cpu bit converted to size of longs
1074 *
1075 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1076 */
1077 size_cpus = cpus_size(map);
1078 size_mask = mask_size(map, max);
1079
1080 if (is_dummy || (size_cpus < size_mask)) {
1081 *size += size_cpus;
1082 *type = PERF_CPU_MAP__CPUS;
1083 } else {
1084 *size += size_mask;
1085 *type = PERF_CPU_MAP__MASK;
1086 }
1087
1088 *size += sizeof(struct perf_record_cpu_map_data);
1089 *size = PERF_ALIGN(*size, sizeof(u64));
1090 return zalloc(*size);
1091}
1092
1093void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1094 u16 type, int max)
1095{
1096 data->type = type;
1097
1098 switch (type) {
1099 case PERF_CPU_MAP__CPUS:
1100 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1101 break;
1102 case PERF_CPU_MAP__MASK:
1103 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1104 default:
1105 break;
1106 };
1107}
1108
1109static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1110{
1111 size_t size = sizeof(struct perf_record_cpu_map);
1112 struct perf_record_cpu_map *event;
1113 int max;
1114 u16 type;
1115
1116 event = cpu_map_data__alloc(map, &size, &type, &max);
1117 if (!event)
1118 return NULL;
1119
1120 event->header.type = PERF_RECORD_CPU_MAP;
1121 event->header.size = size;
1122 event->data.type = type;
1123
1124 cpu_map_data__synthesize(&event->data, map, type, max);
1125 return event;
1126}
1127
1128int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1129 struct perf_cpu_map *map,
1130 perf_event__handler_t process,
1131 struct machine *machine)
1132{
1133 struct perf_record_cpu_map *event;
1134 int err;
1135
1136 event = cpu_map_event__new(map);
1137 if (!event)
1138 return -ENOMEM;
1139
1140 err = process(tool, (union perf_event *) event, NULL, machine);
1141
1142 free(event);
1143 return err;
1144}
1145
1146int perf_event__synthesize_stat_config(struct perf_tool *tool,
1147 struct perf_stat_config *config,
1148 perf_event__handler_t process,
1149 struct machine *machine)
1150{
1151 struct perf_record_stat_config *event;
1152 int size, i = 0, err;
1153
1154 size = sizeof(*event);
1155 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1156
1157 event = zalloc(size);
1158 if (!event)
1159 return -ENOMEM;
1160
1161 event->header.type = PERF_RECORD_STAT_CONFIG;
1162 event->header.size = size;
1163 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1164
1165#define ADD(__term, __val) \
1166 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1167 event->data[i].val = __val; \
1168 i++;
1169
1170 ADD(AGGR_MODE, config->aggr_mode)
1171 ADD(INTERVAL, config->interval)
1172 ADD(SCALE, config->scale)
1173
1174 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1175 "stat config terms unbalanced\n");
1176#undef ADD
1177
1178 err = process(tool, (union perf_event *) event, NULL, machine);
1179
1180 free(event);
1181 return err;
1182}
1183
1184int perf_event__synthesize_stat(struct perf_tool *tool,
1185 u32 cpu, u32 thread, u64 id,
1186 struct perf_counts_values *count,
1187 perf_event__handler_t process,
1188 struct machine *machine)
1189{
1190 struct perf_record_stat event;
1191
1192 event.header.type = PERF_RECORD_STAT;
1193 event.header.size = sizeof(event);
1194 event.header.misc = 0;
1195
1196 event.id = id;
1197 event.cpu = cpu;
1198 event.thread = thread;
1199 event.val = count->val;
1200 event.ena = count->ena;
1201 event.run = count->run;
1202
1203 return process(tool, (union perf_event *) &event, NULL, machine);
1204}
1205
1206int perf_event__synthesize_stat_round(struct perf_tool *tool,
1207 u64 evtime, u64 type,
1208 perf_event__handler_t process,
1209 struct machine *machine)
1210{
1211 struct perf_record_stat_round event;
1212
1213 event.header.type = PERF_RECORD_STAT_ROUND;
1214 event.header.size = sizeof(event);
1215 event.header.misc = 0;
1216
1217 event.time = evtime;
1218 event.type = type;
1219
1220 return process(tool, (union perf_event *) &event, NULL, machine);
1221}
1222
1223void perf_event__read_stat_config(struct perf_stat_config *config, 120void perf_event__read_stat_config(struct perf_stat_config *config,
1224 struct perf_record_stat_config *event) 121 struct perf_record_stat_config *event)
1225{ 122{