Skip to content

Which clock should we use on Windows? #33

Closed

Description

@njsmith

Right now we use time.monotonic everywhere. This is a bit problematic on Windows, where time.monotonic is GetTickCount64, which has ~15 ms resolution. The other option is QueryPerformanceCounter, which has much higher resolution, is also monotonic, and is exposed as time.perf_counter (checked with time.get_clock_info).

The reason for this appears to be that QueryPerformanceCounter has had a troubled past: https://www.python.org/dev/peps/pep-0418/#windows-queryperformancecounter

But all these issues are like "Virtualbox had a bug that was fixed 6 years ago" or "Windows XP is flaky" which is probably true but irrelevant since we don't support it – it's not clear that any of them apply anymore.

Advantages of using a higher-precision clock:

  • Right now if there's just one task running and it does a sleep for t seconds, the actual timeout passed to the underlying system call will be (t + current_time()) - current_time(), which can be pretty imprecise depending on whether the clock ticks over between the two calls. OTOH I don't know what the actual resolution of our sleeping syscall is (currently GetQueuedCompletionStatusEx), or whether anyone cares about millisecond accurate sleeps.

  • I've had two bugs already with tests that assumed that time always, like, passes. These were trivial (like replacing a < with a <=), but it's always annoying to have the tests pass locally then fail on appveyor.

  • If we implement a fancier scheduling system (Design: alternative scheduling models #32) then we'll definitely need better than 15 ms precision to measure tasks running. (Though there's no reason that the clock we use for that has to match the clock we use for deadlines.)

Activity

  1. njsmith commented on Jan 23, 2017

    @njsmith
    MemberAuthor
  2. njsmith commented on Sep 8, 2018

    @njsmith
    MemberAuthor

    @tkerwin reported in chat that they have a program that misbehaves on windows if trio uses time.monotonic, but works correctly if trio uses time.perf_counter. This seems like a pretty good argument for switching over to me.

    How risky is it? It looks like libuv uses QueryPerformanceCounter exclusively (that's what uv__hrtime uses, and I don't see any other timer calls in the libuv source code). Chromium uses QueryPerformanceCounter if the CPU is new enough to have a stable rdtsc (ref) so QPC can be fast. A comment notes that in August 2015, this was true for 72% of Chrome's userbase, and it's presumably only gone up in the three years since then.

    I guess I'd still like to see the output from these commands on some Windows system:

    python -m timeit -s "from time import monotonic" "monotonic()" 
    python -m timeit -s "from time import perf_counter" "perf_counter()"
    

    (On my linux laptop, they both give ~0.05 µs/loop)

  3. tkerwin commented on Sep 11, 2018

    @tkerwin
    Contributor

    On Windows 10, with an i5-3570:

    λ python -m timeit -s "from time import monotonic" "monotonic()"
    5000000 loops, best of 5: 78 nsec per loop
    
    λ python -m timeit -s "from time import perf_counter" "perf_counter()"
    2000000 loops, best of 5: 125 nsec per loop
    
  4. njsmith commented on Sep 12, 2018

    @njsmith
    MemberAuthor

    Meh, 50 ns is like an attribute lookup or two, I think spending that once or twice per pass through the run loop is fine if it's giving better user-visible behavior.

    @tkerwin Any interest in putting together a PR?

  5. added a commit that references this issue on Sep 29, 2018
  6. njsmith commented on Sep 29, 2018

    @njsmith
    MemberAuthor

    Resolved in #33

  7. pquentin commented on Sep 29, 2018

    @pquentin
    Member

    And in #682 :)

  8. pquentin commented on Nov 16, 2018

    @pquentin
    Member

    For reference, on macOS 10.14 with a mid-2014 Macbook Pro:

    $ python -m timeit -s "from time import monotonic" "monotonic()"
    10000000 loops, best of 3: 0.0779 usec per loop
    
    $ python -m timeit -s "from time import perf_counter" "perf_counter()"
    10000000 loops, best of 3: 0.0796 usec per loop
    

    It also takes about 80nsec per loop.

  9. njsmith commented on Nov 16, 2018

    @njsmith
    MemberAuthor

    @pquentin That makes sense. There's some confusing indirection to trace through, but if I'm reading it right, when CPython is built for anything besides Windows, monotonic and perf_counter end up calling the exact same code internally.

  10. added a commit that references this issue on Feb 1, 2019
  11. NewUserHa commented on May 4, 2022

    @NewUserHa

    to adds some info for the thread: Python 3.11 is going to fix time.monotonic() on Windows now too.

    detailed content as always
    thank @njsmith !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions