News for hackers. Stuff that matters.
Re:Windows Timer Resolution: The Great Rule Change by JoeAltmaier on Monday October 05, 2020 @10:58AM (#24688320)
<rant>

Our models of computer timers are woefully inadequate. These things execute billions of instructions per second. Why shouldn't we be able to schedule a timer at sub-millisecond resolution?

Answer: we can. But the APIs are very old and assume conditions no longer present. Or something like that. Anyway, they don't get the job done.

Everybody seems to start a hardware timer at some regular period, then simulate 'timer interrupts' for applications off that timer's interrupt. If you want 12.5ms but the ol' ticker is ticking at 1ms intervals, you get 13 or so depending on where in an interval you asked, it could be 12.

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.

Even back in the 1980'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.

It kept a whole chain of scheduled expirations sorted by time. When the interrupt occurred it'd call the callback of the 1st entry and discard it. Then it'd reprogram the timer for the remaining time on the next.

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.

And when nobody was using the timer, it didn't run! Zero wasted CPU.

Imagine how precise timers could be today, on our super duper gigahertz hardware.

But what do we get? We get broken, laggy, high-latency, late timer callbacks at some abominable minimum period. Sigh.

</rant>

Re:Windows Timer Resolution: The Great Rule Change by simias on Monday October 05, 2020 @02:39PM (#24690624)
Aren't most modern CPUs tickless[1]? They effectively work the way you describe, the scheduler will sleep as long as possible instead of just "ticking" a NOP if nothing is scheduled.

The problem is that what you describe is very hard to guarantee in a non-realtime OS. Modern CPUs are pretty damn fast, that's true, but they can't task switch on a dime. The delay between an IRQ being triggered and task being scheduled can vary pretty wildly, especially if the CPU is already under a heavy load.

You can still try your luck with nanosleep() and see how that treats you. It might work. It probably won't always work.

In general if you need to react so fast on some event you probably want to avoid polling at all costs. That's why most OSs are fine relaxing timer constraints for better overall performance. If you can't react on an external interrupt and you really need to be reactive busy-polling is still probably your best bet.

[1] https://en.wikipedia.org/wiki/Tickless_kernel

Re:Windows Timer Resolution: The Great Rule Change by LoSboccacc on Monday October 05, 2020 @02:17PM (#24690406)
> Why shouldn't we be able to schedule a timer at sub-millisecond resolution

it's not just the API, the whole operating system need to be aware of the need to operate at such frequency, because even if the clocks are faster, the software gets interrupted all the time. what good would a nanosecond api do if an interrupt can stall the cpu for a microsecond?

hence the need for a whole class of realtime operating systems. the next question would then be, why aren't all computers using a realtime operating system? because with the relaxed time constraints we get great returns in speed and cost.

Re:Windows Timer Resolution: The Great Rule Change by ishitatsuyuki on Monday October 05, 2020 @08:59PM (#24693795)
Linux has implemented hrtimer [1] a while ago and it can easily achieve sub-ms precision. You can just try some benchmark with nanosleep() on your system. It works in a similar way to the PIT you described, although I don't think it tries to offset the delay from interrupts.

What I want to say is that we already have precise timers usable on modern hardware; as for why Windows doesn't use that properly, I have no idea, but I can guess that's some technical debt in the scheduler.

[1] https://www.kernel.org/doc/ols/2006/ols2006v1-pages-333-346....

Re:Windows Timer Resolution: The Great Rule Change by toast0 on Monday October 05, 2020 @02:57PM (#24690798)
If you want to be really mad, consider that modern x86 chips can fire a timer interrupt from the timestamp counter (TSC). Early TSCs weren't frequency stable, but most (all?) now use some fixed frequency, something around the bus speed, and the most recent models even tell you what the frequency is. With that, you could have sub-microsecond timers, if your system isn't too busy and/or you have the right priorities set for preemption.
Re:Windows Timer Resolution: The Great Rule Change by JoeAltmaier on Monday October 05, 2020 @04:44PM (#24691827)
I remember that! Worked at Intel's biggest chip customer (very early on). They asked us what we wanted in the next generation. Nobody said much, so I made a manifesto - bus data breakpoint interrupts, jump history fifo, and a timestamp counter and count-match interrupt register! It showed up in the next spec, but as a privileged instruction sadly. And at clock speed. I wanted it at a fixed frequency for I/O timing, and I wanted it for everybody - drivers, apps, kernel, everbody.
Re:Windows Timer Resolution: The Great Rule Change by _0ffh on Monday October 05, 2020 @02:29PM (#24690515)
Used the exact same method to schedule events in an OS-less embedded system.

I feel your frustration.