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