Skip to contentSkip to conversation

lib/timeutils: add monotonic clock support

PATCH New Alexander 1 participant 1 message May 5, 2014

Subsystems (2)
lore
Share & tools
lore

Alexander

author
#0 lore raw

PATCH 1/3 lib/timeutils: add monotonic clock support

Quoted 1 line

From b641e268ab0aa1597e46f79accd169c388f06c7c Mon Sep 17 00:00:00 2001

From: Alex Samilovskih <[email protected]>
Date: Fri, 2 May 2014 18:15:12 +0000
Subject: [PATCH 1/3] Add support for monotonic clock

---
 include/timeutils.h |  1 +
 lib/timeutils.c     | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
include/timeutils.h +1 -0
index bcae613..7cf972d 100644
@@ -51,5 +51,6 @@ typedef uint64_t nsec_t;
 #define FORMAT_TIMESPAN_MAX 64
 
 int parse_timestamp(const char *t, usec_t *usec);
+int gettime_monotonic(struct timeval *tv);
 
 #endif /* UTIL_LINUX_TIME_UTIL_H */
lib/timeutils.c +24 -0
index 7fe6218..b3fb87b 100644
@@ -336,3 +336,27 @@ int parse_timestamp(const char *t, usec_t *usec)
 
 	return 0;
 }
+
+/*  Get the current time that is not a subject to resetting and drifting  */
+int gettime_monotonic(struct timeval *tv)
+{
+#ifdef CLOCK_MONOTONIC
+	int ret;
+	struct timespec ts;
+	
+#ifdef CLOCK_MONOTONIC_RAW
+	if (!(ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
+#else /* CLOCK_MONOTONIC_RAW is not available */
+	if (!(ret = clock_gettime(CLOCK_MONOTONIC, &ts))) {
+#endif
+		tv->tv_sec = ts.tv_sec;
+		tv->tv_usec = ts.tv_nsec / 1000;
+	}
+	
+	return ret;
+	
+#else /* CLOCK_MONOTONIC is not available */
+
+	return gettimeofday(tv, NULL);
+#endif
+}
Signature · 1 line
1.9.2