Jump to: navigation, search

Difference between revisions of "Oslo/blueprints/time monotonic"

(Rationale)
 
Line 7: Line 7:
 
Using time.time() to compute timeouts is wrong. The elapsed time may be wrong on update of the system clock. A monotonic clock should be used to avoid this issue. I propose to add a timeutils.time_monotonic() function. It will use time.monotonic() if available, or fallback to clock_getclock(CLOCK_MONOTONIC) on Linux.
 
Using time.time() to compute timeouts is wrong. The elapsed time may be wrong on update of the system clock. A monotonic clock should be used to avoid this issue. I propose to add a timeutils.time_monotonic() function. It will use time.monotonic() if available, or fallback to clock_getclock(CLOCK_MONOTONIC) on Linux.
  
For a longer rationale, read the [http://legacy.python.org/dev/peps/pep-0418/|PEP 418].
+
For a longer rationale, read the [http://legacy.python.org/dev/peps/pep-0418/ PEP 418].
  
 
== Use time.monotonic() ==
 
== Use time.monotonic() ==
  
Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): [https://www.mail-archive.com/openstack-dev@lists.openstack.org/msg18658.html|openstack-dev: time.sleep is affected by eventlet.monkey_patch()].
+
Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): [https://www.mail-archive.com/openstack-dev@lists.openstack.org/msg18658.html openstack-dev: time.sleep is affected by eventlet.monkey_patch()].
  
 
For greenlet, the monotonic clock can be used with something like that:
 
For greenlet, the monotonic clock can be used with something like that:
Line 17: Line 17:
 
eventlet.hubs._threadlocal.hub = eventlet.hubs.get_default_hub().Hub(monotonic_time)
 
eventlet.hubs._threadlocal.hub = eventlet.hubs.get_default_hub().Hub(monotonic_time)
  
time_monotonic() should be used for any function computing a timeout. Recent example: [https://review.openstack.org/#/c/71003/|oslo.messaging: Add an optional timeout parameter to Listener.poll]
+
time_monotonic() should be used for any function computing a timeout. Recent example: [https://review.openstack.org/#/c/71003/ oslo.messaging: Add an optional timeout parameter to Listener.poll]
  
 
== Implementation ==
 
== Implementation ==
  
The following patch is based on the file time_monotonic.py from my Trollius project. Trollius is tested on Linux, Mac OS X, Windows, FreeBSD and OpenIndiana (which is almost Solaris): [https://review.openstack.org/#/c/85717/|timeutils: add time_monotonic() function]. The reimplement reuses time.monotonic() if available.
+
The following patch is based on the file time_monotonic.py from my Trollius project. Trollius is tested on Linux, Mac OS X, Windows, FreeBSD and OpenIndiana (which is almost Solaris): [https://review.openstack.org/#/c/85717/ timeutils: add time_monotonic() function]. The reimplement reuses time.monotonic() if available.
  
 
== Alternatives ==
 
== Alternatives ==

Latest revision as of 14:53, 7 April 2014

Authors

  • Victor Stinner - victor.stinner@enovance.com

Rationale

Using time.time() to compute timeouts is wrong. The elapsed time may be wrong on update of the system clock. A monotonic clock should be used to avoid this issue. I propose to add a timeutils.time_monotonic() function. It will use time.monotonic() if available, or fallback to clock_getclock(CLOCK_MONOTONIC) on Linux.

For a longer rationale, read the PEP 418.

Use time.monotonic()

Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): openstack-dev: time.sleep is affected by eventlet.monkey_patch().

For greenlet, the monotonic clock can be used with something like that:

eventlet.hubs._threadlocal.hub = eventlet.hubs.get_default_hub().Hub(monotonic_time)

time_monotonic() should be used for any function computing a timeout. Recent example: oslo.messaging: Add an optional timeout parameter to Listener.poll

Implementation

The following patch is based on the file time_monotonic.py from my Trollius project. Trollius is tested on Linux, Mac OS X, Windows, FreeBSD and OpenIndiana (which is almost Solaris): timeutils: add time_monotonic() function. The reimplement reuses time.monotonic() if available.

Alternatives

  • https://pypi.python.org/pypi/Monotime
    • On Linux, it uses CLOCK_MONOTONIC_RAW which is wrong according to my PEP 418. Extract: "Slewing is generally desirable (i.e. we should use CLOCK_MONOTONIC, not CLOCK_MONOTONIC_RAW) if one wishes to measure "real" time (and not a time-like object like CPU cycles)."
    • Monotime module doesn't support Windows nor Solaris. Does it matters for Oslo Incubator?
    • It doesn't reuse time.monotonic() if available.
  • python-monotonic-time