classification
Title: Implement time.monotonic() on Mac OS X
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: haypo, ned.deily, nicholas.riley, pitrou, python-dev, ronaldoussoren
Priority: normal Keywords: patch

Created on 2012-02-23 23:09 by haypo, last changed 2012-03-12 23:29 by haypo. This issue is now closed.

Files
File name Uploaded Description Edit
monotonic.patch nicholas.riley, 2012-03-12 21:03 review
monotonic-2.patch nicholas.riley, 2012-03-12 23:18 review
Messages (8)
msg154095 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-02-23 23:09
time.monotonic() can use mach_absolute_time() on Mac OS X. mach_timebase_info() may be used to convert the result to a number of seconds. Examples:

https://github.com/ThomasHabets/monotonic_clock/blob/master/src/monotonic_mach.c
http://svn.boost.org/svn/boost/trunk/boost/chrono/detail/inlined/mac/chrono.hpp
(search steady_clock)

Another way is to use clock_get_time() with host_get_clock_service(SYSTEM_CLOCK). Example:

https://github.com/gavinbeatty/python-monotonic-time/blob/master/darwin.c
msg155478 - (view) Author: Nicholas Riley (nicholas.riley) * Date: 2012-03-12 21:03
Attached is a patch which implements time.monotonic() using mach_absolute_time() / mach_timebase_info().  This was recommended by Apple in their technical Q&A 1398:

https://developer.apple.com/library/mac/#qa/qa1398/
msg155502 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-12 22:42
> Attached is a patch which implements time.monotonic() using
> mach_absolute_time() / mach_timebase_info().

Is it possible that mach_timebase_info() fails? No according to the Technical Q&A QA1398.

> time * timebase.numer / timebase.denom

"time * timebase.numer" may overflow, you should maybe multiply using the double type to avoid the overflow.
msg155507 - (view) Author: Nicholas Riley (nicholas.riley) * Date: 2012-03-12 22:54
timebase.numer and timebase.denom are always 1 in practice (see the "note" in the tech Q&A); I included them for completeness, but maybe it's just better to not bother?
msg155511 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-12 22:58
> timebase.numer and timebase.denom are always 1 in practice

In this case, you will not lose precision and it's safe to cast to double to multiply and divide ;-)
msg155515 - (view) Author: Nicholas Riley (nicholas.riley) * Date: 2012-03-12 23:18
OK, you have a point :-)  New patch attached.
msg155520 - (view) Author: Roundup Robot (python-dev) Date: 2012-03-12 23:27
New changeset 3c875719e46d by Victor Stinner in branch 'default':
Issue #14104: Implement time.monotonic() on Mac OS X,
http://hg.python.org/cpython/rev/3c875719e46d
msg155521 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-03-12 23:29
Path commited, thanks for the patch Nicholas!
History
Date User Action Args
2012-03-12 23:29:14hayposetmessages: + msg155521
2012-03-12 23:27:28hayposetstatus: open -> closed
resolution: fixed
2012-03-12 23:27:09python-devsetnosy: + python-dev
messages: + msg155520
2012-03-12 23:18:03nicholas.rileysetfiles: + monotonic-2.patch

messages: + msg155515
2012-03-12 23:02:00ned.deilysetstage: patch review
2012-03-12 22:58:37hayposetmessages: + msg155511
2012-03-12 22:54:32nicholas.rileysetmessages: + msg155507
2012-03-12 22:42:51hayposetmessages: + msg155502
2012-03-12 21:03:54nicholas.rileysetfiles: + monotonic.patch

nosy: + nicholas.riley
messages: + msg155478

keywords: + patch
2012-02-23 23:31:26pitrousetnosy: + ronaldoussoren, ned.deily
2012-02-23 23:09:14haypocreate