lttng cli: Accept human readable sizes for --subbuf-size
A message that is not in the archive (sent off-list or not yet received): [email protected]
I wonder if this "string parsing human readable size" should go in libcommon. I can *easily* see that code being used in sessiond and relayd if some bytes size options are used.
Basically, you could simply move that code in src/common/utils.c/.h
Comments below:
Simon Marchi:
Quoted 99 lines
--subbuf-size accepts sizes such as:
- 123 -> 123
- 123k -> 123 * 1024
- 123M -> 123 * 1024 * 1024
- 123G -> 123 * 1024 * 1024 * 1024It uses the new
parse_size_suffixfunction, which could probably be used at other places, such as tracefile size.Unit tests are included.
Signed-off-by Simon Marchi <[email protected]>---
New in v3:
* Changes following Mathieu Desnoyers' comments.
- Function nameparse_human_size->parse_size_suffix
- Replace division for bit shift, multipliers by log2
- Coding style issues.
- strtoll -> strtoull
* Unit tests (depends on fix of object names clash)src/bin/lttng/commands/enable_channels.c | 21 ++++- src/bin/lttng/utils.c | 123 +++++++++++++++++++++++++++++- src/bin/lttng/utils.h | 6 ++ tests/unit/Makefile.am | 15 +++- tests/unit/test_parse_size_suffix.c | 72 +++++++++++++++++ 5 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 tests/unit/test_parse_size_suffix.csrc/bin/lttng/commands/enable_channels.c +18
-3@@ -66,7 +66,7 @@ static struct poptOption long_options[] = { {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, - {"subbuf-size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0}, + {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},@@ -289,6 +289,7 @@ int cmd_enable_channels(int argc, const char **argv)int opt, ret =CMD_SUCCESS ;static poptContext pc;char *session_name =NULL ; +char *opt_arg =NULL ;init_channel_config ();@@ -309,8 +310,22 @@ int cmd_enable_channels(int argc, const char **argv)DBG ("Channel set to overwrite" );break ;case OPT_SUBBUF_SIZE : -/* TODO Replace atol with strtol and check for errors */ - chan.attr.subbuf_size =atol (poptGetOptArg (pc)); +/* Parse the size */ + opt_arg =poptGetOptArg (pc); +if (parse_size_suffix (opt_arg, &chan.attr.subbuf_size) <0 ) { +ERR ("Wrong value the --subbuf-size parameter: %s" , opt_arg); + ret =CMD_ERROR ; + goto end; + } + +/* Check if power of 2 */ +if ((chan.attr.subbuf_size -1 ) & chan.attr.subbuf_size) { +ERR ("The subbuf size is not a power of 2: %" PRIu64" (%s)" , + chan.attr.subbuf_size, opt_arg); + ret =CMD_ERROR ; + goto end; + } +DBG ("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);break ;case OPT_NUM_SUBBUF :src/bin/lttng/utils.c +5
-1@@ -16,9 +16,11 @@*/ #define _GNU_SOURCE -#include <stdlib.h> +#include <assert.h> #include <ctype.h> #include <limits.h> +#include <regex.h> +#include <stdlib.h> #include <common/error.h> @@ -77,3 +79,122 @@ void list_cmd_options(FILE *ofp, struct poptOption *options) } } } + +/**
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 93
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+* Prints the error message corresponding to a regex error code. +* +* @param errcode The error code. +* @param regex The regex object that produced the error code. +*/ +static void regex_print_error (int errcode,regex_t *regex) +{ +/* Get length of error message and allocate accordingly */ +size_t length =regerror (errcode, regex,NULL ,0 ); +char *buffer =malloc (length);
I know it's not _necessary_ but I would prefer to have the same std across the code. So, we usually declare the variables without assignation (that comes from a function call), then use assert() for given params (if needed) and finally used them.
Here, declare length and buffer without assignation. Assert on "regex != NULL" because a NULL value seems to me that it might break (or segfault). Finally, set length, check the return error code and set buffer checking also for errors.
Use zmalloc also please.
Quoted 17 lines
src/bin/lttng/utils.c +17
-0+ +if (!buffer) { +ERR ("regex_print_error: malloc failed" ); +return ; + } + +/* Get and print error message */ +regerror (errcode, regex, buffer, length); +ERR ("regex error: %s\n" , buffer); +free (buffer); + +} + +/** +* Parse a string that represents a size in human readable format. It +* supports decimal integers suffixed by 'k', 'M' or 'G'. +*
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 120
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+* The suffix multiply the integer by: +* 'k': 1024 +* 'M': 1024 * 1024 +* 'G': 1024 * 1024 * 1024 +* +* @param str The string to parse. +* @param size Pointer to a size_t that will be filled with the +* resulting size. +* +* @return 0 on success, -1 on failure.
No tabs is needed here after "return".
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 130
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+*/ +int parse_size_suffix (char *str,uint64_t *size) +{ +regex_t regex; +int ret; +const int nmatch =2 ; +regmatch_t matches[nmatch]; + +if (!str) +return 0 ;
*Always* {} around if/else statement. I know... the check patch script is not detecting it right but the CodingStyle mentions it.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 138
Quoted 8 lines
src/bin/lttng/utils.c +8
-0+ +/* Compile regex */ + ret =regcomp (®ex,"^[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$" ,0 ); + +/* Check for regex error */ +if (ret !=0 ) { +regex_print_error (ret, ®ex); +
Useless space.
Quoted 6 lines
src/bin/lttng/utils.c +6
-0+ ret =-1 ; + goto end; + } + +/* Match regex */ + ret =regexec (®ex, str, nmatch, matches,0 );
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 154
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+if (ret !=0 ) { +/* There is no match (or error) */ + ret =-1 ; + goto end; + } + +/* There is a match ! */ +unsigned long long base_size; +long shift =0 ; + errno =0 ;
Why is errno set to 0 ?
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 155
Quoted 1 line
src/bin/lttng/utils.c +1
-0+ base_size =strtoll (str,NULL ,10 );
I think you want "strtoUll" here.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 156
Quoted 1 line
src/bin/lttng/utils.c +1
-0+
No space.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 158
Quoted 2 lines
src/bin/lttng/utils.c +2
-0+/* Check result of conversion */ +if (errno !=0 ) {
You should check base_size == ULLONG_MAX which is the return value of an overflow meaning an error instead of errno.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 165
Quoted 7 lines
src/bin/lttng/utils.c +7
-0+PERROR ("strtoull" ); + ret =-1 ; + goto end; + } + +/* Check if there is a suffix */ +regmatch_t suffix_match = matches[1 ];
Declare first then assign. You can do this is suffix_match was used in a smaller scope (inside a if block for instance).
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 170
Quoted 5 lines
src/bin/lttng/utils.c +5
-0+if (suffix_match.rm_eo - suffix_match.rm_so ==1 ) { +switch (*(str + suffix_match.rm_so)) { +case 'K' : +case 'k' : + shift =KIBI_LOG2 ;
I wonder if these *_LOG2 should go in default.h ? Thoughts?
Quoted 18 lines
src/bin/lttng/utils.c +18
-0+break ; +case 'M' : + shift =MEBI_LOG2 ; +break ; +case 'G' : + shift =GIBI_LOG2 ; +break ; +default : +ERR ("parse_human_size: invalid suffix" ); + ret =-1 ; + goto end; + } + } + + *size = base_size << shift; + +/* Check for overflow */ +if ((*size >> shift) != base_size) {
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 198
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+ERR ("parse_size_suffix: oops, overflow detected." ); + ret =-1 ; + goto end; + } + + ret =0 ; + goto end; + +end: +regfree (®ex);
If regex was uninit like for instance on regcomp error, does this call behaves well ?
Thanks!
David
Quoted 130 lines
src/bin/lttng/utils.c +2
-0+return ret; +}src/bin/lttng/utils.h +6
-0@@ -19,8 +19,14 @@#define _LTTNG_UTILS_H#include <popt.h> +#include <stdint.h> + +#define KIBI_LOG2 10 +#define MEBI_LOG2 20 +#define GIBI_LOG2 30char *get_session_name (void );void list_cmd_options (FILE *ofp,struct poptOption *options); +int parse_size_suffix (char *str,uint64_t *size);#endif /* _LTTNG_UTILS_H */ tests/unit/Makefile.am +12
-3@@ -14,10 +14,11 @@ LIBCOMMON=$(top_builddir)/src/common/libcommon.la LIBSESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la LIBHASHTABLE=$(top_builddir)/src/common/hashtable/libhashtable.la +# Define test programs +noinst_PROGRAMS = test_uri test_session test_kernel_data test_parse_size_suffix + if HAVE_LIBLTTNG_UST_CTL -noinst_PROGRAMS = test_uri test_session test_ust_data test_kernel_data -else -noinst_PROGRAMS = test_uri test_session test_kernel_data +noinst_PROGRAMS += test_ust_data endif # URI unit tests@@ -69,3 +70,11 @@ test_kernel_data_SOURCES = test_kernel_data.c test_kernel_data_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBSESSIOND_COMM) $(LIBHASHTABLE) \ -lrt test_kernel_data_LDADD += $(KERN_DATA_TRACE) + +# parse_size_suffix unit test +PARSE_SIZE_SUFFIX=$(top_srcdir)/src/bin/lttng/utils.o \ + $(top_srcdir)/src/bin/lttng/conf.o + +test_parse_size_suffix_SOURCES = test_parse_size_suffix.c +test_parse_size_suffix_LDADD = $(LIBTAP) +test_parse_size_suffix_LDADD += $(PARSE_SIZE_SUFFIX)tests/unit/test_parse_size_suffix.c new file +72
-0@@ -0,0 +1,72 @@ +/* +* Copyright (C) - 2013 Simon Marchi <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by as +* published by the Free Software Foundation; only version 2 of the License. +* +* This program is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., 51 +* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <assert.h> +#include <string.h> + +#include <tap/tap.h> + +#include <src/bin/lttng/utils.h> + +/* For lttngerr.h */ +int lttng_opt_quiet =1 ; +int lttng_opt_verbose =3 ; + +/* Valid test cases */ +static char *valid_tests_inputs[] = {"0" ,"1234" ,"16k" ,"128K" ,"32M" ,"1024G" }; +static uint64_t valid_tests_expected_results[] = {0 ,1234 ,16384 ,131072 ,33554432 ,1099511627776 }; +static const int num_valid_tests =sizeof (valid_tests_inputs) /sizeof (valid_tests_inputs[0 ]); + +/* Invalid test cases */ +static char *invalid_tests_inputs[] = {"" ,"-1" ,"k" ,"4611686018427387904G" }; +static const int num_invalid_tests =sizeof (invalid_tests_inputs) /sizeof (invalid_tests_inputs[0 ]); + +void test_parse_size_suffix (void ) +{ +uint64_t result; +int ret; +int i; + +// Test valid cases +for (i =0 ; i < num_valid_tests; i++) { +char name[100 ]; +sprintf (name,"testing %s" , valid_tests_inputs[i]); + + ret =parse_size_suffix (valid_tests_inputs[i], &result); +ok (ret ==0 && result == valid_tests_expected_results[i], name); + } + +// Test invalid cases +for (i =0 ; i < num_invalid_tests; i++) { +char name[100 ]; +sprintf (name,"testing %s" , invalid_tests_inputs[i]); + + ret =parse_size_suffix (invalid_tests_inputs[i], &result); +ok (ret !=0 , name); + } +} + +int main (int argc,char **argv) +{ +plan_tests (num_valid_tests + num_invalid_tests); + +diag ("parse_size_suffix tests" ); + +test_parse_size_suffix (); + +return exit_status (); +}
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 196 lines · * David Goulet ([email protected]) wrote:
I wonder if this "string parsing human readable size" should go in libcommon. I can *easily* see that code being used in sessiond and relayd if some bytes size options are used.
Basically, you could simply move that code in src/common/utils.c/.h
Comments below:
Simon Marchi:
Quoted 99 lines
--subbuf-size accepts sizes such as:
- 123 -> 123
- 123k -> 123 * 1024
- 123M -> 123 * 1024 * 1024
- 123G -> 123 * 1024 * 1024 * 1024It uses the new
parse_size_suffixfunction, which could probably be used at other places, such as tracefile size.Unit tests are included.
Signed-off-by Simon Marchi <[email protected]>---
New in v3:
* Changes following Mathieu Desnoyers' comments.
- Function nameparse_human_size->parse_size_suffix
- Replace division for bit shift, multipliers by log2
- Coding style issues.
- strtoll -> strtoull
* Unit tests (depends on fix of object names clash)src/bin/lttng/commands/enable_channels.c | 21 ++++- src/bin/lttng/utils.c | 123 +++++++++++++++++++++++++++++- src/bin/lttng/utils.h | 6 ++ tests/unit/Makefile.am | 15 +++- tests/unit/test_parse_size_suffix.c | 72 +++++++++++++++++ 5 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 tests/unit/test_parse_size_suffix.csrc/bin/lttng/commands/enable_channels.c +18
-3@@ -66,7 +66,7 @@ static struct poptOption long_options[] = { {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, - {"subbuf-size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0}, + {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},@@ -289,6 +289,7 @@ int cmd_enable_channels(int argc, const char **argv)int opt, ret =CMD_SUCCESS ;static poptContext pc;char *session_name =NULL ; +char *opt_arg =NULL ;init_channel_config ();@@ -309,8 +310,22 @@ int cmd_enable_channels(int argc, const char **argv)DBG ("Channel set to overwrite" );break ;case OPT_SUBBUF_SIZE : -/* TODO Replace atol with strtol and check for errors */ - chan.attr.subbuf_size =atol (poptGetOptArg (pc)); +/* Parse the size */ + opt_arg =poptGetOptArg (pc); +if (parse_size_suffix (opt_arg, &chan.attr.subbuf_size) <0 ) { +ERR ("Wrong value the --subbuf-size parameter: %s" , opt_arg); + ret =CMD_ERROR ; + goto end; + } + +/* Check if power of 2 */ +if ((chan.attr.subbuf_size -1 ) & chan.attr.subbuf_size) { +ERR ("The subbuf size is not a power of 2: %" PRIu64" (%s)" , + chan.attr.subbuf_size, opt_arg); + ret =CMD_ERROR ; + goto end; + } +DBG ("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);break ;case OPT_NUM_SUBBUF :src/bin/lttng/utils.c +5
-1@@ -16,9 +16,11 @@*/ #define _GNU_SOURCE -#include <stdlib.h> +#include <assert.h> #include <ctype.h> #include <limits.h> +#include <regex.h> +#include <stdlib.h> #include <common/error.h> @@ -77,3 +79,122 @@ void list_cmd_options(FILE *ofp, struct poptOption *options) } } } + +/** src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 93
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+* Prints the error message corresponding to a regex error code. +* +* @param errcode The error code. +* @param regex The regex object that produced the error code. +*/ +static void regex_print_error (int errcode,regex_t *regex) +{ +/* Get length of error message and allocate accordingly */ +size_t length =regerror (errcode, regex,NULL ,0 ); +char *buffer =malloc (length);I know it's not _necessary_ but I would prefer to have the same std across the code. So, we usually declare the variables without assignation (that comes from a function call), then use
assert()for given params (if needed) and finally used them.Here, declare length and buffer without assignation. Assert on "regex != NULL" because a NULL value seems to me that it might break (or segfault). Finally, set length, check the return error code and set buffer checking also for errors.
Use zmalloc also please.
Quoted 17 lines
src/bin/lttng/utils.c +17
-0+ +if (!buffer) { +ERR ("regex_print_error: malloc failed" ); +return ; + } + +/* Get and print error message */ +regerror (errcode, regex, buffer, length); +ERR ("regex error: %s\n" , buffer); +free (buffer); + +} + +/** +* Parse a string that represents a size in human readable format. It +* supports decimal integers suffixed by 'k', 'M' or 'G'. +* src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 120
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+* The suffix multiply the integer by: +* 'k': 1024 +* 'M': 1024 * 1024 +* 'G': 1024 * 1024 * 1024 +* +* @param str The string to parse. +* @param size Pointer to a size_t that will be filled with the +* resulting size. +* +* @return 0 on success, -1 on failure. No tabs is needed here after "return".
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 130
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+*/ +int parse_size_suffix (char *str,uint64_t *size) +{ +regex_t regex; +int ret; +const int nmatch =2 ; +regmatch_t matches[nmatch]; + +if (!str) +return 0 ;*Always* {} around if/else statement. I know... the check patch script is not detecting it right but the CodingStyle mentions it.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 138
Quoted 8 lines
src/bin/lttng/utils.c +8
-0+ +/* Compile regex */ + ret =regcomp (®ex,"^[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$" ,0 ); + +/* Check for regex error */ +if (ret !=0 ) { +regex_print_error (ret, ®ex); +Useless space.
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+ ret =-1 ; + goto end; + } + +/* Match regex */ + ret =regexec (®ex, str, nmatch, matches,0 ); +if (ret !=0 ) { +/* There is no match (or error) */ + ret =-1 ; + goto end;
Quoted 8 lines
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 154
Quoted 6 lines
src/bin/lttng/utils.c +6
-0+ } + +/* There is a match ! */ +unsigned long long base_size; +long shift =0 ; + errno =0 ;Why is errno set to 0 ?
see strtoull(3).
Quoted 7 lines
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 155
Quoted 2 lines
src/bin/lttng/utils.c +1
-0+ base_size =strtoll (str,NULL ,10 );I think you want "strtoUll" here.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 156
Quoted 1 line
src/bin/lttng/utils.c +1
-0+
Quoted 7 lines
No space.
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 158
Quoted 2 lines
src/bin/lttng/utils.c +2
-0+/* Check result of conversion */ +if (errno !=0 ) {You should check
base_size==ULLONG_MAXwhich is the return value of an overflow meaning an error instead of errno.
wrong, see strtoull(3).
Quoted 12 lines
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 165
Quoted 8 lines
src/bin/lttng/utils.c +7
-0+PERROR ("strtoull" ); + ret =-1 ; + goto end; + } + +/* Check if there is a suffix */ +regmatch_t suffix_match = matches[1 ];Declare first then assign. You can do this is
suffix_matchwas used in a smaller scope (inside a if block for instance).
Quoted 7 lines
src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 170
Quoted 5 lines
src/bin/lttng/utils.c +5
-0+if (suffix_match.rm_eo - suffix_match.rm_so ==1 ) { +switch (*(str + suffix_match.rm_so)) { +case 'K' : +case 'k' : + shift =KIBI_LOG2 ;I wonder if these
*_LOG2should go indefault.h? Thoughts?
could. no strong feeling. Thanks,
Mathieu
Quoted 171 lines
Quoted 19 lines
src/bin/lttng/utils.c +18
-0+break ; +case 'M' : + shift =MEBI_LOG2 ; +break ; +case 'G' : + shift =GIBI_LOG2 ; +break ; +default : +ERR ("parse_human_size: invalid suffix" ); + ret =-1 ; + goto end; + } + } + + *size = base_size << shift; + +/* Check for overflow */ +if ((*size >> shift) != base_size) {src/bin/lttng/utils.c void list_cmd_options(FILE *ofp, struct poptOption *options) · line 198
Quoted 10 lines
src/bin/lttng/utils.c +10
-0+ERR ("parse_size_suffix: oops, overflow detected." ); + ret =-1 ; + goto end; + } + + ret =0 ; + goto end; + +end: +regfree (®ex);If regex was uninit like for instance on regcomp error, does this call behaves well ?
Thanks!
DavidQuoted 120 lines
src/bin/lttng/utils.c +2
-0+return ret; +}src/bin/lttng/utils.h +6
-0@@ -19,8 +19,14 @@#define _LTTNG_UTILS_H#include <popt.h> +#include <stdint.h> + +#define KIBI_LOG2 10 +#define MEBI_LOG2 20 +#define GIBI_LOG2 30char *get_session_name (void );void list_cmd_options (FILE *ofp,struct poptOption *options); +int parse_size_suffix (char *str,uint64_t *size);#endif /* _LTTNG_UTILS_H */ tests/unit/Makefile.am +12
-3@@ -14,10 +14,11 @@ LIBCOMMON=$(top_builddir)/src/common/libcommon.la LIBSESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la LIBHASHTABLE=$(top_builddir)/src/common/hashtable/libhashtable.la +# Define test programs +noinst_PROGRAMS = test_uri test_session test_kernel_data test_parse_size_suffix + if HAVE_LIBLTTNG_UST_CTL -noinst_PROGRAMS = test_uri test_session test_ust_data test_kernel_data -else -noinst_PROGRAMS = test_uri test_session test_kernel_data +noinst_PROGRAMS += test_ust_data endif # URI unit tests@@ -69,3 +70,11 @@ test_kernel_data_SOURCES = test_kernel_data.c test_kernel_data_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBSESSIOND_COMM) $(LIBHASHTABLE) \ -lrt test_kernel_data_LDADD += $(KERN_DATA_TRACE) + +# parse_size_suffix unit test +PARSE_SIZE_SUFFIX=$(top_srcdir)/src/bin/lttng/utils.o \ + $(top_srcdir)/src/bin/lttng/conf.o + +test_parse_size_suffix_SOURCES = test_parse_size_suffix.c +test_parse_size_suffix_LDADD = $(LIBTAP) +test_parse_size_suffix_LDADD += $(PARSE_SIZE_SUFFIX)tests/unit/test_parse_size_suffix.c new file +62
-0@@ -0,0 +1,72 @@ +/* +* Copyright (C) - 2013 Simon Marchi <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by as +* published by the Free Software Foundation; only version 2 of the License. +* +* This program is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., 51 +* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <assert.h> +#include <string.h> + +#include <tap/tap.h> + +#include <src/bin/lttng/utils.h> + +/* For lttngerr.h */ +int lttng_opt_quiet =1 ; +int lttng_opt_verbose =3 ; + +/* Valid test cases */ +static char *valid_tests_inputs[] = {"0" ,"1234" ,"16k" ,"128K" ,"32M" ,"1024G" }; +static uint64_t valid_tests_expected_results[] = {0 ,1234 ,16384 ,131072 ,33554432 ,1099511627776 }; +static const int num_valid_tests =sizeof (valid_tests_inputs) /sizeof (valid_tests_inputs[0 ]); + +/* Invalid test cases */ +static char *invalid_tests_inputs[] = {"" ,"-1" ,"k" ,"4611686018427387904G" }; +static const int num_invalid_tests =sizeof (invalid_tests_inputs) /sizeof (invalid_tests_inputs[0 ]); + +void test_parse_size_suffix (void ) +{ +uint64_t result; +int ret; +int i; + +// Test valid cases +for (i =0 ; i < num_valid_tests; i++) { +char name[100 ]; +sprintf (name,"testing %s" , valid_tests_inputs[i]); + + ret =parse_size_suffix (valid_tests_inputs[i], &result); +ok (ret ==0 && result == valid_tests_expected_results[i], name); + } + +// Test invalid cases +for (i =0 ; i < num_invalid_tests; i++) { +char name[100 ]; +sprintf (name,"testing %s" , invalid_tests_inputs[i]); + + ret =parse_size_suffix (invalid_tests_inputs[i], &result); +ok (ret !=0 , name); + } +} +tests/unit/test_parse_size_suffix.c line 72
Quoted 10 lines
tests/unit/test_parse_size_suffix.c new file +10
-0+int main (int argc,char **argv) +{ +plan_tests (num_valid_tests + num_invalid_tests); + +diag ("parse_size_suffix tests" ); + +test_parse_size_suffix (); + +return exit_status (); +}_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 22 lines · David Goulet
Quoted 6 lines
(quoted diff) +2
-0@@ -77,3 +79,122 @@ void list_cmd_options(FILE *ofp, struct poptOption *options) } } } + +/**Review void list_cmd_options(FILE *ofp, struct poptOption *options) · line 93
Quoted 10 lines
(quoted diff) +10
-0+ * Prints the error message corresponding to a regex error code. + * + * @param errcode The error code. + * @param regex The regex object that produced the error code. + */ +static void regex_print_error(int errcode, regex_t *regex) +{ + /* Get length of error message and allocate accordingly */ + size_t length = regerror(errcode, regex, NULL, 0); + char *buffer = malloc(length);I know it's not _necessary_ but I would prefer to have the same std across the code. So, we usually declare the variables without assignation (that comes from a function call), then use
assert()for given params (if needed) and finally used them.
Quoted 6 lines
Here, declare length and buffer without assignation. Assert on "regex != NULL" because a NULL value seems to me that it might break (or segfault). Finally, set length, check the return error code and set buffer checking also for errors.
Use zmalloc also please.
Ok, no problem.
There is nothing specified for the return value of regerror so there is nothing to check. Actually, it does abort() if you pass a wrong error code...
Quoted 1 line
I wonder if these
*_LOG2should go indefault.h? Thoughts?
I see default.h as a place you put default arbitrary values for buffer sizes, paths, names, etc. KIBI_LOG2 and al are not values you decide. I think they are fine in utils.h.
Quoted 2 lines
If regex was uninit like for instance on regcomp error, does this call behaves well ?
Good catch, changed.
"Ack" for all other comments.
Thanks,
Simon