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 | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 301 | fprintf(trace->output, "Invalid syscall %d id, skipping...\n", id); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && |
| 306 | trace__read_syscall_info(trace, id)) |
| 307 | goto out_cant_read; |
| 308 | |
| 309 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) |
| 310 | goto out_cant_read; |
| 311 | |
| 312 | return &trace->syscalls.table[id]; |
| 313 | |
| 314 | out_cant_read: |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame^] | 315 | if (verbose) { |
| 316 | fprintf(trace->output, "Problems reading syscall %d", id); |
| 317 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) |
| 318 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); |
| 319 | fputs(" information\n", trace->output); |
| 320 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
| 325 | struct perf_sample *sample) |
| 326 | { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 327 | char *msg; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 328 | void *args; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 329 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 330 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 331 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 332 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 333 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 334 | if (sc == NULL) |
| 335 | return -1; |
| 336 | |
| 337 | if (sc->filtered) |
| 338 | return 0; |
| 339 | |
| 340 | thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 341 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 342 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 343 | return -1; |
| 344 | |
| 345 | args = perf_evsel__rawptr(evsel, sample, "args"); |
| 346 | if (args == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 347 | fprintf(trace->output, "Problems reading syscall arguments\n"); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 351 | ttrace = thread->priv; |
| 352 | |
| 353 | if (ttrace->entry_str == NULL) { |
| 354 | ttrace->entry_str = malloc(1024); |
| 355 | if (!ttrace->entry_str) |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | ttrace->entry_time = sample->time; |
| 360 | msg = ttrace->entry_str; |
| 361 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); |
| 362 | |
| 363 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); |
| 364 | |
| 365 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 366 | if (!trace->duration_filter) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 367 | trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); |
| 368 | fprintf(trace->output, "%-70s\n", ttrace->entry_str); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 369 | } |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 370 | } else |
| 371 | ttrace->entry_pending = true; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
| 377 | struct perf_sample *sample) |
| 378 | { |
| 379 | int ret; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 380 | u64 duration = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 381 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 382 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 383 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 384 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 385 | if (sc == NULL) |
| 386 | return -1; |
| 387 | |
| 388 | if (sc->filtered) |
| 389 | return 0; |
| 390 | |
| 391 | thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 392 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 393 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 394 | return -1; |
| 395 | |
| 396 | ret = perf_evsel__intval(evsel, sample, "ret"); |
| 397 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 398 | ttrace = thread->priv; |
| 399 | |
| 400 | ttrace->exit_time = sample->time; |
| 401 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 402 | if (ttrace->entry_time) { |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 403 | duration = sample->time - ttrace->entry_time; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 404 | if (trace__filter_duration(trace, duration)) |
| 405 | goto out; |
| 406 | } else if (trace->duration_filter) |
| 407 | goto out; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 408 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 409 | 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] | 410 | |
| 411 | if (ttrace->entry_pending) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 412 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 413 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 414 | fprintf(trace->output, " ... ["); |
| 415 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); |
| 416 | fprintf(trace->output, "]: %s()", sc->name); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 417 | } |
| 418 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 419 | if (ret < 0 && sc->fmt && sc->fmt->errmsg) { |
| 420 | char bf[256]; |
| 421 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), |
| 422 | *e = audit_errno_to_name(-ret); |
| 423 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 424 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 425 | } else if (ret == 0 && sc->fmt && sc->fmt->timeout) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 426 | fprintf(trace->output, ") = 0 Timeout"); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 427 | else |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 428 | fprintf(trace->output, ") = %d", ret); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 429 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 430 | fputc('\n', trace->output); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 431 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 432 | ttrace->entry_pending = false; |
| 433 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 437 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
| 438 | struct perf_sample *sample) |
| 439 | { |
| 440 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
| 441 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; |
| 442 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 443 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 444 | |
| 445 | if (ttrace == NULL) |
| 446 | goto out_dump; |
| 447 | |
| 448 | ttrace->runtime_ms += runtime_ms; |
| 449 | trace->runtime_ms += runtime_ms; |
| 450 | return 0; |
| 451 | |
| 452 | out_dump: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 453 | 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] | 454 | evsel->name, |
| 455 | perf_evsel__strval(evsel, sample, "comm"), |
| 456 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), |
| 457 | runtime, |
| 458 | perf_evsel__intval(evsel, sample, "vruntime")); |
| 459 | return 0; |
| 460 | } |
| 461 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 462 | 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] | 463 | { |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 464 | struct perf_evlist *evlist = perf_evlist__new(); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 465 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 466 | int err = -1, i; |
| 467 | unsigned long before; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 468 | const bool forks = argc > 0; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 469 | |
| 470 | if (evlist == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 471 | fprintf(trace->output, "Not enough memory to run!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 472 | goto out; |
| 473 | } |
| 474 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 475 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
| 476 | 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] | 477 | 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] | 478 | goto out_delete_evlist; |
| 479 | } |
| 480 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 481 | if (trace->sched && |
| 482 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
| 483 | trace__sched_stat_runtime)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 484 | 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] | 485 | goto out_delete_evlist; |
| 486 | } |
| 487 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 488 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
| 489 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 490 | 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] | 491 | goto out_delete_evlist; |
| 492 | } |
| 493 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 494 | err = trace__symbols_init(trace, evlist); |
| 495 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 496 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 497 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 498 | } |
| 499 | |
Arnaldo Carvalho de Melo | f77a951 | 2012-12-10 16:41:31 -0300 | [diff] [blame] | 500 | perf_evlist__config(evlist, &trace->opts); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 501 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 502 | signal(SIGCHLD, sig_handler); |
| 503 | signal(SIGINT, sig_handler); |
| 504 | |
| 505 | if (forks) { |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 506 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 507 | argv, false, false); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 508 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 509 | fprintf(trace->output, "Couldn't run the workload!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 510 | goto out_delete_maps; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 514 | err = perf_evlist__open(evlist); |
| 515 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 516 | fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 517 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | err = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 521 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 522 | fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 523 | goto out_close_evlist; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | perf_evlist__enable(evlist); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 527 | |
| 528 | if (forks) |
| 529 | perf_evlist__start_workload(evlist); |
| 530 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 531 | 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] | 532 | again: |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 533 | before = trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 534 | |
| 535 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 536 | union perf_event *event; |
| 537 | |
| 538 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 539 | const u32 type = event->header.type; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 540 | tracepoint_handler handler; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 541 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 542 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 543 | ++trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 544 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 545 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 546 | if (err) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 547 | 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] | 548 | continue; |
| 549 | } |
| 550 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 551 | if (trace->base_time == 0) |
| 552 | trace->base_time = sample.time; |
| 553 | |
| 554 | if (type != PERF_RECORD_SAMPLE) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 555 | trace__process_event(trace, &trace->host, event); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 556 | continue; |
| 557 | } |
| 558 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 559 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 560 | if (evsel == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 561 | 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] | 562 | continue; |
| 563 | } |
| 564 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 565 | if (sample.raw_data == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 566 | 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] | 567 | perf_evsel__name(evsel), sample.tid, |
| 568 | sample.cpu, sample.raw_size); |
| 569 | continue; |
| 570 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 571 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 572 | handler = evsel->handler.func; |
| 573 | handler(trace, evsel, &sample); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 577 | if (trace->nr_events == before) { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 578 | if (done) |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 579 | goto out_unmap_evlist; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 580 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 581 | poll(evlist->pollfd, evlist->nr_fds, -1); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | if (done) |
| 585 | perf_evlist__disable(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 586 | |
| 587 | goto again; |
| 588 | |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 589 | out_unmap_evlist: |
| 590 | perf_evlist__munmap(evlist); |
| 591 | out_close_evlist: |
| 592 | perf_evlist__close(evlist); |
| 593 | out_delete_maps: |
| 594 | perf_evlist__delete_maps(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 595 | out_delete_evlist: |
| 596 | perf_evlist__delete(evlist); |
| 597 | out: |
| 598 | return err; |
| 599 | } |
| 600 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 601 | static size_t trace__fprintf_threads_header(FILE *fp) |
| 602 | { |
| 603 | size_t printed; |
| 604 | |
| 605 | printed = fprintf(fp, "\n _____________________________________________________________________\n"); |
| 606 | printed += fprintf(fp," __) Summary of events (__\n\n"); |
| 607 | printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); |
| 608 | printed += fprintf(fp," _____________________________________________________________________\n\n"); |
| 609 | |
| 610 | return printed; |
| 611 | } |
| 612 | |
| 613 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
| 614 | { |
| 615 | size_t printed = trace__fprintf_threads_header(fp); |
| 616 | struct rb_node *nd; |
| 617 | |
| 618 | for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) { |
| 619 | struct thread *thread = rb_entry(nd, struct thread, rb_node); |
| 620 | struct thread_trace *ttrace = thread->priv; |
| 621 | const char *color; |
| 622 | double ratio; |
| 623 | |
| 624 | if (ttrace == NULL) |
| 625 | continue; |
| 626 | |
| 627 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; |
| 628 | |
| 629 | color = PERF_COLOR_NORMAL; |
| 630 | if (ratio > 50.0) |
| 631 | color = PERF_COLOR_RED; |
| 632 | else if (ratio > 25.0) |
| 633 | color = PERF_COLOR_GREEN; |
| 634 | else if (ratio > 5.0) |
| 635 | color = PERF_COLOR_YELLOW; |
| 636 | |
| 637 | printed += color_fprintf(fp, color, "%20s", thread->comm); |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 638 | printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 639 | printed += color_fprintf(fp, color, "%5.1f%%", ratio); |
| 640 | printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); |
| 641 | } |
| 642 | |
| 643 | return printed; |
| 644 | } |
| 645 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 646 | static int trace__set_duration(const struct option *opt, const char *str, |
| 647 | int unset __maybe_unused) |
| 648 | { |
| 649 | struct trace *trace = opt->value; |
| 650 | |
| 651 | trace->duration_filter = atof(str); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 655 | static int trace__open_output(struct trace *trace, const char *filename) |
| 656 | { |
| 657 | struct stat st; |
| 658 | |
| 659 | if (!stat(filename, &st) && st.st_size) { |
| 660 | char oldname[PATH_MAX]; |
| 661 | |
| 662 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); |
| 663 | unlink(oldname); |
| 664 | rename(filename, oldname); |
| 665 | } |
| 666 | |
| 667 | trace->output = fopen(filename, "w"); |
| 668 | |
| 669 | return trace->output == NULL ? -errno : 0; |
| 670 | } |
| 671 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 672 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
| 673 | { |
| 674 | const char * const trace_usage[] = { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 675 | "perf trace [<options>] [<command>]", |
| 676 | "perf trace [<options>] -- <command> [<options>]", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 677 | NULL |
| 678 | }; |
| 679 | struct trace trace = { |
| 680 | .audit_machine = audit_detect_machine(), |
| 681 | .syscalls = { |
| 682 | . max = -1, |
| 683 | }, |
| 684 | .opts = { |
| 685 | .target = { |
| 686 | .uid = UINT_MAX, |
| 687 | .uses_mmap = true, |
| 688 | }, |
| 689 | .user_freq = UINT_MAX, |
| 690 | .user_interval = ULLONG_MAX, |
| 691 | .no_delay = true, |
| 692 | .mmap_pages = 1024, |
| 693 | }, |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 694 | .output = stdout, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 695 | }; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 696 | const char *output_name = NULL; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 697 | const char *ev_qualifier_str = NULL; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 698 | const struct option trace_options[] = { |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 699 | OPT_STRING('e', "expr", &ev_qualifier_str, "expr", |
| 700 | "list of events to trace"), |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 701 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 702 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
| 703 | "trace events on existing process id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 704 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 705 | "trace events on existing thread id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 706 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 707 | "system-wide collection from all CPUs"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 708 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 709 | "list of cpus to monitor"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 710 | OPT_BOOLEAN('i', "no-inherit", &trace.opts.no_inherit, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 711 | "child tasks do not inherit counters"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 712 | OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 713 | "number of mmap data pages"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 714 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 715 | "user to profile"), |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 716 | OPT_CALLBACK(0, "duration", &trace, "float", |
| 717 | "show only events with duration > N.M ms", |
| 718 | trace__set_duration), |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 719 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame^] | 720 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 721 | OPT_END() |
| 722 | }; |
| 723 | int err; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 724 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 725 | |
| 726 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 727 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 728 | if (output_name != NULL) { |
| 729 | err = trace__open_output(&trace, output_name); |
| 730 | if (err < 0) { |
| 731 | perror("failed to create output file"); |
| 732 | goto out; |
| 733 | } |
| 734 | } |
| 735 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 736 | if (ev_qualifier_str != NULL) { |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 737 | const char *s = ev_qualifier_str; |
| 738 | |
| 739 | trace.not_ev_qualifier = *s == '!'; |
| 740 | if (trace.not_ev_qualifier) |
| 741 | ++s; |
| 742 | trace.ev_qualifier = strlist__new(true, s); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 743 | if (trace.ev_qualifier == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 744 | fputs("Not enough memory to parse event qualifier", |
| 745 | trace.output); |
| 746 | err = -ENOMEM; |
| 747 | goto out_close; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 751 | err = perf_target__validate(&trace.opts.target); |
| 752 | if (err) { |
| 753 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 754 | fprintf(trace.output, "%s", bf); |
| 755 | goto out_close; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 756 | } |
| 757 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 758 | err = perf_target__parse_uid(&trace.opts.target); |
| 759 | if (err) { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 760 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 761 | fprintf(trace.output, "%s", bf); |
| 762 | goto out_close; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 763 | } |
| 764 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 765 | if (!argc && perf_target__none(&trace.opts.target)) |
Namhyung Kim | ee76120 | 2012-10-05 14:02:14 +0900 | [diff] [blame] | 766 | trace.opts.target.system_wide = true; |
| 767 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 768 | err = trace__run(&trace, argc, argv); |
| 769 | |
| 770 | if (trace.sched && !err) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 771 | trace__fprintf_thread_summary(&trace, trace.output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 772 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 773 | out_close: |
| 774 | if (output_name != NULL) |
| 775 | fclose(trace.output); |
| 776 | out: |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 777 | return err; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 778 | } |