Refactor the APIs for tracing options
cover Refactor the APIs for tracing options
Yordan Karadzhov (VMware) (6):
libtracefs: Fix issues with tracefs_option_id()
libtracefs: Modify the arguments of tracefs_option_is_set()
libtracefs: Encapsulate "struct tracefs_options_mask"
libtracefs: Move the "options" code to tracefs-instance.c
libtracefs: Option's bit masks to be owned by the instance
libtracefs: Rename tracefs_option_is_set()
Documentation/libtracefs-option-get.txt | 4 +- include/tracefs.h | 15 +- src/tracefs-instance.c | 266 +++++++++++++++++++++++ src/tracefs-tools.c | 276 ------------------------ 4 files changed, 273 insertions(+), 288 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/6 libtracefs: Fix issues with tracefs_option_id()
Few mistakes have been made when introducing this API function. First of all, its declaration is missing from the header file. In addition to this the argument type is missing "const".
--- include/tracefs.h | 1 + src/tracefs-tools.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/tracefs.h +1 -0
@@ -145,5 +145,6 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_oint tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);const char *tracefs_option_name (enum tracefs_option_id id); +enum tracefs_option_id tracefs_option_id (const char *name);#endif /* _TRACE_FS_H */
src/tracefs-tools.c +1 -1
@@ -181,7 +181,7 @@ const char *tracefs_option_name(enum tracefs_option_id id)* Returns trace option ID or TRACEFS_OPTION_INVALID in case of an error or * unknown option name. */ -enum tracefs_option_id tracefs_option_id (char *name) +enum tracefs_option_id tracefs_option_id (const char *name) {int i;
Signature · 1 line
2.27.0
PATCH 2/6 libtracefs: Modify the arguments of tracefs_option_is_set()
In reply to Yordan Karadzhov (VMware)This function is supposed to take as argument a mask returned by "tracefs_options_get_supported()" or "tracefs_options_get_enabled()", hence this argument must be a pointer.
--- include/tracefs.h | 4 ++-- src/tracefs-tools.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
include/tracefs.h +2 -2
@@ -136,8 +136,8 @@ struct tracefs_options_mask { };void tracefs_option_set (struct tracefs_options_mask *options,enum tracefs_option_id id);void tracefs_option_clear (struct tracefs_options_mask *options,enum tracefs_option_id id); -bool tracefs_option_is_set (struct tracefs_options_mask options,enum tracefs_option_id id); - +bool tracefs_option_is_set (struct tracefs_options_mask *options, +enum tracefs_option_id id);struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id);struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);
src/tracefs-tools.c +3 -2
@@ -359,10 +359,11 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_o* Returns true if an option with given id is set in the bitmask, * false if it is not set. */ -bool tracefs_option_is_set (struct tracefs_options_mask options,enum tracefs_option_id id) +bool tracefs_option_is_set (struct tracefs_options_mask *options, +enum tracefs_option_id id) {if (id >TRACEFS_OPTION_INVALID ) -return options.mask & (1ULL << (id -1 )); +return options->mask & (1ULL << (id -1 ));return false; }
Signature · 1 line
2.27.0
PATCH 3/6 libtracefs: Encapsulate "struct tracefs_options_mask"
In reply to Yordan Karadzhov (VMware)The definition of the mask gets hidden from the user. This way we will be able to modify this definition in the future, without breaking the API. Such a modification will be compulsory, if too many new tracing options are added in the future. Note that encapsulating the mask definition, requires two API methods to be eliminated, however those methods have no particular use-cases for the moment.
--- include/tracefs.h | 6 +----- src/tracefs-tools.c | 33 +++++++++++---------------------- 2 files changed, 12 insertions(+), 27 deletions(-)
include/tracefs.h +1 -5
@@ -131,11 +131,7 @@ enum tracefs_option_id { };#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1) -struct tracefs_options_mask { -unsigned long long mask; -}; -void tracefs_option_set (struct tracefs_options_mask *options,enum tracefs_option_id id); -void tracefs_option_clear (struct tracefs_options_mask *options,enum tracefs_option_id id); +struct tracefs_options_mask ;bool tracefs_option_is_set (struct tracefs_options_mask *options,enum tracefs_option_id id);struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);
src/tracefs-tools.c +11 -22
@@ -19,6 +19,10 @@#define TRACE_CTRL "tracing_on" +struct tracefs_options_mask { +unsigned long long mask; +}; +static const char *const options_map[] = {"unknown" ,"annotate" ,
@@ -197,6 +201,13 @@ enum tracefs_option_id tracefs_option_id(const char *name)return TRACEFS_OPTION_INVALID ; } +static void tracefs_option_set (struct tracefs_options_mask *options, +enum tracefs_option_id id) +{ +if (options && id >TRACEFS_OPTION_INVALID ) + options->mask |= (1ULL << (id -1 )); +} +static struct tracefs_options_mask *trace_get_options (struct tracefs_instance *instance,bool enabled) {
@@ -366,25 +377,3 @@ bool tracefs_option_is_set(struct tracefs_options_mask *options,return options->mask & (1ULL << (id -1 ));return false; } - -/** -* tracefs_option_set - Set option in options bitmask -* @options: Pointer to a bitmask with options -* @id: trace option id -*/ -void tracefs_option_set (struct tracefs_options_mask *options,enum tracefs_option_id id) -{ -if (options && id >TRACEFS_OPTION_INVALID ) - options->mask |= (1ULL << (id -1 )); -} - -/** -* tracefs_option_clear - Clear option from options bitmask -* @options: Pointer to a bitmask with options -* @id: trace option id -*/ -void tracefs_option_clear (struct tracefs_options_mask *options,enum tracefs_option_id id) -{ -if (options && id >TRACEFS_OPTION_INVALID ) - options->mask &= ~(1ULL << (id -1 )); -}
Signature · 1 line
2.27.0
Re: [PATCH 3/6] libtracefs: Encapsulate "struct tracefs_options_mask"
src/tracefs-tools.c enum tracefs_option_id tracefs_option_id(const char *name) · line 205
Quoted 6 lines · On Fri, 2 Apr 2021 16:19:44 +0300 "Yordan Karadzhov (VMware)"…
src/tracefs-tools.c +2
-0@@ -197,6 +201,13 @@ enum tracefs_option_id tracefs_option_id(const char *name)return TRACEFS_OPTION_INVALID ; } +static void tracefs_option_set (struct tracefs_options_mask *options, +enum tracefs_option_id id)
Patch is fine, but lets rename this function to:
trace_option_set()
As functions named "tracefs_*()" should be used only by functions that are for the API or will be for the API (staging functions).
-- Steve
Quoted 8 lines
src/tracefs-tools.c +5
-0+{ +if (options && id >TRACEFS_OPTION_INVALID ) + options->mask |= (1ULL << (id -1 )); +} +static struct tracefs_options_mask *trace_get_options (struct tracefs_instance *instance,bool enabled) {
PATCH 4/6 libtracefs: Move the "options" code to tracefs-instance.c
In reply to Yordan Karadzhov (VMware)We would like the instance object to own two bit masks (struct tracefs_options_mask) associated with all supported and all enabled options. However, the definition of the instance itself is not public, hence the code implementing the "options" related APIs has to in the same source file as the definition of the instance.
--- src/tracefs-instance.c | 266 +++++++++++++++++++++++++++++++++++++++++ src/tracefs-tools.c | 266 ----------------------------------------- 2 files changed, 266 insertions(+), 266 deletions(-)
src/tracefs-instance.c +266 -0
@@ -18,6 +18,10 @@#include "tracefs.h" #include "tracefs-local.h" +struct tracefs_options_mask { +unsigned long long mask; +}; +#define FLAG_INSTANCE_NEWLY_CREATED (1 << 0)struct tracefs_instance {char *trace_dir;
@@ -587,3 +591,265 @@ out:free (all_clocks);return ret; } + +static const char *const options_map[] = { +"unknown" , +"annotate" , +"bin" , +"blk_cgname" , +"blk_cgroup" , +"blk_classic" , +"block" , +"context-info" , +"disable_on_free" , +"display-graph" , +"event-fork" , +"funcgraph-abstime" , +"funcgraph-cpu" , +"funcgraph-duration" , +"funcgraph-irqs" , +"funcgraph-overhead" , +"funcgraph-overrun" , +"funcgraph-proc" , +"funcgraph-tail" , +"func_stack_trace" , +"function-fork" , +"function-trace" , +"graph-time" , +"hex" , +"irq-info" , +"latency-format" , +"markers" , +"overwrite" , +"pause-on-trace" , +"printk-msg-only" , +"print-parent" , +"raw" , +"record-cmd" , +"record-tgid" , +"sleep-time" , +"stacktrace" , +"sym-addr" , +"sym-offset" , +"sym-userobj" , +"trace_printk" , +"userstacktrace" , +"verbose" }; + +/** +* tracefs_option_name - Get trace option name from id +* @id: trace option id +* +* Returns string with option name, or "unknown" in case of not known option id. +* The returned string must *not* be freed. +*/ +const char *tracefs_option_name (enum tracefs_option_id id) +{ +/* Make sure options map contains all the options */ +BUILD_BUG_ON (ARRAY_SIZE (options_map) !=TRACEFS_OPTION_MAX ); + +if (id <TRACEFS_OPTION_MAX ) +return options_map[id]; + +return options_map[0 ]; +} + +/** +* tracefs_option_id - Get trace option ID from name +* @name: trace option name +* +* Returns trace option ID or TRACEFS_OPTION_INVALID in case of an error or +* unknown option name. +*/ +enum tracefs_option_id tracefs_option_id (const char *name) +{ +int i; + +if (!name) +return TRACEFS_OPTION_INVALID ; + +for (i =0 ; i <TRACEFS_OPTION_MAX ; i++) { +if (strlen (name) ==strlen (options_map[i]) && + !strcmp (options_map[i], name)) +return i; + } + +return TRACEFS_OPTION_INVALID ; +} + +static void tracefs_option_set (struct tracefs_options_mask *options, +enum tracefs_option_id id) +{ +if (options && id >TRACEFS_OPTION_INVALID ) + options->mask |= (1ULL << (id -1 )); +} + +static struct tracefs_options_mask *trace_get_options (struct tracefs_instance *instance, +bool enabled) +{ +struct tracefs_options_mask *bitmask; +enum tracefs_option_id id; +char file[PATH_MAX ]; +struct dirent *dent; +char *dname =NULL ; +DIR *dir =NULL ; +long long val; + + bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); +if (!bitmask) +return NULL ; + dname =tracefs_instance_get_file (instance,"options" ); +if (!dname) + goto error; + dir =opendir (dname); +if (!dir) + goto error; + +while ((dent =readdir (dir))) { +if (*dent->d_name =='.' ) +continue ; +if (enabled) { +snprintf (file,PATH_MAX ,"options/%s" , dent->d_name); +if (tracefs_instance_file_read_number (instance, file, &val) !=0 || + val !=1 ) +continue ; + } + id =tracefs_option_id (dent->d_name); +if (id !=TRACEFS_OPTION_INVALID ) +tracefs_option_set (bitmask, id); + } +closedir (dir); +tracefs_put_tracing_file (dname); + +return bitmask; + +error: +if (dir) +closedir (dir); +tracefs_put_tracing_file (dname); +free (bitmask); +return NULL ; +} + +/** +* tracefs_options_get_supported - Get all supported trace options in given instance +* @instance: ftrace instance, can be NULL for the top instance +* +* Returns allocated bitmask structure with all trace options, supported in given +* instance, or NULL in case of an error. The returned structure must be freed with free() +*/ +struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) +{ +return trace_get_options (instance, false); +} + +/** +* tracefs_options_get_enabled - Get all currently enabled trace options in given instance +* @instance: ftrace instance, can be NULL for the top instance +* +* Returns allocated bitmask structure with all trace options, enabled in given +* instance, or NULL in case of an error. The returned structure must be freed with free() +*/ +struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) +{ +return trace_get_options (instance, true); +} + +static int trace_config_option (struct tracefs_instance *instance, +enum tracefs_option_id id,bool set) +{ +char *set_str = set ?"1" :"0" ; +char file[PATH_MAX ]; +const char *name; + + name =tracefs_option_name (id); +if (!name) +return -1 ; + +snprintf (file,PATH_MAX ,"options/%s" , name); +if (strlen (set_str) !=tracefs_instance_file_write (instance, file, set_str)) +return -1 ; +return 0 ; +} + +/** +* tracefs_option_enable - Enable trace option +* @instance: ftrace instance, can be NULL for the top instance +* @id: trace option id +* +* Returns -1 in case of an error or 0 otherwise +*/ +int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id) +{ +return trace_config_option (instance, id, true); +} + +/** +* tracefs_option_diasble - Disable trace option +* @instance: ftrace instance, can be NULL for the top instance +* @id: trace option id +* +* Returns -1 in case of an error or 0 otherwise +*/ +int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id) +{ +return trace_config_option (instance, id, false); +} + +/** +* tracefs_option_is_supported - Check if an option is supported +* @instance: ftrace instance, can be NULL for the top instance +* @id: trace option id +* +* Returns true if an option with given id is supported by the system, false if +* it is not supported. +*/ +bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id) +{ +const char *name =tracefs_option_name (id); +char file[PATH_MAX ]; + +if (!name) +return false; +snprintf (file,PATH_MAX ,"options/%s" , name); +return tracefs_file_exists (instance, file); +} + +/** +* tracefs_option_is_enabled - Check if an option is enabled in given instance +* @instance: ftrace instance, can be NULL for the top instance +* @id: trace option id +* +* Returns true if an option with given id is enabled in the given instance, +* false if it is not enabled. +*/ +bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id) +{ +const char *name =tracefs_option_name (id); +char file[PATH_MAX ]; +long long res; + +if (!name) +return false; +snprintf (file,PATH_MAX ,"options/%s" , name); +if (!tracefs_instance_file_read_number (instance, file, &res) && res) +return true; + +return false; +} + +/** +* tracefs_option_is_set - Check if given option is set in the bitmask +* @options: Options bitmask +* @id: trace option id +* +* Returns true if an option with given id is set in the bitmask, +* false if it is not set. +*/ +bool tracefs_option_is_set (struct tracefs_options_mask *options, +enum tracefs_option_id id) +{ +if (id >TRACEFS_OPTION_INVALID ) +return options->mask & (1ULL << (id -1 )); +return false; +}
src/tracefs-tools.c +0 -266
275 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH 4/6] libtracefs: Move the "options" code to tracefs-instance.c
Quoted 5 lines · On Fri, 2 Apr 2021 16:19:45 +0300 "Yordan Karadzhov (VMware)"…
We would like the instance object to own two bit masks (struct
tracefs_options_mask) associated with all supported and all enabled options. However, the definition of the instance itself is not public, hence the code implementing the "options" related APIs has to in the same source file as the definition of the instance.
The above is not true. We can keep the code in tracefs-tools.c and make it "public" for the internal use of the library, but not part of the API. To do so, you place shared structures and functions in include/tracefs-local.h, and mark all non static functions with "__hidden" which will keep it from being used outside the library.
Do you still feel that the moving of this code is needed?
-- Steve
Quoted 6 lines
Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- src/tracefs-instance.c | 266 +++++++++++++++++++++++++++++++++++++++++ src/tracefs-tools.c | 266 ----------------------------------------- 2 files changed, 266 insertions(+), 266 deletions(-)
Re: [PATCH 4/6] libtracefs: Move the "options" code to tracefs-instance.c
Quoted 1 line · On Fri, 2 Apr 2021 10:17:56 -0400 Steven Rostedt <[email protected]> wrote:
Do you still feel that the moving of this code is needed?
The rest of the patch series looks fine. Can you send a v2? I'll let you and Tzvetomir decide if you want the code from this patch moved to the instance file. But at a minimum, lets rename tracefs_option_set() to trace_option_set().
Thanks!
-- Steve
Re: [PATCH 4/6] libtracefs: Move the "options" code to tracefs-instance.c
In reply to Steven RostedtQuoted 8 lines · Steven Rostedt
Quoted 5 lines · On Fri, 2 Apr 2021 16:19:45 +0300 "Yordan Karadzhov (VMware)"…
We would like the instance object to own two bit masks (struct
tracefs_options_mask) associated with all supported and all enabled options. However, the definition of the instance itself is not public, hence the code implementing the "options" related APIs has to in the same source file as the definition of the instance.
Quoted 7 lines
The above is not true. We can keep the code in
tracefs-tools.cand make it "public" for the internal use of the library, but not part of the API. To do so, you place shared structures and functions ininclude/tracefs-local.h, and mark all non static functions with "__hidden" which will keep it from being used outside the library.Do you still feel that the moving of this code is needed?
Hi Steven,
Thanks a lot for the review!
I wasn't sure if it is OK to move the "tracefs_instance" struct to "include/tracefs-local.h". If this is the case, then I will drop this patch and send v2.
cheers,
Yordan
Quoted 9 lines
-- Steve
Quoted 5 lines
Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- src/tracefs-instance.c | 266 +++++++++++++++++++++++++++++++++++++++++ src/tracefs-tools.c | 266 ----------------------------------------- 2 files changed, 266 insertions(+), 266 deletions(-)
PATCH 5/6 libtracefs: Option's bit masks to be owned by the instance
In reply to Yordan Karadzhov (VMware)If the instance owns two mask objects, we no longer need to dynamically allocate memory in tracefs_options_get_supported() and tracefs_options_get_enabled(). This will simplify the code on the caller side, since the user is no longer responsible for freeing those masks.
--- Documentation/libtracefs-option-get.txt | 4 +--- include/tracefs.h | 6 +++--- src/tracefs-instance.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-)
Documentation/libtracefs-option-get.txt +1 -3
@@ -52,14 +52,13 @@ EXAMPLE -- #include <tracefs.h> ... -struct tracefs_options_mask *options; +const struct tracefs_options_mask *options; ... options = tracefs_options_get_supported(NULL); if (!options) { /* Failed to get supported options */ } else { ... - free(options); } ... options = tracefs_options_get_enabled(NULL);
@@ -67,7 +66,6 @@ if (!options) { /* Failed to get options, enabled in the top instance */ } else { ... - free(options); } ...
include/tracefs.h +3 -3
@@ -132,11 +132,11 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);
src/tracefs-instance.c +12 -12
@@ -27,6 +27,8 @@ struct tracefs_instance {char *trace_dir;char *name;int flags; +struct tracefs_options_mask supported_opts; +struct tracefs_options_mask enabled_opts; };/**
@@ -695,9 +697,8 @@ static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *iDIR *dir =NULL ;long long val; - bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); -if (!bitmask) -return NULL ; + bitmask = enabled? &instance->enabled_opts : &instance->supported_opts; + dname =tracefs_instance_get_file (instance,"options" );if (!dname) goto error;
@@ -727,7 +728,6 @@ error:if (dir)closedir (dir);tracefs_put_tracing_file (dname); -free (bitmask);return NULL ; }
@@ -735,10 +735,10 @@ error:* tracefs_options_get_supported - Get all supported trace options in given instance * @instance: ftrace instance, can be NULL for the top instance * -* Returns allocated bitmask structure with all trace options, supported in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns bitmask structure with all trace options, supported in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) {return trace_get_options (instance, false); }
@@ -747,10 +747,10 @@ struct tracefs_options_mask *tracefs_options_get_supported(struct tracefs_instan* tracefs_options_get_enabled - Get all currently enabled trace options in given instance * @instance: ftrace instance, can be NULL for the top instance * -* Returns allocated bitmask structure with all trace options, enabled in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns allocated bitmask structure with all trace options, enabled in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) {return trace_get_options (instance, true); }
@@ -758,7 +758,7 @@ struct tracefs_options_mask *tracefs_options_get_enabled(struct tracefs_instancestatic int trace_config_option (struct tracefs_instance *instance,enum tracefs_option_id id,bool set) { -char *set_str = set ?"1" :"0" ; +const char *set_str = set ?"1" :"0" ;char file[PATH_MAX ];const char *name;
@@ -846,7 +846,7 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_o* Returns true if an option with given id is set in the bitmask, * false if it is not set. */ -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id) {if (id >TRACEFS_OPTION_INVALID )
Signature · 1 line
2.27.0
Re: [PATCH 5/6] libtracefs: Option's bit masks to be owned by the instance
Quoted 23 lines · Yordan Karadzhov (VMware)
If the instance owns two mask objects, we no longer need to dynamically allocate memory in
tracefs_options_get_supported()andtracefs_options_get_enabled(). This will simplify the code on the caller side, since the user is no longer responsible for freeing those masks.Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- Documentation/libtracefs-option-get.txt | 4 +--- include/tracefs.h | 6 +++--- src/tracefs-instance.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-)Documentation/libtracefs-option-get.txt +0
-1@@ -52,14 +52,13 @@ EXAMPLE -- #include <tracefs.h> -struct tracefs_options_mask *options;
Documentation/libtracefs-option-get.txt EXAMPLE · line 62
Quoted 10 lines
Documentation/libtracefs-option-get.txt +1
-1+const struct tracefs_options_mask *options; options = tracefs_options_get_supported(NULL); if (!options) { /* Failed to get supported options */ } else { - free(options); }
The description of the return value of tracefs_options_get_supported() and tracefs_options_get_enabled() should be changed also, as it suggests to free the return value;
Quoted 42 lines
options =
tracefs_options_get_enabled(NULL);Documentation/libtracefs-option-get.txt +0
-1@@ -67,7 +66,6 @@ if (!options) { /* Failed to get options, enabled in the top instance */ } else { - free(options); }include/tracefs.h +3
-3@@ -132,11 +132,11 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);src/tracefs-instance.c +2
-0@@ -27,6 +27,8 @@ struct tracefs_instance {char *trace_dir;char *name;int flags; +struct tracefs_options_mask supported_opts; +struct tracefs_options_mask enabled_opts; };/**
src/tracefs-instance.c static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *i · line 700
Quoted 7 lines
src/tracefs-instance.c +1
-3@@ -695,9 +697,8 @@ static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *iDIR *dir =NULL ;long long val; - bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); -if (!bitmask) -return NULL ; + bitmask = enabled? &instance->enabled_opts : &instance->supported_opts;
This makes the API not thread safe. There should be no problem with supported options, as they cannot be changed at runtime. I think it is OK to limit the APIs , as I see no use case to call them from multiple threads on the same instance, but this should be described in the documentation.
Quoted 15 lines
src/tracefs-instance.c +1
-1+ dname =tracefs_instance_get_file (instance,"options" );if (!dname) goto error;@@ -727,7 +728,6 @@ error:if (dir)closedir (dir);tracefs_put_tracing_file (dname); -free (bitmask);return NULL ; }@@ -735,10 +735,10 @@ error:* tracefs_options_get_supported - Get all supported trace options in given instance * @instance: ftrace instance, can be NULL for the top instance
src/tracefs-instance.c error: · line 743
Quoted 10 lines
src/tracefs-instance.c +3
-3* -* Returns allocated bitmask structure with all trace options, supported in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns bitmask structure with all trace options, supported in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) {return trace_get_options (instance, false);
As supported options cannot be changed at runtime, they can be read only once. The next calls can return the already initialised bitmask with supported options.
Quoted 36 lines
src/tracefs-instance.c +5
-6}@@ -747,10 +747,10 @@ struct tracefs_options_mask *tracefs_options_get_supported(struct tracefs_instan* tracefs_options_get_enabled - Get all currently enabled trace options in given instance * @instance: ftrace instance, can be NULL for the top instance * -* Returns allocated bitmask structure with all trace options, enabled in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns allocated bitmask structure with all trace options, enabled in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) {return trace_get_options (instance, true); }@@ -758,7 +758,7 @@ struct tracefs_options_mask *tracefs_options_get_enabled(struct tracefs_instancestatic int trace_config_option (struct tracefs_instance *instance,enum tracefs_option_id id,bool set) { -char *set_str = set ?"1" :"0" ; +const char *set_str = set ?"1" :"0" ;char file[PATH_MAX ];const char *name;@@ -846,7 +846,7 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_o* Returns true if an option with given id is set in the bitmask, * false if it is not set. */ -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id) {if (id >TRACEFS_OPTION_INVALID ) --2.27.0
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
Re: [PATCH 5/6] libtracefs: Option's bit masks to be owned by the instance
Quoted 83 lines · Tzvetomir Stoyanov
Quoted 22 lines · Yordan Karadzhov (VMware)
If the instance owns two mask objects, we no longer need to dynamically allocate memory in
tracefs_options_get_supported()andtracefs_options_get_enabled(). This will simplify the code on the caller side, since the user is no longer responsible for freeing those masks.Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- Documentation/libtracefs-option-get.txt | 4 +--- include/tracefs.h | 6 +++--- src/tracefs-instance.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-)Documentation/libtracefs-option-get.txt +0
-1@@ -52,14 +52,13 @@ EXAMPLE -- #include <tracefs.h> -struct tracefs_options_mask *options;Documentation/libtracefs-option-get.txt EXAMPLE · line 62
Quoted 10 lines
Documentation/libtracefs-option-get.txt +1
-1+const struct tracefs_options_mask *options; options = tracefs_options_get_supported(NULL); if (!options) { /* Failed to get supported options */ } else { - free(options); }The description of the return value of
tracefs_options_get_supported()andtracefs_options_get_enabled()should be changed also, as it suggests to free the return value;Quoted 45 lines
options = tracefs_options_get_enabled(NULL);Documentation/libtracefs-option-get.txt +0
-1@@ -67,7 +66,6 @@ if (!options) { /* Failed to get options, enabled in the top instance */ } else { - free(options); }include/tracefs.h +3
-3@@ -132,11 +132,11 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);src/tracefs-instance.c +2
-0@@ -27,6 +27,8 @@ struct tracefs_instance {char *trace_dir;char *name;int flags; +struct tracefs_options_mask supported_opts; +struct tracefs_options_mask enabled_opts; };/** @@ -695,9 +697,8 @@ static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *iDIR *dir =NULL ;long long val;
Quoted 9 lines
src/tracefs-instance.c static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *i · line 700
Quoted 4 lines
src/tracefs-instance.c +1
-3- bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); -if (!bitmask) -return NULL ; + bitmask = enabled? &instance->enabled_opts : &instance->supported_opts;This makes the API not thread safe. There should be no problem with supported options, as they cannot be changed at runtime. I think it is OK to limit the APIs , as I see no use case to call them from multiple threads on the same instance, but this should be described in the documentation.
Discussing is this API is thread safe or not is not really relevant. Even if you make tracefs_options_get_enabled() thread safe, this does not change anything since the options can be already changed by the time when you examine the bits of the mask.
Quoted 13 lines
Quoted 13 lines
src/tracefs-instance.c +1
-1+ dname =tracefs_instance_get_file (instance,"options" );if (!dname) goto error;@@ -727,7 +728,6 @@ error:if (dir)closedir (dir);tracefs_put_tracing_file (dname); -free (bitmask);return NULL ; }
Quoted 16 lines
src/tracefs-instance.c error: · line 743
Quoted 13 lines
src/tracefs-instance.c +3
-3@@ -735,10 +735,10 @@ error:* tracefs_options_get_supported - Get all supported trace options in given instance * @instance: ftrace instance, can be NULL for the top instance * -* Returns allocated bitmask structure with all trace options, supported in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns bitmask structure with all trace options, supported in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance) {return trace_get_options (instance, false);As supported options cannot be changed at runtime, they can be read only once. The next calls can return the already initialised bitmask with supported options.
If you already have the mask, you do not need to call this function second time.
May be if the user intentionally calls the function again, we should allow him to regenerate the mask.
What do you think?
Thanks!
Yordan
Quoted 38 lines
Quoted 37 lines
src/tracefs-instance.c +5
-6}@@ -747,10 +747,10 @@ struct tracefs_options_mask *tracefs_options_get_supported(struct tracefs_instan* tracefs_options_get_enabled - Get all currently enabled trace options in given instance * @instance: ftrace instance, can be NULL for the top instance * -* Returns allocated bitmask structure with all trace options, enabled in given -* instance, or NULL in case of an error. The returned structure must be freed with free() +* Returns allocated bitmask structure with all trace options, enabled in given instance, +* or NULL in case of an error. */ -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance) {return trace_get_options (instance, true); }@@ -758,7 +758,7 @@ struct tracefs_options_mask *tracefs_options_get_enabled(struct tracefs_instancestatic int trace_config_option (struct tracefs_instance *instance,enum tracefs_option_id id,bool set) { -char *set_str = set ?"1" :"0" ; +const char *set_str = set ?"1" :"0" ;char file[PATH_MAX ];const char *name;@@ -846,7 +846,7 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_o* Returns true if an option with given id is set in the bitmask, * false if it is not set. */ -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id) {if (id >TRACEFS_OPTION_INVALID ) --2.27.0
Re: [PATCH 5/6] libtracefs: Option's bit masks to be owned by the instance
Quoted 10 lines · On Mon, 5 Apr 2021 18:01:41 +0300 Yordan Karadzhov <[email protected]> wrote:
Quoted 5 lines
This makes the API not thread safe. There should be no problem with supported options, as they cannot be changed at runtime. I think it is OK to limit the APIs , as I see no use case to call them from multiple threads on the same instance, but this should be described in the documentation.
Discussing is this API is thread safe or not is not really relevant. Even if you make
tracefs_options_get_enabled()thread safe, this does not change anything since the options can be already changed by the time when you examine the bits of the mask.
You are correct, but you also do not want the update of the bitmask to be corrupted. Thus I think a pthread_mutex() should be added to the updating of the value, such that we don't have a read/modify/write corruption. Sure the reader of the mask may get some stale data if its being updated. But it shouldn't get corrupted data.
void tracefs_option_set(struct tracefs_options_mask *options, enum tracefs_option_id id)
{
if (options && id > TRACEFS_OPTION_INVALID)
options->mask |= (1ULL << (id - 1));
}If two threads are doing this to the same mask, you can have an option set that gets cleared by another writer, and even though the option is set after the call, it will be clear in the mask. That is, it's not stale data, it's incorrect data. That is, the readers after that corruption will get an option not set, even though it is.
-- Steve
Re: [PATCH 5/6] libtracefs: Option's bit masks to be owned by the instance
In reply to Yordan Karadzhov (VMware)Quoted 75 lines · Yordan Karadzhov (VMware)
If the instance owns two mask objects, we no longer need to dynamically allocate memory in
tracefs_options_get_supported()andtracefs_options_get_enabled(). This will simplify the code on the caller side, since the user is no longer responsible for freeing those masks.Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- Documentation/libtracefs-option-get.txt | 4 +--- include/tracefs.h | 6 +++--- src/tracefs-instance.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-)Documentation/libtracefs-option-get.txt +1
-3@@ -52,14 +52,13 @@ EXAMPLE -- #include <tracefs.h> -struct tracefs_options_mask *options; +const struct tracefs_options_mask *options; options = tracefs_options_get_supported(NULL); if (!options) { /* Failed to get supported options */ } else { - free(options); } options = tracefs_options_get_enabled(NULL);@@ -67,7 +66,6 @@ if (!options) { /* Failed to get options, enabled in the top instance */ } else { - free(options); }include/tracefs.h +3
-3@@ -132,11 +132,11 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);src/tracefs-instance.c +2
-0@@ -27,6 +27,8 @@ struct tracefs_instance {char *trace_dir;char *name;int flags; +struct tracefs_options_mask supported_opts; +struct tracefs_options_mask enabled_opts; };/**
src/tracefs-instance.c static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *i · line 700
Quoted 7 lines
src/tracefs-instance.c +1
-3@@ -695,9 +697,8 @@ static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *iDIR *dir =NULL ;long long val; - bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); -if (!bitmask) -return NULL ; + bitmask = enabled? &instance->enabled_opts : &instance->supported_opts;
The "instance" argument can be NULL, for the top instance. There should be global enabled_opts and supported_opts, to be used in that case.
[..]
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
Re: [PATCH 5/6] libtracefs: Option's bit masks to be owned by the instance
Quoted 79 lines · Tzvetomir Stoyanov
Quoted 77 lines · Yordan Karadzhov (VMware)
If the instance owns two mask objects, we no longer need to dynamically allocate memory in
tracefs_options_get_supported()andtracefs_options_get_enabled(). This will simplify the code on the caller side, since the user is no longer responsible for freeing those masks.Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- Documentation/libtracefs-option-get.txt | 4 +--- include/tracefs.h | 6 +++--- src/tracefs-instance.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-)Documentation/libtracefs-option-get.txt +1
-3@@ -52,14 +52,13 @@ EXAMPLE -- #include <tracefs.h> -struct tracefs_options_mask *options; +const struct tracefs_options_mask *options; options = tracefs_options_get_supported(NULL); if (!options) { /* Failed to get supported options */ } else { - free(options); } options = tracefs_options_get_enabled(NULL);@@ -67,7 +66,6 @@ if (!options) { /* Failed to get options, enabled in the top instance */ } else { - free(options); }include/tracefs.h +3
-3@@ -132,11 +132,11 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (struct tracefs_options_mask *options, +bool tracefs_option_is_set (const struct tracefs_options_mask *options,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id); -struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance); +const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);bool tracefs_option_is_enabled (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_enable (struct tracefs_instance *instance,enum tracefs_option_id id);int tracefs_option_diasble (struct tracefs_instance *instance,enum tracefs_option_id id);src/tracefs-instance.c +2
-0@@ -27,6 +27,8 @@ struct tracefs_instance {char *trace_dir;char *name;int flags; +struct tracefs_options_mask supported_opts; +struct tracefs_options_mask enabled_opts; };/** @@ -695,9 +697,8 @@ static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *iDIR *dir =NULL ;long long val;
Quoted 7 lines
src/tracefs-instance.c static struct tracefs_options_mask *trace_get_options(struct tracefs_instance *i · line 700
Quoted 4 lines
src/tracefs-instance.c +1
-3- bitmask =calloc (1 ,sizeof (struct tracefs_options_mask )); -if (!bitmask) -return NULL ; + bitmask = enabled? &instance->enabled_opts : &instance->supported_opts;The "instance" argument can be NULL, for the top instance. There should be global
enabled_optsandsupported_opts, to be used in that case.
Yes, this is a bug and I have it in v2 as well. Seems like I will need to send v3.
Thanks a lot!
Y.
Quoted 4 lines
[..]
PATCH 6/6 libtracefs: Rename tracefs_option_is_set()
In reply to Yordan Karadzhov (VMware)The old name of the function is potentially confusing. Indeed the function do not check anything about the option itself (is it supported or is it enabled). The function only check if inside the mask the bit that correspond to a given option is set.
--- include/tracefs.h | 4 ++-- src/tracefs-instance.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
include/tracefs.h +2 -2
@@ -132,8 +132,8 @@ enum tracefs_option_id {#define TRACEFS_OPTION_MAX (TRACEFS_OPTION_VERBOSE + 1)struct tracefs_options_mask ; -bool tracefs_option_is_set (const struct tracefs_options_mask *options, -enum tracefs_option_id id); +bool tracefs_option_mask_is_set (const struct tracefs_options_mask *options, +enum tracefs_option_id id);const struct tracefs_options_mask *tracefs_options_get_supported (struct tracefs_instance *instance);bool tracefs_option_is_supported (struct tracefs_instance *instance,enum tracefs_option_id id);const struct tracefs_options_mask *tracefs_options_get_enabled (struct tracefs_instance *instance);
src/tracefs-instance.c +3 -3
@@ -839,15 +839,15 @@ bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_o }/** -* tracefs_option_is_set - Check if given option is set in the bitmask +* tracefs_option_mask_is_set - Check if given option is set in the bitmask * @options: Options bitmask * @id: trace option id * * Returns true if an option with given id is set in the bitmask, * false if it is not set. */ -bool tracefs_option_is_set (const struct tracefs_options_mask *options, -enum tracefs_option_id id) +bool tracefs_option_mask_is_set (const struct tracefs_options_mask *options, +enum tracefs_option_id id) {if (id >TRACEFS_OPTION_INVALID )return options->mask & (1ULL << (id -1 ));
Signature · 1 line
2.27.0