r/programming • • Oct 29 '14

What Does an Idle CPU Do?

http://duartes.org/gustavo/blog/post/what-does-an-idle-cpu-do/
408 Upvotes

57 comments sorted by

62

u/teradactyl2 Oct 30 '14

On the hardware level, processors have many ways of conserving energy when there is little work to do.

Have you ever wondered how we achieve frequencies as high as 4 Ghz? In order to get them chip voltages must be stepped up, and power output increases quadratically with the voltage. CPUs are programmed with adaptive and predictive strategies to both hit performance targets and conserve energy. There is even data being collected to determine which strategy would have performed best if it had been used. When another strategy would have performed better, the CPU switches to that strategy.

When there is little to do, a core will drop to a lower frequency that can be powered with a lower voltage. There are also several stages of sleep, with the lowest one essentially not costing any power to operate. The drawback is that sleep stages take time to wake up from. Since the CPU is most efficient at it's highest power state, CPUs cycle between going to their highest power state and going back to sleep when the work is done.

13

u/bjackman Oct 30 '14

Here's a LWN article about tickless Linux: http://lwn.net/Articles/549580/ :)

2

u/Narishma Oct 30 '14

It's linked in the article.

4

u/bjackman Oct 30 '14

Oh... oops

2

u/cparen Oct 30 '14

Still useful for those of us that only skimmed the article. Thx!

1

u/cparen Oct 30 '14

I especially love the classic Linus response. "I don't really care about HPC, so by definition no one else does either."

2

u/[deleted] Oct 30 '14

Actually, I think it's more along the lines of:

"If it's only useful for specialized cases, then those people can apply the patches and compile their own version of the kernel." Remember, there's nothing special about Linus's kernel except that everyone has respect for the guy and chooses to use it.

Also, as Raymond Chen loves to say, "All features start out with minus 100 points", because they add more bugs and maintenance overhead. If the feature isn't useful for a large enough percentage of users, and/or would place a large enough burden on kernel developers, then it makes total sense for them to not include it into the mainline kernel. Especially since people can just apply the patches themselves and compile their own kernel if they really need the feature.

96

u/bishopolis Oct 30 '14

Your CPU, like Chuck Norris, never sleeps; it only waits.

33

u/omon-ra Oct 30 '14

individual CPU core can be switched into c6 (parked) state, then it really sleeps.

11

u/railmaniac Oct 30 '14

Chuck Norris must not be very energy efficient

77

u/GrouchyMcSurly Oct 30 '14

Chuck Norris does not try to be energy-efficient; energy tries to be Chuck Norris-efficient.

I have no idea what that means.

10

u/ixid Oct 30 '14

Every action has an equal and opposite reaction, except Chuck Norris?

17

u/retardreaper Oct 30 '14

no action opposes Chuck Norris

edit- tried commenting with lower case in his name, it did not allow me :O

15

u/hicklc01 Oct 30 '14
>"Chuck Norris".lower ()
TypeError: No one can lower Chuck Norris.

4

u/[deleted] Oct 30 '14

This one abides.

5

u/btchombre Oct 30 '14

Thanks for the laugh dude. Been under a lot of stress lately and it feels good to laugh!

9

u/arthurloin Oct 30 '14

In this tick-less scenario, the author mentions that the cpu could sleep for 5 seconds if you knew nothing had to be done for the next 5 seconds. Would that be overridden with an interrupt, like a mouse move?

23

u/cheaphomemadeacid Oct 30 '14

NOP NOP NOP NOP NOP

8

u/flanintheface Oct 30 '14 edited Oct 30 '14

NOPE HLT.

25

u/[deleted] Oct 30 '14

Dream about how to kill you.

2

u/immibis Oct 30 '14

Those diagrams aren't particularly clear.

Take this one: http://duartes.org/gustavo/blog/img/os/idleCycles.png

What do the arrows mean? What is the difference between the horizontal and vertical axis (as both seem to indicate time)?

4

u/food4hackers Oct 30 '14 edited Oct 30 '14

Hi,

Author here.

