tag:blogger.com,1999:blog-5246987755651065286.post7685139167430536241..comments2024-02-22T16:15:42.388-08:00Comments on cbloom rants: 03-19-13 - Windows Sleep Variationcbloomhttp://www.blogger.com/profile/10714564834899413045noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-5246987755651065286.post-87034141982324828652013-04-03T08:33:55.390-07:002013-04-03T08:33:55.390-07:00Cool; interesting to know that it's actually r...Cool; interesting to know that it's actually rounding *down* not to nearest.<br /><br />I wish the MSDN docs were a bit better at identifying changes over time.cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-89762542543591214932013-04-02T19:40:33.374-07:002013-04-02T19:40:33.374-07:00I did the xperf test to see what was happening. Sl...I did the xperf test to see what was happening. Sleep() itself is (modulo the XP/later-versions change) ridiculously consistent.<br /><br />http://randomascii.wordpress.com/2013/04/02/sleep-variation-investigated/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-78313142663209736832013-03-27T06:23:01.511-07:002013-03-27T06:23:01.511-07:00That was an impressive deflection of my dickiness!...That was an impressive deflection of my dickiness! Well done, sir.<br /><br />Yeah that would be interesting. I may try it...cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-35707678790193510552013-03-26T20:17:58.496-07:002013-03-26T20:17:58.496-07:00It makes me want to record an xperf trace to see w...It makes me want to record an xperf trace to see where the variation is coming from. On an unloaded multi-core system I would expect really low variance on all systems. Windows 7 and Windows 8 both allow recording enough xperf/ETW data to figure out exactly what is going on.<br /><br />An xperf trace shows every context switch for every process on the system so you can see when your process is woken, early, late, whatever, and why.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-73707210038403827492013-03-26T16:43:24.329-07:002013-03-26T16:43:24.329-07:00@nothings - I think the main issue is that the tes...@nothings - I think the main issue is that the tests basically do sleep-sleep-sleep over and over, so the first one is going to get aligned with the timer interrupt, and then after that they fall roughly on alignment.<br /><br />I think WinXP's behavior is easiest to explain. What seems to be happening in the WinXP case is the test gets roughly aligned to the interrupt. The thread wakes up and then says Sleep(2) again. The OS puts us to sleep for *at least* 2 millis. Because there's some time passed since the wake up, that means we will sleep 2 time intervals plus almost one more full interval.<br /><br />Thus on WinXP the sleep times are all just under n+1 and the sdev is quite small because we that almost exactly each time.<br /><br />That's the behavior I would have guessed for all Windows. The surprise was the variation.<br /><br />And to be clear once again : this post is just a bit of tech trivia amusement, not a serious issue anyone should care about.<br />cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-35180917581338488202013-03-26T16:38:57.202-07:002013-03-26T16:38:57.202-07:00Gee thanks Bruce, that really added a lot.
Gee thanks Bruce, that really added a lot.<br />cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-11331509585316603042013-03-26T16:32:46.188-07:002013-03-26T16:32:46.188-07:00Remember that Sleep() on Windows is driven by a 1 ...Remember that Sleep() on Windows is driven by a 1 KHz timer. Therefore the implementation has too choose between rounding up and rounding down when choosing which timer interrupt to wake up on. Windows XP rounds up, which makes for a minimum sleep of 2 ms. Windows 7 appears to round down.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-19922836764782112442013-03-21T04:32:36.491-07:002013-03-21T04:32:36.491-07:00Did you make sure to delay a random amount of time...Did you make sure to delay a random amount of time so the Sleep start time was decorrelated from any regular scheduling timer?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-54404717475085914822013-03-19T16:14:52.963-07:002013-03-19T16:14:52.963-07:00Eh, no thanks.
The C++11 api doc there says that ...Eh, no thanks.<br /><br />The C++11 api doc there says that the sleep must be >= the specified time. That means they shouldn't be calling Sleep(n) at all, which has always been unreliable; they should be using the win32 waitable timer mechanism, which does at least guarantee the >= inequality.<br /><br />BTW for the record : no (normal) app should ever Sleep(n) (or sleep_for or sleep_until). Never ever; it's broken code.<br /><br />(I use it in my thread stress test; the main Oodle code doesn't ever sleep(n))<br />cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-25630075842381198542013-03-19T15:51:08.684-07:002013-03-19T15:51:08.684-07:00Interesting!
I wonder, could you try these out an...Interesting!<br /><br />I wonder, could you try these out and compare the results:<br />- http://en.cppreference.com/w/cpp/thread/sleep_for<br />- http://en.cppreference.com/w/cpp/thread/sleep_until<br />// Just out of curiosity.Matthttps://www.blogger.com/profile/03155815661102270775noreply@blogger.com