1 #include <traceevent/event-parse.h>
3 #include "util/color.h"
4 #include "util/debug.h"
5 #include "util/evlist.h"
6 #include "util/machine.h"
7 #include "util/session.h"
8 #include "util/thread.h"
9 #include "util/parse-options.h"
10 #include "util/strlist.h"
11 #include "util/intlist.h"
12 #include "util/thread_map.h"
17 #include <linux/futex.h>
19 /* For older distros: */
21 # define MAP_STACK 0x20000
25 # define MADV_HWPOISON 100
28 #ifndef MADV_MERGEABLE
29 # define MADV_MERGEABLE 12
32 #ifndef MADV_UNMERGEABLE
33 # define MADV_UNMERGEABLE 13
48 #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
49 .nr_entries = ARRAY_SIZE(array), \
53 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
54 struct syscall_arg *arg)
57 struct strarray *sa = arg->parm;
59 if (idx < 0 || idx >= sa->nr_entries)
60 return scnprintf(bf, size, "%d", idx);
62 return scnprintf(bf, size, "%s", sa->entries[idx]);
65 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
67 static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
68 struct syscall_arg *arg)
70 return scnprintf(bf, size, "%#lx", arg->val);
73 #define SCA_HEX syscall_arg__scnprintf_hex
75 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
76 struct syscall_arg *arg)
78 int printed = 0, prot = arg->val;
80 if (prot == PROT_NONE)
81 return scnprintf(bf, size, "NONE");
82 #define P_MMAP_PROT(n) \
83 if (prot & PROT_##n) { \
84 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
94 P_MMAP_PROT(GROWSDOWN);
99 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
104 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
106 static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
107 struct syscall_arg *arg)
109 int printed = 0, flags = arg->val;
111 #define P_MMAP_FLAG(n) \
112 if (flags & MAP_##n) { \
113 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
118 P_MMAP_FLAG(PRIVATE);
122 P_MMAP_FLAG(ANONYMOUS);
123 P_MMAP_FLAG(DENYWRITE);
124 P_MMAP_FLAG(EXECUTABLE);
127 P_MMAP_FLAG(GROWSDOWN);
129 P_MMAP_FLAG(HUGETLB);
132 P_MMAP_FLAG(NONBLOCK);
133 P_MMAP_FLAG(NORESERVE);
134 P_MMAP_FLAG(POPULATE);
136 #ifdef MAP_UNINITIALIZED
137 P_MMAP_FLAG(UNINITIALIZED);
142 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
147 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
149 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
150 struct syscall_arg *arg)
152 int behavior = arg->val;
155 #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
158 P_MADV_BHV(SEQUENTIAL);
159 P_MADV_BHV(WILLNEED);
160 P_MADV_BHV(DONTNEED);
162 P_MADV_BHV(DONTFORK);
164 P_MADV_BHV(HWPOISON);
165 #ifdef MADV_SOFT_OFFLINE
166 P_MADV_BHV(SOFT_OFFLINE);
168 P_MADV_BHV(MERGEABLE);
169 P_MADV_BHV(UNMERGEABLE);
171 P_MADV_BHV(HUGEPAGE);
173 #ifdef MADV_NOHUGEPAGE
174 P_MADV_BHV(NOHUGEPAGE);
177 P_MADV_BHV(DONTDUMP);
186 return scnprintf(bf, size, "%#x", behavior);
189 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
191 static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
193 enum syscall_futex_args {
194 SCF_UADDR = (1 << 0),
197 SCF_TIMEOUT = (1 << 3),
198 SCF_UADDR2 = (1 << 4),
202 int cmd = op & FUTEX_CMD_MASK;
206 #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
207 P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
208 P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
209 P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
210 P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
211 P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
212 P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
213 P_FUTEX_OP(WAKE_OP); break;
214 P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
215 P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
216 P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
217 P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
218 P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
219 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
220 default: printed = scnprintf(bf, size, "%#x", cmd); break;
223 if (op & FUTEX_PRIVATE_FLAG)
224 printed += scnprintf(bf + printed, size - printed, "|PRIV");
226 if (op & FUTEX_CLOCK_REALTIME)
227 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
232 #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
234 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
235 static DEFINE_STRARRAY(itimers);
237 static const char *whences[] = { "SET", "CUR", "END",
245 static DEFINE_STRARRAY(whences);
247 static const char *fcntl_cmds[] = {
248 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
249 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
250 "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
253 static DEFINE_STRARRAY(fcntl_cmds);
255 static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
256 struct syscall_arg *arg)
258 int printed = 0, flags = arg->val;
260 if (!(flags & O_CREAT))
261 arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
264 return scnprintf(bf, size, "RDONLY");
266 if (flags & O_##n) { \
267 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
291 if ((flags & O_SYNC) == O_SYNC)
292 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
304 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
309 #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
311 static struct syscall_fmt {
314 size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
320 { .name = "access", .errmsg = true, },
321 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
322 { .name = "brk", .hexret = true,
323 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
324 { .name = "mmap", .hexret = true, },
325 { .name = "connect", .errmsg = true, },
326 { .name = "fcntl", .errmsg = true,
327 .arg_scnprintf = { [1] = SCA_STRARRAY, /* cmd */ },
328 .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, },
329 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
330 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
331 { .name = "futex", .errmsg = true,
332 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
333 { .name = "getitimer", .errmsg = true,
334 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
335 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
336 { .name = "ioctl", .errmsg = true,
337 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
338 { .name = "lseek", .errmsg = true,
339 .arg_scnprintf = { [2] = SCA_STRARRAY, /* whence */ },
340 .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
341 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
342 { .name = "madvise", .errmsg = true,
343 .arg_scnprintf = { [0] = SCA_HEX, /* start */
344 [2] = SCA_MADV_BHV, /* behavior */ }, },
345 { .name = "mmap", .hexret = true,
346 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
347 [2] = SCA_MMAP_PROT, /* prot */
348 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
349 { .name = "mprotect", .errmsg = true,
350 .arg_scnprintf = { [0] = SCA_HEX, /* start */
351 [2] = SCA_MMAP_PROT, /* prot */ }, },
352 { .name = "mremap", .hexret = true,
353 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
354 [4] = SCA_HEX, /* new_addr */ }, },
355 { .name = "munmap", .errmsg = true,
356 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
357 { .name = "open", .errmsg = true,
358 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
359 { .name = "open_by_handle_at", .errmsg = true,
360 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
361 { .name = "openat", .errmsg = true,
362 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
363 { .name = "poll", .errmsg = true, .timeout = true, },
364 { .name = "ppoll", .errmsg = true, .timeout = true, },
365 { .name = "pread", .errmsg = true, .alias = "pread64", },
366 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
367 { .name = "read", .errmsg = true, },
368 { .name = "recvfrom", .errmsg = true, },
369 { .name = "select", .errmsg = true, .timeout = true, },
370 { .name = "setitimer", .errmsg = true,
371 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
372 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
373 { .name = "socket", .errmsg = true, },
374 { .name = "stat", .errmsg = true, .alias = "newstat", },
375 { .name = "uname", .errmsg = true, .alias = "newuname", },
378 static int syscall_fmt__cmp(const void *name, const void *fmtp)
380 const struct syscall_fmt *fmt = fmtp;
381 return strcmp(name, fmt->name);
384 static struct syscall_fmt *syscall_fmt__find(const char *name)
386 const int nmemb = ARRAY_SIZE(syscall_fmts);
387 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
391 struct event_format *tp_format;
394 struct syscall_fmt *fmt;
395 size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
399 static size_t fprintf_duration(unsigned long t, FILE *fp)
401 double duration = (double)t / NSEC_PER_MSEC;
402 size_t printed = fprintf(fp, "(");
405 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
406 else if (duration >= 0.01)
407 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
409 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
410 return printed + fprintf(fp, "): ");
413 struct thread_trace {
417 unsigned long nr_events;
422 static struct thread_trace *thread_trace__new(void)
424 return zalloc(sizeof(struct thread_trace));
427 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
429 struct thread_trace *ttrace;
434 if (thread->priv == NULL)
435 thread->priv = thread_trace__new();
437 if (thread->priv == NULL)
440 ttrace = thread->priv;
445 color_fprintf(fp, PERF_COLOR_RED,
446 "WARNING: not enough memory, dropping samples!\n");
451 struct perf_tool tool;
455 struct syscall *table;
457 struct perf_record_opts opts;
461 unsigned long nr_events;
462 struct strlist *ev_qualifier;
463 bool not_ev_qualifier;
464 struct intlist *tid_list;
465 struct intlist *pid_list;
467 bool multiple_threads;
468 double duration_filter;
472 static bool trace__filter_duration(struct trace *trace, double t)
474 return t < (trace->duration_filter * NSEC_PER_MSEC);
477 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
479 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
481 return fprintf(fp, "%10.3f ", ts);
484 static bool done = false;
486 static void sig_handler(int sig __maybe_unused)
491 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
492 u64 duration, u64 tstamp, FILE *fp)
494 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
495 printed += fprintf_duration(duration, fp);
497 if (trace->multiple_threads)
498 printed += fprintf(fp, "%d ", thread->tid);
503 static int trace__process_event(struct trace *trace, struct machine *machine,
504 union perf_event *event)
508 switch (event->header.type) {
509 case PERF_RECORD_LOST:
510 color_fprintf(trace->output, PERF_COLOR_RED,
511 "LOST %" PRIu64 " events!\n", event->lost.lost);
512 ret = machine__process_lost_event(machine, event);
514 ret = machine__process_event(machine, event);
521 static int trace__tool_process(struct perf_tool *tool,
522 union perf_event *event,
523 struct perf_sample *sample __maybe_unused,
524 struct machine *machine)
526 struct trace *trace = container_of(tool, struct trace, tool);
527 return trace__process_event(trace, machine, event);
530 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
532 int err = symbol__init();
537 machine__init(&trace->host, "", HOST_KERNEL_ID);
538 machine__create_kernel_maps(&trace->host);
540 if (perf_target__has_task(&trace->opts.target)) {
541 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
545 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
555 static int syscall__set_arg_fmts(struct syscall *sc)
557 struct format_field *field;
560 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
561 if (sc->arg_scnprintf == NULL)
565 sc->arg_parm = sc->fmt->arg_parm;
567 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
568 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
569 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
570 else if (field->flags & FIELD_IS_POINTER)
571 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
578 static int trace__read_syscall_info(struct trace *trace, int id)
582 const char *name = audit_syscall_to_name(id, trace->audit_machine);
587 if (id > trace->syscalls.max) {
588 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
590 if (nsyscalls == NULL)
593 if (trace->syscalls.max != -1) {
594 memset(nsyscalls + trace->syscalls.max + 1, 0,
595 (id - trace->syscalls.max) * sizeof(*sc));
597 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
600 trace->syscalls.table = nsyscalls;
601 trace->syscalls.max = id;
604 sc = trace->syscalls.table + id;
607 if (trace->ev_qualifier) {
608 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
610 if (!(in ^ trace->not_ev_qualifier)) {
613 * No need to do read tracepoint information since this will be
620 sc->fmt = syscall_fmt__find(sc->name);
622 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
623 sc->tp_format = event_format__new("syscalls", tp_name);
625 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
626 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
627 sc->tp_format = event_format__new("syscalls", tp_name);
630 if (sc->tp_format == NULL)
633 return syscall__set_arg_fmts(sc);
636 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
641 if (sc->tp_format != NULL) {
642 struct format_field *field;
644 struct syscall_arg arg = {
649 for (field = sc->tp_format->format.fields->next; field;
650 field = field->next, ++arg.idx, bit <<= 1) {
654 printed += scnprintf(bf + printed, size - printed,
655 "%s%s: ", printed ? ", " : "", field->name);
656 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
657 arg.val = args[arg.idx];
659 arg.parm = sc->arg_parm[arg.idx];
660 printed += sc->arg_scnprintf[arg.idx](bf + printed,
661 size - printed, &arg);
663 printed += scnprintf(bf + printed, size - printed,
664 "%ld", args[arg.idx]);
671 printed += scnprintf(bf + printed, size - printed,
673 printed ? ", " : "", i, args[i]);
681 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
682 struct perf_sample *sample);
684 static struct syscall *trace__syscall_info(struct trace *trace,
685 struct perf_evsel *evsel,
686 struct perf_sample *sample)
688 int id = perf_evsel__intval(evsel, sample, "id");
693 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
694 * before that, leaving at a higher verbosity level till that is
695 * explained. Reproduced with plain ftrace with:
697 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
698 * grep "NR -1 " /t/trace_pipe
700 * After generating some load on the machine.
704 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
705 id, perf_evsel__name(evsel), ++n);
710 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
711 trace__read_syscall_info(trace, id))
714 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
717 return &trace->syscalls.table[id];
721 fprintf(trace->output, "Problems reading syscall %d", id);
722 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
723 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
724 fputs(" information\n", trace->output);
729 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
730 struct perf_sample *sample)
735 struct thread *thread;
736 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
737 struct thread_trace *ttrace;
745 thread = machine__findnew_thread(&trace->host, sample->pid,
747 ttrace = thread__trace(thread, trace->output);
751 args = perf_evsel__rawptr(evsel, sample, "args");
753 fprintf(trace->output, "Problems reading syscall arguments\n");
757 ttrace = thread->priv;
759 if (ttrace->entry_str == NULL) {
760 ttrace->entry_str = malloc(1024);
761 if (!ttrace->entry_str)
765 ttrace->entry_time = sample->time;
766 msg = ttrace->entry_str;
767 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
769 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
771 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
772 if (!trace->duration_filter) {
773 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
774 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
777 ttrace->entry_pending = true;
782 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
783 struct perf_sample *sample)
787 struct thread *thread;
788 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
789 struct thread_trace *ttrace;
797 thread = machine__findnew_thread(&trace->host, sample->pid,
799 ttrace = thread__trace(thread, trace->output);
803 ret = perf_evsel__intval(evsel, sample, "ret");
805 ttrace = thread->priv;
807 ttrace->exit_time = sample->time;
809 if (ttrace->entry_time) {
810 duration = sample->time - ttrace->entry_time;
811 if (trace__filter_duration(trace, duration))
813 } else if (trace->duration_filter)
816 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
818 if (ttrace->entry_pending) {
819 fprintf(trace->output, "%-70s", ttrace->entry_str);
821 fprintf(trace->output, " ... [");
822 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
823 fprintf(trace->output, "]: %s()", sc->name);
826 if (sc->fmt == NULL) {
828 fprintf(trace->output, ") = %d", ret);
829 } else if (ret < 0 && sc->fmt->errmsg) {
831 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
832 *e = audit_errno_to_name(-ret);
834 fprintf(trace->output, ") = -1 %s %s", e, emsg);
835 } else if (ret == 0 && sc->fmt->timeout)
836 fprintf(trace->output, ") = 0 Timeout");
837 else if (sc->fmt->hexret)
838 fprintf(trace->output, ") = %#x", ret);
842 fputc('\n', trace->output);
844 ttrace->entry_pending = false;
849 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
850 struct perf_sample *sample)
852 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
853 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
854 struct thread *thread = machine__findnew_thread(&trace->host,
857 struct thread_trace *ttrace = thread__trace(thread, trace->output);
862 ttrace->runtime_ms += runtime_ms;
863 trace->runtime_ms += runtime_ms;
867 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
869 perf_evsel__strval(evsel, sample, "comm"),
870 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
872 perf_evsel__intval(evsel, sample, "vruntime"));
876 static bool skip_sample(struct trace *trace, struct perf_sample *sample)
878 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
879 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
882 if (trace->pid_list || trace->tid_list)
888 static int trace__process_sample(struct perf_tool *tool,
889 union perf_event *event __maybe_unused,
890 struct perf_sample *sample,
891 struct perf_evsel *evsel,
892 struct machine *machine __maybe_unused)
894 struct trace *trace = container_of(tool, struct trace, tool);
897 tracepoint_handler handler = evsel->handler.func;
899 if (skip_sample(trace, sample))
902 if (trace->base_time == 0)
903 trace->base_time = sample->time;
906 handler(trace, evsel, sample);
912 perf_session__has_tp(struct perf_session *session, const char *name)
914 struct perf_evsel *evsel;
916 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
918 return evsel != NULL;
921 static int parse_target_str(struct trace *trace)
923 if (trace->opts.target.pid) {
924 trace->pid_list = intlist__new(trace->opts.target.pid);
925 if (trace->pid_list == NULL) {
926 pr_err("Error parsing process id string\n");
931 if (trace->opts.target.tid) {
932 trace->tid_list = intlist__new(trace->opts.target.tid);
933 if (trace->tid_list == NULL) {
934 pr_err("Error parsing thread id string\n");
942 static int trace__run(struct trace *trace, int argc, const char **argv)
944 struct perf_evlist *evlist = perf_evlist__new();
945 struct perf_evsel *evsel;
947 unsigned long before;
948 const bool forks = argc > 0;
950 if (evlist == NULL) {
951 fprintf(trace->output, "Not enough memory to run!\n");
955 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
956 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
957 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
958 goto out_delete_evlist;
962 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
963 trace__sched_stat_runtime)) {
964 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
965 goto out_delete_evlist;
968 err = perf_evlist__create_maps(evlist, &trace->opts.target);
970 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
971 goto out_delete_evlist;
974 err = trace__symbols_init(trace, evlist);
976 fprintf(trace->output, "Problems initializing symbol libraries!\n");
977 goto out_delete_maps;
980 perf_evlist__config(evlist, &trace->opts);
982 signal(SIGCHLD, sig_handler);
983 signal(SIGINT, sig_handler);
986 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
989 fprintf(trace->output, "Couldn't run the workload!\n");
990 goto out_delete_maps;
994 err = perf_evlist__open(evlist);
996 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
997 goto out_delete_maps;
1000 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1002 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
1003 goto out_close_evlist;
1006 perf_evlist__enable(evlist);
1009 perf_evlist__start_workload(evlist);
1011 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
1013 before = trace->nr_events;
1015 for (i = 0; i < evlist->nr_mmaps; i++) {
1016 union perf_event *event;
1018 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1019 const u32 type = event->header.type;
1020 tracepoint_handler handler;
1021 struct perf_sample sample;
1025 err = perf_evlist__parse_sample(evlist, event, &sample);
1027 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
1031 if (trace->base_time == 0)
1032 trace->base_time = sample.time;
1034 if (type != PERF_RECORD_SAMPLE) {
1035 trace__process_event(trace, &trace->host, event);
1039 evsel = perf_evlist__id2evsel(evlist, sample.id);
1040 if (evsel == NULL) {
1041 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
1045 if (sample.raw_data == NULL) {
1046 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
1047 perf_evsel__name(evsel), sample.tid,
1048 sample.cpu, sample.raw_size);
1052 handler = evsel->handler.func;
1053 handler(trace, evsel, &sample);
1056 goto out_unmap_evlist;
1060 if (trace->nr_events == before) {
1062 goto out_unmap_evlist;
1064 poll(evlist->pollfd, evlist->nr_fds, -1);
1068 perf_evlist__disable(evlist);
1073 perf_evlist__munmap(evlist);
1075 perf_evlist__close(evlist);
1077 perf_evlist__delete_maps(evlist);
1079 perf_evlist__delete(evlist);
1084 static int trace__replay(struct trace *trace)
1086 const struct perf_evsel_str_handler handlers[] = {
1087 { "raw_syscalls:sys_enter", trace__sys_enter, },
1088 { "raw_syscalls:sys_exit", trace__sys_exit, },
1091 struct perf_session *session;
1094 trace->tool.sample = trace__process_sample;
1095 trace->tool.mmap = perf_event__process_mmap;
1096 trace->tool.mmap2 = perf_event__process_mmap2;
1097 trace->tool.comm = perf_event__process_comm;
1098 trace->tool.exit = perf_event__process_exit;
1099 trace->tool.fork = perf_event__process_fork;
1100 trace->tool.attr = perf_event__process_attr;
1101 trace->tool.tracing_data = perf_event__process_tracing_data;
1102 trace->tool.build_id = perf_event__process_build_id;
1104 trace->tool.ordered_samples = true;
1105 trace->tool.ordering_requires_timestamps = true;
1107 /* add tid to output */
1108 trace->multiple_threads = true;
1110 if (symbol__init() < 0)
1113 session = perf_session__new(input_name, O_RDONLY, 0, false,
1115 if (session == NULL)
1118 err = perf_session__set_tracepoints_handlers(session, handlers);
1122 if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
1123 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
1127 if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
1128 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
1132 err = parse_target_str(trace);
1138 err = perf_session__process_events(session, &trace->tool);
1140 pr_err("Failed to process events, error %d", err);
1143 perf_session__delete(session);
1148 static size_t trace__fprintf_threads_header(FILE *fp)
1152 printed = fprintf(fp, "\n _____________________________________________________________________\n");
1153 printed += fprintf(fp," __) Summary of events (__\n\n");
1154 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
1155 printed += fprintf(fp," _____________________________________________________________________\n\n");
1160 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1162 size_t printed = trace__fprintf_threads_header(fp);
1165 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1166 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1167 struct thread_trace *ttrace = thread->priv;
1174 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1176 color = PERF_COLOR_NORMAL;
1178 color = PERF_COLOR_RED;
1179 else if (ratio > 25.0)
1180 color = PERF_COLOR_GREEN;
1181 else if (ratio > 5.0)
1182 color = PERF_COLOR_YELLOW;
1184 printed += color_fprintf(fp, color, "%20s", thread->comm);
1185 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1186 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1187 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1193 static int trace__set_duration(const struct option *opt, const char *str,
1194 int unset __maybe_unused)
1196 struct trace *trace = opt->value;
1198 trace->duration_filter = atof(str);
1202 static int trace__open_output(struct trace *trace, const char *filename)
1206 if (!stat(filename, &st) && st.st_size) {
1207 char oldname[PATH_MAX];
1209 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1211 rename(filename, oldname);
1214 trace->output = fopen(filename, "w");
1216 return trace->output == NULL ? -errno : 0;
1219 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1221 const char * const trace_usage[] = {
1222 "perf trace [<options>] [<command>]",
1223 "perf trace [<options>] -- <command> [<options>]",
1226 struct trace trace = {
1227 .audit_machine = audit_detect_machine(),
1236 .user_freq = UINT_MAX,
1237 .user_interval = ULLONG_MAX,
1243 const char *output_name = NULL;
1244 const char *ev_qualifier_str = NULL;
1245 const struct option trace_options[] = {
1246 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1247 "list of events to trace"),
1248 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1249 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
1250 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1251 "trace events on existing process id"),
1252 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
1253 "trace events on existing thread id"),
1254 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
1255 "system-wide collection from all CPUs"),
1256 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
1257 "list of cpus to monitor"),
1258 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
1259 "child tasks do not inherit counters"),
1260 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
1261 "number of mmap data pages"),
1262 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
1264 OPT_CALLBACK(0, "duration", &trace, "float",
1265 "show only events with duration > N.M ms",
1266 trace__set_duration),
1267 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
1268 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1274 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
1276 if (output_name != NULL) {
1277 err = trace__open_output(&trace, output_name);
1279 perror("failed to create output file");
1284 if (ev_qualifier_str != NULL) {
1285 const char *s = ev_qualifier_str;
1287 trace.not_ev_qualifier = *s == '!';
1288 if (trace.not_ev_qualifier)
1290 trace.ev_qualifier = strlist__new(true, s);
1291 if (trace.ev_qualifier == NULL) {
1292 fputs("Not enough memory to parse event qualifier",
1299 err = perf_target__validate(&trace.opts.target);
1301 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1302 fprintf(trace.output, "%s", bf);
1306 err = perf_target__parse_uid(&trace.opts.target);
1308 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1309 fprintf(trace.output, "%s", bf);
1313 if (!argc && perf_target__none(&trace.opts.target))
1314 trace.opts.target.system_wide = true;
1317 err = trace__replay(&trace);
1319 err = trace__run(&trace, argc, argv);
1321 if (trace.sched && !err)
1322 trace__fprintf_thread_summary(&trace, trace.output);
1325 if (output_name != NULL)
1326 fclose(trace.output);