
What Does an Idle CPU Do? - brunorsini
http://duartes.org/gustavo/blog/post/what-does-an-idle-cpu-do/
======
DonGateley
In the days of OS/360 and OS/370 (IBM's mainframe OS in the 60's and '70s) no
instructions at all executed in the wait state. There was no OS level idle
process. Instead, the internal CPU microcode itself ran a little loop without
dispatching any machine instructions until the tentacles that reached out from
that loop were tickled by an external interrupting event. That loop had to be
very short for latency reasons and very prickly to test for all the conditions
that could take the processor out of it to resume dispatching instructions or
do critical internal things.

I know this because I wrote that 6 (initially 8) micro-instruction loop for
the IBM System/370 Model 155-157 in the late '60s. I was told back then that
since that model became the most widely sold machine ever I had written the
code most executed in the history of computing. :-)

The status latch in the machine that indicated that loop was running was fed
to a light on the console and, yep, it was nearly always lit other than when
booting the OS.

~~~
beat
They kept that trick up for a long time. As of AIX 3.2.5 back in the 1990s, an
"idle" RS/6000 ran 70% cpu utilization. They were super-fast and efficient in
their day, too.

Dunno how true that is today, but I wouldn't be surprised if AIX still uses
the same kind of tight event loop internally.

~~~
socceroos
RS/6000\. Heh - I'm the only person left in my state who still knows how one
of these works.

Diagnosing startup problems with all those super quick flashing codes was a
nightmare.

~~~
beat
Yeah, but SMIT is still the best old-school admin tool ever.

Back in the day, I considered doing an open-source knockoff, just to call it
SMUT.

------
AceJohnny2
Gustavo Duarte's (not to be confused with Androider Matias Duarte) blog posts
are awesome. I still highly recommend his 2009 in-depth review of how the
Linux kernel manages memory: [http://duartes.org/gustavo/blog/post/how-the-
kernel-manages-...](http://duartes.org/gustavo/blog/post/how-the-kernel-
manages-your-memory/)

~~~
pwelch
Wish I could upvote you twice. His posts are amazing.

------
s_kanev
Yeah, cpu idleness is pretty fascinating and, as it turns out, quite
important. Shameless plug, I just published a paper [1], showing that if you
mess up how deeply you go to sleep in cpuidle, you can loose up to ~15%
latency in datacenter services like websearch.

