3 * Function graph tracer.
4 * Copyright (c) 2008 Frederic Weisbecker <fweisbec@gmail.com>
5 * Mostly borrowed from function tracer which
6 * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
16 #define TRACE_GRAPH_INDENT 2
19 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
20 #define TRACE_GRAPH_PRINT_CPU 0x2
21 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
22 #define TRACE_GRAPH_PRINT_PROC 0x8
24 static struct tracer_opt trace_opts[] = {
25 /* Display overruns ? */
26 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
28 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
29 /* Display Overhead ? */
30 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
31 /* Display proc name/pid */
32 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
36 static struct tracer_flags tracer_flags = {
37 /* Don't display overruns and proc by default */
38 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD,
42 /* pid on the last trace processed */
43 static pid_t last_pid[NR_CPUS] = { [0 ... NR_CPUS-1] = -1 };
45 static int graph_trace_init(struct trace_array *tr)
49 for_each_online_cpu(cpu)
50 tracing_reset(tr, cpu);
52 ret = register_ftrace_graph(&trace_graph_return,
56 tracing_start_cmdline_record();
61 static void graph_trace_reset(struct trace_array *tr)
63 tracing_stop_cmdline_record();
64 unregister_ftrace_graph();
67 static inline int log10_cpu(int nb)
76 static enum print_line_t
77 print_graph_cpu(struct trace_seq *s, int cpu)
81 int log10_this = log10_cpu(cpu);
82 int log10_all = log10_cpu(cpus_weight_nr(cpu_online_map));
86 * Start with a space character - to make it stand out
87 * to the right a bit when trace output is pasted into
90 ret = trace_seq_printf(s, " ");
93 * Tricky - we space the CPU field according to the max
94 * number of online CPUs. On a 2-cpu system it would take
95 * a maximum of 1 digit - on a 128 cpu system it would
96 * take up to 3 digits:
98 for (i = 0; i < log10_all - log10_this; i++) {
99 ret = trace_seq_printf(s, " ");
101 return TRACE_TYPE_PARTIAL_LINE;
103 ret = trace_seq_printf(s, "%d) ", cpu);
105 return TRACE_TYPE_PARTIAL_LINE;
107 return TRACE_TYPE_HANDLED;
110 #define TRACE_GRAPH_PROCINFO_LENGTH 14
112 static enum print_line_t
113 print_graph_proc(struct trace_seq *s, pid_t pid)
120 /* sign + log10(MAX_INT) + '\0' */
123 strncpy(comm, trace_find_cmdline(pid), 7);
125 sprintf(pid_str, "%d", pid);
127 /* 1 stands for the "-" character */
128 len = strlen(comm) + strlen(pid_str) + 1;
130 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
131 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
133 /* First spaces to align center */
134 for (i = 0; i < spaces / 2; i++) {
135 ret = trace_seq_printf(s, " ");
137 return TRACE_TYPE_PARTIAL_LINE;
140 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
142 return TRACE_TYPE_PARTIAL_LINE;
144 /* Last spaces to align center */
145 for (i = 0; i < spaces - (spaces / 2); i++) {
146 ret = trace_seq_printf(s, " ");
148 return TRACE_TYPE_PARTIAL_LINE;
150 return TRACE_TYPE_HANDLED;
154 /* If the pid changed since the last trace, output this event */
155 static enum print_line_t
156 verif_pid(struct trace_seq *s, pid_t pid, int cpu)
161 if (last_pid[cpu] != -1 && last_pid[cpu] == pid)
162 return TRACE_TYPE_HANDLED;
164 prev_pid = last_pid[cpu];
168 * Context-switch trace line:
170 ------------------------------------------
171 | 1) migration/0--1 => sshd-1755
172 ------------------------------------------
175 ret = trace_seq_printf(s,
176 " ------------------------------------------\n");
178 TRACE_TYPE_PARTIAL_LINE;
180 ret = print_graph_cpu(s, cpu);
181 if (ret == TRACE_TYPE_PARTIAL_LINE)
182 TRACE_TYPE_PARTIAL_LINE;
184 ret = print_graph_proc(s, prev_pid);
185 if (ret == TRACE_TYPE_PARTIAL_LINE)
186 TRACE_TYPE_PARTIAL_LINE;
188 ret = trace_seq_printf(s, " => ");
190 TRACE_TYPE_PARTIAL_LINE;
192 ret = print_graph_proc(s, pid);
193 if (ret == TRACE_TYPE_PARTIAL_LINE)
194 TRACE_TYPE_PARTIAL_LINE;
196 ret = trace_seq_printf(s,
197 "\n ------------------------------------------\n\n");
199 TRACE_TYPE_PARTIAL_LINE;
205 trace_branch_is_leaf(struct trace_iterator *iter,
206 struct ftrace_graph_ent_entry *curr)
208 struct ring_buffer_iter *ring_iter;
209 struct ring_buffer_event *event;
210 struct ftrace_graph_ret_entry *next;
212 ring_iter = iter->buffer_iter[iter->cpu];
217 event = ring_buffer_iter_peek(ring_iter, NULL);
222 next = ring_buffer_event_data(event);
224 if (next->ent.type != TRACE_GRAPH_RET)
227 if (curr->ent.pid != next->ent.pid ||
228 curr->graph_ent.func != next->ret.func)
234 static enum print_line_t
235 print_graph_irq(struct trace_seq *s, unsigned long addr,
236 enum trace_type type, int cpu, pid_t pid)
240 if (addr < (unsigned long)__irqentry_text_start ||
241 addr >= (unsigned long)__irqentry_text_end)
242 return TRACE_TYPE_UNHANDLED;
244 if (type == TRACE_GRAPH_ENT) {
245 ret = trace_seq_printf(s, "==========> | ");
248 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
249 ret = print_graph_cpu(s, cpu);
250 if (ret == TRACE_TYPE_PARTIAL_LINE)
251 return TRACE_TYPE_PARTIAL_LINE;
254 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
255 ret = print_graph_proc(s, pid);
256 if (ret == TRACE_TYPE_PARTIAL_LINE)
257 return TRACE_TYPE_PARTIAL_LINE;
259 ret = trace_seq_printf(s, " | ");
261 return TRACE_TYPE_PARTIAL_LINE;
265 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
266 ret = trace_seq_printf(s, " ");
268 return TRACE_TYPE_PARTIAL_LINE;
271 ret = trace_seq_printf(s, "<========== |\n");
274 return TRACE_TYPE_PARTIAL_LINE;
275 return TRACE_TYPE_HANDLED;
278 static enum print_line_t
279 print_graph_duration(unsigned long long duration, struct trace_seq *s)
281 unsigned long nsecs_rem = do_div(duration, 1000);
282 /* log10(ULONG_MAX) + '\0' */
288 sprintf(msecs_str, "%lu", (unsigned long) duration);
291 ret = trace_seq_printf(s, msecs_str);
293 return TRACE_TYPE_PARTIAL_LINE;
295 len = strlen(msecs_str);
297 /* Print nsecs (we don't want to exceed 7 numbers) */
299 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
300 ret = trace_seq_printf(s, ".%s", nsecs_str);
302 return TRACE_TYPE_PARTIAL_LINE;
303 len += strlen(nsecs_str);
306 ret = trace_seq_printf(s, " us ");
308 return TRACE_TYPE_PARTIAL_LINE;
310 /* Print remaining spaces to fit the row's width */
311 for (i = len; i < 7; i++) {
312 ret = trace_seq_printf(s, " ");
314 return TRACE_TYPE_PARTIAL_LINE;
317 ret = trace_seq_printf(s, "| ");
319 return TRACE_TYPE_PARTIAL_LINE;
320 return TRACE_TYPE_HANDLED;
324 /* Signal a overhead of time execution to the output */
326 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
328 /* Duration exceeded 100 msecs */
329 if (duration > 100000ULL)
330 return trace_seq_printf(s, "! ");
332 /* Duration exceeded 10 msecs */
333 if (duration > 10000ULL)
334 return trace_seq_printf(s, "+ ");
336 return trace_seq_printf(s, " ");
339 /* Case of a leaf function on its call entry */
340 static enum print_line_t
341 print_graph_entry_leaf(struct trace_iterator *iter,
342 struct ftrace_graph_ent_entry *entry, struct trace_seq *s)
344 struct ftrace_graph_ret_entry *ret_entry;
345 struct ftrace_graph_ret *graph_ret;
346 struct ring_buffer_event *event;
347 struct ftrace_graph_ent *call;
348 unsigned long long duration;
352 event = ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
353 ret_entry = ring_buffer_event_data(event);
354 graph_ret = &ret_entry->ret;
355 call = &entry->graph_ent;
356 duration = graph_ret->rettime - graph_ret->calltime;
359 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
360 ret = print_graph_overhead(duration, s);
362 return TRACE_TYPE_PARTIAL_LINE;
366 ret = print_graph_duration(duration, s);
367 if (ret == TRACE_TYPE_PARTIAL_LINE)
368 return TRACE_TYPE_PARTIAL_LINE;
371 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
372 ret = trace_seq_printf(s, " ");
374 return TRACE_TYPE_PARTIAL_LINE;
377 ret = seq_print_ip_sym(s, call->func, 0);
379 return TRACE_TYPE_PARTIAL_LINE;
381 ret = trace_seq_printf(s, "();\n");
383 return TRACE_TYPE_PARTIAL_LINE;
385 return TRACE_TYPE_HANDLED;
388 static enum print_line_t
389 print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
390 struct trace_seq *s, pid_t pid, int cpu)
394 struct ftrace_graph_ent *call = &entry->graph_ent;
397 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
398 ret = trace_seq_printf(s, " ");
400 return TRACE_TYPE_PARTIAL_LINE;
404 ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, pid);
405 if (ret == TRACE_TYPE_UNHANDLED) {
407 ret = trace_seq_printf(s, " | ");
409 return TRACE_TYPE_PARTIAL_LINE;
411 if (ret == TRACE_TYPE_PARTIAL_LINE)
412 return TRACE_TYPE_PARTIAL_LINE;
417 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
418 ret = trace_seq_printf(s, " ");
420 return TRACE_TYPE_PARTIAL_LINE;
423 ret = seq_print_ip_sym(s, call->func, 0);
425 return TRACE_TYPE_PARTIAL_LINE;
427 ret = trace_seq_printf(s, "() {\n");
429 return TRACE_TYPE_PARTIAL_LINE;
431 return TRACE_TYPE_HANDLED;
434 static enum print_line_t
435 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
436 struct trace_iterator *iter, int cpu)
439 struct trace_entry *ent = iter->ent;
442 if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
443 return TRACE_TYPE_PARTIAL_LINE;
446 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
447 ret = print_graph_cpu(s, cpu);
448 if (ret == TRACE_TYPE_PARTIAL_LINE)
449 return TRACE_TYPE_PARTIAL_LINE;
453 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
454 ret = print_graph_proc(s, ent->pid);
455 if (ret == TRACE_TYPE_PARTIAL_LINE)
456 return TRACE_TYPE_PARTIAL_LINE;
458 ret = trace_seq_printf(s, " | ");
460 return TRACE_TYPE_PARTIAL_LINE;
463 if (trace_branch_is_leaf(iter, field))
464 return print_graph_entry_leaf(iter, field, s);
466 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
470 static enum print_line_t
471 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
472 struct trace_entry *ent, int cpu)
476 unsigned long long duration = trace->rettime - trace->calltime;
479 if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
480 return TRACE_TYPE_PARTIAL_LINE;
483 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
484 ret = print_graph_cpu(s, cpu);
485 if (ret == TRACE_TYPE_PARTIAL_LINE)
486 return TRACE_TYPE_PARTIAL_LINE;
490 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
491 ret = print_graph_proc(s, ent->pid);
492 if (ret == TRACE_TYPE_PARTIAL_LINE)
493 return TRACE_TYPE_PARTIAL_LINE;
495 ret = trace_seq_printf(s, " | ");
497 return TRACE_TYPE_PARTIAL_LINE;
501 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
502 ret = print_graph_overhead(duration, s);
504 return TRACE_TYPE_PARTIAL_LINE;
508 ret = print_graph_duration(duration, s);
509 if (ret == TRACE_TYPE_PARTIAL_LINE)
510 return TRACE_TYPE_PARTIAL_LINE;
513 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
514 ret = trace_seq_printf(s, " ");
516 return TRACE_TYPE_PARTIAL_LINE;
519 ret = trace_seq_printf(s, "}\n");
521 return TRACE_TYPE_PARTIAL_LINE;
524 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
525 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
528 return TRACE_TYPE_PARTIAL_LINE;
531 ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
532 if (ret == TRACE_TYPE_PARTIAL_LINE)
533 return TRACE_TYPE_PARTIAL_LINE;
535 return TRACE_TYPE_HANDLED;
538 static enum print_line_t
539 print_graph_comment(struct print_entry *trace, struct trace_seq *s,
540 struct trace_entry *ent, struct trace_iterator *iter)
546 if (verif_pid(s, ent->pid, iter->cpu) == TRACE_TYPE_PARTIAL_LINE)
547 return TRACE_TYPE_PARTIAL_LINE;
550 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
551 ret = print_graph_cpu(s, iter->cpu);
552 if (ret == TRACE_TYPE_PARTIAL_LINE)
553 return TRACE_TYPE_PARTIAL_LINE;
557 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
558 ret = print_graph_proc(s, ent->pid);
559 if (ret == TRACE_TYPE_PARTIAL_LINE)
560 return TRACE_TYPE_PARTIAL_LINE;
562 ret = trace_seq_printf(s, " | ");
564 return TRACE_TYPE_PARTIAL_LINE;
568 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
569 ret = trace_seq_printf(s, " ");
571 return TRACE_TYPE_PARTIAL_LINE;
575 ret = trace_seq_printf(s, " | ");
577 return TRACE_TYPE_PARTIAL_LINE;
580 if (trace->depth > 0)
581 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
582 ret = trace_seq_printf(s, " ");
584 return TRACE_TYPE_PARTIAL_LINE;
588 ret = trace_seq_printf(s, "/* %s", trace->buf);
590 return TRACE_TYPE_PARTIAL_LINE;
592 if (ent->flags & TRACE_FLAG_CONT)
593 trace_seq_print_cont(s, iter);
595 /* Strip ending newline */
596 if (s->buffer[s->len - 1] == '\n') {
597 s->buffer[s->len - 1] = '\0';
601 ret = trace_seq_printf(s, " */\n");
603 return TRACE_TYPE_PARTIAL_LINE;
605 return TRACE_TYPE_HANDLED;
610 print_graph_function(struct trace_iterator *iter)
612 struct trace_seq *s = &iter->seq;
613 struct trace_entry *entry = iter->ent;
615 switch (entry->type) {
616 case TRACE_GRAPH_ENT: {
617 struct ftrace_graph_ent_entry *field;
618 trace_assign_type(field, entry);
619 return print_graph_entry(field, s, iter,
622 case TRACE_GRAPH_RET: {
623 struct ftrace_graph_ret_entry *field;
624 trace_assign_type(field, entry);
625 return print_graph_return(&field->ret, s, entry, iter->cpu);
628 struct print_entry *field;
629 trace_assign_type(field, entry);
630 return print_graph_comment(field, s, entry, iter);
633 return TRACE_TYPE_UNHANDLED;
637 static void print_graph_headers(struct seq_file *s)
641 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
642 seq_printf(s, "CPU ");
643 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
644 seq_printf(s, "TASK/PID ");
645 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD)
646 seq_printf(s, "OVERHEAD/");
647 seq_printf(s, "DURATION FUNCTION CALLS\n");
651 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
653 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
654 seq_printf(s, "| | ");
655 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
657 seq_printf(s, "| | | | |\n");
659 seq_printf(s, " | | | | |\n");
661 static struct tracer graph_trace __read_mostly = {
662 .name = "function_graph",
663 .init = graph_trace_init,
664 .reset = graph_trace_reset,
665 .print_line = print_graph_function,
666 .print_header = print_graph_headers,
667 .flags = &tracer_flags,
670 static __init int init_graph_trace(void)
672 return register_tracer(&graph_trace);
675 device_initcall(init_graph_trace);