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_reset_online_cpus(tr);
60 tracing_start_cmdline_record();
65 static void graph_trace_reset(struct trace_array *tr)
67 tracing_stop_cmdline_record();
68 unregister_ftrace_graph();
71 static inline int log10_cpu(int nb)
80 static enum print_line_t
81 print_graph_cpu(struct trace_seq *s, int cpu)
85 int log10_this = log10_cpu(cpu);
86 int log10_all = log10_cpu(cpumask_weight(cpu_online_mask));
90 * Start with a space character - to make it stand out
91 * to the right a bit when trace output is pasted into
94 ret = trace_seq_printf(s, " ");
97 * Tricky - we space the CPU field according to the max
98 * number of online CPUs. On a 2-cpu system it would take
99 * a maximum of 1 digit - on a 128 cpu system it would
100 * take up to 3 digits:
102 for (i = 0; i < log10_all - log10_this; i++) {
103 ret = trace_seq_printf(s, " ");
105 return TRACE_TYPE_PARTIAL_LINE;
107 ret = trace_seq_printf(s, "%d) ", cpu);
109 return TRACE_TYPE_PARTIAL_LINE;
111 return TRACE_TYPE_HANDLED;
114 #define TRACE_GRAPH_PROCINFO_LENGTH 14
116 static enum print_line_t
117 print_graph_proc(struct trace_seq *s, pid_t pid)
124 /* sign + log10(MAX_INT) + '\0' */
127 strncpy(comm, trace_find_cmdline(pid), 7);
129 sprintf(pid_str, "%d", pid);
131 /* 1 stands for the "-" character */
132 len = strlen(comm) + strlen(pid_str) + 1;
134 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
135 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
137 /* First spaces to align center */
138 for (i = 0; i < spaces / 2; i++) {
139 ret = trace_seq_printf(s, " ");
141 return TRACE_TYPE_PARTIAL_LINE;
144 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
146 return TRACE_TYPE_PARTIAL_LINE;
148 /* Last spaces to align center */
149 for (i = 0; i < spaces - (spaces / 2); i++) {
150 ret = trace_seq_printf(s, " ");
152 return TRACE_TYPE_PARTIAL_LINE;
154 return TRACE_TYPE_HANDLED;
158 /* If the pid changed since the last trace, output this event */
159 static enum print_line_t
160 verif_pid(struct trace_seq *s, pid_t pid, int cpu, pid_t *last_pids_cpu)
167 return TRACE_TYPE_HANDLED;
169 last_pid = per_cpu_ptr(last_pids_cpu, cpu);
171 if (*last_pid == pid)
172 return TRACE_TYPE_HANDLED;
174 prev_pid = *last_pid;
178 return TRACE_TYPE_HANDLED;
180 * Context-switch trace line:
182 ------------------------------------------
183 | 1) migration/0--1 => sshd-1755
184 ------------------------------------------
187 ret = trace_seq_printf(s,
188 " ------------------------------------------\n");
190 TRACE_TYPE_PARTIAL_LINE;
192 ret = print_graph_cpu(s, cpu);
193 if (ret == TRACE_TYPE_PARTIAL_LINE)
194 TRACE_TYPE_PARTIAL_LINE;
196 ret = print_graph_proc(s, prev_pid);
197 if (ret == TRACE_TYPE_PARTIAL_LINE)
198 TRACE_TYPE_PARTIAL_LINE;
200 ret = trace_seq_printf(s, " => ");
202 TRACE_TYPE_PARTIAL_LINE;
204 ret = print_graph_proc(s, pid);
205 if (ret == TRACE_TYPE_PARTIAL_LINE)
206 TRACE_TYPE_PARTIAL_LINE;
208 ret = trace_seq_printf(s,
209 "\n ------------------------------------------\n\n");
211 TRACE_TYPE_PARTIAL_LINE;
217 trace_branch_is_leaf(struct trace_iterator *iter,
218 struct ftrace_graph_ent_entry *curr)
220 struct ring_buffer_iter *ring_iter;
221 struct ring_buffer_event *event;
222 struct ftrace_graph_ret_entry *next;
224 ring_iter = iter->buffer_iter[iter->cpu];
229 event = ring_buffer_iter_peek(ring_iter, NULL);
234 next = ring_buffer_event_data(event);
236 if (next->ent.type != TRACE_GRAPH_RET)
239 if (curr->ent.pid != next->ent.pid ||
240 curr->graph_ent.func != next->ret.func)
246 /* Signal a overhead of time execution to the output */
248 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
250 /* If duration disappear, we don't need anything */
251 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
254 /* Non nested entry or return */
256 return trace_seq_printf(s, " ");
258 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
259 /* Duration exceeded 100 msecs */
260 if (duration > 100000ULL)
261 return trace_seq_printf(s, "! ");
263 /* Duration exceeded 10 msecs */
264 if (duration > 10000ULL)
265 return trace_seq_printf(s, "+ ");
268 return trace_seq_printf(s, " ");
271 static enum print_line_t
272 print_graph_irq(struct trace_seq *s, unsigned long addr,
273 enum trace_type type, int cpu, pid_t pid)
277 if (addr < (unsigned long)__irqentry_text_start ||
278 addr >= (unsigned long)__irqentry_text_end)
279 return TRACE_TYPE_UNHANDLED;
282 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
283 ret = print_graph_cpu(s, cpu);
284 if (ret == TRACE_TYPE_PARTIAL_LINE)
285 return TRACE_TYPE_PARTIAL_LINE;
288 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
289 ret = print_graph_proc(s, pid);
290 if (ret == TRACE_TYPE_PARTIAL_LINE)
291 return TRACE_TYPE_PARTIAL_LINE;
292 ret = trace_seq_printf(s, " | ");
294 return TRACE_TYPE_PARTIAL_LINE;
298 ret = print_graph_overhead(-1, s);
300 return TRACE_TYPE_PARTIAL_LINE;
302 if (type == TRACE_GRAPH_ENT)
303 ret = trace_seq_printf(s, "==========>");
305 ret = trace_seq_printf(s, "<==========");
308 return TRACE_TYPE_PARTIAL_LINE;
310 /* Don't close the duration column if haven't one */
311 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
312 trace_seq_printf(s, " |");
313 ret = trace_seq_printf(s, "\n");
316 return TRACE_TYPE_PARTIAL_LINE;
317 return TRACE_TYPE_HANDLED;
320 static enum print_line_t
321 print_graph_duration(unsigned long long duration, struct trace_seq *s)
323 unsigned long nsecs_rem = do_div(duration, 1000);
324 /* log10(ULONG_MAX) + '\0' */
330 sprintf(msecs_str, "%lu", (unsigned long) duration);
333 ret = trace_seq_printf(s, "%s", msecs_str);
335 return TRACE_TYPE_PARTIAL_LINE;
337 len = strlen(msecs_str);
339 /* Print nsecs (we don't want to exceed 7 numbers) */
341 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
342 ret = trace_seq_printf(s, ".%s", nsecs_str);
344 return TRACE_TYPE_PARTIAL_LINE;
345 len += strlen(nsecs_str);
348 ret = trace_seq_printf(s, " us ");
350 return TRACE_TYPE_PARTIAL_LINE;
352 /* Print remaining spaces to fit the row's width */
353 for (i = len; i < 7; i++) {
354 ret = trace_seq_printf(s, " ");
356 return TRACE_TYPE_PARTIAL_LINE;
359 ret = trace_seq_printf(s, "| ");
361 return TRACE_TYPE_PARTIAL_LINE;
362 return TRACE_TYPE_HANDLED;
366 static int print_graph_abs_time(u64 t, struct trace_seq *s)
368 unsigned long usecs_rem;
370 usecs_rem = do_div(t, 1000000000);
373 return trace_seq_printf(s, "%5lu.%06lu | ",
374 (unsigned long)t, usecs_rem);
377 /* Case of a leaf function on its call entry */
378 static enum print_line_t
379 print_graph_entry_leaf(struct trace_iterator *iter,
380 struct ftrace_graph_ent_entry *entry, struct trace_seq *s)
382 struct ftrace_graph_ret_entry *ret_entry;
383 struct ftrace_graph_ret *graph_ret;
384 struct ring_buffer_event *event;
385 struct ftrace_graph_ent *call;
386 unsigned long long duration;
390 event = ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
391 ret_entry = ring_buffer_event_data(event);
392 graph_ret = &ret_entry->ret;
393 call = &entry->graph_ent;
394 duration = graph_ret->rettime - graph_ret->calltime;
397 ret = print_graph_overhead(duration, s);
399 return TRACE_TYPE_PARTIAL_LINE;
402 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
403 ret = print_graph_duration(duration, s);
404 if (ret == TRACE_TYPE_PARTIAL_LINE)
405 return TRACE_TYPE_PARTIAL_LINE;
409 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
410 ret = trace_seq_printf(s, " ");
412 return TRACE_TYPE_PARTIAL_LINE;
415 ret = seq_print_ip_sym(s, call->func, 0);
417 return TRACE_TYPE_PARTIAL_LINE;
419 ret = trace_seq_printf(s, "();\n");
421 return TRACE_TYPE_PARTIAL_LINE;
423 return TRACE_TYPE_HANDLED;
426 static enum print_line_t
427 print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
428 struct trace_seq *s, pid_t pid, int cpu)
432 struct ftrace_graph_ent *call = &entry->graph_ent;
435 ret = print_graph_overhead(-1, s);
437 return TRACE_TYPE_PARTIAL_LINE;
440 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
441 ret = trace_seq_printf(s, " | ");
443 return TRACE_TYPE_PARTIAL_LINE;
447 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
448 ret = trace_seq_printf(s, " ");
450 return TRACE_TYPE_PARTIAL_LINE;
453 ret = seq_print_ip_sym(s, call->func, 0);
455 return TRACE_TYPE_PARTIAL_LINE;
457 ret = trace_seq_printf(s, "() {\n");
459 return TRACE_TYPE_PARTIAL_LINE;
461 return TRACE_TYPE_HANDLED;
464 static enum print_line_t
465 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
466 struct trace_iterator *iter)
470 pid_t *last_entry = iter->private;
471 struct trace_entry *ent = iter->ent;
472 struct ftrace_graph_ent *call = &field->graph_ent;
475 if (verif_pid(s, ent->pid, cpu, last_entry) == TRACE_TYPE_PARTIAL_LINE)
476 return TRACE_TYPE_PARTIAL_LINE;
479 ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, ent->pid);
480 if (ret == TRACE_TYPE_PARTIAL_LINE)
481 return TRACE_TYPE_PARTIAL_LINE;
484 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
485 ret = print_graph_abs_time(iter->ts, s);
487 return TRACE_TYPE_PARTIAL_LINE;
491 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
492 ret = print_graph_cpu(s, cpu);
493 if (ret == TRACE_TYPE_PARTIAL_LINE)
494 return TRACE_TYPE_PARTIAL_LINE;
498 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
499 ret = print_graph_proc(s, ent->pid);
500 if (ret == TRACE_TYPE_PARTIAL_LINE)
501 return TRACE_TYPE_PARTIAL_LINE;
503 ret = trace_seq_printf(s, " | ");
505 return TRACE_TYPE_PARTIAL_LINE;
508 if (trace_branch_is_leaf(iter, field))
509 return print_graph_entry_leaf(iter, field, s);
511 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
515 static enum print_line_t
516 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
517 struct trace_entry *ent, struct trace_iterator *iter)
522 pid_t *last_pid = iter->private;
523 unsigned long long duration = trace->rettime - trace->calltime;
526 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
527 return TRACE_TYPE_PARTIAL_LINE;
530 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
531 ret = print_graph_abs_time(iter->ts, s);
533 return TRACE_TYPE_PARTIAL_LINE;
537 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
538 ret = print_graph_cpu(s, cpu);
539 if (ret == TRACE_TYPE_PARTIAL_LINE)
540 return TRACE_TYPE_PARTIAL_LINE;
544 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
545 ret = print_graph_proc(s, ent->pid);
546 if (ret == TRACE_TYPE_PARTIAL_LINE)
547 return TRACE_TYPE_PARTIAL_LINE;
549 ret = trace_seq_printf(s, " | ");
551 return TRACE_TYPE_PARTIAL_LINE;
555 ret = print_graph_overhead(duration, s);
557 return TRACE_TYPE_PARTIAL_LINE;
560 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
561 ret = print_graph_duration(duration, s);
562 if (ret == TRACE_TYPE_PARTIAL_LINE)
563 return TRACE_TYPE_PARTIAL_LINE;
567 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
568 ret = trace_seq_printf(s, " ");
570 return TRACE_TYPE_PARTIAL_LINE;
573 ret = trace_seq_printf(s, "}\n");
575 return TRACE_TYPE_PARTIAL_LINE;
578 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
579 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
582 return TRACE_TYPE_PARTIAL_LINE;
585 ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
586 if (ret == TRACE_TYPE_PARTIAL_LINE)
587 return TRACE_TYPE_PARTIAL_LINE;
589 return TRACE_TYPE_HANDLED;
592 static enum print_line_t
593 print_graph_comment(struct print_entry *trace, struct trace_seq *s,
594 struct trace_entry *ent, struct trace_iterator *iter)
599 pid_t *last_pid = iter->private;
602 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
603 ret = print_graph_abs_time(iter->ts, s);
605 return TRACE_TYPE_PARTIAL_LINE;
609 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
610 return TRACE_TYPE_PARTIAL_LINE;
613 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
614 ret = print_graph_cpu(s, cpu);
615 if (ret == TRACE_TYPE_PARTIAL_LINE)
616 return TRACE_TYPE_PARTIAL_LINE;
620 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
621 ret = print_graph_proc(s, ent->pid);
622 if (ret == TRACE_TYPE_PARTIAL_LINE)
623 return TRACE_TYPE_PARTIAL_LINE;
625 ret = trace_seq_printf(s, " | ");
627 return TRACE_TYPE_PARTIAL_LINE;
631 ret = print_graph_overhead(-1, s);
633 return TRACE_TYPE_PARTIAL_LINE;
636 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
637 ret = trace_seq_printf(s, " | ");
639 return TRACE_TYPE_PARTIAL_LINE;
643 if (trace->depth > 0)
644 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
645 ret = trace_seq_printf(s, " ");
647 return TRACE_TYPE_PARTIAL_LINE;
651 ret = trace_seq_printf(s, "/* %s", trace->buf);
653 return TRACE_TYPE_PARTIAL_LINE;
655 /* Strip ending newline */
656 if (s->buffer[s->len - 1] == '\n') {
657 s->buffer[s->len - 1] = '\0';
661 ret = trace_seq_printf(s, " */\n");
663 return TRACE_TYPE_PARTIAL_LINE;
665 return TRACE_TYPE_HANDLED;
670 print_graph_function(struct trace_iterator *iter)
672 struct trace_seq *s = &iter->seq;
673 struct trace_entry *entry = iter->ent;
675 switch (entry->type) {
676 case TRACE_GRAPH_ENT: {
677 struct ftrace_graph_ent_entry *field;
678 trace_assign_type(field, entry);
679 return print_graph_entry(field, s, iter);
681 case TRACE_GRAPH_RET: {
682 struct ftrace_graph_ret_entry *field;
683 trace_assign_type(field, entry);
684 return print_graph_return(&field->ret, s, entry, iter);
687 struct print_entry *field;
688 trace_assign_type(field, entry);
689 return print_graph_comment(field, s, entry, iter);
692 return TRACE_TYPE_UNHANDLED;
696 static void print_graph_headers(struct seq_file *s)
700 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
701 seq_printf(s, " TIME ");
702 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
703 seq_printf(s, "CPU");
704 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
705 seq_printf(s, " TASK/PID ");
706 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
707 seq_printf(s, " DURATION ");
708 seq_printf(s, " FUNCTION CALLS\n");
712 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
713 seq_printf(s, " | ");
714 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
716 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
717 seq_printf(s, " | | ");
718 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
719 seq_printf(s, " | | ");
720 seq_printf(s, " | | | |\n");
723 static void graph_trace_open(struct trace_iterator *iter)
725 /* pid on the last trace processed */
726 pid_t *last_pid = alloc_percpu(pid_t);
730 pr_warning("function graph tracer: not enough memory\n");
732 for_each_possible_cpu(cpu) {
733 pid_t *pid = per_cpu_ptr(last_pid, cpu);
737 iter->private = last_pid;
740 static void graph_trace_close(struct trace_iterator *iter)
742 percpu_free(iter->private);
745 static struct tracer graph_trace __read_mostly = {
746 .name = "function_graph",
747 .open = graph_trace_open,
748 .close = graph_trace_close,
749 .init = graph_trace_init,
750 .reset = graph_trace_reset,
751 .print_line = print_graph_function,
752 .print_header = print_graph_headers,
753 .flags = &tracer_flags,
756 static __init int init_graph_trace(void)
758 return register_tracer(&graph_trace);
761 device_initcall(init_graph_trace);