]> git.ozlabs.org Git - ccan/commitdiff
time: time_mono() helpers.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 4 Jun 2014 03:43:19 +0000 (13:13 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 10 Jun 2014 06:00:28 +0000 (15:30 +0930)
From patch by David Gibson, rewritten on new ccan/time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/time/test/run-monotonic.c [new file with mode: 0644]
ccan/time/time.c
ccan/time/time.h

diff --git a/ccan/time/test/run-monotonic.c b/ccan/time/test/run-monotonic.c
new file mode 100644 (file)
index 0000000..ed70f3d
--- /dev/null
@@ -0,0 +1,24 @@
+#include <ccan/time/time.h>
+#include <ccan/time/time.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       struct timemono t1, t2;
+       struct timerel t3;
+
+       plan_tests(2);
+
+       /* Test time_mono */
+       t1 = time_mono();
+       t2 = time_mono();
+
+       ok1(!time_less_(t2.ts, t1.ts));
+
+       t3.ts.tv_sec = 1;
+       t3.ts.tv_nsec = 0;
+
+       ok1(time_less(timemono_between(t1, t2), t3));
+
+       return exit_status();
+}
index 54cb3f4b73ae601cf469592237bc165748593e4c..8e7fbffe739da0ba6cebed78b8c7624023269e1b 100644 (file)
@@ -25,6 +25,17 @@ struct timeabs time_now(void)
 }
 #endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT */
 
+struct timemono time_mono(void)
+{
+       struct timemono ret;
+#ifdef TIME_HAVE_MONOTONIC
+       clock_gettime(CLOCK_MONOTONIC, &ret.ts);
+#else /* Best we can do */
+       ret.ts = time_now().ts;
+#endif /* !HAVE_TIME_MONOTONIC */
+       return ret;
+}
+
 struct timerel time_divide(struct timerel t, unsigned long div)
 {
        struct timerel res;
index 7bb3547d6227630fb5eb682544dc7e2eb4c2e6aa..46568425c7cd44594e3964d3f8435f316d11eed4 100644 (file)
@@ -48,6 +48,28 @@ struct timeabs {
        struct timespec ts;
 };
 
+/**
+ * struct timemono - a monotonic time.
+ * @ts: the actual timespec value.
+ *
+ * This comes from the monotonic clock (if available), so it's useful
+ * for measuring intervals as it won't change even if the system clock
+ * is moved for some reason.
+ */
+struct timemono {
+       struct timespec ts;
+};
+
+/**
+ * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
+ *
+ * Otherwise time_mono() just refers to time_now().  Your code might
+ * test this if you really need a monotonic clock.
+ */
+#if (HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT) && defined(CLOCK_MONOTONIC)
+#define TIME_HAVE_MONOTONIC 1
+#endif
+
 struct timespec time_check_(struct timespec in, const char *abortstr);
 
 /**
@@ -99,6 +121,16 @@ struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
  */
 struct timeabs time_now(void);
 
+/**
+ * time_mono - return the current monotonic time
+ *
+ * This value is only really useful for measuring time intervals.
+ *
+ * See also:
+ *     time_since()
+ */
+struct timemono time_mono(void);
+
 static inline bool time_greater_(struct timespec a, struct timespec b)
 {
        if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
@@ -272,6 +304,22 @@ static inline struct timerel time_between(struct timeabs recent, struct timeabs
        return t;
 }
 
+/**
+ * timemono_between - time between two monotonic times
+ * @recent: the larger time.
+ * @old: the smaller time.
+ *
+ * This returns a well formed struct timerel of @recent - @old.
+ */
+static inline struct timerel timemono_between(struct timemono recent,
+                                             struct timemono old)
+{
+       struct timerel t;
+
+       t.ts = time_sub_(recent.ts, old.ts);
+       return t;
+}
+
 /**
  * timeabs_sub - subtract a relative time from an absolute time
  * @abs: the absolute time.