]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: provide an rdtsc() function for time comparisons
authorWilly Tarreau <w@1wt.eu>
Wed, 29 Apr 2015 15:13:35 +0000 (17:13 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 29 Apr 2015 17:14:03 +0000 (19:14 +0200)
This one returns a timestamp, either the one from the CPU or from
gettimeofday() in 64-bit format. The purpose is to be able to compare
timestamps on various entities to make it easier to detect updates.
It can also be used for benchmarking in certain situations during
development.

include/common/standard.h

index d0d5065dfad4afe54d6d7daa62232caea7e24d8f..bb63535f72e237cb7fc21063b24d0addc91daedf 100644 (file)
@@ -25,6 +25,7 @@
 #include <limits.h>
 #include <string.h>
 #include <time.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
@@ -959,4 +960,23 @@ static inline unsigned char utf8_return_length(unsigned char code)
        return code & 0x0f;
 }
 
+/* returns a 64-bit a timestamp with the finest resolution available. The
+ * unit is intentionally not specified. It's mostly used to compare dates.
+ */
+#if defined(__i386__) || defined(__x86_64__)
+static inline unsigned long long rdtsc()
+{
+     unsigned int a, d;
+     asm volatile("rdtsc" : "=a" (a), "=d" (d));
+     return a + ((unsigned long long)d << 32);
+}
+#else
+static inline unsigned long long rdtsc()
+{
+       struct timeval tv;
+       gettimeofday(&tv, NULL);
+       return tv.tv_sec * 1000000 + tv.tv_usec;
+}
+#endif
+
 #endif /* _COMMON_STANDARD_H */