Battery Mystries: How OS knows the battery charge level?🤔
Once upon a time I was mewing 🤫🧏🏻‍♂️ and I stared at my laptop battery icon, wondered how on Earth it knows when to freak out about low charge. Operating system is calculating and displaying battery percentage in real time. It’s just crazy, right? We are gonna talk about it like a lowkey chill guy.
I am going to use Linux for explanation because it is open and you can find information about it easily. Even you aren’t familiar with linux I will try to explain it in a way that you can understand. Also, try linux if you haven’t yet.
By the way try this command if you are on Linux (linux user -> +10000 aura đź—ż): cat /sys/class/power_supply/BAT1/capacity
or replace BAT1
with your battery name. Generally it should be BAT0
or BAT1
. The command gives the battery charge level in percentage. Also go to the /sys/class/power_supply/BAT1/
directory and see the files there. There are files like capacity, status, voltage_now, current_now etc. On reading them, we get information about the battery. The current charge(capacity) is calculated by capacity=(charge_now/charge_full)*100 and also it can be calculated by charge_percentage=(energy_now/energy_full)*100 energy in microjoules.
What are these files though, why they are here, who summoned them? They are files created by kernel and they are called sysfs files. You have probably heard “Everything is a file in Linux” right? Well this is one of the examples.
The agenda of this post is not to explain how we(user space) read it, but how the operating system(kernel) calculates it. So let’s come to that. Before that if you dont know what a kernel is then here is a short explanation. A kernel is the core of an operating system and it is the bridge between the hardware and the software. It is responsible for managing the system resources, and it is the first program that runs when the computer starts.
The main way by which the kernel talks to the battery hardware is through the ACPI(Advanced Configuration and Power Interface) which is an open standard for power management. gets battery info by retrieving information from ACPI interfaces exposed by the system firmware (BIOS/UEFI). So how our OS talks to the battery like a sigma?
ACPI from Operating System POV
ACPI is a platform independent specification. It defines the structure of ACPI tables, platform-specific registers, an interpreter for AML (ACPI Machine Language), device support, and event handling mechanisms.
Upon powering on the system, the firmware (BIOS or UEFI) is initialized, which in turn initializes the ACPI subsystem. The firmware uses data from power management hardware to populate and update the relevant ACPI tables. The operating system accesses these tables, which may include AML code, and executes them through an AML interpreter.
OS Bootloader and kernel
ACPI battery driver (acpi-battery
) is used by the Linux kernel to communicate with ACPI subsystem to obtain the battery information. During the kernel boot, the Linux kernel detects and loads ACPI subsystem using the information provided by the ACPI tables.
ACPI Table parsing
The kernel uses an ACPI interpreter to parse the AML code embedded in the ACPI tables. This allows the kernel to configure system devices, power management settings etc based on the ACPI data.
Kernel-User interaction (via sysfs)
Once ACPI subsystem is initialized the kernel exposes ACPI-related data to userspace which is avilable through sysfs interface typically under /sys/class/power_supply
and /sys/firmware/acpi
. This information can now be displayed in battery status bar or read by applications by reading data from sysfs(e.g., capacity
, staus
).
TLDR;
When the system powers up, the firmware initializes ACPI(an open specification for power management), loads ACPI tables, and hands them over to the kernel, which parses them for hardware and power management. ACPI data is then exposed via sysfs, allowing userspace tools and GUI utilities(like the battery status bar) to display power information such as battery status and capacity.
I found this cool paper very useful while writing this blog. It provides a deeper dive into the topic, so definitely check it out.
References
I hope you liked this post. If you wanna say Hi or anything, here’s my Telegram @ashirbadtele or you can mail me at ashirbadreal@proton.me.