3 * Function graph tracer.
4 * Copyright (c) 2008-2009 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>
15 #include "trace_output.h"
17 #define TRACE_GRAPH_INDENT 2
20 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
21 #define TRACE_GRAPH_PRINT_CPU 0x2
22 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
23 #define TRACE_GRAPH_PRINT_PROC 0x8
24 #define TRACE_GRAPH_PRINT_DURATION 0x10
25 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
27 static struct tracer_opt trace_opts[] = {
28 /* Display overruns? (for self-debug purpose) */
29 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
31 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
32 /* Display Overhead ? */
33 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
34 /* Display proc name/pid */
35 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
36 /* Display duration of execution */
37 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
38 /* Display absolute time of an entry */
39 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
43 static struct tracer_flags tracer_flags = {
44 /* Don't display overruns and proc by default */
45 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
46 TRACE_GRAPH_PRINT_DURATION,
50 /* pid on the last trace processed */
53 static int graph_trace_init(struct trace_array *tr)
55 int ret = register_ftrace_graph(&trace_graph_return,
59 tracing_start_cmdline_record();
64 static void graph_trace_reset(struct trace_array *tr)
66 tracing_stop_cmdline_record();
67 unregister_ftrace_graph();
70 static inline int log10_cpu(int nb)
79 static enum print_line_t
80 print_graph_cpu(struct trace_seq *s, int cpu)
84 int log10_this = log10_cpu(cpu);
85 int log10_all = log10_cpu(cpumask_weight(cpu_online_mask));
89 * Start with a space character - to make it stand out
90 * to the right a bit when trace output is pasted into
93 ret = trace_seq_printf(s, " ");
96 * Tricky - we space the CPU field according to the max
97 * number of online CPUs. On a 2-cpu system it would take
98 * a maximum of 1 digit - on a 128 cpu system it would
99 * take up to 3 digits:
101 for (i = 0; i < log10_all - log10_this; i++) {
102 ret = trace_seq_printf(s, " ");
104 return TRACE_TYPE_PARTIAL_LINE;
106 ret = trace_seq_printf(s, "%d) ", cpu);
108 return TRACE_TYPE_PARTIAL_LINE;
110 return TRACE_TYPE_HANDLED;
113 #define TRACE_GRAPH_PROCINFO_LENGTH 14
115 static enum print_line_t
116 print_graph_proc(struct trace_seq *s, pid_t pid)
123 /* sign + log10(MAX_INT) + '\0' */
126 strncpy(comm, trace_find_cmdline(pid), 7);
128 sprintf(pid_str, "%d", pid);
130 /* 1 stands for the "-" character */
131 len = strlen(comm) + strlen(pid_str) + 1;
133 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
134 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
136 /* First spaces to align center */
137 for (i = 0; i < spaces / 2; i++) {
138 ret = trace_seq_printf(s, " ");
140 return TRACE_TYPE_PARTIAL_LINE;
143 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
145 return TRACE_TYPE_PARTIAL_LINE;
147 /* Last spaces to align center */
148 for (i = 0; i < spaces - (spaces / 2); i++) {
149 ret = trace_seq_printf(s, " ");
151 return TRACE_TYPE_PARTIAL_LINE;
153 return TRACE_TYPE_HANDLED;
157 /* If the pid changed since the last trace, output this event */
158 static enum print_line_t
159 verif_pid(struct trace_seq *s, pid_t pid, int cpu, pid_t *last_pids_cpu)
166 return TRACE_TYPE_HANDLED;
168 last_pid = per_cpu_ptr(last_pids_cpu, cpu);
170 if (*last_pid == pid)
171 return TRACE_TYPE_HANDLED;
173 prev_pid = *last_pid;
177 return TRACE_TYPE_HANDLED;
179 * Context-switch trace line:
181 ------------------------------------------
182 | 1) migration/0--1 => sshd-1755
183 ------------------------------------------
186 ret = trace_seq_printf(s,
187 " ------------------------------------------\n");
189 return TRACE_TYPE_PARTIAL_LINE;
191 ret = print_graph_cpu(s, cpu);
192 if (ret == TRACE_TYPE_PARTIAL_LINE)
193 return TRACE_TYPE_PARTIAL_LINE;
195 ret = print_graph_proc(s, prev_pid);
196 if (ret == TRACE_TYPE_PARTIAL_LINE)
197 return TRACE_TYPE_PARTIAL_LINE;
199 ret = trace_seq_printf(s, " => ");
201 return TRACE_TYPE_PARTIAL_LINE;
203 ret = print_graph_proc(s, pid);
204 if (ret == TRACE_TYPE_PARTIAL_LINE)
205 return TRACE_TYPE_PARTIAL_LINE;
207 ret = trace_seq_printf(s,
208 "\n ------------------------------------------\n\n");
210 return TRACE_TYPE_PARTIAL_LINE;
212 return TRACE_TYPE_HANDLED;
215 static struct ftrace_graph_ret_entry *
216 get_return_for_leaf(struct trace_iterator *iter,
217 struct ftrace_graph_ent_entry *curr)
219 struct ring_buffer_iter *ring_iter;
220 struct ring_buffer_event *event;
221 struct ftrace_graph_ret_entry *next;
223 ring_iter = iter->buffer_iter[iter->cpu];
225 /* First peek to compare current entry and the next one */
227 event = ring_buffer_iter_peek(ring_iter, NULL);
229 /* We need to consume the current entry to see the next one */
230 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
231 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
238 next = ring_buffer_event_data(event);
240 if (next->ent.type != TRACE_GRAPH_RET)
243 if (curr->ent.pid != next->ent.pid ||
244 curr->graph_ent.func != next->ret.func)
247 /* this is a leaf, now advance the iterator */
249 ring_buffer_read(ring_iter, NULL);
254 /* Signal a overhead of time execution to the output */
256 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
258 /* If duration disappear, we don't need anything */
259 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
262 /* Non nested entry or return */
264 return trace_seq_printf(s, " ");
266 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
267 /* Duration exceeded 100 msecs */
268 if (duration > 100000ULL)
269 return trace_seq_printf(s, "! ");
271 /* Duration exceeded 10 msecs */
272 if (duration > 10000ULL)
273 return trace_seq_printf(s, "+ ");
276 return trace_seq_printf(s, " ");
279 static enum print_line_t
280 print_graph_irq(struct trace_seq *s, unsigned long addr,
281 enum trace_type type, int cpu, pid_t pid)
285 if (addr < (unsigned long)__irqentry_text_start ||
286 addr >= (unsigned long)__irqentry_text_end)
287 return TRACE_TYPE_UNHANDLED;
290 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
291 ret = print_graph_cpu(s, cpu);
292 if (ret == TRACE_TYPE_PARTIAL_LINE)
293 return TRACE_TYPE_PARTIAL_LINE;
296 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
297 ret = print_graph_proc(s, pid);
298 if (ret == TRACE_TYPE_PARTIAL_LINE)
299 return TRACE_TYPE_PARTIAL_LINE;
300 ret = trace_seq_printf(s, " | ");
302 return TRACE_TYPE_PARTIAL_LINE;
306 ret = print_graph_overhead(-1, s);
308 return TRACE_TYPE_PARTIAL_LINE;
310 if (type == TRACE_GRAPH_ENT)
311 ret = trace_seq_printf(s, "==========>");
313 ret = trace_seq_printf(s, "<==========");
316 return TRACE_TYPE_PARTIAL_LINE;
318 /* Don't close the duration column if haven't one */
319 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
320 trace_seq_printf(s, " |");
321 ret = trace_seq_printf(s, "\n");
324 return TRACE_TYPE_PARTIAL_LINE;
325 return TRACE_TYPE_HANDLED;
328 static enum print_line_t
329 print_graph_duration(unsigned long long duration, struct trace_seq *s)
331 unsigned long nsecs_rem = do_div(duration, 1000);
332 /* log10(ULONG_MAX) + '\0' */
338 sprintf(msecs_str, "%lu", (unsigned long) duration);
341 ret = trace_seq_printf(s, "%s", msecs_str);
343 return TRACE_TYPE_PARTIAL_LINE;
345 len = strlen(msecs_str);
347 /* Print nsecs (we don't want to exceed 7 numbers) */
349 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
350 ret = trace_seq_printf(s, ".%s", nsecs_str);
352 return TRACE_TYPE_PARTIAL_LINE;
353 len += strlen(nsecs_str);
356 ret = trace_seq_printf(s, " us ");
358 return TRACE_TYPE_PARTIAL_LINE;
360 /* Print remaining spaces to fit the row's width */
361 for (i = len; i < 7; i++) {
362 ret = trace_seq_printf(s, " ");
364 return TRACE_TYPE_PARTIAL_LINE;
367 ret = trace_seq_printf(s, "| ");
369 return TRACE_TYPE_PARTIAL_LINE;
370 return TRACE_TYPE_HANDLED;
374 static int print_graph_abs_time(u64 t, struct trace_seq *s)
376 unsigned long usecs_rem;
378 usecs_rem = do_div(t, 1000000000);
381 return trace_seq_printf(s, "%5lu.%06lu | ",
382 (unsigned long)t, usecs_rem);
385 /* Case of a leaf function on its call entry */
386 static enum print_line_t
387 print_graph_entry_leaf(struct trace_iterator *iter,
388 struct ftrace_graph_ent_entry *entry,
389 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
391 struct ftrace_graph_ret *graph_ret;
392 struct ftrace_graph_ent *call;
393 unsigned long long duration;
397 graph_ret = &ret_entry->ret;
398 call = &entry->graph_ent;
399 duration = graph_ret->rettime - graph_ret->calltime;
402 ret = print_graph_overhead(duration, s);
404 return TRACE_TYPE_PARTIAL_LINE;
407 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
408 ret = print_graph_duration(duration, s);
409 if (ret == TRACE_TYPE_PARTIAL_LINE)
410 return TRACE_TYPE_PARTIAL_LINE;
414 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
415 ret = trace_seq_printf(s, " ");
417 return TRACE_TYPE_PARTIAL_LINE;
420 ret = seq_print_ip_sym(s, call->func, 0);
422 return TRACE_TYPE_PARTIAL_LINE;
424 ret = trace_seq_printf(s, "();\n");
426 return TRACE_TYPE_PARTIAL_LINE;
428 return TRACE_TYPE_HANDLED;
431 static enum print_line_t
432 print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
433 struct trace_seq *s, pid_t pid, int cpu)
437 struct ftrace_graph_ent *call = &entry->graph_ent;
440 ret = print_graph_overhead(-1, s);
442 return TRACE_TYPE_PARTIAL_LINE;
445 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
446 ret = trace_seq_printf(s, " | ");
448 return TRACE_TYPE_PARTIAL_LINE;
452 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
453 ret = trace_seq_printf(s, " ");
455 return TRACE_TYPE_PARTIAL_LINE;
458 ret = seq_print_ip_sym(s, call->func, 0);
460 return TRACE_TYPE_PARTIAL_LINE;
462 ret = trace_seq_printf(s, "() {\n");
464 return TRACE_TYPE_PARTIAL_LINE;
467 * we already consumed the current entry to check the next one
468 * and see if this is a leaf.
470 return TRACE_TYPE_NO_CONSUME;
473 static enum print_line_t
474 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
475 struct trace_iterator *iter)
479 pid_t *last_entry = iter->private;
480 struct trace_entry *ent = iter->ent;
481 struct ftrace_graph_ent *call = &field->graph_ent;
482 struct ftrace_graph_ret_entry *leaf_ret;
485 if (verif_pid(s, ent->pid, cpu, last_entry) == TRACE_TYPE_PARTIAL_LINE)
486 return TRACE_TYPE_PARTIAL_LINE;
489 ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, ent->pid);
490 if (ret == TRACE_TYPE_PARTIAL_LINE)
491 return TRACE_TYPE_PARTIAL_LINE;
494 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
495 ret = print_graph_abs_time(iter->ts, s);
497 return TRACE_TYPE_PARTIAL_LINE;
501 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
502 ret = print_graph_cpu(s, cpu);
503 if (ret == TRACE_TYPE_PARTIAL_LINE)
504 return TRACE_TYPE_PARTIAL_LINE;
508 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
509 ret = print_graph_proc(s, ent->pid);
510 if (ret == TRACE_TYPE_PARTIAL_LINE)
511 return TRACE_TYPE_PARTIAL_LINE;
513 ret = trace_seq_printf(s, " | ");
515 return TRACE_TYPE_PARTIAL_LINE;
518 leaf_ret = get_return_for_leaf(iter, field);
520 return print_graph_entry_leaf(iter, field, leaf_ret, s);
522 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
526 static enum print_line_t
527 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
528 struct trace_entry *ent, struct trace_iterator *iter)
533 pid_t *last_pid = iter->private;
534 unsigned long long duration = trace->rettime - trace->calltime;
537 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
538 return TRACE_TYPE_PARTIAL_LINE;
541 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
542 ret = print_graph_abs_time(iter->ts, s);
544 return TRACE_TYPE_PARTIAL_LINE;
548 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
549 ret = print_graph_cpu(s, cpu);
550 if (ret == TRACE_TYPE_PARTIAL_LINE)
551 return TRACE_TYPE_PARTIAL_LINE;
555 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
556 ret = print_graph_proc(s, ent->pid);
557 if (ret == TRACE_TYPE_PARTIAL_LINE)
558 return TRACE_TYPE_PARTIAL_LINE;
560 ret = trace_seq_printf(s, " | ");
562 return TRACE_TYPE_PARTIAL_LINE;
566 ret = print_graph_overhead(duration, s);
568 return TRACE_TYPE_PARTIAL_LINE;
571 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
572 ret = print_graph_duration(duration, s);
573 if (ret == TRACE_TYPE_PARTIAL_LINE)
574 return TRACE_TYPE_PARTIAL_LINE;
578 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
579 ret = trace_seq_printf(s, " ");
581 return TRACE_TYPE_PARTIAL_LINE;
584 ret = trace_seq_printf(s, "}\n");
586 return TRACE_TYPE_PARTIAL_LINE;
589 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
590 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
593 return TRACE_TYPE_PARTIAL_LINE;
596 ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
597 if (ret == TRACE_TYPE_PARTIAL_LINE)
598 return TRACE_TYPE_PARTIAL_LINE;
600 return TRACE_TYPE_HANDLED;
603 static enum print_line_t
604 print_graph_comment(struct print_entry *trace, struct trace_seq *s,
605 struct trace_entry *ent, struct trace_iterator *iter)
610 pid_t *last_pid = iter->private;
613 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
614 ret = print_graph_abs_time(iter->ts, s);
616 return TRACE_TYPE_PARTIAL_LINE;
620 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
621 return TRACE_TYPE_PARTIAL_LINE;
624 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
625 ret = print_graph_cpu(s, cpu);
626 if (ret == TRACE_TYPE_PARTIAL_LINE)
627 return TRACE_TYPE_PARTIAL_LINE;
631 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
632 ret = print_graph_proc(s, ent->pid);
633 if (ret == TRACE_TYPE_PARTIAL_LINE)
634 return TRACE_TYPE_PARTIAL_LINE;
636 ret = trace_seq_printf(s, " | ");
638 return TRACE_TYPE_PARTIAL_LINE;
642 ret = print_graph_overhead(-1, s);
644 return TRACE_TYPE_PARTIAL_LINE;
647 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
648 ret = trace_seq_printf(s, " | ");
650 return TRACE_TYPE_PARTIAL_LINE;
654 if (trace->depth > 0)
655 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
656 ret = trace_seq_printf(s, " ");
658 return TRACE_TYPE_PARTIAL_LINE;
662 ret = trace_seq_printf(s, "/* %s", trace->buf);
664 return TRACE_TYPE_PARTIAL_LINE;
666 /* Strip ending newline */
667 if (s->buffer[s->len - 1] == '\n') {
668 s->buffer[s->len - 1] = '\0';
672 ret = trace_seq_printf(s, " */\n");
674 return TRACE_TYPE_PARTIAL_LINE;
676 return TRACE_TYPE_HANDLED;
681 print_graph_function(struct trace_iterator *iter)
683 struct trace_seq *s = &iter->seq;
684 struct trace_entry *entry = iter->ent;
686 switch (entry->type) {
687 case TRACE_GRAPH_ENT: {
688 struct ftrace_graph_ent_entry *field;
689 trace_assign_type(field, entry);
690 return print_graph_entry(field, s, iter);
692 case TRACE_GRAPH_RET: {
693 struct ftrace_graph_ret_entry *field;
694 trace_assign_type(field, entry);
695 return print_graph_return(&field->ret, s, entry, iter);
698 struct print_entry *field;
699 trace_assign_type(field, entry);
700 return print_graph_comment(field, s, entry, iter);
703 return TRACE_TYPE_UNHANDLED;
707 static void print_graph_headers(struct seq_file *s)
711 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
712 seq_printf(s, " TIME ");
713 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
714 seq_printf(s, "CPU");
715 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
716 seq_printf(s, " TASK/PID ");
717 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
718 seq_printf(s, " DURATION ");
719 seq_printf(s, " FUNCTION CALLS\n");
723 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
724 seq_printf(s, " | ");
725 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
727 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
728 seq_printf(s, " | | ");
729 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
730 seq_printf(s, " | | ");
731 seq_printf(s, " | | | |\n");
734 static void graph_trace_open(struct trace_iterator *iter)
736 /* pid on the last trace processed */
737 pid_t *last_pid = alloc_percpu(pid_t);
741 pr_warning("function graph tracer: not enough memory\n");
743 for_each_possible_cpu(cpu) {
744 pid_t *pid = per_cpu_ptr(last_pid, cpu);
748 iter->private = last_pid;
751 static void graph_trace_close(struct trace_iterator *iter)
753 percpu_free(iter->private);
756 static struct tracer graph_trace __read_mostly = {
757 .name = "function_graph",
758 .open = graph_trace_open,
759 .close = graph_trace_close,
760 .init = graph_trace_init,
761 .reset = graph_trace_reset,
762 .print_line = print_graph_function,
763 .print_header = print_graph_headers,
764 .flags = &tracer_flags,
765 #ifdef CONFIG_FTRACE_SELFTEST
766 .selftest = trace_selftest_startup_function_graph,
770 static __init int init_graph_trace(void)
772 return register_tracer(&graph_trace);
775 device_initcall(init_graph_trace);