#!/usr/bin/env python3 """Sample package power with powermetrics (requires sudo) at 1 s intervals into a JSONL file; a companion function integrates energy over a benchmark's wall-clock window to produce joules per token. Usage: sudo powermetrics --samplers cpu_power,gpu_power,thermal -i 1000 -f plist | python3 power_sampler.py --out power.jsonl """ import argparse, json, plistlib, sys, time def main(): ap = argparse.ArgumentParser(); ap.add_argument("--out", required=True); a = ap.parse_args() buf = b""; out = open(a.out, "a") for chunk in iter(lambda: sys.stdin.buffer.read(65536), b""): buf += chunk while b"" in buf: i = buf.index(b"") + len(b"") doc, buf = buf[:i], buf[i:] s = doc.find(b" lo: e += (pa + pb) / 2 * 1e-3 * (hi - lo) return e if __name__ == "__main__": main()