[tor-commits] [tor/master] Add a function to add msec to a monotime.

nickm at torproject.org nickm at torproject.org
Wed Dec 20 14:18:28 UTC 2017


commit dd6dec2665af9964d8f940c27f3f0815a649424a
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Dec 13 08:54:29 2017 -0500

    Add a function to add msec to a monotime.
    
    We'll use this for the channel padding logic.
---
 src/common/compat_time.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/compat_time.h |  9 +++++++++
 src/test/test_util.c     | 28 ++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/src/common/compat_time.c b/src/common/compat_time.c
index fa8dbdfac..396a7b754 100644
--- a/src/common/compat_time.c
+++ b/src/common/compat_time.c
@@ -357,6 +357,14 @@ monotime_is_zero(const monotime_t *val)
   return val->abstime_ == 0;
 }
 
+void
+monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
+{
+  const uint64_t nsec = msec * ONE_MILLION;
+  const uint64_t ticks = (nsec * mach_time_info.denom) / mach_time_info.numer;
+  out->abstime_ = val->abstime_ + ticks;
+}
+
 /* end of "__APPLE__" */
 #elif defined(HAVE_CLOCK_GETTIME)
 
@@ -453,6 +461,19 @@ monotime_is_zero(const monotime_t *val)
   return val->ts_.tv_sec == 0 && val->ts_.tv_nsec == 0;
 }
 
+void
+monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
+{
+  const uint32_t sec = msec / 1000;
+  const uint32_t msec_remainder = msec % 1000;
+  out->ts_.tv_sec = val->ts_.tv_sec + sec;
+  out->ts_.tv_nsec = val->ts_.tv_nsec + (msec_remainder * ONE_MILLION);
+  if (out->ts_.tv_nsec > ONE_BILLION) {
+    out->ts_.tv_nsec -= ONE_BILLION;
+    out->ts_.tv_sec += 1;
+  }
+}
+
 /* end of "HAVE_CLOCK_GETTIME" */
 #elif defined (_WIN32)
 
@@ -605,6 +626,20 @@ monotime_coarse_is_zero(const monotime_coarse_t *val)
   return val->tick_count_ == 0;
 }
 
+void
+monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
+{
+  const uint64_t nsec = msec * ONE_MILLION;
+  const uint64_t ticks = (nsec * nsec_per_tick_denom) / nsec_per_tick_numer;
+  out->pcount_ = val->pcount_ + ticks;
+}
+
+void
+monotime_coarse_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
+{
+  out->tick_count_ = val->tick_count_ + msec;
+}
+
 /* end of "_WIN32" */
 #elif defined(MONOTIME_USING_GETTIMEOFDAY)
 
@@ -658,6 +693,19 @@ monotime_is_zero(const monotime_t *val)
   return val->tv_.tv_sec == 0 && val->tv_.tv_usec == 0;
 }
 
+void
+monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
+{
+  const uint32_t sec = msec / 1000;
+  const uint32_t msec_remainder = msec % 1000;
+  out->tv_.tv_sec = val->tv_.tv_sec + sec;
+  out->tv_.tv_usec = val->tv_.tv_nsec + (msec_remainder * 1000);
+  if (out->tv_.tv_usec > ONE_MILLION) {
+    out->tv_.tv_usec -= ONE_MILLION;
+    out->tv_.tv_sec += 1;
+  }
+}
+
 /* end of "MONOTIME_USING_GETTIMEOFDAY" */
 #else
 #error "No way to implement monotonic timers."
diff --git a/src/common/compat_time.h b/src/common/compat_time.h
index 68c11fb60..6ddd11883 100644
--- a/src/common/compat_time.h
+++ b/src/common/compat_time.h
@@ -114,6 +114,12 @@ void monotime_zero(monotime_t *out);
  */
 int monotime_is_zero(const monotime_t *out);
 
+/**
+ * Set <b>out</b> to N milliseconds after <b>val</b>.
+ */
+/* XXXX We should add a more generic function here if we ever need to */
+void monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec);
+
 #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
 /**
  * Set <b>out</b> to the current coarse time.
@@ -155,12 +161,15 @@ int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
     const monotime_coarse_t *end);
 void monotime_coarse_zero(monotime_coarse_t *out);
 int monotime_coarse_is_zero(const monotime_coarse_t *val);
+void monotime_coarse_add_msec(monotime_coarse_t *out,
+                              const monotime_coarse_t *val, uint32_t msec);
 #else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
 #define monotime_coarse_diff_nsec monotime_diff_nsec
 #define monotime_coarse_diff_usec monotime_diff_usec
 #define monotime_coarse_diff_msec monotime_diff_msec
 #define monotime_coarse_zero monotime_zero
 #define monotime_coarse_is_zero monotime_is_zero
+#define monotime_coarse_add_msec monotime_add_msec
 #endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
 
 void tor_gettimeofday(struct timeval *timeval);
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 7210f1674..1c58ad695 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -5948,6 +5948,33 @@ test_util_monotonic_time_zero(void *arg)
 }
 
 static void
+test_util_monotonic_time_add_msec(void *arg)
+{
+  (void) arg;
+  monotime_t t1, t2;
+  monotime_coarse_t ct1, ct2;
+  monotime_init();
+
+  monotime_get(&t1);
+  monotime_coarse_get(&ct1);
+
+  /* adding zero does nothing */
+  monotime_add_msec(&t2, &t1, 0);
+  monotime_coarse_add_msec(&ct2, &ct1, 0);
+  tt_i64_op(monotime_diff_msec(&t1, &t2), OP_EQ, 0);
+  tt_i64_op(monotime_coarse_diff_msec(&ct1, &ct2), OP_EQ, 0);
+
+  /* Add 1337 msec; see if the diff function agree */
+  monotime_add_msec(&t2, &t1, 1337);
+  monotime_coarse_add_msec(&ct2, &ct1, 1337);
+  tt_i64_op(monotime_diff_msec(&t1, &t2), OP_EQ, 1337);
+  tt_i64_op(monotime_coarse_diff_msec(&ct1, &ct2), OP_EQ, 1337);
+
+ done:
+  ;
+}
+
+static void
 test_util_htonll(void *arg)
 {
   (void)arg;
@@ -6181,6 +6208,7 @@ struct testcase_t util_tests[] = {
   UTIL_TEST(monotonic_time, 0),
   UTIL_TEST(monotonic_time_ratchet, TT_FORK),
   UTIL_TEST(monotonic_time_zero, 0),
+  UTIL_TEST(monotonic_time_add_msec, 0),
   UTIL_TEST(htonll, 0),
   UTIL_TEST(get_unquoted_path, 0),
   END_OF_TESTCASES





More information about the tor-commits mailing list