Skip to contentSkip to conversation

time: basic utility for getting the system time

PATCH In review James Prestwood 3 participants 5 messages Jan 23, 2019 – Jan 24, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 time: basic utility for getting the system time

Timeouts are sometimes overkill for long timeouts, or timeouts that don't require being immediately serviced once expired. For this reason the 'time' module was added which allows you to get the system clock time, as well as some basic APIs to check if two time values are before/after one another, calculating an offset from a start time, and finding the diff between two time values. This allows an application to handle its own timeouts lazily with no tie to the main loop.

The APIs were modeled after the kernel APIs in jiffies.h

---
 Makefile.am |  6 +++--
 ell/ell.h   |  1 +
 ell/ell.sym |  7 ++++++
 ell/time.c  | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/time.h  | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 2 deletions(-)
 create mode 100644 ell/time.c
 create mode 100644 ell/time.h
Makefile.am +4 -2
index 3eecbe0..1c089ec 100644
@@ -51,7 +51,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/dhcp.h \
 			ell/cert.h \
 			ell/ecc.h \
-			ell/ecdh.h
+			ell/ecdh.h \
+			ell/time.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -121,7 +122,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/ecc.h \
 			ell/ecc-external.c \
 			ell/ecc.c \
-			ell/ecdh.c
+			ell/ecdh.c \
+			ell/time.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ell.h +1 -0
index aab6417..691bdb2 100644
@@ -59,3 +59,4 @@
 #include <ell/cert.h>
 #include <ell/ecc.h>
 #include <ell/ecdh.h>
+#include <ell/time.h>
ell/ell.sym +7 -0
index 7643a25..d1fb2ff 100644
@@ -485,6 +485,13 @@ global:
 	/* ecdh */
 	l_ecdh_generate_key_pair;
 	l_ecdh_generate_shared_secret;
+
+	/* time */
+	l_time_now;
+	l_time_after;
+	l_time_before;
+	l_time_offset;
+	l_time_diff;
 local:
 	*;
 };
ell/time.c new file +72 -0

72 lines not on this page · raw message

ell/time.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/2 unit: add unit/test-time.c

A few basic sanity checks for time module.

---
 .gitignore       |  1 +
 Makefile.am      |  5 +++-
 unit/test-time.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-time.c
.gitignore +1 -0
index 75ab7b2..520c509 100644
@@ -58,6 +58,7 @@ unit/test-dhcp
 unit/test-dir-watch
 unit/test-ecc
 unit/test-ecdh
+unit/test-time
 unit/cert-*.pem
 unit/cert-*.csr
 unit/cert-*.srl
Makefile.am +4 -1
index 1c089ec..5bc13bd 100644
@@ -170,7 +170,8 @@ unit_tests = unit/test-unit \
 			unit/test-dhcp \
 			unit/test-dir-watch \
 			unit/test-ecc \
-			unit/test-ecdh
+			unit/test-ecdh \
+			unit/test-time
 
 dbus_tests = unit/test-hwdb \
 			unit/test-dbus \
@@ -294,6 +295,8 @@ unit_test_ecc_LDADD = ell/libell-private.la
 unit_test_ecdh_LDADD = ell/libell-private.la
 unit_test_ecdh_LDFLAGS = -Wl,-wrap,l_getrandom
 
+unit_test_time_LDADD = ell/libell-private.la
+
 if MAINTAINER_MODE
 noinst_LTLIBRARIES += unit/example-plugin.la
 endif
unit/test-time.c new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 16 lines · James Prestwood

Timeouts are sometimes overkill for long timeouts, or timeouts that don't require being immediately serviced once expired. For this reason the 'time' module was added which allows you to get the system clock time, as well as some basic APIs to check if two time values are before/after one another, calculating an offset from a start time, and finding the diff between two time values. This allows an application to handle its own timeouts lazily with no tie to the main loop.

The APIs were modeled after the kernel APIs in jiffies.h

---
  Makefile.am |  6 +++--
  ell/ell.h   |  1 +
  ell/ell.sym |  7 ++++++
  ell/time.c  | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/time.h  | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
  5 files changed, 151 insertions(+), 2 deletions(-)
Quoted 3 lines

create mode 100644 ell/time.c
create mode 100644 ell/time.h

Both applied, thanks.

Regards,
-Denis

#3 lore raw
In reply to James Prestwood
Quoted 11 lines · James Prestwood

+/**
+ * l_time_offset
+ *
+ * @time: Start time to calculate offset
+ * @offset: Amount of time to add to 'time'
+ *
+ * Adds an offset to a time value. This checks for overflow, and if detected
+ * returns ULONG_MAX.
+ *
+ * Returns: A time value 'time' + 'offset'. Or ULONG_MAX if time + offset
+ * exceeds ULONG_MAX.

This isn't valid for platforms where long is 32 bits (those that implement LP32, ILP32 and LLP64 data models). Since you're returning an uint64_t I think you should return UINT64_MAX instead.

ell/time.h line 41

Quoted 6 lines
ell/time.h new file +6 -0
+static inline bool l_time_after(uint64_t a, uint64_t b)
+{
+       if ((int64_t)(b - a) < 0)
+               return true;
+
+       return false;

This doesn't behave as documented in all cases. For example, both assertions in the below code will being triggered (manually comment out one at time :)):

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

static inline bool l_time_after(uint64_t a, uint64_t b)

{
    if ((int64_t)(b - a) < 0) {
        return true;
    }
    return false;
}

int main()

{
    uint64_t a = UINT64_MAX;
    uint64_t b = UINT64_MAX / 2;

    assert(l_time_after(a, b));
    assert(!l_time_after(b, a));
}

Since both 'a' and 'b' are of the same type, why not check if a > b instead? Also, there is no need for the branch:

static inline bool l_time_after(uint64_t a, uint64_t b)

{
    return a > b;
}

ell/time.h line 52

Quoted 11 lines
ell/time.h new file +11 -0
+}
+
+static inline bool l_time_before(uint64_t a, uint64_t b)
+{
+       return l_time_after(b, a);
+}
+
+static inline uint64_t l_time_offset(uint64_t time, uint64_t offset)
+{
+       /* check overflow */
+       if ((int64_t)(time + offset) <= 0)

