Add "func_no_repete" tracing option
v2 cover Add "func_no_repete" tracing option
The new option for function tracing aims to save space on the ring buffer and to make it more readable in the case when a single function is called number of times consecutively:
while (cond) do_func();
Instead of having an identical records for each call of the function we will record only the first call, followed by an event showing the number of repeats.
v2 changes:
* As suggested by Steven in his review, we now record not only the repetition count, but also the time elapsed between the last repetition of the function and the actual generation of the "func_repeats" event. 16 bits are used to record the repetition count. In the case of an overflow of the counter a second pair of "function" and "func_repeats" events will be generated. The time interval gets codded by using up to 48 (32 + 16) bits.
Yordan Karadzhov (VMware) (5):
tracing: Define new ftrace event "func_repeats"
tracing: Add "last_func_repeats" to struct trace_array
tracing: Add method for recording "func_repeats" events
tracing: Unify the logic for function tracing options
tracing: Add "func_no_repeats" option for function tracing
kernel/trace/trace.c | 29 +++++ kernel/trace/trace.h | 25 ++++ kernel/trace/trace_entries.h | 28 +++++ kernel/trace/trace_functions.c | 222 +++++++++++++++++++++++++++++---- kernel/trace/trace_output.c | 47 +++++++ 5 files changed, 324 insertions(+), 27 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/5 tracing: Define new ftrace event "func_repeats"
The event aims to consolidate the function tracing record in the cases when a single function is called number of times consecutively.
while (cond) do_func();
This may happen in various scenarios (busy waiting for example). The new ftrace event can be used to show repeated function events with a single event and save space on the ring buffer
--- kernel/trace/trace.h | 3 +++ kernel/trace/trace_entries.h | 39 ++++++++++++++++++++++++++++++ kernel/trace/trace_output.c | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+)
kernel/trace/trace.h +3 -0
@@ -45,6 +45,7 @@ enum trace_type {TRACE_BPUTS ,TRACE_HWLAT ,TRACE_RAW_DATA , +TRACE_FUNC_REPEATS , __TRACE_LAST_TYPE, };
@@ -442,6 +443,8 @@ extern void __ftrace_bad_type(void); TRACE_GRAPH_ENT); \ IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \TRACE_GRAPH_RET ); \ + IF_ASSIGN(var, ent, struct func_repeats_entry, \ +TRACE_FUNC_REPEATS ); \__ftrace_bad_type (); \ }while (0 )
kernel/trace/trace_entries.h +39 -0
@@ -338,3 +338,42 @@ FTRACE_ENTRY(hwlat, hwlat_entry, __entry->nmi_total_ts, __entry->nmi_count) ); + +#define FUNC_REPEATS_GET_DELTA_TS (entry) \ +(((u64)entry->top_delta_ts << 32) | entry->bottom_delta_ts) \ + +#define FUNC_REPEATS_SET_DELTA_TS (entry, delta) \ + do { \ + if (likely(!((u64)delta >> 32))) { \ + entry->bottom_delta_ts = delta; \ + entry->top_delta_ts = 0; \ + } else { \ + if (likely(!((u64)delta >> 48))) { \ + entry->bottom_delta_ts = delta & U32_MAX; \ + entry->top_delta_ts = (delta >> 32); \ + } else { \ +/* Timestamp overflow. Set to max. */ \ + entry->bottom_delta_ts =U32_MAX ; \ + entry->top_delta_ts =U16_MAX ; \ + } \ + } \ + } while (0); \ + +FTRACE_ENTRY (func_repeats, func_repeats_entry, + +TRACE_FUNC_REPEATS , + +F_STRUCT ( + __field( unsigned long, ip ) + __field( unsigned long, parent_ip ) + __field( u16 , count ) + __field( u16 , top_delta_ts ) +__field ( u32 , bottom_delta_ts ) + ), + +F_printk (" %ps <-%ps\t(repeats:%u delta_ts: -%llu)" , + (void *)__entry->ip, + (void *)__entry->parent_ip, + __entry->count, +FUNC_REPEATS_GET_DELTA_TS (__entry)) +);
kernel/trace/trace_output.c +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.25.1
Steven Rostedt
maintainerRe: [PATCH v2 1/5] tracing: Define new ftrace event "func_repeats"
Quoted 43 lines · On Mon, 29 Mar 2021 16:05:29 +0300 "Yordan Karadzhov (VMware)"…
The event aims to consolidate the function tracing record in the cases when a single function is called number of times consecutively.
while (cond) do_func();This may happen in various scenarios (busy waiting for example). The new ftrace event can be used to show repeated function events with a single event and save space on the ring buffer
Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- kernel/trace/trace.h | 3 +++ kernel/trace/trace_entries.h | 39 ++++++++++++++++++++++++++++++ kernel/trace/trace_output.c | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+)kernel/trace/trace.h +3
-0@@ -45,6 +45,7 @@ enum trace_type {TRACE_BPUTS ,TRACE_HWLAT ,TRACE_RAW_DATA , +TRACE_FUNC_REPEATS , __TRACE_LAST_TYPE, };@@ -442,6 +443,8 @@ extern void __ftrace_bad_type(void); TRACE_GRAPH_ENT); \ IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \TRACE_GRAPH_RET ); \ + IF_ASSIGN(var, ent, struct func_repeats_entry, \ +TRACE_FUNC_REPEATS ); \__ftrace_bad_type (); \ }while (0 )kernel/trace/trace_entries.h +0
-0
kernel/trace/trace_entries.h FTRACE_ENTRY(hwlat, hwlat_entry, · line 345
Quoted 8 lines
kernel/trace/trace_entries.h +5
-0@@ -338,3 +338,42 @@ FTRACE_ENTRY(hwlat, hwlat_entry, __entry->nmi_total_ts, __entry->nmi_count) ); + +#define FUNC_REPEATS_GET_DELTA_TS (entry) \ +(((u64)entry->top_delta_ts << 32) | entry->bottom_delta_ts) \ + +#define FUNC_REPEATS_SET_DELTA_TS (entry, delta) \
Hmm, this isn't used anywhere. Why is it defined here?
kernel/trace/trace_entries.h FTRACE_ENTRY(hwlat, hwlat_entry, · line 347
Quoted 2 lines
kernel/trace/trace_entries.h +2
-0+ do { \ + if (likely(!((u64)delta >> 32))) { \
If statements are more expensive than shifts and masks, thus, this first if statement isn't needed. Because:
entry->bottom_delta_ts = delta & U32_MAX; entry->top_delta_ts = (delta >> 32);
Would produce the same faster as this conditional, but be generally faster.
kernel/trace/trace_entries.h FTRACE_ENTRY(hwlat, hwlat_entry, · line 357
Quoted 10 lines
kernel/trace/trace_entries.h +10
-0+ entry->bottom_delta_ts = delta; \ + entry->top_delta_ts =0 ; \ + }else { \ +if (likely (!((u64 )delta >>48 ))) { \ + entry->bottom_delta_ts = delta &U32_MAX ; \ + entry->top_delta_ts = (delta >>32 ); \ + }else { \ +/* Timestamp overflow. Set to max. */ \ + entry->bottom_delta_ts =U32_MAX ; \ + entry->top_delta_ts =U16_MAX ; \
I'm almost thinking we should just ignore this as it should never happen, because 2^48 nanoseconds is 78 hours. If we are repeating the same function over and over again for over 78 hours, we have more issues to worry about ;-)
-- Steve
Quoted 87 lines
kernel/trace/trace_entries.h +22
-0+ } \ + } \ + }while (0 ); \ + +FTRACE_ENTRY (func_repeats, func_repeats_entry, + +TRACE_FUNC_REPEATS , + +F_STRUCT ( + __field( unsigned long, ip ) + __field( unsigned long, parent_ip ) + __field( u16 , count ) + __field( u16 , top_delta_ts ) +__field ( u32 , bottom_delta_ts ) + ), + +F_printk (" %ps <-%ps\t(repeats:%u delta_ts: -%llu)" , + (void *)__entry->ip, + (void *)__entry->parent_ip, + __entry->count, +FUNC_REPEATS_GET_DELTA_TS (__entry)) +);kernel/trace/trace_output.c +47
-0@@ -1373,6 +1373,52 @@ static struct trace_event trace_raw_data_event = { .funcs = &trace_raw_data_funcs, }; +static enum print_line_t +trace_func_repeats_raw (struct trace_iterator *iter,int flags, +struct trace_event *event) +{ +struct func_repeats_entry *field; +struct trace_seq *s = &iter->seq; + +trace_assign_type (field, iter->ent); + +trace_seq_printf (s,"%lu %lu %u %llu\n" , + field->ip, + field->parent_ip, + field->count, +FUNC_REPEATS_GET_DELTA_TS (field)); + +return trace_handle_return (s); +} + +static enum print_line_t +trace_func_repeats_print (struct trace_iterator *iter,int flags, +struct trace_event *event) +{ +struct func_repeats_entry *field; +struct trace_seq *s = &iter->seq; + +trace_assign_type (field, iter->ent); + +seq_print_ip_sym (s, field->ip, flags); +trace_seq_puts (s," <-" ); +seq_print_ip_sym (s, field->parent_ip, flags); +trace_seq_printf (s," (repeats: %u, delta_ts: -%llu)\n" , + field->count, +FUNC_REPEATS_GET_DELTA_TS (field)); + +return trace_handle_return (s); +} + +static struct trace_event_functions trace_func_repeats_funcs = { + .trace = trace_func_repeats_print, + .raw = trace_func_repeats_raw, +}; + +static struct trace_event trace_func_repeats_event = { + .type =TRACE_FUNC_REPEATS , + .funcs = &trace_func_repeats_funcs, +};static struct trace_event *events[]__initdata = { &trace_fn_event,@@ -1385,6 +1431,7 @@ static struct trace_event *events[] __initdata = { &trace_print_event, &trace_hwlat_event, &trace_raw_data_event, + &trace_func_repeats_event, NULL };
PATCH v2 2/5 tracing: Add "last_func_repeats" to struct trace_array
In reply to Yordan Karadzhov (VMware)The field is used to keep track of the consecutive (on the same CPU) calls of a single function. This information is needed in order to consolidate the function tracing record in the cases when a single function is called number of times.
--- kernel/trace/trace.c | 1 + kernel/trace/trace.h | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+)
kernel/trace/trace.c +1 -0
@@ -9103,6 +9103,7 @@ static int __remove_instance(struct trace_array *tr)ftrace_clear_pids (tr);ftrace_destroy_function_files (tr);tracefs_remove (tr->dir); +free_percpu (tr->last_func_repeats);free_trace_buffers (tr);for (i =0 ; i < tr->nr_topts; i++) {
kernel/trace/trace.h +18 -0
@@ -262,6 +262,17 @@ struct cond_snapshot {cond_update_fn_t update; }; +/* +* struct trace_func_repeats - used to keep track of the consecutive +* (on the same CPU) calls of a single function. +*/ +struct trace_func_repeats { +unsigned long ip; +unsigned long parent_ip; +unsigned long count; +u64 ts_last_call; +}; +/* * The trace array - an array of per-CPU trace arrays. This is the * highest level data structure that individual tracers deal with.
@@ -358,8 +369,15 @@ struct trace_array {#ifdef CONFIG_TRACER_SNAPSHOT struct cond_snapshot *cond_snapshot;#endif +struct trace_func_repeats __percpu *last_func_repeats; }; +static inline struct trace_func_repeats __percpu * +tracer_alloc_func_repeats (struct trace_array *tr) +{ + return tr->last_func_repeats = alloc_percpu(struct trace_func_repeats); +} +enum {TRACE_ARRAY_FL_GLOBAL = (1 <<0 ) };
Signature · 1 line
2.25.1
PATCH v2 3/5 tracing: Add method for recording "func_repeats" events
In reply to Yordan Karadzhov (VMware)This patch only provides the implementation of the method. Later we will used it in a combination with a new option for function tracing.
--- kernel/trace/trace.c | 26 ++++++++++++++++++++++++++ kernel/trace/trace.h | 4 ++++ 2 files changed, 30 insertions(+)
kernel/trace/trace.c +26 -0
@@ -3116,6 +3116,32 @@ static void ftrace_trace_userstack(struct trace_array *tr,#endif /* CONFIG_STACKTRACE */ +void trace_last_func_repeats (struct trace_array *tr, +struct trace_func_repeats *last_info, +unsigned int trace_ctx) +{ +struct trace_buffer *buffer = tr->array_buffer.buffer; +struct func_repeats_entry *entry; +struct ring_buffer_event *event; +u64 delta; + + event =__trace_buffer_lock_reserve (buffer,TRACE_FUNC_REPEATS , +sizeof (*entry), trace_ctx); +if (!event) +return ; + + delta =ring_buffer_event_time_stamp (buffer, event) - + last_info->ts_last_call; + + entry =ring_buffer_event_data (event); + entry->ip = last_info->ip; + entry->parent_ip = last_info->parent_ip; + entry->count = last_info->count; + FUNC_REPEATS_SET_DELTA_TS(entry, delta) + +__buffer_unlock_commit (buffer, event); +} +/* created for use with alloc_percpu */ struct trace_buffer_struct {int nesting;
kernel/trace/trace.h +4 -0
@@ -701,6 +701,10 @@ static inline void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, }#endif /* CONFIG_STACKTRACE */ +void trace_last_func_repeats (struct trace_array *tr, +struct trace_func_repeats *last_info, +unsigned int trace_ctx); +extern u64 ftrace_now (int cpu);extern void trace_find_cmdline (int pid,char comm[]);
Signature · 1 line
2.25.1
PATCH v2 4/5 tracing: Unify the logic for function tracing options
In reply to Yordan Karadzhov (VMware)Currently the logic for dealing with the options for function tracing has two different implementations. One is used when we set the flags (in "static int func_set_flag()") and another used when we initialize the tracer (in "static int function_trace_init()"). Those two implementations are meant to do essentially the same thing and they are both not very convenient for adding new options. In this patch we add a helper function that provides a single implementation of the logic for dealing with the options and we make it such that new options can be easily added.
--- kernel/trace/trace_functions.c | 66 ++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 26 deletions(-)
kernel/trace/trace_functions.c +40 -26
@@ -27,13 +27,17 @@ function_trace_call(unsigned long ip, unsigned long parent_ip,static void function_stack_trace_call (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs); +static ftrace_func_t select_trace_function (u32 flags_val);static struct tracer_flags func_flags;/* Our option */ enum { +TRACE_FUNC_NO_OPTS =0x0 ,/* No flags set. */ TRACE_FUNC_OPT_STACK =0x1 , }; +#define TRACE_FUNC_OPT_MASK (TRACE_FUNC_OPT_STACK) +int ftrace_allocate_ftrace_ops (struct trace_array *tr) {struct ftrace_ops *ops;
@@ -97,12 +101,9 @@ static int function_trace_init(struct trace_array *tr)if (!tr->ops)return -ENOMEM ; -/* Currently only the global instance can do stack tracing */ -if (tr->flags &TRACE_ARRAY_FL_GLOBAL && - func_flags.val &TRACE_FUNC_OPT_STACK ) - func = function_stack_trace_call; -else - func = function_trace_call; + func =select_trace_function (func_flags.val); +if (!func) +return -EINVAL ;ftrace_init_array_ops (tr, func);
@@ -205,6 +206,18 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,local_irq_restore (flags); } +static ftrace_func_t select_trace_function (u32 flags_val) +{ +switch (flags_val &TRACE_FUNC_OPT_MASK ) { +case TRACE_FUNC_NO_OPTS : +return function_trace_call; +case TRACE_FUNC_OPT_STACK : +return function_stack_trace_call; +default : +return NULL ; + } +} + static struct tracer_opt func_opts[] = {#ifdef CONFIG_STACKTRACE { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
@@ -213,7 +226,7 @@ static struct tracer_opt func_opts[] = { };static struct tracer_flags func_flags = { - .val =0 ,/* By default: all flags disabled */ + .val =TRACE_FUNC_NO_OPTS ,/* By default: all flags disabled */ .opts = func_opts };
@@ -235,30 +248,31 @@ static struct tracer function_trace;static int func_set_flag (struct trace_array *tr,u32 old_flags,u32 bit,int set) { -switch (bit) { -case TRACE_FUNC_OPT_STACK : -/* do nothing if already set */ -if (!!set == !!(func_flags.val &TRACE_FUNC_OPT_STACK )) -break ; +ftrace_func_t func; +u32 new_flags_val; -/* We can change this flag when not running. */ -if (tr->current_trace != &function_trace) -break ; +/* Do nothing if already set. */ +if (!!set == !!(func_flags.val & bit)) +return 0 ; -unregister_ftrace_function (tr->ops); +/* We can change this flag only when not running. */ +if (tr->current_trace != &function_trace) +return 0 ; -if (set) { - tr->ops->func = function_stack_trace_call; -register_ftrace_function (tr->ops); - }else { - tr->ops->func = function_trace_call; -register_ftrace_function (tr->ops); - } + new_flags_val = (func_flags.val & ~(1UL << (bit -1 ))); + new_flags_val |= (set << (bit -1 )); -break ; -default : + func =select_trace_function (new_flags_val); +if (!func)return -EINVAL ; - } + +/* Check if there's anything to change. */ +if (tr->ops->func == func) +return 0 ; + +unregister_ftrace_function (tr->ops); + tr->ops->func = func; +register_ftrace_function (tr->ops);return 0 ; }
Signature · 1 line
2.25.1
Steven Rostedt
maintainerRe: [PATCH v2 4/5] tracing: Unify the logic for function tracing options
Quoted 58 lines · On Mon, 29 Mar 2021 16:05:32 +0300 "Yordan Karadzhov (VMware)"…
Currently the logic for dealing with the options for function tracing has two different implementations. One is used when we set the flags (in "static int
func_set_flag()") and another used when we initialize the tracer (in "static intfunction_trace_init()"). Those two implementations are meant to do essentially the same thing and they are both not very convenient for adding new options. In this patch we add a helper function that provides a single implementation of the logic for dealing with the options and we make it such that new options can be easily added.Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- kernel/trace/trace_functions.c | 66 ++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 26 deletions(-)kernel/trace/trace_functions.c +8
-6@@ -27,13 +27,17 @@ function_trace_call(unsigned long ip, unsigned long parent_ip,static void function_stack_trace_call (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs); +static ftrace_func_t select_trace_function (u32 flags_val);static struct tracer_flags func_flags;/* Our option */ enum { +TRACE_FUNC_NO_OPTS =0x0 ,/* No flags set. */ TRACE_FUNC_OPT_STACK =0x1 , }; +#define TRACE_FUNC_OPT_MASK (TRACE_FUNC_OPT_STACK) +int ftrace_allocate_ftrace_ops (struct trace_array *tr) {struct ftrace_ops *ops;@@ -97,12 +101,9 @@ static int function_trace_init(struct trace_array *tr)if (!tr->ops)return -ENOMEM ; -/* Currently only the global instance can do stack tracing */ -if (tr->flags &TRACE_ARRAY_FL_GLOBAL && - func_flags.val &TRACE_FUNC_OPT_STACK ) - func = function_stack_trace_call; -else - func = function_trace_call; + func =select_trace_function (func_flags.val); +if (!func) +return -EINVAL ;ftrace_init_array_ops (tr, func);@@ -205,6 +206,18 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,local_irq_restore (flags); } +static ftrace_func_t select_trace_function (u32 flags_val)
kernel/trace/trace_functions.c function_stack_trace_call(unsigned long ip, unsigned long parent_ip, · line 219
Quoted 10 lines
kernel/trace/trace_functions.c +10
-0+{ +switch (flags_val &TRACE_FUNC_OPT_MASK ) { +case TRACE_FUNC_NO_OPTS : +return function_trace_call; +case TRACE_FUNC_OPT_STACK : +return function_stack_trace_call; +default : +return NULL ; + } +}
Is there a reason why you defined this function here and not before its first use? When defining functions, I tend to try to define them before their first use to not need to declare the static prototype above.
The reasons for doing the static prototype and using a static function is usually because of #ifdef around the first use, and keeping the function from being hidden by the #ifdef, or the static function already exists, and then gets used in a function before it, where it's just easier to add the static declaration than moving the function.
Quoted 14 lines
kernel/trace/trace_functions.c +2
-1+ static struct tracer_opt func_opts[] = {#ifdef CONFIG_STACKTRACE { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },@@ -213,7 +226,7 @@ static struct tracer_opt func_opts[] = { };static struct tracer_flags func_flags = { - .val =0 ,/* By default: all flags disabled */ + .val =TRACE_FUNC_NO_OPTS ,/* By default: all flags disabled */ .opts = func_opts };
kernel/trace/trace_functions.c static struct tracer function_trace; · line 252
Quoted 10 lines
kernel/trace/trace_functions.c +2
-5@@ -235,30 +248,31 @@ static struct tracer function_trace;static int func_set_flag (struct trace_array *tr,u32 old_flags,u32 bit,int set) { -switch (bit) { -case TRACE_FUNC_OPT_STACK : -/* do nothing if already set */ -if (!!set == !!(func_flags.val &TRACE_FUNC_OPT_STACK )) -break ; +ftrace_func_t func; +u32 new_flags_val;
Nit, but the variable should just be "new_flags", which is consistent with old_flags. In the kernel we don't need to the variable names to be so verbose.
Quoted 12 lines
kernel/trace/trace_functions.c +6
-4-/* We can change this flag when not running. */ -if (tr->current_trace != &function_trace) -break ; +/* Do nothing if already set. */ +if (!!set == !!(func_flags.val & bit)) +return 0 ; -unregister_ftrace_function (tr->ops); +/* We can change this flag only when not running. */ +if (tr->current_trace != &function_trace) +return 0 ;
kernel/trace/trace_functions.c static struct tracer function_trace; · line 263
Quoted 10 lines
kernel/trace/trace_functions.c +2
-7-if (set) { - tr->ops->func = function_stack_trace_call; -register_ftrace_function (tr->ops); - }else { - tr->ops->func = function_trace_call; -register_ftrace_function (tr->ops); - } + new_flags_val = (func_flags.val & ~(1UL << (bit -1 ))); + new_flags_val |= (set << (bit -1 ));
bit is already the mask, no need to shift it, nor there's no reason for the extra set of parenthesis. And the above can be done in one line.
new_flags = (func_flags.val & ~bit) | (set ? bit : 0);
-- Steve
Quoted 18 lines
kernel/trace/trace_functions.c +10
-3-break ; -default : + func =select_trace_function (new_flags_val); +if (!func)return -EINVAL ; - } + +/* Check if there's anything to change. */ +if (tr->ops->func == func) +return 0 ; + +unregister_ftrace_function (tr->ops); + tr->ops->func = func; +register_ftrace_function (tr->ops);return 0 ; }
Re: [PATCH v2 4/5] tracing: Unify the logic for function tracing options
Hi Steven,
Hi Steven,
Quoted 41 lines · Steven Rostedt
kernel/trace/trace_functions.c static struct tracer function_trace; · line 252
Quoted 12 lines
kernel/trace/trace_functions.c +2
-5@@ -235,30 +248,31 @@ static struct tracer function_trace;static int func_set_flag (struct trace_array *tr,u32 old_flags,u32 bit,int set) { -switch (bit) { -case TRACE_FUNC_OPT_STACK : -/* do nothing if already set */ -if (!!set == !!(func_flags.val &TRACE_FUNC_OPT_STACK )) -break ; +ftrace_func_t func; +u32 new_flags_val;Nit, but the variable should just be "
new_flags", which is consistent withold_flags. In the kernel we don't need to the variable names to be so verbose.Quoted 12 lines
kernel/trace/trace_functions.c +6
-4-/* We can change this flag when not running. */ -if (tr->current_trace != &function_trace) -break ; +/* Do nothing if already set. */ +if (!!set == !!(func_flags.val & bit)) +return 0 ; -unregister_ftrace_function (tr->ops); +/* We can change this flag only when not running. */ +if (tr->current_trace != &function_trace) +return 0 ;kernel/trace/trace_functions.c static struct tracer function_trace; · line 263
Quoted 10 lines
kernel/trace/trace_functions.c +2
-7-if (set) { - tr->ops->func = function_stack_trace_call; -register_ftrace_function (tr->ops); - }else { - tr->ops->func = function_trace_call; -register_ftrace_function (tr->ops); - } + new_flags_val = (func_flags.val & ~(1UL << (bit -1 ))); + new_flags_val |= (set << (bit -1 ));bit is already the mask, no need to shift it, nor there's no reason for the extra set of parenthesis. And the above can be done in one line.
Quoted 2 lines
new_flags = (func_flags.val & ~bit) | (set ? bit : 0);
OK, I totally misinterpreted the meaning of the "bit" argument of the function. I did not realized it is a mask. I was thinking the argument gives only the number of the bit that changes (like 5 for the 5-th bit inside the mask).
Thanks!
Yordan
PATCH v2 5/5 tracing: Add "func_no_repeats" option for function tracing
In reply to Yordan Karadzhov (VMware)If the option is activated the function tracing record gets consolidated in the cases when a single function is called number of times consecutively. Instead of having an identical record for each call of the function we will record only the first call following by event showing the number of repeats.
fix last
--- kernel/trace/trace_functions.c | 161 ++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 3 deletions(-)
kernel/trace/trace_functions.c +158 -3
@@ -27,16 +27,28 @@ function_trace_call(unsigned long ip, unsigned long parent_ip,static void function_stack_trace_call (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs); +static void +function_no_repeats_trace_call (unsigned long ip,unsigned long parent_ip, +struct ftrace_ops *op,struct ftrace_regs *fregs); +static void +function_stack_no_repeats_trace_call (unsigned long ip,unsigned long parent_ip, +struct ftrace_ops *op, +struct ftrace_regs *fregs);static ftrace_func_t select_trace_function (u32 flags_val);static struct tracer_flags func_flags;/* Our option */ enum { -TRACE_FUNC_NO_OPTS =0x0 ,/* No flags set. */ -TRACE_FUNC_OPT_STACK =0x1 , + +TRACE_FUNC_NO_OPTS =0x0 ,/* No flags set. */ +TRACE_FUNC_OPT_STACK =0x1 , +TRACE_FUNC_OPT_NO_REPEATS =0x2 , + +/* Update this to next highest bit. */ +TRACE_FUNC_OPT_HIGHEST_BIT =0x4 }; -#define TRACE_FUNC_OPT_MASK (TRACE_FUNC_OPT_STACK) +#define TRACE_FUNC_OPT_MASK (TRACE_FUNC_OPT_HIGHEST_BIT - 1)int ftrace_allocate_ftrace_ops (struct trace_array *tr) {
@@ -90,6 +102,17 @@ void ftrace_destroy_function_files(struct trace_array *tr)ftrace_free_ftrace_ops (tr); } +static bool handle_func_repeats (struct trace_array *tr,u32 flags_val) +{ +if (!tr->last_func_repeats && + (flags_val &TRACE_FUNC_OPT_NO_REPEATS )) { +if (!tracer_alloc_func_repeats (tr)) +return false; + } + +return true; +} +static int function_trace_init (struct trace_array *tr) {ftrace_func_t func;
@@ -105,6 +128,9 @@ static int function_trace_init(struct trace_array *tr)if (!func)return -EINVAL ; +if (!handle_func_repeats (tr, func_flags.val)) +return -ENOMEM ; +ftrace_init_array_ops (tr, func); tr->array_buffer.cpu =raw_smp_processor_id ();
@@ -206,6 +232,127 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,local_irq_restore (flags); } +static inline bool is_repeat_check (struct trace_array *tr, +struct trace_func_repeats *last_info, +unsigned long ip,unsigned long parent_ip) +{ +if (last_info->ip == ip && + last_info->parent_ip == parent_ip && + last_info->count <U16_MAX ) { + last_info->ts_last_call = +ring_buffer_time_stamp (tr->array_buffer.buffer); + last_info->count++; +return true; + } + +return false; +} + +static inline void process_repeats (struct trace_array *tr, +unsigned long ip,unsigned long parent_ip, +struct trace_func_repeats *last_info, +unsigned int trace_ctx) +{ +if (last_info->count) { +trace_last_func_repeats (tr, last_info, trace_ctx); + last_info->count =0 ; + } + + last_info->ip = ip; + last_info->parent_ip = parent_ip; +} + +static void +function_no_repeats_trace_call (unsigned long ip,unsigned long parent_ip, +struct ftrace_ops *op, +struct ftrace_regs *fregs) +{ +struct trace_func_repeats *last_info; +struct trace_array *tr = op->private; +struct trace_array_cpu *data; +unsigned int trace_ctx; +unsigned long flags; +int bit; +int cpu; + +if (unlikely (!tr->function_enabled)) +return ; + + bit =ftrace_test_recursion_trylock (ip, parent_ip); +if (bit <0 ) +return ; + +preempt_disable_notrace (); + + cpu =smp_processor_id (); + data =per_cpu_ptr (tr->array_buffer.data, cpu); +if (atomic_read (&data->disabled)) + goto out; + +/* +* An interrupt may happen at any place here. But as far as I can see, +* the only damage that this can cause is to mess up the repetition +* counter without valuable data being lost. +* TODO: think about a solution that is better than just hoping to be +* lucky. +*/ + last_info =per_cpu_ptr (tr->last_func_repeats, cpu); +if (is_repeat_check (tr, last_info, ip, parent_ip)) + goto out; + +local_save_flags (flags); + trace_ctx =tracing_gen_ctx_flags (flags); +process_repeats (tr, ip, parent_ip, last_info, trace_ctx); + +trace_function (tr, ip, parent_ip, trace_ctx); + +out: +ftrace_test_recursion_unlock (bit); +preempt_enable_notrace (); +} + +static void +function_stack_no_repeats_trace_call (unsigned long ip,unsigned long parent_ip, +struct ftrace_ops *op, +struct ftrace_regs *fregs) +{ +struct trace_func_repeats *last_info; +struct trace_array *tr = op->private; +struct trace_array_cpu *data; +unsigned long flags; +long disabled; +int cpu; +unsigned int trace_ctx; + +if (unlikely (!tr->function_enabled)) +return ; + +/* +* Need to use raw, since this must be called before the +* recursive protection is performed. +*/ +local_irq_save (flags); + cpu =raw_smp_processor_id (); + data =per_cpu_ptr (tr->array_buffer.data, cpu); + disabled =atomic_inc_return (&data->disabled); + +if (likely (disabled ==1 )) { + last_info =per_cpu_ptr (tr->last_func_repeats, cpu); +if (is_repeat_check (tr, last_info, ip, parent_ip)) + goto out; + + trace_ctx =tracing_gen_ctx_flags (flags); +process_repeats (tr, ip, parent_ip, last_info, trace_ctx); + +trace_function (tr, ip, parent_ip, trace_ctx); +__trace_stack (tr, trace_ctx,STACK_SKIP ); + } + + out: +atomic_dec (&data->disabled); +local_irq_restore (flags); +} +static ftrace_func_t select_trace_function (u32 flags_val) {switch (flags_val &TRACE_FUNC_OPT_MASK ) {
@@ -213,6 +360,10 @@ static ftrace_func_t select_trace_function(u32 flags_val)return function_trace_call;case TRACE_FUNC_OPT_STACK :return function_stack_trace_call; +case TRACE_FUNC_OPT_NO_REPEATS : +return function_no_repeats_trace_call; +case TRACE_FUNC_OPT_STACK |TRACE_FUNC_OPT_NO_REPEATS : +return function_stack_no_repeats_trace_call;default :return NULL ; }
@@ -222,6 +373,7 @@ static struct tracer_opt func_opts[] = { #ifdef CONFIG_STACKTRACE { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) }, #endif + { TRACER_OPT(func-no-repeats, TRACE_FUNC_OPT_NO_REPEATS) }, { } /* Always set a last empty entry */ };
@@ -270,6 +422,9 @@ func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)if (tr->ops->func == func)return 0 ; +if (!handle_func_repeats (tr, new_flags_val)) +return -ENOMEM ; +unregister_ftrace_function (tr->ops); tr->ops->func = func;register_ftrace_function (tr->ops);
Signature · 1 line
2.25.1
kernel test robot
build botRe: [PATCH v2 5/5] tracing: Add "func_no_repeats" option for function tracing
Thank you for the patch! Yet something to improve:
Hi "Yordan,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/perf/core]
[also build test ERROR on linux/master trace/for-next linus/master v5.12-rc5 next-20210329] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Yordan-Karadzhov-VMware/Add-func_no_repete-tracing-option/20210329-211207 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 08ef1af4de5fe7de9c6d69f1e22e51b66e385d9b config: mips-randconfig-r001-20210329 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 482283042f795ecc27838a3b2f76b5494991401c)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/0day-ci/linux/commit/1aad52db914f80d753799262fa90fc0ccf79320d
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yordan-Karadzhov-VMware/Add-func_no_repete-tracing-option/20210329-211207 git checkout 1aad52db914f80d753799262fa90fc0ccf79320d
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=mips
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> kernel/trace/trace_functions.c:243:50: error: too few arguments to function call, expected 2, have 1
ring_buffer_time_stamp(tr->array_buffer.buffer);
~~~~~~~~~~~~~~~~~~~~~~ ^
include/linux/ring_buffer.h:183:5: note: 'ring_buffer_time_stamp' declared here
u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu);
^1 error generated.
vim +243 kernel/trace/trace_functions.c
234
235 static inline bool is_repeat_check(struct trace_array *tr,
236 struct trace_func_repeats *last_info,
237 unsigned long ip, unsigned long parent_ip)
238 {
239 if (last_info->ip == ip &&
240 last_info->parent_ip == parent_ip &&
241 last_info->count < U16_MAX) {
242 last_info->ts_last_call =
> 243 ring_buffer_time_stamp(tr->array_buffer.buffer);
244 last_info->count++;
245 return true;
246 }
247
248 return false;
249 }
250
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Steven Rostedt
maintainerRe: [PATCH v2 5/5] tracing: Add "func_no_repeats" option for function tracing
In reply to Yordan Karadzhov (VMware)Quoted 9 lines · On Mon, 29 Mar 2021 16:05:33 +0300 "Yordan Karadzhov (VMware)"…
If the option is activated the function tracing record gets consolidated in the cases when a single function is called number of times consecutively. Instead of having an identical record for each call of the function we will record only the first call following by event showing the number of repeats.
Signed-off-by Yordan Karadzhov (VMware) <[email protected]>fix last
You seem to have left the above extra text "fix last" in both versions.
But the rest of the patch looks fine.
-- Steve