Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


❑ Clocks with a rating between 300 and 399 are reasonably fast and accurate.
❑ Perfect clocks that are the ideal source get a rating between 400 and 499.

The best clock sources can currently be found on the PowerPC architecture where two clocks with a
rating of 400 are available. The time stamp counter (TSC) on IA-32 and AMD64 machines — usually the
most accurate device on these architectures — has a rating of 300. The best clocks on most architectures
have similar ratings. The developers do not exaggerate the performance of the devices and leave plenty
of space for improvement on the hardware side.

It does not come as a surprise thatreadis used to read the current cycle value of the clock. Note that
the value returned does not use any fixed timing basis for all clocks, but needs to be converted into a
nanosecond value individually. For this purpose, the field membersmultandshiftare used to multiply
or divide, respectively, the cycles value as follows:

<clocksource.h>
static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
{
u64 ret = (u64)cycles;
ret = (ret * cs->mult) >> cs->shift;
return ret;
}

Note thatcycle_tis defined as an unsigned integer with 64 bits independent of the underlying platform.

If a clock does not provide time values with 64 bits, thenmaskspecifies a bitmask to select the appropriate
bits. The macroCLOCKSOURCE_MASK(bits)constructs the proper mask for a given number of bits.

Finally, the fieldflagsofstruct clocksourcespecifies — you will have guessed it — a number of flags.
Only one flag is relevant for our purposes.CLOCK_SOURCE_CONTINUOUSrepresents a continuous clock,
although the meaning is not quite the mathematical sense of of ‘‘continuous.’’ Instead, it describes that
the clock is free-running if set to 1 and thus cannot skip. If it is set to 0, then some cycles might be lost;
that is, if the last cycle value wasn, then the next value does not necessarily need to ben+1evenifitwas
read at the next possible moment. A clock must exhibit this flag to be usable for high-resolution timers.

For booting purposes and if nothing really better is available on the machine (which should never be the
case after bootup), the kernel provides a jiffies-based clock^10 :

kernel/time/jiffies.c
#define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/ACTHZ))

struct clocksource clocksource_jiffies = {
.name = "jiffies",
.rating = 1, /* lowest valid rating*/
.read = jiffies_read,
.mask = 0xffffffff, /*32bits*/
.mult = NSEC_PER_JIFFY << JIFFIES_SHIFT, /* details above */
.shift = JIFFIES_SHIFT,
};

(^10) Note that if the jiffy clock were used as the main clock source, then the kernel would be responsible to update thejiffiesvalue
by some apt means, for instance, directly from the timer interrupt. Usually, architectures don’t do this. It, therefore, does not really
make sense to use this clock for tickless systems that emulate the jiffies layer via clock sources. In fact, using the jiffies clock source
is a nice way to crash dynamic tick systems, at least on kernel 2.6.24...

Free download pdf