Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command
@ 2020-04-14 15:56 Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The "trace-cmd reset" command is supposed to set the ftrace state to default.
However, some ftrace configurations are not reseted: synthetic events and
error_log file. The command is extended to clean these.
Two new tracefs APIs were also implemented, to check if given ftrace
file / directory exists:
  bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
  bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);

[
 v5 changes:
   Removed accepted patches from the set
   Address Steven's comments
 v4 chanegs:
   Implemented new tracefs APIs to check if given ftrace file /
   directory exists. Use the new APIs to check if synthetic_events and
   error_log files exist, before accessing them.
 v3 changes:
    Remove the warning in case there are no configured synthetic events.
 v2 changes:
    Removed these patches from the "SQL-like syntax for ftrace histograms
    configuration" patch set, as they are not directly related to it.
]

Tzvetomir Stoyanov (VMware) (3):
  trace-cmd: Add new tracefs APIs for checking if a ftrace file /
    directory exists
  trace-cmd: Unit tests for new libtracefs APIs
  trace-cmd: Clear error log on reset subcommand

 include/tracefs/tracefs.h      |  3 +++
 lib/tracefs/tracefs-instance.c | 46 +++++++++++++++++++++++++++++++++-
 tracecmd/trace-record.c        | 23 +++++++++++++++++
 utest/tracefs-utest.c          | 16 ++++++++++++
 4 files changed, 87 insertions(+), 1 deletion(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists
  2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand Tzvetomir Stoyanov (VMware)
  2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Some ftrace files and directories are optional, depending on specific
kernel configuration or version. It is a good practice to check if
the file / directory exist, before trying to access it.
There are a lot of places in trace-cmd implementation with such checks.
The new libtracefs APIs can be used for this, they are ftarce instance aware:
 bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
 bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs/tracefs.h      |  3 +++
 lib/tracefs/tracefs-instance.c | 46 +++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index 85690b66..bc8bebcb 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -33,6 +33,9 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
 char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 char *file, int *psize);
 
+bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
+bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);
+
 /* events */
 void tracefs_list_free(char **list);
 char **tracefs_event_systems(const char *tracing_dir);
diff --git a/lib/tracefs/tracefs-instance.c b/lib/tracefs/tracefs-instance.c
index b96ab61c..a9ab64bd 100644
--- a/lib/tracefs/tracefs-instance.c
+++ b/lib/tracefs/tracefs-instance.c
@@ -13,7 +13,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-
+#include <linux/limits.h>
 #include "tracefs.h"
 #include "tracefs-local.h"
 
@@ -247,3 +247,47 @@ char *tracefs_instance_file_read(struct tracefs_instance *instance,
 
 	return buf;
 }
+
+static bool check_file_exist(struct tracefs_instance *instance,
+			     char *name, bool dir)
+{
+	char file[PATH_MAX];
+	struct stat st;
+	char *path;
+	int ret;
+
+	path = tracefs_instance_get_dir(instance);
+	snprintf(file, PATH_MAX, "%s/%s", path, name);
+	tracefs_put_tracing_file(path);
+	ret = stat(file, &st);
+	if (ret < 0)
+		return false;
+
+	return !dir == !S_ISDIR(st.st_mode);
+}
+
+/**
+ * tracefs_file_exist - Check if a file with given name exists in given instance
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @name: name of the file
+ *
+ * Returns true if the file exists, false otherwise
+ *
+ * If a directory with the given name exists, false is returned.
+ */
+bool tracefs_file_exist(struct tracefs_instance *instance, char *name)
+{
+	return check_file_exist(instance, name, false);
+}
+
+/**
+ * tracefs_dir_exist - Check if a directory with given name exists in given instance
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @name: name of the directory
+ *
+ * Returns true if the directory exists, false otherwise
+ */
+bool tracefs_dir_exist(struct tracefs_instance *instance, char *name)
+{
+	return check_file_exist(instance, name, true);
+}
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs
  2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand Tzvetomir Stoyanov (VMware)
  2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Unit tests for tracefs APIs, checking for ftrace file / directory existence:
  bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
  bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 2473e893..3f57ecad 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -175,6 +175,7 @@ static void test_instance_file_read(struct tracefs_instance *inst, char *fname)
 
 #define ALL_TRACERS	"available_tracers"
 #define CUR_TRACER	"current_tracer"
+#define PER_CPU		"per_cpu"
 static void test_instance_file(void)
 {
 	struct tracefs_instance *instance = NULL;
@@ -251,6 +252,21 @@ static void test_instance_file(void)
 	tracefs_put_tracing_file(inst_file);
 	free(fname);
 
+	CU_TEST(tracefs_file_exist(NULL, (char *)name) == false);
+	CU_TEST(tracefs_dir_exist(NULL, (char *)name) == false);
+	CU_TEST(tracefs_file_exist(instance, (char *)name) == false);
+	CU_TEST(tracefs_dir_exist(instance, (char *)name) == false);
+
+	CU_TEST(tracefs_file_exist(NULL, CUR_TRACER) == true);
+	CU_TEST(tracefs_dir_exist(NULL, CUR_TRACER) == false);
+	CU_TEST(tracefs_file_exist(instance, CUR_TRACER) == true);
+	CU_TEST(tracefs_dir_exist(instance, CUR_TRACER) == false);
+
+	CU_TEST(tracefs_file_exist(NULL, PER_CPU) == false);
+	CU_TEST(tracefs_dir_exist(NULL, PER_CPU) == true);
+	CU_TEST(tracefs_file_exist(instance, PER_CPU) == false);
+	CU_TEST(tracefs_dir_exist(instance, PER_CPU) == true);
+
 	CU_TEST(tracefs_instance_destroy(NULL) != 0);
 	CU_TEST(tracefs_instance_destroy(instance) == 0);
 	CU_TEST(tracefs_instance_destroy(instance) != 0);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand
  2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
  2020-04-14 15:56 ` [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
  2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The "trace-cmd reset" command is supposed to set the ftrace state to default.
However, the ftrace error logs are not reseted.
A logic is added to delete the content of "error_log" file when "trace-cmd reset" is executed.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 tracecmd/trace-record.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 1b3f403f..349a5596 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -4820,6 +4820,28 @@ static void clear_triggers(void)
 		clear_instance_triggers(instance);
 }
 
+static void clear_instance_error_log(struct buffer_instance *instance)
+{
+	char *file;
+
+	if (!tracefs_file_exist(instance->tracefs, "error_log"))
+		return;
+
+	file = tracefs_instance_get_file(instance->tracefs, "error_log");
+	if (!file)
+		return;
+	write_file(file, " ");
+	tracefs_put_tracing_file(file);
+}
+
+static void clear_error_log(void)
+{
+	struct buffer_instance *instance;
+
+	for_all_instances(instance)
+		clear_instance_error_log(instance);
+}
+
 static void clear_all_synth_events(void)
 {
 	char sevent[BUFSIZ];
@@ -5564,6 +5586,7 @@ void trace_reset(int argc, char **argv)
 	clear_filters();
 	clear_triggers();
 	clear_all_synth_events();
+	clear_error_log();
 	/* set clock to "local" */
 	reset_clock();
 	reset_event_pid();
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand Tzvetomir Stoyanov (VMware)

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git