This repository was archived by the owner on Nov 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpower_ab.py
More file actions
executable file
·291 lines (254 loc) · 11.8 KB
/
Copy pathpower_ab.py
File metadata and controls
executable file
·291 lines (254 loc) · 11.8 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
#!/usr/bin/python3
"""Alternating A/B power trials on the SMC "Total System Power" rail, with honest verdicts.
power_ab.py observe --seconds 120 one arm, conditions recorded, no change made
power_ab.py ab --label X --a-cmd CMD --b-cmd CMD pairs of arms in AB, BA, AB... order
[--pairs 3] [--seconds 120] [--settle 20] [--interval 1] [--allow-ac] [--out DIR]
power_ab.py summarize RESULT.json re-print the verdict of a saved run
Arm A is the baseline, arm B the candidate; `--a-cmd` / `--b-cmd` put the machine into that state (for
example `mseries-power-tune revert X` / `mseries-power-tune apply X`). Nothing here changes display,
audio or kernel state by itself; commands are the caller's responsibility.
Verdict rules (mseries/POWER-ROADMAP.md honesty rules):
* at least 3 pairs; every pair is (A, B) measured back to back, order alternating;
* conditions must match across arms: same power source, display connectors, backlight level, and
arm CPU-busy medians within 5 points; on AC the run is only accepted with --allow-ac and is labelled;
* an effect is reported only if every paired delta has the same sign AND |median delta| exceeds the
noise floor, which is the spread of the A-arm medians across pairs (repeatability of the baseline)
and at least 0.25 W. Otherwise the result is "inconclusive", with the reasons.
Stdlib only; the sysfs/proc roots are overridable (MSERIES_SYS, MSERIES_PROC) for tests.
"""
import argparse
import json
import os
import statistics
import subprocess
import sys
import time
from pathlib import Path
SYS = Path(os.environ.get('MSERIES_SYS', '/sys'))
PROC = Path(os.environ.get('MSERIES_PROC', '/proc'))
MIN_PAIRS = 3
MIN_EFFECT_W = 0.25
MAX_BUSY_GAP = 5.0
def read(path, default=None):
try:
return Path(path).read_text().strip()
except OSError:
return default
def power_input():
"""Path of the SMC total-system power sensor (microwatts)."""
for hw in sorted((SYS / 'class/hwmon').glob('hwmon*')):
if read(hw / 'name') != 'macsmc_hwmon':
continue
for label in sorted(hw.glob('power*_label')):
if read(label) == 'Total System Power':
return hw / label.name.replace('_label', '_input')
raise SystemExit('no SMC "Total System Power" sensor found under %s' % SYS)
def cpu_times():
fields = [int(x) for x in read(PROC / 'stat').splitlines()[0].split()[1:9]]
return sum(fields), fields[3] + fields[4]
def quantile(values, q):
s = sorted(values)
if not s:
return None
pos = (len(s) - 1) * q
lo = int(pos)
hi = min(lo + 1, len(s) - 1)
return s[lo] + (s[hi] - s[lo]) * (pos - lo)
def top_processes(seconds=0.0, limit=6):
"""CPU% per process over a short window (one core = 100), for recording, never for killing."""
hz = os.sysconf('SC_CLK_TCK')
def snap():
out = {}
for p in PROC.iterdir():
if not p.name.isdigit():
continue
try:
comm = (p / 'comm').read_text().strip()
f = (p / 'stat').read_text().rsplit(')', 1)[1].split()
out[p.name] = (comm, int(f[11]) + int(f[12]))
except (OSError, IndexError, ValueError):
pass
return out
a = snap()
time.sleep(seconds)
b = snap()
rows = []
for pid, (comm, t) in b.items():
if pid in a and seconds > 0:
rows.append((round((t - a[pid][1]) / hz / seconds * 100, 1), comm, int(pid)))
rows.sort(reverse=True)
return [{'cpu': c, 'comm': n, 'pid': p} for c, n, p in rows[:limit]]
def conditions():
"""Read-only snapshot of state that changes power: source, battery, display connectors, backlight."""
ps = SYS / 'class/power_supply'
drm = {}
for status in sorted((SYS / 'class/drm').glob('card*-*/status')):
drm[status.parent.name] = read(status)
backlight = {}
for bl in sorted((SYS / 'class/backlight').glob('*')):
backlight[bl.name] = read(bl / 'brightness')
load = read(PROC / 'loadavg', '').split()[:3]
return {
'ac_online': read(ps / 'macsmc-ac/online'),
'battery_status': read(ps / 'macsmc-battery/status'),
'battery_capacity': read(ps / 'macsmc-battery/capacity'),
'battery_energy_now': read(ps / 'macsmc-battery/energy_now'),
'drm_connected': sorted(k for k, v in drm.items() if v == 'connected'),
'backlight': backlight,
'loadavg': load,
}
def sample(seconds, interval=1.0):
sensor = power_input()
t0, i0 = cpu_times()
watts = []
n = max(1, int(round(seconds / interval)))
for _ in range(n):
time.sleep(interval)
watts.append(int(read(sensor)) / 1e6)
t1, i1 = cpu_times()
busy = 100.0 * (1 - (i1 - i0) / (t1 - t0)) if t1 > t0 else 0.0
return watts, busy
def arm_summary(watts, busy):
return {
'n': len(watts),
'median': round(statistics.median(watts), 3),
'mean': round(statistics.fmean(watts), 3),
'p10': round(quantile(watts, 0.10), 3),
'p90': round(quantile(watts, 0.90), 3),
'min': round(min(watts), 3),
'max': round(max(watts), 3),
'cpu_busy': round(busy, 1),
}
def run_arm(name, cmd, seconds, settle, interval):
if cmd:
subprocess.run(cmd, shell=True, check=True)
time.sleep(settle)
before = conditions()
watts, busy = sample(seconds, interval)
after = conditions()
return {'arm': name, 'cmd': cmd, 'started': before, 'ended': after,
'top': top_processes(min(2.0, interval * 2)), **arm_summary(watts, busy), 'samples': watts}
def display_key(c):
return (tuple(c['drm_connected']), tuple(sorted(c['backlight'].items())))
def verdict(result, allow_ac=False):
"""Return (verdict string, reasons, stats). Pure function of a saved result."""
pairs = result['pairs']
reasons = []
arms = [a for p in pairs for a in (p['A'], p['B'])]
deltas = [p['B']['median'] - p['A']['median'] for p in pairs]
a_meds = [p['A']['median'] for p in pairs]
b_meds = [p['B']['median'] for p in pairs]
stats = {
'pairs': len(pairs),
'A_median_of_medians': round(statistics.median(a_meds), 3) if a_meds else None,
'B_median_of_medians': round(statistics.median(b_meds), 3) if b_meds else None,
'paired_deltas_B_minus_A': [round(d, 3) for d in deltas],
'median_delta': round(statistics.median(deltas), 3) if deltas else None,
'A_repeat_spread': round(max(a_meds) - min(a_meds), 3) if a_meds else None,
}
if len(pairs) < MIN_PAIRS:
reasons.append('only %d pair(s); need %d' % (len(pairs), MIN_PAIRS))
sources = {(a['started']['ac_online'], a['ended']['ac_online']) for a in arms}
if len({s for pair in sources for s in pair}) > 1:
reasons.append('power source changed during the run')
on_ac = any(a['started']['ac_online'] == '1' for a in arms)
if on_ac and not allow_ac:
reasons.append('on AC power: battery drain not measured; rerun on battery or pass --allow-ac')
keys = {display_key(a['started']) for a in arms} | {display_key(a['ended']) for a in arms}
if len(keys) > 1:
reasons.append('display connectors or backlight changed between arms')
busy = [a['cpu_busy'] for a in arms]
if busy and max(busy) - min(busy) > MAX_BUSY_GAP:
reasons.append('CPU busy differs %.1f-%.1f%% across arms (workload not matched)' % (min(busy), max(busy)))
if deltas:
same_sign = all(d > 0 for d in deltas) or all(d < 0 for d in deltas)
floor = max(MIN_EFFECT_W, stats['A_repeat_spread'] or 0)
stats['noise_floor_w'] = round(floor, 3)
if not same_sign:
reasons.append('paired deltas disagree in sign')
if abs(stats['median_delta']) <= floor:
reasons.append('median delta %.2f W within noise floor %.2f W' % (stats['median_delta'], floor))
if reasons:
return 'inconclusive', reasons, stats
label = 'saving measured' if stats['median_delta'] < 0 else 'cost measured'
if on_ac:
label += ' (on AC, system rail only)'
return label, [], stats
def fmt_arm(a):
return '%s: median %.2f W (p10 %.2f, p90 %.2f, n=%d), CPU %.1f%%' % (
a['arm'], a['median'], a['p10'], a['p90'], a['n'], a['cpu_busy'])
def print_result(result, allow_ac):
v, reasons, stats = verdict(result, allow_ac)
print('label: %s' % result['label'])
for i, p in enumerate(result['pairs'], 1):
print('pair %d (%s): %s | %s' % (i, p['order'], fmt_arm(p['A']), fmt_arm(p['B'])))
print(json.dumps(stats))
print('verdict: %s' % v)
for r in reasons:
print(' - %s' % r)
return v
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest='cmd', required=True)
ob = sub.add_parser('observe')
ob.add_argument('--seconds', type=float, default=120)
ob.add_argument('--interval', type=float, default=1.0)
ob.add_argument('--out')
ab = sub.add_parser('ab')
ab.add_argument('--label', required=True)
ab.add_argument('--a-cmd', default='')
ab.add_argument('--b-cmd', default='')
ab.add_argument('--pairs', type=int, default=3)
ab.add_argument('--seconds', type=float, default=120)
ab.add_argument('--settle', type=float, default=20)
ab.add_argument('--interval', type=float, default=1.0)
ab.add_argument('--allow-ac', action='store_true')
ab.add_argument('--out')
ab.add_argument('--end-on-b', action='store_true', help='do not return to arm A after the last pair')
sm = sub.add_parser('summarize')
sm.add_argument('file')
sm.add_argument('--allow-ac', action='store_true')
args = ap.parse_args(argv)
if args.cmd == 'summarize':
result = json.loads(Path(args.file).read_text())
print_result(result, args.allow_ac or result.get('allow_ac', False))
return 0
if args.cmd == 'observe':
arm = run_arm('observe', '', args.seconds, 0, args.interval)
print(fmt_arm(arm))
print(json.dumps({k: arm['started'][k] for k in ('ac_online', 'battery_status', 'drm_connected', 'loadavg')}))
print('top: ' + ', '.join('%s %.1f%%' % (t['comm'], t['cpu']) for t in arm['top']))
if args.out:
Path(args.out).write_text(json.dumps(arm, indent=1))
return 0
pairs = []
for i in range(args.pairs):
order = 'AB' if i % 2 == 0 else 'BA'
got = {}
for name in order:
cmd = args.a_cmd if name == 'A' else args.b_cmd
try:
got[name] = run_arm(name, cmd, args.seconds, args.settle, args.interval)
except subprocess.CalledProcessError as e:
print('arm %s command failed (%s); returning to arm A and stopping' % (name, e), flush=True)
if args.a_cmd and name == 'B':
subprocess.run(args.a_cmd, shell=True)
return 2
print('pair %d %s' % (i + 1, fmt_arm(got[name])), flush=True)
pairs.append({'order': order, 'A': got['A'], 'B': got['B']})
result = {'label': args.label, 'allow_ac': args.allow_ac, 'when': time.strftime('%Y-%m-%dT%H:%M:%S%z'),
'kernel': os.uname().release, 'seconds': args.seconds, 'settle': args.settle, 'pairs': pairs}
if args.out:
out = Path(args.out)
out.mkdir(parents=True, exist_ok=True)
path = out / ('%s-%s.json' % (args.label, time.strftime('%Y%m%d-%H%M%S')))
path.write_text(json.dumps(result, indent=1))
print('saved %s' % path)
print_result(result, args.allow_ac)
# Leave the machine in the baseline (A) state: an odd number of pairs ends on arm B.
if args.a_cmd and pairs and pairs[-1]['order'] == 'AB' and not args.end_on_b:
subprocess.run(args.a_cmd, shell=True, check=True)
print('restored arm A state')
return 0
if __name__ == '__main__':
sys.exit(main())