"""At what interval does ri_energy_nj actually advance under continuous load? The crate's own liveness criterion: a rail is live when it advanced in >=80% of sampled intervals. Applied here to find the shortest interval that clears it, which is the counter's declarable update period. """ import ctypes, os, time, statistics _l = ctypes.CDLL("/usr/lib/libSystem.B.dylib"); _b = ctypes.create_string_buffer(464); _p = os.getpid() def nj(): _l.proc_pid_rusage(ctypes.c_int(_p), ctypes.c_int(6), ctypes.byref(_b)) return int.from_bytes(_b.raw[336:344], "little") def trial(interval_s, n=60): """Burn for `interval_s` per sample; report the fraction that advanced.""" adv = 0; deltas = [] prev = nj() for _ in range(n): t_end = time.perf_counter() + interval_s x = 0 while time.perf_counter() < t_end: x += 1 # burn this thread's CPU cur = nj() if cur > prev: adv += 1 deltas.append(cur - prev) prev = cur nz = [d for d in deltas if d > 0] return adv / n, (statistics.median(nz)/1e3 if nz else 0.0) print(f"{'interval':>10} {'advanced':>9} {'median ΔµJ':>11} live(>=80%)") for us in (10, 50, 100, 250, 500, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000): frac, med = trial(us/1e6) print(f"{us:>8}µs {frac*100:>8.0f}% {med:>11.1f} {'yes' if frac >= 0.80 else 'no'}")