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