Original source: https://manybutfinite.com/post/what-does-an-idle-cpu-do/index.html
In the last Post I said the fundamental axiom of OS behavior are that to any given time, exactly one and only one task is a Ctive on a CPU. But if there ' s absolutely nothing to do, then what?
It turns out so this situation is extremely common, and for most personal computers it ' s actually the Norm:an ocean of Sleeping processes, all waiting on some condition to wake up, while nearly 100% ' CPU time ' going into the mythical ' ID Le task. " In fact, if the CPU was consistently busy for a normal user, it's often a misconfiguration, bug, or malware.
Since we can ' t violate our axiom, some task needs to is active on a CPU. The because it ' s good design:it would is unwise to spread special all over the cases kernel checking whether the There Fact an active task. A is far better when there are no exceptions. Whenever you to write an if statement, Nyan Cat cries. And second, we need to does something with all those idle CPUs, lest they get spunky and, you know, create Skynet.
Keep design consistency and is one step ahead of the devil, OS developers create a idle task that gets scheduled to Run when there ' s no other work. We have seen in the Linux boot process This idle task is process 0, a direct descendent of the very That's runs when a computer is a. It is initialized in Rest_init where init_idle_bootup_taskinitializes the idle scheduling class.
Briefly, Linux supports different scheduling classes for things-like real-time processes, regular user processes, and so O N. When it's time to choose a process to become the active task, which classes are queried in order of priority. That's way, the nuclear reactor control code always gets to run before the Web browser. Often, though, classes return NULL, meaning they don ' t have a suitable process to run-they ' re all sleeping. But the Idle scheduling class, which runs last, never fails:it always the returns task.
That's all good, but let's get down to just what exactly this idle the task is doing. So this is cpu_idle_loop, courtesy of Open Source:cpu_idle_loop
1 2 3 4 5 6 7 8 9 each |
while (1) {while (!need_resched ()) {Cpuidle_idle_call (); }/* [Note:switch to a different task.
We'll return to this loop when the idle task are again selected to run.
* * schedule_preempt_disabled (); }
|
I ' ve omitted many details, and we'll look in task switching closely later on, but if you read the code for you ' ll get the G Ist of It:as long as there ' s no need to reschedule, meaning change the active task, stay idle. Measured in elapsed time, this loop and it cousins in other OSes are probably the most executed pieces of code in Computi NG history. For Intel processors, staying idle traditionally meant running The halt instruction:native_halt
1 2 3 4 |
static inline void Native_halt (void) {ASM volatile ("HLT"::: "Memory");} |
HLT stops code execution in the processor and puts it in a halted state. It ' s weird to-across the world millions and millions of intel-like CPUs are spending the majority of their Halted, even while they ' re powered up. It ' s also not terribly efficient, energy wise, which led chip makers to develop-sleep deeper for states, processors Trade off less power consumption for longer wake-up latency. The kernel ' s Cpuidle subsystem is responsible for taking advantage of these power-saving modes.
Now once we tell the CPU to halt, or sleep, we are need to somehow bring it. If you ' ve read the Last post, you might suspect interrupts are involved, and indeed they. Interrupts spur the CPU out of it halted state and back into action. So putting the all together, here's what your system mostly does as you read a fully rendered Web page:
The other interrupts besides the timer interrupt also get the processor moving. That's what happens if you click on a Web page, for Example:your mouse issues a interrupt, its driver processes it, and Suddenly a process is runnable because it has fresh input. At "point" need_resched () returns True, and the idle task is booted out in favor of your.
But let ' s stick to idleness in this post. Here's the idle loop over time:
In this example the timer interrupt is programmed by the kernel to happen every 4 milliseconds (ms). This is the tick period. That means we are ticks per second and so the tick rateor tick frequency is. That's a typical value for Linux running on Intel processors with Hz being another crowd. This is defined in the CONFIG_HZ option to build the kernel.
Now this looks like a awful lot of pointless work for a idle CPU, and it is. Without fresh input from the outside world, the CPU would remain stuck in this hellish nap getting woken a sec Ond while your laptop battery is drained. If This is running in a virtual machine, we ' re burning both power and valuable cycles the from the host CPU.
The solution is "to have" a dynamic tick so and the CPU is idle, the timer interrupt is either deactivated or rep Rogrammed to happen in a point where the kernel knows there'll be work to does (for example, a process might have a timer Expiring in 5 seconds, so we must not sleep past that). This is also called tickless mode.
Finally, suppose you have one active process in a system, for example a long-running cpu-intensive task. That ' s nearly identical to a idle System:these diagrams remain about the same, just substitute the one process for the I Dle task and the pictures are accurate. In so case it's still pointless to interrupt the task every 4 ms for no good reason:it ' s merely OS jitter slowing your Work ever so slightly. Linux can also stop the fixed-rate tick in this one-process scenario, in what ' s called Adaptive-tick mode. Eventually, a fixed-rate tick may is gone altogether.
That ' s enough idleness for one post. The kernel ' s idle behavior is a important part of the OS puzzle, and it's very similar to other situations we ' ll This helps us builds the picture of a running kernel. More next week, RSS and Twitter.