Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


As already suggested by its name, the primary use of this timer is in theprofilingof applications
in which a search is made for the most compute-intensive fragments of a program so that these
can be optimized accordingly. This is an important consideration, particularly in scientific or
operating system-related applications.

The timer type — and the periodic interval length — must be specified when an interval timer is
installed. In our example,TTIMER_REALis used for a real-time timer.

The behavior of alarm timers can be simulated with interval timers by selectingITIMER_REALas the timer
type and deinstalling the timer after the first time-out. Interval timers are therefore a generalized form of
alarm timers.

15.7.2 The alarm and setitimerSystem Calls


alarminstalls timers of theITIMER_REALtype (real-time timers), whilesetitimeris used to install not
only real-time, but also virtual and profiling timers. The system calls all end up indo_setitimer.The
implementation of both system calls rests on a common mechanism that is defined inkernel/itimer.c.
The implementation is centered aroundstruct hrtimer, so if high-resolution support is available, the
corresponding advantages are automatically transferred into userland and not only available to the
kernel. Note that sincealarmuses a timer of typeITIMER_REAL, the system calls can interfere with
each other.

The starting points of the system calls are, as usual, the two functionssys_alarmandsys_setitimer.
Both functions use the auxiliary functiondo_setitimerto actually implement the timer:

kernel/itimer.c
int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)

Three parameters are required.whichspecifies the timer type, and can beITIMER_REAL,ITIMER_VIRTUAL,
orITIMER_PROF.valuecontains all relevant information about the new timer. If the timer replaces an
already existing one, thenovalueis employed to return the previously active timer description.

Specifying timer properties is simple:

<time.h>
struct itimerval {
struct timeval it_interval; /* timer interval */
struct timeval it_value; /* current value */
};

Essentially,timevaldenotes the length of the periodic interval after which the timer expires.it_value
denotes the amount of time remaining until the timer expires next. All details are documented in the
manual pagesetitimer(2).

Extensionsto the TaskStructure


The task structure of each process contains a pointer to an instance ofstruct signal_structthat
includes several elements to accommodate information required for timers:

<sched.h>
struct signal_struct {
...
/* ITIMER_REAL timer for the process */
Free download pdf