libtracefs: Add some helpful APIS
cover libtracefs: Add some helpful APIS
From: "Steven Rostedt (Google)" <[email protected]>
Add tracefs_event_is_enabled() (which supersedes https://lore.kernel.org/all/[email protected]/)
Also add getting and setting the ring buffer size.
Steven Rostedt (Google) (3):
libtracefs: Add tracefs_event_is_enabled() API
libtracefs: Add tracefs_instance_get_buffer_size() API
libtracefs: Add tracefs_instance_set_buffer_size() API
Documentation/libtracefs-events.txt | 33 ++++- Documentation/libtracefs-instances-utils.txt | 22 +++- Documentation/libtracefs.txt | 4 + include/tracefs.h | 10 ++ src/tracefs-events.c | 128 +++++++++++++++++-- src/tracefs-instance.c | 64 ++++++++++ 6 files changed, 247 insertions(+), 14 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/3 libtracefs: Add tracefs_event_is_enabled() API
From: "Steven Rostedt (Google)" <[email protected]>
Add a function that checks if an event (or events) are enabled or not. Returns the enums:
TRACEFS_ERROR = -1
TRACEFS_ALL_DISABLED = 0
TRACEFS_ALL_ENABLED = 1
TRACEFS_SOME_ENABLED = 2
--- Documentation/libtracefs-events.txt | 33 ++++++- Documentation/libtracefs.txt | 2 + include/tracefs.h | 9 ++ src/tracefs-events.c | 128 ++++++++++++++++++++++++++-- 4 files changed, 162 insertions(+), 10 deletions(-)
Documentation/libtracefs-events.txt +32 -1
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_event_systems, tracefs_system_events, tracefs_event_enable, tracefs_event_disable, -tracefs_iterate_raw_events, tracefs_iterate_stop - Work with trace systems and events. +tracefs_event_is_enabled, tracefs_iterate_raw_events, tracefs_iterate_stop - Work with trace systems and events. SYNOPSIS --------
@@ -12,12 +12,22 @@ SYNOPSIS -- *#include <tracefs.h>* +enum tracefs_event_state { + TRACEFS_ERROR = -1, + TRACEFS_ALL_DISABLED = 0, + TRACEFS_ALL_ENABLED = 1, + TRACEFS_SOME_ENABLED = 2, +}; + char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_); char pass:[*]pass:[*]*tracefs_system_events*(const char pass:[*]_tracing_dir_, const char pass:[*]_system_); int *tracefs_event_enable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); int *tracefs_event_disable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); +enum tracefs_enable_state *tracefs_event_is_enabled*(struct tracefs_instance pass:[*]_instance_, + const char pass:[*]_system_, const char pass:[*]_event_); + int *tracefs_iterate_raw_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_cpus_, int _cpu_size_, int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]),
@@ -61,6 +71,24 @@ events. That is, if _instance_ is NULL, then the top level tracing directory is used. If both _system_ and _event_ are NULL then all events are disabled for the given _instance_, and so on. +The *tracefs_event_is_enabled()* returns if an event is enabled, a set of +events are enabled, a system is enabled, or all events are enabled. If both +_system_ and _event_ are NULL, then it returns the enable state of all events. +If _system_ is not NULL and _event_ is NULL, then it will check if all the events +in all the systems that _system_ and return the enable state of those events. +If _system_ is NULL and _event_ is not NULL, then it will match all the events +in all systems that match _event_ and return their enabled state. If both _system_ +and _event_ are not NULL, then it will return the enabled state of all matching +events. The enabled state is defined as: + +*TRACEFS_ERROR* - An error occurred including no event were matched. + +*TRACEFS_ALL_DISABLED* - All matching events are disabled. + +*TRACEFS_ALL_ENABLED* - All matching events are enabled. + +*TRACEFS_SOME_ENABLED* - Some matching events were enabled while others were not. + The *tracefs_iterate_raw_events()* function will read the tracefs raw data buffers and call the specified _callback_ function for every event it encounters. Events are iterated in sorted order: oldest first. An initialized
@@ -95,6 +123,9 @@ found, it will return -1 and errno will be set. If no errors occur, but no event are found that match the _system_ and _event_ parameters, then -1 is returned and errno is not set. +The *tracefs_event_is_enabled()* returns the enabled status of the matching events +or TRACEFS_ERROR on error. + The *tracefs_iterate_raw_events()* function returns -1 in case of an error or 0 otherwise.
Documentation/libtracefs.txt +2 -0
@@ -53,6 +53,8 @@ Trace events: const char pass:[*]_event_); int *tracefs_event_disable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); + enum tracefs_enable_state *tracefs_event_is_enabled*(struct tracefs_instance pass:[*]_instance_, + const char pass:[*]_system_, const char pass:[*]_event_); int *tracefs_iterate_raw_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_cpus_, int _cpu_size_, int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]), void pass:[*]_callback_context_); void *tracefs_iterate_stop*(struct tracefs_instance pass:[*]_instance_); struct tep_handle pass:[*]*tracefs_local_events*(const char pass:[*]_tracing_dir_);
include/tracefs.h +9 -0
@@ -67,8 +67,17 @@ int tracefs_trace_off(struct tracefs_instance *instance);int tracefs_trace_on_fd (int fd);int tracefs_trace_off_fd (int fd); +enum tracefs_enable_state { +TRACEFS_ERROR =-1 , +TRACEFS_ALL_DISABLED =0 , +TRACEFS_ALL_ENABLED =1 , +TRACEFS_SOME_ENABLED =2 , +}; +int tracefs_event_enable (struct tracefs_instance *instance,const char *system,const char *event);int tracefs_event_disable (struct tracefs_instance *instance,const char *system,const char *event); +enum tracefs_enable_state tracefs_event_is_enabled (struct tracefs_instance *instance, +const char *system,const char *event);char *tracefs_error_last (struct tracefs_instance *instance);char *tracefs_error_all (struct tracefs_instance *instance);
src/tracefs-events.c +119 -9
181 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 2/3 libtracefs: Add tracefs_instance_get_buffer_size() API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the function tracefs_instance_get_buffer_size() to return the size of the ring buffer.
--- Documentation/libtracefs-instances-utils.txt | 14 +++++--- Documentation/libtracefs.txt | 1 + include/tracefs.h | 1 + src/tracefs-instance.c | 37 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-)
Documentation/libtracefs-instances-utils.txt +10 -4
@@ -3,9 +3,8 @@ libtracefs(3) NAME ---- -tracefs_instance_get_name, tracefs_instance_get_trace_dir, -tracefs_instances_walk, tracefs_instance_exists - -Helper functions for working with tracing instances. +tracefs_instance_get_name, tracefs_instance_get_trace_dir, tracefs_instances_walk, tracefs_instance_exists, +tracefs_instance_get_buffer_size - Helper functions for working with tracing instances. SYNOPSIS --------
@@ -17,7 +16,7 @@ const char pass:[*]*tracefs_instance_get_name*(struct tracefs_instance pass:[*]_ const char pass:[*]*tracefs_instance_get_trace_dir*(struct tracefs_instance pass:[*]_instance_); int *tracefs_instances_walk*(int (pass:[*]_callback_)(const char pass:[*], void pass:[*]), void pass:[*]_context)_; bool *tracefs_instance_exists*(const char pass:[*]_name_); - +size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); -- DESCRIPTION
@@ -39,6 +38,10 @@ called for the top top instance. The *tracefs_instance_exists()* function checks if an instance with the given _name_ exists in the system. +The *tracefs_instace_get_buffer_size()* returns the size of the ring buffer. If _cpu_ +is negative, it returns the total size of all the per CPU ring buffers, otherwise +it returns the size of the per CPU ring buffer for _cpu_. + RETURN VALUE ------------ The *tracefs_instance_get_name()* returns a string or NULL in case of the top
@@ -53,6 +56,9 @@ if the iteration was stopped by the _callback_, or -1 in case of an error. The *tracefs_instance_exists()* returns true if an instance with the given _name_ exists in the system or false otherwise. +The *tracefs_instance_get_buffer_size()* returns the size of the ring buffer depending on +the _cpu_ value passed in, or -1 on error. + EXAMPLE ------- [source,c]
Documentation/libtracefs.txt +1 -0
@@ -45,6 +45,7 @@ Trace instances: char pass:[*]*tracefs_instance_get_affinity*(struct tracefs_instance pass:[*]_instance_); int *tracefs_instance_get_affinity_set*(struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_set_, size_t _set_size_); char pass:[*]*tracefs_instance_get_affinity_raw*(struct tracefs_instance pass:[*]_instance_); + size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); Trace events: char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_);
include/tracefs.h +1 -0
@@ -55,6 +55,7 @@ char *tracefs_instance_get_affinity(struct tracefs_instance *instance);char *tracefs_instance_get_affinity_raw (struct tracefs_instance *instance);int tracefs_instance_get_affinity_set (struct tracefs_instance *instance,cpu_set_t *set,size_t set_size); +ssize_t tracefs_instance_get_buffer_size (struct tracefs_instance *instance,int cpu);char **tracefs_instances (const char *regex);bool tracefs_instance_exists (const char *name);
src/tracefs-instance.c +37 -0
@@ -363,6 +363,43 @@ const char *tracefs_instance_get_name(struct tracefs_instance *instance)return NULL ; } +/** +* tracefs_instance_get_buffer_size - return the buffer size of the ring buffer +* @instance: The instance to get the buffer size from +* @cpu: if less that zero, will return the total size, otherwise the cpu size +* +* Returns the buffer size. If @cpu is less than zero, it returns the total size +* of the ring buffer otherwise it returs the size of the buffer for the given +* CPU. +* +* Returns -1 on error. +*/ +ssize_t tracefs_instance_get_buffer_size (struct tracefs_instance *instance,int cpu) +{ +unsigned long long size; +char *path; +char *val; +int ret; + +if (cpu <0 ) { + val =tracefs_instance_file_read (instance,"buffer_total_size_kb" ,NULL ); + }else { + ret =asprintf (&path,"per_cpu/cpu%d/buffer_size_kb" , cpu); +if (ret <0 ) +return ret; + + val =tracefs_instance_file_read (instance, path,NULL ); +free (path); + } + +if (!val) +return -1 ; + + size =strtoull (val,NULL ,0 ); +free (val); +return size; +} +/** * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred * @instance: ftrace instance
Signature · 1 line
2.35.1
PATCH 3/3 libtracefs: Add tracefs_instance_set_buffer_size() API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add functionality that sets the ring buffer size.
--- Documentation/libtracefs-instances-utils.txt | 10 +++++++- Documentation/libtracefs.txt | 1 + src/tracefs-instance.c | 27 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-)
Documentation/libtracefs-instances-utils.txt +9 -1
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_instance_get_name, tracefs_instance_get_trace_dir, tracefs_instances_walk, tracefs_instance_exists, -tracefs_instance_get_buffer_size - Helper functions for working with tracing instances. +tracefs_instance_get_buffer_size, tracefs_instance_set_buffer_size - Helper functions for working with tracing instances. SYNOPSIS --------
@@ -17,6 +17,7 @@ const char pass:[*]*tracefs_instance_get_trace_dir*(struct tracefs_instance pass int *tracefs_instances_walk*(int (pass:[*]_callback_)(const char pass:[*], void pass:[*]), void pass:[*]_context)_; bool *tracefs_instance_exists*(const char pass:[*]_name_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); +int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); -- DESCRIPTION
@@ -42,6 +43,11 @@ The *tracefs_instace_get_buffer_size()* returns the size of the ring buffer. If is negative, it returns the total size of all the per CPU ring buffers, otherwise it returns the size of the per CPU ring buffer for _cpu_. +The *tracefs_instance_set_buffer_size()* function sets the size of the ring buffer. +If _cpu_ is negative, then it sets all the per CPU ring buffers to _size_ (note +the total size is the number of CPUs * _size_). If _cpu_ is specified, then it only +sets the size of the per CPU ring buffer. + RETURN VALUE ------------ The *tracefs_instance_get_name()* returns a string or NULL in case of the top
@@ -59,6 +65,8 @@ exists in the system or false otherwise. The *tracefs_instance_get_buffer_size()* returns the size of the ring buffer depending on the _cpu_ value passed in, or -1 on error. +The *tracefs_instance_set_buffer_size()* returns zero on success and -1 on error. + EXAMPLE ------- [source,c]
Documentation/libtracefs.txt +1 -0
@@ -46,6 +46,7 @@ Trace instances: int *tracefs_instance_get_affinity_set*(struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_set_, size_t _set_size_); char pass:[*]*tracefs_instance_get_affinity_raw*(struct tracefs_instance pass:[*]_instance_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); + int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); Trace events: char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_);
src/tracefs-instance.c +27 -0
@@ -400,6 +400,33 @@ ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, intreturn size; } +int tracefs_instance_set_buffer_size (struct tracefs_instance *instance,size_t size,int cpu) +{ +char *path; +char *val; +int ret; + + ret =asprintf (&val,"%zd" , size); +if (ret <0 ) +return ret; + +if (cpu <0 ) { + ret =tracefs_instance_file_write (instance,"buffer_size_kb" , val); + }else { + ret =asprintf (&path,"per_cpu/cpu%d/buffer_size_kb" , cpu); +if (ret <0 ) { +free (val); +return ret; + } + + ret =tracefs_instance_file_write (instance, path,"val" ); +free (path); + } +free (val); + +return ret <0 ?-1 :0 ; +} +/** * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred * @instance: ftrace instance
Signature · 1 line
2.35.1