Programming for Low Power: It's everyone's responsibility Arjan van de Ven Software and Solutions Group July 25th, 2007
The next 45 minutes <ul><li>Some concepts behind power </li></ul><ul><li>How software is ruining your power consumption </...
Why software matters for power <ul><li>It’s the hardware that consumes the power after all, right? </li></ul><ul><li>Hardw...
What consumes power in a PC <ul><li>Lamps </li></ul><ul><li>Radios </li></ul><ul><li>Analog bits </li></ul><ul><li>Process...
Race-to-idle Save power by running at the highest speed <ul><li>Example:  </li></ul><ul><ul><li>CPU that consumes 34 Watts...
Race-to-idle Save power by running at the highest speed <ul><li>At half speed, energy consumption is </li></ul><ul><ul><li...
Sleep duty cycle <ul><li>It matters how frequently you go in and out of idle </li></ul><ul><li>Ideal  </li></ul><ul><li>Re...
Sleep duty cycle <ul><li>It matters how frequently you go in and out of idle </li></ul>!= Lesson: Stay in idle for long pe...
Software spoils the game <ul><li>Depending on the exact model, a processor wants idle periods of at least 20ms to 50ms for...
Enemy #1: Polling <ul><li>Applications have been polling for </li></ul><ul><ul><li>checking if the mouse moved .. once per...
Enemy #2: Timers <ul><li>Even after removing all the stupid polling, there are situations where you need to do a housekeep...
Enemy #2: Timers <ul><li>In userspace: </li></ul><ul><ul><li>use  g_timeout_add_seconds()  for glib applications </li></ul...
Enemy #3: Disk IO <ul><li>Disks and CDs are moving, analog parts </li></ul><ul><ul><li>They consume a lot of power when in...
Java / C# / Python / Ruby / ... <ul><li>High level languages give quick results.... </li></ul><ul><ul><li>... but sometime...
Special case: Media playback <ul><li>For DVD playback: Don't spin up the CD all the time </li></ul><ul><li>For audio playb...
Tools: PowerTOP and strace <ul><li>Informative: run &quot;strace&quot; on your program </li></ul><ul><ul><li>When it's idl...
Tools: PowerTOP, strace
Tools: PowerTOP, strace
 
 
C/P/T states <ul><ul><li>CPUs have 2 states: </li></ul></ul><ul><ul><ul><li>Executing instructions </li></ul></ul></ul><ul...
C states <ul><ul><li>Deeper C-states have a longer latency to wake up  </li></ul></ul><ul><ul><li>Functionality restrictio...
P states <ul><ul><li>P-states control the frequency and voltage of the processor </li></ul></ul><ul><ul><li>Frequency save...
T states <ul><ul><li>T-states forcefully introduce idle cycles in the processor </li></ul></ul><ul><ul><li>T-states do not...
Upcoming SlideShare
Loading in …5
×

Os Vandeven

