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