From: Willy Tarreau Date: Thu, 27 Apr 2023 06:51:54 +0000 (+0200) Subject: MINOR: time: add conversions to/from nanosecond timestamps X-Git-Tag: v2.8-dev9~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=591fa59;p=thirdparty%2Fhaproxy.git MINOR: time: add conversions to/from nanosecond timestamps In order to ease the transition away from the timeval used in internal timestamps, let's first create a few functions and macro to return a counter from a timeval and conversely, as well as ease the conversions to/from ns/us/ms/sec to save the user from having to count zeroes and to think about appending ULL in conversions. --- diff --git a/include/haproxy/time.h b/include/haproxy/time.h index 97b4ee256b..f3d73893a0 100644 --- a/include/haproxy/time.h +++ b/include/haproxy/time.h @@ -109,6 +109,52 @@ static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms return tv; } +/* + * Converts a struct timeval to a relative timestamp in nanoseconds (only + * wraps every 585 years, i.e. never for our purpose). + */ +static forceinline ullong tv_to_ns(const struct timeval *tv) +{ + ullong ret; + + ret = (ullong)tv->tv_sec * 1000000000ULL; + ret += (ullong)tv->tv_usec * 1000ULL; + return ret; +} + +/* turns nanoseconds to seconds, just to avoid typos */ +static forceinline uint ns_to_sec(ullong ns) +{ + return ns / 1000000000ULL; +} + +/* turns nanoseconds to milliseconds, just to avoid typos */ +static forceinline uint ns_to_ms(ullong ns) +{ + return ns / 1000000ULL; +} + +/* turns seconds to nanoseconds, just to avoid typos */ +static forceinline ullong sec_to_ns(uint sec) +{ + return sec * 1000000000ULL; +} + +/* turns milliseconds to nanoseconds, just to avoid typos */ +static forceinline ullong ms_to_ns(uint ms) +{ + return ms * 1000000ULL; +} + +/* turns microseconds to nanoseconds, just to avoid typos */ +static forceinline ullong us_to_ns(uint us) +{ + return us * 1000ULL; +} + +/* creates a struct timeval from a relative timestamp in nanosecond */ +#define NS_TO_TV(t) ((const struct timeval){ .tv_sec = t / 1000000000ULL, .tv_usec = (t % 1000000000ULL) / 1000U }) + /* Return a number of 1024Hz ticks between 0 and 1023 for input number of * usecs between 0 and 999999. This function has been optimized to remove * any divide and multiply, as it is completely optimized away by the compiler