Skip to content

Commit 0706f1c

Browse files
author
Steven Rostedt
committed
tracing: adding function timings to function profiler
If the function graph trace is enabled, the function profiler will use it to take the timing of the functions. cat /debug/tracing/trace_stat/functions Function Hit Time -------- --- ---- mwait_idle 127 183028.4 us schedule 26 151997.7 us __schedule 31 151975.1 us sys_wait4 2 74080.53 us do_wait 2 74077.80 us sys_newlstat 138 39929.16 us do_path_lookup 179 39845.79 us vfs_lstat_fd 138 39761.97 us user_path_at 153 39469.58 us path_walk 179 39435.76 us __link_path_walk 189 39143.73 us [...] Note the times are skewed due to the function graph tracer not taking into account schedules. Signed-off-by: Steven Rostedt <srostedt@redhat.com>
1 parent 493762f commit 0706f1c

6 files changed

Lines changed: 117 additions & 19 deletions

File tree

‎kernel/trace/ftrace.c‎

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include <asm/ftrace.h>
3535

36-
#include "trace.h"
36+
#include "trace_output.h"
3737
#include "trace_stat.h"
3838

3939
#define FTRACE_WARN_ON(cond) \
@@ -246,6 +246,9 @@ struct ftrace_profile {
246246
struct hlist_node node;
247247
unsigned long ip;
248248
unsigned long counter;
249+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
250+
unsigned long long time;
251+
#endif
249252
};
250253

251254
struct ftrace_profile_page {
@@ -303,6 +306,22 @@ static void *function_stat_start(struct tracer_stat *trace)
303306
return function_stat_next(&profile_pages_start->records[0], 0);
304307
}
305308

309+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
310+
/* function graph compares on total time */
311+
static int function_stat_cmp(void *p1, void *p2)
312+
{
313+
struct ftrace_profile *a = p1;
314+
struct ftrace_profile *b = p2;
315+
316+
if (a->time < b->time)
317+
return -1;
318+
if (a->time > b->time)
319+
return 1;
320+
else
321+
return 0;
322+
}
323+
#else
324+
/* not function graph compares against hits */
306325
static int function_stat_cmp(void *p1, void *p2)
307326
{
308327
struct ftrace_profile *a = p1;
@@ -315,22 +334,43 @@ static int function_stat_cmp(void *p1, void *p2)
315334
else
316335
return 0;
317336
}
337+
#endif
318338

319339
static int function_stat_headers(struct seq_file *m)
320340
{
341+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
342+
seq_printf(m, " Function Hit Time\n"
343+
" -------- --- ----\n");
344+
#else
321345
seq_printf(m, " Function Hit\n"
322346
" -------- ---\n");
347+
#endif
323348
return 0;
324349
}
325350

326351
static int function_stat_show(struct seq_file *m, void *v)
327352
{
328353
struct ftrace_profile *rec = v;
329354
char str[KSYM_SYMBOL_LEN];
355+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
356+
static struct trace_seq s;
357+
static DEFINE_MUTEX(mutex);
358+
359+
mutex_lock(&mutex);
360+
trace_seq_init(&s);
361+
trace_print_graph_duration(rec->time, &s);
362+
#endif
330363

331364
kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
365+
seq_printf(m, " %-30.30s %10lu", str, rec->counter);
366+
367+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
368+
seq_printf(m, " ");
369+
trace_print_seq(m, &s);
370+
mutex_unlock(&mutex);
371+
#endif
372+
seq_putc(m, '\n');
332373

333-
seq_printf(m, " %-30.30s %10lu\n", str, rec->counter);
334374
return 0;
335375
}
336376

@@ -534,11 +574,52 @@ function_profile_call(unsigned long ip, unsigned long parent_ip)
534574
local_irq_restore(flags);
535575
}
536576

577+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
578+
static int profile_graph_entry(struct ftrace_graph_ent *trace)
579+
{
580+
function_profile_call(trace->func, 0);
581+
return 1;
582+
}
583+
584+
static void profile_graph_return(struct ftrace_graph_ret *trace)
585+
{
586+
unsigned long flags;
587+
struct ftrace_profile *rec;
588+
589+
local_irq_save(flags);
590+
rec = ftrace_find_profiled_func(trace->func);
591+
if (rec)
592+
rec->time += trace->rettime - trace->calltime;
593+
local_irq_restore(flags);
594+
}
595+
596+
static int register_ftrace_profiler(void)
597+
{
598+
return register_ftrace_graph(&profile_graph_return,
599+
&profile_graph_entry);
600+
}
601+
602+
static void unregister_ftrace_profiler(void)
603+
{
604+
unregister_ftrace_graph();
605+
}
606+
#else
537607
static struct ftrace_ops ftrace_profile_ops __read_mostly =
538608
{
539609
.func = function_profile_call,
540610
};
541611

