Skip to contentSkip to conversation

libtracefs fixes and improvements

PATCH Superseded Tzvetomir Stoyanov (VMware) 2 participants 11 messages Nov 10, 2020 – Nov 12, 2020

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover libtracefs fixes and improvements

A few final tracefs library fixes and improvements, before releasing it as an official library.

v2 changes:
- Change tracefs_instance_create() to return pointer to allocated trace instance structure
- Add new API: tracefs_instance_is_new() to check if the trace instance is newly created by the library.

Tzvetomir Stoyanov (VMware) (6):
trace-cmd: Change tracefs.h include path
libtracefs: Change APIs to work with constant strings
libtracefs: Add new API to check if instance exists
libtracefs: Combine allocate and create APIs into one
libtracefs: Add new tracefs API tracefs_instances_walk()
trace-cmd: Add new libtrasefs API to get the current trace clock

 include/trace-cmd/trace-cmd.h       |   1 +
 include/tracefs/tracefs.h           |  18 +--
 lib/trace-cmd/trace-timesync.c      |   5 +-
 lib/tracefs/include/tracefs-local.h |   1 +
 lib/tracefs/tracefs-events.c        |  61 +++++-----
 lib/tracefs/tracefs-instance.c      | 172 +++++++++++++++++++++++++---
 tracecmd/include/trace-local.h      |   4 +-
 tracecmd/trace-record.c             |  72 ++++++------
 tracecmd/trace-show.c               |   2 +-
 tracecmd/trace-stat.c               |  83 ++++----------
 utest/tracefs-utest.c               | 127 ++++++++++++++++----
 11 files changed, 357 insertions(+), 189 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/6 trace-cmd: Change tracefs.h include path

The tracefs.h file is installed in
<install_prefix>/include/tracefs/tracefs.h.
However, in trace-cmd.h it is inluded just as "tracefs.h". When trace-cmd.h is used in trace-cmd build context that include is not a problem, as all local header paths are described in the Makefiles. But when other application uses trace-cmd.h and tracefs.h, installed in the system, the compilation fails due to this broken include.

---
 include/trace-cmd/trace-cmd.h | 1 +
 1 file changed, 1 insertion(+)
include/trace-cmd/trace-cmd.h +1 -0
index f3c95f30..3c2b4745 100644
@@ -7,6 +7,7 @@
 #define _TRACE_CMD_H
 
 #include "traceevent/event-parse.h"
+#include "tracefs/tracefs.h"
 
 #define TRACECMD_MAGIC { 23, 8, 68 }
 
Signature · 1 line
2.28.0
#2 lore raw

PATCH v2 2/6 libtracefs: Change APIs to work with constant strings

In reply to Tzvetomir Stoyanov (VMware)

Some tracefs library APIs retrieve trace instance parameters as srings. As these strings are not meant to be changed by the API callers, set them to be constant. These APIs are affected:
tracefs_instance_get_name()
tracefs_event_systems()
tracefs_system_events()
tracefs_tracers()
tracefs_list_free()

---
 include/tracefs/tracefs.h      | 10 ++++----
 lib/tracefs/tracefs-events.c   | 47 ++++++++++++++--------------------
 lib/tracefs/tracefs-instance.c |  4 +--
 tracecmd/trace-record.c        |  2 +-
 utest/tracefs-utest.c          | 23 +++++++++--------
 5 files changed, 39 insertions(+), 47 deletions(-)
include/tracefs/tracefs.h +5 -5
index 8ee7ba6e..8d193853 100644
@@ -24,7 +24,7 @@ struct tracefs_instance *tracefs_instance_alloc(const char *name);
 void tracefs_instance_free(struct tracefs_instance *instance);
 int tracefs_instance_create(struct tracefs_instance *instance);
 int tracefs_instance_destroy(struct tracefs_instance *instance);
-char *tracefs_instance_get_name(struct tracefs_instance *instance);
+const char *tracefs_instance_get_name(struct tracefs_instance *instance);
 char *
 tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
 char *tracefs_instance_get_dir(struct tracefs_instance *instance);
@@ -37,9 +37,9 @@ bool tracefs_file_exists(struct tracefs_instance *instance, char *name);
 bool tracefs_dir_exists(struct tracefs_instance *instance, char *name);
 
 /* events */
