X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=a092d940f422979b724fa5a61a72365c54b83ad7;hp=08139e5e2cb23d691c92c6800cef841e2362bc57;hb=81684730b3134c61ca310bf26733c01d783103d7;hpb=2daf65025e0fe5c15179dc4bfb26f875e3d31a26 diff --git a/src/common/utils.c b/src/common/utils.c index 08139e5e2..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': @@ -1646,38 +1673,3 @@ int utils_show_help(int section, const char *page_name, end: return ret; } - -LTTNG_HIDDEN -int timespec_to_ms(struct timespec ts, unsigned long *ms) -{ - unsigned long res, remain_ms; - - if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { - errno = EOVERFLOW; - return -1; /* multiplication overflow */ - } - res = ts.tv_sec * MSEC_PER_SEC; - remain_ms = ULONG_MAX - res; - if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { - errno = EOVERFLOW; - return -1; /* addition overflow */ - } - res += ts.tv_nsec / NSEC_PER_MSEC; - *ms = res; - return 0; -} - -LTTNG_HIDDEN -struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) -{ - uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + - (uint64_t) t1.tv_nsec; - uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + - (uint64_t) t2.tv_nsec; - uint64_t diff = max(ts1, ts2) - min(ts1, ts2); - struct timespec res; - - res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; - res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; - return res; -}