You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2014/02/07 05:49:06 UTC

[2/2] git commit: TS-2556: minor ink_hrtime cleanup

TS-2556: minor ink_hrtime cleanup

Remove unnecessary based time conversion APIs. These don't do
anything different from the standard hrtime converters.

Add a timespec conversion ink_hrtime_from_timespec(). Add
ink_hrtime_from_timeval(). Use hrtime converters internally, rather
than duplicating arithmetic.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/704f0a85
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/704f0a85
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/704f0a85

Branch: refs/heads/master
Commit: 704f0a85ad717f2fb0698ee9a32306d51469280d
Parents: 0c3691e
Author: James Peach <jp...@apache.org>
Authored: Thu Feb 6 20:24:58 2014 -0800
Committer: James Peach <jp...@apache.org>
Committed: Thu Feb 6 20:48:30 2014 -0800

----------------------------------------------------------------------
 iocore/aio/AIO.cc                    |  2 +-
 iocore/eventsystem/ProtectedQueue.cc |  2 +-
 lib/ts/EventNotify.cc                |  5 +---
 lib/ts/ink_hrtime.cc                 |  4 +--
 lib/ts/ink_hrtime.h                  | 46 +++++++++++++++----------------
 5 files changed, 27 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/704f0a85/iocore/aio/AIO.cc
----------------------------------------------------------------------
diff --git a/iocore/aio/AIO.cc b/iocore/aio/AIO.cc
index a64918a..6a9c041 100644
--- a/iocore/aio/AIO.cc
+++ b/iocore/aio/AIO.cc
@@ -537,7 +537,7 @@ aio_thread_main(void *arg)
         op->thread->schedule_imm_signal(op);
       ink_mutex_acquire(&my_aio_req->aio_mutex);
     } while (1);
-    timespec timedwait_msec = ink_based_hrtime_to_timespec(ink_get_hrtime() + HRTIME_MSECONDS(net_config_poll_timeout));
+    timespec timedwait_msec = ink_hrtime_to_timespec(ink_get_hrtime() + HRTIME_MSECONDS(net_config_poll_timeout));
     ink_cond_timedwait(&my_aio_req->aio_cond, &my_aio_req->aio_mutex, &timedwait_msec);
   }
   return 0;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/704f0a85/iocore/eventsystem/ProtectedQueue.cc
----------------------------------------------------------------------
diff --git a/iocore/eventsystem/ProtectedQueue.cc b/iocore/eventsystem/ProtectedQueue.cc
index cfa1ec4..cc735a3 100644
--- a/iocore/eventsystem/ProtectedQueue.cc
+++ b/iocore/eventsystem/ProtectedQueue.cc
@@ -145,7 +145,7 @@ ProtectedQueue::dequeue_timed(ink_hrtime cur_time, ink_hrtime timeout, bool slee
   if (sleep) {
     ink_mutex_acquire(&lock);
     if (INK_ATOMICLIST_EMPTY(al)) {
-      timespec ts = ink_based_hrtime_to_timespec(timeout);
+      timespec ts = ink_hrtime_to_timespec(timeout);
       ink_cond_timedwait(&might_have_data, &lock, &ts);
     }
     ink_mutex_release(&lock);

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/704f0a85/lib/ts/EventNotify.cc
----------------------------------------------------------------------
diff --git a/lib/ts/EventNotify.cc b/lib/ts/EventNotify.cc
index c986146..296e220 100644
--- a/lib/ts/EventNotify.cc
+++ b/lib/ts/EventNotify.cc
@@ -141,11 +141,8 @@ EventNotify::timedwait(int timeout) // milliseconds
     return errno;
 #else
   ink_timestruc abstime;
-  ink_hrtime curtime;
-
-  curtime = ink_get_hrtime_internal() + timeout * HRTIME_MSECOND;
-  abstime = ink_based_hrtime_to_timespec(curtime);
 
+  abstime = ink_hrtime_to_timespec(ink_get_hrtime_internal() + HRTIME_SECONDS(timeout));
   return ink_cond_timedwait(&m_cond, &m_mutex, &abstime);
 #endif
 }

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/704f0a85/lib/ts/ink_hrtime.cc
----------------------------------------------------------------------
diff --git a/lib/ts/ink_hrtime.cc b/lib/ts/ink_hrtime.cc
index b8c3993..6d2870b 100644
--- a/lib/ts/ink_hrtime.cc
+++ b/lib/ts/ink_hrtime.cc
@@ -212,8 +212,8 @@ init_hrtime_basis()
     // accuracy must be at least 100 microseconds
   } while (t2 - t1 > HRTIME_USECONDS(100));
   b = (t2 + t1) / 2;
