1 # Copyright (c) 2009, 2010 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 clock_gettime_name = 'clock_gettime'
24 if sys.platform.startswith("linux"):
26 time_t = ctypes.c_long
27 elif sys.platform.startswith("netbsd"):
28 # NetBSD uses function renaming for ABI versioning. While the proper
29 # way to get the appropriate version is of course "#include <time.h>",
30 # it is difficult with ctypes. The following is appropriate for
31 # recent versions of NetBSD, including NetBSD-6.
33 clock_gettime_name = '__clock_gettime50'
35 time_t = ctypes.c_int64
36 elif sys.platform.startswith("freebsd"):
38 time_t = ctypes.c_int64
42 class timespec(ctypes.Structure):
45 ('tv_nsec', ctypes.c_long),
48 librt = ctypes.CDLL(LIBRT)
49 clock_gettime = getattr(librt, clock_gettime_name)
50 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
52 # Librt shared library could not be loaded
61 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) == 0:
62 return t.tv_sec + t.tv_nsec * 1e-9
63 # Kernel does not support CLOCK_MONOTONIC
67 # Use time.monotonic() if Python version >= 3.3
68 if not hasattr(time, 'monotonic'):
69 time.monotonic = monotonic
73 """ Returns the system's monotonic time if possible, otherwise returns the
74 current time as the amount of time since the epoch, in milliseconds, as a
76 return time.monotonic() * 1000.0