Skip to content

Power saving in Ubuntu

June 3, 2013

First: a disclaimer: some parts of this post are obscure to me… likely they will be to you!

Second: this post is about power saving with Ubuntu GNU / Linux 12.04 (aka precise pangolin)

Third: Discrete VS integrate graphic card post is using some concepts in this post… if you are not coming from there you should read it.

Fourth: if you are not coming from Discrete VS integrate graphic card you should take a look and read it (quickly).

Well, my problem was that my Linux box: Asus N53 SV running Ubuntu 12.04 did not turn off the discrete graphic card when on battery consuming the it very quickly. Ah… active desktop manager is unity (not gnome shell). Searching among system setting did not get me anything interesting. So I started to find manual / ad-hoc solution.

Keyword I remembered was acpi.

So I started to search and fell in /etc/acpi/events.
Uao… could not believe to events which names sounded good:
ac

# /etc/acpi/events/ac
# Called when the user connects ac power to us
#

event=ac_adapter
action=/etc/acpi/power.sh

battery

# /etc/acpi/events/battery
# Called when AC power goes away and we switch to battery

event=battery
action=/etc/acpi/power.sh

Both call /etc/acpi/power.sh. All seem straightforward but already here something strange. From acpid(8)

The string “%e” will be replaced by the literal text of the event for which the action was invoked

So: why event’s name is not passed to action? No answer yey, sorry.
power.sh looks like:

#!/bin/sh

test -f /usr/share/acpi-support/key-constants || exit 0

. /usr/share/acpi-support/policy-funcs

CheckUPowerPolicy

if [ -z "$*" ] && ( [ `CheckPolicy` = 0 ] || CheckUPowerPolicy ); then
    exit;
fi

pm-powersave $*

It calls pm-powersave but before some checks that are defined in /usr/share/acpi-support/policy-funcs:

CheckUPowerPolicy() {
	if pidof upowerd > /dev/null; then
		return 0;
	else
		return 1;
	fi
}
CheckPolicy() {
	local PMS
	PMS="gnome-power-manager kpowersave xfce4-power-manager"
	PMS="$PMS guidance-power-manager.py dalston-power-applet"
	if pidof -x $PMS > /dev/null ||
	   (pidof dcopserver > /dev/null && test -x /usr/bin/dcop && /usr/bin/dcop kded kded loadedModules | grep -q klaptopdaemon) ||
	   PowerDevilRunning ; then
		echo 0;
	else
		echo 1;
	fi
}

PowerDevilRunning() {
	test -x /usr/bin/dbus-send || return 1

	for p in $(pidof kded4); do
		test -r /proc/$p/environ || continue
		local DBUS_SESS=$(cat /proc/$p/environ | grep -z "DBUS_SESSION_BUS_ADDRESS=")
		test "$DBUS_SESS" != "" || continue
		(su - $(ps -o user= $p) -c "$DBUS_SESS dbus-send --print-reply --dest=org.kde.kded /kded org.kde.kded.loadedModules" | grep -q powerdevil) && return 0
	done

	return 1
}

Now:

  • $* is always an empty string: see above
  • CheckPolicy always echos a 0
  • CheckUPowerPolicy returns true (0) upowerd is running

So pm-powersave is never called.

That’s not fully true actually. It happened that it is called:

on battery:

20130416081250: args:
20130416081250: CheckPolicy: 1
20130416081250: CheckUPowerPolicy: 1
20130416081250: doing pm-powersave…20130419224831: args:

on ac:

20130416081250: args:
20130419224831: CheckPolicy: 1
20130419224831: CheckUPowerPolicy: 1
20130419224831: doing pm-powersave…

So pm-powersave is called… but not I need that.

Uh-oh… that’s a problem!
Solution I found was to work directly in /etc/pm/power.d.
Scripts there are called with argument true is power saving must enabled (running on battery) and false must be disabled (running on ac).
This is the script I put there:

#!/bin/sh

DEBUG=n
FILE_LOG="/tmp/ac-battery.log"

logDebug ()
{
	if [ "${DEBUG}" != "y" ] ; then
		return 0
	fi

	echo "$(date +%Y%d%m%H%M%S) [DD] $*" >>"${FILE_LOG}"
}

battery ()
{
	logDebug "setting on battery cofiguration."

	logDebug "forcing brightness to 1."
       	echo 1 >/sys/class/backlight/acpi_video1/brightness

	sleep 1m
	logDebug "turning off discrete graphic card."
	echo OFF >/sys/kernel/debug/vgaswitcheroo/switch
}

ac ()
{
	logDebug "setting on ac cofiguration."

	logDebug "turning on discrete graphic card."
	echo ON >/sys/kernel/debug/vgaswitcheroo/switch

	logDebug "forcing brightness to 9."
	echo 9 >/sys/class/backlight/acpi_video0/brightness
}

case $1 in
	true | True | TRUE)
		battery &
	;;

	false | False | FALSE)
		ac &
	;;

	*)
		logDebug "$0 called with unknown '$1' argument."
	;;
esac

Note the sleep 1m in battery function. Why?
Well do not know but without that pc sticks when graphic login screen is appearing or while typing the password or just after confirm the password. That 1 minute delay solved all my problems.

That’s all folks: now when running on battery brightness is lowered to 1 and discrete graphic card is turned off and when running on ac brightness is raised to 9 and discrete graphic card tuned on.

An additional note.
I grabbed output of acpi_listen and
going on battery:

ac_adapter AC0 00000080 00000001
processor CPU0 00000081 00000000
processor CPU1 00000081 00000000
processor CPU2 00000081 00000000
processor CPU3 00000081 00000000
processor CPU4 00000081 00000000
processor CPU5 00000081 00000000
processor CPU6 00000081 00000000
processor CPU7 00000081 00000000
battery BAT0 00000080 00000001
battery BAT0 00000081 00000001
ac_adapter AC0 00000080 00000001

goind on ac:

ac_adapter AC0 00000080 00000000
processor CPU0 00000081 00000000
processor CPU1 00000081 00000000
processor CPU2 00000081 00000000
processor CPU3 00000081 00000000
processor CPU4 00000081 00000000
processor CPU5 00000081 00000000
processor CPU6 00000081 00000000
processor CPU7 00000081 00000000
battery BAT0 00000080 00000001

Really have not understand how to interpret it!

From → Technology

One Comment
  1. anthonyvenable110 permalink

    Reblogged this on anthonyvenable110.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: