About saving power in Debian - Terminal In A Simputer

About saving power in Debian

Recently I reinstalled Debian with the rc (Click here if u r interested) to obtain a fresh Debian Testing system and updated it to sid.
Debian Sid is also called Debian Unstable, but don't be too scared to try it out, it's not that dangerous.

Leap to the topic. Generally, when it comes to Linux, the open-source thing is often famous for making a laptop last much shorter than Windows system due to lack of drive support by default.
Actually, with some optimization, Linux systems like Debian could really extend the battery life.

Take my Lenovo m490 for example, it could last about 7 hours on battery if I keep slightly using it without watching videos and listening to some music.

Obtain a better battery

Battery really matters, I bought one 9-cell battery to replace the original 6-cell battery on M490 and found out the new one is 1.5~2 times the life of the original one in the same condition.

If u wanna buy a laptop and cares about the battery life, u should really care about the number of the cells in battery.

Use Intel Graphical Card

Unfortunately, Lenovo M490 is a Nvidia Optimus Laptop, bringing a world of pain to Linux users:

  1. Both GPUs are on and active.
  2. The HDMI output is “hardwired” to the discrete GPU.
  3. The VGA output is “hardwired” to the internal GPU.
  4. Only the VGA output works by default.
  5. You can work with only one output at a time.
  6. You can only toggle between outputs using an external project such as Bumblebee.

