Skip to contentSkip to conversation

libtracefs: Introduce new API to reset ftrace instance

PATCH In review Tzvetomir Stoyanov (VMware) 2 participants 6 messages Apr 26, 2023 – May 30, 2023 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover libtracefs: Introduce new API to reset ftrace instance

A new API tracefs_instance_reset() for resetting instance to its default state, based on the logic from "trace-cmd reset" command.

v2 changes:
- Check if the kernel is configured with triggers enabled, before calling clear_trigger().
- Clear "stack_trace_filter" file.
- Fixed spelling and coding style errors.

Tzvetomir Stoyanov (VMware) (3):
libtracefs: New API to reset ftrace instance
libtracefs: Documentation for tracefs_instance_reset()
libtracefs: Unit test for tracefs_instance_reset()

 Documentation/libtracefs-instances-manage.txt |   7 +-
 Documentation/libtracefs.txt                  |   1 +
 include/tracefs-local.h                       |   1 +
 include/tracefs.h                             |   1 +
 src/tracefs-instance.c                        | 207 ++++++++++++++++++
 src/tracefs-utils.c                           |  20 ++
 utest/tracefs-utest.c                         | 183 ++++++++++++++++
 7 files changed, 418 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 1/3 libtracefs: New API to reset ftrace instance

Resetting a ftrace instance to its default state is not a trivial task. A lot of trace files have to be modified, with different syntaxes and in strict order. Although there is such functionality in "trace-cmd reset" command, it will be good to have it in the tracefs library as well.
A new API tracefs_instance_reset() is introduced, which resets given ftrace instance to its default state. The logic and most of the helper functions from "trace-cmd reset" command are copied in the tracefs library.

---
 include/tracefs-local.h |   1 +
 include/tracefs.h       |   1 +
 src/tracefs-instance.c  | 207 ++++++++++++++++++++++++++++++++++++++++
 src/tracefs-utils.c     |  20 ++++
 4 files changed, 229 insertions(+)
include/tracefs-local.h +1 -0
index 2007d26..da99a30 100644
@@ -64,6 +64,7 @@ int trace_get_instance(struct tracefs_instance *instance);
 /* Can be overridden */
 void tracefs_warning(const char *fmt, ...);
 
+char *strstrip(char *str);
 int str_read_file(const char *file, char **buffer, bool warn);
 char *trace_append_file(const char *dir, const char *name);
 char *trace_find_tracing_dir(bool debugfs);
include/tracefs.h +1 -0
index 3547b5a..5e9d84b 100644
@@ -23,6 +23,7 @@ int tracefs_tracing_dir_is_mounted(bool mount, const char **path);
 struct tracefs_instance;
 
 void tracefs_instance_free(struct tracefs_instance *instance);
+void tracefs_instance_reset(struct tracefs_instance *instance);
 struct tracefs_instance *tracefs_instance_create(const char *name);
 struct tracefs_instance *tracefs_instance_alloc(const char *tracing_dir,
 						const char *name);
src/tracefs-instance.c +207 -0

210 lines not on this page · raw message

src/tracefs-utils.c +20 -0
index 9acf2ad..ef90677 100644
@@ -319,6 +319,26 @@ void tracefs_put_tracing_file(char *name)
 	free(name);
 }
 
