Minimal Powertop as Bash Script esp. for Embedded Environments

I wrote this for my kindle and android devices. It's a nice, easy script though and should be great especially for embedded environments, where you have a shell but not all of the powertop dependencies. It works wonderfully on my notebook and netbook, but you may have to slightly adjust the path names to work on your system.

It turns out that this wasn't needed for either, as the kindle comes with powertop preinstalled. But it can't work on either of my devices, as timer_stats are disabled in their kernels.
I do wonder: Why do they disable the timer_stats? Is the overhead of timer_stats too high? Let me know how you use it!

The output of the following script looks like this:
Total Timer Interrupts
  0:    4257659   IO-APIC-edge      timer
LOC:    1052758   Local timer interrupts

Number of CPU Cycles spent in power mode
C0: 1511 | C1: 92 | C2: 31214 | C3: 1618641 | Total: 1651458

Causes for Wakeup
99826 total events, 408.259 events/sec
 26121,  4670 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
 24825,  4383 SignalSender     hrtimer_start_range_ns (hrtimer_wakeup)
 12411,     0 swapper          hrtimer_start_range_ns (tick_sched_timer)
 9618,  5025 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
 6343,  5089 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
 2442,  2324 xbindkeys        hrtimer_start_range_ns (hrtimer_wakeup)
 2439,  4357 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
 1241,   943 ulatencyd        hrtimer_start_range_ns (hrtimer_wakeup)
  986,  3247 kworker/0:3      wakeup_rh (rh_timer_func)
  986,  2175 konsole          usb_hcd_poll_rh_status (rh_timer_func)
  975,  2068 plasma-desktop   hrtimer_start_range_ns (hrtimer_wakeup)
  750,  4490 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
  606,  5092 SignalSender     hrtimer_start_range_ns (hrtimer_wakeup)
  606,  4858 SignalSender     hrtimer_start_range_ns (hrtimer_wakeup)
  540,     0 swapper          hrtimer_start (tick_sched_timer)
  505,  5028 SignalSender     hrtimer_start_range_ns (hrtimer_wakeup)
  505,  4973 SignalSender     hrtimer_start_range_ns (hrtimer_wakeup)
  487,  4325 chrome           hrtimer_start_range_ns (hrtimer_wakeup)
  358,  2175 konsole          hrtimer_start_range_ns (hrtimer_wakeup)

Exit with Ctrl-C



In any case, here you go:

#!/bin/bash
echo 1 | sudo tee /proc/timer_stats
for i in 0 1 2 3 4 5 6 7 8; do Cstates[$i]=0; done
while true; do
clear
[ -e /sys/class/power_supply/BAT*/current_now ] && echo Current Battery Consumption: $(cat /sys/class/power_supply/BAT*/current_now)
echo
echo Total Timer Interrupts
grep -i time /proc/interrupts
echo

echo Number of CPU Cycles spent in power mode
Cycles=0
for i in 0 1 2 3 4 5 6 7 8; do
if [ -e /sys/devices/system/cpu/cpu0/cpuidle/state${i}/time ]; then
temp=$(cat /sys/devices/system/cpu/cpu0/cpuidle/state${i}/time)
diff=$[ $temp - ${Cstates[$i]} ]
echo -n C${i}: $diff '| '
Cstates[$i]=$(cat /sys/devices/system/cpu/cpu0/cpuidle/state${i}/time)
Cycles=$[ $Cycles + $diff ]
fi
done
echo -n Total: $Cycles
echo
echo
echo Causes for Wakeup
cat /proc/timer_stats | sort -nr | head -n 20
echo
echo Exit with Ctrl-C
sleep 3
done
echo 0 | sudo tee /proc/timer_stats


If you like this post, share it and subscribe to the RSS feed so you don't miss the next one. In any case, check the related posts section below. (Because maybe I'm just having a really bad day and normally I write much more interesting articles about theses subjects! Or maybe you'll only understand what I meant here once you've read all my other posts on the topic. ;) )

No comments:

Post a Comment

I appreciate comments. And I do read them.