From: Jonathan Rajotte Date: Fri, 14 Dec 2018 21:32:12 +0000 (-0500) Subject: Support minute and hour as time suffixes X-Git-Tag: v2.12.0-rc1~661 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=81684730b3134c61ca310bf26733c01d783103d7;p=lttng-tools.git Support minute and hour as time suffixes utils_parse_time_suffix now support the following suffix: "us" for microsecond, "ms" for millisecond, "s" for second, "m" for minute, "h" for hour This removes the use of "m" for milliseconds and "u" for microseconds. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng-sessiond/thread-utils.c b/src/bin/lttng-sessiond/thread-utils.c index 549cd69f8..16ae9d692 100644 --- a/src/bin/lttng-sessiond/thread-utils.c +++ b/src/bin/lttng-sessiond/thread-utils.c @@ -22,8 +22,6 @@ #include #include -#define USEC_PER_SEC 1000000 - /* * Quit pipe for all threads. This permits a single cancellation point * for all threads when receiving an event on the pipe. diff --git a/src/common/time.h b/src/common/time.h index 894ab9259..bb0991de6 100644 --- a/src/common/time.h +++ b/src/common/time.h @@ -22,10 +22,18 @@ #include #include -#define MSEC_PER_SEC 1000ULL -#define NSEC_PER_SEC 1000000000ULL -#define NSEC_PER_MSEC 1000000ULL -#define NSEC_PER_USEC 1000ULL +#define MSEC_PER_SEC 1000ULL +#define NSEC_PER_SEC 1000000000ULL +#define NSEC_PER_MSEC 1000000ULL +#define NSEC_PER_USEC 1000ULL +#define USEC_PER_SEC 1000000ULL +#define USEC_PER_MSEC 1000ULL + +#define SEC_PER_MINUTE 60ULL +#define MINUTE_PER_HOUR 60ULL + +#define USEC_PER_MINUTE (USEC_PER_SEC * SEC_PER_MINUTE) +#define USEC_PER_HOURS (USEC_PER_MINUTE * MINUTE_PER_HOUR) bool locale_supports_utf8(void); diff --git a/src/common/utils.c b/src/common/utils.c index 3dc2d9297..a092d940f 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1104,12 +1104,19 @@ end: /** * Parse a string that represents a time in human readable format. It - * supports decimal integers suffixed by 's', 'u', 'm', 'us', and 'ms'. + * supports decimal integers suffixed by: + * "us" for microsecond, + * "ms" for millisecond, + * "s" for second, + * "m" for minute, + * "h" for hour * * The suffix multiply the integer by: - * 'u'/'us': 1 - * 'm'/'ms': 1000 - * 's': 1000000 + * "us" : 1 + * "ms" : 1000 + * "s" : 1000000 + * "m" : 60000000 + * "h" : 3600000000 * * Note that unit-less numbers are assumed to be microseconds. * @@ -1124,7 +1131,7 @@ int utils_parse_time_suffix(char const * const str, uint64_t * const time_us) { int ret; uint64_t base_time; - long multiplier = 1; + uint64_t multiplier = 1; const char *str_end; char *num_end; @@ -1161,17 +1168,37 @@ int utils_parse_time_suffix(char const * const str, uint64_t * const time_us) /* Check if a prefix is present. */ switch (*num_end) { case 'u': - multiplier = 1; - /* Skip another letter in the 'us' case. */ - num_end += (*(num_end + 1) == 's') ? 2 : 1; + /* + * Microsecond (us) + * + * Skip the "us" if the string matches the "us" suffix, + * otherwise let the check for the end of the string handle + * the error reporting. + */ + if (*(num_end + 1) == 's') { + num_end += 2; + } break; case 'm': - multiplier = 1000; - /* Skip another letter in the 'ms' case. */ - num_end += (*(num_end + 1) == 's') ? 2 : 1; + if (*(num_end + 1) == 's') { + /* Millisecond (ms) */ + multiplier = USEC_PER_MSEC; + /* Skip the 's' */ + num_end++; + } else { + /* Minute (m) */ + multiplier = USEC_PER_MINUTE; + } + num_end++; break; case 's': - multiplier = 1000000; + /* Second */ + multiplier = USEC_PER_SEC; + num_end++; + break; + case 'h': + /* Hour */ + multiplier = USEC_PER_HOURS; num_end++; break; case '\0': diff --git a/tests/unit/test_utils_parse_time_suffix.c b/tests/unit/test_utils_parse_time_suffix.c index 96482769b..e7e782d26 100644 --- a/tests/unit/test_utils_parse_time_suffix.c +++ b/tests/unit/test_utils_parse_time_suffix.c @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -37,16 +38,25 @@ struct valid_test_input { static struct valid_test_input valid_tests_inputs[] = { { "0", 0 }, { "1234", 1234 }, - { "0u", 0 }, - { "1234u", 1234 }, - { "16m", 16000 }, - { "128m", 128000 }, + { "1234us", 1234 }, + { "16ms", 16000 }, + { "128ms", 128000 }, { "32s", 32000000 }, + { "1m", 60000000 }, + { "20m", 1200000000 }, + { "1h", 3600000000 }, + { "5h", 18000000000 }, { "00", 0 }, - { "0m", 0 }, + { "0us", 0 }, + { "0ms", 0 }, { "0s", 0 }, - { "00m", 0 }, + { "0m", 0 }, + { "0h", 0 }, + { "00us", 0 }, + { "00ms", 0 }, { "00s", 0 }, + { "00m", 0 }, + { "00h", 0 }, { "12ms", 12000 }, { "3597us", 3597 }, { "+5", 5 }, @@ -79,6 +89,14 @@ static char *invalid_tests_inputs[] = { "14ns", "14ms garbage after value", "0x14s", + "0u", + "5mS", + "5Ms", + "12ussr", + "67msrp", + "14si", + "12mo", + "53hi", }; static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); @@ -90,11 +108,10 @@ static void test_utils_parse_time_suffix(void) /* Test valid cases */ for (i = 0; i < num_valid_tests; i++) { - char name[100]; - - sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); + char name[256]; ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result); + sprintf(name, "valid test case: %s expected %" PRIu64, valid_tests_inputs[i].input, result); ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name); }