Skip to content

Commit ad909e2

Browse files
Steven Rostedt (Red Hat)rostedt
authored andcommitted
tracing: Add internal tracing_snapshot() functions
The new snapshot feature is quite handy. It's a way for the user to take advantage of the spare buffer that, until then, only the latency tracers used to "snapshot" the buffer when it hit a max latency. Now users can trigger a "snapshot" manually when some condition is hit in a program. But a snapshot currently can not be triggered by a condition inside the kernel. With the addition of tracing_snapshot() and tracing_snapshot_alloc(), snapshots can now be taking when a condition is hit, and the developer wants to snapshot the case without stopping the trace. Note, any snapshot will overwrite the old one, so take care in how this is done. These new functions are to be used like tracing_on(), tracing_off() and trace_printk() are. That is, they should never be called in the mainline Linux kernel. They are solely for the purpose of debugging. The tracing_snapshot() will not allocate a buffer, but it is safe to be called from any context (except NMIs). But if a snapshot buffer isn't allocated when it is called, it will write to the live buffer, complaining about the lack of a snapshot buffer, and then stop tracing (giving you the "permanent snapshot"). tracing_snapshot_alloc() will allocate the snapshot buffer if it was not already allocated and then take the snapshot. This routine *may sleep*, and must be called from context that can sleep. The allocation is done with GFP_KERNEL and not atomic. If you need a snapshot in an atomic context, say in early boot, then it is best to call the tracing_snapshot_alloc() before then, where it will allocate the buffer, and then you can use the tracing_snapshot() anywhere you want and still get snapshots. Cc: Hiraku Toyooka <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Peter Zijlstra <[email protected]> Signed-off-by: Steven Rostedt <[email protected]>
1 parent a695cb5 commit ad909e2

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

include/linux/kernel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ enum ftrace_dump_mode {
483483
void tracing_on(void);
484484
void tracing_off(void);
485485
int tracing_is_on(void);
486+
void tracing_snapshot(void);
487+
void tracing_snapshot_alloc(void);
486488

487489
extern void tracing_start(void);
488490
extern void tracing_stop(void);
@@ -570,6 +572,8 @@ static inline void trace_dump_stack(void) { }
570572
static inline void tracing_on(void) { }
571573
static inline void tracing_off(void) { }
572574
static inline int tracing_is_on(void) { return 0; }
575+
static inline void tracing_snapshot(void) { }
576+
static inline void tracing_snapshot_alloc(void) { }
573577

574578
static inline __printf(1, 2)
575579
int trace_printk(const char *fmt, ...)

kernel/trace/trace.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,90 @@ void tracing_on(void)
339339
}
340340
EXPORT_SYMBOL_GPL(tracing_on);
341341

342+
#ifdef CONFIG_TRACER_SNAPSHOT
343+
/**
344+
* trace_snapshot - take a snapshot of the current buffer.
345+
*
346+
* This causes a swap between the snapshot buffer and the current live
347+
* tracing buffer. You can use this to take snapshots of the live
348+
* trace when some condition is triggered, but continue to trace.
349+
*
350+
* Note, make sure to allocate the snapshot with either
351+
* a tracing_snapshot_alloc(), or by doing it manually
352+
* with: echo 1 > /sys/kernel/debug/tracing/snapshot
353+
*
354+
* If the snapshot buffer is not allocated, it will stop tracing.
355+
* Basically making a permanent snapshot.
356+
*/
357+
void tracing_snapshot(void)
358+
{
359+
struct trace_array *tr = &global_trace;
360+
struct tracer *tracer = tr->current_trace;
361+
unsigned long flags;
362+
363+
if (!tr->allocated_snapshot) {
364+
trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
365+
trace_printk("*** stopping trace here! ***\n");
366+
tracing_off();
367+
return;
368+
}
369+
370+
/* Note, snapshot can not be used when the tracer uses it */
371+
if (tracer->use_max_tr) {
372+
trace_printk("*** LATENCY TRACER ACTIVE ***\n");
373+
trace_printk("*** Can not use snapshot (sorry) ***\n");
374+
return;
375+
}
376+
377+
local_irq_save(flags);
378+
update_max_tr(tr, current, smp_processor_id());
379+
local_irq_restore(flags);
380+
}
381+
382+
static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
383+
struct trace_buffer *size_buf, int cpu_id);
384+
385+
/**
386+
* trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
387+
*
388+
* This is similar to trace_snapshot(), but it will allocate the
389+
* snapshot buffer if it isn't already allocated. Use this only
390+
* where it is safe to sleep, as the allocation may sleep.
391+
*
392+
* This causes a swap between the snapshot buffer and the current live
393+
* tracing buffer. You can use this to take snapshots of the live
394+
* trace when some condition is triggered, but continue to trace.
395+
*/
396+
void tracing_snapshot_alloc(void)
397+
{
398+
struct trace_array *tr = &global_trace;
399+
int ret;
400+
401+
if (!tr->allocated_snapshot) {
402+
403+
/* allocate spare buffer */
404+
ret = resize_buffer_duplicate_size(&tr->max_buffer,
405+
&tr->trace_buffer, RING_BUFFER_ALL_CPUS);
406+
if (WARN_ON(ret < 0))
407+
return;
408+
409+
tr->allocated_snapshot = true;
410+
}
411+
412+
tracing_snapshot();
413+
}
414+
#else
415+
void tracing_snapshot(void)
416+
{
417+
WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
418+
}
419+
void tracing_snapshot_alloc(void)
420+
{
421+
/* Give warning */
422+
tracing_snapshot();
423+
}
424+
#endif /* CONFIG_TRACER_SNAPSHOT */
425+
342426
/**
343427
* tracing_off - turn off tracing buffers
344428
*

0 commit comments

Comments
 (0)