The current implementation of tracefs_synth_create() API always creates the synthetic event in the top trace instance - the "trigger" files of the start and end events are updated in the context of the top instance. Then the same synthetic event can be enabled and operates in any trace instance. That logic works well in the most use cases, where there is a single application, managing these events and the trace process. However, there are use cases where multiple applications can run different trace sessions, each in its own trace instance. For those use cases, the default logic does not work well - the synthetic event can be created by one application in the top instance, and cannot be reused easily by any other applications in different trace instance. The tracefs_synth structure already has an instance context, but there is no way to set it to other than default top instance. That's why a new API is introduced: tracefs_synth_set_instance(), which sets the instance in the tracefs_synth structure. If that API is called before tracefs_synth_create(), then the "trigger" files will be updated only in the context of that user instance, instead of the default one. This allows synthetic events to be more flexible and reduces the interference between different trace instance. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- include/tracefs.h | 1 + src/tracefs-hist.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/tracefs.h b/include/tracefs.h index 3547b5a..9a53e0e 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -607,6 +607,7 @@ struct tracefs_hist *tracefs_synth_get_start_hist(struct tracefs_synth *synth); int tracefs_synth_create(struct tracefs_synth *synth); int tracefs_synth_destroy(struct tracefs_synth *synth); void tracefs_synth_free(struct tracefs_synth *synth); +int tracefs_synth_set_instance(struct tracefs_synth *synth, struct tracefs_instance *instance); int tracefs_synth_echo_cmd(struct trace_seq *seq, struct tracefs_synth *synth); int tracefs_synth_raw_fmt(struct trace_seq *seq, struct tracefs_synth *synth); const char *tracefs_synth_show_event(struct tracefs_synth *synth); diff --git a/src/tracefs-hist.c b/src/tracefs-hist.c index fb6231e..9141bbb 100644 --- a/src/tracefs-hist.c +++ b/src/tracefs-hist.c @@ -2178,6 +2178,25 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth) return hist; } +/** + * tracefs_synth_set_instance - Set the ftrace instance of the synthetic events + * @synth: The tracefs_synth descriptor + * @instance: ftrace instance + * + * Set the ftrace instance, in which the synthetic event will be created. By default, + * the top instance is used. This API must be called before the call to tracefs_synth_create(), + * in order to use the new instance when creating the event. + * + * Returns 0 on success and -1 on error. + */ +int tracefs_synth_set_instance(struct tracefs_synth *synth, struct tracefs_instance *instance) +{ + if (!synth) + return -1; + synth->instance = instance; + return 0; +} + /** * tracefs_synth_create - creates the synthetic event on the system * @synth: The tracefs_synth descriptor -- 2.39.2