Robert Richter | 4e31902 | 2013-06-11 17:29:18 +0200 | [diff] [blame] | 1 | #include <traceevent/event-parse.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 3 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 4 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 5 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 6 | #include "util/machine.h" |
| 7 | #include "util/thread.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 8 | #include "util/parse-options.h" |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 9 | #include "util/strlist.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 10 | #include "util/thread_map.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 11 | |
| 12 | #include <libaudit.h> |
| 13 | #include <stdlib.h> |
| 14 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 15 | static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, unsigned long arg) |
| 16 | { |
| 17 | return scnprintf(bf, size, "%#lx", arg); |
| 18 | } |
| 19 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 20 | static struct syscall_fmt { |
| 21 | const char *name; |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 22 | const char *alias; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 23 | bool errmsg; |
| 24 | bool timeout; |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 25 | bool hexret; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 26 | } syscall_fmts[] = { |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 27 | { .name = "access", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 28 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 29 | { .name = "brk", .hexret = true, }, |
| 30 | { .name = "mmap", .hexret = true, }, |
Arnaldo Carvalho de Melo | a14bb86 | 2013-07-30 16:38:23 -0300 | [diff] [blame] | 31 | { .name = "connect", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 32 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, |
| 33 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, |
| 34 | { .name = "futex", .errmsg = true, }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame^] | 35 | { .name = "lstat", .errmsg = true, .alias = "newlstat", }, |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 36 | { .name = "mmap", .hexret = true, }, |
| 37 | { .name = "mremap", .hexret = true, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 38 | { .name = "open", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 39 | { .name = "poll", .errmsg = true, .timeout = true, }, |
| 40 | { .name = "ppoll", .errmsg = true, .timeout = true, }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame^] | 41 | { .name = "pread", .errmsg = true, .alias = "pread64", }, |
| 42 | { .name = "pwrite", .errmsg = true, .alias = "pwrite64", }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 43 | { .name = "read", .errmsg = true, }, |
| 44 | { .name = "recvfrom", .errmsg = true, }, |
| 45 | { .name = "select", .errmsg = true, .timeout = true, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 46 | { .name = "socket", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 47 | { .name = "stat", .errmsg = true, .alias = "newstat", }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame^] | 48 | { .name = "uname", .errmsg = true, .alias = "newuname", }, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static int syscall_fmt__cmp(const void *name, const void *fmtp) |
| 52 | { |
| 53 | const struct syscall_fmt *fmt = fmtp; |
| 54 | return strcmp(name, fmt->name); |
| 55 | } |
| 56 | |
| 57 | static struct syscall_fmt *syscall_fmt__find(const char *name) |
| 58 | { |
| 59 | const int nmemb = ARRAY_SIZE(syscall_fmts); |
| 60 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); |
| 61 | } |
| 62 | |
| 63 | struct syscall { |
| 64 | struct event_format *tp_format; |
| 65 | const char *name; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 66 | bool filtered; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 67 | struct syscall_fmt *fmt; |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 68 | size_t (**arg_scnprintf)(char *bf, size_t size, unsigned long arg); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 69 | }; |
| 70 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 71 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
| 72 | { |
| 73 | double duration = (double)t / NSEC_PER_MSEC; |
| 74 | size_t printed = fprintf(fp, "("); |
| 75 | |
| 76 | if (duration >= 1.0) |
| 77 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); |
| 78 | else if (duration >= 0.01) |
| 79 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); |
| 80 | else |
| 81 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 82 | return printed + fprintf(fp, "): "); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 83 | } |
| 84 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 85 | struct thread_trace { |
| 86 | u64 entry_time; |
| 87 | u64 exit_time; |
| 88 | bool entry_pending; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 89 | unsigned long nr_events; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 90 | char *entry_str; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 91 | double runtime_ms; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static struct thread_trace *thread_trace__new(void) |
| 95 | { |
| 96 | return zalloc(sizeof(struct thread_trace)); |
| 97 | } |
| 98 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 99 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 100 | { |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 101 | struct thread_trace *ttrace; |
| 102 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 103 | if (thread == NULL) |
| 104 | goto fail; |
| 105 | |
| 106 | if (thread->priv == NULL) |
| 107 | thread->priv = thread_trace__new(); |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 108 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 109 | if (thread->priv == NULL) |
| 110 | goto fail; |
| 111 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 112 | ttrace = thread->priv; |
| 113 | ++ttrace->nr_events; |
| 114 | |
| 115 | return ttrace; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 116 | fail: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 117 | color_fprintf(fp, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 118 | "WARNING: not enough memory, dropping samples!\n"); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 122 | struct trace { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 123 | struct perf_tool tool; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 124 | int audit_machine; |
| 125 | struct { |
| 126 | int max; |
| 127 | struct syscall *table; |
| 128 | } syscalls; |
| 129 | struct perf_record_opts opts; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 130 | struct machine host; |
| 131 | u64 base_time; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 132 | FILE *output; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 133 | unsigned long nr_events; |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 134 | struct strlist *ev_qualifier; |
| 135 | bool not_ev_qualifier; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 136 | bool sched; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 137 | bool multiple_threads; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 138 | double duration_filter; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 139 | double runtime_ms; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 140 | }; |
| 141 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 142 | static bool trace__filter_duration(struct trace *trace, double t) |
| 143 | { |
| 144 | return t < (trace->duration_filter * NSEC_PER_MSEC); |
| 145 | } |
| 146 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 147 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
| 148 | { |
| 149 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; |
| 150 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 151 | return fprintf(fp, "%10.3f ", ts); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 152 | } |
| 153 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 154 | static bool done = false; |
| 155 | |
| 156 | static void sig_handler(int sig __maybe_unused) |
| 157 | { |
| 158 | done = true; |
| 159 | } |
| 160 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 161 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 162 | u64 duration, u64 tstamp, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 163 | { |
| 164 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 165 | printed += fprintf_duration(duration, fp); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 166 | |
| 167 | if (trace->multiple_threads) |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 168 | printed += fprintf(fp, "%d ", thread->tid); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 169 | |
| 170 | return printed; |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 173 | static int trace__process_event(struct trace *trace, struct machine *machine, |
| 174 | union perf_event *event) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 175 | { |
| 176 | int ret = 0; |
| 177 | |
| 178 | switch (event->header.type) { |
| 179 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 180 | color_fprintf(trace->output, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 181 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
| 182 | ret = machine__process_lost_event(machine, event); |
| 183 | default: |
| 184 | ret = machine__process_event(machine, event); |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 191 | static int trace__tool_process(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 192 | union perf_event *event, |
| 193 | struct perf_sample *sample __maybe_unused, |
| 194 | struct machine *machine) |
| 195 | { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 196 | struct trace *trace = container_of(tool, struct trace, tool); |
| 197 | return trace__process_event(trace, machine, event); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) |
| 201 | { |
| 202 | int err = symbol__init(); |
| 203 | |
| 204 | if (err) |
| 205 | return err; |
| 206 | |
| 207 | machine__init(&trace->host, "", HOST_KERNEL_ID); |
| 208 | machine__create_kernel_maps(&trace->host); |
| 209 | |
| 210 | if (perf_target__has_task(&trace->opts.target)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 211 | err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 212 | trace__tool_process, |
| 213 | &trace->host); |
| 214 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 215 | err = perf_event__synthesize_threads(&trace->tool, trace__tool_process, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 216 | &trace->host); |
| 217 | } |
| 218 | |
| 219 | if (err) |
| 220 | symbol__exit(); |
| 221 | |
| 222 | return err; |
| 223 | } |
| 224 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 225 | static int syscall__set_arg_fmts(struct syscall *sc) |
| 226 | { |
| 227 | struct format_field *field; |
| 228 | int idx = 0; |
| 229 | |
| 230 | sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *)); |
| 231 | if (sc->arg_scnprintf == NULL) |
| 232 | return -1; |
| 233 | |
| 234 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { |
| 235 | if (field->flags & FIELD_IS_POINTER) |
| 236 | sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex; |
| 237 | ++idx; |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 243 | static int trace__read_syscall_info(struct trace *trace, int id) |
| 244 | { |
| 245 | char tp_name[128]; |
| 246 | struct syscall *sc; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 247 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
| 248 | |
| 249 | if (name == NULL) |
| 250 | return -1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 251 | |
| 252 | if (id > trace->syscalls.max) { |
| 253 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); |
| 254 | |
| 255 | if (nsyscalls == NULL) |
| 256 | return -1; |
| 257 | |
| 258 | if (trace->syscalls.max != -1) { |
| 259 | memset(nsyscalls + trace->syscalls.max + 1, 0, |
| 260 | (id - trace->syscalls.max) * sizeof(*sc)); |
| 261 | } else { |
| 262 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); |
| 263 | } |
| 264 | |
| 265 | trace->syscalls.table = nsyscalls; |
| 266 | trace->syscalls.max = id; |
| 267 | } |
| 268 | |
| 269 | sc = trace->syscalls.table + id; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 270 | sc->name = name; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 271 | |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 272 | if (trace->ev_qualifier) { |
| 273 | bool in = strlist__find(trace->ev_qualifier, name) != NULL; |
| 274 | |
| 275 | if (!(in ^ trace->not_ev_qualifier)) { |
| 276 | sc->filtered = true; |
| 277 | /* |
| 278 | * No need to do read tracepoint information since this will be |
| 279 | * filtered out. |
| 280 | */ |
| 281 | return 0; |
| 282 | } |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 283 | } |
| 284 | |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 285 | sc->fmt = syscall_fmt__find(sc->name); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 286 | |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 287 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
| 288 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 289 | |
| 290 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { |
| 291 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
| 292 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 293 | } |
| 294 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 295 | if (sc->tp_format == NULL) |
| 296 | return -1; |
| 297 | |
| 298 | return syscall__set_arg_fmts(sc); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 299 | } |
| 300 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 301 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
| 302 | unsigned long *args) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 303 | { |
| 304 | int i = 0; |
| 305 | size_t printed = 0; |
| 306 | |
| 307 | if (sc->tp_format != NULL) { |
| 308 | struct format_field *field; |
| 309 | |
| 310 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 311 | printed += scnprintf(bf + printed, size - printed, |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 312 | "%s%s: ", printed ? ", " : "", field->name); |
| 313 | |
| 314 | if (sc->arg_scnprintf && sc->arg_scnprintf[i]) |
| 315 | printed += sc->arg_scnprintf[i](bf + printed, size - printed, args[i]); |
| 316 | else |
| 317 | printed += scnprintf(bf + printed, size - printed, |
| 318 | "%ld", args[i]); |
| 319 | ++i; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 320 | } |
| 321 | } else { |
| 322 | while (i < 6) { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 323 | printed += scnprintf(bf + printed, size - printed, |
| 324 | "%sarg%d: %ld", |
| 325 | printed ? ", " : "", i, args[i]); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 326 | ++i; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return printed; |
| 331 | } |
| 332 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 333 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
| 334 | struct perf_sample *sample); |
| 335 | |
| 336 | static struct syscall *trace__syscall_info(struct trace *trace, |
| 337 | struct perf_evsel *evsel, |
| 338 | struct perf_sample *sample) |
| 339 | { |
| 340 | int id = perf_evsel__intval(evsel, sample, "id"); |
| 341 | |
| 342 | if (id < 0) { |
Arnaldo Carvalho de Melo | adaa18b | 2013-08-22 17:55:25 -0300 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried |
| 346 | * before that, leaving at a higher verbosity level till that is |
| 347 | * explained. Reproduced with plain ftrace with: |
| 348 | * |
| 349 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable |
| 350 | * grep "NR -1 " /t/trace_pipe |
| 351 | * |
| 352 | * After generating some load on the machine. |
| 353 | */ |
| 354 | if (verbose > 1) { |
| 355 | static u64 n; |
| 356 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", |
| 357 | id, perf_evsel__name(evsel), ++n); |
| 358 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && |
| 363 | trace__read_syscall_info(trace, id)) |
| 364 | goto out_cant_read; |
| 365 | |
| 366 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) |
| 367 | goto out_cant_read; |
| 368 | |
| 369 | return &trace->syscalls.table[id]; |
| 370 | |
| 371 | out_cant_read: |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 372 | if (verbose) { |
| 373 | fprintf(trace->output, "Problems reading syscall %d", id); |
| 374 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) |
| 375 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); |
| 376 | fputs(" information\n", trace->output); |
| 377 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
| 382 | struct perf_sample *sample) |
| 383 | { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 384 | char *msg; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 385 | void *args; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 386 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 387 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 388 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 389 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 390 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 391 | if (sc == NULL) |
| 392 | return -1; |
| 393 | |
| 394 | if (sc->filtered) |
| 395 | return 0; |
| 396 | |
| 397 | thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 398 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 399 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 400 | return -1; |
| 401 | |
| 402 | args = perf_evsel__rawptr(evsel, sample, "args"); |
| 403 | if (args == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 404 | fprintf(trace->output, "Problems reading syscall arguments\n"); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 408 | ttrace = thread->priv; |
| 409 | |
| 410 | if (ttrace->entry_str == NULL) { |
| 411 | ttrace->entry_str = malloc(1024); |
| 412 | if (!ttrace->entry_str) |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | ttrace->entry_time = sample->time; |
| 417 | msg = ttrace->entry_str; |
| 418 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); |
| 419 | |
| 420 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); |
| 421 | |
| 422 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 423 | if (!trace->duration_filter) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 424 | trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); |
| 425 | fprintf(trace->output, "%-70s\n", ttrace->entry_str); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 426 | } |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 427 | } else |
| 428 | ttrace->entry_pending = true; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
| 434 | struct perf_sample *sample) |
| 435 | { |
| 436 | int ret; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 437 | u64 duration = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 438 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 439 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 440 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 441 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 442 | if (sc == NULL) |
| 443 | return -1; |
| 444 | |
| 445 | if (sc->filtered) |
| 446 | return 0; |
| 447 | |
| 448 | thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 449 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 450 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 451 | return -1; |
| 452 | |
| 453 | ret = perf_evsel__intval(evsel, sample, "ret"); |
| 454 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 455 | ttrace = thread->priv; |
| 456 | |
| 457 | ttrace->exit_time = sample->time; |
| 458 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 459 | if (ttrace->entry_time) { |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 460 | duration = sample->time - ttrace->entry_time; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 461 | if (trace__filter_duration(trace, duration)) |
| 462 | goto out; |
| 463 | } else if (trace->duration_filter) |
| 464 | goto out; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 465 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 466 | trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 467 | |
| 468 | if (ttrace->entry_pending) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 469 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 470 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 471 | fprintf(trace->output, " ... ["); |
| 472 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); |
| 473 | fprintf(trace->output, "]: %s()", sc->name); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 474 | } |
| 475 | |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 476 | if (sc->fmt == NULL) { |
| 477 | signed_print: |
| 478 | fprintf(trace->output, ") = %d", ret); |
| 479 | } else if (ret < 0 && sc->fmt->errmsg) { |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 480 | char bf[256]; |
| 481 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), |
| 482 | *e = audit_errno_to_name(-ret); |
| 483 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 484 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 485 | } else if (ret == 0 && sc->fmt->timeout) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 486 | fprintf(trace->output, ") = 0 Timeout"); |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 487 | else if (sc->fmt->hexret) |
| 488 | fprintf(trace->output, ") = %#x", ret); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 489 | else |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 490 | goto signed_print; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 491 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 492 | fputc('\n', trace->output); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 493 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 494 | ttrace->entry_pending = false; |
| 495 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 499 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
| 500 | struct perf_sample *sample) |
| 501 | { |
| 502 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
| 503 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; |
| 504 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 505 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 506 | |
| 507 | if (ttrace == NULL) |
| 508 | goto out_dump; |
| 509 | |
| 510 | ttrace->runtime_ms += runtime_ms; |
| 511 | trace->runtime_ms += runtime_ms; |
| 512 | return 0; |
| 513 | |
| 514 | out_dump: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 515 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 516 | evsel->name, |
| 517 | perf_evsel__strval(evsel, sample, "comm"), |
| 518 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), |
| 519 | runtime, |
| 520 | perf_evsel__intval(evsel, sample, "vruntime")); |
| 521 | return 0; |
| 522 | } |
| 523 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 524 | static int trace__run(struct trace *trace, int argc, const char **argv) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 525 | { |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 526 | struct perf_evlist *evlist = perf_evlist__new(); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 527 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 528 | int err = -1, i; |
| 529 | unsigned long before; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 530 | const bool forks = argc > 0; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 531 | |
| 532 | if (evlist == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 533 | fprintf(trace->output, "Not enough memory to run!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 534 | goto out; |
| 535 | } |
| 536 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 537 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
| 538 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 539 | fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 540 | goto out_delete_evlist; |
| 541 | } |
| 542 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 543 | if (trace->sched && |
| 544 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
| 545 | trace__sched_stat_runtime)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 546 | fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n"); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 547 | goto out_delete_evlist; |
| 548 | } |
| 549 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 550 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
| 551 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 552 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 553 | goto out_delete_evlist; |
| 554 | } |
| 555 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 556 | err = trace__symbols_init(trace, evlist); |
| 557 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 558 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 559 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 560 | } |
| 561 | |
Arnaldo Carvalho de Melo | f77a951 | 2012-12-10 16:41:31 -0300 | [diff] [blame] | 562 | perf_evlist__config(evlist, &trace->opts); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 563 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 564 | signal(SIGCHLD, sig_handler); |
| 565 | signal(SIGINT, sig_handler); |
| 566 | |
| 567 | if (forks) { |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 568 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 569 | argv, false, false); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 570 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 571 | fprintf(trace->output, "Couldn't run the workload!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 572 | goto out_delete_maps; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 576 | err = perf_evlist__open(evlist); |
| 577 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 578 | fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 579 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | err = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 583 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 584 | fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 585 | goto out_close_evlist; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | perf_evlist__enable(evlist); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 589 | |
| 590 | if (forks) |
| 591 | perf_evlist__start_workload(evlist); |
| 592 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 593 | trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 594 | again: |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 595 | before = trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 596 | |
| 597 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 598 | union perf_event *event; |
| 599 | |
| 600 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 601 | const u32 type = event->header.type; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 602 | tracepoint_handler handler; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 603 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 604 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 605 | ++trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 606 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 607 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 608 | if (err) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 609 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 610 | continue; |
| 611 | } |
| 612 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 613 | if (trace->base_time == 0) |
| 614 | trace->base_time = sample.time; |
| 615 | |
| 616 | if (type != PERF_RECORD_SAMPLE) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 617 | trace__process_event(trace, &trace->host, event); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 618 | continue; |
| 619 | } |
| 620 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 621 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 622 | if (evsel == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 623 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 624 | continue; |
| 625 | } |
| 626 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 627 | if (sample.raw_data == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 628 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 629 | perf_evsel__name(evsel), sample.tid, |
| 630 | sample.cpu, sample.raw_size); |
| 631 | continue; |
| 632 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 633 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 634 | handler = evsel->handler.func; |
| 635 | handler(trace, evsel, &sample); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 639 | if (trace->nr_events == before) { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 640 | if (done) |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 641 | goto out_unmap_evlist; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 642 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 643 | poll(evlist->pollfd, evlist->nr_fds, -1); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | if (done) |
| 647 | perf_evlist__disable(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 648 | |
| 649 | goto again; |
| 650 | |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 651 | out_unmap_evlist: |
| 652 | perf_evlist__munmap(evlist); |
| 653 | out_close_evlist: |
| 654 | perf_evlist__close(evlist); |
| 655 | out_delete_maps: |
| 656 | perf_evlist__delete_maps(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 657 | out_delete_evlist: |
| 658 | perf_evlist__delete(evlist); |
| 659 | out: |
| 660 | return err; |
| 661 | } |
| 662 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 663 | static size_t trace__fprintf_threads_header(FILE *fp) |
| 664 | { |
| 665 | size_t printed; |
| 666 | |
| 667 | printed = fprintf(fp, "\n _____________________________________________________________________\n"); |
| 668 | printed += fprintf(fp," __) Summary of events (__\n\n"); |
| 669 | printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); |
| 670 | printed += fprintf(fp," _____________________________________________________________________\n\n"); |
| 671 | |
| 672 | return printed; |
| 673 | } |
| 674 | |
| 675 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
| 676 | { |
| 677 | size_t printed = trace__fprintf_threads_header(fp); |
| 678 | struct rb_node *nd; |
| 679 | |
| 680 | for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) { |
| 681 | struct thread *thread = rb_entry(nd, struct thread, rb_node); |
| 682 | struct thread_trace *ttrace = thread->priv; |
| 683 | const char *color; |
| 684 | double ratio; |
| 685 | |
| 686 | if (ttrace == NULL) |
| 687 | continue; |
| 688 | |
| 689 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; |
| 690 | |
| 691 | color = PERF_COLOR_NORMAL; |
| 692 | if (ratio > 50.0) |
| 693 | color = PERF_COLOR_RED; |
| 694 | else if (ratio > 25.0) |
| 695 | color = PERF_COLOR_GREEN; |
| 696 | else if (ratio > 5.0) |
| 697 | color = PERF_COLOR_YELLOW; |
| 698 | |
| 699 | printed += color_fprintf(fp, color, "%20s", thread->comm); |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 700 | printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 701 | printed += color_fprintf(fp, color, "%5.1f%%", ratio); |
| 702 | printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); |
| 703 | } |
| 704 | |
| 705 | return printed; |
| 706 | } |
| 707 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 708 | static int trace__set_duration(const struct option *opt, const char *str, |
| 709 | int unset __maybe_unused) |
| 710 | { |
| 711 | struct trace *trace = opt->value; |
| 712 | |
| 713 | trace->duration_filter = atof(str); |
| 714 | return 0; |
| 715 | } |
| 716 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 717 | static int trace__open_output(struct trace *trace, const char *filename) |
| 718 | { |
| 719 | struct stat st; |
| 720 | |
| 721 | if (!stat(filename, &st) && st.st_size) { |
| 722 | char oldname[PATH_MAX]; |
| 723 | |
| 724 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); |
| 725 | unlink(oldname); |
| 726 | rename(filename, oldname); |
| 727 | } |
| 728 | |
| 729 | trace->output = fopen(filename, "w"); |
| 730 | |
| 731 | return trace->output == NULL ? -errno : 0; |
| 732 | } |
| 733 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 734 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
| 735 | { |
| 736 | const char * const trace_usage[] = { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 737 | "perf trace [<options>] [<command>]", |
| 738 | "perf trace [<options>] -- <command> [<options>]", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 739 | NULL |
| 740 | }; |
| 741 | struct trace trace = { |
| 742 | .audit_machine = audit_detect_machine(), |
| 743 | .syscalls = { |
| 744 | . max = -1, |
| 745 | }, |
| 746 | .opts = { |
| 747 | .target = { |
| 748 | .uid = UINT_MAX, |
| 749 | .uses_mmap = true, |
| 750 | }, |
| 751 | .user_freq = UINT_MAX, |
| 752 | .user_interval = ULLONG_MAX, |
| 753 | .no_delay = true, |
| 754 | .mmap_pages = 1024, |
| 755 | }, |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 756 | .output = stdout, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 757 | }; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 758 | const char *output_name = NULL; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 759 | const char *ev_qualifier_str = NULL; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 760 | const struct option trace_options[] = { |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 761 | OPT_STRING('e', "expr", &ev_qualifier_str, "expr", |
| 762 | "list of events to trace"), |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 763 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 764 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
| 765 | "trace events on existing process id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 766 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 767 | "trace events on existing thread id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 768 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 769 | "system-wide collection from all CPUs"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 770 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 771 | "list of cpus to monitor"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 772 | OPT_BOOLEAN('i', "no-inherit", &trace.opts.no_inherit, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 773 | "child tasks do not inherit counters"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 774 | OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 775 | "number of mmap data pages"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 776 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 777 | "user to profile"), |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 778 | OPT_CALLBACK(0, "duration", &trace, "float", |
| 779 | "show only events with duration > N.M ms", |
| 780 | trace__set_duration), |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 781 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 782 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 783 | OPT_END() |
| 784 | }; |
| 785 | int err; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 786 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 787 | |
| 788 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 789 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 790 | if (output_name != NULL) { |
| 791 | err = trace__open_output(&trace, output_name); |
| 792 | if (err < 0) { |
| 793 | perror("failed to create output file"); |
| 794 | goto out; |
| 795 | } |
| 796 | } |
| 797 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 798 | if (ev_qualifier_str != NULL) { |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 799 | const char *s = ev_qualifier_str; |
| 800 | |
| 801 | trace.not_ev_qualifier = *s == '!'; |
| 802 | if (trace.not_ev_qualifier) |
| 803 | ++s; |
| 804 | trace.ev_qualifier = strlist__new(true, s); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 805 | if (trace.ev_qualifier == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 806 | fputs("Not enough memory to parse event qualifier", |
| 807 | trace.output); |
| 808 | err = -ENOMEM; |
| 809 | goto out_close; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 813 | err = perf_target__validate(&trace.opts.target); |
| 814 | if (err) { |
| 815 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 816 | fprintf(trace.output, "%s", bf); |
| 817 | goto out_close; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 818 | } |
| 819 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 820 | err = perf_target__parse_uid(&trace.opts.target); |
| 821 | if (err) { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 822 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 823 | fprintf(trace.output, "%s", bf); |
| 824 | goto out_close; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 825 | } |
| 826 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 827 | if (!argc && perf_target__none(&trace.opts.target)) |
Namhyung Kim | ee76120 | 2012-10-05 14:02:14 +0900 | [diff] [blame] | 828 | trace.opts.target.system_wide = true; |
| 829 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 830 | err = trace__run(&trace, argc, argv); |
| 831 | |
| 832 | if (trace.sched && !err) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 833 | trace__fprintf_thread_summary(&trace, trace.output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 834 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 835 | out_close: |
| 836 | if (output_name != NULL) |
| 837 | fclose(trace.output); |
| 838 | out: |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 839 | return err; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 840 | } |