-void tracefs_list_free(char **list);
-char **tracefs_event_systems(const char *tracing_dir);
-char **tracefs_system_events(const char *tracing_dir, const char *system);
+void tracefs_list_free(const char **list);
+const char **tracefs_event_systems(const char *tracing_dir);
+const char **tracefs_system_events(const char *tracing_dir, const char *system);
 int tracefs_iterate_raw_events(struct tep_handle *tep,
 				struct tracefs_instance *instance,
 				int (*callback)(struct tep_event *,
@@ -47,7 +47,7 @@ int tracefs_iterate_raw_events(struct tep_handle *tep,
 						int, void *),
 				void *callback_context);
 
-char **tracefs_tracers(const char *tracing_dir);
+const char **tracefs_tracers(const char *tracing_dir);
 
 struct tep_handle *tracefs_local_events(const char *tracing_dir);
 struct tep_handle *tracefs_local_events_system(const char *tracing_dir,
lib/tracefs/tracefs-events.c +19 -28

122 lines not on this page · raw message

lib/tracefs/tracefs-instance.c +2 -2
index 50e88534..e9ec3401 100644
@@ -169,7 +169,7 @@ char *tracefs_instance_get_dir(struct tracefs_instance *instance)
  * Returns the name of the given @instance.
  * The returned string must *not* be freed.
  */
-char *tracefs_instance_get_name(struct tracefs_instance *instance)
+const char *tracefs_instance_get_name(struct tracefs_instance *instance)
 {
 	if (instance)
 		return instance->name;
@@ -252,8 +252,8 @@ static bool check_file_exists(struct tracefs_instance *instance,
 			     char *name, bool dir)
 {
 	char file[PATH_MAX];
-	struct stat st;
 	char *path;
+	struct stat st;
 	int ret;
 
 	path = tracefs_instance_get_dir(instance);
tracecmd/trace-record.c +1 -1
index 72a5c8c9..8cd44dd0 100644
@@ -3861,7 +3861,7 @@ static void connect_to_agent(struct buffer_instance *instance)
 	unsigned int *ports;
 	int i, *fds = NULL;
 	bool use_fifos = false;
-	char *name;
+	const char *name;
 
 	name = tracefs_instance_get_name(instance->tracefs);
 	if (!no_fifos) {
utest/tracefs-utest.c +12 -11
index 1c146576..31a700ff 100644
@@ -180,7 +180,7 @@ static void test_instance_file(void)
 {
 	struct tracefs_instance *instance = NULL;
 	const char *name = get_rand_str();
-	char *inst_name = NULL;
+	const char *inst_name = NULL;
 	const char *tdir;
 	char *inst_file;
 	char *inst_dir;
@@ -308,9 +308,9 @@ static void test_check_files(const char *fdir, char **files)
 
 static void test_system_event(void)
 {
+	const char **systems;
+	const char **events;
 	const char *tdir;
-	char **systems;
-	char **events;
 	char *sdir = NULL;
 
 	tdir  = tracefs_get_tracing_dir();
@@ -324,13 +324,13 @@ static void test_system_event(void)
 
 	asprintf(&sdir, "%s/events/%s", tdir, systems[0]);
 	CU_TEST(sdir != NULL);
-	test_check_files(sdir, events);
+	test_check_files(sdir, (char **)events);
 	free(sdir);
 	sdir = NULL;
 
 	asprintf(&sdir, "%s/events", tdir);
 	CU_TEST(sdir != NULL);
-	test_check_files(sdir, systems);
+	test_check_files(sdir, (char **)systems);
 
 	tracefs_list_free(systems);
 	tracefs_list_free(events);
@@ -340,8 +340,8 @@ static void test_system_event(void)
 
 static void test_tracers(void)
 {
+	const char **tracers;
 	const char *tdir;
-	char **tracers;
 	char *tfile;
 	char *tracer;
 	int i;
@@ -356,7 +356,7 @@ static void test_tracers(void)
 
 	tracer = strtok(tfile, " ");
 	while (tracer) {
-		exclude_string(tracers, tracer);
+		exclude_string((char **)tracers, tracer);
 		tracer = strtok(NULL, " ");
 	}
 
@@ -367,7 +367,8 @@ static void test_tracers(void)
 	free(tfile);
 }
 
-static void test_check_events(struct tep_handle *tep, char *system, bool exist)
+static void test_check_events(struct tep_handle *tep,
+			      const char *system, bool exist)
 {
 	struct dirent *dent;
 	char file[PATH_MAX];
@@ -409,8 +410,8 @@ static void test_check_events(struct tep_handle *tep, char *system, bool exist)
 static void test_local_events(void)
 {
 	struct tep_handle *tep;
+	const char **systems;
 	const char *tdir;
-	char **systems;
 	char *lsystems[3];
 	int i;
 
@@ -430,9 +431,9 @@ static void test_local_events(void)
 	memset(lsystems, 0, sizeof(lsystems));
 	for (i = 0; systems[i]; i++) {
 		if (!lsystems[0])
-			lsystems[0] = systems[i];
+			lsystems[0] = (char *)systems[i];
 		else if (!lsystems[2])
-			lsystems[2] = systems[i];
+			lsystems[2] = (char *)systems[i];
 		else
 			break;
 	}
Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH v2 2/6] libtracefs: Change APIs to work with constant strings

Quoted 5 lines · On Tue, 10 Nov 2020 14:22:45 +0200 "Tzvetomir Stoyanov (VMware)"…

Some tracefs library APIs retrieve trace instance parameters as srings. As these strings are not meant to be changed by the API callers, set them to be constant. These APIs are affected:

tracefs_instance_get_name()

Correct, because we pass back a pointer to a stored internal name.

Quoted 4 lines

tracefs_event_systems()
tracefs_system_events()
tracefs_tracers()
tracefs_list_free()

I don't think we need to make the above const char*. We are allocating them and these strings don't point to anything internal. In other words, they are OK to modify without causing issues.

I think we only need the tracefs_instance_get_name() to be const.

Previously, I thought we were passing back internal pointers, but if we are making strings with strdup, then we don't need to use const char *.

-- Steve

#4 lore raw

PATCH v2 3/6 libtracefs: Add new API to check if instance exists

In reply to Tzvetomir Stoyanov (VMware)

A new API is implemented:
tracefs_instance_exists()
which can be used to check if ftrace instance with given name exists in the system.

---
 include/tracefs/tracefs.h      |  1 +
 lib/tracefs/tracefs-instance.c | 22 +++++++++++++++++++++-
 utest/tracefs-utest.c          |  2 ++
 3 files changed, 24 insertions(+), 1 deletion(-)
include/tracefs/tracefs.h +1 -0
index 8d193853..e5bf3df4 100644
@@ -33,6 +33,7 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
 char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 char *file, int *psize);
 
+bool tracefs_instance_exists(const char *name);
 bool tracefs_file_exists(struct tracefs_instance *instance, char *name);
 bool tracefs_dir_exists(struct tracefs_instance *instance, char *name);
 
lib/tracefs/tracefs-instance.c +21 -1
index e9ec3401..c19dd3b4 100644
@@ -257,7 +257,10 @@ static bool check_file_exists(struct tracefs_instance *instance,
 	int ret;
 
 	path = tracefs_instance_get_dir(instance);
-	snprintf(file, PATH_MAX, "%s/%s", path, name);
+	if (name)
+		snprintf(file, PATH_MAX, "%s/%s", path, name);
+	else
+		snprintf(file, PATH_MAX, "%s", path);
 	tracefs_put_tracing_file(path);
 	ret = stat(file, &st);
 	if (ret < 0)
@@ -266,6 +269,23 @@ static bool check_file_exists(struct tracefs_instance *instance,
 	return !dir == !S_ISDIR(st.st_mode);
 }
 
+/**
+ * tracefs_instance_exists - Check an instance with given name exists
+ * @name: name of the instance
+ *
+ * Returns true if the instance exists, false otherwise
+ *
+ */
+bool tracefs_instance_exists(const char *name)
+{
+	char file[PATH_MAX];
+
+	if (!name)
+		return false;
+	snprintf(file, PATH_MAX, "instances/%s", name);
+	return check_file_exists(NULL, file, true);
+}
+
 /**
  * tracefs_file_exists - Check if a file with given name exists in given instance
  * @instance: ftrace instance, can be NULL for the top instance
utest/tracefs-utest.c +2 -0
index 31a700ff..292bcab0 100644
@@ -197,6 +197,7 @@ static void test_instance_file(void)
 	CU_TEST(asprintf(&inst_dir, "%s/instances/%s", tdir, name) > 0);
 	CU_TEST(stat(inst_dir, &st) != 0);
 
+	CU_TEST(tracefs_instance_exists(name) == false);
 	instance = tracefs_instance_alloc(name);
 	CU_TEST(instance != NULL);
 	CU_TEST(stat(inst_dir, &st) != 0);
@@ -205,6 +206,7 @@ static void test_instance_file(void)
 	CU_TEST(strcmp(inst_name, name) == 0);
 
 	CU_TEST(tracefs_instance_create(instance) == 0);
+	CU_TEST(tracefs_instance_exists(name) == true);
 	CU_TEST(stat(inst_dir, &st) == 0);
 	CU_TEST(S_ISDIR(st.st_mode));
 
Signature · 1 line
2.28.0
#5 lore raw

PATCH v2 4/6 libtracefs: Combine allocate and create APIs into one

In reply to Tzvetomir Stoyanov (VMware)

Having two APIs for allocating and creating trace instance can be confusing. The tracefs_instance_alloc() is removed and its logic is moved to tracefs_instance_create() API. This single API first creates the instance in the system (if it does not exist) and then allocates and initializes tracefs_instance structure. Trace-cmd code and unit tests are modified to use the new API.
A new API is introduced, to check if the tracefs instance is newly created by the library or if it already exist.
tracefs_instance_is_new()

---
 include/tracefs/tracefs.h      |  4 +-
 lib/trace-cmd/trace-timesync.c |  5 +--
 lib/tracefs/tracefs-instance.c | 59 +++++++++++++++++++++-------
 tracecmd/include/trace-local.h |  4 +-
 tracecmd/trace-record.c        | 70 ++++++++++++++++------------------
 tracecmd/trace-show.c          |  2 +-
 tracecmd/trace-stat.c          |  2 +-
 utest/tracefs-utest.c          | 25 ++++++------
 8 files changed, 97 insertions(+), 74 deletions(-)
include/tracefs/tracefs.h +2 -2
index e5bf3df4..63f1944a 100644
@@ -20,10 +20,10 @@ char *tracefs_find_tracing_dir(void);
 /* ftarce instances */
 struct tracefs_instance;
 
-struct tracefs_instance *tracefs_instance_alloc(const char *name);
 void tracefs_instance_free(struct tracefs_instance *instance);
-int tracefs_instance_create(struct tracefs_instance *instance);
+struct tracefs_instance *tracefs_instance_create(const char *name);
 int tracefs_instance_destroy(struct tracefs_instance *instance);
+bool tracefs_instance_is_new(struct tracefs_instance *instance);
 const char *tracefs_instance_get_name(struct tracefs_instance *instance);
 char *
 tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
lib/trace-cmd/trace-timesync.c +2 -3
index 35a41394..397487e5 100644
@@ -235,16 +235,15 @@ static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
 static struct tracefs_instance *
 clock_synch_create_instance(const char *clock, unsigned int cid)
 {
-	struct tracefs_instance *instance;
+	struct tracefs_instance *instance = NULL;
 	char inst_name[256];
 
 	snprintf(inst_name, 256, "clock_synch-%d", cid);
 
-	instance = tracefs_instance_alloc(inst_name);
+	instance = tracefs_instance_create(inst_name);
 	if (!instance)
 		return NULL;
 
-	tracefs_instance_create(instance);
 	tracefs_instance_file_write(instance, "trace", "\0");
 	if (clock)
 		tracefs_instance_file_write(instance, "trace_clock", clock);
lib/tracefs/tracefs-instance.c +45 -14

96 lines not on this page · raw message

tracecmd/include/trace-local.h +2 -2
index d148aa16..207aa68f 100644
@@ -199,6 +199,7 @@ struct filter_pids {
 
 struct buffer_instance {
 	struct buffer_instance	*next;
+	char			*name;
 	struct tracefs_instance	*tracefs;
 	unsigned long long	trace_id;
 	char			*cpumask;
@@ -277,7 +278,7 @@ extern struct buffer_instance *first_instance;
 #define is_agent(instance)	((instance)->flags & BUFFER_FL_AGENT)
 #define is_guest(instance)	((instance)->flags & BUFFER_FL_GUEST)
 
-struct buffer_instance *create_instance(const char *name);
+struct buffer_instance *allocate_instance(const char *name);
 void add_instance(struct buffer_instance *instance, int cpu_count);
 void update_first_instance(struct buffer_instance *instance, int topt);
 
@@ -286,7 +287,6 @@ void show_instance_file(struct buffer_instance *instance, const char *name);
 int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu);
 
 /* moved from trace-cmd.h */
-void tracecmd_create_top_instance(char *name);
 void tracecmd_remove_instances(void);
 int tracecmd_add_event(const char *event_str, int stack);
 void tracecmd_enable_events(void);
tracecmd/trace-record.c +32 -38

148 lines not on this page · raw message

tracecmd/trace-show.c +1 -1
index a6f21027..eb328527 100644
@@ -64,7 +64,7 @@ void trace_show(int argc, char **argv)
 			if (buffer)
 				die("Can only show one buffer at a time");
 			buffer = optarg;
-			instance = create_instance(optarg);
+			instance = allocate_instance(optarg);
 			if (!instance)
 				die("Failed to create instance");
 			break;
tracecmd/trace-stat.c +1 -1
index 260d0c37..5f79ff8a 100644
@@ -926,7 +926,7 @@ void trace_stat (int argc, char **argv)
 			usage(argv);
 			break;
 		case 'B':
-			instance = create_instance(optarg);
+			instance = allocate_instance(optarg);
 			if (!instance)
 				die("Failed to create instance");
 			add_instance(instance, tracecmd_count_cpus());
utest/tracefs-utest.c +12 -13
index 292bcab0..e24fb38b 100644
@@ -179,6 +179,7 @@ static void test_instance_file_read(struct tracefs_instance *inst, char *fname)
 static void test_instance_file(void)
 {
 	struct tracefs_instance *instance = NULL;
+	struct tracefs_instance *second = NULL;
 	const char *name = get_rand_str();
 	const char *inst_name = NULL;
 	const char *tdir;
@@ -198,17 +199,19 @@ static void test_instance_file(void)
 	CU_TEST(stat(inst_dir, &st) != 0);
 
 	CU_TEST(tracefs_instance_exists(name) == false);
-	instance = tracefs_instance_alloc(name);
+	instance = tracefs_instance_create(name);
 	CU_TEST(instance != NULL);
-	CU_TEST(stat(inst_dir, &st) != 0);
-	inst_name = tracefs_instance_get_name(instance);
-	CU_TEST(inst_name != NULL);
-	CU_TEST(strcmp(inst_name, name) == 0);
-
-	CU_TEST(tracefs_instance_create(instance) == 0);
+	CU_TEST(tracefs_instance_is_new(instance));
+	second = tracefs_instance_create(name);
+	CU_TEST(second != NULL);
+	CU_TEST(!tracefs_instance_is_new(second));
+	tracefs_instance_free(second);
 	CU_TEST(tracefs_instance_exists(name) == true);
 	CU_TEST(stat(inst_dir, &st) == 0);
 	CU_TEST(S_ISDIR(st.st_mode));
+	inst_name = tracefs_instance_get_name(instance);
+	CU_TEST(inst_name != NULL);
+	CU_TEST(strcmp(inst_name, name) == 0);
 
 	fname = tracefs_instance_get_dir(NULL);
 	CU_TEST(fname != NULL);
@@ -475,12 +478,8 @@ static int test_suite_init(void)
 	test_tep = tracefs_local_events_system(NULL, systems);
 	if (test_tep == NULL)
 		return 1;
-
-	test_instance = tracefs_instance_alloc(TEST_INSTANCE_NAME);
-	if (test_instance == NULL)
-		return 1;
-
-	if (tracefs_instance_create(test_instance) < 0)
+	test_instance = tracefs_instance_create(TEST_INSTANCE_NAME);
+	if (!test_instance)
 		return 1;
 
 	return 0;
Signature · 1 line
2.28.0
#6 lore raw

Re: [PATCH v2 4/6] libtracefs: Combine allocate and create APIs into one

lib/trace-cmd/trace-timesync.c static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport, · line 238

Quoted 11 lines · On Tue, 10 Nov 2020 14:22:47 +0200 "Tzvetomir Stoyanov (VMware)"…

tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);

lib/trace-cmd/trace-timesync.c +1 -1
index 35a41394..397487e5 100644
@@ -235,16 +235,15 @@ static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
 static struct tracefs_instance *
 clock_synch_create_instance(const char *clock, unsigned int cid)
 {
-	struct tracefs_instance *instance;
+	struct tracefs_instance *instance = NULL;

Why set instance to NULL? It gets assigned a value in the second line.

-- Steve

Quoted 13 lines
lib/trace-cmd/trace-timesync.c +1 -2
 	char inst_name[256];
 
 	snprintf(inst_name, 256, "clock_synch-%d", cid);
 
-	instance = tracefs_instance_alloc(inst_name);
+	instance = tracefs_instance_create(inst_name);
 	if (!instance)
 		return NULL;
 
-	tracefs_instance_create(instance);
 	tracefs_instance_file_write(instance, "trace", "\0");
 	if (clock)
 		tracefs_instance_file_write(instance, "trace_clock", clock);
#7 lore raw

PATCH v2 5/6 libtracefs: Add new tracefs API tracefs_instances_walk()

In reply to Tzvetomir Stoyanov (VMware)

The logic for finding all configured ftrace instances is encapuslated in a new tracefs_instances_walk() API. A user specified callback is called for each ftrace instance in the system, excpet for the top level one.
The implementation of "trace-cmd stat" is modified to use the new API.

---
 include/tracefs/tracefs.h           |  1 +
 lib/tracefs/include/tracefs-local.h |  1 +
 lib/tracefs/tracefs-events.c        | 14 ++++----
 lib/tracefs/tracefs-instance.c      | 52 +++++++++++++++++++++++++++++
 tracecmd/trace-stat.c               | 52 ++++++++---------------------
 utest/tracefs-utest.c               | 51 ++++++++++++++++++++++++++++
 6 files changed, 125 insertions(+), 46 deletions(-)
include/tracefs/tracefs.h +1 -0
index 63f1944a..e796462a 100644
@@ -32,6 +32,7 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
 				const char *file, const char *str);
 char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 char *file, int *psize);
+int tracefs_instances_walk(int (*callback)(const char *, void *), void *context);
 
 bool tracefs_instance_exists(const char *name);
 bool tracefs_file_exists(struct tracefs_instance *instance, char *name);
lib/tracefs/include/tracefs-local.h +1 -0
index fe327a0f..08b67fa9 100644
@@ -9,5 +9,6 @@
 /* Can be overridden */
 void warning(const char *fmt, ...);
 int str_read_file(const char *file, char **buffer);
+char *trace_append_file(const char *dir, const char *name);
 
 #endif /* _TRACE_FS_LOCAL_H */
lib/tracefs/tracefs-events.c +7 -7
index 19f98058..60797748 100644
@@ -210,7 +210,7 @@ static char **add_list_string(char **list, const char *name, int len)
 	return list;
 }
 
-static char *append_file(const char *dir, const char *name)
+char *trace_append_file(const char *dir, const char *name)
 {
 	char *file;
 	int ret;
@@ -265,7 +265,7 @@ const char **tracefs_event_systems(const char *tracing_dir)
 	if (!tracing_dir)
 		return NULL;
 
-	events_dir = append_file(tracing_dir, "events");
+	events_dir = trace_append_file(tracing_dir, "events");
 	if (!events_dir)
 		return NULL;
 
@@ -290,14 +290,14 @@ const char **tracefs_event_systems(const char *tracing_dir)
 		    strcmp(name, "..") == 0)
 			continue;
 
-		sys = append_file(events_dir, name);
+		sys = trace_append_file(events_dir, name);
 		ret = stat(sys, &st);
 		if (ret < 0 || !S_ISDIR(st.st_mode)) {
 			free(sys);
 			continue;
 		}
 
-		enable = append_file(sys, "enable");
+		enable = trace_append_file(sys, "enable");
 
 		ret = stat(enable, &st);
 		if (ret >= 0)
@@ -358,7 +358,7 @@ const char **tracefs_system_events(const char *tracing_dir, const char *system)
 		    strcmp(name, "..") == 0)
 			continue;
 
-		event = append_file(system_dir, name);
+		event = trace_append_file(system_dir, name);
 		ret = stat(event, &st);
 		if (ret < 0 || !S_ISDIR(st.st_mode)) {
 			free(event);
@@ -400,7 +400,7 @@ const char **tracefs_tracers(const char *tracing_dir)
 	if (!tracing_dir)
 		return NULL;
 
-	available_tracers = append_file(tracing_dir, "available_tracers");
+	available_tracers = trace_append_file(tracing_dir, "available_tracers");
 	if (!available_tracers)
 		return NULL;
 
@@ -492,7 +492,7 @@ static int read_header(struct tep_handle *tep, const char *tracing_dir)
 	int len;
 	int ret = -1;
 
-	header = append_file(tracing_dir, "events/header_page");
+	header = trace_append_file(tracing_dir, "events/header_page");
 
 	ret = stat(header, &st);
 	if (ret < 0)
lib/tracefs/tracefs-instance.c +52 -0

61 lines not on this page · raw message

tracecmd/trace-stat.c +13 -39

68 lines not on this page · raw message

utest/tracefs-utest.c +51 -0

61 lines not on this page · raw message

Signature · 1 line
2.28.0
#8 lore raw

Re: [PATCH v2 5/6] libtracefs: Add new tracefs API tracefs_instances_walk()

On Tue, 10 Nov 2020 14:22:48 +0200
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:
\> +

Quoted 31 lines
lib/tracefs/tracefs-instance.c +31 -0
+/**
+ * tracefs_instances_walk - Iterate through all ftrace instances in the system
+ * @callback: user callback, called for each instance. Instance name is passed
+ *	      as input parameter. If the @callback returns non-zero,
+ *	      the iteration stops.
+ * @context: user context, passed to the @callback.
+ *
+ * Returns -1 in case of an error, 0 otherwise.
+ */
+int tracefs_instances_walk(int (*callback)(const char *, void *), void *context)
+{
+	struct dirent *dent;
+	char *path = NULL;
+	DIR *dir = NULL;
+	struct stat st;
+	int fret = -1;
+	int ret;
+
+	path = tracefs_get_tracing_file("instances");
+	if (!path)
+		return -1;
+	ret = stat(path, &st);
+	if (ret < 0 || !S_ISDIR(st.st_mode))
+		goto out;
+
+	dir = opendir(path);
+	if (!dir)
+		goto out;
+	fret = 0;
+	while ((dent = readdir(dir))) {
+		char *instance;

lib/tracefs/tracefs-instance.c bool tracefs_dir_exists(struct tracefs_instance *instance, char *name) · line 387

Quoted 10 lines
lib/tracefs/tracefs-instance.c +10 -0
+
+		if (strcmp(dent->d_name, ".") == 0 ||
+		    strcmp(dent->d_name, "..") == 0)
+			continue;
+		instance = trace_append_file(path, dent->d_name);
+		ret = stat(instance, &st);
+		free(instance);
+		if (ret < 0 || !S_ISDIR(st.st_mode))
+			continue;
+		if (callback(dent->d_name, context))

Should we notify the caller of this function if the callback returned non zero?

lib/tracefs/tracefs-instance.c bool tracefs_dir_exists(struct tracefs_instance *instance, char *name) · line 391

Quoted 4 lines
lib/tracefs/tracefs-instance.c +4 -0
+			break;
+	}
+
+out:

Hmm, would dir ever be anything but non NULL here?

-- Steve

Quoted 5 lines
lib/tracefs/tracefs-instance.c +5 -0
+	if (dir)
+		closedir(dir);
+	tracefs_put_tracing_file(path);
+	return fret;
+}
#9 lore raw

Re: [PATCH v2 5/6] libtracefs: Add new tracefs API tracefs_instances_walk()

Quoted 50 lines · Steven Rostedt

On Tue, 10 Nov 2020 14:22:48 +0200
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:
\> +

Quoted 31 lines
lib/tracefs/tracefs-instance.c +31 -0
+/**
+ * tracefs_instances_walk - Iterate through all ftrace instances in the system
+ * @callback: user callback, called for each instance. Instance name is passed
+ *         as input parameter. If the @callback returns non-zero,
+ *         the iteration stops.
+ * @context: user context, passed to the @callback.
+ *
+ * Returns -1 in case of an error, 0 otherwise.
+ */
+int tracefs_instances_walk(int (*callback)(const char *, void *), void *context)
+{
+     struct dirent *dent;
+     char *path = NULL;
+     DIR *dir = NULL;
+     struct stat st;
+     int fret = -1;
+     int ret;
+
+     path = tracefs_get_tracing_file("instances");
+     if (!path)
+             return -1;
+     ret = stat(path, &st);
+     if (ret < 0 || !S_ISDIR(st.st_mode))
+             goto out;
+
+     dir = opendir(path);
+     if (!dir)
+             goto out;
+     fret = 0;
+     while ((dent = readdir(dir))) {
+             char *instance;

lib/tracefs/tracefs-instance.c bool tracefs_dir_exists(struct tracefs_instance *instance, char *name) · line 387

Quoted 10 lines
lib/tracefs/tracefs-instance.c +10 -0
+
+             if (strcmp(dent->d_name, ".") == 0 ||
+                 strcmp(dent->d_name, "..") == 0)
+                     continue;
+             instance = trace_append_file(path, dent->d_name);
+             ret = stat(instance, &st);
+             free(instance);
+             if (ret < 0 || !S_ISDIR(st.st_mode))
+                     continue;
+             if (callback(dent->d_name, context))

Should we notify the caller of this function if the callback returned non zero?

Quoted 7 lines

lib/tracefs/tracefs-instance.c bool tracefs_dir_exists(struct tracefs_instance *instance, char *name) · line 391

Quoted 4 lines
lib/tracefs/tracefs-instance.c +4 -0
+                     break;
+     }
+
+out:

Hmm, would dir ever be anything but non NULL here?

It could be NULL if opendir() or stat() fails.

Thanks, Steve! I'll address your comments in the next version of the patch set.

Quoted 7 lines

-- Steve

Quoted 5 lines
lib/tracefs/tracefs-instance.c +5 -0
+     if (dir)
+             closedir(dir);
+     tracefs_put_tracing_file(path);
+     return fret;
+}
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#10 lore raw

PATCH v2 6/6 trace-cmd: Add new libtrasefs API to get the current trace clock

In reply to Tzvetomir Stoyanov (VMware)

A new API is added to tracefs library:
tracefs_get_clock()
It reads the trace_clock file from the ftrace file system, parses it and returns a string with the current trace clock. The returned string must be freed by free().

---
 include/tracefs/tracefs.h      |  2 ++
 lib/tracefs/tracefs-instance.c | 35 ++++++++++++++++++++++++++++++++++
 tracecmd/trace-stat.c          | 29 +++++++---------------------
 utest/tracefs-utest.c          | 30 ++++++++++++++++++++++++++++-
 4 files changed, 73 insertions(+), 23 deletions(-)
include/tracefs/tracefs.h +2 -0
index e796462a..07269cee 100644
@@ -57,4 +57,6 @@ struct tep_handle *tracefs_local_events_system(const char *tracing_dir,
 int tracefs_fill_local_events(const char *tracing_dir,
 			       struct tep_handle *tep, int *parsing_failures);
 
+char *tracefs_get_clock(struct tracefs_instance *instance);
+
 #endif /* _TRACE_FS_H */
lib/tracefs/tracefs-instance.c +35 -0
index 5ff313c7..6158804c 100644
@@ -394,3 +394,38 @@ out:
 	tracefs_put_tracing_file(path);
 	return fret;
 }
+
+/**
+ * tracefs_get_clock - Get the current trace clock
+ * @instance: ftrace instance, can be NULL for the top instance
+ *
+ * Returns the current trace clock of the given instance, or NULL in
+ * case of an error.
+ * The return string must be freed by free()
+ */
+char *tracefs_get_clock(struct tracefs_instance *instance)
+{
+	char *all_clocks = NULL;
+	char *ret = NULL;
+	int bytes = 0;
+	char *clock;
+	char *cont;
+
+	all_clocks  = tracefs_instance_file_read(instance, "trace_clock", &bytes);
+	if (!all_clocks || !bytes)
+		goto out;
+
+	clock = strstr(all_clocks, "[");
+	if (!clock)
+		goto out;
+	clock++;
+	cont = strstr(clock, "]");
+	if (!cont)
+		goto out;
+	*cont = '\0';
+
+	ret = strdup(clock);
+out:
+	free(all_clocks);
+	return ret;
+}
tracecmd/trace-stat.c +7 -22
index e6678eab..9b61a14f 100644
@@ -710,33 +710,18 @@ static void report_buffers(struct buffer_instance *instance)
 
 static void report_clock(struct buffer_instance *instance)
 {
-	char *str;
-	char *cont;
 	char *clock;
 
-	str = get_instance_file_content(instance, "trace_clock");
-	if (!str)
-		return;
-
-	clock = strstr(str, "[");
-	if (!clock)
-		goto out;
-	clock++;
-
-	cont = strstr(clock, "]");
-	if (!cont) /* should never happen */
-		goto out;
-
-	*cont = '\0';
+	if (instance)
+		clock = tracefs_get_clock(instance->tracefs);
+	else
+		clock = tracefs_get_clock(NULL);
 
 	/* Default clock is "local", only show others */
-	if (strcmp(clock, "local") == 0)
-		goto out;
+	if (clock && !strcmp(clock, "local") == 0)
+		printf("\nClock: %s\n", clock);
 
-	printf("\nClock: %s\n", clock);
-
- out:
-	free(str);
+	free(clock);
 }
 
 static void report_cpumask(struct buffer_instance *instance)
utest/tracefs-utest.c +29 -1
index 7e9fe738..798fedb8 100644
@@ -511,6 +511,33 @@ static void test_instances_walk(void)
 	}
 }
 
+static void current_clock_check(const char *clock)
+{
+	int size = 0;
+	char *clocks;
+	char *str;
+
+	clocks = tracefs_instance_file_read(test_instance, "trace_clock", &size);
+	CU_TEST(clocks != NULL);
+	CU_TEST(size > strlen(clock));
+	str = strstr(clocks, clock);
+	CU_TEST(str != NULL);
+	CU_TEST(str != clocks);
+	CU_TEST(*(str - 1) == '[');
+	CU_TEST(*(str + strlen(clock)) == ']');
+	free(clocks);
+}
+
+static void test_get_clock(void)
+{
+	const char *clock;
+
+	clock = tracefs_get_clock(test_instance);
+	CU_TEST(clock != NULL);
+	current_clock_check(clock);
+	free((char *)clock);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -556,5 +583,6 @@ void test_tracefs_lib(void)
 		    test_local_events);
 	CU_add_test(suite, "tracefs_instances_walk API",
 		    test_instances_walk);
-
+	CU_add_test(suite, "tracefs_get_clock API",
+		    test_get_clock);
 }
Signature · 1 line
2.28.0