Talk:ACPI sleep power drain test script

From ThinkWiki
Jump to: navigation, search

Another ACPI testing script

Another ACPI testing script I wrote (/modified the existing one) for more than one battery, and integration in existing suspend scripts (i.e. the Ubuntu suspend system)

most people will go fine with the original, just for the case someone prefers support for more than one battery. I'm not quite sure if the results will be exact for multiple Batteries, since I experience an extra drain when my Thinkpad is switching.


Part 1 must be executed before suspending, Part 2 afterwards.


Part 1:

#!/bin/sh
# test script for measuring power drain during suspend-to-ram with ACPI
# execute before suspend
# in ubuntu: save in /etc/acpi/suspend.d/

# default settings, change if needed
LOG=/var/log/battery.log
DATEFILE=/tmp/BATTESTDATE
BATFILE=/tmp/BATTESTLOAD

# save system time
hwclock --systohc

# get start values
date >> $LOG
DATE_BEFORE=`date +%s`

echo $DATE_BEFORE >$DATEFILE
#get battery states
BAT_BEFORE=0
BATS_DISCHARGING=0
for b in /proc/acpi/battery/*; do
	CBAT=`grep 'remaining capacity' $b/state | awk '{print $3}'`
	BAT_BEFORE=`echo "$BAT_BEFORE + $CBAT"	|bc`
	
	if grep -q '^charging state:.*discharging' $b/state; then
		BATS_DISCHARGING=`echo "$BATS_DISCHARGING + 1" |bc`
	fi
done

echo $BAT_BEFORE >$BATFILE

if [ $BATS_DISCHARGING -eq 0 ]; then
	echo '!!! Not running on battery power while going to sleep!' >> $LOG
fi

Part 2:

#!/bin/sh
# test script for measuring power drain during suspend-to-ram with ACPI
# execute after suspend
# in ubuntu: save in /etc/acpi/resume.d/

# default settings, change if needed
LOG=/var/log/battery.log
DATEFILE=/tmp/BATTESTDATE
BATFILE=/tmp/BATTESTLOAD
BATTERY=BAT0
THRESHOLD=1000

# restore system time
hwclock --hctosys 

DATE_BEFORE=`cat $DATEFILE`
BAT_BEFORE=`cat $BATFILE`
rm -f $BATFILE
rm -f $DATEFILE


# get end values
DATE_AFTER=`date +%s`

#get battery states
BAT_AFTER=0
BATS_DISCHARGING=0
for b in /proc/acpi/battery/*; do
	CBAT=`grep 'remaining capacity' $b/state | awk '{print $3}'`
	BAT_AFTER=`echo "$BAT_AFTER + $CBAT"	|bc`
	
	if grep -q '^charging state:.*discharging' $b/state; then
		BATS_DISCHARGING=`echo "$BATS_DISCHARGING + 1" |bc`
	fi
done

if [ $BATS_DISCHARGING -eq 0 ]; then
	echo '!!! Not running on battery power while waking up!' >> $LOG
fi

# do the calculations 
DIFF=`echo "$BAT_AFTER - $BAT_BEFORE" | bc`
SECONDS=`echo "$DATE_AFTER - $DATE_BEFORE" | bc`
USAGE=`echo "($DIFF * 60 * 60) / ($SECONDS)" | bc`

# output the results
echo "before: $BAT_BEFORE mWh" >> $LOG
echo "after: $BAT_AFTER mWh" >> $LOG
echo "diff: $DIFF mWh" >> $LOG
echo "seconds: $SECONDS sec" >> $LOG
echo "result: $USAGE mW" >> $LOG
if [ $USAGE -gt -$THRESHOLD ]
then
    echo "Power drain seems ok" >> $LOG
else
    echo "Power drain seems a little high" >> $LOG
fi
if [ $SECONDS -lt 1200 ]
then
    echo "!!! The notebook was suspended less than 20 minutes." >> $LOG
    echo "!!! To get representative values please let the notebook sleep" >> $LOG
    echo "!!! for at least 20 minutes." >> $LOG
fi
echo "" >> $LOG