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 ------------------------------------------\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
)
235 static enum print_line_t
236 print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
238 unsigned long nsecs_rem
= do_div(duration
, 1000);
239 /* log10(ULONG_MAX) + '\0' */
245 sprintf(msecs_str
, "%lu", (unsigned long) duration
);
248 ret
= trace_seq_printf(s
, msecs_str
);
250 return TRACE_TYPE_PARTIAL_LINE
;
252 len
= strlen(msecs_str
);
254 /* Print nsecs (we don't want to exceed 7 numbers) */
256 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
257 ret
= trace_seq_printf(s
, ".%s", nsecs_str
);
259 return TRACE_TYPE_PARTIAL_LINE
;
260 len
+= strlen(nsecs_str
);
263 ret
= trace_seq_printf(s
, " us ");
265 return TRACE_TYPE_PARTIAL_LINE
;
267 /* Print remaining spaces to fit the row's width */
268 for (i
= len
; i
< 7; i
++) {
269 ret
= trace_seq_printf(s
, " ");
271 return TRACE_TYPE_PARTIAL_LINE
;
274 ret
= trace_seq_printf(s
, "| ");
276 return TRACE_TYPE_PARTIAL_LINE
;
277 return TRACE_TYPE_HANDLED
;
281 /* Signal a overhead of time execution to the output */
283 print_graph_overhead(unsigned long long duration
, struct trace_seq
*s
)
285 /* Duration exceeded 100 msecs */
286 if (duration
> 100000ULL)
287 return trace_seq_printf(s
, "! ");
289 /* Duration exceeded 10 msecs */
290 if (duration
> 10000ULL)
291 return trace_seq_printf(s
, "+ ");
293 return trace_seq_printf(s
, " ");
296 /* Case of a leaf function on its call entry */
297 static enum print_line_t
298 print_graph_entry_leaf(struct trace_iterator
*iter
,
299 struct ftrace_graph_ent_entry
*entry
, struct trace_seq
*s
)
301 struct ftrace_graph_ret_entry
*ret_entry
;
302 struct ftrace_graph_ret
*graph_ret
;
303 struct ring_buffer_event
*event
;
304 struct ftrace_graph_ent
*call
;
305 unsigned long long duration
;
309 event
= ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
310 ret_entry
= ring_buffer_event_data(event
);
311 graph_ret
= &ret_entry
->ret
;
312 call
= &entry
->graph_ent
;
313 duration
= graph_ret
->rettime
- graph_ret
->calltime
;
316 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
317 ret
= print_graph_overhead(duration
, s
);
319 return TRACE_TYPE_PARTIAL_LINE
;
323 ret
= print_graph_duration(duration
, s
);
324 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
325 return TRACE_TYPE_PARTIAL_LINE
;
328 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
329 ret
= trace_seq_printf(s
, " ");
331 return TRACE_TYPE_PARTIAL_LINE
;
334 ret
= seq_print_ip_sym(s
, call
->func
, 0);
336 return TRACE_TYPE_PARTIAL_LINE
;
338 ret
= trace_seq_printf(s
, "();\n");
340 return TRACE_TYPE_PARTIAL_LINE
;
342 return TRACE_TYPE_HANDLED
;
345 static enum print_line_t
346 print_graph_entry_nested(struct ftrace_graph_ent_entry
*entry
,
351 struct ftrace_graph_ent
*call
= &entry
->graph_ent
;
354 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
355 ret
= trace_seq_printf(s
, " ");
357 return TRACE_TYPE_PARTIAL_LINE
;
361 ret
= trace_seq_printf(s
, " | ");
364 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
365 ret
= trace_seq_printf(s
, " ");
367 return TRACE_TYPE_PARTIAL_LINE
;
370 ret
= seq_print_ip_sym(s
, call
->func
, 0);
372 return TRACE_TYPE_PARTIAL_LINE
;
374 ret
= trace_seq_printf(s
, "() {\n");
376 return TRACE_TYPE_PARTIAL_LINE
;
378 return TRACE_TYPE_HANDLED
;
381 static enum print_line_t
382 print_graph_entry(struct ftrace_graph_ent_entry
*field
, struct trace_seq
*s
,
383 struct trace_iterator
*iter
, int cpu
)
386 struct trace_entry
*ent
= iter
->ent
;
389 if (verif_pid(s
, ent
->pid
, cpu
) == TRACE_TYPE_PARTIAL_LINE
)
390 return TRACE_TYPE_PARTIAL_LINE
;
393 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
394 ret
= print_graph_cpu(s
, cpu
);
395 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
396 return TRACE_TYPE_PARTIAL_LINE
;
400 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
401 ret
= print_graph_proc(s
, ent
->pid
);
402 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
403 return TRACE_TYPE_PARTIAL_LINE
;
405 ret
= trace_seq_printf(s
, " | ");
407 return TRACE_TYPE_PARTIAL_LINE
;
410 if (trace_branch_is_leaf(iter
, field
))
411 return print_graph_entry_leaf(iter
, field
, s
);
413 return print_graph_entry_nested(field
, s
);
417 static enum print_line_t
418 print_graph_return(struct ftrace_graph_ret
*trace
, struct trace_seq
*s
,
419 struct trace_entry
*ent
, int cpu
)
423 unsigned long long duration
= trace
->rettime
- trace
->calltime
;
426 if (verif_pid(s
, ent
->pid
, cpu
) == TRACE_TYPE_PARTIAL_LINE
)
427 return TRACE_TYPE_PARTIAL_LINE
;
430 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
431 ret
= print_graph_cpu(s
, cpu
);
432 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
433 return TRACE_TYPE_PARTIAL_LINE
;
437 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
438 ret
= print_graph_proc(s
, ent
->pid
);
439 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
440 return TRACE_TYPE_PARTIAL_LINE
;
442 ret
= trace_seq_printf(s
, " | ");
444 return TRACE_TYPE_PARTIAL_LINE
;
448 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
449 ret
= print_graph_overhead(duration
, s
);
451 return TRACE_TYPE_PARTIAL_LINE
;
455 ret
= print_graph_duration(duration
, s
);
456 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
457 return TRACE_TYPE_PARTIAL_LINE
;
460 for (i
= 0; i
< trace
->depth
* TRACE_GRAPH_INDENT
; i
++) {
461 ret
= trace_seq_printf(s
, " ");
463 return TRACE_TYPE_PARTIAL_LINE
;
466 ret
= trace_seq_printf(s
, "}\n");
468 return TRACE_TYPE_PARTIAL_LINE
;
471 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERRUN
) {
472 ret
= trace_seq_printf(s
, " (Overruns: %lu)\n",
475 return TRACE_TYPE_PARTIAL_LINE
;
477 return TRACE_TYPE_HANDLED
;
481 print_graph_function(struct trace_iterator
*iter
)
483 struct trace_seq
*s
= &iter
->seq
;
484 struct trace_entry
*entry
= iter
->ent
;
486 switch (entry
->type
) {
487 case TRACE_GRAPH_ENT
: {
488 struct ftrace_graph_ent_entry
*field
;
489 trace_assign_type(field
, entry
);
490 return print_graph_entry(field
, s
, iter
,
493 case TRACE_GRAPH_RET
: {
494 struct ftrace_graph_ret_entry
*field
;
495 trace_assign_type(field
, entry
);
496 return print_graph_return(&field
->ret
, s
, entry
, iter
->cpu
);
499 return TRACE_TYPE_UNHANDLED
;
503 static struct tracer graph_trace __read_mostly
= {
504 .name
= "function_graph",
505 .init
= graph_trace_init
,
506 .reset
= graph_trace_reset
,
507 .print_line
= print_graph_function
,
508 .flags
= &tracer_flags
,
511 static __init
int init_graph_trace(void)
513 return register_tracer(&graph_trace
);
516 device_initcall(init_graph_trace
);