libtracefs: Creating synthetic events
cover libtracefs: Creating synthetic events
From: "Steven Rostedt (VMware)" <[email protected]>
Add an interface to libtracefs that makes it easy to create synthetic events. For example:
struct tracefs_synth *synth; struct tep_handle *tep; struct trace_seq seq; /* Load all events from the system */ tep = tracefs_local_events(NULL); /* Initialize the synthetic event */ synth = tracefs_synth_init(tep, "wakeup_lat", NULL, start_event, NULL, end_event, match_name, start_field, end_field); /* The tep is no longer needed */ tep_free(tep); /* Save the "prio" field as "prio" from the start event */ tracefs_synth_add_start_field(synth, NULL, "prio"); /* Save the "next_comm" as "comm" from the end event */ tracefs_synth_add_end_field(synth, "comm", "next_comm"); /* Save the "prev_prio" as "prev_prio" from the end event */ tracefs_synth_add_end_field(synth, NULL, "prev_prio"); /* * Take a microsecond time difference between end and start * and record as "delta" */ tracefs_synth_add_compare_field(synth, "delta", TRACEFS_TIMESTAMP_USECS, TRACEFS_TIMESTAMP_USECS, TRACEFS_SYNTH_DELTA_END); /* Only record if start event "prio" is less than 100 */ tracefs_synth_add_start_filter(synth, "prio", TRACEFS_COMPARE_LT, "100", false, false); /* * Only record if end event "next_prio" is less than 50 * or the previous task's prio was less than 100. */ tracefs_synth_add_end_filter(synth, "next_prio", TRACEFS_COMPARE_LT, "50", false, false); tracefs_synth_add_end_filter(synth, "prev_prio", TRACEFS_COMPARE_LT, "100", false, true); trace_seq_init(&seq); tracefs_synth_show(&seq, tracefs_synth_show(&s, NULL, synth); trace_seq_terminate(&s); trace_seq_do_printf(&s); trace_seq_destroy(&s); tracefs_synth_free(synth);
Will produce:
echo 'wakeup_lat s32 pid; s32 prio; char comm[16]; s32 prev_prio; u64 delta;' > /sys/kernel/tracing/synthetic_events
echo 'hist:keys=pid:__arg__1=prio,__arg__2=common_timestamp.usecs if (prio < 100)' > /sys/kernel/tracing/events/sched/sched_waking/trigger
echo 'hist:keys=next_pid:pid=next_pid,prio=$__arg__1,comm=next_comm,prev_prio=prev_prio,delta=common_timestamp.usecs-$__arg__2:onmatch(sched.sched_waking).trace(wakeup_lat,$pid,$prio,$comm,$prev_prio,$delta) if (next_prio < 50) || (prev_prio < 100)' > /sys/kernel/tracing/events/sched/sched_switch/trigger
There's functionality to also create and destroy the synthetic event, not only produce the above.
Once this is in libtracefs, it can then be brought over to trace-cruncher where this can be done in Python.
Steven Rostedt (VMware) (4):
libtracefs: Add tracefs_list_pop() to remove the last item
libtracefs: Create a way to create a synthetic event
libtracefs: Add TRACEFS_TIMESTAMP and TRACEFS_TIMESTAMP_USECS to synth
libtracefs: Add man pages for creating synthetic events
Documentation/libtracefs-synth.txt | 353 +++++++++ include/tracefs.h | 70 ++ src/tracefs-hist.c | 1104 ++++++++++++++++++++++++++++ src/tracefs-utils.c | 26 + 4 files changed, 1553 insertions(+) create mode 100644 Documentation/libtracefs-synth.txt
Signature · 1 line
2.30.2
PATCH 1/4 libtracefs: Add tracefs_list_pop() to remove the last item
From: "Steven Rostedt (VMware)" <[email protected]>
Add a way to remove the last item of a string list. This will be needed to revert changes made to the new tracefs_synth structure that will have more than one tracefs_list char ** that must be the same size. If one is successfully updated, but the next one fails, the first one needs to be reverted.
--- include/tracefs.h | 1 + src/tracefs-utils.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/tracefs.h +1 -0
@@ -66,6 +66,7 @@ int tracefs_error_clear(struct tracefs_instance *instance);void tracefs_list_free (char **list);char **tracefs_list_add (char **list,const char *string);int tracefs_list_size (char **list); +int tracefs_list_pop (char **list);/** * tracefs_trace_on_get_fd - Get a file descriptor of "tracing_on" in given instance
src/tracefs-utils.c +26 -0
@@ -448,6 +448,32 @@ char **tracefs_list_add(char **list, const char *string)return list; } +/** +* tracefs_list_pop - Removes the last string added +* @list: The list to remove the last event from +* +* Returns 0 on success, -1 on error. +* Returns 1 if the list is empty or NULL. +*/ +int tracefs_list_pop (char **list) +{ +unsigned long size; + +if (!list || list[0 ]) +return 1 ; + + list--; + size = *(unsigned long *)list; +/* size must be greater than zero */ +if (!size) +return -1 ; + size--; + *list = (char *)size; + list++; + list[size] ='\0' ; +return 0 ; +} +/** * tracefs_list_size - Return the number of strings in the list * @list: The list to determine the size.
Signature · 1 line
2.30.2
PATCH 2/4 libtracefs: Create a way to create a synthetic event
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the following APIs:
tracefs_synth_init()
tracefs_synth_add_match_field()
tracefs_synth_add_compare_field()
tracefs_synth_add_start_field()
tracefs_synth_add_end_field()
tracefs_synth_add_start_filter()
tracefs_synth_add_end_filter()
tracefs_synth_create()
tracefs_synth_destroy()
tracefs_synth_free()
tracefs_synth_show()
to be able to easily create synthetic events using the histogram triggers.
--- include/tracefs.h | 66 +++ src/tracefs-hist.c | 1080 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1146 insertions(+)
include/tracefs.h +66 -0
@@ -300,4 +300,70 @@ int tracefs_hist_continue(struct tracefs_hist *hist);int tracefs_hist_reset (struct tracefs_hist *hist);int tracefs_hist_destroy (struct tracefs_hist *hist); +struct tracefs_synth ; + +/* +* DELTA_END - end_field - start_field +* DELTA_START - start_field - end_field +* ADD - start_field + end_field +*/ +enum tracefs_synth_calc { +TRACEFS_SYNTH_DELTA_END , +TRACEFS_SYNTH_DELTA_START , +TRACEFS_SYNTH_ADD , +}; + +enum tracefs_synth_compare { +TRACEFS_COMPARE_EQ , +TRACEFS_COMPARE_NQ , +TRACEFS_COMPARE_GR , +TRACEFS_COMPARE_GE , +TRACEFS_COMPARE_LT , +TRACEFS_COMPARE_LE , +TRACEFS_COMPARE_RE , +TRACEFS_COMPARE_AND , +}; + +struct tracefs_synth *tracefs_synth_init (struct tep_handle *tep, +const char *name, +const char *start_system, +const char *start_event, +const char *end_system, +const char *end_event, +const char *match_name, +const char *start_match_field, +const char *end_match_field); +int tracefs_synth_add_match_field (struct tracefs_synth *synth, +const char *name, +const char *start_match_field, +const char *end_match_field); +int tracefs_synth_add_compare_field (struct tracefs_synth *synth, +const char *name, +const char *start_compare_field, +const char *end_compare_field, +enum tracefs_synth_calc calc); +int tracefs_synth_add_start_field (struct tracefs_synth *synth, +const char *name, +const char *start_field); +int tracefs_synth_add_end_field (struct tracefs_synth *synth, +const char *name, +const char *end_field); +int tracefs_synth_add_start_filter (struct tracefs_synth *synth, +const char *field, +enum tracefs_synth_compare compare, +const char *val, +bool neg,bool or); +int tracefs_synth_add_end_filter (struct tracefs_synth *synth, +const char *field, +enum tracefs_synth_compare compare, +const char *val, +bool neg,bool or); +int tracefs_synth_create (struct tracefs_instance *instance, +struct tracefs_synth *synth); +int tracefs_synth_destroy (struct tracefs_instance *instance, +struct tracefs_synth *synth); +void tracefs_synth_free (struct tracefs_synth *synth); +int tracefs_synth_show (struct trace_seq *seq,struct tracefs_instance *instance, +struct tracefs_synth *synth); +#endif /* _TRACE_FS_H */
src/tracefs-hist.c +1080 -0
1,083 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 3/4 libtracefs: Add TRACEFS_TIMESTAMP and TRACEFS_TIMESTAMP_USECS to synth
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the fields of TRACEFS_TIMESTAMP and TRACEFS_TIMESTAMP_USECS that can be used to calculate the timestamps between the events for the synthetic event creation.
--- include/tracefs.h | 3 +++ src/tracefs-hist.c | 50 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 13 deletions(-)
include/tracefs.h +3 -0
@@ -324,6 +324,9 @@ enum tracefs_synth_compare { TRACEFS_COMPARE_AND, }; +#define TRACEFS_TIMESTAMP "common_timestamp" +#define TRACEFS_TIMESTAMP_USECS "common_timestamp.usecs" + struct tracefs_synth *tracefs_synth_init(struct tep_handle *tep, const char *name, const char *start_system,
src/tracefs-hist.c +37 -13
120 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 4/4 libtracefs: Add man pages for creating synthetic events
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the man pages that describe all the synthetic event operations.
--- Documentation/libtracefs-synth.txt | 353 +++++++++++++++++++++++++++++ 1 file changed, 353 insertions(+) create mode 100644 Documentation/libtracefs-synth.txt
Documentation/libtracefs-synth.txt new file +353 -0
@@ -0,0 +1,353 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_synth_init, tracefs_synth_add_match_field, tracefs_synth_add_compare_field, tracefs_synth_add_start_field, +tracefs_synth_add_end_field, tracefs_synth_add_start_filter, tracefs_synth_add_end_filter, tracefs_synth_create, +tracefs_synth_destroy, tracefs_synth_free, tracefs_synth_show - Creation of synthetic events + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +struct tracefs_synth pass:[*]tracefs_synth_init(struct tep_handle pass:[*]tep, + const char pass:[*]name, + const char pass:[*]start_system, + const char pass:[*]start_event, + const char pass:[*]end_system, + const char pass:[*]end_event, + const char pass:[*]match_name, + const char pass:[*]start_match_field, + const char pass:[*]end_match_field); +int tracefs_synth_add_match_field(struct tracefs_synth pass:[*]synth, + const char pass:[*]name, + const char pass:[*]start_match_field, + const char pass:[*]end_match_field); +int tracefs_synth_add_compare_field(struct tracefs_synth pass:[*]synth, + const char pass:[*]name, + const char pass:[*]start_compare_field, + const char pass:[*]end_compare_field, + enum tracefs_synth_calc calc); +int tracefs_synth_add_start_field(struct tracefs_synth pass:[*]synth, + const char pass:[*]name, + const char pass:[*]start_field); +int tracefs_synth_add_end_field(struct tracefs_synth pass:[*]synth, + const char pass:[*]name, + const char pass:[*]end_field); +int tracefs_synth_add_start_filter(struct tracefs_synth pass:[*]synth, + const char pass:[*]field, + enum tracefs_synth_compare compare, + const char pass:[*]val, + bool neg, bool or); +int tracefs_synth_add_end_filter(struct tracefs_synth pass:[*]synth, + const char pass:[*]field, + enum tracefs_synth_compare compare, + const char pass:[*]val, + bool neg, bool or); +int tracefs_synth_create(struct tracefs_instance pass:[*]instance, + struct tracefs_synth pass:[*]synth); +int tracefs_synth_destroy(struct tracefs_instance pass:[*]instance, + struct tracefs_synth pass:[*]synth); +void tracefs_synth_free(struct tracefs_synth pass:[*]synth); +int tracefs_synth_show(struct trace_seq pass:[*]seq, struct tracefs_instance pass:[*]instance, + struct tracefs_synth pass:[*]synth); + +-- + +DESCRIPTION +----------- +Synthetic events are dynamic events that are created by matching +two other events which triggers a synthetic event. One event is the starting +event which some field is recorded, and when the second event is executed, +if it has a field (or fields) that matches the starting event's field (or fields) +then it will trigger the synthetic event. The field values other than the matching +fields may be passed from the starting event to the end event to perform calculations +on, or to simply pass as a parameter to the synthetic event. + +One common use case is to set "sched_waking" as the starting event. This event is +triggered when a process is awoken. Then set "sched_switch" as the ending event. +This event is triggered when a new task is scheduled on the CPU. By setting +the "common_pid" of both events as the matching fields, the time between the +two events is considered the wake up latency of that process. Use *TRACEFS_TIMESTAMP* +as a field for both events to calculate the delta in nanoseconds, or use +*TRACEFS_TIMESTAMP_USECS" as the compare fields for both events to calculate the +delta in microseconds. This is used as the example below. + +*tracefs_synth_init*() allocates and initializes a synthetic event. +It does not create the synthetic event, but supplies the minimal information +to do so. See *tracefs_synth_create*() below for how to create the synthetic +event in the system. It requires a _tep_ handler that can be created by +*tracefs_local_events*(3) for more information. The _name_ holds the name +of the synthetic event that will be created. The _start_system is the name +of the system for the starting event. It may be NULL and the first event +with the name of _start_event_ will be chosen. The _end_system_ is the +name of the system for theh ending event. It may be NULL and the first event +with the name of _end_event_ will be chosen as the ending event. If _match_name_ +is given, then this will be the field of the created synthetic event that +holds the matching keys of the starting event's _start_match_field_ and +the ending event's _end_match_field_. If _match_name_ is NULL, then it will +not be recorded in the created synthetic event. + +*tracefs_synth_add_match_field*() will add a second key to match between the +starting event and the ending event. If _name_ is given, then the content +of the matching field will be saved by this _name_ in the synthetic event. +The _start_match_field_ is the field of the starting event to mach with the +ending event's _end_match_field. + +*tracefs_synth_add_compare_field*() is used to compare the _start_compare_field_ +of the starting event with the _end_compare_field_ of the ending event. The _name_ +must be given so that the result will be saved by the synthetic event. It makes +no sense to not pass this to the synthetic event after doing the work of +the compared fields, as it serves no other purpose. The _calc_ parameter +can be one of: + +*TRACEFS_SYNTH_DELTA_END* - calculate the difference between the content in + the _end_compare_field_ from the content of the _start_compare_field_. + + _name_ = _end_compare_field_ - _start_compare_field_ + +*TRACEFS_SYNTH_DELTA_START* - calculate the difference between the content in + the _start_compare_field_ from the content of the _end_compare_field_. + + _name_ = _start_compare_field_ - _end_compare_field_ + +*TRACEFS_SYNTH_ADD* - Add the content of the _start_compare_field_ to the + content of the _end_compare_field_. + + _name_ = _start_compare_field_ + _end_compare_field_ + +*tracefs_synth_add_start_field*() - Records the _start_field_ of the starting +event as _name_ in the synthetic event. If _name_ is NULL, then the name used +will be the same as _start_field_. + +*tracefs_synth_add_end_field*() - Records the _end_field_ of the ending +event as _name_ in the synthetic event. If _name_ is NULL, then the name used +will be the same as _end_field_. + +*tracefs_synth_add_start_filter*() adds a filter to the starting event +comparing the content of the starting event's _field_ to _val_ based +on _compare_. If _neg_ is set, then the compare is wrapped in parenthesis +and negated. The _or_ field only is used if more than one call to +*tracefs_synth_add_start_filter*() is done, and if _or_ is set, the +next compare is "or'd" (||), otherwise it is "and'd" (&&). _compare_ +may be one of: + +*TRACEFS_COMPARE_EQ* - _field_ == _val_ + +*TRACEFS_COMPARE_NQ* - _field_ != _val_ + +*TRACEFS_COMPARE_GR* - _field_ > _val_ + +*TRACEFS_COMPARE_GE* - _field_ >= _val_ + +*TRACEFS_COMPARE_LT* - _field_ < _val_ + +*TRACEFS_COMPARE_LE* - _field_ <pass:[=] _val_ + +*TRACEFS_COMPARE_RE* - _field_ ~ "_val_" : where _field_ is a string. + +*TRACEFS_COMPARE_AND* - _field_ & _val_ : where _field_ is a flags field. + +*tracefs_synth_add_end_filter*() is the same as *tracefs_synth_add_start_filter* but +filters on the ending event. + +*tracefs_synth_create*() creates the synthetic event in the system in the system +in the _instance_ provided. Note, synthetic events apply across all instances, +but some creation requires histograms to be established, which are local to +instances. + +*tracefs_synth_destroy*() destroys the synthetic event. It will attempt to stop +the running of it in the given _instance_, but if its running in another instance +this may fail as busy. + +*tracefs_synth_free*() frees the allocated descriptor returned by +*tracefs_synth_init*(). + +*tracefs_synth_show*() acts like *tracefs_synth_create*(), but instead of creating +the synthetic event in the given _instance_, it will write the echo commands to +manually create it in the _seq_ given. + +RETURN VALUE +------------ +*tracefs_synth_init*() returns an allocated struct tracefs_synth descriptor +on sucess or NULL on error. + +All other functions that return an integer returns zero on success or -1 +on error. + +ERRORS +------ +The following errors are for all the above calls: + +*EPERM* Not run as root user when required. + +*EINVAL* Either a parameter is not valid (NULL when it should not be) + or a field that is not compatible for calculations. + +*ENODEV* An event or one of its fields is not found. + +*EBADE* The fields of the start and end events are not compatible for + either matching or comparing. + +*ENOMEM* not enough memory is available. + +And more errors may have happened from the system calls to the system. + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <tracefs.h> + +#define start_event "sched_waking" +#define start_field "pid" + +#define end_event "sched_switch" +#define end_field "next_pid" + +#define match_name "pid" + +static struct tracefs_synth *synth; + +static void make_event(void) +{ + struct tep_handle *tep; + + /* Load all events from the system */ + tep = tracefs_local_events(NULL); + + /* Initialize the synthetic event */ + synth = tracefs_synth_init(tep, "wakeup_lat", + NULL, start_event, + NULL, end_event, + match_name, + start_field, end_field); + + /* The tep is no longer needed */ + tep_free(tep); + + + /* Save the "prio" field as "prio" from the start event */ + tracefs_synth_add_start_field(synth, NULL, "prio"); + + /* Save the "next_comm" as "comm" from the end event */ + tracefs_synth_add_end_field(synth, "comm", "next_comm"); + + /* Save the "prev_prio" as "prev_prio" from the end event */ + tracefs_synth_add_end_field(synth, NULL, "prev_prio"); + + /* + * Take a microsecond time difference between end and start + * and record as "delta" + */ + tracefs_synth_add_compare_field(synth, "delta", TRACEFS_TIMESTAMP_USECS, + TRACEFS_TIMESTAMP_USECS, + TRACEFS_SYNTH_DELTA_END); + + /* Only record if start event "prio" is less than 100 */ + tracefs_synth_add_start_filter(synth, "prio", + TRACEFS_COMPARE_LT, "100", + false, false); + + /* + * Only record if end event "next_prio" is less than 50 + * or the previous task's prio was less than 100. + */ + tracefs_synth_add_end_filter(synth, "next_prio", + TRACEFS_COMPARE_LT, "50", + false, false); + tracefs_synth_add_end_filter(synth, "prev_prio", + TRACEFS_COMPARE_LT, "100", + false, true); +} + +/* Display how to create the synthetic event */ +static void show_event(void) +{ + struct trace_seq s; + + trace_seq_init(&s); + + tracefs_synth_show(&s, NULL, synth); + trace_seq_terminate(&s); + trace_seq_do_printf(&s); + trace_seq_destroy(&s); +} + +int main (int argc, char **argv) +{ + make_event(); + + if (argc > 1) { + if (!strcmp(argv[1], "create")) { + /* Create the synthetic event */ + tracefs_synth_create(NULL, synth); + } else if (!strcmp(argv[1], "delete")) { + /* Delete the synthetic event */ + tracefs_synth_destroy(NULL, synth); + } else { + printf("usage: %s [create|delete]\n", argv[0]); + exit(-1); + } + } else + show_event(); + + tracefs_synth_free(synth); + + return 0; +} +-- + +FILES +----- +[verse] +-- +*tracefs.h* + Header file to include in order to have access to the library APIs. +*-ltracefs* + Linker switch to add when building a program that uses the library. +-- + +SEE ALSO +-------- +_libtracefs(3)_, +_libtraceevent(3)_, +_trace-cmd(1)_, +_tracefs_hist_alloc(3)_, +_tracefs_hist_free(3)_, +_tracefs_hist_add_key(3)_, +_tracefs_hist_add_value(3)_, +_tracefs_hist_add_name(3)_, +_tracefs_hist_start(3)_, +_tracefs_hist_destory(3)_, +_tracefs_hist_add_sort_key(3)_, +_tracefs_hist_sort_key_direction(3)_ + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +*Tzvetomir Stoyanov* <[email protected]> +*sameeruddin shaik* <[email protected]> +-- +REPORTING BUGS +-------------- +Report bugs to <[email protected]> + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2020 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Signature · 1 line
2.30.2
Quoted 5 lines · On Wed, 21 Jul 2021 23:39:13 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Add an interface to libtracefs that makes it easy to create synthetic events. For example:
I forgot to add (although it's in the man page)
#define start_event "sched_waking" #define start_field "pid" #define end_event "sched_switch" #define end_field "next_pid" #define match_name "pid"
-- Steve
Quoted 18 lines
struct tracefs_synth *synth; struct tep_handle *tep; struct trace_seq seq; /* Load all events from the system */ tep = tracefs_local_events(NULL); /* Initialize the synthetic event */ synth = tracefs_synth_init(tep, "wakeup_lat", NULL, start_event, NULL, end_event, match_name, start_field, end_field); /* The tep is no longer needed */ tep_free(tep);