Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


Note that a direct interpretation of aktime_tas a number of nanoseconds would be possible on 64-bit
machines, but can lead to problems on32-bit machines. Thus, the functionktime_to_nsis provided
to perform the conversion properly. The auxiliary functionktime_equalis provided to decide if two
ktime_ts are identical.

To provide exchangeability with other time formats used in the kernel, some conversion functions are
available:

<ktime.h>
ktime_t timespec_to_ktime(const struct timespec ts)
ktime_t timeval_to_ktime(const struct timeval tv)
struct timespec ktime_to_timespec(const ktime_t kt)
struct timeval ktime_to_timeval(const ktime_t kt)
s64 ktime_to_ns(const ktime_t kt)
s64 ktime_to_us(const ktime_t kt)

The function names specify which quantity is converted into which, so there’s no need to add anything
further.

15.3.4 Objects for Time Management


Recall from the overview that three objects manage timekeeping in the kernel: clock sources, clock
event devices, and tick devices. Each of them is represented by a special data structure discussed in the
following.

Clock Sources


First of all, consider how time values are acquired from the various sources present in a machine. The
kernel defines the abstraction of a clock source for this purpose:

<clocksource.h>
struct clocksource {
char *name;
struct list_head list;
int rating;
cycle_t (*read)(void);
cycle_t mask;
u32 mult;
u32 shift;
unsigned long flags;
...
};

A human-readable name for the source is given inname,andlistis a standard list element that connect
all available clock sources on a standard kernel list.

Not all clocks are of the same quality, and the kernel obviously wants to select the best possible one. Thus,
every clock has to (honestly) specify its own quality inrating. The following intervals are possible:

❑ A rating between 1 and 99 denotes a very bad clock that can only be used as a last resort or dur-
ing boot up, that is, when no better clock is available.
❑ The range 100–199 describes a clock that is fit for real use, but not really desirable if something
better can be found.
Free download pdf