Merge pull request #47 from mullakhmetov/crossplatform-gettime

Refactor cross-platform libc usage
pull/65/head
George Hotz 8 years ago committed by GitHub
commit 6ab4ac2dfb
  1. 33
      common/realtime.py

@ -6,6 +6,9 @@ import subprocess
import multiprocessing import multiprocessing
import os import os
from ctypes.util import find_library
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h> CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
CLOCK_BOOTTIME = 7 CLOCK_BOOTTIME = 7
@ -15,28 +18,30 @@ class timespec(ctypes.Structure):
('tv_nsec', ctypes.c_long), ('tv_nsec', ctypes.c_long),
] ]
libc_name = find_library('c')
if libc_name is None:
platform_name = platform.system()
if platform_name.startswith('linux'):
libc_name = 'libc.so.6'
if platform_name.startswith(('freebsd', 'netbsd')):
libc_name = 'libc.so'
elif platform_name.lower() == 'darwin':
libc_name = 'libc.dylib'
try: try:
libc = ctypes.CDLL('libc.so', use_errno=True) libc = ctypes.CDLL(libc_name, use_errno=True)
except OSError: except OSError:
try: libc = None
libc = ctypes.CDLL('libc.so.6', use_errno=True)
except OSError:
libc = None
if libc is not None: if libc is not None:
libc.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] libc.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def clock_gettime(clk_id): def clock_gettime(clk_id):
if platform.system().lower() == "darwin": t = timespec()
# TODO: fix this if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0:
return time.time() errno_ = ctypes.get_errno()
else: raise OSError(errno_, os.strerror(errno_))
t = timespec() return t.tv_sec + t.tv_nsec * 1e-9
if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t.tv_sec + t.tv_nsec * 1e-9
def monotonic_time(): def monotonic_time():
return clock_gettime(CLOCK_MONOTONIC_RAW) return clock_gettime(CLOCK_MONOTONIC_RAW)

Loading…
Cancel
Save