You are not logged in.

#1 2025-08-16 22:00:14

user0984379
Member
Registered: 2024-02-10
Posts: 5

Reading /sys/class/power_supply/BAT0/status takes a tenth of a second.

I am currently writing a script that reads the contents of /sys/class/power_supply/BAT0/status and puts it into a variable.

When the battery is charging and the output of /sys/class/power_supply/BAT0/status is either "Full" or "Charging" the time to complete the operation is normal.
However, when the battery is discharging and you read from the file /sys/class/power_supply/BAT0/status ; the operation takes an unusually long time to complete. (Especially considering how simple the operation is.)

Output:

$ echo $battery_status

$ time { battery_status=$(< /sys/class/power_supply/BAT0/status); }

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$ echo $battery_status
Full
$ time { battery_status=$(< /sys/class/power_supply/BAT0/status); }

real    0m0.103s
user    0m0.001s
sys     0m0.000s
$ echo $battery_status
Discharging

I've also tried just catting the file by itself without using command substitution and the results are still the same.
Any ideas on why this is occurring?

Offline

#2 2025-08-17 01:11:45

Succulent of your garden
Member
From: Majestic kingdom of pot plants
Registered: 2024-02-29
Posts: 855

Re: Reading /sys/class/power_supply/BAT0/status takes a tenth of a second.

Could be many things. Sometimes by hardware if you disconnect the battery charger the system starts to works in a battery saving performance. As you can see here https://wiki.archlinux.org/title/Laptop#Battery_state , here https://wiki.archlinux.org/title/Power_management and here https://wiki.archlinux.org/title/CPU_frequency_scaling you can do some tweakings. 

You can check the arch wiki if your notebook/laptop does have something. For example here is the asus one: https://wiki.archlinux.org/title/Laptop/ASUS where you can see common errors and among other things. Try to find if your device is in the arch wiki.

Maybe if your are using #!/bin/bash instead of #!/bin/sh that does some impact.  To be honest I'm not so sure if you have by default shell bash this would affect or not. But it is a topic where you can earn some improvement. Bash is the slowest shell in Linux. But I don't think this creates your main issue, just a performance tweak that you can do.

If you are interested in only know the percentage of your battery, here is what I do:

#!/bin/sh
output="$(upower -i /org/freedesktop/UPower/devices/battery_BAT1 | grep percentage | awk '{print $2}')"
echo "^c#FFE7C7^ ${output}"

This scripts works among others, which let's me know the current temperature of the system among other things. Does this works faster ? maybe you should change the "battery_BAT1" for the one that your system have. I think sometimes that changes in the path.

Last edited by Succulent of your garden (2025-08-17 01:12:36)


str( @soyg ) == str( @potplant ) btw!

Offline

#3 2025-08-17 09:37:57

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,058

Re: Reading /sys/class/power_supply/BAT0/status takes a tenth of a second.

Especially considering how simple the operation is.

sysfs isn't like cat'ting files, you're polling a device through a kernel module.
Check whether there're dmesg errors but most likely the battery is just slow to respond?

time acpi

Online

Board footer

Powered by FluxBB