Lenovo Thinkpad X300 , battery management and tp_smapi

This article briefly presents a « solution » to the battery switching issue mentioned at the bottom of this blog entry.

Indeed, while having two batteries ensures a (very) long battery life, you have to switch manually from the secondary battery to the primary, in order to prevent deep discharges that harms lithium batteries.

Until now, I used to manage it by typing some aliases in my shell, but it is clearly not convenient nor reliable (Sometimes, I simply forget it).

But now, one little bash script and one line in your root’s crontable, and the job’s done :)

There is battery_switch.sh (deprecated, see baswim.sh below). It basically allows automatic switching to main battery when a given percentage level is reached by the secondary battery. Some parameters are available by editing variables values directly in the script, but only LOW_LEVEL is really interesting.

The cron line I use:

# sudo crontab -e

# m h  dom mon dow   command
*/5 *   *   *   *   /opt/bin/battery_switch.sh

Of course it is aware of your laptop’s status. In fact, if it switched to the main battery (by forcing the main battery to discharge), and then you re-plug your laptop, it will cease to force discharge and will return to normal state.

Since I may not be clear, do a

tail -F /var/log/battery_switch.log

to get relevant information.

I hope that will help :)

EDIT: please find there baswim (battery switching improved) a new script that provides a *really* interesting feature: It permits to switch from one battery to another according to both remaining percentage and cycles count.

It follows this simple scheme:

1) Get batteries cycle status 
2) Decide which battery should discharge first based on cycles and remaining percentage
(above the low_level) 
3) Get AC status
3) If AC is unplugged, begin discharge with chosen battery
4) If needed by long unplugged operation, switch to the other battery

It basically a hack from the first script, there are some oddities (one line functions…) but hey it works (surprisingly)!

EDIT: A little but likely important update. This time, both scripts can handle two cases: one battery plus dvd-drive or both batteries.

EDIT:

If you want baswim to be a little more aware of acpi events,

you can make some symbolic links in /etc/acpi/battery.d and /etc/acpi/ac.d.

ln -s /opt/bin/baswim.sh /etc/acpi/battery.d/
ln -s /opt/bin/baswim.sh /etc/acpi/ac.d/

Then, baswim will be executed every time you plug or unplug your AC.

Lenovo Thinkpad X300 and Linux – Configuration and power management

I recently replaced my Macbook with a Lenovo Thinkpad X300 notebook. Here are some notes and tips concerning GNU/Linux installation and configuration for an optimal day to day use.

UPDATE (2011/07)

Here are my current settings for power management :

/etc/rc.local: Simplified, no more vm.laptop_mode (not useful with a ssd).

# Audio powersave
echo 1 > /sys/module/snd_hda_intel/parameters/power_save

# For thinkfan
echo watchdog 0 >| /proc/acpi/ibm/fan
chmod 666 /proc/acpi/ibm/fan

# For PHC Voltage regulation kernel patch http://www.linux-phc.org
echo 15 15 15 >| /sys/devices/system/cpu/cpu0/cpufreq/phc_vids
echo 15 15 15 >| /sys/devices/system/cpu/cpu1/cpufreq/phc_vids

/etc/thinkfan.conf: Simplified, less fan, more silence :)

(0,     0,      40)
(1,     35,     58)
(2,     56,     64)
(5,     58,     128)

Do not forget to follow powertop indications.

First, the specs of this new toy:

* Intel® Core 2 Duo SL7100

* 2x1Go PC2-5300 DDR

* 64GB SSD

* 1.61 Kg with 6 cells main battery and Ultrabay 3 cells secondary battery

Hardware

Then, lspci output:

00:00.0 Host bridge: Intel Corporation Mobile PM965/GM965/GL960 Memory Controller Hub (rev 0c)
00:02.0 VGA compatible controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)
00:02.1 Display controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)
00:19.0 Ethernet controller: Intel Corporation 82566MM Gigabit Network Connection (rev 03)
00:1a.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #4 (rev 03)
00:1a.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #5 (rev 03)
00:1a.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #2 (rev 03)
00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)
00:1c.0 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 1 (rev 03)
00:1c.1 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 2 (rev 03)
00:1c.2 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 3 (rev 03)
00:1d.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #1 (rev 03)
00:1d.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #2 (rev 03)
00:1d.2 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #3 (rev 03)
00:1d.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #1 (rev 03)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev f3)
00:1f.0 ISA bridge: Intel Corporation 82801HBM (ICH8M-E) LPC Interface Controller (rev 03)
00:1f.1 IDE interface: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) IDE Controller (rev 03)
00:1f.2 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 03)
00:1f.3 SMBus: Intel Corporation 82801H (ICH8 Family) SMBus Controller (rev 03)
03:00.0 Network controller: Intel Corporation PRO/Wireless 4965 AG or AGN [Kedron] Network Connection (rev 61)

About lsusb, just one useful line:

Bus 003 Device 002: ID 0483:2016 SGS Thomson Microelectronics Fingerprint Reader

Important note: since I disabled Bluetooth, WiMAX and the touchpad in the BIOS,

they are not present in lspci and lsusb outputs, but both Bluetooth and touchpad are functional with Ubuntu.

Quite a modern hardware, and thus some issues concerning Linux support. Indeed, only the 2.6.28 offers a good support, previous versions are either bugged or hackish.

Operating System

I chose Ubuntu 9.04 beta for an easy yet fine setup, with LVM+Crypto for the root partition. However a bug in xserver-xorg-evdev in 64 bits edition breaks the must-have trackpoint scrolling feature. Thus, I decided to run 32-bit version, despite a 64 bits CPU.

