Page 1 of 1

What's best? APIC timer or PIT?

Posted: Wed Apr 27, 2005 8:58 am
by ChrML
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.

Re:What's best? APIC timer or PIT?

Posted: Wed Apr 27, 2005 10:07 am
by Brendan
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

Re:What's best? APIC timer or PIT?

Posted: Wed Apr 27, 2005 11:20 am
by ChrML
I see. Thanks a lot for taking time to write this :). I guess what I want to do is to primarily focus on the i386+ architecture, and later make som abstraction seperating most of the higher level kernel functions from the lower ones.

Btw, do you have any examples/links on the APIC timer other than the Inten Architecture manual?

Re:What's best? APIC timer or PIT?

Posted: Wed Apr 27, 2005 6:45 pm
by Brendan
Hi,
ChrML wrote: I see. Thanks a lot for taking time to write this :). I guess what I want to do is to primarily focus on the i386+ architecture, and later make som abstraction seperating most of the higher level kernel functions from the lower ones.

Btw, do you have any examples/links on the APIC timer other than the Inten Architecture manual?
Firstly, don't wait until later to make some abstraction seperating most of the higher level kernel functions from the lower ones - do it as early as you can. I'd (at least) make sure any source code that's hardware specific is in a seperate "i386" directory. This would include lower level memory management, the actual code that switches threads/tasks, CPU detection/initialization, exception handling, etc.

Also, if the timer and scheduler code isn't going to be completely on the "architecture dependant" side of things I'd probably set the timer up to generate a regular, fixed frequency IRQ (not "one-shot" with a variable amount of time between IRQs), as I'd expect other architectures would be more likely to support this (and it can be simulated if an architecture only supports a "one-shot" timer). I'd also use the same timer for maintaining the system timer tick (although it can make the timer's IRQ handler fairly lengthy when it's updating the system timer, waking sleeping threads and being used for task switching).


The Intel manuals are the primary source of information for the local APIC. For the IO APIC Intel has a seperate datasheet (http://www.intel.com/design/chipsets/da ... 290566.htm).

Other sites with local APIC information include:
http://www.osdev.org/osfaq2/index.php/T ... out%20APIC
http://osdev.berlios.de/pic.html
http://www.osdever.net/tutorials/mp.php?the_id=59

There's also the Linux kernel source, which IMHO is too difficult to follow.

Apart from these I haven't been able to find anything else...


Cheers,

Brendan

Re:What's best? APIC timer or PIT?

Posted: Thu Apr 28, 2005 6:57 am
by ChrML
Thanks a lot! That really helped me :). I'll take your tips in consideration when making my OS. I guess you're right about seperating platform and platform independent code, and I will do that as soon as task switching works.

Thanks again :)

Re:What's best? APIC timer or PIT?

Posted: Fri Apr 29, 2005 3:08 am
by Pype.Clicker
hmm ... talking about the page on the FAQ, do you happen to know how one could discover the system bus frequency ? (it usually ranges from 100MHz to some 266MHz iirc, but who knows what future will be made of ...)