Page 1 of 1

LAPIC timer interrupts going slower and faster with load

Posted: Sun Jan 26, 2025 7:12 am
by max
Didn't post for a long time here, nice to see that so many are still here after so long :)

Worked a lot on my system recently and I've made an interesting observation on x86 with the LAPIC timer. I've programmed it to trigger a timer interrupt used for preemptive multitasking every 1ms. To do so I'm using the PIT to sleep for 10ms and then calculate the settings for the periodic timer based on the counted ticks. This works fine so far - under usual conditions.

Now I'm also using that interrupt to track time, so everytime it occurs I count a clock per processor in milliseconds - I know, not optimal. I noticed now that when a process is taking high CPU load, the time is running "slower", so less of the timer interrupts are going through. But, once that process is finished, time is running "faster" for a moment, so it looks like all the interrupts have queued up and are processed. Testing all of this in VirtualBox btw.

I first had the suspicion that on high load, the CPU frequency might increase and in turn the APIC frequency, but wouldn't that mean the interrupt would happen rather more often then less? Also this wouldn't explain the "queueing" effect.

Or could the reason for something like that rather be that maybe interrupts are disabled for too long, causing the timers to queue up? What speaks against this though is that the same effect happens even if the process is just looping and not really using any syscalls or something.

I'm a bit lost on this and wonder if you experienced something similar. Is it required to consistently recalibrate the APIC timer? Is there something regarding power management going on?

Best regards
Max

Re: LAPIC timer interrupts going slower and faster with load

Posted: Sun Jan 26, 2025 6:30 pm
by Octocontrabass
max wrote: Sun Jan 26, 2025 7:12 amIs it required to consistently recalibrate the APIC timer?
No. Any recent CPU will run the APIC timer at a fixed frequency. Intel CPUs report the frequency through CPUID, so you might not need any calibration at all. (Calibration shouldn't be necessary for AMD CPUs either, but AMD has chosen to avoid clearly documenting the nominal APIC timer frequency...)
max wrote: Sun Jan 26, 2025 7:12 amIs there something regarding power management going on?
I would guess it's something related to virtualization. Hypervisors can easily trap things like HLT instructions to inject timer interrupts, but when the guest is busy doing things that aren't trapped, the hypervisor has to wait until something controlled by the host OS issues a hardware interrupt.

Re: LAPIC timer interrupts going slower and faster with load

Posted: Mon Jan 27, 2025 4:16 am
by max
Thanks for the hints @Octocontrabass. The part about virtualization makes sense indeed. I'll maybe try and test it on real hardware as well to see how it behaves.