ftrace: Add support to set_graph_notrace filter (v2)
cover ftrace: Add support to set_graph_notrace filter (v2)
Hi Steve,
I broke the patch into pieces so that it can address what you said. The patch 1 and 2 are cleanup and preparation you mentioned. Patch 3 is an one more cleanup I found during the work. Patch 4 is core of this patchset and main diferrence to prior version is added comments.
This is based on the current linux-trace/for-next.
I also put this on 'ftrace/nograph-v2' branch in my tree
git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git
Any comments are welcome, thanks.
Namhyung
Namhyung Kim (4):
ftrace: Get rid of ftrace_graph_filter_enabled
ftrace: Introduce struct ftrace_graph_data
ftrace: Narrow down the protected area of graph_lock
ftrace: Add set_graph_notrace filter
include/linux/ftrace.h | 1 + kernel/trace/ftrace.c | 140 ++++++++++++++++++++++++++--------- kernel/trace/trace.h | 25 ++++++- kernel/trace/trace_functions_graph.c | 56 +++++++++++++- 4 files changed, 181 insertions(+), 41 deletions(-)
Signature · 1 line
1.7.11.7
PATCH 1/4 ftrace: Get rid of ftrace_graph_filter_enabled
From: Namhyung Kim <[email protected]>
The ftrace_graph_filter_enabled means that user sets function filter and it always has same meaning of ftrace_graph_count > 0.
--- kernel/trace/ftrace.c | 6 +----- kernel/trace/trace.h | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +1 -5
@@ -3776,7 +3776,6 @@ static const struct file_operations ftrace_notrace_fops = {static DEFINE_MUTEX (graph_lock);int ftrace_graph_count; -int ftrace_graph_filter_enabled;unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS ]__read_mostly ; static void *
@@ -3799,7 +3798,7 @@ static void *g_start(struct seq_file *m, loff_t *pos)mutex_lock (&graph_lock);/* Nothing, tell g_show to print all functions are enabled */ -if (!ftrace_graph_filter_enabled && !*pos) +if (!ftrace_graph_count && !*pos)return (void *)1 ;return __g_next (m, pos);
@@ -3845,7 +3844,6 @@ ftrace_graph_open(struct inode *inode, struct file *file)mutex_lock (&graph_lock);if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { - ftrace_graph_filter_enabled =0 ; ftrace_graph_count =0 ;memset (ftrace_graph_funcs,0 ,sizeof (ftrace_graph_funcs)); }
@@ -3925,8 +3923,6 @@ out:if (fail)return -EINVAL ; - ftrace_graph_filter_enabled = !!(*idx); -return 0 ; }
kernel/trace/trace.h +1 -2
@@ -730,7 +730,6 @@ extern void __trace_graph_return(struct trace_array *tr,#ifdef CONFIG_DYNAMIC_FTRACE /* TODO: make this variable */ #define FTRACE_GRAPH_MAX_FUNCS 32 -extern int ftrace_graph_filter_enabled;extern int ftrace_graph_count;extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS ];
@@ -738,7 +737,7 @@ static inline int ftrace_graph_addr(unsigned long addr) {int i; -if (!ftrace_graph_filter_enabled) +if (!ftrace_graph_count)return 1 ;for (i =0 ; i < ftrace_graph_count; i++) {
Signature · 1 line
1.7.11.7
PATCH 2/4 ftrace: Introduce struct ftrace_graph_data
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
The struct ftrace_graph_data is for generalizing the access to set_graph_function file. This is a preparation for adding support to set_graph_notrace.
--- kernel/trace/ftrace.c | 81 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 19 deletions(-)
kernel/trace/ftrace.c +62 -19
@@ -3641,7 +3641,7 @@ __setup("ftrace_filter=", set_ftrace_filter);#ifdef CONFIG_FUNCTION_GRAPH_TRACER static char ftrace_graph_buf[FTRACE_FILTER_SIZE ]__initdata ; -static int ftrace_set_func (unsigned long *array,int *idx,char *buffer); +static int ftrace_set_func (unsigned long *array,int *idx,int size,char *buffer);static int __init set_graph_function (char *str) {
@@ -3659,7 +3659,7 @@ static void __init set_ftrace_early_graph(char *buf) func =strsep (&buf,"," );/* we allow only one expression at a time */ ret =ftrace_set_func (ftrace_graph_funcs, &ftrace_graph_count, - func); +FTRACE_GRAPH_MAX_FUNCS , func);if (ret)printk (KERN_DEBUG "ftrace: function %s not " "traceable\n" , func);
@@ -3778,12 +3778,21 @@ static DEFINE_MUTEX(graph_lock);int ftrace_graph_count;unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS ]__read_mostly ; +struct ftrace_graph_data { +unsigned long *table; +size_t size; +int *count; +const struct seq_operations *seq_ops; +}; +static void *__g_next (struct seq_file *m,loff_t *pos) { -if (*pos >= ftrace_graph_count) +struct ftrace_graph_data *fgd = m->private; + +if (*pos >= *fgd->count)return NULL ; -return &ftrace_graph_funcs[*pos]; +return &fgd->table[*pos]; } static void *
@@ -3795,10 +3804,12 @@ g_next(struct seq_file *m, void *v, loff_t *pos)static void *g_start (struct seq_file *m,loff_t *pos) { +struct ftrace_graph_data *fgd = m->private; +mutex_lock (&graph_lock);/* Nothing, tell g_show to print all functions are enabled */ -if (!ftrace_graph_count && !*pos) +if (!*fgd->count && !*pos)return (void *)1 ;return __g_next (m, pos);
@@ -3834,37 +3845,68 @@ static const struct seq_operations ftrace_graph_seq_ops = { };static int -ftrace_graph_open (struct inode *inode,struct file *file) +__ftrace_graph_open (struct inode *inode,struct file *file, +struct ftrace_graph_data *fgd) {int ret =0 ; -if (unlikely (ftrace_disabled)) -return -ENODEV ; -mutex_lock (&graph_lock);if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { - ftrace_graph_count =0 ; -memset (ftrace_graph_funcs,0 ,sizeof (ftrace_graph_funcs)); + *fgd->count =0 ; +memset (fgd->table,0 , fgd->size *sizeof (*fgd->table)); }mutex_unlock (&graph_lock); -if (file->f_mode &FMODE_READ ) - ret =seq_open (file, &ftrace_graph_seq_ops); +if (file->f_mode &FMODE_READ ) { + ret =seq_open (file, fgd->seq_ops); +if (!ret) { +struct seq_file *m = file->private_data; + m->private = fgd; + } + }else + file->private_data = fgd;return ret; }static int +ftrace_graph_open (struct inode *inode,struct file *file) +{ +struct ftrace_graph_data *fgd; + +if (unlikely (ftrace_disabled)) +return -ENODEV ; + + fgd =kmalloc (sizeof (*fgd),GFP_KERNEL ); +if (fgd ==NULL ) +return -ENOMEM ; + + fgd->table = ftrace_graph_funcs; + fgd->size =FTRACE_GRAPH_MAX_FUNCS ; + fgd->count = &ftrace_graph_count; + fgd->seq_ops = &ftrace_graph_seq_ops; + +return __ftrace_graph_open (inode, file, fgd); +} + +static int ftrace_graph_release (struct inode *inode,struct file *file) { -if (file->f_mode &FMODE_READ ) +if (file->f_mode &FMODE_READ ) { +struct seq_file *m = file->private_data; + +kfree (m->private);seq_release (inode, file); + }else { +kfree (file->private_data); + } +return 0 ; }static int -ftrace_set_func (unsigned long *array,int *idx,char *buffer) +ftrace_set_func (unsigned long *array,int *idx,int size,char *buffer) {struct dyn_ftrace *rec;struct ftrace_page *pg;
@@ -3877,7 +3919,7 @@ ftrace_set_func(unsigned long *array, int *idx, char *buffer)/* decode regex */ type =filter_parse_regex (buffer,strlen (buffer), &search, ¬); -if (!not && *idx >=FTRACE_GRAPH_MAX_FUNCS ) +if (!not && *idx >= size)return -EBUSY ; search_len =strlen (search);
@@ -3905,7 +3947,7 @@ ftrace_set_func(unsigned long *array, int *idx, char *buffer) fail =0 ;if (!exists) { array[(*idx)++] = rec->ip; -if (*idx >=FTRACE_GRAPH_MAX_FUNCS ) +if (*idx >= size) goto out; } }else {
@@ -3932,6 +3974,7 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, {struct trace_parser parser;ssize_t read, ret; +struct ftrace_graph_data *fgd = file->private_data;if (!cnt)return 0 ;
@@ -3949,8 +3992,8 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, parser.buffer[parser.idx] =0 ;/* we allow only one expression at a time */ - ret =ftrace_set_func (ftrace_graph_funcs, &ftrace_graph_count, - parser.buffer); + ret =ftrace_set_func (fgd->table, fgd->count, fgd->size, + parser.buffer);if (ret) goto out_free; }
Signature · 1 line
1.7.11.7
PATCH 3/4 ftrace: Narrow down the protected area of graph_lock
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
It seems that it doesn't need to hold graph_lock during preparation of the trace_parser. At least it makes the code a bit simpler IMHO.
--- kernel/trace/ftrace.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
kernel/trace/ftrace.c +9 -13
@@ -3973,37 +3973,33 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos) { struct trace_parser parser; -ssize_t read, ret; +ssize_t read, ret =0 ;struct ftrace_graph_data *fgd = file->private_data;if (!cnt)return 0 ; -mutex_lock (&graph_lock); - -if (trace_parser_get_init (&parser,FTRACE_BUFF_MAX )) { - ret = -ENOMEM ; - goto out_unlock; - } +if (trace_parser_get_init (&parser,FTRACE_BUFF_MAX )) +return -ENOMEM ; read =trace_get_user (&parser, ubuf, cnt, ppos);if (read >=0 &&trace_parser_loaded ((&parser))) { parser.buffer[parser.idx] =0 ; +mutex_lock (&graph_lock); +/* we allow only one expression at a time */ ret =ftrace_set_func (fgd->table, fgd->count, fgd->size, parser.buffer); -if (ret) - goto out_free; + +mutex_unlock (&graph_lock); } - ret = read; +if (!ret) + ret = read; -out_free:trace_parser_put (&parser); -out_unlock: -mutex_unlock (&graph_lock);return ret; }
Signature · 1 line
1.7.11.7
PATCH 4/4 ftrace: Add set_graph_notrace filter
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
The set_graph_notrace filter is analogous to set_ftrace_notrace and can be used for eliminating uninteresting part of function graph trace output. It also works with set_graph_function nicely.
# cd /sys/kernel/debug/tracing/
# echo do_page_fault > set_graph_function
# perf ftrace live true
2) | do_page_fault() {
2) | __do_page_fault() {
2) 0.381 us | down_read_trylock();
2) 0.055 us | __might_sleep();
2) 0.696 us | find_vma();
2) | handle_mm_fault() {
2) | handle_pte_fault() {
2) | __do_fault() {
2) | filemap_fault() {
2) | find_get_page() {
2) 0.033 us | __rcu_read_lock();
2) 0.035 us | __rcu_read_unlock();
2) 1.696 us | }
2) 0.031 us | __might_sleep();
2) 2.831 us | }
2) | _raw_spin_lock() {
2) 0.046 us | add_preempt_count();
2) 0.841 us | }
2) 0.033 us | page_add_file_rmap();
2) | _raw_spin_unlock() {
2) 0.057 us | sub_preempt_count();
2) 0.568 us | }
2) | unlock_page() {
2) 0.084 us | page_waitqueue();
2) 0.126 us | __wake_up_bit();
2) 1.117 us | }
2) 7.729 us | }
2) 8.397 us | }
2) 8.956 us | }
2) 0.085 us | up_read();
2) + 12.745 us | }
2) + 13.401 us | }
... # echo handle_mm_fault > set_graph_notrace
# perf ftrace live true
1) | do_page_fault() {
1) | __do_page_fault() {
1) 0.205 us | down_read_trylock();
1) 0.041 us | __might_sleep();
1) 0.344 us | find_vma();
1) 0.069 us | up_read();
1) 4.692 us | }
1) 5.311 us | }
...--- include/linux/ftrace.h | 1 + kernel/trace/ftrace.c | 33 +++++++++++++++++++++ kernel/trace/trace.h | 22 ++++++++++++++ kernel/trace/trace_functions_graph.c | 56 ++++++++++++++++++++++++++++++++++-- 4 files changed, 109 insertions(+), 3 deletions(-)
include/linux/ftrace.h +1 -0
@@ -721,6 +721,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,extern char __irqentry_text_start[];extern char __irqentry_text_end[]; +#define FTRACE_NOTRACE_DEPTH 65536#define FTRACE_RETFUNC_DEPTH 50#define FTRACE_RETSTACK_ALLOC_SIZE 32 extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
kernel/trace/ftrace.c +33 -0
@@ -3776,7 +3776,9 @@ static const struct file_operations ftrace_notrace_fops = {static DEFINE_MUTEX (graph_lock);int ftrace_graph_count; +int ftrace_graph_notrace_count;unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS ]__read_mostly ; +unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS ]__read_mostly ;struct ftrace_graph_data {unsigned long *table;
@@ -3891,6 +3893,26 @@ ftrace_graph_open(struct inode *inode, struct file *file) }static int +ftrace_graph_notrace_open (struct inode *inode,struct file *file) +{ +struct ftrace_graph_data *fgd; + +if (unlikely (ftrace_disabled)) +return -ENODEV ; + + fgd =kmalloc (sizeof (*fgd),GFP_KERNEL ); +if (fgd ==NULL ) +return -ENOMEM ; + + fgd->table = ftrace_graph_notrace_funcs; + fgd->size =FTRACE_GRAPH_MAX_FUNCS ; + fgd->count = &ftrace_graph_notrace_count; + fgd->seq_ops = &ftrace_graph_seq_ops; + +return __ftrace_graph_open (inode, file, fgd); +} + +static int ftrace_graph_release (struct inode *inode,struct file *file) {if (file->f_mode &FMODE_READ ) {
@@ -4011,6 +4033,14 @@ static const struct file_operations ftrace_graph_fops = { .llseek = ftrace_filter_lseek, .release = ftrace_graph_release, }; + +static const struct file_operations ftrace_graph_notrace_fops = { + .open = ftrace_graph_notrace_open, + .read = seq_read, + .write = ftrace_graph_write, + .llseek = ftrace_filter_lseek, + .release = ftrace_graph_release, +};#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ static __init int ftrace_init_dyn_debugfs (struct dentry *d_tracer)
@@ -4032,6 +4062,9 @@ static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)trace_create_file ("set_graph_function" ,0444 , d_tracer,NULL , &ftrace_graph_fops); +trace_create_file ("set_graph_notrace" ,0444 , d_tracer, +NULL , + &ftrace_graph_notrace_fops);#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ return 0 ;
kernel/trace/trace.h +22 -0
@@ -732,6 +732,8 @@ extern void __trace_graph_return(struct trace_array *tr,#define FTRACE_GRAPH_MAX_FUNCS 32extern int ftrace_graph_count;extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS ]; +extern int ftrace_graph_notrace_count; +extern unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS ];static inline int ftrace_graph_addr (unsigned long addr) {
@@ -757,11 +759,31 @@ static inline int ftrace_graph_addr(unsigned long addr)return 0 ; } + +static inline int ftrace_graph_notrace_addr (unsigned long addr) +{ +int i; + +if (!ftrace_graph_notrace_count) +return 0 ; + +for (i =0 ; i < ftrace_graph_notrace_count; i++) { +if (addr == ftrace_graph_notrace_funcs[i]) +return 1 ; + } + +return 0 ; +}#else static inline int ftrace_graph_addr (unsigned long addr) {return 1 ; } + +static inline int ftrace_graph_notrace_addr (unsigned long addr) +{ +return 0 ; +}#endif /* CONFIG_DYNAMIC_FTRACE */ #else /* CONFIG_FUNCTION_GRAPH_TRACER */ static inline enum print_line_t
kernel/trace/trace_functions_graph.c +53 -3
92 lines not on this page · raw message
Signature · 1 line
1.7.11.7