Skip to contentSkip to conversation

Add some helpful APIS

PATCH New Steven Rostedt 1 participant 5 messages Nov 14, 2022 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover Add some helpful APIS

From: "Steven Rostedt (Google)" <[email protected]>

Add tracefs_event_is_enabled() and tracefs_tracing_dir_is mounted().

Also add getting and setting the ring buffer size.

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Add tracefs_instance_set_buffer_size() prototype in tracefs.h

- Added tracefs_tracing_dir_is_mounted() API.

Steven Rostedt (Google) (4):
libtracefs: Add tracefs_event_is_enabled() API
libtracefs: Add tracefs_instance_get_buffer_size() API
libtracefs: Add tracefs_instance_set_buffer_size() API
libtracefs: Add tracefs_tracing_dir_is_mounted() API

 Documentation/libtracefs-events.txt          |  33 ++++-
 Documentation/libtracefs-files.txt           |  14 +-
 Documentation/libtracefs-instances-utils.txt |  22 +++-
 Documentation/libtracefs.txt                 |   5 +
 include/tracefs.h                            |  12 ++
 src/tracefs-events.c                         | 128 +++++++++++++++++--
 src/tracefs-instance.c                       |  64 ++++++++++
 src/tracefs-utils.c                          |  62 +++++++--
 8 files changed, 312 insertions(+), 28 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/4 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
index f998c79b04ec..d5bd779273e2 100644
@@ -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
index 0081210f8951..e02974004ef8 100644
@@ -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
index 9f0bdc62836a..f2524b07501d 100644
@@ -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
#2 lore raw

PATCH v2 2/4 libtracefs: Add tracefs_instance_get_buffer_size() API

In reply to Steven Rostedt

From: "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
index a79cc3447d80..9bec8a9163cd 100644
@@ -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
index e02974004ef8..3b485a018ed4 100644
@@ -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
index f2524b07501d..3391debccc80 100644
@@ -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
index 249a5c9a3abe..206f8c99cf36 100644
@@ -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
#3 lore raw

PATCH v2 3/4 libtracefs: Add tracefs_instance_set_buffer_size() API

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add functionality that sets the ring buffer size.

---
 Documentation/libtracefs-instances-utils.txt | 10 +++++++-
 Documentation/libtracefs.txt                 |  1 +
 include/tracefs.h                            |  1 +
 src/tracefs-instance.c                       | 27 ++++++++++++++++++++
 4 files changed, 38 insertions(+), 1 deletion(-)
Documentation/libtracefs-instances-utils.txt +9 -1
index 9bec8a9163cd..bc8c9a7bf8e9 100644
@@ -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
index 3b485a018ed4..73c4997efeef 100644
@@ -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_);
include/tracefs.h +1 -0
index 3391debccc80..5ff0c6f134d3 100644
@@ -56,6 +56,7 @@ 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);
+int tracefs_instance_set_buffer_size(struct tracefs_instance *instance, size_t size, int cpu);
 char **tracefs_instances(const char *regex);
 
 bool tracefs_instance_exists(const char *name);
src/tracefs-instance.c +27 -0
index 206f8c99cf36..6905f61ff929 100644
@@ -400,6 +400,33 @@ ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, int
 	return 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
#4 lore raw

PATCH v2 4/4 libtracefs: Add tracefs_tracing_dir_is_mounted() API

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add the tracefs_tracing_dir_is_mounted() API that passes in a mount flag and an address to a char pointer (or NULL). It returns 1 if the tracing_dir was already mounted and 0 if not. If the mount flag is set, then it will try to mount it and return -1 if it fails. If path points to the address of a char*, then it will be set to the path that was mounted, or was already mounted.

This is useful for wanting to unmount the tracing_dir if the tool was the one that mounted it.

---
 Documentation/libtracefs-files.txt | 14 ++++++-
 Documentation/libtracefs.txt       |  1 +
 include/tracefs.h                  |  1 +
 src/tracefs-utils.c                | 62 ++++++++++++++++++++++++------
 4 files changed, 64 insertions(+), 14 deletions(-)
Documentation/libtracefs-files.txt +12 -2
index 2a3f5449839d..d22e75900098 100644
@@ -3,8 +3,8 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir -
-Find and set locations of trace directory and files.
+tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir,
+tracefs_tracing_dir_is_mounted - Find and set locations of trace directory and files.
 
 SYNOPSIS
 --------
@@ -17,6 +17,7 @@ void *tracefs_put_tracing_file*(char pass:[*]_name_);
 const char pass:[*]*tracefs_tracing_dir*(void);
 const char pass:[*]*tracefs_debug_dir*(void);
 int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_)
+int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_);
 --
 
 DESCRIPTION
@@ -52,6 +53,12 @@ that it will return where the debugfs file system is mounted. If it
 is not mounted it will try to mount it. The return string must _not_
 be freed.
 
+*tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory is
+already mounted and 0 if it is not. If _mount_ is true, it will try to
+mount it if it is not, and returns 0 if it succesfully mounted it and -1
+if it did not. If _path_ is set, it will be assigned to the path where it
+mounted it. _path_ is internal and should not be freed.
+
 RETURN VALUE
 ------------
 The *tracefs_set_tracing_dir()* function returns 0 on success, -1 otherwise.
@@ -65,6 +72,9 @@ in case of an error. The returned string must _not_ be freed.
 The *tracefs_debug_dir()* function returns a constant string or NULL
 in case of an error. The returned string must _not_ be freed.
 
+The *tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory
+is already mounted, 0 if it is not, and -1 on error.
+
 EXAMPLE
 -------
 [source,c]
Documentation/libtracefs.txt +1 -0
index 73c4997efeef..cc53388438e5 100644
@@ -17,6 +17,7 @@ Locations of tracing files and directories:
 	const char pass:[*]*tracefs_tracing_dir*(void);
 	const char pass:[*]*tracefs_debug_dir*(void);
 	int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_)
+	int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_);
 
 Trace instances:
 	struct tracefs_instance pass:[*]*tracefs_instance_create*(const char pass:[*]_name_);
include/tracefs.h +1 -0
index 5ff0c6f134d3..10f84a25c31e 100644
@@ -17,6 +17,7 @@ void tracefs_put_tracing_file(char *name);
 const char *tracefs_tracing_dir(void);
 const char *tracefs_debug_dir(void);
 int tracefs_set_tracing_dir(char *tracing_dir);
+int tracefs_tracing_dir_is_mounted(bool mount, const char **path);
 
 /* ftrace instances */
 struct tracefs_instance;
src/tracefs-utils.c +50 -12

88 lines not on this page · raw message

Signature · 1 line
2.35.1