2,235 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,235
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Os Vandeven

  1. 1. Programming for Low Power: It's everyone's responsibility Arjan van de Ven Software and Solutions Group July 25th, 2007
  2. 2. The next 45 minutes <ul><li>Some concepts behind power </li></ul><ul><li>How software is ruining your power consumption </li></ul><ul><li>Tools to help you diagnose </li></ul>
  3. 3. Why software matters for power <ul><li>It’s the hardware that consumes the power after all, right? </li></ul><ul><li>Hardware can scale its power consumption down … </li></ul><ul><li>… . but software needs to allow for this </li></ul><ul><li>A single, misbehaving, application can destroy any power savings the hardware can do! </li></ul>
  4. 4. What consumes power in a PC <ul><li>Lamps </li></ul><ul><li>Radios </li></ul><ul><li>Analog bits </li></ul><ul><li>Processor </li></ul><ul><ul><li>Executing code </li></ul></ul><ul><ul><li>Transitions </li></ul></ul><ul><li>Memory </li></ul>
  5. 5. Race-to-idle Save power by running at the highest speed <ul><li>Example: </li></ul><ul><ul><li>CPU that consumes 34 Watts at full speed, 24 Watts at half speed and 1 Watts when idle </li></ul></ul><ul><ul><li>MP3 decoding that takes ½ second at half speed for a second of audio, ¼ second at full speed </li></ul></ul>Half speed Full speed
  6. 6. Race-to-idle Save power by running at the highest speed <ul><li>At half speed, energy consumption is </li></ul><ul><ul><li>E = 0.5s * 24W + 0.5s * 1W = 12.5 Joules </li></ul></ul><ul><li>At full speed, energy consumption is </li></ul><ul><ul><li>E = 0.25s * 34W + 0.75s * 1W = 9.25 Joules </li></ul></ul>Lesson: Do things as fast as you can so that the system can be idle longer
  7. 7. Sleep duty cycle <ul><li>It matters how frequently you go in and out of idle </li></ul><ul><li>Ideal </li></ul><ul><li>Reality </li></ul>= !=
  8. 8. Sleep duty cycle <ul><li>It matters how frequently you go in and out of idle </li></ul>!= Lesson: Stay in idle for long periods of time Avoid interrupting idle as much as possible
  9. 9. Software spoils the game <ul><li>Depending on the exact model, a processor wants idle periods of at least 20ms to 50ms for it's power saving to work well </li></ul><ul><li>The Linux kernel (prior to 2.6.21) wakes up from idle 250 or 1000 times per second </li></ul><ul><ul><li>Tickless idle to the rescue; eliminates most of these wakeups </li></ul></ul><ul><li>On a standard Linux desktop, userspace software easily wakes the processor from idle 400 or more times per second </li></ul>Current software is misbehaving and wasting power
  10. 10. Enemy #1: Polling <ul><li>Applications have been polling for </li></ul><ul><ul><li>checking if the mouse moved .. once per second (gnome-screensaver) </li></ul></ul><ul><ul><li>checking if the sound volume changed .. 10 times per second (mixer applet) </li></ul></ul><ul><ul><li>checking if it's time to show the next minute in the clock .. once per second (clock applet) </li></ul></ul><ul><ul><li>checking if someone forgot to notify for a condition variable change .. 10 times per second (firefox) </li></ul></ul><ul><ul><li>checking if something got put on an internal queue .. 30 times per second (gamin) or 10 times per second (evolution) </li></ul></ul><ul><ul><li>checking to see if a smartcard reader got inserted on USB ... 10 times per second (gdm-daemon) </li></ul></ul><ul><ul><li>checking if there is data on a pipe .. 10000 times per second (gksu) </li></ul></ul><ul><ul><li>waking up 200 times per second (Macromedia Flash plugin) </li></ul></ul><ul><ul><li>.... </li></ul></ul><ul><li>Needless to say that this is not good programming practice </li></ul>Frequent polling causes spattergroit
  11. 11. Enemy #2: Timers <ul><li>Even after removing all the stupid polling, there are situations where you need to do a housekeeping task in the future </li></ul><ul><li>Problem: on a system there are many programs needing this, resulting the following wakeup pattern: </li></ul><ul><li>Grouping timers system wide can improve this: </li></ul>
  12. 12. Enemy #2: Timers <ul><li>In userspace: </li></ul><ul><ul><li>use g_timeout_add_seconds() for glib applications </li></ul></ul><ul><li>In kernel space: </li></ul><ul><ul><li>use round_jiffies() and round_jiffies_relative() </li></ul></ul><ul><li>Consideration: </li></ul><ul><ul><li>if the entire internet hits your server at the start of each second... it will die. Diversity is good. </li></ul></ul>Group timer events system wide but not internet wide
  13. 13. Enemy #3: Disk IO <ul><li>Disks and CDs are moving, analog parts </li></ul><ul><ul><li>They consume a lot of power when in use </li></ul></ul><ul><li>High speed links (SATA) consume lots of power </li></ul><ul><ul><li>Except when in power-save mode (idle) </li></ul></ul><ul><li>Besides the obvious, some things cause disk IO that you might not expect: </li></ul><ul><ul><li>Opening files that are in cache (atime update) </li></ul></ul><ul><ul><ul><li>use O_NOATIME flag to the open() call if possible </li></ul></ul></ul><ul><ul><li>Looking for files or directories that do not exist </li></ul></ul><ul><ul><ul><li>even when using GAMIN (or INOTIFY) </li></ul></ul></ul>
  14. 14. Java / C# / Python / Ruby / ... <ul><li>High level languages give quick results.... </li></ul><ul><ul><li>... but sometimes the semantics get implemented with frequent polling! </li></ul></ul><ul><li>Some language primitives are implemented badly </li></ul><ul><ul><li>pick a different JVM ? </li></ul></ul><ul><ul><li>Don't use the primitive </li></ul></ul><ul><li>Evaluate your runtime language environment before deciding to use it! </li></ul>
  15. 15. Special case: Media playback <ul><li>For DVD playback: Don't spin up the CD all the time </li></ul><ul><li>For audio playback: Don't spin up the harddisk all the time </li></ul><ul><li>For both: decode in larger batches (so you can be idle longer) </li></ul><ul><li>Pick a large buffer size default </li></ul><ul><ul><li>Large enough for a minute of audio or 20 minutes of video </li></ul></ul>Use large buffers for multimedia
  16. 16. Tools: PowerTOP and strace <ul><li>Informative: run &quot;strace&quot; on your program </li></ul><ul><ul><li>When it's idle.. is it really? </li></ul></ul><ul><li>Don't forget threads! </li></ul>
  17. 17. Tools: PowerTOP, strace
  18. 18. Tools: PowerTOP, strace
  19. 21. C/P/T states <ul><ul><li>CPUs have 2 states: </li></ul></ul><ul><ul><ul><li>Executing instructions </li></ul></ul></ul><ul><ul><ul><li>Not executing instructions (idle) </li></ul></ul></ul><ul><ul><li>P-states determine how fast (what frequency) the processor executes instructions </li></ul></ul><ul><ul><li>C-states determine how deep the processor can sleep when not executing instructions </li></ul></ul><ul><ul><li>T-states are the emergency brake to forcefully reduce execution speed to prevent overheating </li></ul></ul>
  20. 22. C states <ul><ul><li>Deeper C-states have a longer latency to wake up </li></ul></ul><ul><ul><li>Functionality restriction: C3 and deeper turn of cache-snooping </li></ul></ul><ul><ul><ul><li>Result: The CPU wakes up on any DMA bus master activity </li></ul></ul></ul><ul><ul><ul><li>Current CPUs don’t wake up entirely: C2-popup </li></ul></ul></ul><ul><ul><ul><li>Frequent DMA will prevent the use of C3 and deeper </li></ul></ul></ul><ul><ul><ul><ul><li>USB </li></ul></ul></ul></ul>185us 1.2 W C4/C5 85us 7.7 W C3 10us 12.9 W C2 0 13.5 W C1 34 W / 25 W C0 Latency Power C state
  21. 23. P states <ul><ul><li>P-states control the frequency and voltage of the processor </li></ul></ul><ul><ul><li>Frequency saves only a little power </li></ul></ul><ul><ul><li>Voltage is the key factor (P goes with V^2) </li></ul></ul><ul><ul><li>Intel processors can switch very quickly (microseconds) </li></ul></ul><ul><ul><li>Ondemand governor determines the best frequency in Linux </li></ul></ul>
  22. 24. T states <ul><ul><li>T-states forcefully introduce idle cycles in the processor </li></ul></ul><ul><ul><li>T-states do not control voltage! </li></ul></ul><ul><ul><li>Idle cycles do not get into power saving C-states </li></ul></ul><ul><ul><li>Avoid T-states whenever you can, they do not save energy </li></ul></ul>

×