Montonic time functions

Module: dvbcss.monotonic_time

This module implements operating system specific access to high resolution monotonic system timers for the following operating systems:

  • Windows 2000 Pro or later
  • Linux
  • Mac OS X

It implements a time() function and sleep() function that work the same as the time.time() and time.sleep() functions in the python standard library.

It also adds a timeNanos() and timeMicros() variants that report time in units of nanoseconds or microseconds instead of seconds.

See operating system specific implementation details below to understand the limitations of the functions provided in this module.

Note

For all supported operating systems, the sleep() function is not guaranteed to use the same underlying timer as the time() and therefore should be considered inaccurate.

Example use

>>> import dvbcss.monotonic_time as monotonic_time
>>> monotonic_time.time()
4695.582637038
>>> monotonic_time.timeNanos()
4700164952506L
>>> monotonic_time.timeMicros()
4703471405L
>>> monotonic_time.sleep(0.5)   # sleep 1/2 second

Operating system implementation details

The precision and accuracy of the clocks and sleep functions are dependent on the host operating system and hardware. This module can therefore provide no performance guarantees.

Windows

Windows NT 5.0 (Windows 2000 Professional) or later is supported, including the cygwin environment.

The time() function and its variants are based on the QueryPerformanceCounter() high resolution timer system call. This clock is guaranteed to be monotonic and have 1 microsecond precision or better.

The sleep() function is based on the CreateWaitableTimer() and SetWaitableTimer() system calls.

Note that the sleep() function for Windows is not guaranteed to be accurate because it is not possible to create blocking (non polling) delays based on the clock source used. However it should have significantly higher precision than the standard 15ms windows timers and will be fine for short delays.

Mac OS X

The time() function and its variants are based on the mach_absolute_time() system call.

The clock is guaranteed to be monotonic. Apple provides no guarantees on precision, however in practice it is usually based on hardware tick counters in the processor or support chips and so is extremely high precision (microseconds or better).

The sleep() function is based on the nanosleep() system call. It is unclear whether this uses the same underlying counter as mach_absolute_time().

Linux

The time() function and its variants are based on the clock_gettime() system call requesting CLOCK_MONOTONIC.

The sleep() function is based on the nanosleep() system call. It is unclear whether this uses the same underlying counter as CLOCK_MONOTONIC.

Functions

time(), timeMicros() and timeNanos()

dvbcss.monotonic_time.time()[source]

Return monotonic time in seconds and fractions of seconds (as a float). The precision is operating system dependent.

dvbcss.monotonic_time.timeMicros()[source]

Return monotonic time in integer microseconds. The precision is operating system dependent.

dvbcss.monotonic_time.timeNanos()[source]

Return monotonic time in integer nanoseconds. The precision is operating system dependent.

sleep()

dvbcss.monotonic_time.sleep(t)[source]

Sleep for specified number of second and fractions of seconds (as a float). The precision is operating system dependent.

Throws TimeoutError:
 if the underlying system call used to sleep reported a timeout (OS dependent behaviour)
Throws InterruptedException:
 if a signal or other interruption is received while sleeping (OS dependent behaviour)

Note

For all supported operating systems, the sleep() function is not guaranteed to use the same underlying timer as the time() and therefore should be considered inaccurate.