| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | # python module to get MONOTONIC time, borrowed here |
|---|
| 4 | # http://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python |
|---|
| 5 | |
|---|
| 6 | __all__ = ["monotonic_time"] |
|---|
| 7 | |
|---|
| 8 | import os |
|---|
| 9 | import ctypes |
|---|
| 10 | |
|---|
| 11 | if os.name == 'posix': |
|---|
| 12 | |
|---|
| 13 | CLOCK_MONOTONIC = 1 # see <linux/time.h> |
|---|
| 14 | |
|---|
| 15 | class timespec(ctypes.Structure): |
|---|
| 16 | _fields_ = [ |
|---|
| 17 | ('tv_sec', ctypes.c_long), |
|---|
| 18 | ('tv_nsec', ctypes.c_long) |
|---|
| 19 | ] |
|---|
| 20 | |
|---|
| 21 | librt = ctypes.CDLL('librt.so.1', use_errno=True) |
|---|
| 22 | clock_gettime = librt.clock_gettime |
|---|
| 23 | clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] |
|---|
| 24 | |
|---|
| 25 | def monotonic_time(): |
|---|
| 26 | t = timespec() |
|---|
| 27 | if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0: |
|---|
| 28 | errno_ = ctypes.get_errno() |
|---|
| 29 | raise OSError(errno_, os.strerror(errno_)) |
|---|
| 30 | return t.tv_sec + t.tv_nsec * 1e-9 |
|---|
| 31 | |
|---|
| 32 | # this is platform dependent |
|---|
| 33 | else: |
|---|
| 34 | raise NotImplementedError |
|---|
| 35 | |
|---|
| 36 | if __name__ == "__main__": |
|---|
| 37 | print monotonic_time() |
|---|