Tools for checking ACPI battery data
Fully Charged
Linux offers some useful command-line tools that leverage the ACPI power management framework to display detailed data on the status of a laptop battery.
The quality of life for any laptop owner depends on the battery maintaining enough charge to operate off the power grid. Eventually, however, you will see a message informing you that the charge has reached a minimum threshold, and you have to save your data quickly or else switch to mains mode to continue working while the battery charges in the background.
The data for the warning message come from the Advanced Configuration and Power Interface (ACPI) [1] [2]. ACPI, which first appeared in 1996, has now replaced its predecessor, Advanced Power Management (APM); the current version 6 dates from April 2015. (For more information on APM and ACPI check out TuxMobil [3] and the ACPI how-to [4].)
ACPI used to be a separate component in Linux, but the Linux kernel developers now maintain ACPI functionality within the framework of the Unified Extensible Firmware Interface (UEFI) [5]. Linux comes with some command-line tools that let you read information on battery status and health. For Debian and its derivatives, you'll need the acpi
, acpid
, and acpi-support-base
packages.
Up to Linux 2.6.26, ACPI information was found on the proc filesystem [6] below /proc/acpi/
[7]. The developers have now reorganized the filesystem and have moved the information to /sys/class/power_supply/
.
Checking on Power
Figure 1 shows power data for a Lenovo ThinkPad X 250 running Debian 8. The device has two batteries, one built-in and one replaceable, so the first command in Figure 1 reveals three power supplies: AC
power, as well as the first and second batteries (BAT0
and BAT1
).
The following command in Figure 1:
cat /sys/class/power_supply/BAT0/status
outputs the contents of the .../BAT0/status
file, which shows the status of BAT0
. In this case, the command reveals that BAT0
is charging. The other possible messages are Discharging
and Fully charged
.
You can always obtain some basic information by parsing individual files in the /sys/class/power_supply/
directory. But finding and interpreting the data can cause some effort. A number of utilities make life easier and serve up the data in a human-readable format. These utilities include Upower [8], Acpi [9], and Acpitool [10], as well as the Intelligent Battery Monitor, Ibam [11].
All four programs provide information on the various power sources. Upower uses the --enumerate
switch (short form: -e
) (Listing 1); Acpi has two options: --ac-adapter
(-a
) and --battery
(-b
) (Listing 2).
Listing 1
Upower with enumerate
$ upower --enumerate /org/freedesktop/UPower/devices/line_power_AC /org/freedesktop/UPower/devices/battery_BAT0 /org/freedesktop/UPower/devices/battery_BAT1 /org/freedesktop/UPower/devices/DisplayDevice
Listing 2
Acpi Power Info
$ acpi --ac-adapter --battery Battery 0: Charging, 50%, 01:00:01 until charged Battery 1: Unknown, 75% Adapter 0: on-line
Acpitool provides a compact overview (Listing 3). Whereas Upower only lists the individual energy sources, Acpi and Acpitool provide more details.
Listing 3
Overview in Acpitool
$ acpitool Battery #1 : Unknown, 75.35% Battery #2 : Charging, 50.98%, 00:59:05 AC adapter : online Thermal info: <not available>
If the output in the terminal does not appeal to you, most Linux desktop systems provide a tool for monitoring power usage (Figure 2).
Details
If you call Upower with the --show-info
(-i
) option and the name of the power source, it displays all the matching details. The --dump
(-d
) switch lists the details for all the power sources.
Listing 4 shows the results for the second battery in the device. In addition to the manufacturer, the model, the serial number, and the type – a lithium-ion battery in this case – Upower lists more specifications that are useful for everyday operations. Among other things, it shows the battery's charge capacity (only around 90 percent here). Designed for a capacity of 71Wh, the battery now only manages 64Wh due to its advanced age. The time to full
line shows that it will take around 90 minutes to fully charge the battery.
Listing 4
Details in Upower
$ upower --show-info /org/freedesktop/UPower/devices/battery_BAT1 native-path: BAT1 vendor: SANYO model: 45N1777 serial: 27945 power supply: yes updated: Tue May 9, 2017 11:24:13 CEST (1 seconds ago) has history: yes has statistics: yes battery present: yes rechargeable: yes state: Charging warning-level: none energy: 34.2 Wh energy-empty: 0 Wh energy-full: 64 Wh energy-full-design: 71.28 Wh energy-rate: 21.177 W voltage: 11.566 V time to full: 1.4 hours percentage: 53% capacity: 89.7868% technology: lithium-ion icon-name: 'battery-good-charging-symbolic' History (charge): 1494321853 53,000 charging History (rate): 1494321853 21,177 charging
The acpi
command provides less detail but at least enough information for a quick overview. Call acpi
with the --details
option (-i
) enabled. The command in Listing 5 uses grep to limit the output to the information for the first battery (Battery 0
).
Listing 5
Quick Overview in Acpi
$ acpi -i | grep "Battery 0" Battery 0: Charging, 56%, 1:18:07 AM until charged Battery 0: design capacity 6127 mAh, last full capacity 5502 mAh = 89%
For a complete overview of all power sources, call acpi --everything
(short form -V
) or acpitool -Be
. In addition to the charge status of the batteries, the output includes the value of the temperature sensor, as well as information about the cooling system (Figure 3). You can retrieve the necessary information using the options --thermal
(short form -t
) or --cooling
(short form -c
).
The --show-empty
(-s
) option tells acpi
to show you devices that are not currently working. You can thus identify defective or improperly connected system components. The output in Listing 6 indicates that something is wrong with two batteries.
Listing 6
Diagnosing Battery Problems
$ acpi -s Battery 0: Unknown, 4% Battery 1: Charging, 75%, charging at zero rate -- will never fully charge.
Lifetime and Charging Duration
The Ibam tool calculates both the remaining lifetime and the probable charging time. Listing 7 shows a program call with the --battery
option, which returns a remaining battery life of 19 minutes and forecasts a charging time of around 90 minutes.
Listing 7
Ibam: Checking Battery Time
$ ibam --battery Battery time left: 0:19:12 Charge time left: 1:34:48 Adapted charge time left: 1:34:48
For graphical output, combine Ibam with Gnuplot [12]. Ibam always stores the time values for the charge state locally below ~/.ibam/
. The --plot
option evaluates this data. You will see a Gnuplot window displaying the charge state (Figure 4), where the x axis represents the recorded charge state and the y axis shows the time.
Buy this article as PDF
(incl. VAT)