+/* The function is copied from trace-cmd */
+__hidden char *strstrip(char *str)
+{
+	char *s;
+
+	if (!str)
+		return NULL;
+
+	s = str + strlen(str) - 1;
+	while (s >= str && isspace(*s))
+		s--;
+	s++;
+	*s = '\0';
+
+	for (s = str; *s && isspace(*s); s++)
+		;
+
+	return s;
+}
+
 __hidden int str_read_file(const char *file, char **buffer, bool warn)
 {
 	char stbuf[BUFSIZ];
Signature · 1 line
2.39.2
#2 lore raw

PATCH v2 2/3 libtracefs: Documentation for tracefs_instance_reset()

In reply to Tzvetomir Stoyanov (VMware)

The newly introduced API tracefs_instance_reset() should be described in the tracefs man pages.

---
 Documentation/libtracefs-instances-manage.txt | 7 +++++--
 Documentation/libtracefs.txt                  | 1 +
 2 files changed, 6 insertions(+), 2 deletions(-)
Documentation/libtracefs-instances-manage.txt +5 -2
index c03a272..1e0735e 100644
@@ -4,7 +4,7 @@ libtracefs(3)
 NAME
 ----
 tracefs_instance_create, tracefs_instance_destroy, tracefs_instance_alloc, tracefs_instance_free,
-tracefs_instance_is_new, tracefs_instances - Manage trace instances.
+tracefs_instance_is_new, tracefs_instances, tracefs_instance_reset - Manage trace instances.
 
 SYNOPSIS
 --------
@@ -18,6 +18,7 @@ struct tracefs_instance pass:[*]*tracefs_instance_alloc*(const char pass:[*]_tra
 void *tracefs_instance_free*(struct tracefs_instance pass:[*]_instance_);
 bool *tracefs_instance_is_new*(struct tracefs_instance pass:[*]_instance_);
 char pass:[**]*tracefs_instances*(const char pass:[*]_regex_);
+void *tracefs_instance_reset*(struct tracefs_instance pass:[*]_instance_);
 
 --
 
@@ -60,6 +61,8 @@ it will match all instances that exist. The returned list must be freed with
 *tracefs_list_free*(3). Note, if no instances are found an empty list is returned
 and that too needs to be free with *tracefs_list_free*(3).
 
+The *tracefs_instance_reset*() function resets the given _instance_ to its default state.
+
 RETURN VALUE
 ------------
 The *tracefs_instance_create()* and *tracefs_instance_alloc()* functions return a pointer to
@@ -106,7 +109,7 @@ struct tracefs_instance *inst = tracefs_instance_alloc(NULL, "bar");
 	}
 
 	...
-
+	tracefs_instance_reset(inst);
 	tracefs_instance_free(inst);
 --
 FILES
Documentation/libtracefs.txt +1 -0
index c3f448d..052e1c9 100644
@@ -25,6 +25,7 @@ Trace instances:
 	struct tracefs_instance pass:[*]*tracefs_instance_alloc*(const char pass:[*]_tracing_dir_, const char pass:[*]_name_);
 	void *tracefs_instance_free*(struct tracefs_instance pass:[*]_instance_);
 	char pass:[**]*tracefs_instances*(const char pass:[*]_regex_);
+	void *tracefs_instance_reset*(struct tracefs_instance pass:[*]_instance_);
 	bool *tracefs_instance_is_new*(struct tracefs_instance pass:[*]_instance_);
 	bool *tracefs_file_exists*(struct tracefs_instance pass:[*]_instance_, char pass:[*]_name_);
 	bool *tracefs_dir_exists*(struct tracefs_instance pass:[*]_instance_, char pass:[*]_name_);
Signature · 1 line
2.39.2
#3 lore raw

PATCH v2 3/3 libtracefs: Unit test for tracefs_instance_reset()

In reply to Tzvetomir Stoyanov (VMware)

The logic of the tracefs_instance_reset() is complex and should be covered by the unit tests of the tracefs library.

---
 utest/tracefs-utest.c | 183 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 183 insertions(+)
utest/tracefs-utest.c +183 -0
index e0e3c07..47f65cb 100644
@@ -1618,6 +1618,187 @@ static void test_instance_file(void)
 	free(inst_dir);
 }
 
