Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


At a first glance, it might not make much sense to first multiply byJIFFIES_SHIFTand then again divide
by the same value. Nevertheless, this bogosity is required because the NTP code does not work with zero
shifts.^11 Also note that the jiffies clock has a rating of 1, which makes it definitely the worst clock in the
whole system.

The read routine for the jiffies clock is particularly simple: No hardware interaction is required. It suffices
to return the currentjiffiesvalue.

The time-stamp counter usually provides the best clock found on IA-32 and AMD64 machines:

arch/x86/kernel/tsc_64.c
static struct clocksource clocksource_tsc = {
.name = "tsc",
.rating = 300,
.read = read_tsc,
.mask = CLOCKSOURCE_MASK(64),
.shift = 22,
.flags = CLOCK_SOURCE_IS_CONTINUOUS |
CLOCK_SOURCE_MUST_VERIFY,
};

read_tscuses some assembler code to read out the current counter value from hardware.

Working with Clock Sources


How can a clock be used? First of all, it must be registered with the kernel. The function
clocksource_registeris responsible for this. The source is only added to the globalclocksource_list
(defined inkernel/time/clocksource.c), which sorts all available clock sources by their rat-
ing.select_clocksourceis called to select the best clock source. Normally this will pick the
clock with the best rating, but it is also possible to specify a preference from userland via
/sys/devices/system/clocksource/clocksource0/current_clocksource, which is used by the kernel
instead. Two global variables are provided for this purpose:


  1. current_clocksourcepoints to the clock source that is currently the best one.

  2. next_clocksourcepoints to an instance ofstruct clocksourcethat is better than the one
    used at the moment. The kernel automatically switches to the best clock source when a new
    best clock source is registered.


To read the clock, the kernel provides the following functions:

❑ __get_realtime_clock_tstakes a pointer to an instance ofstruct timespecas argument, reads
the current clock, converts the result, and stores in thetimespecinstance.
❑ getnstimeofdayis a front-end for__get_realtime_clock_ts,butalsoworksifnohigh-
resolution clocks are available in the system. In this case,getnstimeofdayas defined in
kernel/time.c(instead ofkernel/time/timekeeping.c)isusedtoprovideatimespecthat
fulfills only low-resolution requirements.

(^11) The definition ofNSEC_PER_JIFFYcontains the pre-processor symbolACTHZ. WhileHZdenotes the base low-resolution fre-
quency that can be selected at compile time, the frequency that the system actually provides will differ slightly because of hardware
limitations.ACTHZstores the frequency at which the clock is actually running.

Free download pdf