Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


unsigned long data;

timer = list_entry(head->next,struct timer_list,entry);
fn = timer->function;
data = timer->data;

detach_timer(timer, 1);
fn(data);
}
}
...
}

Activating Timers


When new timers are installed, a distinction must be made as to whether they are required by the kernel
itself or by applications in userspace. First, let’s discuss the mechanism for kernel timers because user
timers also build on this mechanism.

add_timeris used to insert a fully supplied instance oftimer_listinto the structures just described
above:

<timer.h>
static inline void add_timer(struct timer_list *timer);

After checking several safety conditions (e.g., the same timer may not be added twice), work is delegated
to theinternal_add_timerfunction whose task is to place the new timer at the right position in the data
structures.

The kernel must first compute the number of ticks after which time-out of the new timer will occur
because an absolute time-out value is specified in the data structure of new drivers. To compensate for
any missed timer handling calls,expires - base->timer_jiffiesis used to compute the offset.

The group and the position within the group can be determined on the basis of this value. All that
now need be done is to add the new timer to the linked list. Because it is placed at theendof the list
and because therun_timer_listis processed from the beginning, afirst-in, first-out mechanism is
implemented.

15.3 Generic Time Subsystem


Low-resolution timers are useful for a wide range of situations and deal well with many possible use
cases. This broadness, however, complicates support for timers with high resolution. Years of develop-
ment have shown that it is very hard to integrate them into the existing framework. The kernel therefore
supports a second timing mechanism.

While low-resolution timers are based on jiffies as fundamental units of time, high-resolution timers
use human time units, namely, nanoseconds. This is reasonable because high precision timers are mostly
required for userland applications, and the natural way for programmers to think about time is in human
units. And, most important, 1 nanosecond is a precisely defined time interval, whereas the length of one
jiffy tick depends on the kernel configuration.
Free download pdf