Skip to content

Change to high-resolution clock per #33 - #682

Merged
njsmith merged 4 commits into
python-trio:masterfrom
tkerwin:master
Sep 29, 2018
Merged

njsmith merged 4 commits into
python-trio:masterfrom
tkerwin:master

Conversation

@tkerwin

@tkerwin tkerwin commented Sep 25, 2018

Copy link
Copy Markdown
Contributor

Per the discussion in issue #33 this changes the clock to use time.perf_counter() rather than time.monotonic() on all platforms.

@njsmith

njsmith commented Sep 26, 2018

Copy link
Copy Markdown
Member

The core change here looks good, and fixes #33. Thanks! But I'm going to request a few tweaks :-).

I had to go read the CPython source code to figure out what perf_counter actually does (the docs are kind of vague). It turns out that every platform except Windows, it's exactly the same as time.monotonic; and on Windows, it uses QueryPerformanceCounter instead of GetTickCount64. We should probably add a comment about this so that the next person to see it doesn't have to go spelunking through the CPython source code.

You should add a newsfragment, e.g. newsfragments/33.feature.rst: "On Windows, the Trio main loop now uses a higher-precision clock by default."

It looks like there are a bunch of other places in the Trio source code that still use time.monotonic:

~/trio/trio$ rg monotonic
testing/_mock_clock.py
95:        self._real_clock = time.monotonic

tests/test_timeouts.py
12:    start = time.monotonic()
14:    dur = time.monotonic() - start
28:    # meant that our calls to time.monotonic() returned exactly the same

tests/test_testing.py
313:    real_start = time.monotonic()
323:    real_duration = time.monotonic() - real_start
381:    real_start = time.monotonic()
383:    assert time.monotonic() - real_start < 1

_core/_run.py
14:from time import monotonic
96:    # accidentally call time.monotonic() directly or start comparing clocks
104:        return self.offset + monotonic()
1236:      clock: ``None`` to use the default system-specific monotonic clock;

_core/_parking_lot.py
42:# global monotonic counter that ensures FIFO ordering. The main advantage of

_core/tests/test_run.py
256:    time.sleep(time.get_clock_info("monotonic").resolution)
837:        real_start = time.monotonic()
841:        real_duration = time.monotonic() - real_start
845:        # (== is possible because time.monotonic on Windows is really low res)

_core/tests/test_ki.py
584:    start = time.monotonic()
588:        end = time.monotonic()

I think these should all become perf_counter as well?

CI failures should be fixed by #683, so no need to worry about those.

@codecov

codecov Bot commented Sep 27, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #682 into master will increase coverage by 0.2%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #682     +/-   ##
=========================================
+ Coverage   83.77%   83.97%   +0.2%     
=========================================
  Files          94       94             
  Lines       13046    13006     -40     
  Branches      788      783      -5     
=========================================
- Hits        10929    10922      -7     
+ Misses       2096     2062     -34     
- Partials       21       22      +1
Impacted Files Coverage Δ
trio/testing/_mock_clock.py 88.37% <100%> (ø) ⬆️
trio/_core/_run.py 76.74% <100%> (+0.08%) ⬆️
trio/tests/test_testing.py 95.07% <100%> (+0.33%) ⬆️
trio/_core/tests/test_ki.py 87.94% <100%> (ø) ⬆️
trio/tests/test_timeouts.py 100% <100%> (ø) ⬆️
trio/_core/tests/test_run.py 96.21% <100%> (ø) ⬆️
trio/socket.py 64.86% <0%> (-35.14%) ⬇️
trio/tests/test_sync.py 93.92% <0%> (ø) ⬆️
trio/_sync.py 96.12% <0%> (ø) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 46f4405...d73d44e. Read the comment docs.

@njsmith njsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is almost there! A last few tweaks:

Comment thread trio/_core/_run.py
args: Positional arguments to be passed to *async_fn*. If you need to
pass keyword arguments, then use :func:`functools.partial`.

clock: ``None`` to use the default system-specific monotonic clock;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just referring to the general concept of a "monotonic clock", so should stay the same.


Every call to :func:`run` has an associated clock.

By default, trio uses an unspecified monotonic clock, but this can be

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should stay the same – it's talking about the general guarantees we make about the clock, and we still don't guarantee anything except that it has the property of monotonicity. (This is why we can change from time.monotonic to time.perf_counter :-).)

Comment thread newsfragments/33.feature.rst Outdated
@@ -0,0 +1 @@
The Trio main loop now uses a higher-precision clock by default. No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should say "on Windows", since that's the only platform affected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the blanket statement in the newsfragments is correct, since we change the function without regard for platform. However, we should add a sentence that notes that this only affects Windows platforms due to the details of how cPython is implemented.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is: we use a different function on all platforms, but on most platforms this doesn't change behavior at all, so it's not of interest to end users. The only place where precision changes is on Windows.

@njsmith

njsmith commented Sep 29, 2018

Copy link
Copy Markdown
Member

@tkerwin I edited the newsfragment again, see what you think?

@tkerwin

tkerwin commented Sep 29, 2018

Copy link
Copy Markdown
Contributor Author

Yeah, that's good.

@njsmith
njsmith merged commit c6dcfdd into python-trio:master Sep 29, 2018
@njsmith

njsmith commented Sep 29, 2018

Copy link
Copy Markdown
Member

Thanks, and welcome! 🎉 🎂 And, no pressure, but if you'd like to keep contributing then we'd love to have you, so I'm sending you a github invite now. You can read more about this in our contributing documentation.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants