Skip to content

Instantly share code, notes, and snippets.

@kelvie
Created January 4, 2024 00:33
Show Gist options
  • Save kelvie/1d3e08c253fb8c4b650aa559108537b1 to your computer and use it in GitHub Desktop.
Save kelvie/1d3e08c253fb8c4b650aa559108537b1 to your computer and use it in GitHub Desktop.
compare rapl and battery charge info on Framework AMD 13 (2023 edition)
#!/bin/bash
#
# Comparing the two sources of energy usage on a framework 13 AMD edition
# tempdir=$(mktemp -d)
# cleanup() {
# rm -rf "$tempdir"
# }
# trap cleanup EXIT
energyfile=/sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj
batterychargefile=/sys/class/power_supply/BAT1/charge_now
# check permissions
if [[ ! -r $energyfile ]]; then
echo "Cannot read $energyfile (maybe run as root)" >&2
exit 1
fi
if [[ ! -r $batterychargefile ]]; then
echo "Cannot read $batterychargefile (maybe run as root)" >&2
exit 1
fi
voltageminfile=$(dirname "$batterychargefile")/voltage_min_design
if [[ ! -r $voltageminfile ]]; then
echo "Cannot read $voltageminfile (maybe run as root)" >&2
exit 1
fi
bat_uvolts=$(cat "$voltageminfile")
last_energy=
last_batterycharge=
battery_seconds_since_change=1
# calculate watts from joules
while true; do
ts=$(date +%s)
energy=$(cat "$energyfile")
if [[ -n $last_energy ]]; then
# convert to watts
watts=$(echo "scale=2; ($energy - $last_energy) / 1000000" | bc)
echo "$ts rapl: $watts W"
fi
batterycharge=$(cat "$batterychargefile")
if [[ -n $last_batterycharge ]]; then
# battery charge is in uah
# bat_volts is in uv
# therefore watts = (uah * uv) * 3600 / 1000000000000
uah_change=$(echo " ($batterycharge - $last_batterycharge)" | bc)
watts=$(echo "scale=2; ($uah_change) * $bat_uvolts *3600 / 1000000000000 / $battery_seconds_since_change" | bc)
if [[ $uah_change -ne 0 ]]; then
battery_seconds_since_change=1
echo "$ts battery: $watts W"
else
battery_seconds_since_change=$((battery_seconds_since_change + 1))
fi
fi
last_energy=$energy
last_batterycharge=$batterycharge
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment