Hi,
ChrML wrote:
Who's best of the APIC timer or the PIT for task scheduling, and what about compatibility on different architectures? I haven't been able to find any good PIT or APIC timer documentations to test them out. When I don't do anything with my PIT, IRQ0 only fires once.
The local APIC timer's base frequency is derived from the CPUs system bus frequency (typically 100 MHz or more) while the PIT chip runs from a (roughly) 1.193 MHz base frequency. This (and the much better range for the frequency divider) means the local APIC can be used for a far wider range of frequencies (or far greater accuracy for small delays).
As the PIT isn't built into the CPU it requires slow IO port accesses to read/change it - the local APIC has less overhead (as it is built into the CPU). This isn't a problem if you configure the PIT to generate a fixed frequency though.
For a multi-CPU OS the local APIC is often the only sane choice, as with most scheduler designs you need a time source for each CPU. Also there are older SMP motherboards that don't connect the PIT chip to the IO APIC, so that it's difficult to get the PIT to trigger an IRQ once the IO APICs are used.
However...
Even though all Intel Pentium and newer CPUs have a local APIC the local APIC can be disabled in an irreversable way. Most (pre-Pentium IV) single-CPU BIOSs disable the local APIC for backwards compatibility, such that it's impossible for any OS to use the local APIC on these computers.
Also use of the local APIC is more complicated and there's much less information on the internet on how to use it correctly (code that uses/configures the PIT is quite common). For example, before you can accurately use the local APIC timer you'd need to figure out the CPUs system bus frequency (ie. use another timer to figure out how quick it is).
Then there's the (often forgotten) CMOS/RTC timer (IRQ 8 setup for a periodic interrupt). This timer is generally worse in all cases (worse frequency control, at least one IO port access needed every IRQ, etc). It may be quite usable for the scheduler though - (for e.g. it's easy to set it up for an IRQ every 1 mS).
So, what's best? Without doubt the local APIC timer is technically better, but when the OS can't use the local APIC it needs to use the PIT (or IRQ 8/RTC/CMOS). IMHO the best thing an OS can do is support both, and use the local APIC (instead of the PIT) if it can.
For e.g. my OS uses the RTC/CMOS timer to keep track of real time (the system timer tick), and the local APIC (or PIT if the local APIC isn't accessable) in "one-shot" mode for the scheduler.
As for compatibility, the hardware used by other architectures isn't compatible with the hardware used in 80x86 PCs. To get around this, somewhere between the hardware and applications there's a platform independant interface.
For my OS I use a highly customised platform dependant micro-kernel (written in NASM) with a platform independant "kernel API" to the rest of the OS (device drivers, applications, etc). The point where platform dependant code meets platform independant code is this "kernel API".
For a large monolithic kernel you could have highly customised platform dependant code built into the kernel's core, with a standard interface between it and the rest of the kernel (and rest of the OS). In this case the point where platform dependant code meets platform independant code would be somewhere in the middle of the kernel.
You could also have a kernel where all functions are platform independant. In this case some functions would have several platform dependant versions. Here the point (or points) where platform dependant code meets platform independant code is spread all over the kernel.
Depending on where platform dependant code meets platform independant code is in your OS you've got several options. For me, I can do whatever is the best for each platform and hide it within the micro-kernel. For you? I don't know...
In any case the PIT is just as incompatible with other architectures as the local APIC

.
Cheers,
Brendan