[ovs-dev] [PATCH] timeval: Add monotonic time functionality for NetBSD and FreeBSD.

Ryan Wilson wryan at nicira.com
Tue Jun 3 04:45:36 UTC 2014


This patch also checks the system platform as clock_gettime
could exist on different platforms but with different values of
CLOCK_MONOTONIC and different definitions of 'struct timespec'.
In this case, the system call would be expected to catch the
error, which is dangerous.

This patch ensures Linux, NetBSD and FreeBSD platforms use
clock_gettime with their corresponding correct values and
definitions. All other platforms use time.time().

Signed-off-by: Ryan Wilson <wryan at nicira.com>
---
 python/ovs/timeval.py |   21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/python/ovs/timeval.py b/python/ovs/timeval.py
index 4463e87..6f3cbb3 100644
--- a/python/ovs/timeval.py
+++ b/python/ovs/timeval.py
@@ -12,17 +12,29 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import sys
 import time
 
 LIBRT = 'librt.so.1'
-CLOCK_MONOTONIC = 1
 
 try:
     import ctypes
 
+    if sys.platform.startswith("linux"):
+        CLOCK_MONOTONIC = 1
+        time_t = ctypes.c_long
+    elif sys.platform.startswith("netbsd"):
+        CLOCK_MONOTONIC = 3
+        time_t = ctypes.c_int64
+    elif sys.platform.startswith("freebsd"):
+        CLOCK_MONOTONIC = 4
+        time_t = ctypes.c_int64
+    else:
+        raise Exception
+
     class timespec(ctypes.Structure):
         _fields_ = [
-            ('tv_sec', ctypes.c_long),
+            ('tv_sec', time_t),
             ('tv_nsec', ctypes.c_long),
         ]
 
@@ -48,8 +60,9 @@ if not hasattr(time, 'monotonic'):
     time.monotonic = monotonic
 
 def msec():
-    """Returns the current time, as the amount of time since the epoch, in
-    milliseconds, as a float."""
+    """ Returns the system's monotonic time if possible, otherwise returns the
+    current time as the amount of time since the epoch, in milliseconds, as a
+    float."""
     return time.monotonic() * 1000.0
 
 
-- 
1.7.9.5




More information about the dev mailing list