testsAndMisc/linux_configuration/i3-configuration/i3blocks/battery_status.sh
Krzysztof kuhy Rudnicki c8c727e9d5 i3blocks: eliminate fork-storm with persist mode + zero-fork sysfs reads
Resource-usage report showed ~29 cores of average load coming from i3blocks
helper scripts forking awk/tr/grep/bc/sensors/nvidia-smi every tick. Rewrite
all five hot-path scripts to eliminate forks:

- volume.sh: persist mode, blocks on 'pactl subscribe' event stream.
  No polling, no sleep, no fork per tick.
- gpu_monitor.sh: persist mode, single long-lived 'nvidia-smi --loop=5'
  feeds a bash 'while read' loop. Falls back to /sys for amdgpu.
- battery_status.sh: reads /sys/class/power_supply/BAT*/ directly.
  Zero forks; replaces 'acpi | awk' pipeline.
- cpu_monitor.sh: reads /proc/loadavg and k10temp/coretemp /sys/class/hwmon.
  Zero forks; replaces 'sensors | awk | tr' + bc arithmetic.
- motherboard_temp.sh: reads nct*/it*/f71* Super-I/O hwmon node directly.
  Zero forks.

Configure volume + gpu_monitor with interval=persist so i3blocks keeps
one long-lived producer each instead of forking per tick.

Also add:
- kill_stale_recorders.sh -- kill stray ffmpeg x11grab / dotnet-trace /
  dotnet-monitor processes left running after sessions.
- monitors.slice -- resource-capped user slice (CPUQuota=50%,
  MemoryMax=512M, MemorySwapMax=0 for zram safety, TasksMax=256) to
  bound future monitoring regressions.
- efficient-polling-scripts SKILL -- rules for writing status-bar and
  polling scripts without forks; fork-pipeline to bash-builtin translation
  table; verification checklist.

Verified live: strace -c on cpu_monitor.sh shows 1 execve / 0 clones;
persist producers (pactl subscribe, nvidia-smi --loop) show 0 CPU ticks
over a 3s idle sample. Per-invocation timing 1.6-1.9 ms (was 30-80 ms).
2026-04-20 21:54:29 +02:00

50 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# i3blocks battery indicator, zero-fork per invocation.
#
# Reads /sys/class/power_supply directly instead of forking `acpi | awk`.
# Uses only bash builtins (read, printf, arithmetic, parameter expansion).
set -u
bat=
for d in /sys/class/power_supply/BAT*/; do
[[ -d $d ]] && {
bat=$d
break
}
done
if [[ -z $bat ]]; then
# Desktop with no battery — emit empty block so i3bar hides it.
echo
exit 0
fi
cap='N/A'
[[ -r ${bat}capacity ]] && read -r cap < "${bat}capacity"
status=''
[[ -r ${bat}status ]] && read -r status < "${bat}status"
# Compute time remaining from energy_now/power_now (µWh / µW → hours).
# Falls back to charge_now/current_now on batteries that expose charge instead.
time_str=''
num=0
den=0
if [[ -r ${bat}energy_now && -r ${bat}power_now ]]; then
read -r num < "${bat}energy_now"
read -r den < "${bat}power_now"
elif [[ -r ${bat}charge_now && -r ${bat}current_now ]]; then
read -r num < "${bat}charge_now"
read -r den < "${bat}current_now"
fi
if ((den > 0 && num > 0)); then
total_min=$((num * 60 / den))
printf -v time_str '%02d:%02d' "$((total_min / 60))" "$((total_min % 60))"
fi
printf ' %s%%' "$cap"
[[ -n $time_str ]] && printf ', %s' "$time_str"
[[ $status == Charging ]] && printf ', '
printf '\n'