Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions tools/power-ab
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ done
[[ -n "$test" ]] || usage 1
[[ $EUID -eq 0 ]] || { echo "power-ab: run as root" >&2; exit 1; }

if ((detach)); then
args=(--unplug "$unplug" --phase "$phase" --settle "$settle" --rounds "$rounds" --delay-ms "$delay_ms"
--units "$units" --user-units "$user_units" --user "$uname" --suspend "$suspend" "$test")
setsid nohup "$0" "${args[@]}" > /dev/null 2>&1 < /dev/null &
echo "power-ab: started (pid $!); unplug within ${unplug} s"
exit 0
fi

# The battery: the power supply of type Battery that reports current_now
bat=
for d in /sys/class/power_supply/*; do
Expand Down Expand Up @@ -124,8 +116,11 @@ case "$test" in
{ echo "power-ab: systemctl suspend would not enter s2idle here" >&2; exit 1; } ;;
*) echo "power-ab: --suspend takes rtcwake or systemd, not '$suspend'" >&2; exit 1 ;;
esac
[[ -r "$bat/charge_counter" || -r "$bat/charge_now" ]] \
|| { echo "power-ab: the gauge has neither charge_counter nor charge_now" >&2; exit 1; } ;;
# Read one, not just test for the file: a gauge that withholds its charge
# (the 9a's MAX77779 after a power-on reset answers ENODATA) has readable
# files and no figure, and the sleep phase's only number comes from them
cat "$bat/charge_counter" >/dev/null 2>&1 || cat "$bat/charge_now" >/dev/null 2>&1 \
|| { echo "power-ab: the gauge gives neither charge_counter nor charge_now (a file that can't be read, or no data after a gauge reset)" >&2; exit 1; } ;;
units)
[[ -n "$units$user_units" ]] || { echo "power-ab: units needs --units or --user-units" >&2; exit 1; } ;;
eas)
Expand All @@ -140,6 +135,16 @@ case "$test" in
|| { echo "power-ab: no CPU energy model in /sys/kernel/debug/energy_model: EAS can't engage" >&2; exit 1; } ;;
esac

# Detach only after the checks above: a detached run has no terminal, so a
# refusal there would go nowhere and the owner would unplug for nothing
if ((detach)); then
args=(--unplug "$unplug" --phase "$phase" --settle "$settle" --rounds "$rounds" --delay-ms "$delay_ms"
--units "$units" --user-units "$user_units" --user "$uname" --suspend "$suspend" "$test")
setsid nohup "$0" "${args[@]}" > /dev/null 2>&1 < /dev/null &
echo "power-ab: started (pid $!); unplug within ${unplug} s"
exit 0
fi

# The units test touches only units that are active now
[[ -z "$uname" ]] && uname=$(loginctl list-users --no-legend 2>/dev/null | awk '$2 != "root" {print $2; exit}')
usys=() uusr=()
Expand Down