Repository navigation
Expand file tree
/
Copy pathpower-ab
More file actions
executable file
·324 lines (305 loc) · 14 KB
/
Copy pathpower-ab
File metadata and controls
executable file
·324 lines (305 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# SPDX-License-Identifier: MIT
#
# power-ab: measure battery drain on the phone for two settings, A and B,
# alternated in rounds so slow drift (battery voltage, temperature, a
# background job) cancels out. Runs ON the phone, detached, because the USB
# cable has to be out while it measures (and USB may not come back after an
# unplug until the next boot): the results go to a file on the root
# filesystem.
#
# Usage (on the phone, as root):
# power-ab [options] TEST
#
# TEST:
# baseline A and B identical (shows the noise floor)
# c2 A = C2 idle state disabled on every CPU, B = enabled
# (needs a DTB with the power-c2-* overlay)
# ufs-gate A = UFS clock gating (and so idle Hibern8) off, B = on
# ufs-delay A = Hibern8 after 150 ms idle (kernel default), B = after
# --delay-ms (default 4, the stock Android value)
# s2idle A = awake, B = frozen in s2idle for the whole phase
# (rtcwake -m freeze, or --suspend systemd; frozen again
# after an early wake). Turn
# the screen off after unplugging: both phases assume it off.
# Nothing runs while frozen, so both phases are measured
# from the gauge's charge counter (1 mAh steps: use phases of
# 10 minutes or more); A is also sampled as usual
# units A = the --units system units and --user-units of --user
# stopped, B = started (units not active at the start are
# left alone)
# eas A = energy-aware scheduling off (kernel.sched_energy_aware
# 0), B = on (1). Only meaningful with a CPU energy model
# registered (/sys/kernel/debug/energy_model) and under a
# steady load, such as a video playing, since an idle phone
# barely schedules anything
#
# Options:
# --unplug SECS wait before the first phase, to pull the cable (default 60)
# --phase SECS length of each phase (default 120)
# --settle SECS seconds at the start of each phase not counted (default 20)
# --rounds N A/B rounds (default 4)
# --delay-ms MS B value for ufs-delay (default 4)
# --units "U..." system units for the units test
# --user-units "U..." user units for the units test
# --user NAME whose user units (default: the first logged-in user)
# --detach re-run itself with setsid/nohup and return at once
# --suspend HOW s2idle: "rtcwake" freezes straight through /sys/power/state
# (default); "systemd" arms the RTC and runs systemctl suspend,
# so the system-sleep hooks run (the 9a's NFC hook must)
#
# Output: /var/lib/omamobile/power/<time>-<test>.csv (one sample a second:
# time, phase, voltage_now uV, current_now uA, current_avg uA,
# charge_counter uAh; s2idle's B phase writes one row as it freezes and one
# as it ends) and <time>-<test>.txt (mean current and power per phase, then
# A vs B; for s2idle, the mean current from the charge counter too).
# Every setting the test changes is restored at the end, also on SIGTERM.
#
# See device/cheetah/POWER-PLAN.md, section 4.
set -uo pipefail
unplug=60 phase=120 settle=20 rounds=4 delay_ms=4 detach=0 test= suspend=rtcwake
units= user_units= uname=
usage() { sed -n '/^# Usage/,/^# See /p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-0}"; }
while (($#)); do
case "$1" in
--unplug) unplug="${2:?}"; shift ;;
--suspend) suspend="${2:?}"; shift ;;
--phase) phase="${2:?}"; shift ;;
--settle) settle="${2:?}"; shift ;;
--rounds) rounds="${2:?}"; shift ;;
--delay-ms) delay_ms="${2:?}"; shift ;;
--units) units="${2?}"; shift ;;
--user-units) user_units="${2?}"; shift ;;
--user) uname="${2?}"; shift ;;
--detach) detach=1 ;;
-h|--help) usage ;;
baseline|c2|ufs-gate|ufs-delay|s2idle|units|eas) test="$1" ;;
*) usage 1 ;;
esac
shift
done
[[ -n "$test" ]] || usage 1
[[ $EUID -eq 0 ]] || { echo "power-ab: run as root" >&2; exit 1; }
# The battery: the power supply of type Battery that reports current_now
bat=
for d in /sys/class/power_supply/*; do
[[ "$(cat "$d/type" 2>/dev/null)" == Battery && -r "$d/current_now" ]] && { bat=$d; break; }
done
[[ -n "$bat" ]] || { echo "power-ab: no battery with current_now (is max17042_battery loaded?)" >&2; exit 1; }
ufs=$(ls -d /sys/bus/platform/devices/*.ufs 2>/dev/null | sed -n 1p)
eas=/proc/sys/kernel/sched_energy_aware
idle_states=(/sys/devices/system/cpu/cpu[0-7]/cpuidle/state1/disable)
case "$test" in
c2)
[[ -e "${idle_states[0]}" ]] || { echo "power-ab: no cpuidle state1 (boot a DTB with the C2 overlay)" >&2; exit 1; } ;;
ufs-gate|ufs-delay)
[[ -n "$ufs" && -w "$ufs/clkgate_enable" ]] || { echo "power-ab: no UFS clock gating attributes" >&2; exit 1; } ;;
s2idle)
command -v rtcwake >/dev/null && grep -qw s2idle /sys/power/mem_sleep \
|| { echo "power-ab: needs rtcwake and s2idle" >&2; exit 1; }
case "$suspend" in
rtcwake) ;;
systemd)
# systemctl suspend enters what sleep.conf and mem_sleep resolve to;
# deep never wakes on these phones, so refuse unless that is s2idle
msm=$(systemd-analyze cat-config systemd/sleep.conf 2>/dev/null |
sed -n 's/^MemorySleepMode=//p' | tail -n1)
if [[ -n $msm ]]; then [[ ${msm%% *} == s2idle ]]
else grep -q '\[s2idle\]' /sys/power/mem_sleep; fi ||
{ 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
# 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)
# as root [[ -w ]] only says the file exists; the sysctl takes 0 and 1
# whenever EAS is possible, even with no energy model to engage it
[[ -e "$eas" ]] || { echo "power-ab: no $eas (kernel without EAS)" >&2; exit 1; }
em_domains=()
for f in /sys/kernel/debug/energy_model/cpu*; do
[[ -d $f ]] && em_domains+=("${f##*/}")
done
(( ${#em_domains[@]} )) \
|| { 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=()
for u in $units; do systemctl -q is-active "$u" && usys+=("$u"); done
if [[ -n "$user_units" && -n "$uname" ]]; then
for u in $user_units; do
systemctl --user -M "$uname@.host" -q is-active "$u" 2>/dev/null && uusr+=("$u")
done
fi
units_start() {
((${#usys[@]})) && systemctl start "${usys[@]}"
((${#uusr[@]})) && systemctl --user -M "$uname@.host" start "${uusr[@]}"
}
units_stop() {
((${#uusr[@]})) && systemctl --user -M "$uname@.host" stop "${uusr[@]}"
((${#usys[@]})) && systemctl stop "${usys[@]}"
}
# Save what we may change, restore it on exit
declare -A saved
save() { [[ -r "$1" ]] && saved["$1"]=$(cat "$1"); }
for f in "${idle_states[@]}"; do [[ -e "$f" ]] && save "$f"; done
[[ -n "$ufs" ]] && { save "$ufs/clkgate_enable"; save "$ufs/clkgate_delay_ms"; }
[[ $test == eas ]] && save "$eas"
restore() {
local f
for f in "${!saved[@]}"; do echo "${saved[$f]}" > "$f" 2>/dev/null; done
[[ $test == units ]] && units_start > /dev/null 2>&1
}
trap 'restore; exit 1' TERM INT HUP
trap restore EXIT
set_cfg() { # A|B
local v f
case "$test" in
baseline) : ;;
c2)
[[ $1 == A ]] && v=1 || v=0
for f in "${idle_states[@]}"; do echo "$v" > "$f"; done ;;
ufs-gate)
[[ $1 == A ]] && v=0 || v=1
echo "$v" > "$ufs/clkgate_enable" ;;
ufs-delay)
[[ $1 == A ]] && v=150 || v=$delay_ms
echo 1 > "$ufs/clkgate_enable"
echo "$v" > "$ufs/clkgate_delay_ms" ;;
units)
if [[ $1 == A ]]; then units_stop; else units_start; fi > /dev/null 2>&1 ;;
eas)
[[ $1 == A ]] && v=0 || v=1
echo "$v" > "$eas" 2> /dev/null || echo "# eas: writing $v failed at $(date +%s)" >> "$csv" ;;
esac
}
out=/var/lib/omamobile/power
mkdir -p "$out"
stamp=$(date +%Y%m%d-%H%M%S)
csv="$out/$stamp-$test.csv" txt="$out/$stamp-$test.txt"
{
echo "# power-ab $test: phase ${phase}s settle ${settle}s rounds $rounds unplug ${unplug}s delay_ms $delay_ms suspend $suspend"
echo "# kernel $(uname -r); battery $bat; ufs ${ufs:-none}"
[[ $test == units ]] && echo "# units: ${usys[*]}; user units ($uname): ${uusr[*]}"
[[ $test == eas ]] && echo "# eas: energy model ${em_domains[*]}"
echo "time,phase,voltage_uv,current_ua,current_avg_ua,charge_counter_uah,charge_now_uah"
} > "$csv"
sleep "$unplug"
if grep -qsx 1 /sys/class/power_supply/*/online; then
echo "# warning: a supply reports at start (cable still in?)" >> "$csv"
fi
# Failed sysfs reads (including stale cached telemetry) must not become
# zero-valued samples. Exit through the restoration trap before summarizing.
sample() { # TAG
local name value
local -a values=()
for name in voltage_now current_now current_avg charge_counter charge_now; do
value=
if [[ $name == voltage_now || $name == current_now ]]; then
if ! value=$(cat "$bat/$name" 2>/dev/null) || [[ ! $value =~ ^-?[0-9]+$ ]]; then
sleep 1
if ! value=$(cat "$bat/$name" 2>/dev/null) || [[ ! $value =~ ^-?[0-9]+$ ]]; then
echo "# power-ab: invalid or unavailable $name after retry; stopping" >> "$csv"
echo "power-ab: invalid or unavailable $name after retry; stopping" >&2
exit 1
fi
fi
elif ! value=$(cat "$bat/$name" 2>/dev/null) || [[ ! $value =~ ^-?[0-9]+$ ]]; then
value=
fi
values+=("$value")
done
printf '%s,%s,%s,%s,%s,%s,%s\n' "$(date +%s)" "$1" "${values[@]}" >> "$csv"
}
freeze_phase() { # one B phase of s2idle: frozen until the phase is over
local t0 left
t0=$(date +%s)
sample B-freeze
sync
while left=$(( phase - ($(date +%s) - t0) )); (( left > 5 )); do
if [[ $suspend == systemd ]]; then
# the sleep hooks run before the kernel freezes: with less than 30 s
# left the alarm could fire while still awake, so stay awake instead
if (( left < 30 )); then sleep "$left"; break; fi
# the RTC alarm, then systemd's suspend (hooks included); back when the
# success count moves or the alarm is well past
local s0 t1
s0=$(cat /sys/power/suspend_stats/success 2>/dev/null || echo 0); t1=$(date +%s)
rtcwake -m no -s "$left" > /dev/null 2>&1 || { echo "# rtcwake failed at $(date +%s)" >> "$csv"; sleep 1; continue; }
systemctl suspend > /dev/null 2>&1 || { echo "# systemctl suspend failed at $(date +%s)" >> "$csv"; sleep 5; continue; }
while [[ $(cat /sys/power/suspend_stats/success 2>/dev/null || echo 0) == "$s0" ]] && (( $(date +%s) - t1 < left + 60 )); do sleep 2; done
else
rtcwake -m freeze -s "$left" > /dev/null 2>&1 || { echo "# rtcwake failed at $(date +%s)" >> "$csv"; sleep 1; }
fi
done
sample B-end
}
for ((r = 1; r <= rounds; r++)); do
for p in A B; do
set_cfg "$p"
if [[ $test == s2idle && $p == B ]]; then freeze_phase; sync; continue; fi
t0=$(date +%s)
while (( $(date +%s) - t0 < phase )); do
t=$(( $(date +%s) - t0 ))
tag=$p
(( t < settle )) && tag="${p}-settle"
sample "$tag"
sleep 1
done
sync
done
done
restore
trap - EXIT
# Summary: mean current and power per phase. Discharge current is negative.
awk -F, -v test="$test" '
/^#/ || $1 == "time" { next }
$2 == "A" || $2 == "B" {
n[$2]++; i[$2] += $4; pw[$2] += ($3 / 1e6) * ($4 / 1e6)
}
END {
printf "test %s\n", test
for (p in n)
printf "%s: %d samples, mean current %.1f mA, mean power %.0f mW\n",
p, n[p], i[p] / n[p] / 1000, pw[p] / n[p] * 1000
if (n["A"] && n["B"])
printf "B - A: %+.1f mA, %+.0f mW (positive = B draws less)\n",
(i["B"] / n["B"] - i["A"] / n["A"]) / 1000,
(pw["B"] / n["B"] - pw["A"] / n["A"]) * 1000
}' "$csv" > "$txt"
# From the charge counter (column 6, QH) and, as a cross-check, the
# reported remaining charge (column 7, RepCap, which the gauge's model also
# corrects; the only one a gauge without charge_counter, such as the Pixel
# 9a's, gives): the charge used per phase over its duration. A phase spans its
# first counted sample (after the settle) to its last; B (s2idle) its
# B-freeze row to its B-end row. 1 mAh steps: longer phases, finer numbers.
# (reported charge is the gauge's model-corrected remaining charge: coarser
# than a coulomb counter, and the only sleep figure on a gauge without one)
for col in 6:"charge counter" 7:"reported charge"; do
awk -F, -v c="${col%%:*}" -v name="${col#*:}" '
/^#/ || $1 == "time" || $c == "" { next }
$2 == "A" { if (!inA) { a0t = $1; a0q = $c; inA = 1 } a1t = $1; a1q = $c; next }
inA { qa += a0q - a1q; ta += a1t - a0t; inA = 0 }
$2 == "B-freeze" { b0t = $1; b0q = $c }
$2 == "B-end" { qb += b0q - $c; tb += $1 - b0t }
END {
if (inA) { qa += a0q - a1q; ta += a1t - a0t }
if (ta) printf "A (%s): %.1f mAh over %d s, mean %.1f mA\n", name, qa / 1000, ta, qa / ta * 3.6
if (tb) printf "B (%s): %.1f mAh over %d s, mean %.1f mA\n", name, qb / 1000, tb, qb / tb * 3.6
}' "$csv" >> "$txt"
done
sync