]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: time: add now_mono_time() and now_cpu_time()
authorWilly Tarreau <w@1wt.eu>
Wed, 17 Oct 2018 16:59:53 +0000 (18:59 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 18 Oct 2018 14:39:48 +0000 (16:39 +0200)
These two functions retrieve respectively the monotonic clock time and
the per-thread CPU time when available on the platform, or return zero.
These syscalls may require to link with -lrt on certain libc, which is
enabled in the Makefile with USE_RT=1 (default on Linux systems).

include/common/time.h

index aeb19860d2c85c2cc9dea5fe23d8cd78b7c5eaa3..d4446dc146a7b99d299197ee76052035071a5c0c 100644 (file)
@@ -22,7 +22,9 @@
 #ifndef _COMMON_TIME_H
 #define _COMMON_TIME_H
 
+#include <stdint.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <sys/time.h>
 #include <common/config.h>
 #include <common/standard.h>
@@ -514,6 +516,30 @@ REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const stru
         tv1;                       \
 })
 
+/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
+static inline uint64_t now_mono_time()
+{
+#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
+       struct timespec ts;
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#else
+       return 0;
+#endif
+}
+
+/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
+static inline uint64_t now_cpu_time()
+{
+#if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
+       struct timespec ts;
+       clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
+       return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#else
+       return 0;
+#endif
+}
+
 /* Update the idle time value twice a second, to be called after
  * tv_update_date() when called after poll(). It relies on <before_poll> to be
  * updated to the system time before calling poll().