blob: 120fdfb3d920f303de89027c8b9998b5ac8330ab [file] [log] [blame]
Robert Richter4e319022013-06-11 17:29:18 +02001#include <traceevent/event-parse.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002#include "builtin.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03003#include "util/color.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004#include "util/evlist.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03005#include "util/machine.h"
6#include "util/thread.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03007#include "util/parse-options.h"
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03008#include "util/strlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03009#include "util/thread_map.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030010
11#include <libaudit.h>
12#include <stdlib.h>
13
14static struct syscall_fmt {
15 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030016 const char *alias;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030017 bool errmsg;
18 bool timeout;
19} syscall_fmts[] = {
Arnaldo Carvalho de Melo8b745262012-10-18 19:06:13 -030020 { .name = "access", .errmsg = true, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030021 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
Arnaldo Carvalho de Meloa14bb862013-07-30 16:38:23 -030022 { .name = "connect", .errmsg = true, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030023 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
24 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
25 { .name = "futex", .errmsg = true, },
Arnaldo Carvalho de Melo8b745262012-10-18 19:06:13 -030026 { .name = "open", .errmsg = true, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030027 { .name = "poll", .errmsg = true, .timeout = true, },
28 { .name = "ppoll", .errmsg = true, .timeout = true, },
29 { .name = "read", .errmsg = true, },
30 { .name = "recvfrom", .errmsg = true, },
31 { .name = "select", .errmsg = true, .timeout = true, },
Arnaldo Carvalho de Melo8b745262012-10-18 19:06:13 -030032 { .name = "socket", .errmsg = true, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030033 { .name = "stat", .errmsg = true, .alias = "newstat", },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030034};
35
36static int syscall_fmt__cmp(const void *name, const void *fmtp)
37{
38 const struct syscall_fmt *fmt = fmtp;
39 return strcmp(name, fmt->name);
40}
41
42static struct syscall_fmt *syscall_fmt__find(const char *name)
43{
44 const int nmemb = ARRAY_SIZE(syscall_fmts);
45 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
46}
47
48struct syscall {
49 struct event_format *tp_format;
50 const char *name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030051 bool filtered;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030052 struct syscall_fmt *fmt;
53};
54
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -020055static size_t fprintf_duration(unsigned long t, FILE *fp)
56{
57 double duration = (double)t / NSEC_PER_MSEC;
58 size_t printed = fprintf(fp, "(");
59
60 if (duration >= 1.0)
61 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
62 else if (duration >= 0.01)
63 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
64 else
65 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
66 return printed + fprintf(stdout, "): ");
67}
68
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030069struct thread_trace {
70 u64 entry_time;
71 u64 exit_time;
72 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -030073 unsigned long nr_events;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030074 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -030075 double runtime_ms;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030076};
77
78static struct thread_trace *thread_trace__new(void)
79{
80 return zalloc(sizeof(struct thread_trace));
81}
82
83static struct thread_trace *thread__trace(struct thread *thread)
84{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -030085 struct thread_trace *ttrace;
86
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030087 if (thread == NULL)
88 goto fail;
89
90 if (thread->priv == NULL)
91 thread->priv = thread_trace__new();
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -030092
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030093 if (thread->priv == NULL)
94 goto fail;
95
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -030096 ttrace = thread->priv;
97 ++ttrace->nr_events;
98
99 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300100fail:
101 color_fprintf(stdout, PERF_COLOR_RED,
102 "WARNING: not enough memory, dropping samples!\n");
103 return NULL;
104}
105
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300106struct trace {
107 int audit_machine;
108 struct {
109 int max;
110 struct syscall *table;
111 } syscalls;
112 struct perf_record_opts opts;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300113 struct machine host;
114 u64 base_time;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300115 struct strlist *ev_qualifier;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300116 unsigned long nr_events;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300117 bool sched;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300118 bool multiple_threads;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300119 double duration_filter;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300120 double runtime_ms;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300121};
122
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300123static bool trace__filter_duration(struct trace *trace, double t)
124{
125 return t < (trace->duration_filter * NSEC_PER_MSEC);
126}
127
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300128static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
129{
130 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
131
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200132 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300133}
134
Namhyung Kimf15eb532012-10-05 14:02:16 +0900135static bool done = false;
136
137static void sig_handler(int sig __maybe_unused)
138{
139 done = true;
140}
141
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300142static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200143 u64 duration, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300144{
145 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200146 printed += fprintf_duration(duration, fp);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300147
148 if (trace->multiple_threads)
Adrian Hunter38051232013-07-04 16:20:31 +0300149 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300150
151 return printed;
152}
153
154static int trace__process_event(struct machine *machine, union perf_event *event)
155{
156 int ret = 0;
157
158 switch (event->header.type) {
159 case PERF_RECORD_LOST:
160 color_fprintf(stdout, PERF_COLOR_RED,
161 "LOST %" PRIu64 " events!\n", event->lost.lost);
162 ret = machine__process_lost_event(machine, event);
163 default:
164 ret = machine__process_event(machine, event);
165 break;
166 }
167
168 return ret;
169}
170
171static int trace__tool_process(struct perf_tool *tool __maybe_unused,
172 union perf_event *event,
173 struct perf_sample *sample __maybe_unused,
174 struct machine *machine)
175{
176 return trace__process_event(machine, event);
177}
178
179static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
180{
181 int err = symbol__init();
182
183 if (err)
184 return err;
185
186 machine__init(&trace->host, "", HOST_KERNEL_ID);
187 machine__create_kernel_maps(&trace->host);
188
189 if (perf_target__has_task(&trace->opts.target)) {
190 err = perf_event__synthesize_thread_map(NULL, evlist->threads,
191 trace__tool_process,
192 &trace->host);
193 } else {
194 err = perf_event__synthesize_threads(NULL, trace__tool_process,
195 &trace->host);
196 }
197
198 if (err)
199 symbol__exit();
200
201 return err;
202}
203
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300204static int trace__read_syscall_info(struct trace *trace, int id)
205{
206 char tp_name[128];
207 struct syscall *sc;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -0300208 const char *name = audit_syscall_to_name(id, trace->audit_machine);
209
210 if (name == NULL)
211 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300212
213 if (id > trace->syscalls.max) {
214 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
215
216 if (nsyscalls == NULL)
217 return -1;
218
219 if (trace->syscalls.max != -1) {
220 memset(nsyscalls + trace->syscalls.max + 1, 0,
221 (id - trace->syscalls.max) * sizeof(*sc));
222 } else {
223 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
224 }
225
226 trace->syscalls.table = nsyscalls;
227 trace->syscalls.max = id;
228 }
229
230 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -0300231 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300232
233 if (trace->ev_qualifier && !strlist__find(trace->ev_qualifier, name)) {
234 sc->filtered = true;
235 /*
236 * No need to do read tracepoint information since this will be
237 * filtered out.
238 */
239 return 0;
240 }
241
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -0300242 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300243
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300244 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
245 sc->tp_format = event_format__new("syscalls", tp_name);
246
247 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
248 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
249 sc->tp_format = event_format__new("syscalls", tp_name);
250 }
251
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300252 return sc->tp_format != NULL ? 0 : -1;
253}
254
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300255static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
256 unsigned long *args)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300257{
258 int i = 0;
259 size_t printed = 0;
260
261 if (sc->tp_format != NULL) {
262 struct format_field *field;
263
264 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300265 printed += scnprintf(bf + printed, size - printed,
266 "%s%s: %ld", printed ? ", " : "",
267 field->name, args[i++]);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300268 }
269 } else {
270 while (i < 6) {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300271 printed += scnprintf(bf + printed, size - printed,
272 "%sarg%d: %ld",
273 printed ? ", " : "", i, args[i]);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300274 ++i;
275 }
276 }
277
278 return printed;
279}
280
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300281typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
282 struct perf_sample *sample);
283
284static struct syscall *trace__syscall_info(struct trace *trace,
285 struct perf_evsel *evsel,
286 struct perf_sample *sample)
287{
288 int id = perf_evsel__intval(evsel, sample, "id");
289
290 if (id < 0) {
291 printf("Invalid syscall %d id, skipping...\n", id);
292 return NULL;
293 }
294
295 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
296 trace__read_syscall_info(trace, id))
297 goto out_cant_read;
298
299 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
300 goto out_cant_read;
301
302 return &trace->syscalls.table[id];
303
304out_cant_read:
Arnaldo Carvalho de Melo814d7a42012-10-24 18:44:13 -0200305 printf("Problems reading syscall %d", id);
306 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
307 printf("(%s)", trace->syscalls.table[id].name);
308 puts(" information");
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300309 return NULL;
310}
311
312static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
313 struct perf_sample *sample)
314{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300315 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300316 void *args;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300317 size_t printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300318 struct thread *thread;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300319 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300320 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300321
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300322 if (sc == NULL)
323 return -1;
324
325 if (sc->filtered)
326 return 0;
327
328 thread = machine__findnew_thread(&trace->host, sample->tid);
329 ttrace = thread__trace(thread);
330 if (ttrace == NULL)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300331 return -1;
332
333 args = perf_evsel__rawptr(evsel, sample, "args");
334 if (args == NULL) {
335 printf("Problems reading syscall arguments\n");
336 return -1;
337 }
338
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300339 ttrace = thread->priv;
340
341 if (ttrace->entry_str == NULL) {
342 ttrace->entry_str = malloc(1024);
343 if (!ttrace->entry_str)
344 return -1;
345 }
346
347 ttrace->entry_time = sample->time;
348 msg = ttrace->entry_str;
349 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
350
351 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
352
353 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300354 if (!trace->duration_filter) {
355 trace__fprintf_entry_head(trace, thread, 1, sample->time, stdout);
356 printf("%-70s\n", ttrace->entry_str);
357 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300358 } else
359 ttrace->entry_pending = true;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300360
361 return 0;
362}
363
364static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
365 struct perf_sample *sample)
366{
367 int ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200368 u64 duration = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300369 struct thread *thread;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300370 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300371 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300372
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300373 if (sc == NULL)
374 return -1;
375
376 if (sc->filtered)
377 return 0;
378
379 thread = machine__findnew_thread(&trace->host, sample->tid);
380 ttrace = thread__trace(thread);
381 if (ttrace == NULL)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300382 return -1;
383
384 ret = perf_evsel__intval(evsel, sample, "ret");
385
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300386 ttrace = thread->priv;
387
388 ttrace->exit_time = sample->time;
389
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300390 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200391 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300392 if (trace__filter_duration(trace, duration))
393 goto out;
394 } else if (trace->duration_filter)
395 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200396
397 trace__fprintf_entry_head(trace, thread, duration, sample->time, stdout);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300398
399 if (ttrace->entry_pending) {
400 printf("%-70s", ttrace->entry_str);
401 } else {
402 printf(" ... [");
403 color_fprintf(stdout, PERF_COLOR_YELLOW, "continued");
404 printf("]: %s()", sc->name);
405 }
406
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300407 if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
408 char bf[256];
409 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
410 *e = audit_errno_to_name(-ret);
411
412 printf(") = -1 %s %s", e, emsg);
413 } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
414 printf(") = 0 Timeout");
415 else
416 printf(") = %d", ret);
417
418 putchar('\n');
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300419out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300420 ttrace->entry_pending = false;
421
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300422 return 0;
423}
424
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300425static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
426 struct perf_sample *sample)
427{
428 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
429 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
430 struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
431 struct thread_trace *ttrace = thread__trace(thread);
432
433 if (ttrace == NULL)
434 goto out_dump;
435
436 ttrace->runtime_ms += runtime_ms;
437 trace->runtime_ms += runtime_ms;
438 return 0;
439
440out_dump:
441 printf("%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
442 evsel->name,
443 perf_evsel__strval(evsel, sample, "comm"),
444 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
445 runtime,
446 perf_evsel__intval(evsel, sample, "vruntime"));
447 return 0;
448}
449
Namhyung Kimf15eb532012-10-05 14:02:16 +0900450static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300451{
Namhyung Kim334fe7a2013-03-11 16:43:12 +0900452 struct perf_evlist *evlist = perf_evlist__new();
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300453 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300454 int err = -1, i;
455 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +0900456 const bool forks = argc > 0;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300457
458 if (evlist == NULL) {
459 printf("Not enough memory to run!\n");
460 goto out;
461 }
462
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300463 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
464 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
465 printf("Couldn't read the raw_syscalls tracepoints information!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300466 goto out_delete_evlist;
467 }
468
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300469 if (trace->sched &&
470 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
471 trace__sched_stat_runtime)) {
472 printf("Couldn't read the sched_stat_runtime tracepoint information!\n");
473 goto out_delete_evlist;
474 }
475
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300476 err = perf_evlist__create_maps(evlist, &trace->opts.target);
477 if (err < 0) {
478 printf("Problems parsing the target to trace, check your options!\n");
479 goto out_delete_evlist;
480 }
481
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300482 err = trace__symbols_init(trace, evlist);
483 if (err < 0) {
484 printf("Problems initializing symbol libraries!\n");
Namhyung Kim3beb0862013-03-15 14:48:50 +0900485 goto out_delete_maps;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300486 }
487
Arnaldo Carvalho de Melof77a9512012-12-10 16:41:31 -0300488 perf_evlist__config(evlist, &trace->opts);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300489
Namhyung Kimf15eb532012-10-05 14:02:16 +0900490 signal(SIGCHLD, sig_handler);
491 signal(SIGINT, sig_handler);
492
493 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +0900494 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Namhyung Kim55e162e2013-03-11 16:43:17 +0900495 argv, false, false);
Namhyung Kimf15eb532012-10-05 14:02:16 +0900496 if (err < 0) {
497 printf("Couldn't run the workload!\n");
Namhyung Kim3beb0862013-03-15 14:48:50 +0900498 goto out_delete_maps;
Namhyung Kimf15eb532012-10-05 14:02:16 +0900499 }
500 }
501
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300502 err = perf_evlist__open(evlist);
503 if (err < 0) {
504 printf("Couldn't create the events: %s\n", strerror(errno));
Namhyung Kim3beb0862013-03-15 14:48:50 +0900505 goto out_delete_maps;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300506 }
507
508 err = perf_evlist__mmap(evlist, UINT_MAX, false);
509 if (err < 0) {
510 printf("Couldn't mmap the events: %s\n", strerror(errno));
Namhyung Kim3beb0862013-03-15 14:48:50 +0900511 goto out_close_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300512 }
513
514 perf_evlist__enable(evlist);
Namhyung Kimf15eb532012-10-05 14:02:16 +0900515
516 if (forks)
517 perf_evlist__start_workload(evlist);
518
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300519 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300520again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300521 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300522
523 for (i = 0; i < evlist->nr_mmaps; i++) {
524 union perf_event *event;
525
526 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
527 const u32 type = event->header.type;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300528 tracepoint_handler handler;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300529 struct perf_sample sample;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300530
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300531 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300532
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300533 err = perf_evlist__parse_sample(evlist, event, &sample);
534 if (err) {
535 printf("Can't parse sample, err = %d, skipping...\n", err);
536 continue;
537 }
538
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300539 if (trace->base_time == 0)
540 trace->base_time = sample.time;
541
542 if (type != PERF_RECORD_SAMPLE) {
543 trace__process_event(&trace->host, event);
544 continue;
545 }
546
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300547 evsel = perf_evlist__id2evsel(evlist, sample.id);
548 if (evsel == NULL) {
549 printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
550 continue;
551 }
552
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300553 if (sample.raw_data == NULL) {
554 printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
555 perf_evsel__name(evsel), sample.tid,
556 sample.cpu, sample.raw_size);
557 continue;
558 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300559
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300560 handler = evsel->handler.func;
561 handler(trace, evsel, &sample);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300562 }
563 }
564
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300565 if (trace->nr_events == before) {
Namhyung Kimf15eb532012-10-05 14:02:16 +0900566 if (done)
Namhyung Kim3beb0862013-03-15 14:48:50 +0900567 goto out_unmap_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +0900568
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300569 poll(evlist->pollfd, evlist->nr_fds, -1);
Namhyung Kimf15eb532012-10-05 14:02:16 +0900570 }
571
572 if (done)
573 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300574
575 goto again;
576
Namhyung Kim3beb0862013-03-15 14:48:50 +0900577out_unmap_evlist:
578 perf_evlist__munmap(evlist);
579out_close_evlist:
580 perf_evlist__close(evlist);
581out_delete_maps:
582 perf_evlist__delete_maps(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300583out_delete_evlist:
584 perf_evlist__delete(evlist);
585out:
586 return err;
587}
588
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300589static size_t trace__fprintf_threads_header(FILE *fp)
590{
591 size_t printed;
592
593 printed = fprintf(fp, "\n _____________________________________________________________________\n");
594 printed += fprintf(fp," __) Summary of events (__\n\n");
595 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
596 printed += fprintf(fp," _____________________________________________________________________\n\n");
597
598 return printed;
599}
600
601static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
602{
603 size_t printed = trace__fprintf_threads_header(fp);
604 struct rb_node *nd;
605
606 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
607 struct thread *thread = rb_entry(nd, struct thread, rb_node);
608 struct thread_trace *ttrace = thread->priv;
609 const char *color;
610 double ratio;
611
612 if (ttrace == NULL)
613 continue;
614
615 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
616
617 color = PERF_COLOR_NORMAL;
618 if (ratio > 50.0)
619 color = PERF_COLOR_RED;
620 else if (ratio > 25.0)
621 color = PERF_COLOR_GREEN;
622 else if (ratio > 5.0)
623 color = PERF_COLOR_YELLOW;
624
625 printed += color_fprintf(fp, color, "%20s", thread->comm);
Adrian Hunter38051232013-07-04 16:20:31 +0300626 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300627 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
628 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
629 }
630
631 return printed;
632}
633
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300634static int trace__set_duration(const struct option *opt, const char *str,
635 int unset __maybe_unused)
636{
637 struct trace *trace = opt->value;
638
639 trace->duration_filter = atof(str);
640 return 0;
641}
642
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300643int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
644{
645 const char * const trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +0900646 "perf trace [<options>] [<command>]",
647 "perf trace [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300648 NULL
649 };
650 struct trace trace = {
651 .audit_machine = audit_detect_machine(),
652 .syscalls = {
653 . max = -1,
654 },
655 .opts = {
656 .target = {
657 .uid = UINT_MAX,
658 .uses_mmap = true,
659 },
660 .user_freq = UINT_MAX,
661 .user_interval = ULLONG_MAX,
662 .no_delay = true,
663 .mmap_pages = 1024,
664 },
665 };
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300666 const char *ev_qualifier_str = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300667 const struct option trace_options[] = {
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300668 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
669 "list of events to trace"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300670 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
671 "trace events on existing process id"),
672 OPT_STRING(0, "tid", &trace.opts.target.tid, "tid",
673 "trace events on existing thread id"),
674 OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide,
675 "system-wide collection from all CPUs"),
676 OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu",
677 "list of cpus to monitor"),
678 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
679 "child tasks do not inherit counters"),
680 OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages,
681 "number of mmap data pages"),
682 OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user",
683 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -0300684 OPT_CALLBACK(0, "duration", &trace, "float",
685 "show only events with duration > N.M ms",
686 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300687 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300688 OPT_END()
689 };
690 int err;
Namhyung Kim32caf0d2012-10-05 14:02:13 +0900691 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300692
693 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300694
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -0300695 if (ev_qualifier_str != NULL) {
696 trace.ev_qualifier = strlist__new(true, ev_qualifier_str);
697 if (trace.ev_qualifier == NULL) {
698 puts("Not enough memory to parse event qualifier");
699 return -ENOMEM;
700 }
701 }
702
Namhyung Kim32caf0d2012-10-05 14:02:13 +0900703 err = perf_target__validate(&trace.opts.target);
704 if (err) {
705 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
706 printf("%s", bf);
707 return err;
708 }
709
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300710 err = perf_target__parse_uid(&trace.opts.target);
711 if (err) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300712 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
713 printf("%s", bf);
714 return err;
715 }
716
Namhyung Kimf15eb532012-10-05 14:02:16 +0900717 if (!argc && perf_target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +0900718 trace.opts.target.system_wide = true;
719
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300720 err = trace__run(&trace, argc, argv);
721
722 if (trace.sched && !err)
723 trace__fprintf_thread_summary(&trace, stdout);
724
725 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300726}