612+
static int register_ftrace_profiler(void)
613+
{
614+
return register_ftrace_function(&ftrace_profile_ops);
615+
}
616+
617+
static void unregister_ftrace_profiler(void)
618+
{
619+
unregister_ftrace_function(&ftrace_profile_ops);
620+
}
621+
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
622+
542623
static ssize_t
543624
ftrace_profile_write(struct file *filp, const char __user *ubuf,
544625
size_t cnt, loff_t *ppos)
@@ -570,11 +651,15 @@ ftrace_profile_write(struct file *filp, const char __user *ubuf,
570651
goto out;
571652
}
572653

573-
register_ftrace_function(&ftrace_profile_ops);
654+
ret = register_ftrace_profiler();
655+
if (ret < 0) {
656+
cnt = ret;
657+
goto out;
658+
}
574659
ftrace_profile_enabled = 1;
575660
} else {
576661
ftrace_profile_enabled = 0;
577-
unregister_ftrace_function(&ftrace_profile_ops);
662+
unregister_ftrace_profiler();
578663
}
579664
}
580665
out:

‎kernel/trace/trace.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,6 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
402402
return cnt;
403403
}
404404

405-
static void
406-
trace_print_seq(struct seq_file *m, struct trace_seq *s)
407-
{
408-
int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
409-
410-
s->buffer[len] = 0;
411-
seq_puts(m, s->buffer);
412-
413-
trace_seq_init(s);
414-
}
415-
416405
/**
417406
* update_max_tr - snapshot all trace buffers from global_trace to max_tr
418407
* @tr: tracer

‎kernel/trace/trace.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ extern unsigned long trace_flags;
605605
/* Standard output formatting function used for function return traces */
606606
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
607607
extern enum print_line_t print_graph_function(struct trace_iterator *iter);
608+
extern enum print_line_t
609+
trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
608610

609611
#ifdef CONFIG_DYNAMIC_FTRACE
610612
/* TODO: make this variable */
@@ -636,7 +638,6 @@ static inline int ftrace_graph_addr(unsigned long addr)
636638
return 1;
637639
}
638640
#endif /* CONFIG_DYNAMIC_FTRACE */
639-
640641
#else /* CONFIG_FUNCTION_GRAPH_TRACER */
641642
static inline enum print_line_t
642643
print_graph_function(struct trace_iterator *iter)

‎kernel/trace/trace_functions_graph.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ print_graph_irq(struct trace_iterator *iter, unsigned long addr,
426426
return TRACE_TYPE_HANDLED;
427427
}
428428

429-
static enum print_line_t
430-
print_graph_duration(unsigned long long duration, struct trace_seq *s)
429+
enum print_line_t
430+
trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
431431
{
432432
unsigned long nsecs_rem = do_div(duration, 1000);
433433
/* log10(ULONG_MAX) + '\0' */
@@ -464,12 +464,23 @@ print_graph_duration(unsigned long long duration, struct trace_seq *s)
464464
if (!ret)
465465
return TRACE_TYPE_PARTIAL_LINE;
466466
}
467+
return TRACE_TYPE_HANDLED;
468+
}
469+
470+
static enum print_line_t
471+
print_graph_duration(unsigned long long duration, struct trace_seq *s)
472+
{
473+
int ret;
474+
475+
ret = trace_print_graph_duration(duration, s);
476+
if (ret != TRACE_TYPE_HANDLED)
477+
return ret;
467478

468479
ret = trace_seq_printf(s, "| ");
469480
if (!ret)
470481
return TRACE_TYPE_PARTIAL_LINE;
471-
return TRACE_TYPE_HANDLED;
472482

483+
return TRACE_TYPE_HANDLED;
473484
}
474485

475486
/* Case of a leaf function on its call entry */

‎kernel/trace/trace_output.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
1919

2020
static int next_event_type = __TRACE_LAST_TYPE + 1;
2121

22+
void trace_print_seq(struct seq_file *m, struct trace_seq *s)
23+
{
24+
int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
25+
26+
s->buffer[len] = 0;
27+
seq_puts(m, s->buffer);
28+
29+
trace_seq_init(s);
30+
}
31+
2232
enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
2333
{
2434
struct trace_seq *s = &iter->seq;

‎kernel/trace/trace_output.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ trace_print_bprintk_msg_only(struct trace_iterator *iter);
2020
extern enum print_line_t
2121
trace_print_printk_msg_only(struct trace_iterator *iter);
2222

23+
extern void trace_print_seq(struct seq_file *m, struct trace_seq *s);
24+
2325
extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
2426
__attribute__ ((format (printf, 2, 3)));
2527
extern int

0 commit comments

Comments
 (0)