-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: Sleep(1 * time.Nanosecond) pauses at least 1.9ms on Windows 10 or Windows 7 #29485
Comments
@40345839668 Most people who use this issue tracker can not read Turkish. Please comment in English. Thanks. @destinysync A sleep of one nanosecond is unlikely to work on any system. A sleep is always going to be limited by the clock frequency of the system. |
@ianlancetaylor , not only 1 * time.Nanosecond, any time.Sleep duration less than 1.9ms, e.g., 500 * time.Microsecond, 1 * time.Millisecond, pauses at least 1.9ms. My computer's CPU is Intel i5 8250U, which has 4 cores, 8 threads, and a max turbo speed of 3.4 GHz, when I tested on the same hardware inside virtual machine with Ubuntu desktop 18.04 operating system , time: Sleep(1 * time.Nanosecond) only pauses 26.079µs, while outside the virtual machine on a Windows 10 system, time: Sleep(1 * time.Nanosecond) pauses 1.9951ms, so the root of the problem might not be hardware related. the difference between 1.9951ms and 26.079µs does not seem huge on normal occasions, but I am trying to build a reliable protocol on top of UDP, due to 1.9951ms delay per datagram , transferring a file with a size of only 10Mb between a UDP client and a UDP server on the same machine takes more than 15 seconds, in comparison, TCP finishes the task within 0.5 seconds. |
This is what I see on my Windows computer too. 1ms sleep for time.Sleep(time.Nanosecond) sounds about right for current Windows time.Sleep implementation. If you have suggestion on how to improve the code, you are most welcome. Alex |
I was imprecise in referring to the clock frequency. I didn't mean the frequency of the processor. I meant the frequency of the operating system: how often it does timer interrupts. It's entirely possible that there is something to be fixed here. But |
@destinysync what does this yield?
|
yields:
|
@destinysync |
That shows a possible resolution of 100ns (0.0001ms). Go may be calling an obsolete Windows API which doesn't support that. Windows 7 supports 100ns resolution timers: EDIT: Go uses SetWaitableTimer for the profiler. Windows 2000 supports 100ns resolution sleep: EDIT: A thread does not resume when the above call's Interval has elapsed; it becomes eligible for execution. |
Related: #8687 |
@destinysync I suggest revising this issue to cover time.After/NewTimer since its precision is probably more important than time.Sleep. And time.After/AfterFunc is the right tool for protocol pauses. EDIT: Possibly related: #17696 |
@alexbrainman @destinysync I suggested changing this issue to be about time.After/NewTimer. If you prefer, I'll open a separate issue regarding resolution of time.NewTimer on Windows; please let me know! |
I do not know how to fix this issue. Regardless of what description of this issue is. Alex |
SetWaitableTimer claims 100ns resolution; Go currently uses it in the profiler but not elsewhere (that I could find). Wouldn't that improve time.NewTimer/After over its current ~2ms resolution? https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-setwaitabletimerex EDIT: hardware background... |
Currently The function is documented as " |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/zbe1kiCREc0
time.Sleep function works correctly on Go Playground and Ubuntu desktop 18.04, but when on Windows 10 or Windows 7, time.Sleep pauses at least 1.9ms
What did you expect to see?
1ns
What did you see instead?
1.9951ms
The text was updated successfully, but these errors were encountered: