Skip to content

Commit 93e31ff

Browse files
tzanussirostedt
authored andcommitted
tracing: Add 'snapshot' event trigger command
Add 'snapshot' event_command. snapshot event triggers are added by the user via this command in a similar way and using practically the same syntax as the analogous 'snapshot' ftrace function command, but instead of writing to the set_ftrace_filter file, the snapshot event trigger is written to the per-event 'trigger' files: echo 'snapshot' > .../somesys/someevent/trigger The above command will turn on snapshots for someevent i.e. whenever someevent is hit, a snapshot will be done. This also adds a 'count' version that limits the number of times the command will be invoked: echo 'snapshot:N' > .../somesys/someevent/trigger Where N is the number of times the command will be invoked. The above command will snapshot N times for someevent i.e. whenever someevent is hit N times, a snapshot will be done. Also adds a new tracing_alloc_snapshot() function - the existing tracing_snapshot_alloc() function is a special version of tracing_snapshot() that also does the snapshot allocation - the snapshot triggers would like to be able to do just the allocation but not take a snapshot; the existing tracing_snapshot_alloc() in turn now also calls tracing_alloc_snapshot() underneath to do that allocation. Link: http://lkml.kernel.org/r/c9524dd07ce01f9dcbd59011290e0a8d5b47d7ad.1382622043.git.tom.zanussi@linux.intel.com Signed-off-by: Tom Zanussi <[email protected]> [ fix up from kbuild test robot <[email protected] report ] Signed-off-by: Steven Rostedt <[email protected]>
1 parent 2a2df32 commit 93e31ff

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

include/linux/ftrace_event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ struct ftrace_event_file {
346346
enum event_trigger_type {
347347
ETT_NONE = (0),
348348
ETT_TRACE_ONOFF = (1 << 0),
349+
ETT_SNAPSHOT = (1 << 1),
349350
};
350351

351352
extern void destroy_preds(struct ftrace_event_file *file);

kernel/trace/trace.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,28 @@ void free_snapshot(struct trace_array *tr)
594594
tr->allocated_snapshot = false;
595595
}
596596

597+
/**
598+
* tracing_alloc_snapshot - allocate snapshot buffer.
599+
*
600+
* This only allocates the snapshot buffer if it isn't already
601+
* allocated - it doesn't also take a snapshot.
602+
*
603+
* This is meant to be used in cases where the snapshot buffer needs
604+
* to be set up for events that can't sleep but need to be able to
605+
* trigger a snapshot.
606+
*/
607+
int tracing_alloc_snapshot(void)
608+
{
609+
struct trace_array *tr = &global_trace;
610+
int ret;
611+
612+
ret = alloc_snapshot(tr);
613+
WARN_ON(ret < 0);
614+
615+
return ret;
616+
}
617+
EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
618+
597619
/**
598620
* trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
599621
*
@@ -607,11 +629,10 @@ void free_snapshot(struct trace_array *tr)
607629
*/
608630
void tracing_snapshot_alloc(void)
609631
{
610-
struct trace_array *tr = &global_trace;
611632
int ret;
612633

613-
ret = alloc_snapshot(tr);
614-
if (WARN_ON(ret < 0))
634+
ret = tracing_alloc_snapshot();
635+
if (ret < 0)
615636
return;
616637

617638
tracing_snapshot();
@@ -623,6 +644,12 @@ void tracing_snapshot(void)
623644
WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
624645
}
625646
EXPORT_SYMBOL_GPL(tracing_snapshot);
647+
int tracing_alloc_snapshot(void)
648+
{
649+
WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
650+
return -ENODEV;
651+
}
652+
EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
626653
void tracing_snapshot_alloc(void)
627654
{
628655
/* Give warning */

kernel/trace/trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ struct event_command {
12111211

12121212
extern int trace_event_enable_disable(struct ftrace_event_file *file,
12131213
int enable, int soft_disable);
1214+
extern int tracing_alloc_snapshot(void);
12141215

12151216
extern const char *__start___trace_bprintk_fmt[];
12161217
extern const char *__stop___trace_bprintk_fmt[];

kernel/trace/trace_events_trigger.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,90 @@ static struct event_command trigger_traceoff_cmd = {
696696
.get_trigger_ops = onoff_get_trigger_ops,
697697
};
698698

699+
#ifdef CONFIG_TRACER_SNAPSHOT
700+
static void
701+
snapshot_trigger(struct event_trigger_data *data)
702+
{
703+
tracing_snapshot();
704+
}
705+
706+
static void
707+
snapshot_count_trigger(struct event_trigger_data *data)
708+
{
709+
if (!data->count)
710+
return;
711+
712+
if (data->count != -1)
713+
(data->count)--;
714+
715+
snapshot_trigger(data);
716+
}
717+
718+
static int
719+
register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
720+
struct event_trigger_data *data,
721+
struct ftrace_event_file *file)
722+
{
723+
int ret = register_trigger(glob, ops, data, file);
724+
725+
if (ret > 0 && tracing_alloc_snapshot() != 0) {
726+
unregister_trigger(glob, ops, data, file);
727+
ret = 0;
728+
}
729+
730+
return ret;
731+
}
732+
733+
static int
734+
snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
735+
struct event_trigger_data *data)
736+
{
737+
return event_trigger_print("snapshot", m, (void *)data->count,
738+
data->filter_str);
739+
}
740+
741+
static struct event_trigger_ops snapshot_trigger_ops = {
742+
.func = snapshot_trigger,
743+
.print = snapshot_trigger_print,
744+
.init = event_trigger_init,
745+
.free = event_trigger_free,
746+
};
747+
748+
static struct event_trigger_ops snapshot_count_trigger_ops = {
749+
.func = snapshot_count_trigger,
750+
.print = snapshot_trigger_print,
751+
.init = event_trigger_init,
752+
.free = event_trigger_free,
753+
};
754+
755+
static struct event_trigger_ops *
756+
snapshot_get_trigger_ops(char *cmd, char *param)
757+
{
758+
return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
759+
}
760+
761+
static struct event_command trigger_snapshot_cmd = {
762+
.name = "snapshot",
763+
.trigger_type = ETT_SNAPSHOT,
764+
.func = event_trigger_callback,
765+
.reg = register_snapshot_trigger,
766+
.unreg = unregister_trigger,
767+
.get_trigger_ops = snapshot_get_trigger_ops,
768+
};
769+
770+
static __init int register_trigger_snapshot_cmd(void)
771+
{
772+
int ret;
773+
774+
ret = register_event_command(&trigger_snapshot_cmd);
775+
WARN_ON(ret < 0);
776+
777+
return ret;
778+
}
779+
#else
780+
static __init int register_trigger_snapshot_cmd(void) { return 0; }
781+
#endif /* CONFIG_TRACER_SNAPSHOT */
782+
699783
static __init void unregister_trigger_traceon_traceoff_cmds(void)
700784
{
701785
unregister_event_command(&trigger_traceon_cmd);
@@ -719,6 +803,7 @@ static __init int register_trigger_traceon_traceoff_cmds(void)
719803
__init int register_trigger_cmds(void)
720804
{
721805
register_trigger_traceon_traceoff_cmds();
806+
register_trigger_snapshot_cmd();
722807

723808
return 0;
724809
}

0 commit comments

Comments
 (0)