If u happen to be a Nvidia Optimus user and wanna solve the problem, install and configure as below:

  1. Install Nvidia Proprietary Driver. (From Debian Wiki)

    1. Install nvidia-detect if u haven't installed it.

      sudo apt-get install nvidia-detect
      
    2. Detect.

      nvidia-detect
      
    3. Watch what it returned and install the recommended package. For example, when it returns this:

      Detected NVIDIA GPUs:
      02:00.0 VGA compatible controller [0300]: NVIDIA Corporation GF108 [GeForce GT 430] [10de:0de1] (rev a1)
      Your card is supported by the default drivers.
      It is recommended to install the
          nvidia-driver
      package.
      

      u should install the nvidia-driver package.

      sudo apt-get install nvidia-driver
      
  2. Find the version for ur driver here.

  3. Follow the installation here to install the Nvidia Driver things.
  4. Follow the installation here to install Bumblebee. I did the things below:
    Install the free nouveau driver.

    sudo apt-get install bumblebee primus
    
  5. Permanently switching off the discrete card (From Noam Tenne's blog)

    1. Check the files and directories in /etc/modprobe.d/ .

      ls /etc/modprobe.d/
      
    2. Find the file whose name includes blacklist, for me it's nvidia-blacklists-nouveau.conf . So edit it.

      sudo nano /etc/modprobe.d/nvidia-blacklists-nouveau.conf
      

      Append the following lines to it (there's already "blacklist nouveau" in the file, so it's no use appending it again):

      # Blacklist the original nvidia module
      blacklist nvidia
      
    3. Edit /etc/modules and append the following lines to it (like what I did above):

      # Switch off discrete GPU
      bbswitch load_state=0
      
    4. Apply the changes.

      sudo update-initramfs -u
      

Powertop

The autotune works perfectly. (From This)

  1. Install powertop.

    sudo apt-get install powertop
    
  2. Make the following command a startup script [I added it to my autostart file of openbox].

    powertop --auto-tune
    

Laptop mode tools with cpufrequtils

Thank AskUbuntu, StackExchange and Debian Wiki

  1. Install Laptop mode tools

    sudo apt-get install laptop-mode-tools
    
  2. Run the tools and configure.

    sudo lmt-config-gui
    

    Be sure to turn on "Enable module cpufreq"

  3. Install cpufrequtils package.

    sudo apt-get install cpufrequtils
    
  4. See the information of the CPU.

    cpufreq-info
    

    There r 4 drivers named "intel_pstate", so edit /etc/default/grub:

    sudo nano /etc/default/grub
    

    Change the GRUB_CMDLINE_LINUX_DEFAULT line to the following:

    GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 intel_pstate=disable crashkernel=384M-:128M"
    

    Then run:

    sudo update-grub
    
  5. Reboot, and get the current CPU governor:

    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    

    When the laptop is unplugged, it should return powersave.
    Run the command cpufreq-info again, it'll return something like this, meaning the process has just succeeded:

    cpufrequtils 008: cpufreq-info (C) Dominik Brodowski 2004-2009
    Report errors and bugs to [email protected], please.
    analyzing CPU 0:
      driver: acpi-cpufreq
      CPUs which run at the same hardware frequency: 0
      CPUs which need to have their frequency coordinated by software: 0
      maximum transition latency: 10.0 us.
      hardware limits: 1.20 GHz - 2.60 GHz
      available frequency steps: 2.60 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz
      available cpufreq governors: conservative, userspace, powersave, ondemand, performance, schedutil
      current policy: frequency should be within 1.20 GHz and 2.60 GHz.
                      The governor "powersave" may decide which speed to use
                      within this range.
      current CPU frequency is 1.20 GHz.
      cpufreq stats: 2.60 GHz:0.03%, 2.60 GHz:0.00%, 2.50 GHz:0.00%, 2.40 GHz:0.00%, 2.30 GHz:0.00%, 2.20 GHz:0.00%, 2.10 GHz:0.00%, 2.00 GHz:0.00%, 1.90 GHz:0.00%, 1.80 GHz:0.00%, 1.70 GHz:0.00%, 1.60 GHz:0.00%, 1.50 GHz:0.00%, 1.40 GHz:0.00%, 1.30 GHz:0.00%, 1.20 GHz:99.97%  (7958)
    analyzing CPU 1:
      driver: acpi-cpufreq
      CPUs which run at the same hardware frequency: 1
      CPUs which need to have their frequency coordinated by software: 1
      maximum transition latency: 10.0 us.
      hardware limits: 1.20 GHz - 2.60 GHz
      available frequency steps: 2.60 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz
      available cpufreq governors: conservative, userspace, powersave, ondemand, performance, schedutil
      current policy: frequency should be within 1.20 GHz and 2.60 GHz.
                      The governor "powersave" may decide which speed to use
                      within this range.
      current CPU frequency is 1.20 GHz.
      cpufreq stats: 2.60 GHz:0.02%, 2.60 GHz:0.00%, 2.50 GHz:0.00%, 2.40 GHz:0.00%, 2.30 GHz:0.00%, 2.20 GHz:0.00%, 2.10 GHz:0.00%, 2.00 GHz:0.00%, 1.90 GHz:0.00%, 1.80 GHz:0.00%, 1.70 GHz:0.00%, 1.60 GHz:0.00%, 1.50 GHz:0.00%, 1.40 GHz:0.00%, 1.30 GHz:0.00%, 1.20 GHz:99.98%  (2660)
    analyzing CPU 2:
      driver: acpi-cpufreq
      CPUs which run at the same hardware frequency: 2
      CPUs which need to have their frequency coordinated by software: 2
      maximum transition latency: 10.0 us.
      hardware limits: 1.20 GHz - 2.60 GHz
      available frequency steps: 2.60 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz
      available cpufreq governors: conservative, userspace, powersave, ondemand, performance, schedutil
      current policy: frequency should be within 1.20 GHz and 2.60 GHz.
                      The governor "powersave" may decide which speed to use
                      within this range.
      current CPU frequency is 1.20 GHz.
      cpufreq stats: 2.60 GHz:0.03%, 2.60 GHz:0.00%, 2.50 GHz:0.00%, 2.40 GHz:0.00%, 2.30 GHz:0.00%, 2.20 GHz:0.00%, 2.10 GHz:0.00%, 2.00 GHz:0.00%, 1.90 GHz:0.00%, 1.80 GHz:0.00%, 1.70 GHz:0.00%, 1.60 GHz:0.00%, 1.50 GHz:0.00%, 1.40 GHz:0.00%, 1.30 GHz:0.00%, 1.20 GHz:99.97%  (5310)
    analyzing CPU 3:
      driver: acpi-cpufreq
      CPUs which run at the same hardware frequency: 3
      CPUs which need to have their frequency coordinated by software: 3
      maximum transition latency: 10.0 us.
      hardware limits: 1.20 GHz - 2.60 GHz
      available frequency steps: 2.60 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz
      available cpufreq governors: conservative, userspace, powersave, ondemand, performance, schedutil
      current policy: frequency should be within 1.20 GHz and 2.60 GHz.
                      The governor "powersave" may decide which speed to use
                      within this range.
      current CPU frequency is 1.20 GHz.
      cpufreq stats: 2.60 GHz:0.03%, 2.60 GHz:0.00%, 2.50 GHz:0.00%, 2.40 GHz:0.00%, 2.30 GHz:0.00%, 2.20 GHz:0.00%, 2.10 GHz:0.00%, 2.00 GHz:0.00%, 1.90 GHz:0.00%, 1.80 GHz:0.00%, 1.70 GHz:0.00%, 1.60 GHz:0.00%, 1.50 GHz:0.00%, 1.40 GHz:0.00%, 1.30 GHz:0.00%, 1.20 GHz:99.97%  (2654)
    

Use a lightweight desktop or windows manager

I just leaped from XFCE to LXDE for more battery life, then from LXDE to its windows manager, openbox.

Now I use LXDE when the laptop is plugged and Openbox when it is unplgged. All startup script for longer life is added to the autostart file of openbox.

BTW, Lxde and Openbox doesn't have power manager by default, so I installed acpid(Thank Openbox Wiki for help) to configure the action when the Laptop is closed. It's easy u should refer to this to understand.

My laptop suspends when its lid is closed. My acpid scripts for it:

  1. /etc/acpi/events/lm_lid (It exists by default and don't need much configuration)

    event=button/lid
    action=/etc/acpi/actions/lm_lid.sh %e
    
  2. /etc/acpi/actions/lm_lid.sh (It exists by default too)

    #! /bin/sh

    sudo pm-suspend

To make your changes take effect after adding or modifying the events files you must do a kill -SIGHUP `pidof acpid`

If u like, u could try something like i3 for longer battery life.

Brightness

By default, there's no GUI for brightness in LXDE, I tried the following command and it worked:

echo 5 | sudo tee /sys/class/backlight/intel_backlight/brightness

So adjust the number and do it the way u like! (From AskUbuntu)

Disable touchpad

Thank Arch Linux Wiki (I couldn't find the link, but thanks) for inspiration.

  1. List ur input devices using the following command (sudo apt-get install xinput if it's not been installed).

    xinput

    For me, it returns:

    ⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
    ⎜   ↳ MOSART Semi. 2.4G Keyboard Mouse        	id=11	[slave  pointer  (2)]
    ⎜   ↳ SynPS/2 Synaptics TouchPad              	id=14	[slave  pointer  (2)]
    ⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
        ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
        ↳ Power Button                            	id=6	[slave  keyboard (3)]
        ↳ Video Bus                               	id=7	[slave  keyboard (3)]
        ↳ Video Bus                               	id=8	[slave  keyboard (3)]
        ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
        ↳ AT Translated Set 2 keyboard            	id=13	[slave  keyboard (3)]
        ↳ ThinkPad Extra Buttons                  	id=15	[slave  keyboard (3)]
        ↳ MOSART Semi. 2.4G Keyboard Mouse        	id=16	[slave  keyboard (3)]
        ↳ MOSART Semi. 2.4G Keyboard Mouse        	id=10	[slave  keyboard (3)]
        ↳ Integrated Camera                       	id=12	[slave  keyboard (3)]
    
  2. Disable the touchpad using the following command.

    xinput --disable "SynPS/2 Synaptics TouchPad"

    If it takes effect, make it a startup script (I added it to my autostart file for mouse version of the openbox).

Make a Mouse Version for Openbox

Sometimes when I forget to bring a mouse with me outside, I will need the touchpad to do something for the laptop, so there's always a need to have a version for mouse with touchpad disabled and a version for touchpad.

So I made a version for mouse. (Thank Ubuntu Wiki for the inspiration)

  1. Make a desktop for the version.

    sudo nano /usr/share/xsessions/openbox1.desktop
  2. In the new file, type the following things.

    [Desktop Entry]
    Name=Openbox (Mouse Session)
    Comment=Log in using the Openbox window manager (without a session manager)
    Exec=/home/yum/openbox-mouse-session
    Icon=openbox
    Type=Application
    
  3. This means I will have to make a file for it. Now that I've been logged in as the user yum and the Terminal session is now in my own home directory /home/yum, I just have to make a file for it and edit the file:

    nano openbox-mouse-session
  4. Put the following code in it and save the file:

    #!/usr/bin/env bash
    
    xinput --disable "SynPS/2 Synaptics TouchPad" &
    
    exec /usr/bin/openbox-session
    
  5. Make it accessible and executable to all users.

    chmod 777 openbox-mouse-session
Now u could log out and log into the newly created session. If u r not using openbox u could replace the /usr/bin/openbox-session with ur desktop/windows manager's X Session command, then replace Openbox (Mouse Session) with ur desired name.

Someday when the laptop becomes useless, I will consider making a leap to a laptop with long battery life.