Documentation and unit tests for new trace marker APIs
cover Documentation and unit tests for new trace marker APIs
Documentation and unit tests for new trace marker APIs
Tzvetomir Stoyanov (VMware) (2):
libtracefs: Document marker APIs
libtracefs: Unit test for trace marker APIs
Documentation/libtracefs-marker.txt | 127 ++++++++++++++++++++++++++++ utest/tracefs-utest.c | 91 ++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 Documentation/libtracefs-marker.txt
Signature · 1 line
2.30.2
PATCH 1/2 libtracefs: Document marker APIs
Added man pages describing these functions:
tracefs_marker_init()
tracefs_marker_print()
tracefs_marker_vprint()
tracefs_marker_write()
tracefs_marker_close()
Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>
--- Documentation/libtracefs-marker.txt | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Documentation/libtracefs-marker.txt
Documentation/libtracefs-marker.txt new file +127 -0
@@ -0,0 +1,127 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_marker_init, tracefs_marker_close, tracefs_marker_print, tracefs_marker_vprint, tracefs_marker_write - +Open, close and write data in the frace marker file. + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +int *tracefs_marker_init*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_marker_print*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, _..._); +int *tracefs_marker_vprint*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, va_list _ap_); +int *tracefs_marker_write*(struct tracefs_instance pass:[*]_instance_, void pass:[*]_data_, int _len_); +void *tracefs_marker_close*(struct tracefs_instance pass:[*]_instance_); + +-- + +DESCRIPTION +----------- +Set of functions to work with ftrace maker. See Documentation/trace/ftrace.rst from the Linux +kernel tree for more information about ftrace marker functionality. All these APIs have _instance_ +as a first argument. If NULL is passed as _instance_, the top trace instance is used. + +The _tracefs_marker_init()_ function initializes the library for working with ftrace marker of +the selected _instance_. It is not mandatory to call this API before working with the marker, any +of the marker APIs will call it automatically, if the library is not yet initialized to work with +the marker form given instance. But calling _tracefs_marker_init()_ in advance will speed up +the other marker APIs. + +The _tracefs_marker_print()_ function writes a formatted string in the trace marker of the selected +_instance_. The _fmt_ argument is a string in printf format, followed by variable arguments _..._. + +The _tracefs_marker_vprint()_ function writes a formatted string in the trace marker of the selected +_instance_. The _fmt_ argument is a string in printf format, followed by list _ap_ of arguments. + +The _tracefs_marker_write()_ function writes a binary data in the trace marker of the selected +_instance_. The _data_ points to the data with length _len_, that is going to be written in +the trace marker. + +The _tracefs_marker_close()_ function closes the ftrace marker of the selected instance. + +RETURN VALUE +------------ +The _tracefs_marker_init()_, _tracefs_marker_print()_, _tracefs_marker_vprint()_ and +_tracefs_marker_write()_ functions return 0 if the operation is successful, or -1 in case of an error. + +EXAMPLE +------- +[source,c] +-- +#include <tracefs.h> + +if (tracefs_marker_init(NULL) < 0) { + /* Failed to initialize the library with the ftrace marker of the top trace instance */ +} + +void foo_print(char *format, ...) +{ + va_list ap; + va_start(ap, format); + if (tracefs_marker_vprint(NULL, format, ap) < 0) { + /* Failed to print in the trace marker */ + } + va_end(ap); +} + +void foo_print_string(char *message) +{ + if (tracefs_marker_print(NULL, "Message from user space: %s", message) < 0) { + /* Failed to print in the trace marker */ + } +} + +unsigned int data = 0xdeadbeef; + + if (tracefs_marker_write(NULL, &data, sizeof(data)) < 0) { + /* Failed to write in the trace marker */ + } + + +tracefs_marker_close(); +-- +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)_, +Documentation/trace/ftrace.rst from the Linux kernel tree + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +*Tzvetomir Stoyanov* <[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) 2021 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Signature · 1 line
2.30.2
PATCH 2/2 libtracefs: Unit test for trace marker APIs
In reply to Tzvetomir Stoyanov (VMware)Added unit tests for these APIs:
tracefs_marker_init()
tracefs_marker_print()
tracefs_marker_vprint()
tracefs_marker_write()
tracefs_marker_close()
Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>
--- utest/tracefs-utest.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
utest/tracefs-utest.c +91 -0
@@ -169,6 +169,95 @@ static const char *get_rand_str(void)return str; } +struct marker_find { +int len; +int count; +void *data; +}; + +static int test_marker_callback (struct tep_event *event,struct tep_record *record, +int cpu,void *context) +{ +struct marker_find *walk = context; +struct tep_format_field *field; + +if (!walk) +return -1 ; +if (record->size < walk->len) +return 0 ; + + field =tep_find_field (event,"buf" ); +if (field &&memcmp (walk->data, record->data + field->offset, walk->len) ==0 ) + walk->count++; + +return 0 ; +} + +static bool find_test_marker (struct tracefs_instance *instance,void *data,int len,int expected) +{ +int ret; +struct marker_find walk; + + walk.data = data; + walk.len = len; + walk.count =0 ; + ret =tracefs_iterate_raw_events (test_tep, instance,NULL ,0 , + test_marker_callback, &walk); +CU_TEST (ret ==0 ); + +return walk.count == expected; +} + +static int marker_vprint (struct tracefs_instance *instance,char *fmt, ...) +{ +va_list ap; +int ret; + +va_start (ap, fmt); + ret =tracefs_marker_vprint (instance, fmt, ap); +va_end (ap); + +return ret; +} + +#define MARKERS_WRITE_COUNT 100 +static void test_instance_ftrace_marker (struct tracefs_instance *instance) +{ +const char *string =get_rand_str (); +unsigned int data =0xdeadbeef ; +char *str; +int i; + +CU_TEST (tracefs_marker_init (instance) ==0 ); +tracefs_marker_close (instance); + +for (i =0 ; i <MARKERS_WRITE_COUNT ; i++) { +CU_TEST (tracefs_marker_write (instance, &data,sizeof (data)) ==0 ); + } +CU_TEST (find_test_marker (instance, &data,sizeof (data),MARKERS_WRITE_COUNT )); + +for (i =0 ; i <MARKERS_WRITE_COUNT ; i++) { +CU_TEST (tracefs_marker_print (instance,"Test marker: %s 0x%X" , string, data) ==0 ); + } +asprintf (&str,"Test marker: %s 0x%X" , string, data); +CU_TEST (find_test_marker (instance, str,strlen (str) +1 ,MARKERS_WRITE_COUNT )); +free (str); + +for (i =0 ; i <MARKERS_WRITE_COUNT ; i++) { +CU_TEST (marker_vprint (instance,"Test marker V: %s 0x%X" , string, data) ==0 ); + } +asprintf (&str,"Test marker V: %s 0x%X" , string, data); +CU_TEST (find_test_marker (instance, str,strlen (str) +1 ,MARKERS_WRITE_COUNT )); +free (str); + +tracefs_marker_close (instance); +} + +static void test_ftrace_marker (void ) +{ +test_instance_ftrace_marker (test_instance); +} +static void test_trace_file (void ) {const char *tmp =get_rand_str ();
@@ -1075,4 +1164,6 @@ void test_tracefs_lib(void) test_tracing_options);CU_add_test (suite,"custom system directory" , test_custom_trace_dir); +CU_add_test (suite,"ftrace marker" , + test_ftrace_marker); }
Signature · 1 line
2.30.2