-  now = ink_timespec_to_based_hrtime(&timespec_basis);
-  ts = ink_based_hrtime_to_timespec(now);
+  now = ink_hrtime_from_timespec(&timespec_basis);
+  ts = ink_hrtime_to_timespec(now);
   ink_assert(ts.tv_sec == timespec_basis.tv_sec && ts.tv_nsec == timespec_basis.tv_nsec);
   hrtime_offset = now - b;
   hrtime_basis = b;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/704f0a85/lib/ts/ink_hrtime.h
----------------------------------------------------------------------
diff --git a/lib/ts/ink_hrtime.h b/lib/ts/ink_hrtime.h
index 2f7dc45..cfece39 100644
--- a/lib/ts/ink_hrtime.h
+++ b/lib/ts/ink_hrtime.h
@@ -156,12 +156,15 @@ ink_hrtime_from_nsec(unsigned int nsec)
 }
 
 static inline ink_hrtime
-ink_hrtime_from_timeval(struct timeval *tv)
+ink_hrtime_from_timespec(const struct timespec * ts)
 {
-  int64_t usecs;
+  return ink_hrtime_from_sec(ts->tv_sec) + ink_hrtime_from_nsec(ts->tv_nsec);
+}
 
-  usecs = tv->tv_sec * 1000000 + tv->tv_usec;
-  return (ink_hrtime_from_usec((unsigned int) usecs));
+static inline ink_hrtime
+ink_hrtime_from_timeval(const struct timeval * tv)
+{
+  return ink_hrtime_from_sec(tv->tv_sec) + ink_hrtime_from_usec(tv->tv_usec);
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -211,6 +214,16 @@ ink_hrtime_to_nsec(ink_hrtime t)
   return ((ink_hrtime) (t / HRTIME_NSECOND));
 }
 
+static inline struct timespec
+ink_hrtime_to_timespec(ink_hrtime t)
+{
+  struct timespec ts;
+
+  ts.tv_sec = ink_hrtime_to_sec(t);
+  ts.tv_nsec = t % HRTIME_SECOND;
+  return (ts);
+}
+
 static inline struct timeval
 ink_hrtime_to_timeval(ink_hrtime t)
 {
@@ -248,11 +261,11 @@ ink_get_hrtime_internal()
 #elif defined(freebsd)
   timespec ts;
   clock_gettime(CLOCK_REALTIME, &ts);
-  return (ts.tv_sec * HRTIME_SECOND + ts.tv_nsec * HRTIME_NSECOND);
+  return ink_hrtime_from_timespec(&ts);
 #else
   timeval tv;
   gettimeofday(&tv, NULL);
-  return (tv.tv_sec * HRTIME_SECOND + tv.tv_usec * HRTIME_USECOND);
+  return ink_hrtime_from_timeval(&tv);
 #endif
 #else /* !defined (NEED_HRTIME) */
   return gethrtime();
@@ -267,11 +280,11 @@ ink_get_based_hrtime_internal()
 #elif !HAVE_CLOCK_GETTIME
   timeval tv;
   gettimeofday(&tv, NULL);
-  return (tv.tv_sec * HRTIME_SECOND + tv.tv_usec * HRTIME_USECOND);
+  return ink_hrtime_from_timeval(&tv);
 #else
   timespec ts;
   clock_gettime(CLOCK_REALTIME, &ts);
-  return (ts.tv_sec * HRTIME_SECOND + ts.tv_nsec * HRTIME_NSECOND);
+  return ink_hrtime_from_timespec(&ts);
 #endif
 }
 
@@ -292,7 +305,7 @@ ink_gethrtimeofday(struct timeval *tp, void *)
 static inline int
 ink_time()
 {
-  return (int) (ink_get_based_hrtime_internal() / HRTIME_SECOND);
+  return (int) ink_hrtime_to_sec(ink_get_based_hrtime_internal());
 }
 
 static inline int
@@ -313,19 +326,4 @@ ink_hrtime_add(ink_hrtime t1, ink_hrtime t2)
   return (t1 + t2);
 }
 
-static inline timespec
-ink_based_hrtime_to_timespec(ink_hrtime t)
-{
-  timespec ts;
-  ts.tv_sec = (time_t) (t / HRTIME_SECOND);
-  ts.tv_nsec = (t % HRTIME_SECOND);
-  return ts;
-}
-
-static inline ink_hrtime
-ink_timespec_to_based_hrtime(timespec * ts)
-{
-  return (ts->tv_sec * HRTIME_SECOND + ts->tv_nsec * HRTIME_NSECOND);
-}
-
 #endif /* _ink_hrtime_h_ */