summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2017-01-23 16:54:51 +0100
committerDavid Lamparter <equinox@opensourcerouting.org>2017-01-23 16:54:51 +0100
commit7790a2d69ee8439dbed951e77703458bb472dd4f (patch)
treecedad4cd65bf5ac37e5c4ea968491f21f5150976
parent45680e7568eb69d5f991b5c0c174e479878727ee (diff)
lib: time: add new monotime() helpers
This adds monotime() to retrieve monotonic clock time, as well as monotime_since() and monotime_until() for relative monotonic time. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
-rw-r--r--lib/Makefile.am3
-rw-r--r--lib/monotime.h70
2 files changed, 72 insertions, 1 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index f2c076c475..5dd38ee45a 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -46,7 +46,8 @@ pkginclude_HEADERS = \
ptm_lib.h csv.h bfd.h vrf.h ns.h systemd.h bitfield.h \
fifo.h memory_vty.h mpls.h imsg.h openbsd-queue.h openbsd-tree.h \
skiplist.h qobj.h wheel.h \
- event_counter.h
+ event_counter.h \
+ monotime.h
noinst_HEADERS = \
plist_int.h
diff --git a/lib/monotime.h b/lib/monotime.h
new file mode 100644
index 0000000000..10d3bb604d
--- /dev/null
+++ b/lib/monotime.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2017 David Lamparter, for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _FRR_MONOTIME_H
+#define _FRR_MONOTIME_H
+
+#include <stdint.h>
+#include <time.h>
+#include <sys/time.h>
+
+#ifndef TIMESPEC_TO_TIMEVAL
+/* should be in sys/time.h on BSD & Linux libcs */
+#define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
+ (tv)->tv_sec = (ts)->tv_sec; \
+ (tv)->tv_usec = (ts)->tv_nsec / 1000; \
+ } while (0)
+#endif
+
+static inline time_t monotime(struct timeval *tvo)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ if (tvo) {
+ TIMESPEC_TO_TIMEVAL(tvo, &ts);
+ }
+ return ts.tv_sec;
+}
+
+/* the following two return microseconds, not time_t!
+ *
+ * also, they're negative forms of each other, but having both makes the
+ * code more readable
+ */
+static inline int64_t monotime_since(const struct timeval *ref,
+ struct timeval *out)
+{
+ struct timeval tv;
+ monotime(&tv);
+ timersub(&tv, ref, &tv);
+ if (out)
+ *out = tv;
+ return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec;
+}
+
+static inline int64_t monotime_until(const struct timeval *ref,
+ struct timeval *out)
+{
+ struct timeval tv;
+ monotime(&tv);
+ timersub(ref, &tv, &tv);
+ if (out)
+ *out = tv;
+ return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec;
+}
+
+#endif /* _FRR_MONOTIME_H */