[1]
[http://www.skanev.org/papers/iiswc14ep.pdf](http://www.skanev.org/papers/iiswc14ep.pdf)

~~~
amluto
This reminds me: if you're stuck on a Linux kernel before 3.9 and you're using
Sandy Bridge hardware or newer, turn off C1E in BIOS. The "C1E" setting
controls a misfeature in which the CPU will look at the OS's request for a
light sleep and say "Ha! You must have asked for light sleep by accident! I'm
going to go all the way to sleep instead!" This causes surprisingly severe
performance problems (we've seen milliseconds of unexplained random hiccups).

Linux 3.9 and higher is immune: it just forcibly disables this feature.

~~~
makomk
Hah. I guess that's a major benchmark win - decreases power usage in common
cases, and few CPU benchmarks test wake-from-sleep latency either directly or
indirectly.

------
amluto
Tiny little correction: On x86 multi-processor machines, the CPU can be woken
from idle by an interrupt or by a write to a designated address in main memory
from another CPU. Linux uses* the latter technique to very efficiently wake up
a process on one CPU when it receives data from another CPU. This is a big
speedup on some workloads.

* This didn't work very well until Linux 3.16.

------
amelius
But what does a CPU running an idle virtual machine do?

~~~
ciupicri
By the way, FreeDOS and MS-DOS use 100% of the CPU when running on a
VirtualBox virtual machine [1].

[1]: [http://www.freedos.org/wiki/index.php/VirtualBox_-
_Chapter_7](http://www.freedos.org/wiki/index.php/VirtualBox_-_Chapter_7)

~~~
userbinator
DOS-style operating systems were written at a time when halting the CPU to
reduce power consumption was basically not a concern, so the "idle loop" is
actually an infinite loop (TSRs often hooked into this loop to do "background
processing", a rudimentary form of task switching.)

 _I seriously doubt that it is healthy for any computer to run for a longer
time with 100% CPU usage._

Computers in the DOS and Win9x days would be at 100% CPU usage for basically
as long as they were switched on; and a modern one should be absolutely fine
running at 100% for an indefinite amount of time provided that things like the
cooling system are in good working condition. The biggest difference between
CPUs now and CPUs then is the difference in power consumption between
idle/sleep and full power - e.g. a 66MHz 486 consumes <5W at full power (and
not much difference when halted) vs. ~5W at idle and close to 100W for a
recent Haswell i7.

 _If I have a DOS guest running for some time in VirtualBox, with full CPU
usage, OS X eventually will pop up a nice half transparent window. It tells me
that my computer needs now to be shut down and that I should push the power
button long enough to do so. It 's the Mac version of the BSOD._

That suggests insufficient cooling. If it's a system that's been in use for a
while, it's probably time to clean out the heatsinks and fans. If a new system
is overheating from 100% CPU usage, then I would consider it defective.

~~~
kalleboo
Yeah there's definitely something wrong with his hardware. If the CPU is
overheating, even on my constantly thermally-constrained first-gen MacBook Pro
Retina, it should just throttle down the CPU speed to something safe and
painfully slow. He's definitely got some hardware fault if it's panicing
(check the panic.log)

------
leonatan
When I was young, I never understood why there was an idle process in Windows,
eating my CPU... The days without Internet.

------
abuzzooz
I believe that these days unused CPU cores are physically shut down by turning
off the voltage supply that's feeding them, in order to save power. This is a
commonly used technique in chip design called "power gating". It is one of the
most important techniques for prolonging battery life in mobile devices.

------
esmi
"The fundamental axiom here is that at any given moment, exactly one task is
active on a CPU."

Maybe I missed it, and I did check the earlier post and I understand this is
trying to be a simplification, but what was the argument for why I should take
the above axiom? Was there one?

Or I am just being too literal and all the author is really trying to say is
the current task pointer must always be valid, in Linux. Which is probably
true enough (I don't know Linux well) but a much less powerful statement.

From the post contents I suspect the author is using OS as a synonym for
Linux.

------
typedweb
I seem to recall the idle 'task' on Windows zero's empty memory pages, so
there might be less of what's happening in the article on those machines.

------
sbierwagen
Back In The Day, before Windows implemented calling the HLT instruction in the
idle loop, overclockers would run a userland program that did the same thing,
called cpuidle.

[http://screenshots.en.sftcdn.net/en/scrn/5000/5197/cpu-
idle-...](http://screenshots.en.sftcdn.net/en/scrn/5000/5197/cpu-idle-6.jpg)

[http://www.cpuidle.de/news.php](http://www.cpuidle.de/news.php)

~~~
coryking
There was actually several of them used by overclockers. I was always a fan of
"waterfall". It made a huge difference in the CPU temperature!

[http://estu.nit.ac.jp/~e982457/other/cpuidle/idle.htm](http://estu.nit.ac.jp/~e982457/other/cpuidle/idle.htm)

------
bjornsing
I was hoping for an explanation as to why process 0 needs to be "multitasking
cooperatively", as in calling needs_reschedule()... Anybody?

~~~
ww520
It's for avoiding to halt the CPU when there are other pending tasks ready to
run.

When the idle task is the only task running, it halts the CPU to save power.
When the scheduling timer interrupt fires, it wakes up the CPU and calls the
scheduler to schedule tasks. If there are other tasks ready to run, the idle
task is swapped out to let another task to run but it is still in the ready
state. Depending on the scheduling algorithm, the idle task might get picked
to run again before other ready tasks. In that case, it should not halt the
CPU but rather just busy-loops the rest of the quantum. At that time, the
timer interrupt fires and schedules another ready task to run right the way
without needing to wake up the CPU.

------
kakakiki
Excellent blog! I am loving all the other posts he has written!

------
CmonDev
So there is a distributed mega-super-computer being idle on this planet.

------
DogeDogeDoge
nop nop nop nop

------
Codhisattva
Counts sheep.

------
sholanozie
This is a really awesome article - thanks for sharing!

------
mindslight
Spread spectrum modulate its emissions to backhaul cached crypto keys?

(/me ducks)

edit: oops, the downvoters are correct; modern cpus have dedicated hardware
for that.

------
beat
What does an idle CPU do?

Oh, lifts weights, writes a little poetry, works on its side project startup,
surfs for cat videos...

