Page 1 of 2
The PIT and VirtualBox
Posted: Wed Jul 23, 2008 2:10 pm
by -m32
Has anyone else had issue with the PIT when using VirtualBox?
No matter what frequency value I use, it seems that the PIT in virtual box takes the same amount of time - about 1 second. This makes for pretty crappy multi-processing. I have to assume then that Windows & other OSes don't use the PIT for their timing... because they run just fine in VirtualBox.
The function I've been using since my OS' epoch:
Code: Select all
void timer_set_freq(uint16 freq) {
uint32 divisor = 1193180 / freq;
outb(0x43, 0x36);
outb(0x40, divisor & 0xFF);
outb(0x40, (divisor >> 8) & 0xFF);
}
It of course works fine on real hardware and in Bochs....
Re: The PIT and VirtualBox
Posted: Wed Jul 23, 2008 2:20 pm
by itisiuk
thats doing the same as my code so it must be a problem with virtbox although mine is running in qemu.
windows and others use the apic timer instead of the pit so that might be why they run fine.
i wouldnt know what todo about this problem other than contact the ppl who made virtbox and ask about the problem
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 3:13 am
by JamesM
The PIT works fine in virtualbox for me!
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 3:20 am
by AJ
Same here - I've never had a problem with it. In fact, Bochs is the only one that ever gave me a problem when I forgot to sync properly.
Cheers,
Adam
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 4:27 am
by octa
i hav this prob in bochs !!!
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 5:57 am
by -m32
That's weird because I've tried this on several host machines too and I get the same results.
What's weirder is, if (using VirtualBox) I set the PIT to ~1000Hz and I repeatedly query the RTC for the current time, it takes about 35 seconds for one second to pass on the RTC... And if I set the PIT to 100Hz, it takes about 2-4 seconds for one second to pass... If I set it to 10Hz I get almost accurate timing...
Changing the frequency of the PIT shouln't affect the RTC -- it doesn't on real hardware. I don't get it...

Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 6:02 am
by JamesM
-m32 wrote:That's weird because I've tried this on several host machines too and I get the same results.
What's weirder is, if (using VirtualBox) I set the PIT to ~1000Hz and I repeatedly query the RTC for the current time, it takes about 35 seconds for one second to pass on the RTC... And if I set the PIT to 100Hz, it takes about 2-4 seconds for one second to pass... If I set it to 10Hz I get almost accurate timing...
Changing the frequency of the PIT shouln't affect the RTC -- it doesn't on real hardware. I don't get it...

Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.
Not only that, but emulators run as user space processes and as such it is difficult->impossible to have a clock emulation frequency greater than the host OS's fastest signal/interrupt mechanism, so at a frequency of 1KHz who knows what the emulator is actually doing, because it sure as hell can't get vtimer interrupts at that frequency...
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 6:13 am
by -m32
Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.
Well my polling loop is only 3 lines, outb, inb and putc. I can't imagine that such a small amount of machine instruction causes such a HUGE delay. Besides, If I use a frequency like 100Hz, there's no reason to have it take 4 seconds for one second to register in the RTC. 100Hz is completely reasonable.
Not only that, but emulators run as user space processes and as such it is difficult->impossible to have a clock emulation frequency greater than the host OS's fastest signal/interrupt mechanism, so at a frequency of 1KHz who knows what the emulator is actually doing, because it sure as hell can't get vtimer interrupts at that frequency...
I understand that, my point was only to demonstrate the major discrepancy in the timing as it is not experienced in any other machine virtualizer.
Thanks for the feedback.
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 6:13 am
by suthers
Code: Select all
void timer_set_freq(uint16 freq) {
uint32 divisor = 1193180 / freq;
outb(0x43, 0x36);
outb(0x40, divisor & 0xFF);
outb(0x40, (divisor >> 8) & 0xFF);
}
What's the point of doing the '& 0xFF' to pass the top bite of the divisor?
(Just as a side note)
Jules
Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 6:17 am
by -m32
suthers wrote:
What's the point of doing the '& 0xFF' to pass the top bite of the divisor?
(Just as a side note)
Jules
None, it's just me being anal

Re: The PIT and VirtualBox
Posted: Thu Jul 24, 2008 6:20 am
by suthers
But if it is a problem of your polling loop being to long it might help a bit
Jules
Re: The PIT and VirtualBox
Posted: Sat Jul 26, 2008 11:18 am
by kscguru
-m32 wrote:Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.
Well my polling loop is only 3 lines, outb, inb and putc. I can't imagine that such a small amount of machine instruction causes such a HUGE delay. Besides, If I use a frequency like 100Hz, there's no reason to have it take 4 seconds for one second to register in the RTC. 100Hz is completely reasonable.
I understand that, my point was only to demonstrate the major discrepancy in the timing as it is not experienced in any other machine virtualizer.
outb/inb and putc (which I assume writes to the VGA framebuffer internally?) are some of the most expensive instructions to virtualize. A lot of virtualizers will trap on the outb, trap again on inb, get through a few dozen putc instructions and trap on the framebuffer write ... each trap is tens or hundreds of thousands of instructions. It is astonishingly tricky to keep time moving in a VM, and almost impossible with a hard-to-virtualize workload like an I/O-heavy tight loop. To get into too much detail ... I/Os are extremely bad for VT/SVM-based virtualizers, so VIrtualBox (which is VT-based KVM + qemu internally) will do badly but VMware (which can run in other modes) and Bochs (which is all emulation) are likely to do better.
Put more work in the loop, or a "sti; hlt;" (which is often used to "catch up" time) and wait for interrupt delivery from the PIC.
Re: The PIT and VirtualBox
Posted: Mon Jul 28, 2008 10:46 am
by -m32
Anyway, this kind of deviated from my original point.. setting the PIT in VirtualBox doesn't seem to have an effect on the frequency of interrupts fired. 10Hz, 20Hz, 100Hz, 1000Hz, whatever, it still takes at least 1 second to get an interrupt. - And that's with no code in my int handler except putting a period (.) on the screen. No matter how much machine instruction there is, still 1 second for the PIT interval.
Like I said it doesn't happen on any other virtualizer or hardware.
Sigh.... maybe I should just submit a bug report.

Re: The PIT and VirtualBox
Posted: Tue Jul 29, 2008 2:15 am
by JamesM
Possibly, but as I said I don't get the same behaviour. Is your host kernel compiled tickless, by any chance?
Re: The PIT and VirtualBox
Posted: Tue Jul 29, 2008 1:45 pm
by -m32
JamesM wrote:Possibly, but as I said I don't get the same behaviour. Is your host kernel compiled tickless, by any chance?
Nope
Tried it on various hosts on different boxes. As for OSes: XP, Vista x64, Fedora 8. Same thing.