[ccan] [PATCH] timesince: Add module
David Gibson
david at gibson.dropbear.id.au
Sat May 31 22:48:24 EST 2014
This patch adds a new "timesince" module. This builds on the 'time'
module, making it easy to measure time intervals from a set starting
point. It uses the monotonic clock when possible, so it won't be broken
by concurrent changes to the system time.
Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
---
ccan/timesince/LICENSE | 1 +
ccan/timesince/_info | 43 +++++++++++++++++++++++++++++++++++++++++++
ccan/timesince/test/run.c | 34 ++++++++++++++++++++++++++++++++++
ccan/timesince/timesince.c | 37 +++++++++++++++++++++++++++++++++++++
ccan/timesince/timesince.h | 12 ++++++++++++
5 files changed, 127 insertions(+)
create mode 120000 ccan/timesince/LICENSE
create mode 100644 ccan/timesince/_info
create mode 100644 ccan/timesince/test/run.c
create mode 100644 ccan/timesince/timesince.c
create mode 100644 ccan/timesince/timesince.h
diff --git a/ccan/timesince/LICENSE b/ccan/timesince/LICENSE
new file mode 120000
index 0000000..2354d12
--- /dev/null
+++ b/ccan/timesince/LICENSE
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/timesince/_info b/ccan/timesince/_info
new file mode 100644
index 0000000..f6b0c82
--- /dev/null
+++ b/ccan/timesince/_info
@@ -0,0 +1,43 @@
+#include <string.h>
+#include <stdio.h>
+#include "config.h"
+
+/**
+ * timesince - Convenience functions for time since a given point
+ *
+ * This code allows code to time itself, by saving a start point, then
+ * easily allowing the time from that start point to be calculated.
+ * When available this uses CLOCK_MONOTONIC, so it won't be affected
+ * by concurrent updates to the system clock.
+ *
+ * Example:
+ * #include <ccan/timesince/timesince.h>
+ * #include <stdio.h>
+ * #include <inttypes.h>
+ *
+ * int main(int argc, char *argv[])
+ * {
+ * struct timespec start;
+ *
+ * start = timesince_start();
+ * // Do things
+ * printf("Operation took %" PRId64 "ms\n", timesince_msec(start));
+ * exit(0);
+ * }
+ *
+ * Author: David Gibson <david at gibson.dropbear.id.au
+ * License: BSD-MIT
+ */
+int main(int argc, char *argv[])
+{
+ /* Expect exactly one argument */
+ if (argc != 2)
+ return 1;
+
+ if (strcmp(argv[1], "depends") == 0) {
+ printf("ccan/time\n");
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/ccan/timesince/test/run.c b/ccan/timesince/test/run.c
new file mode 100644
index 0000000..483737b
--- /dev/null
+++ b/ccan/timesince/test/run.c
@@ -0,0 +1,34 @@
+#include <unistd.h>
+#include <inttypes.h>
+#include <stdlib.h>
+
+#include <ccan/timesince/timesince.h>
+#include <ccan/tap/tap.h>
+
+#include <ccan/timesince/timesince.c>
+
+#define EPSILON_MS 10
+
+static int sleep_test(void)
+{
+ struct timespec start;
+ unsigned long long ms;
+
+ start = timesince_start();
+ sleep(1);
+ ms = timesince_msec(start);
+ diag("1s sleep => timesince_ms() of %llu\n", ms);
+ return abs(ms - 1000) < EPSILON_MS;
+}
+
+int main(void)
+{
+ /* This is how many tests you plan to run */
+ plan_tests(1);
+
+ /* Simple thing we expect to succeed */
+ ok1(sleep_test());
+
+ /* This exits depending on whether all tests passed */
+ return exit_status();
+}
diff --git a/ccan/timesince/timesince.c b/ccan/timesince/timesince.c
new file mode 100644
index 0000000..7a3ab0e
--- /dev/null
+++ b/ccan/timesince/timesince.c
@@ -0,0 +1,37 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#include <unistd.h>
+
+#include <ccan/time/time.h>
+#include <ccan/timesince/timesince.h>
+
+#if HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT
+#include <time.h>
+
+#if _POSIX_MONOTONIC_CLOCK
+static struct time_monotonic(void)
+{
+ struct timespec ret;
+ clock_gettime(CLOCK_MONOTONIC, &ret);
+ return TIME_CHECK(ret);
+}
+#else
+#define time_monotonic time_now
+#endif
+#else
+#define time_monotonic time_now
+#endif
+
+struct timespec timesince_start(void)
+{
+ return time_monotonic();
+}
+
+struct timespec timesince(struct timespec start)
+{
+ return time_sub(time_monotonic(), start);
+}
+
+uint64_t timesince_msec(struct timespec start)
+{
+ return time_to_msec(timesince(start));
+}
diff --git a/ccan/timesince/timesince.h b/ccan/timesince/timesince.h
new file mode 100644
index 0000000..7d8ae0f
--- /dev/null
+++ b/ccan/timesince/timesince.h
@@ -0,0 +1,12 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#ifndef CCAN_TIMESINCE_H
+#define CCAN_TIMESINCE_H
+#include "config.h"
+
+#include <ccan/time/time.h>
+
+struct timespec timesince_start(void);
+struct timespec timesince(struct timespec start);
+uint64_t timesince_msec(struct timespec start);
+
+#endif /* CCAN_TIMESINCE_H */
--
1.9.3
More information about the ccan
mailing list