]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: time: add conversions to/from nanosecond timestamps
authorWilly Tarreau <w@1wt.eu>
Thu, 27 Apr 2023 06:51:54 +0000 (08:51 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 28 Apr 2023 14:08:08 +0000 (16:08 +0200)
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.

include/haproxy/time.h

index 97b4ee256b9b756873a0bdbf657a940d1fe49a4b..f3d73893a0fd9d2ad0596d81bb04efcf9adaae0f 100644 (file)
@@ -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