Skip to contentSkip to conversation

Documentation and unit tests for new trace marker APIs

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 3 messages Apr 9, 2021 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover Documentation and unit tests for new trace marker APIs

Documentation and unit tests for new trace marker APIs

v2 changes:
Split the APIs in two groups - printing formated strings and writing binary data

Tzvetomir Stoyanov (VMware) (2):
libtracefs: Document marker APIs
libtracefs: Unit test for trace marker APIs

 Documentation/libtracefs-marker.txt     | 116 ++++++++++++++++++++++++
 Documentation/libtracefs-marker_raw.txt | 102 +++++++++++++++++++++
 Documentation/libtracefs.txt            |  10 ++
 utest/tracefs-utest.c                   | 116 ++++++++++++++++++++++++
 4 files changed, 344 insertions(+)
 create mode 100644 Documentation/libtracefs-marker.txt
 create mode 100644 Documentation/libtracefs-marker_raw.txt
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/2 libtracefs: Document marker APIs

Added man pages describing these functions:
tracefs_print_init();
tracefs_printf();
tracefs_vprintf();
tracefs_print_close();
tracefs_binary_init();
tracefs_binary_write();
tracefs_binary_close();

---
 Documentation/libtracefs-marker.txt     | 116 ++++++++++++++++++++++++
 Documentation/libtracefs-marker_raw.txt | 102 +++++++++++++++++++++
 Documentation/libtracefs.txt            |  10 ++
 3 files changed, 228 insertions(+)
 create mode 100644 Documentation/libtracefs-marker.txt
 create mode 100644 Documentation/libtracefs-marker_raw.txt
Documentation/libtracefs-marker.txt new file +116 -0
new file mode 100644
index 0000000..1905662
@@ -0,0 +1,116 @@
+libtracefs(3)
+=============
+
+NAME
+----
+tracefs_print_init, tracefs_print_close, tracefs_printf, tracefs_vprintf -
+Open, close and write formated strings in the trace buffer.
+
+SYNOPSIS
+--------
+[verse]
+--
+*#include <tracefs.h>*
+
+int *tracefs_print_init*(struct tracefs_instance pass:[*]_instance_);
+int *tracefs_printf*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, _..._);
+int *tracefs_vprintf*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, va_list _ap_);
+void *tracefs_print_close*(struct tracefs_instance pass:[*]_instance_);
+
+--
+
+DESCRIPTION
+-----------
+Set of functions to write formated strings in the trace buffer.
+See Documentation/trace/ftrace.rst from the Linux kernel tree for more information about writing
+data from user space in the trace buffer. All these APIs have _instance_ as a first argument. If
+NULL is passed as _instance_, the top trace instance is used.
+
+The _tracefs_print_init()_ function initializes the library for writing into the trace buffer of
+the selected _instance_. It is not mandatory to call this API before writing strings, any of
+the printf APIs will call it automatically, if the library is not yet initialized. But calling
+_tracefs_print_init()_ in advance will speed up the writing.
+
+The _tracefs_printf()_ function writes a formatted string in the trace buffer of the selected
+_instance_. The _fmt_ argument is a string in printf format, followed by variable arguments _..._.
+
+The _tracefs_vprintf()_ function writes a formatted string in the trace buffer of the selected
+_instance_. The _fmt_ argument is a string in printf format, followed by list _ap_ of arguments.
+
+The _tracefs_print_close()_ function closes the resources, used by the library for writing in
+the trace buffer of the selected instance.
+
+RETURN VALUE
+------------
+The _tracefs_print_init()_, _tracefs_printf()_, and _tracefs_vprintf()_ functions return 0 if
+the operation is successful, or -1 in case of an error.
+
+EXAMPLE
+-------
+[source,c]
+--
+#include <tracefs.h>
+
+if (tracefs_print_init(NULL) < 0) {
+ /* Failed to initialize the library for writing in the trace buffer of the top trace instance */
+}
+
+void foo_print(char *format, ...)
+{
+	va_list ap;
+	va_start(ap, format);
+	if (tracefs_vprintf(NULL, format, ap) < 0) {
+		/* Failed to print in the trace buffer */
+	}
+	va_end(ap);
+}
+
+void foo_print_string(char *message)
+{
+	if (tracefs_printf(NULL, "Message from user space: %s", message) < 0) {
+		/* Failed to print in the trace buffer */
+	}
+}
+
+tracefs_print_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).
Documentation/libtracefs-marker_raw.txt new file +102 -0

