Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management



  1. The expiration of the timer must be set to a future point in time. The callback function can
    perform this manipulation because it gets a pointer to thehrtimerinstance for the currently
    running timer as function parameter. To simplify matters, the kernel provides an auxiliary
    function to forward the expiration time of a timer:
    unsigned long
    hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);


This resets thetimerso that it expires afternow[usuallynowis set to the value returned by
hrtimer_clock_base->get_time()]. The exact expiration time is determined by taking the
old expiration time of the timer and addingintervalso often that the new expiration time
lies pastnow. The function returns the number of times thatintervalhadtobeaddedtothe
expiration time to exceednow.
Let us illustrate the behavior by an example. If the old expiration time is 5,nowis 12,
andintervalis 2, then the new expiration time will be 13. The return value is 4 because
13 = 5 + 4 ×2.

A common application for high-resolution timers is to put a task to sleep for a specified, short amount of
time. The kernel provides another data structure for this purpose:

<hrtimer.h>struct hrtimer_sleeper {
struct hrtimer timer;
struct task_struct *task;
};

Anhrtimerinstance is bundled with a pointer to the task in question. The kernel useshrtimer_wakeup
as the expiration function for sleepers. When the timer expires, thehrtimer_sleepercan be derived
from thehrtimerusing thecontainer_ofmechanism (note that the timer is embedded instruct
hrtimer_sleeper), and the associated task can be woken up.

15.4.2 Setting Timers


Setting a new timer is a two-step process:


  1. hrtimer_initis used to initialize ahrtimerinstance.
    void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
    enum hrtimer_mode mode);
    timerdenotes the affected high-resolution timer,clockis the clock to bind the timer to, and
    modespecifies if absolute or relative time values (relative to the current time) are used. Two
    constants are available for selection:

    enum hrtimer_mode {
    HRTIMER_MODE_ABS, /* Time value is absolute */
    HRTIMER_MODE_REL, /* Time value is relative to now */
    };

  2. hrtimer_startsets the expiration time of a timer and starts it.

Free download pdf