Jump to: navigation, search

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

(Created page with "== 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...")
 
Line 11: Line 11:
 
== Use time.monotonic() ==
 
== Use time.monotonic() ==
  
Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time():
+
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()].
https://www.mail-archive.com/openstack-dev@lists.openstack.org/msg18697.html
 
  
 
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 18: 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:
+
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]
https://review.openstack.org/#/c/71003/
 
  
 
== 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):
+
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.
https://review.openstack.org/#/c/85717/
 
  
 
== Alternatives ==
 
== Alternatives ==
Line 31: Line 28:
 
** 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)."
 
** 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?
 
** Monotime module doesn't support Windows nor Solaris. Does it matters for Oslo Incubator?
 
+
** It doesn't reuse time.monotonic() if available.
There is also python-monotonic-time, but it doesn't look to be available on PyPI: https://github.com/gavinbeatty/python-monotonic-time
+
* python-monotonic-time
 +
** It doesn't look to be available on PyPI: https://github.com/gavinbeatty/python-monotonic-time

Revision as of 14:47, 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.

For a longer rationale, read the 418.

Use time.monotonic()

Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): 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: 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): 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