Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


15.2 Implementation of Low-Resolution


Timers


Since low-resolution timers have been around in the kernel for many years and are used in hundreds of
places, their implementation is covered first. In the following, assume that the kernel is defined to work
with periodic ticks. The situation is more involved if dynamic ticks are in use, but that case is discussed
in Section 15.5.

15.2.1 Timer Activation and Process Accounting


As the time base for timers, the kernel uses the timer interrupt of the processor or any other suitable
periodic source. On IA-32 and AMD64 systems, the programmable interrupt timer (PIT) or the High
Precision Event Timer (HPET) can be employed for this purpose. Nearly all modestly modern systems
of this type are equipped with an HPET, and if one is available, it is preferred to the PIT.^3 The interrupt
occurs at regular intervals — exactlyHZtimes per second.HZis defined by an architecture-specific pre-
processor symbol in<asm-arch/param.h>. The assigned value can be configured at compile time via the
configuration optionCONFIG_HZ.

HZ=250is used as the default value for most machine types, especially on the ubiquitous IA-32 and
AMD64 architectures.

TheHZfrequency is also defined (and used) when dynamic ticks are enabled
because it is the fundamental quantity for many timekeeping tasks. On a busy
system where something nontrivial (unlike the idle task) is always available to be
done, there is superficially no difference between dynamic and periodic ticks.
Differences only arise when there is little to do and some timer interrupts can be
skipped.

HigherHZvalues will, in general, lead to better interactivity and responsiveness of the system, particu-
larly because the scheduler is called at each timer tick. As a drawback, more system work needs to be
done because the timer routines are called more often; thus the general kernel overhead will increase
with increasingHZsettings. This makes largeHZvalues preferable for desktop and multimedia systems,
whereas lowerHZvalues are better for servers and batch machines where interactivity is not much of a
concern.

Early kernels in the 2.6 series directly hooked into the timer interrupt to start timer activation and process
accounting, but this has been somewhat complicated by the introduction of the generic clock framework.
Figure 15-4 provides an overview of the situation on IA-32 and AMD64 machines.

The details differ for other architectures, but the principle is nevertheless the same. (How a particular
architecture proceeds is usually set up intime_initwhich is called at boot time to initialize the funda-
mental low-resolution timekeeping.) The periodic clock is set up to operate atHZticks per second. IA-32
registerstimer_interruptas the interrupt handler, whereas AMD64 usestimer_event_interrupt.Both
functions notify the generic, architecture-independent time processing layers of the kernel by calling the
event handler of the so-called global clock (see Section 15.3). Different handler functions are employed

(^3) Using the HPET can be disabled with the kernel command-line optionhpet=disable, though.

Free download pdf