Repository navigation
Description
What version of Go are you using (go version)?
go version go1.20.5 windows/amd64 - "new"
go version go1.15.13 windows/amd64 - "old"
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
windows_amd64 - Windows 10 Pro, 22H2, Build 19045.2965
windows_amd64 - Windows 7 Ultimate, 6.1.7601, Service Pack 1, Build 7601
What did you do?
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println()
fmt.Println("Time Resolution")
for i := 0; i < 10; i++ {
fmt.Printf(" %v\n", timeDiff())
}
fmt.Println()
fmt.Println("Sleep Resolution")
time.Sleep(1) // sync with the sleep timer
for i := 0; i < 10; i++ {
fmt.Printf(" %v\n", sleepDiff())
}
}
func timeDiff() time.Duration {
t0 := time.Now()
for {
t := time.Now()
if t != t0 {
return t.Sub(t0)
}
}
}
func sleepDiff() time.Duration {
t0 := time.Now()
time.Sleep(1)
return time.Now().Sub(t0)
}What did you expect to see?
Results using Go v1.15.13 or v1.20.5 code on Windows 7.
Time Resolution is fine. Sleep Resolution is fine.
Time Resolution
1.0001ms
1ms
1.0001ms
1ms
1.0001ms
1ms
1.0001ms
1.0001ms
1ms
1.0001ms
Sleep Resolution
1ms
1.0001ms
1.0001ms
1ms
1.0001ms
1ms
1.0001ms
1ms
1.0001ms
1.0001ms
What did you see instead?
Results using Go v1.20.5 code on Windows 10.
Time Resolution is fine. Sleep Resolution is bad. It should be ~1ms.
Time Resolution
509.5Β΅s
555.1Β΅s
587.7Β΅s
95.9Β΅s
585.7Β΅s
585.4Β΅s
596.7Β΅s
514.4Β΅s
514.4Β΅s
514.1Β΅s
Sleep Resolution
15.2307ms
34.8Β΅s
13.9486ms
14.8512ms
15.1862ms
15.0722ms
15.7843ms
15.6718ms
898.4Β΅s
14.525ms
Results using Go v1.15.13 code on Windows 10.
Time Resolution is ok. Sleep Resolution is ok. Neither are as good as when run on Windows 7, but they are certainly usable.
Time Resolution
1.0394ms
1.0505ms
918.6Β΅s
1.022ms
997.7Β΅s
1.0016ms
1.0788ms
986.7Β΅s
1.0057ms
993.9Β΅s
Sleep Resolution
1.0853ms
1.1475ms
1.1202ms
995.8Β΅s
1.211ms
1.2033ms
1.2386ms
1.2209ms
1.0501ms
1.1822ms
=== Comments
Comments:
A problem I was having with a program I was working on led me to this issue.
So yes, it makes a difference in the real world.
I don't have a Windows 11 PC.
It would be interesting to see if it has the problem or not.
The "new" code was built on the Windows 10 PC.
The "old" code was build on the Windows 7 PC.
Both Windows PC clocks were running at 1ms due to other software on them.
This was verified using TimerTool.exe.
But this is moot, since the very first Sleep should have caused Go's auto-clock-switch to force it to 1ms and keep it there till 50ms after the last Sleep.
The Windows 10 PC is about 5 times faster than the much older Windows 7 PC.
Yet the results from the older PC are much "cleaner" (less latency error).
The "new" (I didn't try old) code was fine on both a Linux server and Raspberry Pi 4.
Activity
[-]affected/package: runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/-][+]runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/+][-]runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/-][+]runtime: time.Sleep problem on Windows 10, but not Windows 7 or Linux[/+]ianlancetaylor commentedon Jun 27, 2023
CC @golang/windows
seankhliao commentedon Jun 27, 2023
is this #44343 ?
johnrs commentedon Jun 27, 2023
I think that it's possible that they might be related, but the symptoms seem pretty different.
mknyszek commentedon Jun 28, 2023
I suspect they are the same issue (especially given the Go version ranges). Could you elaborate on which of the symptoms are different? Thanks.
Also, if you're so inclined, it should be straightforward to identify if it's the same issue by trying 8fdc79e and the commit before.
johnrs commentedon Jun 28, 2023
I stand corrected. I didn't read it carefully enough. Sorry.
My gut feeling is that there are 2 issues happening at the same time. One is the Windows auto-switch from 15.6ms to 1ms is broken. And the other is the "noisy" results, some even LESS than the period (that should only happen on a random sleep, not then they are consecutive (except for the first).
Still, it's VERY strange that I don't see either issue when I run the test on Windows 7.
12 remaining items
johnrs commentedon Jul 4, 2023
@ChrisHines Interesting. When I search #44343 for osRelax I don't see much mention of it. Could you point me to where the counter arguments against restoring it are? Thanks.
ChrisHines commentedon Jul 5, 2023
@johnrs Looking for mentions of
timeBeginPeriodmight be better.Here are a few comments worth reading:
I admit there is less debate about it in that thread than I remembered. I think I was confusing it with this other thread that I had come across contemporary to my previous work on runtime timers. You will probably remember this thread, as you commented there more than once. The part I think is most relevant starts here:
johnrs commentedon Jul 5, 2023
@ChrisHines Thanks!
The relevant part that you referred to is what the auto switch (os.Relax()) mostly resolved. Also, given the current Windows 10, a Go program's Windows clock setting shouldn't affect any other programs.
I certainly like the goal of avoid using the Windows clock entirely, but I don't understand why os.Relax() hasn't been put back in use till the eventual goal is achieved.
ChrisHines commentedon Jul 5, 2023
I can't answer that, but I think it's a valid question.
johnrs commentedon Jul 5, 2023
What's the procedure to make it happen? Open a new issue proposing it?
ChrisHines commentedon Jul 5, 2023
Perhaps. Or add a new comment to #44343 with your suggestion. I suspect a new issue would get marked as a duplicate of that one again but maybe it wouldn't if it was written as a proposal for a specific change rather than a problem report.
johnrs commentedon Jul 6, 2023
Thanks. Will post on #44343 and see what happens.
johnrs commentedon Jul 13, 2023
@ChrisHines
No response. What's the procedure for a proposal?
Thanks.
ChrisHines commentedon Jul 14, 2023
@johnrs The procedure is documented here: https://github.com/golang/proposal
johnrs commentedon Jul 14, 2023
Thanks!
jxqu3 commentedon Oct 29, 2023
I'm working in an autoclicker in Windows 11 and I'm having a similar problem. If I execute mousedown and mouseup without a
time.Sleep, I get thousands of clicks per second, but as soon as I add atime.Sleep, even if it'stime.Sleep(1 * time.Nanosecond)it goes down to 66 clicks per second, which is about 15.15ms of sleep.johnrs commentedon Oct 29, 2023