tracing: Add percpu buffers for trace_printk()
Steven Rostedt
maintainerRFC PATCH 1/1 tracing: Add percpu buffers for trace_printk()
This is a new version of the trace_printk() lockless code.
Previously, I posted an RFC patch set that convert the trace_printk() to use per-cpu buffers. Two sets, one for normal context (with interrupts disabled) and the other for NMI.
But this required the user to set a flag to say they wanted per-cpu buffers. Peter did not care for this and wanted them on by default.
My reluctance was due to the per cpu buffers wasting space for a debug item (adding a trace_printk). Currently, there are no users of trace_printk() so to handle this, I made both the static format and dynamic format trace_printks add to the trace_printk_fmt section (the static uses trace_bprintk). By doing this, at boot up we can determine if a trace_printk has been used, and if so, we allocate the per_cpu buffers for it. A check is also done at module load.
This patch goes a bit further, as Peter has requested. It allocates 4 sets of trace_printk per-cpu buffers. A normal, softirq, hardirq, and NMI context buffers. The trace printk will look to see what context it is in, and use the appropriate buffer. This removes the need for both locks as well as disabling interrupts.
Once the buffers are allocated, they stay around until the machine is rebooted.
Thoughts?
-- Steve
----
include/linux/kernel.h | 13 ++-- kernel/trace/trace.c | 184 +++++++++++++++++++++++++++++++------------ kernel/trace/trace.h | 2 + kernel/trace/trace_printk.c | 4 + 4 files changed, 148 insertions(+), 55 deletions(-) --------------------------- commit a4d1457ed059a668a2d5fa7dfba741ae086df10f Author: Steven Rostedt <[email protected]> Date: Thu Sep 22 14:01:55 2011 -0400
tracing: Add percpu buffers for trace_printk()
Currently, trace_printk() uses a single buffer to write into to calculate the size and format needed to save the trace. To do this safely in an SMP environment, a spin_lock() is taken to only allow one writer at a time to the buffer. But this could also affect what is being traced, and add synchronization that would not be there otherwise.
Ideally, using percpu buffers would be useful, but since trace_printk() is only used in development, having per cpu buffers for something never used is a waste of space. Thus, the use of the trace_bprintk() format section is changed to be used for static fmts as well as dynamic ones. Then at boot up, we can check if the section that holds the trace_printk formats is non-empty, and if it does contain something, then we know a trace_printk() has been added to the kernel. At this time the trace_printk per cpu buffers are allocated. A check is also done at module load time in case a module is added that contains a trace_printk().
Once the buffers are allocated, they are never freed. If you use a trace_printk() then you should know what you are doing.
A buffer is made for each type of context:
normal
softirq
irq
nmi
The context is checked and the appropriate buffer is used. This allows for totally lockless usage of trace_printk(), and they no longer even disable interrupts.
include/linux/kernel.h +7 -6
@@ -464,15 +464,16 @@ do { \ #define trace_printk(fmt, args...) \ do { \ + static const char *trace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))) = \ + __builtin_constant_p(fmt) ? fmt : NULL; \ + \ __trace_printk_check_format(fmt, ##args); \ - if (__builtin_constant_p(fmt)) { \ - static const char *trace_printk_fmt \ - __attribute__((section("__trace_printk_fmt"))) = \ - __builtin_constant_p(fmt) ? fmt : NULL; \ \ + if (__builtin_constant_p(fmt)) \ __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ - } else \ - __trace_printk(_THIS_IP_, fmt, ##args); \ + else \ + __trace_printk(_THIS_IP_, fmt, ##args); \ } while (0) extern __printf(2, 3)
kernel/trace/trace.c +135 -49
267 lines not on this page · raw message
kernel/trace/trace.h +2 -0
@@ -815,6 +815,8 @@ extern struct list_head ftrace_events;extern const char *__start___trace_bprintk_fmt[];extern const char *__stop___trace_bprintk_fmt[]; +void trace_printk_init_buffers (void ); +#undef FTRACE_ENTRY#define FTRACE_ENTRY (call, struct_name, id, tstruct, print) \ extern struct ftrace_event_call \
kernel/trace/trace_printk.c +4 -0
@@ -51,6 +51,10 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)const char **iter;char *fmt; +/* allocate the trace_printk per cpu buffers */ +if (start != end) +trace_printk_init_buffers (); +mutex_lock (&btrace_mutex);for (iter = start; iter < end; iter++) {struct trace_bprintk_fmt *tb_fmt =lookup_format (*iter);