On Linux >2.6.28, use CLOCK_MONOTONIC_RAW

Closes #1
This commit is contained in:
Ori Livneh 2015-06-19 10:59:38 -07:00
parent 1956c44008
commit 1e0c8a3de5
1 changed files with 19 additions and 3 deletions

View File

@ -38,17 +38,30 @@
limitations under the License.
"""
from __future__ import absolute_import, division
import ctypes
import ctypes.util
import os
import platform
import re
import sys
import time
__all__ = ('monotonic',)
def get_os_release():
"""Get the leading numeric component of the OS release."""
return re.match('[\d.]+', platform.release()).group(0)
def compare_versions(v1, v2):
"""Compare two version strings."""
def normalize(v):
return map(int, re.sub(r'(\.0+)*$', '', v).split('.'))
return cmp(normalize(v1), normalize(v2))
try:
monotonic = time.monotonic
except AttributeError:
@ -99,7 +112,10 @@ except AttributeError:
ts = timespec()
if sys.platform.startswith('linux'):
CLOCK_MONOTONIC = 1
if compare_versions(get_os_release(), '2.6.28') > 0:
CLOCK_MONOTONIC = 4 # CLOCK_MONOTONIC_RAW
else:
CLOCK_MONOTONIC = 1
elif sys.platform.startswith('freebsd'):
CLOCK_MONOTONIC = 4
elif 'bsd' in sys.platform: