TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Windows Timer Resolution: The Great Rule Change

102 pointsby nikbackmover 4 years ago

18 comments

trashfaceover 4 years ago
I tested my own system using Bruce&#x27;s &quot;measure_interval.cpp&quot; program (on Windows 1909):<p>- Slack (sometimes) sets the global timer to 1ms when it is in the foreground, but restores it in background<p>- Spotify sets the global timer to 1ms, no matter what. Even if it isn&#x27;t playing.<p>- Skype sets 1ms, if started at Startup (which it defaults to), even though I am logged out and it just has a tray icon. But when I manually start it, it doesn&#x27;t (always) set it to 1ms.<p>- VSCode will set it to 1ms when you are interacting with it, but will eventually revert to 15.6ms if left alone (even if it is still in foreground).<p>- Firefox doesn&#x27;t appear to set it (on its own; I presume that if I opened a tab that was using a low setTimeout or requestAnimFrame it might).<p>Spotify is interesting. A lot of people probably have that app, and since it sets 1ms unconditionally, it would have been setting fast-timer mode prior to the 2004 update, which could inadvertently &quot;speed up&quot; whatever games people were running.<p>That includes my own game, which uses a foreground sleep of as low as 1ms to try to hit its time target, and I don&#x27;t call timeBeginPeriod. I guess I&#x27;ll find out when I get the 2004 update.
评论 #24692978 未加载
TonyTrappover 4 years ago
&gt; A program might depend on a fast timer resolution and fail to request it. There have been multiple claims that some games have this problem (...)<p>Yup, I wrote such a (small, freeware) game 15+ years ago. I wasn&#x27;t aware of timeBeginPeriod at the time, but I observed that for some inexplicable reason, the game ran more smoothly when Winamp was running in the background. :-)
JoeAltmaierover 4 years ago
&lt;rant&gt;<p>Our models of computer timers are woefully inadequate. These things execute billions of instructions per second. Why shouldn&#x27;t we be able to schedule a timer at sub-millisecond resolution?<p>Answer: we can. But the APIs are very old and assume conditions no longer present. Or something like that. Anyway, they don&#x27;t get the job done.<p>Everybody seems to start a hardware timer at some regular period, then simulate &#x27;timer interrupts&#x27; for applications off that timer&#x27;s interrupt. If you want 12.5ms but the ol&#x27; ticker is ticking at 1ms intervals, you get 13 or so depending on where in an interval you asked, it could be 12.<p>Even if nobody is using the timer, its ticking away wasting CPU time. So the tendency is, to make the period as long as possible without pissing everybody off.<p>Even back in the 1980&#x27;s, I worked on an OS running on the 8086 with a service called PIT (Programmable Interval Timer). You said what interval you wanted; it programmed the hardware timer for that. If it was already running, and your interval was shorter than what remained, it would reprogram it for your short time, then when it went off it reprogrammed it for the remainder.<p>It kept a whole chain of scheduled expirations sorted by time. When the interrupt occurred it&#x27;d call the callback of the 1st entry and discard it. Then it&#x27;d reprogram the timer for the remaining time on the next.<p>It took into account the time of the callback; the time to take the interrupt and reprogram. And it achieved sub-millisecond scheduling even back on that old sad hardware.<p>And when nobody was using the timer, it didn&#x27;t run! Zero wasted CPU.<p>Imagine how precise timers could be today, on our super duper gigahertz hardware.<p>But what do we get? We get broken, laggy, high-latency, late timer callbacks at some abominable minimum period. Sigh.<p>&lt;&#x2F;rant&gt;
评论 #24690624 未加载
评论 #24690406 未加载
评论 #24693795 未加载
评论 #24690798 未加载
评论 #24690515 未加载
dstaleyover 4 years ago
I once spent ages trying to determine why a Python unit test that sorted timestamps constantly failed on Windows. In the test, we compared the timestamps of performed operations, and checked to confirm that the operations happened in sequence based on their timestamp (I&#x27;m sure many of you see where this is going). On Windows, the timestamp for all the actions was exactly the same, so when sorted, the actions appeared out-of-order. It was then that I discovered Python&#x27;s time library on Windows only reports times with a resolution of ~1ms, whereas on Linux the same code reports times with a resolution of ~10us. That one was actually super fun to track down, but super disappointing to discover it&#x27;s not something that&#x27;s easily remedied.<p>(For those about to suggest how it should have been done, the application also stored an atomic revision counter, so the unit test was switched to that instead of a timestamp.)
评论 #24697216 未加载
BitterAmethystover 4 years ago
At work we have an application that calls `timeBeginPeriod(1)` to get timer callbacks (from `CreateTimerQueue`) firing at 5ms resolutions but we are not seeing the behaviour described in the article. We observe no change to the timer resolution after calling `timeBeginPeriod(1)`, which unfortunatly is a breaking change to our app.<p>The lack of information and response from Microsoft on this has been quite frustrating.
评论 #24686431 未加载
gambitingover 4 years ago
Ah yes, reminds me how on my previous project I was in charge of writing a server to mix audio for multiple clients in real time. The server worked well on my local Windows 10 machine, but when deployed to a cloud instance of Windows Server 2016 it ran very very poorly, just barely quickly enough to process data in time.<p>That&#x27;s when I discovered that doing a &quot;process more data if there is any, if not - sleep(1)&quot; loop is a <i>very</i> bad way of doing it, as on Windows Server 2016 &quot;sleep(1)&quot; means &quot;sleep 16ms&quot;. It all worked fine once the timer resolution was changed to 1ms, but yeah, the default value will screw you over if you have anything this time sensitive and are using sleeps or waits on windows.
评论 #24685822 未加载
评论 #24686140 未加载
评论 #24685872 未加载
throwaway889900over 4 years ago
Seems like this was reported over 4 months ago! [1]<p>[1] <a href="https:&#x2F;&#x2F;developercommunity.visualstudio.com&#x2F;content&#x2F;problem&#x2F;1093078&#x2F;timebeginperiod-function-dont-change-anymore-the-r.html" rel="nofollow">https:&#x2F;&#x2F;developercommunity.visualstudio.com&#x2F;content&#x2F;problem&#x2F;...</a>
garaetjjteover 4 years ago
Author might want to disable Wordpress &quot;pingback&quot; feature, as it seems abused. WTF is that, it seems bots are copying content, swapping random words and reposting on some generic looking sites..? What&#x27;s even the purpose of this?
jfreudenover 4 years ago
From a technical point of view, this is an interesting change, and I&#x27;m not sure if it&#x27;s a bug or not. From a scientific point of view, I definitely bristled at &quot;cleaned up to remove randomness&quot;... :P
stinosover 4 years ago
<i>It shouldn’t be doing this, but it is</i><p>In my opinion this still remains the conclusion, as it has been for the past decades. I cannot remember when I read a bit on Sleep() behavior and timeBeginPeriod() but I remember that what I read was enough to make clear you just shouldn&#x27;t rely on these (unless you&#x27;re 100% sure the consequences are within your spec and will remain so), also not because the workarounds are also widely known (IIRC - things like using WaitForSingleObject if you need accurate Sleep).
评论 #24686037 未加载
perryizgr8over 4 years ago
I don&#x27;t know if something is wrong with my internet or machine, but a lot of graphs&#x2F;pictures seem to be missing on this page.
duke360over 4 years ago
i&#x27;ve always tought that Sleep(n) means Sleep at least n... not Sleep around, or Sleep exactly n, what i&#x27;m missing here?
评论 #24685549 未加载
badsectoraculaover 4 years ago
About the game fixing utilities, while it is annoying that these wont work at the moment, they should still be able to work by installing a hook that attaches itself to the game&#x27;s process and calls timeBeginPeriod (several other unofficial game patches work like this already).
the8472over 4 years ago
&gt; and the timer interrupt is a global resource.<p>Shouldn&#x27;t this at least be per-core rather than global? Then most cores can keep scheduling at a low tick rate and only one or two have to take care of the jittery processes.
Stierlitzover 4 years ago
That&#x27;s an interesting read. I recall reading about some airline communication system that used to freeze when a 32 bit counter in Windows overflowed. Would the way windows implements timer interrupts have anything to do with this.
cm2187over 4 years ago
Does the timer behave in the same way on Windows Server?
评论 #24686020 未加载
londons_exploreover 4 years ago
This seems deliberate... It&#x27;s trying to prevent one application &#x27;randomly breaking&#x27; when another application is running.<p>Seems like a good move to me - just a bit of a shame a few applications might break.
rwallaceover 4 years ago
&gt; One case where timer-based scheduling is needed is when implementing a web browser. The JavaScript standard has a function called setTimeout which asks the browser to call a JavaScript function some number of milliseconds later. Chromium uses timers (mostly WaitForSingleObject with timeouts rather than Sleep) to implement this and other functionality. This often requires raising the timer interrupt frequency.<p>Why does it require that? Timeouts should normally be on the order of minutes. Why does Chrome need timer interrupts to happen many times per second?
评论 #24685751 未加载
评论 #24686120 未加载