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