+static bool test_check_file_content(struct tracefs_instance *instance, char *file,
+				    char *content, bool full_match, bool ignore_comments)
+{
+	char *save = NULL;
+	char *buf, *line;
+	bool ret = false;
+	int len;
+
+	if (!tracefs_file_exists(instance, file))
+		return false;
+
+	buf = tracefs_instance_file_read(instance, file, NULL);
+	if (strlen(content) == 0) {
+		/* check for empty file */
+		if (!buf)
+			return true;
+		if (!ignore_comments) {
+			if (strlen(buf) > 0)
+				goto out;
+		} else {
+			line = strtok_r(buf, "\n", &save);
+			while (line) {
+				if (line[0] != '#')
+					goto out;
+				line = strtok_r(NULL, "\n", &save);
+			}
+		}
+	} else {
+		if (!buf || strlen(buf) < 1)
+			return false;
+		if (full_match) {
+			/* strip the newline */
+			len = strlen(buf) - 1;
+			while (buf[len] == '\n' || buf[len] == '\r') {
+				buf[len] = '\0';
+				len = strlen(buf) - 1;
+				if (len < 0)
+					goto out;
+			}
+			if (strcmp(buf, content))
+				goto out;
+		} else {
+			if (!strstr(buf, content))
+				goto out;
+		}
+	}
+
+	ret = true;
+out:
+	free(buf);
+	return ret;
+}
+
+static bool test_check_event_file_content(struct tracefs_instance *instance,
+					  char *system, char *event, char *file,
+					  char *content, bool full_match, bool ignore_comments)
+{
+	char *efile;
+	int ret;
+
+	ret = asprintf(&efile, "events/%s/%s/%s", system, event, file);
+	if (ret <= 0)
+		return false;
+	ret = test_check_file_content(instance, efile, content, full_match, ignore_comments);
+	free(efile);
+	return ret;
+}
+
+static bool check_cpu_mask(struct tracefs_instance *instance)
+{
+	int cpus = sysconf(_SC_NPROCESSORS_CONF);
+	int fullwords = (cpus - 1) / 32;
+	int bits = (cpus - 1) % 32 + 1;
+	int len = (fullwords + 1) * 9;
+	char buf[len + 1];
+
+	buf[0] = '\0';
+	sprintf(buf, "%x", (unsigned int)((1ULL << bits) - 1));
+	while (fullwords-- > 0)
+		strcat(buf, ",ffffffff");
+
+	return test_check_file_content(instance, "tracing_cpumask", buf, true, false);
+}
+
+static bool test_instance_check_default_state(struct tracefs_instance *instance)
+{
+	char **systems;
+	char **events;
+	int i, j;
+	int ok;
+
+	if (tracefs_trace_is_on(instance) != 1)
+		return false;
+	if (!test_check_file_content(instance, "current_tracer", "nop", true, false))
+		return false;
+	if (!test_check_file_content(instance, "events/enable", "0", true, false))
+		return false;
+	if (!test_check_file_content(instance, "set_ftrace_pid", "no pid", true, false))
+		return false;
+	if (!test_check_file_content(instance, "trace", "", true, true))
+		return false;
+	if (!test_check_file_content(instance, "error_log", "", true, false))
+		return false;
+	if (!test_check_file_content(instance, "trace_clock", "[local]", false, false))
+		return false;
+	if (!test_check_file_content(instance, "set_event_pid", "", true, false))
+		return false;
+	if (!test_check_file_content(instance, "tracing_max_latency", "0", true, false))
+		return false;
+	if (!test_check_file_content(instance, "set_ftrace_filter", "", true, true))
+		return false;
+	if (!test_check_file_content(instance, "set_ftrace_notrace", "", true, true))
+		return false;
+	if (!check_cpu_mask(instance))
+		return false;
+
+	ok = 1;
+	systems = tracefs_event_systems(NULL);
+	if (systems) {
+		for (i = 0; systems[i]; i++) {
+			events = tracefs_system_events(NULL, systems[i]);
+			if (!events)
+				continue;
+			for (j = 0; events[j]; j++) {
+				if (!test_check_event_file_content(instance, systems[i], events[j],
+								    "enable", "0", true, false))
+					break;
+				if (!test_check_event_file_content(instance, systems[i], events[j],
+								    "filter", "none", true, false))
+					break;
+				if (!test_check_event_file_content(instance, systems[i], events[j],
+								    "trigger", "", true, true))
+					break;
+			}
+			if (events[j])
+				ok = 0;
+			tracefs_list_free(events);
+			if (!ok)
+				return false;
+		}
+		tracefs_list_free(systems);
+	}
+
+	return true;
+}
+
+static void test_instance_reset(void)
+{
+	struct tracefs_instance *instance = NULL;
+	const char *name = get_rand_str();
+
+	CU_TEST(tracefs_instance_exists(name) == false);
+	instance = tracefs_instance_create(name);
+	CU_TEST(instance != NULL);
+
+	CU_TEST(test_instance_check_default_state(instance) == true);
+
+	CU_TEST(tracefs_tracer_set(instance, TRACEFS_TRACER_HWLAT) == 0);
+	CU_TEST(tracefs_event_enable(instance, "bridge", "fdb_delete") == 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_pid", "5") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "trace_clock", "global") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_event_pid", "5") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_filter",
+						      "schedule:stacktrace") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_notrace",
+						      "schedule:stacktrace") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "tracing_cpumask", "0f") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "events/syscalls/sys_exit_read/trigger",
+						      "enable_event:kmem:kmalloc:1") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "events/bridge/fdb_delete/filter",
+						      "common_pid == 5") > 0);
+
+	CU_TEST(test_instance_check_default_state(instance) == false);
+
+	tracefs_instance_reset(instance);
+	CU_TEST(test_instance_check_default_state(instance) == true);
+
+	CU_TEST(tracefs_instance_destroy(instance) == 0);
+	tracefs_instance_free(instance);
+}
+
 static bool check_fd_name(int fd, const char *dir, const char *name)
 {
 	char link[PATH_MAX + 1];
@@ -2354,6 +2535,8 @@ void test_tracefs_lib(void)
 		    test_file_fd);
 	CU_add_test(suite, "instance file descriptor",
 		    test_instance_file);
