Extend coverage of "trace-cmd reset" command
v4 cover Extend coverage of "trace-cmd reset" command
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);
[
v2 changes:
- Removed these patches from the "SQL-like syntax for ftrace histograms configuration" patch set, as they are not directly related to it.
v3 changes:
- Remove the warning in case there are no configured synthetic events.
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.
]
Tzvetomir Stoyanov (VMware) (4):
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 synthetic events on reset subcommand
trace-cmd: Clear error log on reset subcommand
include/tracefs/tracefs.h | 3 ++ lib/tracefs/tracefs-instance.c | 49 ++++++++++++++++++++++++++- tracecmd/trace-record.c | 61 ++++++++++++++++++++++++++++++++++ utest/tracefs-utest.c | 16 +++++++++ 4 files changed, 128 insertions(+), 1 deletion(-)
Signature · 1 line
2.24.1
PATCH v4 1/4 trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists
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);
--- include/tracefs/tracefs.h | 3 +++ lib/tracefs/tracefs-instance.c | 49 +++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-)
include/tracefs/tracefs.h +3 -0
@@ -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);
lib/tracefs/tracefs-instance.c +48 -1
58 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v4 1/4] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists
Quoted 55 lines · On Tue, 25 Feb 2020 20:07:30 +0200 "Tzvetomir Stoyanov (VMware)"…
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:
booltracefs_file_exist(struct tracefs_instance *instance, char *name);
booltracefs_dir_exist(struct tracefs_instance *instance, char *name);Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- include/tracefs/tracefs.h | 3 +++ lib/tracefs/tracefs-instance.c | 49 +++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-)include/tracefs/tracefs.h +3
-0@@ -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);lib/tracefs/tracefs-instance.c +13
-1@@ -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,50 @@ 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);
lib/tracefs/tracefs-instance.c char *tracefs_instance_file_read(struct tracefs_instance *instance, · line 271
Quoted 10 lines
lib/tracefs/tracefs-instance.c +10
-0+ ret =stat (file, &st); +if (ret <0 ) +return false; + +if (dir && !S_ISDIR (st.st_mode)) +return false; +if (!dir &&S_ISDIR (st.st_mode)) +return false; + +return true;
BTW, the above can be converted to just:
return !dir == !S_ISDIR(st.st_mode);
lib/tracefs/tracefs-instance.c char *tracefs_instance_file_read(struct tracefs_instance *instance, · line 279
Quoted 8 lines
lib/tracefs/tracefs-instance.c +8
-0+} + +/** +* 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
Probably should comment that this will return false if the "file" exists but is a directory.
Is there any reason we would not want to return true here if it was a directory? A directory is just another kind of "file" (in the Unix world).
-- Steve
Quoted 17 lines
lib/tracefs/tracefs-instance.c +17
-0+*/ +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); +}
Re: [PATCH v4 1/4] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists
Quoted 87 lines · Steven Rostedt
Quoted 55 lines · On Tue, 25 Feb 2020 20:07:30 +0200 "Tzvetomir Stoyanov (VMware)"…
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:
booltracefs_file_exist(struct tracefs_instance *instance, char *name);
booltracefs_dir_exist(struct tracefs_instance *instance, char *name);Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- include/tracefs/tracefs.h | 3 +++ lib/tracefs/tracefs-instance.c | 49 +++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-)include/tracefs/tracefs.h +3
-0@@ -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);lib/tracefs/tracefs-instance.c +13
-1@@ -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,50 @@ 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);lib/tracefs/tracefs-instance.c char *tracefs_instance_file_read(struct tracefs_instance *instance, · line 271
Quoted 10 lines
lib/tracefs/tracefs-instance.c +10
-0+ ret =stat (file, &st); +if (ret <0 ) +return false; + +if (dir && !S_ISDIR (st.st_mode)) +return false; +if (!dir &&S_ISDIR (st.st_mode)) +return false; + +return true;BTW, the above can be converted to just:
return !dir == !S_ISDIR(st.st_mode);lib/tracefs/tracefs-instance.c char *tracefs_instance_file_read(struct tracefs_instance *instance, · line 279
Quoted 8 lines
lib/tracefs/tracefs-instance.c +8
-0+} + +/** +* 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 Probably should comment that this will return false if the "file" exists but is a directory.
Quoted 3 lines
Is there any reason we would not want to return true here if it was a directory? A directory is just another kind of "file" (in the Unix world).
The use case of the API is to check if a given file exists in tracefs, before writing to / reading from it. If the user expects file, but it is a directory - the operation will fail. We can think of adding additional input parameter to the API, to let the user specify the desired behavior of the API ? Something like :
bool tracefs_file_exist(struct tracefs_instance *instance, char *name, bool any);
Quoted 20 lines
-- Steve
Quoted 17 lines
lib/tracefs/tracefs-instance.c +17
-0+*/ +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); +}
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
PATCH v4 2/4 trace-cmd: Unit tests for new libtracefs APIs
In reply to Tzvetomir Stoyanov (VMware)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);
--- utest/tracefs-utest.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
utest/tracefs-utest.c +16 -0
@@ -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 );
Signature · 1 line
2.24.1
PATCH v4 3/4 trace-cmd: Clear synthetic events on reset subcommand
In reply to Tzvetomir Stoyanov (VMware)The "trace-cmd reset" command is supposed to set the ftrace state to default. However, the synthetic events are not reseted.
A logic is added to delete all entries from "synthetic_events" files.
---
v3: remove the warning in case there are no configured synthetic events
v2: remove the patch from "SQL-like syntax for ftrace histograms configuration" patch set
--- tracecmd/trace-record.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
tracecmd/trace-record.c +38 -0
@@ -4638,6 +4638,43 @@ static void clear_triggers(void)clear_instance_triggers (instance); } +static void clear_all_synth_events (void ) +{ +char sevent[BUFSIZ ]; +char *save =NULL ; +char *line; +char *file; +char *buf; +int len; + +if (!tracefs_file_exist (NULL ,"synthetic_events" )) +return ; + file =tracefs_instance_get_file (NULL ,"synthetic_events" ); +if (!file) +return ; + + buf =read_file (file); +if (!buf) + goto out; + + sevent[0 ] ='!' ; + +for (line =strtok_r (buf,"\n" , &save); line; line =strtok_r (NULL ,"\n" , &save)) { + len =strlen (line); +if (len >BUFSIZ -2 ) + len =BUFSIZ -2 ; +strncpy (sevent +1 , line, len); + sevent[len +1 ] ='\0' ; +write_file (file, sevent); + } +out: +free (buf); +tracefs_put_tracing_file (file); + +} + + +static void clear_func_filters (void ) {struct buffer_instance *instance;
@@ -5344,6 +5381,7 @@ void trace_reset(int argc, char **argv)set_buffer_size ();clear_filters ();clear_triggers (); +clear_all_synth_events ();/* set clock to "local" */ reset_clock ();reset_event_pid ();
Signature · 1 line
2.24.1
PATCH v4 4/4 trace-cmd: Clear error log on reset subcommand
In reply to Tzvetomir Stoyanov (VMware)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.
--- tracecmd/trace-record.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tracecmd/trace-record.c +23 -0
@@ -4638,6 +4638,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 ];
@@ -5382,6 +5404,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 ();
Signature · 1 line
2.24.1