102 lines not on this page · raw message

Documentation/libtracefs.txt +10 -0
index cea21fe..77d053d 100644
@@ -51,6 +51,16 @@ Trace helper functions:
 	int _tracefs_trace_on_get_fd_(struct tracefs_instance pass:[*]_instance_);
 	int *tracefs_trace_on_fd*(int _fd_);
 	int *tracefs_trace_off_fd*(int _fd_);
+
+Writing data in the trace buffer:
+	int *tracefs_print_init*(struct tracefs_instance pass:[*]_instance_);
+	int *tracefs_printf*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, _..._);
+	int *tracefs_vprintf*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_fmt_, va_list _ap_);
+	void *tracefs_print_close*(struct tracefs_instance pass:[*]_instance_);
+	int *tracefs_binary_init*(struct tracefs_instance pass:[*]_instance_);
+	int *tracefs_binary_write*(struct tracefs_instance pass:[*]_instance_, void pass:[*]_data_, int _len_);
+	void *tracefs_binary_close*(struct tracefs_instance pass:[*]_instance_);
+
 --
 
 DESCRIPTION
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/2 libtracefs: Unit test for trace marker APIs

In reply to Tzvetomir Stoyanov (VMware)

Added unit tests for thee APIs:
tracefs_print_init();
tracefs_printf();
tracefs_vprintf();
tracefs_print_close();
tracefs_binary_init();
tracefs_binary_write();
tracefs_binary_close();

---
 utest/tracefs-utest.c | 116 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
utest/tracefs-utest.c +116 -0
index f9441af..3f5c832 100644
@@ -192,6 +192,120 @@ static const char *get_rand_str(void)
 	return str;
 }
 
+struct marker_find {
+	int data_offset;
+	int event_id;
+	int count;
+	int len;
+	void *data;
+};
+
+static int test_marker_callback(struct tep_event *event, struct tep_record *record,
+				int cpu, void *context)
+{
+	struct marker_find *walk = context;
+
+	if (!walk)
+		return -1;
+	if (event->id != walk->event_id)
+		return 0;
+	if (record->size < (walk->data_offset + walk->len))
+		return 0;
+
+	if (memcmp(walk->data, record->data + walk->data_offset, walk->len) == 0)
+		walk->count++;
+
+	return 0;
+}
+
+static bool find_test_marker(struct tracefs_instance *instance,
+			     void *data, int len, int expected, bool raw)
+{
+	struct tep_format_field *field;
+	struct tep_event *event;
+	struct marker_find walk;
+	int ret;
+
+	if (raw) {
+		event = tep_find_event_by_name(test_tep, "ftrace", "raw_data");
+		if (event)
+			field = tep_find_field(event, "id");
+
+	} else {
+		event = tep_find_event_by_name(test_tep, "ftrace", "print");
+		if (event)
+			field = tep_find_field(event, "buf");
+	}
+
+	if (!event || !field)
+		return false;
+
+	walk.data = data;
+	walk.len = len;
+	walk.count = 0;
+	walk.event_id = event->id;
+	walk.data_offset = field->offset;
+	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_vprintf(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_print_init(instance) == 0);
+	tracefs_print_close(instance);
+
+	CU_TEST(tracefs_binary_init(instance) == 0);
+	tracefs_binary_close(instance);
+
+	for (i = 0; i < MARKERS_WRITE_COUNT; i++) {
+		CU_TEST(tracefs_binary_write(instance, &data, sizeof(data)) == 0);
+	}
+	CU_TEST(find_test_marker(instance, &data, sizeof(data), MARKERS_WRITE_COUNT, true));
+
+	for (i = 0; i < MARKERS_WRITE_COUNT; i++) {
+		CU_TEST(tracefs_printf(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, false));
+	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, false));
+	free(str);
+
+	tracefs_print_close(instance);
+	tracefs_binary_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();
@@ -1098,4 +1212,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