Most of the hardware components work out the box, without particular setup, including the webcam. The only regression compared to previous kernel versions is the integrated microphone, which remains disabled. I hope to fix it out soon. However, the jack input line is fully functional. Update: just run alsamixer, type tab to enter in the Capture section and enable the internal microphone. Speakers offer a good (and loud) sound experience, quite surprising from a Thinkpad notebook.

No particular thoughts on Ubuntu itself, except that Jaunty Jackalope will certainly bring a better user experience than ever (new themes, new notification system, general speed improvements, boot time dramatically reduced…).

Power management

Now the real deal: how to maximize battery operation and lifetime with the Lenovo Thinkpad X300?

The average base power consumption is 14 W, WiFi on, 75% backlight, some terminals and Firefox.

First, disable everything you will never use in the BIOS. Currently, I disabled Bluetooth, WiMAX, Wireless WAN (3G), »USB Wifi » (wtf?) and touchpad. Saving: 2 W

Then disable Wake On Lan, unless you need it. It keeps the Gigabit Ethernet Controller to remain in an active state that actually use energy. Saving: 1W

Moreover, I blacklisted the e1000e module, since I mainly use WiFi. When I need wired connection, I simply modprobe it.

Then concerning the wlan0 WiFi interface, just run:

iwconfig wlan0 power on

in order to active Power Management. Saving: 0.5W

Some sysctl tweaking in /etc/sysctl.conf:

vm.laptop_mode = 5  # Activating laptop mode, power friendly I/Os
vm.dirty_writeback_centisecs = 2000
kernel.nmi_watchdog = 0
vm.swappiness = 5

And in /etc/rc.local:

echo 10 > /sys/module/snd_hda_intel/parameters/power_save
echo min_power > /sys/class/scsi_host/host0/link_power_management_policy
echo min_power > /sys/class/scsi_host/host1/link_power_management_policy

Saving: 2W

Quite simple, heh? :) Now, my average consumption is 8.8W, allowing a real use of the X300 for more than 6 hours, instead of 4h30.

Last thoughts

The internal fan management is kinda broken in Linux. The workaround is to append thinkpad_acpi to /etc/modules and options thinkpad_acpi fan_control=1 to /etc/modprobe.d/fan for instance.

Then you could use thinkfan and this sample thinkfan.conf:

# thinkfan example config file
# ============================
#
# Syntax:
# (LEVEL, LOW, HIGH)
#
# LEVEL is the fan level that's written to /proc/acpi/ibm/fan
# LOW is the temperature at which to step down to the previous level
# HIGH is the temperature at which to step up to the next level
# All numbers are integers.
#
# You can put spaces anywhere you want. Anything that doesn't match this
# syntax is ignored.
#
# The number of tuples is limited by the size of your system memory.
#
# WARNING: Sanity checks are only made for correct ordering of the fan levels
# and for (LOW < HIGH) within one tuple. You can still use any kind of insane
# temperature limits.

(0,     0,      50)
(1,     48,     58)
(2,     54,     61)
(3,     56,     63)
(4,     58,     65)
(5,     60,     66)
(7,     63,     128)

Concerning lithium batteries, you should keep them charged between 40% and 80%. It is quite easy to do with Thinkpads: just append tp_smapi to /etc/modules, and the upper charge limit will be set to 86%, which is a good compromise. Moreover, instead of completely drain the Ultrabay battery before switching to the main battery, you can manually force discharge; as root type:

echo 1 > /sys/devices/platform/smapi/BAT0/force_discharge

But do not forget that even if you replug your laptop, discharge will resume anyway!

Thus, you have to bypass forced discharge; as root type:

echo 0 > /sys/devices/platform/smapi/BAT0/force_discharge

Annoying workaround, but I will try to find or code a proper solution.

UPDATE: See my Baswim script here.

That’s all folks!

Schluss Mit Macbook

Merci à Pedrov pour l’idée de titre ;)

J’ai vendu mon macbook ce weekend, pour acquérir un Lenovo x300 neuf, avec sa batterie supplémentaire. Le tout tourne à merveille sous linux (Ubuntu 9.04 beta), avec un bon 6h d’autonomie à la clé. Tout ça pour à peine 1.6kg…

Au niveau optimisations, je n’ai rien touché au système d’origine, c’est dire la qualité du matériel et de son support sous linux.

Toutefois, la beta d’ubuntu n’y est pas pour rien, noyau 2.6.28 minimum conseillé pour profiter au maximum de la machine.

Je publierai certainement d’autres billets plus complets au fur et à mesure de mes découvertes.

Petite astuce:

xdpyinfo | grep resolution

vous donnera la resolution DPI de votre écran, qui n’est pas forcément (voire même jamais 96dpi). GNOME Appearance permet ensuite de modifier la valeur du dpi de l’écran, et ça poutre ©

XMMS2 – Garbage Collector

Dave dans ses bonnes œuvres a développé un petit script bien sympa qui vient combler une lacune actuelle de xmms2, à savoir le retrait de la Media List (mlib) des pistes supprimées ou modifiées dans le système de fichier.

En plus simple, après un rehash de la media lib, avec un simple xmms2 mlib rehash, un fichier renommé ou supprimé ne sera pas mis à jour ou retiré dans la mlib.

En attendant l’implémentation propre de la fonctionnalité, mlib-gc.py permet de corriger le tir. C’est un simple script python, à installer dans le chemin de votre choix (pour ma part /opt/bin/xmms2-mlib-gc). Seule dépendance à installer avec le gestionnaire de paquets de votre distribution GNU/Linux : python-xmmsclient.

Le fonctionnement est simple : après modification de la bibliothèque musicale, exécuter mlib-gc.py (chez moi xmms2-mlib-gc.py), exécuter xmms2 mlib addpath /chemin/mediathèque/.

J’espère que cette astuce se montrera utile pour certains, je continuerai régulièrement la rédaction de billets concernant XMMS2.