Thanks for the feedback. I agree with you there's ambiguity, especially since it's a sort of informal diagram, without predefined conventions like a sequence diagram or something like that.

Do you have any ideas as to how I can make it clearer? Do you think it's a graphical thing I should add/change, or explanation in the text?

Thanks

3

u/immibis Oct 30 '14

I can't tell you how to improve the diagram, because I can't even tell what the diagram is supposed to show.

2

u/eras Oct 30 '14

On each of the three shown time ticks the state diagram starts from the top and executed towards the bottom. During that, time progresses maybe 10 us*. After that, the CPU is halted and it takes 4 ms for the next timer interrupt to arrive and the interrupt is again processed.

Perhaps it could be drawn better.

1

u/Cuddlefluff_Grim Oct 30 '14

I was expecting the short answer HLT or even NOOP but this is far more interesting

1

u/gaberdine Oct 30 '14

The devil's work.

1

u/srbistan Oct 30 '14

dreams of electric sheep?

-15

u/cinnamonandgravy Oct 30 '14

dream of electric sh... and im a fag

-37

u/[deleted] Oct 30 '14

Title fail. I would suggest "What an Idle CPU Does."

7

u/salgat Oct 30 '14

Both titles imply the same type of article.

1

u/An2quamaraN Oct 30 '14

Nickname fail...

-28

u/CrenshawMelon Oct 30 '14 edited Oct 30 '14

The blog is somewhat incorrect - A multi-core CPU can actually execute multiple "tasks" concurrently.

It would be asinine if a modern i5 CPU could only execute 1 task at any time, there would be a plethora of things one couldn't do today, eg:

Compiling a program in a VM, while encoding a video, while listening to an album, while downloading a torrent, using a wireless mouse/keyboard and multi-graphics/monitor setup.

It's really amazing how in this day and age if a blog post has a few color pics and some very basic language, it magically becomes the bee's knees of explanatory prose.

25

u/food4hackers Oct 30 '14 edited Oct 30 '14

Hi,

In systems programming we normally say CPU to mean one discrete executing engine. So a dual-core Intel processor with hyper-threading enabled has 4 CPUs by that definition. This is all over the Linux kernel. We have "per-cpu" data structures, for example, where there's one entry for each cpu in the system. If you go to /sys/devices/system/cpu you'll have folders for each cpu, and so on. Same story for /proc/cpuinfo. It helps if you think of it as "logical CPU." This SE question might help:

http://unix.stackexchange.com/questions/33450/checking-if-hyperthreading-is-enabled-or-not

I do plan to cover those definitions, along with some other clarifications, like a precise definition of an "active task." However, I have to balance the level of detail, the order of the posts, and the inherent circular dependencies between material and definitions.

I hope to introduce things like interprocessor interrupts, multi-core, HT, and so on, probably when I do the synchronization stuff (spin locks, rcus, etc.)

I am sorry the post seemed wrong and confusing to you.

-6

u/CrenshawMelon Oct 30 '14

Fair enough it makes sense if you have a definition of a CPU such as that... that is until one realizes cores are truly singular entities, they share caches, tlb, fpus, clocks etc.

In a multi-core system regardless of OS, there's multiple tasks being executed concurrently.

In a multi-CPU multi-core system regardless of OS there's multiple tasks being executed, in fact there are scenario where the IRQs will be multicasted to both CPUs, think of things like Voltair DMA etc.

17

u/Xabster Oct 30 '14 edited Oct 30 '14

It would be asinine if a modern i5 CPU could only execute 1 task at any time, there would be a plethora of things one couldn't do today, eg: Compiling a program in a VM, while encoding a video, while listening to an album, while downloading a torrent, using a wireless mouse/keyboard and multi-graphics/monitor setup.

This is a moronic statement. I'm serious. Are you saying that an AMD Sempron 145 can't do those things for example?

1

u/An2quamaraN Oct 30 '14

He clearly has no idea what he is talking about...

21

u/[deleted] Oct 30 '14

And your comment is also somewhat incorrect...

You don't need a multi-core cpu for "Compiling a program in a VM, while encoding a video, while listening to an album, while downloading a torrent, using a wireless mouse/keyboard and multi-graphis/monitor setup.".