+	CU_add_test(suite, "instance reset",
+		    test_instance_reset);
 	CU_add_test(suite, "systems and events APIs",
 		    test_system_event);
 	CU_add_test(suite, "tracefs_iterate_raw_events API",
Signature · 1 line
2.39.2
#4 lore raw

Re: [PATCH v2 3/3] libtracefs: Unit test for tracefs_instance_reset()

utest/tracefs-utest.c static void test_instance_file(void) · line 1779

Quoted 13 lines · On Wed, 26 Apr 2023 07:11:24 +0300 "Tzvetomir Stoyanov (VMware)"…
utest/tracefs-utest.c +13 -0
+static void test_instance_reset(void)
+{
+	struct tracefs_instance *instance = NULL;
+	const char *name = get_rand_str();
+
+	CU_TEST(tracefs_instance_exists(name) == false);
+	instance = tracefs_instance_create(name);
+	CU_TEST(instance != NULL);
+
+	CU_TEST(test_instance_check_default_state(instance) == true);
+
+	CU_TEST(tracefs_tracer_set(instance, TRACEFS_TRACER_HWLAT) == 0);
+	CU_TEST(tracefs_event_enable(instance, "bridge", "fdb_delete") == 0);

Was there a reason why you picked HWLAT tracer and the fdb_delete trace event? Neither exist in my test machine setup, so this test automatically fails.

-- Steve

Quoted 22 lines
utest/tracefs-utest.c +22 -0
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_pid", "5") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "trace_clock", "global") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_event_pid", "5") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_filter",
+						      "schedule:stacktrace") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_notrace",
+						      "schedule:stacktrace") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "tracing_cpumask", "0f") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "events/syscalls/sys_exit_read/trigger",
+						      "enable_event:kmem:kmalloc:1") > 0);
+	CU_TEST(tracefs_instance_file_write(instance, "events/bridge/fdb_delete/filter",
+						      "common_pid == 5") > 0);
+
+	CU_TEST(test_instance_check_default_state(instance) == false);
+
+	tracefs_instance_reset(instance);
+	CU_TEST(test_instance_check_default_state(instance) == true);
+
+	CU_TEST(tracefs_instance_destroy(instance) == 0);
+	tracefs_instance_free(instance);
+}
+
#5 lore raw

Re: [PATCH v2 3/3] libtracefs: Unit test for tracefs_instance_reset()

Quoted 13 lines · Steven Rostedt
Quoted 9 lines · On Wed, 26 Apr 2023 07:11:24 +0300 "Tzvetomir Stoyanov (VMware)"…
utest/tracefs-utest.c +9 -0
+static void test_instance_reset(void)
+{
+     struct tracefs_instance *instance = NULL;
+     const char *name = get_rand_str();
+
+     CU_TEST(tracefs_instance_exists(name) == false);
+     instance = tracefs_instance_create(name);
+     CU_TEST(instance != NULL);
+
Quoted 9 lines

utest/tracefs-utest.c static void test_instance_file(void) · line 1779

Quoted 4 lines
utest/tracefs-utest.c +4 -0
+     CU_TEST(test_instance_check_default_state(instance) == true);
+
+     CU_TEST(tracefs_tracer_set(instance, TRACEFS_TRACER_HWLAT) == 0);
+     CU_TEST(tracefs_event_enable(instance, "bridge", "fdb_delete") == 0);

Was there a reason why you picked HWLAT tracer and the fdb_delete trace event? Neither exist in my test machine setup, so this test automatically fails.

Hi Steve,
The only reason to choose them is not to generate many events during the test, as the test checks the ftrace configuration and not the generated events. I didn't realize that the tracer and events may not exist in the system. I noticed your commit, thanks for fixing that!

Quoted 25 lines

-- Steve

Quoted 22 lines
utest/tracefs-utest.c +22 -0
+     CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_pid", "5") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "trace_clock", "global") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "set_event_pid", "5") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_filter",
+                                                   "schedule:stacktrace") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "set_ftrace_notrace",
+                                                   "schedule:stacktrace") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "tracing_cpumask", "0f") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "events/syscalls/sys_exit_read/trigger",
+                                                   "enable_event:kmem:kmalloc:1") > 0);
+     CU_TEST(tracefs_instance_file_write(instance, "events/bridge/fdb_delete/filter",
+                                                   "common_pid == 5") > 0);
+
+     CU_TEST(test_instance_check_default_state(instance) == false);
+
+     tracefs_instance_reset(instance);
+     CU_TEST(test_instance_check_default_state(instance) == true);
+
+     CU_TEST(tracefs_instance_destroy(instance) == 0);
+     tracefs_instance_free(instance);
+}
+
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center