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/thread.h"
8 #include "util/parse-options.h"
9 #include "util/strlist.h"
10 #include "util/thread_map.h"
16 static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, unsigned long arg)
18 return scnprintf(bf, size, "%#lx", arg);
21 #define SCA_HEX syscall_arg__scnprintf_hex
23 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size, unsigned long arg)
25 int printed = 0, prot = arg;
27 if (prot == PROT_NONE)
28 return scnprintf(bf, size, "NONE");
29 #define P_MMAP_PROT(n) \
30 if (prot & PROT_##n) { \
31 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
41 P_MMAP_PROT(GROWSDOWN);
46 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
51 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
53 static struct syscall_fmt {
56 size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg);
61 { .name = "access", .errmsg = true, },
62 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
63 { .name = "brk", .hexret = true,
64 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
65 { .name = "mmap", .hexret = true, },
66 { .name = "connect", .errmsg = true, },
67 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
68 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
69 { .name = "futex", .errmsg = true, },
70 { .name = "ioctl", .errmsg = true,
71 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
72 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
73 { .name = "mmap", .hexret = true,
74 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
75 [2] = SCA_MMAP_PROT, /* prot */ }, },
76 { .name = "mprotect", .errmsg = true,
77 .arg_scnprintf = { [0] = SCA_HEX, /* start */
78 [2] = SCA_MMAP_PROT, /* prot */ }, },
79 { .name = "mremap", .hexret = true,
80 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
81 [4] = SCA_HEX, /* new_addr */ }, },
82 { .name = "munmap", .errmsg = true,
83 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
84 { .name = "open", .errmsg = true, },
85 { .name = "poll", .errmsg = true, .timeout = true, },
86 { .name = "ppoll", .errmsg = true, .timeout = true, },
87 { .name = "pread", .errmsg = true, .alias = "pread64", },
88 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
89 { .name = "read", .errmsg = true, },
90 { .name = "recvfrom", .errmsg = true, },
91 { .name = "select", .errmsg = true, .timeout = true, },
92 { .name = "socket", .errmsg = true, },
93 { .name = "stat", .errmsg = true, .alias = "newstat", },
94 { .name = "uname", .errmsg = true, .alias = "newuname", },
97 static int syscall_fmt__cmp(const void *name, const void *fmtp)
99 const struct syscall_fmt *fmt = fmtp;
100 return strcmp(name, fmt->name);
103 static struct syscall_fmt *syscall_fmt__find(const char *name)
105 const int nmemb = ARRAY_SIZE(syscall_fmts);
106 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
110 struct event_format *tp_format;
113 struct syscall_fmt *fmt;
114 size_t (**arg_scnprintf)(char *bf, size_t size, unsigned long arg);
117 static size_t fprintf_duration(unsigned long t, FILE *fp)
119 double duration = (double)t / NSEC_PER_MSEC;
120 size_t printed = fprintf(fp, "(");
123 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
124 else if (duration >= 0.01)
125 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
127 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
128 return printed + fprintf(fp, "): ");
131 struct thread_trace {
135 unsigned long nr_events;
140 static struct thread_trace *thread_trace__new(void)
142 return zalloc(sizeof(struct thread_trace));
145 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
147 struct thread_trace *ttrace;
152 if (thread->priv == NULL)
153 thread->priv = thread_trace__new();
155 if (thread->priv == NULL)
158 ttrace = thread->priv;
163 color_fprintf(fp, PERF_COLOR_RED,
164 "WARNING: not enough memory, dropping samples!\n");
169 struct perf_tool tool;
173 struct syscall *table;
175 struct perf_record_opts opts;
179 unsigned long nr_events;
180 struct strlist *ev_qualifier;
181 bool not_ev_qualifier;
183 bool multiple_threads;
184 double duration_filter;
188 static bool trace__filter_duration(struct trace *trace, double t)
190 return t < (trace->duration_filter * NSEC_PER_MSEC);
193 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
195 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
197 return fprintf(fp, "%10.3f ", ts);
200 static bool done = false;
202 static void sig_handler(int sig __maybe_unused)
207 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
208 u64 duration, u64 tstamp, FILE *fp)
210 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
211 printed += fprintf_duration(duration, fp);
213 if (trace->multiple_threads)
214 printed += fprintf(fp, "%d ", thread->tid);
219 static int trace__process_event(struct trace *trace, struct machine *machine,
220 union perf_event *event)
224 switch (event->header.type) {
225 case PERF_RECORD_LOST:
226 color_fprintf(trace->output, PERF_COLOR_RED,
227 "LOST %" PRIu64 " events!\n", event->lost.lost);
228 ret = machine__process_lost_event(machine, event);
230 ret = machine__process_event(machine, event);
237 static int trace__tool_process(struct perf_tool *tool,
238 union perf_event *event,
239 struct perf_sample *sample __maybe_unused,
240 struct machine *machine)
242 struct trace *trace = container_of(tool, struct trace, tool);
243 return trace__process_event(trace, machine, event);
246 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
248 int err = symbol__init();
253 machine__init(&trace->host, "", HOST_KERNEL_ID);
254 machine__create_kernel_maps(&trace->host);
256 if (perf_target__has_task(&trace->opts.target)) {
257 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
261 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
271 static int syscall__set_arg_fmts(struct syscall *sc)
273 struct format_field *field;
276 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
277 if (sc->arg_scnprintf == NULL)
280 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
281 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
282 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
283 else if (field->flags & FIELD_IS_POINTER)
284 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
291 static int trace__read_syscall_info(struct trace *trace, int id)
295 const char *name = audit_syscall_to_name(id, trace->audit_machine);
300 if (id > trace->syscalls.max) {
301 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
303 if (nsyscalls == NULL)
306 if (trace->syscalls.max != -1) {
307 memset(nsyscalls + trace->syscalls.max + 1, 0,
308 (id - trace->syscalls.max) * sizeof(*sc));
310 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
313 trace->syscalls.table = nsyscalls;
314 trace->syscalls.max = id;
317 sc = trace->syscalls.table + id;
320 if (trace->ev_qualifier) {
321 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
323 if (!(in ^ trace->not_ev_qualifier)) {
326 * No need to do read tracepoint information since this will be
333 sc->fmt = syscall_fmt__find(sc->name);
335 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
336 sc->tp_format = event_format__new("syscalls", tp_name);
338 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
339 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
340 sc->tp_format = event_format__new("syscalls", tp_name);
343 if (sc->tp_format == NULL)
346 return syscall__set_arg_fmts(sc);
349 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
355 if (sc->tp_format != NULL) {
356 struct format_field *field;
358 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
359 printed += scnprintf(bf + printed, size - printed,
360 "%s%s: ", printed ? ", " : "", field->name);
362 if (sc->arg_scnprintf && sc->arg_scnprintf[i])
363 printed += sc->arg_scnprintf[i](bf + printed, size - printed, args[i]);
365 printed += scnprintf(bf + printed, size - printed,
371 printed += scnprintf(bf + printed, size - printed,
373 printed ? ", " : "", i, args[i]);
381 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
382 struct perf_sample *sample);
384 static struct syscall *trace__syscall_info(struct trace *trace,
385 struct perf_evsel *evsel,
386 struct perf_sample *sample)
388 int id = perf_evsel__intval(evsel, sample, "id");
393 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
394 * before that, leaving at a higher verbosity level till that is
395 * explained. Reproduced with plain ftrace with:
397 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
398 * grep "NR -1 " /t/trace_pipe
400 * After generating some load on the machine.
404 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
405 id, perf_evsel__name(evsel), ++n);
410 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
411 trace__read_syscall_info(trace, id))
414 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
417 return &trace->syscalls.table[id];
421 fprintf(trace->output, "Problems reading syscall %d", id);
422 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
423 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
424 fputs(" information\n", trace->output);
429 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
430 struct perf_sample *sample)
435 struct thread *thread;
436 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
437 struct thread_trace *ttrace;
445 thread = machine__findnew_thread(&trace->host, sample->tid);
446 ttrace = thread__trace(thread, trace->output);
450 args = perf_evsel__rawptr(evsel, sample, "args");
452 fprintf(trace->output, "Problems reading syscall arguments\n");
456 ttrace = thread->priv;
458 if (ttrace->entry_str == NULL) {
459 ttrace->entry_str = malloc(1024);
460 if (!ttrace->entry_str)
464 ttrace->entry_time = sample->time;
465 msg = ttrace->entry_str;
466 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
468 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
470 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
471 if (!trace->duration_filter) {
472 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
473 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
476 ttrace->entry_pending = true;
481 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
482 struct perf_sample *sample)
486 struct thread *thread;
487 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
488 struct thread_trace *ttrace;
496 thread = machine__findnew_thread(&trace->host, sample->tid);
497 ttrace = thread__trace(thread, trace->output);
501 ret = perf_evsel__intval(evsel, sample, "ret");
503 ttrace = thread->priv;
505 ttrace->exit_time = sample->time;
507 if (ttrace->entry_time) {
508 duration = sample->time - ttrace->entry_time;
509 if (trace__filter_duration(trace, duration))
511 } else if (trace->duration_filter)
514 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
516 if (ttrace->entry_pending) {
517 fprintf(trace->output, "%-70s", ttrace->entry_str);
519 fprintf(trace->output, " ... [");
520 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
521 fprintf(trace->output, "]: %s()", sc->name);
524 if (sc->fmt == NULL) {
526 fprintf(trace->output, ") = %d", ret);
527 } else if (ret < 0 && sc->fmt->errmsg) {
529 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
530 *e = audit_errno_to_name(-ret);
532 fprintf(trace->output, ") = -1 %s %s", e, emsg);
533 } else if (ret == 0 && sc->fmt->timeout)
534 fprintf(trace->output, ") = 0 Timeout");
535 else if (sc->fmt->hexret)
536 fprintf(trace->output, ") = %#x", ret);
540 fputc('\n', trace->output);
542 ttrace->entry_pending = false;
547 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
548 struct perf_sample *sample)
550 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
551 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
552 struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
553 struct thread_trace *ttrace = thread__trace(thread, trace->output);
558 ttrace->runtime_ms += runtime_ms;
559 trace->runtime_ms += runtime_ms;
563 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
565 perf_evsel__strval(evsel, sample, "comm"),
566 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
568 perf_evsel__intval(evsel, sample, "vruntime"));
572 static int trace__run(struct trace *trace, int argc, const char **argv)
574 struct perf_evlist *evlist = perf_evlist__new();
575 struct perf_evsel *evsel;
577 unsigned long before;
578 const bool forks = argc > 0;
580 if (evlist == NULL) {
581 fprintf(trace->output, "Not enough memory to run!\n");
585 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
586 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
587 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
588 goto out_delete_evlist;
592 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
593 trace__sched_stat_runtime)) {
594 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
595 goto out_delete_evlist;
598 err = perf_evlist__create_maps(evlist, &trace->opts.target);
600 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
601 goto out_delete_evlist;
604 err = trace__symbols_init(trace, evlist);
606 fprintf(trace->output, "Problems initializing symbol libraries!\n");
607 goto out_delete_maps;
610 perf_evlist__config(evlist, &trace->opts);
612 signal(SIGCHLD, sig_handler);
613 signal(SIGINT, sig_handler);
616 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
619 fprintf(trace->output, "Couldn't run the workload!\n");
620 goto out_delete_maps;
624 err = perf_evlist__open(evlist);
626 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
627 goto out_delete_maps;
630 err = perf_evlist__mmap(evlist, UINT_MAX, false);
632 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
633 goto out_close_evlist;
636 perf_evlist__enable(evlist);
639 perf_evlist__start_workload(evlist);
641 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
643 before = trace->nr_events;
645 for (i = 0; i < evlist->nr_mmaps; i++) {
646 union perf_event *event;
648 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
649 const u32 type = event->header.type;
650 tracepoint_handler handler;
651 struct perf_sample sample;
655 err = perf_evlist__parse_sample(evlist, event, &sample);
657 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
661 if (trace->base_time == 0)
662 trace->base_time = sample.time;
664 if (type != PERF_RECORD_SAMPLE) {
665 trace__process_event(trace, &trace->host, event);
669 evsel = perf_evlist__id2evsel(evlist, sample.id);
671 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
675 if (sample.raw_data == NULL) {
676 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
677 perf_evsel__name(evsel), sample.tid,
678 sample.cpu, sample.raw_size);
682 handler = evsel->handler.func;
683 handler(trace, evsel, &sample);
687 if (trace->nr_events == before) {
689 goto out_unmap_evlist;
691 poll(evlist->pollfd, evlist->nr_fds, -1);
695 perf_evlist__disable(evlist);
700 perf_evlist__munmap(evlist);
702 perf_evlist__close(evlist);
704 perf_evlist__delete_maps(evlist);
706 perf_evlist__delete(evlist);
711 static size_t trace__fprintf_threads_header(FILE *fp)
715 printed = fprintf(fp, "\n _____________________________________________________________________\n");
716 printed += fprintf(fp," __) Summary of events (__\n\n");
717 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
718 printed += fprintf(fp," _____________________________________________________________________\n\n");
723 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
725 size_t printed = trace__fprintf_threads_header(fp);
728 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
729 struct thread *thread = rb_entry(nd, struct thread, rb_node);
730 struct thread_trace *ttrace = thread->priv;
737 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
739 color = PERF_COLOR_NORMAL;
741 color = PERF_COLOR_RED;
742 else if (ratio > 25.0)
743 color = PERF_COLOR_GREEN;
744 else if (ratio > 5.0)
745 color = PERF_COLOR_YELLOW;
747 printed += color_fprintf(fp, color, "%20s", thread->comm);
748 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
749 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
750 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
756 static int trace__set_duration(const struct option *opt, const char *str,
757 int unset __maybe_unused)
759 struct trace *trace = opt->value;
761 trace->duration_filter = atof(str);
765 static int trace__open_output(struct trace *trace, const char *filename)
769 if (!stat(filename, &st) && st.st_size) {
770 char oldname[PATH_MAX];
772 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
774 rename(filename, oldname);
777 trace->output = fopen(filename, "w");
779 return trace->output == NULL ? -errno : 0;
782 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
784 const char * const trace_usage[] = {
785 "perf trace [<options>] [<command>]",
786 "perf trace [<options>] -- <command> [<options>]",
789 struct trace trace = {
790 .audit_machine = audit_detect_machine(),
799 .user_freq = UINT_MAX,
800 .user_interval = ULLONG_MAX,
806 const char *output_name = NULL;
807 const char *ev_qualifier_str = NULL;
808 const struct option trace_options[] = {
809 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
810 "list of events to trace"),
811 OPT_STRING('o', "output", &output_name, "file", "output file name"),
812 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
813 "trace events on existing process id"),
814 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
815 "trace events on existing thread id"),
816 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
817 "system-wide collection from all CPUs"),
818 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
819 "list of cpus to monitor"),
820 OPT_BOOLEAN('i', "no-inherit", &trace.opts.no_inherit,
821 "child tasks do not inherit counters"),
822 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
823 "number of mmap data pages"),
824 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
826 OPT_CALLBACK(0, "duration", &trace, "float",
827 "show only events with duration > N.M ms",
828 trace__set_duration),
829 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
830 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
836 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
838 if (output_name != NULL) {
839 err = trace__open_output(&trace, output_name);
841 perror("failed to create output file");
846 if (ev_qualifier_str != NULL) {
847 const char *s = ev_qualifier_str;
849 trace.not_ev_qualifier = *s == '!';
850 if (trace.not_ev_qualifier)
852 trace.ev_qualifier = strlist__new(true, s);
853 if (trace.ev_qualifier == NULL) {
854 fputs("Not enough memory to parse event qualifier",
861 err = perf_target__validate(&trace.opts.target);
863 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
864 fprintf(trace.output, "%s", bf);
868 err = perf_target__parse_uid(&trace.opts.target);
870 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
871 fprintf(trace.output, "%s", bf);
875 if (!argc && perf_target__none(&trace.opts.target))
876 trace.opts.target.system_wide = true;
878 err = trace__run(&trace, argc, argv);
880 if (trace.sched && !err)
881 trace__fprintf_thread_summary(&trace, trace.output);
884 if (output_name != NULL)
885 fclose(trace.output);