Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


Set tick_sched->nohz_mode to NOHZ_MODE_LOWRES

Initialize tick timer and program next tick

tick_nohz->switch_to_nohz

tick_switch_to_oneshot(tick_nohz_handler)

Figure 15-19: Code flow diagram fortick_nohz_switch_to_nohz.

The most important change required for the transition to dynamic ticks is to set the clock event
device to one-shot mode, and to install an appropriate tick handler. This is done by calling
tick_switch_to_oneshot. The new handler istick_nohz_handler,examinedbelow.

Since the dynamic tick mode is now active, thenohz_modefield of the per-CPU instance ofstruct
tick_schedis changed toNOHZ_MODE_LOWRES. To get things going, the kernel finally needs to activate
the first periodic tick by setting the timer to expire at the point in time when the next periodic tick would
have been due.

The DynamicTick Handler


The new tick handlertick_nohz_handlerneeds to assume two responsibilities:


  1. Perform all actions required for the tick mechanism.

  2. Reprogram the tick device such that the next tick expires at the right time.


The code to satisfy these requirements looks as follows. Some initialization work is required to obtain the
per-CPU instance ofstruct tick_schedand the current time:

kernel/time/tick-sched.c
static void tick_nohz_handler(struct clock_event_device *dev)
{
struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
struct pt_regs *regs = get_irq_regs();
int cpu = smp_processor_id();
ktime_t now = ktime_get();

dev->next_event.tv64 = KTIME_MAX;

The role of the global tick device is as before assumed by one particular CPU, and the handler needs to
check if the current CPU is the responsible one. However, the situation is a bit more complicated with
dynamic ticks. If a CPU goes into a long sleep, then it cannot be responsible for the global tick anymore,
and drops the duty. If this is the case, the next CPU whose tick handler is called must assume the duty^18 :

kernel/time/tick-sched.c
if (unlikely(tick_do_timer_cpu == -1))
tick_do_timer_cpu = cpu;

(^18) The case in which all processors sleep for longer than one jiffy is also possible. The kernel needs to consider this case as the dis-
cussion oftick_do_updates_jiffies64shows below.

Free download pdf