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 static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
20 unsigned long arg, u8 *arg_mask __maybe_unused)
22 return scnprintf(bf, size, "%#lx", arg);
25 #define SCA_HEX syscall_arg__scnprintf_hex
27 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
28 unsigned long arg, u8 *arg_mask __maybe_unused)
30 int printed = 0, prot = arg;
32 if (prot == PROT_NONE)
33 return scnprintf(bf, size, "NONE");
34 #define P_MMAP_PROT(n) \
35 if (prot & PROT_##n) { \
36 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
46 P_MMAP_PROT(GROWSDOWN);
51 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
56 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
58 static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
59 unsigned long arg, u8 *arg_mask __maybe_unused)
61 int printed = 0, flags = arg;
63 #define P_MMAP_FLAG(n) \
64 if (flags & MAP_##n) { \
65 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
72 P_MMAP_FLAG(ANONYMOUS);
73 P_MMAP_FLAG(DENYWRITE);
74 P_MMAP_FLAG(EXECUTABLE);
77 P_MMAP_FLAG(GROWSDOWN);
82 P_MMAP_FLAG(NONBLOCK);
83 P_MMAP_FLAG(NORESERVE);
84 P_MMAP_FLAG(POPULATE);
86 #ifdef MAP_UNINITIALIZED
87 P_MMAP_FLAG(UNINITIALIZED);
92 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
97 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
99 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
100 unsigned long arg, u8 *arg_mask __maybe_unused)
105 #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
108 P_MADV_BHV(SEQUENTIAL);
109 P_MADV_BHV(WILLNEED);
110 P_MADV_BHV(DONTNEED);
112 P_MADV_BHV(DONTFORK);
114 P_MADV_BHV(HWPOISON);
115 #ifdef MADV_SOFT_OFFLINE
116 P_MADV_BHV(SOFT_OFFLINE);
118 P_MADV_BHV(MERGEABLE);
119 P_MADV_BHV(UNMERGEABLE);
121 P_MADV_BHV(HUGEPAGE);
123 #ifdef MADV_NOHUGEPAGE
124 P_MADV_BHV(NOHUGEPAGE);
127 P_MADV_BHV(DONTDUMP);
136 return scnprintf(bf, size, "%#x", behavior);
139 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
141 static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, unsigned long arg, u8 *arg_mask)
143 enum syscall_futex_args {
144 SCF_UADDR = (1 << 0),
147 SCF_TIMEOUT = (1 << 3),
148 SCF_UADDR2 = (1 << 4),
152 int cmd = op & FUTEX_CMD_MASK;
156 #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
157 P_FUTEX_OP(WAIT); *arg_mask |= SCF_VAL3|SCF_UADDR2; break;
158 P_FUTEX_OP(WAKE); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
159 P_FUTEX_OP(FD); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
160 P_FUTEX_OP(REQUEUE); *arg_mask |= SCF_VAL3|SCF_TIMEOUT; break;
161 P_FUTEX_OP(CMP_REQUEUE); *arg_mask |= SCF_TIMEOUT; break;
162 P_FUTEX_OP(CMP_REQUEUE_PI); *arg_mask |= SCF_TIMEOUT; break;
163 P_FUTEX_OP(WAKE_OP); break;
164 P_FUTEX_OP(LOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
165 P_FUTEX_OP(UNLOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
166 P_FUTEX_OP(TRYLOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2; break;
167 P_FUTEX_OP(WAIT_BITSET); *arg_mask |= SCF_UADDR2; break;
168 P_FUTEX_OP(WAKE_BITSET); *arg_mask |= SCF_UADDR2; break;
169 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
170 default: printed = scnprintf(bf, size, "%#x", cmd); break;
173 if (op & FUTEX_PRIVATE_FLAG)
174 printed += scnprintf(bf + printed, size - printed, "|PRIV");
176 if (op & FUTEX_CLOCK_REALTIME)
177 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
182 #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
184 static struct syscall_fmt {
187 size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg, u8 *arg_mask);
192 { .name = "access", .errmsg = true, },
193 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
194 { .name = "brk", .hexret = true,
195 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
196 { .name = "mmap", .hexret = true, },
197 { .name = "connect", .errmsg = true, },
198 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
199 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
200 { .name = "futex", .errmsg = true,
201 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
202 { .name = "ioctl", .errmsg = true,
203 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
204 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
205 { .name = "madvise", .errmsg = true,
206 .arg_scnprintf = { [0] = SCA_HEX, /* start */
207 [2] = SCA_MADV_BHV, /* behavior */ }, },
208 { .name = "mmap", .hexret = true,
209 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
210 [2] = SCA_MMAP_PROT, /* prot */
211 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
212 { .name = "mprotect", .errmsg = true,
213 .arg_scnprintf = { [0] = SCA_HEX, /* start */
214 [2] = SCA_MMAP_PROT, /* prot */ }, },
215 { .name = "mremap", .hexret = true,
216 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
217 [4] = SCA_HEX, /* new_addr */ }, },
218 { .name = "munmap", .errmsg = true,
219 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
220 { .name = "open", .errmsg = true, },
221 { .name = "poll", .errmsg = true, .timeout = true, },
222 { .name = "ppoll", .errmsg = true, .timeout = true, },
223 { .name = "pread", .errmsg = true, .alias = "pread64", },
224 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
225 { .name = "read", .errmsg = true, },
226 { .name = "recvfrom", .errmsg = true, },
227 { .name = "select", .errmsg = true, .timeout = true, },
228 { .name = "socket", .errmsg = true, },
229 { .name = "stat", .errmsg = true, .alias = "newstat", },
230 { .name = "uname", .errmsg = true, .alias = "newuname", },
233 static int syscall_fmt__cmp(const void *name, const void *fmtp)
235 const struct syscall_fmt *fmt = fmtp;
236 return strcmp(name, fmt->name);
239 static struct syscall_fmt *syscall_fmt__find(const char *name)
241 const int nmemb = ARRAY_SIZE(syscall_fmts);
242 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
246 struct event_format *tp_format;
249 struct syscall_fmt *fmt;
250 size_t (**arg_scnprintf)(char *bf, size_t size,
251 unsigned long arg, u8 *args_mask);
254 static size_t fprintf_duration(unsigned long t, FILE *fp)
256 double duration = (double)t / NSEC_PER_MSEC;
257 size_t printed = fprintf(fp, "(");
260 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
261 else if (duration >= 0.01)
262 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
264 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
265 return printed + fprintf(fp, "): ");
268 struct thread_trace {
272 unsigned long nr_events;
277 static struct thread_trace *thread_trace__new(void)
279 return zalloc(sizeof(struct thread_trace));
282 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
284 struct thread_trace *ttrace;
289 if (thread->priv == NULL)
290 thread->priv = thread_trace__new();
292 if (thread->priv == NULL)
295 ttrace = thread->priv;
300 color_fprintf(fp, PERF_COLOR_RED,
301 "WARNING: not enough memory, dropping samples!\n");
306 struct perf_tool tool;
310 struct syscall *table;
312 struct perf_record_opts opts;
316 unsigned long nr_events;
317 struct strlist *ev_qualifier;
318 bool not_ev_qualifier;
319 struct intlist *tid_list;
320 struct intlist *pid_list;
322 bool multiple_threads;
323 double duration_filter;
327 static bool trace__filter_duration(struct trace *trace, double t)
329 return t < (trace->duration_filter * NSEC_PER_MSEC);
332 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
334 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
336 return fprintf(fp, "%10.3f ", ts);
339 static bool done = false;
341 static void sig_handler(int sig __maybe_unused)
346 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
347 u64 duration, u64 tstamp, FILE *fp)
349 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
350 printed += fprintf_duration(duration, fp);
352 if (trace->multiple_threads)
353 printed += fprintf(fp, "%d ", thread->tid);
358 static int trace__process_event(struct trace *trace, struct machine *machine,
359 union perf_event *event)
363 switch (event->header.type) {
364 case PERF_RECORD_LOST:
365 color_fprintf(trace->output, PERF_COLOR_RED,
366 "LOST %" PRIu64 " events!\n", event->lost.lost);
367 ret = machine__process_lost_event(machine, event);
369 ret = machine__process_event(machine, event);
376 static int trace__tool_process(struct perf_tool *tool,
377 union perf_event *event,
378 struct perf_sample *sample __maybe_unused,
379 struct machine *machine)
381 struct trace *trace = container_of(tool, struct trace, tool);
382 return trace__process_event(trace, machine, event);
385 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
387 int err = symbol__init();
392 machine__init(&trace->host, "", HOST_KERNEL_ID);
393 machine__create_kernel_maps(&trace->host);
395 if (perf_target__has_task(&trace->opts.target)) {
396 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
400 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
410 static int syscall__set_arg_fmts(struct syscall *sc)
412 struct format_field *field;
415 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
416 if (sc->arg_scnprintf == NULL)
419 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
420 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
421 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
422 else if (field->flags & FIELD_IS_POINTER)
423 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
430 static int trace__read_syscall_info(struct trace *trace, int id)
434 const char *name = audit_syscall_to_name(id, trace->audit_machine);
439 if (id > trace->syscalls.max) {
440 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
442 if (nsyscalls == NULL)
445 if (trace->syscalls.max != -1) {
446 memset(nsyscalls + trace->syscalls.max + 1, 0,
447 (id - trace->syscalls.max) * sizeof(*sc));
449 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
452 trace->syscalls.table = nsyscalls;
453 trace->syscalls.max = id;
456 sc = trace->syscalls.table + id;
459 if (trace->ev_qualifier) {
460 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
462 if (!(in ^ trace->not_ev_qualifier)) {
465 * No need to do read tracepoint information since this will be
472 sc->fmt = syscall_fmt__find(sc->name);
474 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
475 sc->tp_format = event_format__new("syscalls", tp_name);
477 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
478 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
479 sc->tp_format = event_format__new("syscalls", tp_name);
482 if (sc->tp_format == NULL)
485 return syscall__set_arg_fmts(sc);
488 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
494 if (sc->tp_format != NULL) {
495 struct format_field *field;
496 u8 mask = 0, bit = 1;
498 for (field = sc->tp_format->format.fields->next; field;
499 field = field->next, ++i, bit <<= 1) {
503 printed += scnprintf(bf + printed, size - printed,
504 "%s%s: ", printed ? ", " : "", field->name);
506 if (sc->arg_scnprintf && sc->arg_scnprintf[i]) {
507 printed += sc->arg_scnprintf[i](bf + printed, size - printed,
510 printed += scnprintf(bf + printed, size - printed,
516 printed += scnprintf(bf + printed, size - printed,
518 printed ? ", " : "", i, args[i]);
526 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
527 struct perf_sample *sample);
529 static struct syscall *trace__syscall_info(struct trace *trace,
530 struct perf_evsel *evsel,
531 struct perf_sample *sample)
533 int id = perf_evsel__intval(evsel, sample, "id");
538 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
539 * before that, leaving at a higher verbosity level till that is
540 * explained. Reproduced with plain ftrace with:
542 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
543 * grep "NR -1 " /t/trace_pipe
545 * After generating some load on the machine.
549 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
550 id, perf_evsel__name(evsel), ++n);
555 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
556 trace__read_syscall_info(trace, id))
559 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
562 return &trace->syscalls.table[id];
566 fprintf(trace->output, "Problems reading syscall %d", id);
567 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
568 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
569 fputs(" information\n", trace->output);
574 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
575 struct perf_sample *sample)
580 struct thread *thread;
581 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
582 struct thread_trace *ttrace;
590 thread = machine__findnew_thread(&trace->host, sample->pid,
592 ttrace = thread__trace(thread, trace->output);
596 args = perf_evsel__rawptr(evsel, sample, "args");
598 fprintf(trace->output, "Problems reading syscall arguments\n");
602 ttrace = thread->priv;
604 if (ttrace->entry_str == NULL) {
605 ttrace->entry_str = malloc(1024);
606 if (!ttrace->entry_str)
610 ttrace->entry_time = sample->time;
611 msg = ttrace->entry_str;
612 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
614 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
616 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
617 if (!trace->duration_filter) {
618 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
619 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
622 ttrace->entry_pending = true;
627 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
628 struct perf_sample *sample)
632 struct thread *thread;
633 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
634 struct thread_trace *ttrace;
642 thread = machine__findnew_thread(&trace->host, sample->pid,
644 ttrace = thread__trace(thread, trace->output);
648 ret = perf_evsel__intval(evsel, sample, "ret");
650 ttrace = thread->priv;
652 ttrace->exit_time = sample->time;
654 if (ttrace->entry_time) {
655 duration = sample->time - ttrace->entry_time;
656 if (trace__filter_duration(trace, duration))
658 } else if (trace->duration_filter)
661 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
663 if (ttrace->entry_pending) {
664 fprintf(trace->output, "%-70s", ttrace->entry_str);
666 fprintf(trace->output, " ... [");
667 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
668 fprintf(trace->output, "]: %s()", sc->name);
671 if (sc->fmt == NULL) {
673 fprintf(trace->output, ") = %d", ret);
674 } else if (ret < 0 && sc->fmt->errmsg) {
676 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
677 *e = audit_errno_to_name(-ret);
679 fprintf(trace->output, ") = -1 %s %s", e, emsg);
680 } else if (ret == 0 && sc->fmt->timeout)
681 fprintf(trace->output, ") = 0 Timeout");
682 else if (sc->fmt->hexret)
683 fprintf(trace->output, ") = %#x", ret);
687 fputc('\n', trace->output);
689 ttrace->entry_pending = false;
694 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
695 struct perf_sample *sample)
697 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
698 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
699 struct thread *thread = machine__findnew_thread(&trace->host,
702 struct thread_trace *ttrace = thread__trace(thread, trace->output);
707 ttrace->runtime_ms += runtime_ms;
708 trace->runtime_ms += runtime_ms;
712 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
714 perf_evsel__strval(evsel, sample, "comm"),
715 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
717 perf_evsel__intval(evsel, sample, "vruntime"));
721 static bool skip_sample(struct trace *trace, struct perf_sample *sample)
723 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
724 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
727 if (trace->pid_list || trace->tid_list)
733 static int trace__process_sample(struct perf_tool *tool,
734 union perf_event *event __maybe_unused,
735 struct perf_sample *sample,
736 struct perf_evsel *evsel,
737 struct machine *machine __maybe_unused)
739 struct trace *trace = container_of(tool, struct trace, tool);
742 tracepoint_handler handler = evsel->handler.func;
744 if (skip_sample(trace, sample))
747 if (trace->base_time == 0)
748 trace->base_time = sample->time;
751 handler(trace, evsel, sample);
757 perf_session__has_tp(struct perf_session *session, const char *name)
759 struct perf_evsel *evsel;
761 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
763 return evsel != NULL;
766 static int parse_target_str(struct trace *trace)
768 if (trace->opts.target.pid) {
769 trace->pid_list = intlist__new(trace->opts.target.pid);
770 if (trace->pid_list == NULL) {
771 pr_err("Error parsing process id string\n");
776 if (trace->opts.target.tid) {
777 trace->tid_list = intlist__new(trace->opts.target.tid);
778 if (trace->tid_list == NULL) {
779 pr_err("Error parsing thread id string\n");
787 static int trace__run(struct trace *trace, int argc, const char **argv)
789 struct perf_evlist *evlist = perf_evlist__new();
790 struct perf_evsel *evsel;
792 unsigned long before;
793 const bool forks = argc > 0;
795 if (evlist == NULL) {
796 fprintf(trace->output, "Not enough memory to run!\n");
800 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
801 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
802 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
803 goto out_delete_evlist;
807 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
808 trace__sched_stat_runtime)) {
809 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
810 goto out_delete_evlist;
813 err = perf_evlist__create_maps(evlist, &trace->opts.target);
815 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
816 goto out_delete_evlist;
819 err = trace__symbols_init(trace, evlist);
821 fprintf(trace->output, "Problems initializing symbol libraries!\n");
822 goto out_delete_maps;
825 perf_evlist__config(evlist, &trace->opts);
827 signal(SIGCHLD, sig_handler);
828 signal(SIGINT, sig_handler);
831 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
834 fprintf(trace->output, "Couldn't run the workload!\n");
835 goto out_delete_maps;
839 err = perf_evlist__open(evlist);
841 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
842 goto out_delete_maps;
845 err = perf_evlist__mmap(evlist, UINT_MAX, false);
847 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
848 goto out_close_evlist;
851 perf_evlist__enable(evlist);
854 perf_evlist__start_workload(evlist);
856 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
858 before = trace->nr_events;
860 for (i = 0; i < evlist->nr_mmaps; i++) {
861 union perf_event *event;
863 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
864 const u32 type = event->header.type;
865 tracepoint_handler handler;
866 struct perf_sample sample;
870 err = perf_evlist__parse_sample(evlist, event, &sample);
872 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
876 if (trace->base_time == 0)
877 trace->base_time = sample.time;
879 if (type != PERF_RECORD_SAMPLE) {
880 trace__process_event(trace, &trace->host, event);
884 evsel = perf_evlist__id2evsel(evlist, sample.id);
886 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
890 if (sample.raw_data == NULL) {
891 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
892 perf_evsel__name(evsel), sample.tid,
893 sample.cpu, sample.raw_size);
897 handler = evsel->handler.func;
898 handler(trace, evsel, &sample);
902 if (trace->nr_events == before) {
904 goto out_unmap_evlist;
906 poll(evlist->pollfd, evlist->nr_fds, -1);
910 perf_evlist__disable(evlist);
915 perf_evlist__munmap(evlist);
917 perf_evlist__close(evlist);
919 perf_evlist__delete_maps(evlist);
921 perf_evlist__delete(evlist);
926 static int trace__replay(struct trace *trace)
928 const struct perf_evsel_str_handler handlers[] = {
929 { "raw_syscalls:sys_enter", trace__sys_enter, },
930 { "raw_syscalls:sys_exit", trace__sys_exit, },
933 struct perf_session *session;
936 trace->tool.sample = trace__process_sample;
937 trace->tool.mmap = perf_event__process_mmap;
938 trace->tool.comm = perf_event__process_comm;
939 trace->tool.exit = perf_event__process_exit;
940 trace->tool.fork = perf_event__process_fork;
941 trace->tool.attr = perf_event__process_attr;
942 trace->tool.tracing_data = perf_event__process_tracing_data;
943 trace->tool.build_id = perf_event__process_build_id;
945 trace->tool.ordered_samples = true;
946 trace->tool.ordering_requires_timestamps = true;
948 /* add tid to output */
949 trace->multiple_threads = true;
951 if (symbol__init() < 0)
954 session = perf_session__new(input_name, O_RDONLY, 0, false,
959 err = perf_session__set_tracepoints_handlers(session, handlers);
963 if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
964 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
968 if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
969 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
973 err = parse_target_str(trace);
979 err = perf_session__process_events(session, &trace->tool);
981 pr_err("Failed to process events, error %d", err);
984 perf_session__delete(session);
989 static size_t trace__fprintf_threads_header(FILE *fp)
993 printed = fprintf(fp, "\n _____________________________________________________________________\n");
994 printed += fprintf(fp," __) Summary of events (__\n\n");
995 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
996 printed += fprintf(fp," _____________________________________________________________________\n\n");
1001 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1003 size_t printed = trace__fprintf_threads_header(fp);
1006 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1007 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1008 struct thread_trace *ttrace = thread->priv;
1015 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1017 color = PERF_COLOR_NORMAL;
1019 color = PERF_COLOR_RED;
1020 else if (ratio > 25.0)
1021 color = PERF_COLOR_GREEN;
1022 else if (ratio > 5.0)
1023 color = PERF_COLOR_YELLOW;
1025 printed += color_fprintf(fp, color, "%20s", thread->comm);
1026 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1027 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1028 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1034 static int trace__set_duration(const struct option *opt, const char *str,
1035 int unset __maybe_unused)
1037 struct trace *trace = opt->value;
1039 trace->duration_filter = atof(str);
1043 static int trace__open_output(struct trace *trace, const char *filename)
1047 if (!stat(filename, &st) && st.st_size) {
1048 char oldname[PATH_MAX];
1050 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1052 rename(filename, oldname);
1055 trace->output = fopen(filename, "w");
1057 return trace->output == NULL ? -errno : 0;
1060 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1062 const char * const trace_usage[] = {
1063 "perf trace [<options>] [<command>]",
1064 "perf trace [<options>] -- <command> [<options>]",
1067 struct trace trace = {
1068 .audit_machine = audit_detect_machine(),
1077 .user_freq = UINT_MAX,
1078 .user_interval = ULLONG_MAX,
1084 const char *output_name = NULL;
1085 const char *ev_qualifier_str = NULL;
1086 const struct option trace_options[] = {
1087 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1088 "list of events to trace"),
1089 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1090 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
1091 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1092 "trace events on existing process id"),
1093 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
1094 "trace events on existing thread id"),
1095 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
1096 "system-wide collection from all CPUs"),
1097 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
1098 "list of cpus to monitor"),
1099 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
1100 "child tasks do not inherit counters"),
1101 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
1102 "number of mmap data pages"),
1103 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
1105 OPT_CALLBACK(0, "duration", &trace, "float",
1106 "show only events with duration > N.M ms",
1107 trace__set_duration),
1108 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
1109 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1115 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
1117 if (output_name != NULL) {
1118 err = trace__open_output(&trace, output_name);
1120 perror("failed to create output file");
1125 if (ev_qualifier_str != NULL) {
1126 const char *s = ev_qualifier_str;
1128 trace.not_ev_qualifier = *s == '!';
1129 if (trace.not_ev_qualifier)
1131 trace.ev_qualifier = strlist__new(true, s);
1132 if (trace.ev_qualifier == NULL) {
1133 fputs("Not enough memory to parse event qualifier",
1140 err = perf_target__validate(&trace.opts.target);
1142 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1143 fprintf(trace.output, "%s", bf);
1147 err = perf_target__parse_uid(&trace.opts.target);
1149 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1150 fprintf(trace.output, "%s", bf);
1154 if (!argc && perf_target__none(&trace.opts.target))
1155 trace.opts.target.system_wide = true;
1158 err = trace__replay(&trace);
1160 err = trace__run(&trace, argc, argv);
1162 if (trace.sched && !err)
1163 trace__fprintf_thread_summary(&trace, trace.output);
1166 if (output_name != NULL)
1167 fclose(trace.output);