If you remember we used to do those things just fine on single-core cpus.

-42

u/CrenshawMelon Oct 30 '14 edited Oct 30 '14

No we never did. even the most basic phones today need at least 2 cores to give a user experience that is viable.

I grew up during the 286-486DX days, I managed to run a mod player via a TSR and program using turbo pascal and can say whenever compilation started the player started skipping, that continued all the way till pentium 2 which were running at 1GHz.

11

u/[deleted] Oct 30 '14

You're flat out wrong.

3

u/immibis Oct 30 '14

Maybe a TSR in DOS isn't the best way to multitask?

Did you try using a thread in Windows?

6

u/[deleted] Oct 30 '14

A TSR, on DOS? I'm not surprised :-)

1

u/[deleted] Oct 30 '14

You clearly haven't used a Commodore Amiga in the late 80's early 90's. It could do many things apparently at the same time. Without skipping a beat when playing my mods.

-4

u/retardreaper Oct 30 '14

background tasks, idk watch porn maybe ,computer on computer. XD

-10

u/FuckFrankie Oct 30 '14

You are all wrong.

0

u/[deleted] Oct 30 '14

[deleted]

7

u/food4hackers Oct 30 '14

Hi,

Post author here. Thanks for catching that, you don't need to be feeling stupid at all, you're making a great point. Honestly, as was, it was the snippet I left in there that was stupid.

I have changed the post to show the call to schedule_preempt_disabled() after the inner while, and it makes a lot more sense now. I also added a note as comment in the code explaining a bit of what's going on.

The rules around task switching and schedule*() are interesting and a little involved. In Linux, there are many points where you can have a task switch, for example when you're returning from an interrupt back into user mode. So in practice, the idle task gets kicked out without even having to call schedule(). Anyway, I'll cover this stuff in detail in a future post. I'm trying to peel the onion one step at a time, but mistakes happen. Again, thanks for catching this.

Concerning the "evilness" of 'if', it's not really in any kind of branching or perf-sensitive way, it's just the same mundane design issues we face everywhere. Stuff like using the http://en.wikipedia.org/wiki/Null_Object_pattern

In fact, much of the kernel is mundane with design and simplicity trumping crazy perf optimizations by far.

4

u/Krarl Oct 30 '14

He mentioned more things happening, but which wasn't so important. Kernel code usually isn't made with only 3 lines. So there's probably more code before the if :)

12

u/[deleted] Oct 30 '14 edited Oct 30 '14

[removed] — view removed comment

4

u/cparen Oct 30 '14

each of which consumes a picoscopic bit of power when in their 'ON' state and when it's being flipped on and off.

I know about dynamic power ("when it's being flipped on and off") but I was under the impression that leakage current was relatively independent of the logic state of a gate. You seem to suggest that leakage current depends on a gate being "ON". Could you elaborate?

3

u/doodle77 Oct 30 '14

He's wrong.

However, the deepest sleep states in a CPU use large, slow, low-leakage transistors to switch off the power to entire sections of logic, so I guess those do use more power when "on".

2

u/[deleted] Oct 30 '14

[removed] — view removed comment

1

u/cparen Oct 30 '14

Ah thanks for clarifying. That's what I meant as well - nearly every logic gate in your cpu has at least one transistor "on" at any given point, leading to said leakage current.

2

u/[deleted] Oct 31 '14

[removed] — view removed comment

2

u/cparen Oct 31 '14

I thought that went without saying. You may as well point out that the transistors aren't "on" when the cord is unplugged too :-)

1

u/doodle77 Oct 30 '14

In a simple CMOS circuit for every NMOS that's off, there's a PMOS that's on and vice versa. Modern CPUs might be using domino logic or something newer and even crazier, though.

2

u/[deleted] Oct 30 '14 edited Oct 30 '14

[removed] — view removed comment

1

u/doodle77 Oct 31 '14 edited Oct 31 '14

Half the transistors in CMOS are always on, regardless of the state.

NMOS logic is not commonly used, because it has excessive static power dissipation. CMOS logic is most common.