Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


while (1) {
...
tick_nohz_stop_sched_tick();
while (!need_resched())
idle();
...
tick_nohz_restart_sched_tick();
...
}
}

Other architectures differ in some details, but the general principle is the same. After calling
tick_nohz_stop_sched_tickto turn off ticks, the system goes into an endless loop that ends when a
process is available to be scheduled on the processor. Ticks are then necessary again, and are reactivated
bytick_nohz_restart_sched_tick.

Recall that a sleeping process waits for some condition to be fulfilled such that it switches into a runnable
state. A change of this condition is signaled by an interrupt — just suppose that the process has been
waiting for some data to arrive, and the interrupt notifies the system that the data are now available.
Since interrupts occur at random times from the kernel’s point of view, it can well happen that one is
raised during an idle period with ticks turned off. Two conditions can thus require restarting ticks:


  1. An external interrupt make a process runnable, which requires the tick mechanism to
    work.^20 In this case, ticks need to be resumed earlier than initially planned.

  2. The next tick event is due, and the clock interrupt signals that the time for this has come. In
    this case, the tick mechanism is resumed as planned before.


StoppingTicks


Essentially,tick_nohz_stop_sched_tickneeds to perform three tasks:


  1. Check if the next timer wheel event is more than one tick away.

  2. If this is the case, reprogram the tick device to omit the next tick only when it is necessary
    again. This automatically omits all ticks that are not required.

  3. Update the statistical information intick_sched.


Since many details require much attention to corner cases, the actual implementation of
tick_nohz_stop_sched_tickis rather bulky, so I consider a simplified version below.

First of all, the kernel needs to obtain the tick device and thetick_schedinstance for the current CPU:

kernel/time/tick-sched.c
void tick_nohz_stop_sched_tick(void)
{

(^20) To simplify matters, I ignore thattick_nohz_stop_sched_tickis also called fromirq_exitif an interrupt has disturbed a
tickless interval, but did not change the state of the system such that any process became runnable. This also simplifies the discussion
oftick_nohz_stop_sched_tickbecause multiple subsequent invocations ofthe function need not be taken into account.
Additionally, I do not discuss that the jiffies value needs to be updated inirq_enterbecause interrupt handlers would otherwise
assume a wrong value. The function in charge for this istick_nohz_update_jiffies.

Free download pdf