Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


Timers are organized on lists, and the following data structure represents a timer on a list:


<timer.h>
struct timer_list {
struct list_head entry;
unsigned long expires;

void (*function)(unsigned long);
unsigned long data;

struct tvec_t_base_s *base;
};

As usual, a doubly linked list is used to link registered timers with each other.entryis the list head. The
other structure items have the following meaning:


❑ functionsaves a pointer to the callback function invoked upon time-out.
❑ datais an argument for the callback function.
❑ expiresspecifies the time, in jiffies, at which the timer expires.
❑ baseis a pointer to a base element in which the timers are sorted on their expiry time (discussed
in more detail shortly). There is a base element for each processor of the system; consequently,
the CPU upon which the timer runs can be determined usingbase.

The macroDEFINE_TIMER(_name, _function, _expires, _data)is provided to declare a static
timer_listinstance.


Times are given in two formats in the kernel — as offsets or as absolute values. Both make use ofjiffies.
While offsets are used when a new timer is installed, all kernel data structures use absolute values
because they can easily be compared with the currentjiffiestime. Theexpireselement oftimer_list
also uses absolute times and not offsets.


Because programmers tend to think in seconds rather than inHZunits when defining time intervals, the
kernel provides a matching data structure plus the option of converting intojiffies(and, of course,
vice versa):


<time.h>
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};

The elements are self-explanatory. The complete time interval is calculated by adding the specified sec-
ond and microsecond values. Thetimeval_to_jiffiesandjiffies_to_timevalfunctions are used to
convert between this representation and ajiffiesvalue. These functions are implemented in<timer.h>.


Another possibility to specify times includes nanoseconds instead of microseconds:


<time.h>
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
Free download pdf