@cryptotaxi247 / netdata-1 / commits / 88a027efd

add monotonic time func for py2 (https://stackoverflow.com/a/1205762)

lgz committed Jun 1, 2018 at 14:24 UTC 88a027efd94de2b84396980818be7d2ff875ad5e
1 file changed +29
python.d/python_modules/bases/monotonic.py new
+29
@@ -0,0 +1,29 @@
1 +# thx to https://stackoverflow.com/a/1205762
2 +import ctypes
3 +# import os
4 +
5 +CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
6 +
7 +
8 +class _Timespec(ctypes.Structure):
9 + _fields_ = [
10 + ('tv_sec', ctypes.c_long),
11 + ('tv_nsec', ctypes.c_long)
12 + ]
13 +
14 +
15 +_librt = ctypes.CDLL('librt.so.1', use_errno=True)
16 +_clock_gettime = _librt.clock_gettime
17 +_clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(_Timespec)]
18 +
19 +
20 +def time():
21 + t = _Timespec()
22 + if _clock_gettime(CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0:
23 + # errno_ = ctypes.get_errno()
24 + # raise OSError(errno_, os.strerror(errno_))
25 + return None
26 + return t.tv_sec + t.tv_nsec * 1e-9
27 +
28 +
29 +AVAILABLE = bool(time())