Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time, runtime: use QueryPerformanceCounter in time.Now on Windows to improve time resolution #67066

Open
egonelbre opened this issue Apr 26, 2024 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows Performance
Milestone

Comments

@egonelbre
Copy link
Contributor

egonelbre commented Apr 26, 2024

Currently time.Now is using a low precision time, which makes a lot of duration measurements imprecise. Currently there seems to be only QueryPerformanceCounter, which provides sub-microsecond precision for measurements. This lead to replacing time measurement workarounds in benchmarks (#31160).

It's clear that using in benchmarking context QPC is fine, because that's what https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps recommends. However it's less clear whether there are problems with measuring longer durations (e.g. several years) or whether there are peculiarities with call overhead.

To summarize, the open questions that would need answering before it's reasonable to change time.Now behavior are:

  1. What's the reasoning QueryPerformanceCounter wasn't used from the start?
    • As far as I understand, there were older Windows versions and WINE that needed more handholding and using QPC introduced problems. If that's the case, then are these problems still present in versions supported by Go.
  2. Are there problems measuring long durations with QPC, e.g. 2 years. (runtime: windows system clock resolution issue #8687 (comment) does mention it's not stable long-term)
  3. Are there problems with QPC taking significantly longer on some systems (e.g. 1000ns)? e.g. maybe some system using high resolution timer. I don't know there are any such problems, but then again the documentation doesn't seem to provide guarantees.
    • The call overhead to QPC seems to be ~20ns-40ns on my system, compared to the current time.Now 4.5ns.
  4. Are there any problems with regards to system sleeping and waking?

If this is implemented then the change in https://go-review.googlesource.com/c/go/+/557315 can be reverted.

Note, this would be only for the monotonic time measurements.

Related issue for benchmarking:

@qmuntal
Copy link
Contributor

qmuntal commented Apr 26, 2024

Thanks for filling this issue. @gdams @dagood we should forward the open questions to the Windows kernel team.

@cherrymui cherrymui changed the title time: use QueryPerformanceCounter in time.Now on Windows to improve time resolution time, runtime: use QueryPerformanceCounter in time.Now on Windows to improve time resolution Apr 26, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 26, 2024
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 26, 2024
@cherrymui cherrymui added this to the Backlog milestone Apr 26, 2024
@ldemailly
Copy link

ldemailly commented Sep 25, 2024

+100 - thanks for writing this up Egon! (I was going to open a new one when I saw #8687 was closed without a fix)

I have had to use https://github.com/loov/hrtime meanwhile but this should really be transparent without needing special libraries or build tag to work on windows to make a simple elapsed time measurement (even if it's mildly complicated to implement, that's the beauty of cross platform stdlib, hide the complexities)

fortio/terminal#43
fortio/fortio#396

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows Performance
Projects
Development

No branches or pull requests

6 participants