Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


Updating the jiffies value is naturally only required if the last update is more than one tick period ago:

kernel/time/tick-sched.c
if (delta.tv64 >= tick_period.tv64) {

delta = ktime_sub(delta, tick_period);
last_jiffies_update = ktime_add(last_jiffies_update,
tick_period);

The most common case is that one tick period has passed since the last jiffy update, and the code shown
above handles this situation by increasinglast_jifies_updatecorrespondingly. This accounts for the
present tick.

However, it is also possible that the last update was more than one jiffy ago. Some more effort is required
in this case:

kernel/time/tick-sched.c
/* Slow path for long timeouts */
if (unlikely(delta.tv64 >= tick_period.tv64)) {
s64 incr = ktime_to_ns(tick_period);

ticks = ktime_divns(delta, incr);

last_jiffies_update = ktime_add_ns(last_jiffies_update,
incr * ticks);
}

The computation oftickscomputes one tick less than the number of ticks that have been skipped, and
last_jiffies_updatesis updated accordingly. Note that the offset by one is necessary because one tick
period was already added tolast_jiffies_updateat the very beginning. This way, the usual case (i.e.,
one tick period since the last update) runs fast, while more effort is required for the unusual case where
more than one tick period has passed since the last update.

Finally,do_timeris called to update the global jiffies value as discussed in Section 15.2.1:

kernel/time/tick-sched.c
do_timer(++ticks);
}
}

15.5.3 Dynamic Ticks for High-Resolution Systems


Since clock event devices run in one-shot mode anyway if the kernel uses high timer resolution, support
for dynamic ticks is much easier to implement than in the low-resolution case. Recall that the periodic tick
is emulated bytick_sched_timeras discussed above. The function is also used to implement dynamic
ticks. In the discussion in Section 15.4.4, I omitted two elements required for dynamic ticks:


  1. Since CPUs can drop global tick duties, the handler needs to check if this has been the case,
    and assume the duties:
    kernel/time/tick-sched.c
    #ifdef CONFIG_NO_HZ
    if (unlikely(tick_do_timer_cpu == -1))
    tick_do_timer_cpu = cpu;
    #endif

Free download pdf