#!/usr/bin/env python3"""One process's CPU, wakeups and energy, a line a second, for scripts/perf(run-simulator.sh). Reads proc_pid_rusage, which any process may ask of itsown user's processes. `top` and `ps` are setuid root, and a lane's seatbeltrefuses to run a setuid binary at all, so a lane measuring the fan got no CPUfigure from them (fanperf2, 15 Oct 2026). usage.py <pid> [interval s] # until the process endsEach line: wall s, CPU % of one core over the interval, package idle wakeups/s, interrupt wakeups /s, the energy the process was charged in mW over theinterval (ri_energy_nj)."""import ctypesimport sysimport time_U64 = ctypes.c_uint64classRusageInfoV6(ctypes.Structure): _fields_ = [('ri_uuid', ctypes.c_uint8 * 16)] + [(name, _U64) for name in ('ri_user_time', 'ri_system_time', 'ri_pkg_idle_wkups', 'ri_interrupt_wkups', 'ri_pageins','ri_wired_size', 'ri_resident_size', 'ri_phys_footprint', 'ri_proc_start_abstime','ri_proc_exit_abstime', 'ri_child_user_time', 'ri_child_system_time','ri_child_pkg_idle_wkups', 'ri_child_interrupt_wkups', 'ri_child_pageins','ri_child_elapsed_abstime', 'ri_diskio_bytesread', 'ri_diskio_byteswritten','ri_cpu_time_qos_default', 'ri_cpu_time_qos_maintenance', 'ri_cpu_time_qos_background','ri_cpu_time_qos_utility', 'ri_cpu_time_qos_legacy', 'ri_cpu_time_qos_user_initiated','ri_cpu_time_qos_user_interactive', 'ri_billed_system_time', 'ri_serviced_system_time','ri_logical_writes', 'ri_lifetime_max_phys_footprint', 'ri_instructions', 'ri_cycles','ri_billed_energy', 'ri_serviced_energy', 'ri_interval_max_phys_footprint','ri_runnable_time', 'ri_flags', 'ri_user_ptime', 'ri_system_ptime', 'ri_pinstructions','ri_pcycles', 'ri_energy_nj', 'ri_penergy_nj', 'ri_secure_time_in_system','ri_secure_ptime_in_system', 'ri_neural_footprint', 'ri_lifetime_max_neural_footprint','ri_interval_max_neural_footprint')] + [('ri_reserved', _U64 * 9)]classTimebase(ctypes.Structure): _fields_ = [('numer', ctypes.c_uint32), ('denom', ctypes.c_uint32)]_libc = ctypes.CDLL('/usr/lib/libSystem.B.dylib', use_errno=True)_RUSAGE_INFO_V6 = 6_timebase = Timebase()_libc.mach_timebase_info(ctypes.byref(_timebase))def read(pid):"""(cpu ns, idle wakeups, interrupt wakeups, energy nJ), or None once the process is gone.""" info = RusageInfoV6()if _libc.proc_pid_rusage(pid, _RUSAGE_INFO_V6, ctypes.byref(info)) != 0:returnNone# The CPU times are in mach absolute units. cpu = (info.ri_user_time + info.ri_system_time) * _timebase.numer / _timebase.denomreturn cpu, info.ri_pkg_idle_wkups, info.ri_interrupt_wkups, info.ri_energy_njdef main(): pid = int(sys.argv[1]) interval = float(sys.argv[2]) if len(sys.argv) > 2else1.0 last, last_at = read(pid), time.time()while last isnotNone: time.sleep(interval) now, at = read(pid), time.time()if now isNone:break span = at - last_at print(f'{at:.3f} {(now[0] - last[0]) / 1e9 / span * 100:.2f} {(now[1] - last[1]) / span:.1f} ' f'{(now[2] - last[2]) / span:.1f} {(now[3] - last[3]) / 1e6 / span:.2f}', flush=True) last, last_at = now, atif __name__ == '__main__': main()