tst_timer.h: Add timeval helpers
PATCH 1/2 tst_timer.h: Add timeval helpers
--- include/tst_timer.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
include/tst_timer.h +48 -0
@@ -51,6 +51,22 @@ static inline long long tst_timespec_to_ms(struct timespec t) }/* +* Converts timeval to microseconds. +*/ +static inline long long tst_timeval_to_us (struct timeval t) +{ +return t.tv_sec *1000000 + t.tv_usec; +} + +/* +* Converts timeval to miliseconds. +*/ +static inline long long tst_timeval_to_ms (struct timeval t) +{ +return t.tv_sec *1000 + (t.tv_usec +500 ) /1000 ; +} + +/* * Converts ms to struct timeval */ static inline struct timeval tst_ms_to_timeval (long long ms)
@@ -137,6 +153,38 @@ static inline long long tst_timespec_diff_ms(struct timespec t1, }/* +* Returns difference between two timeval structures. +*/ +static inline struct timeval tst_timeval_diff (struct timeval t1, +struct timeval t2) +{ +struct timeval res; + + res.tv_sec = t1.tv_sec - t2.tv_sec; + +if (t1.tv_usec < t2.tv_usec) { + res.tv_sec--; + res.tv_usec =1000000 - (t2.tv_usec - t1.tv_usec); + }else { + res.tv_usec = t1.tv_usec - t2.tv_usec; + } + +return res; +} + +static inline long long tst_timeval_diff_us (struct timeval t1, +struct timeval t2) +{ +return tst_timeval_to_us (tst_timeval_diff (t1, t2)); +} + +static inline long long tst_timeval_diff_ms (struct timeval t1, +struct timeval t2) +{ +return tst_timeval_to_ms (tst_timeval_diff (t1, t2)); +} + +/* * Returns absolute value of difference between two timespec structures. */ static inline struct timespec tst_timespec_abs_diff(struct timespec t1,
Signature · 1 line
2.13.0
RFC PATCH 2/2 tst_timer.h: Make time conversions saturated
The conversions of struct timeval and struct timespec to us and ms are now saturated so instead of overflowing/underflowing long long these return LLONG_MAX/LLONG_MIN.
This commit also adds tst_us_to_timespec(), tst_ms_to_timespec() and newlib_tests/tst_timer test to assert that the saturated conversion actually works.
--- include/tst_timer.h | 129 ++++++++++++++++++++++++++++++++++++++++--- lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/tst_timer.c | 126 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 248 insertions(+), 8 deletions(-) create mode 100644 lib/newlib_tests/tst_timer.c
include/tst_timer.h +121 -8
@@ -33,37 +33,124 @@#include <sys/time.h> #include <time.h> +#include <limits.h> + +#define LLONG_MAX_USEC_OVERFLOW (sec, usec) \ + (sizeof(time_t) == 8 && ((sec == LLONG_MAX/1000000 && usec > LLONG_MAX%1000000) \ + || (sec > LLONG_MAX/1000000))) + +#define LLONG_MIN_USEC_UNDERFLOW (sec, usec) \ + ((sec == LLONG_MIN/1000000 && usec < LLONG_MIN%1000000) \ + || (sec < LLONG_MIN/1000000)) + +#define LLONG_MAX_MSEC_OVERFLOW (sec, msec) \ + ((sec == LLONG_MAX/1000 && msec > LLONG_MAX%1000) \ + || (sec > LLONG_MAX/1000)) + +#define LLONG_MIN_MSEC_UNDERFLOW (sec, msec) \ + ((sec == LLONG_MIN/1000 && msec < LLONG_MIN%1000) \ + || (sec < LLONG_MIN/1000)) + +/* +* Normalize timespec so that abs(tv_nsec) is smaller than 1s. +*/ +static inline struct timespec tst_normalize_timespec (struct timespec t) +{ + t.tv_sec += t.tv_nsec /1000000000 ; + t.tv_nsec %=1000000000 ; + +return t; +} + +/* +* Normalize timeval so that abs(tv_usec) is smaller than 1s. +*/ +static inline struct timeval tst_normalize_timeval (struct timeval t) +{ + t.tv_sec += t.tv_usec /1000000 ; + t.tv_usec %=1000000 ; + +return t; +}/* -* Converts timespec to microseconds. +* Saturated conversion from timespec to microseconds. */ static inline long long tst_timespec_to_us (struct timespec t) { -return t.tv_sec *1000000 + (t.tv_nsec +500 ) /1000 ; + t =tst_normalize_timespec (t); + +long long usec = (t.tv_nsec +500 ) /1000 ; + +#if LONG_MAX >0xffffffff +if (LLONG_MAX_USEC_OVERFLOW (t.tv_sec, usec)) +return LLONG_MAX ; + +if (LLONG_MIN_USEC_UNDERFLOW (t.tv_sec, usec)) +return LLONG_MIN ; +#endif + +return t.tv_sec *1000000 + usec; }/* -* Converts timespec to miliseconds. +* Saturated conversion from timespec to miliseconds. */ static inline long long tst_timespec_to_ms (struct timespec t) { -return t.tv_sec *1000 + (t.tv_nsec +500000 ) /1000000 ; + t =tst_normalize_timespec (t); + +long long msec = (t.tv_nsec +500000 ) /1000000 ; + +#if LONG_MAX >0xffffffff +if (LLONG_MAX_MSEC_OVERFLOW (t.tv_sec, msec)) +return LLONG_MAX ; + +if (LLONG_MIN_MSEC_UNDERFLOW (t.tv_sec, msec)) +return LLONG_MIN ; +#endif + +return t.tv_sec *1000 + msec; }/* -* Converts timeval to microseconds. +* Saturated conversion from timeval to microseconds. */ static inline long long tst_timeval_to_us (struct timeval t) { -return t.tv_sec *1000000 + t.tv_usec; + t =tst_normalize_timeval (t); + +long long usec = t.tv_usec; + +#if LONG_MAX >0xffffffff +if (LLONG_MAX_USEC_OVERFLOW (t.tv_sec, usec)) +return LLONG_MAX ; + +if (LLONG_MIN_USEC_UNDERFLOW (t.tv_sec, usec)) +return LLONG_MIN ; +#endif + +return t.tv_sec *1000000 + usec; }/* -* Converts timeval to miliseconds. +* Saturated conversion from timeval to miliseconds. */ static inline long long tst_timeval_to_ms (struct timeval t) { -return t.tv_sec *1000 + (t.tv_usec +500 ) /1000 ; + t =tst_normalize_timeval (t); + +long long msec = (t.tv_usec +500 ) /1000 ; + +#if LONG_MAX >0xffffffff +if (LLONG_MAX_MSEC_OVERFLOW (t.tv_sec, msec)) +return LLONG_MAX ; + +if (LLONG_MIN_MSEC_UNDERFLOW (t.tv_sec, msec)) +return LLONG_MIN ; +#endif + +return t.tv_sec *1000 + msec; }/*
@@ -93,6 +180,32 @@ static inline struct timeval tst_us_to_timeval(long long us) }/* +* Converts ms to struct timespec +*/ +static inline struct timespec tst_ms_to_timespec (long long ms) +{ +struct timespec ret; + + ret.tv_sec = ms /1000 ; + ret.tv_nsec = (ms %1000 ) *1000000 ; + +return ret; +} + +/* +* Converts us to struct timespec +*/ +static inline struct timespec tst_us_to_timespec (long long us) +{ +struct timespec ret; + + ret.tv_sec = us /1000000 ; + ret.tv_nsec = (us %1000000 ) *1000 ; + +return ret; +} + +/* * Comparsions */ static inline int tst_timespec_lt (struct timespec t1,struct timespec t2)
lib/newlib_tests/.gitignore +1 -0
@@ -15,3 +15,4 @@ test14 tst_device tst_safe_fileops tst_res_hexd +tst_timer
lib/newlib_tests/tst_timer.c new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.13.0
RFC PATCH 2/2 tst_timer.h: Make time conversions saturated
----- Original Message -----
Quoted 10 lines
The conversions of struct timeval and struct timespec to us and ms are now saturated so instead of overflowing/underflowing long long these return LLONG_MAX/LLONG_MIN.
This commit also adds
tst_us_to_timespec(),tst_ms_to_timespec()and newlib_tests/tst_timer test to assert that the saturated conversion actually works.Signed-off-by Cyril Hrubis <[email protected]>---
There's one issue I'm running into, default language std is older
than c99. So with plain configure && make, I fail on:
../include/tst_timer.h:86: error: ‘LLONG_MAX’ undeclared (first use in this function) ../include/tst_timer.h:89: error: ‘LLONG_MIN’ undeclared (first use in this function)
With CFLAGS="-std=gnu99" I run into one minor issue:
clock_nanosleep01.c:64:17: error: initializer element is not constant
but after resolving that, all compiles.
---
What is reasoning behind these ifdefs?
#if LONG_MAX > 0xffffffff
Regards,
Jan
RFC PATCH 2/2 tst_timer.h: Make time conversions saturated
Hi!
Quoted 8 lines
Quoted 8 lines
The conversions of struct timeval and struct timespec to us and ms are now saturated so instead of overflowing/underflowing long long these return LLONG_MAX/LLONG_MIN.
This commit also adds
tst_us_to_timespec(),tst_ms_to_timespec()and newlib_tests/tst_timer test to assert that the saturated conversion actually works.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Cyril Hrubis <[email protected]>---
There's one issue I'm running into, default language std is older
than c99. So with plain configure && make, I fail on:
../include/tst_timer.h:86: error: ???LLONG_MAX??? undeclared (first use in this function)../include/tst_timer.h:89: error: ???LLONG_MIN??? undeclared (first use in this function)
Supposedly we should set _GNU_SOURCE in all testcases that include that header after the change. Or set up fallback LLONG_MAX and LLONG_MIN definitions.
Quoted 2 lines
What is reasoning behind these ifdefs?
#ifLONG_MAX>0xffffffff
To shut up warnings. If we compile on 32bit platform these conditions are always false and we got warnings, a lot of them for each source that includes the header. And since time_t size is as far as I can tell the same as long we check if LONG_MAX is 64bit or not that way. Checking for time_t size with autoconf would be a bit cleaner solution though.
Signature · 2 lines
Cyril Hrubis [email protected]