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