Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


15.2.2 Working with Jiffies


Jiffies provide a simple form of low-resolution time management in the kernel. Although the concept
is simple, some caveats apply when the variable is read or when times specified in jiffies need to be
compared.

Sincejiffies_64can be a composed variable on 32-bit systems, it must not be read directly, but may
only be accessed with the auxiliary functionget_jiffies_64. This ensures that the correct value is
returned on all systems.

Comparing Times


To compare the temporal relation of events, the kernel provides several auxiliary functions that prevent
off-by-one errors if they are used instead of a home-grown comparisons (a,b,andcdenote jiffie time
values for some events):

❑ timer_after(a,b)returns true if timeais after timeb.time_before(a,b)will be true if timea
is before timeb, as you will have guessed.
❑ time_after_eq(a,b)works liketime_after, but also returns true if both times are identical.
time_before_eq(a,b)is the inverse variant.
❑ time_in_range(a,b,c)checks if timeais contained in the time interval denoted by [b,c]. The
boundaries are included in the range, soamay be identical toborc.

Using these functions ensures that wraparounds of thejiffies counter are handled correctly. As a general
rule, kernel code should therefore never compare time values directly, but always use these functions.

Although there are fewer problems when 64-bit times as given byjiffies_64are compared, the kernel
also provides the functions shown above for 64-bit times. Save fortime_in_range, just append_64to the
respective function name to obtain a variant that works with 64-bit time values.

Time Conversion


When it comes to time intervals, jiffies might not be the unit of choice in the minds of most programmers.
It is more conventional to think in milliseconds or microseconds for short time intervals. The kernel thus
provides some auxiliary functions to convert back and forth between these units and jiffies:

<jiffies.h>
unsigned int jiffies_to_msecs(const unsigned long j);
unsigned int jiffies_to_usecs(const unsigned long j);
unsigned long msecs_to_jiffies(const unsigned int m);
unsigned long usecs_to_jiffies(const unsigned int u);

The functions are self-explanatory. However, Section 15.2.3 shows that conversion functions between
jiffies andstruct timevalandstruct timespec, respectively, are also available.

15.2.3 Data Structures


Let us now turn our attention to how low-resolution timers are implemented. You have already seen that
processing is initiated byrun_local_timers, but before this function is discussed, some prerequisites in
the form of data structures must be introduced.
Free download pdf