What if both time and offset are zero? Shouldn't zero be returned? I understand that a time of zero is a problem in and of itself. :)

Also, this will only overflow in one case, i.e. (time + offset) == UINT64_MAX + 1 since your casting the uint64_t typed result of (time + offset) to int64_t. This means that the addition will always be >= 0. Something like the following should work:

static inline uint64_t l_time_offset(uint64_t time, uint64_t offset)

{
       /* check overflow */
      if (offset > UINT64_MAX - time)
          return UINT64_MAX;

      return time + offset;
}
Quoted 1 line

+ return ULONG_MAX;

As I mentioned earlier, this isn't valid for platforms where long is 32 bits (those that implement LP32, ILP32 and LLP64 data models). Since you're returning an uint64_t I think you should return UINT64_MAX instead.

HTH,
-Ossama

#4 lore raw

Hi Ossama,

Quoted 14 lines · Ossama
Quoted 11 lines · James Prestwood

+/**
+ * l_time_offset
+ *
+ * @time: Start time to calculate offset
+ * @offset: Amount of time to add to 'time'
+ *
+ * Adds an offset to a time value. This checks for overflow, and if detected
+ * returns ULONG_MAX.
+ *
+ * Returns: A time value 'time' + 'offset'. Or ULONG_MAX if time + offset
+ * exceeds ULONG_MAX.

Quoted 3 lines

This isn't valid for platforms where long is 32 bits (those that implement LP32, ILP32 and LLP64 data models). Since you're returning an uint64_t I think you should return UINT64_MAX instead.

Yep, my fault for missing that.

Quoted 11 lines

ell/time.h line 41

Quoted 7 lines
ell/time.h new file +6 -0
+static inline bool l_time_after(uint64_t a, uint64_t b)
+{
+       if ((int64_t)(b - a) < 0)
+               return true;
+
+       return false;

This doesn't behave as documented in all cases. For example, both assertions in the below code will being triggered (manually comment out one at time :)):

All right, I should not have been reviewing code today. So after brain reboot, lets try this again.

So the reason for the confusion is that the kernel counts time in jiffies, which can overflow. The logic above works for that, accounting for the overflow. However, the assumption is that the timeouts are small, and would not be more than about half of the available data type width. So your example below is actually an extreme case.

Unfortunately, the rest of the code here assumes that the time is an absolute value, and thus cannot overflow. So each part in isolation sort of made sense, but taken together... not so much.

Quoted 22 lines

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

static inline bool l_time_after(uint64_t a, uint64_t b)

{
     if ((int64_t)(b - a) < 0) {
         return true;
     }
     return false;
}

int main()

{
     uint64_t a = UINT64_MAX;
     uint64_t b = UINT64_MAX / 2;

     assert(l_time_after(a, b));
     assert(!l_time_after(b, a));
}
Quoted 7 lines

Since both 'a' and 'b' are of the same type, why not check if a > b instead? Also, there is no need for the branch:

static inline bool l_time_after(uint64_t a, uint64_t b)

{
     return a > b;
}

Yes, and this is what I actually suggested to James initially. So this is what we will use.

Quoted 21 lines

ell/time.h line 52

Quoted 12 lines
ell/time.h new file +11 -0
+}
+
+static inline bool l_time_before(uint64_t a, uint64_t b)
+{
+       return l_time_after(b, a);
+}
+
+static inline uint64_t l_time_offset(uint64_t time, uint64_t offset)
+{
+       /* check overflow */
+       if ((int64_t)(time + offset) <= 0)

What if both time and offset are zero? Shouldn't zero be returned? I understand that a time of zero is a problem in and of itself. :)

Also, this will only overflow in one case, i.e. (time + offset) == UINT64_MAX + 1 since your casting the uint64_t typed result of (time + offset) to int64_t. This means that the addition will always be >= 0. Something like the following should work:

Quoted 8 lines

static inline uint64_t l_time_offset(uint64_t time, uint64_t offset)

{
        /* check overflow */
       if (offset > UINT64_MAX - time)
           return UINT64_MAX;

       return time + offset;
}

Right, I pushed a patch with your name on it with the fixes to l_time_offset and l_time_after. Hope you don't mind.

Hopefully I didn't screw anything else up.

Quoted 8 lines
Quoted 2 lines

+ return ULONG_MAX;

As I mentioned earlier, this isn't valid for platforms where long is 32 bits (those that implement LP32, ILP32 and LLP64 data models). Since you're returning an uint64_t I think you should return UINT64_MAX instead.

And it is lucky that CLOCK_BOOTTIME reports only microseconds since boot, otherwise we'd have to deal with Y2038 overflows :P

Regards,
-Denis