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