gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / bench / quiet.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Quiet-host gate (PROTOCOL.md amendment A2). |
| 3 | |
| 4 | quiet.py --window S --max-load L [--exclude-tree PID ...] |
| 5 | |
| 6 | Exit 0 when, over the last S seconds, no process outside the excluded trees used more than 20% of |
| 7 | one CPU, and (if L > 0) load1 < L at the end of the window. The nvidia-modeset kernel thread is |
| 8 | excluded (it always spins one core on this host). One JSON line on stdout: the verdict, load1 and |
| 9 | the busiest foreign processes, in clock ticks (integers; 20% of S seconds is S * CLK_TCK / 5 ticks). |
| 10 | """ |
| 11 | import argparse |
| 12 | import json |
| 13 | import os |
| 14 | import sys |
| 15 | import time |
| 16 | |
| 17 | ap = argparse.ArgumentParser() |
| 18 | ap.add_argument("--window", type=int, default=60) |
| 19 | ap.add_argument("--max-load", type=float, default=6.0) |
| 20 | ap.add_argument("--exclude-tree", type=int, nargs="*", default=[]) |
| 21 | a = ap.parse_args() |
| 22 | TCK = os.sysconf("SC_CLK_TCK") |
| 23 | |
| 24 | |
| 25 | def snap(): |
| 26 | out = {} |
| 27 | for d in os.listdir("/proc"): |
| 28 | if not d.isdigit(): |
| 29 | continue |
| 30 | try: |
| 31 | s = open("/proc/%s/stat" % d).read() |
| 32 | except OSError: |
| 33 | continue |
| 34 | comm = s[s.index("(") + 1:s.rindex(")")] |
| 35 | f = s[s.rindex(")") + 2:].split() |
| 36 | out[int(d)] = (comm, int(f[1]), int(f[11]) + int(f[12])) # comm, ppid, utime+stime |
| 37 | return out |
| 38 | |
| 39 | |
| 40 | def tree(procs, roots): |
| 41 | keep, grew = set(roots), True |
| 42 | while grew: |
| 43 | grew = False |
| 44 | for pid, (_, ppid, _) in procs.items(): |
| 45 | if ppid in keep and pid not in keep: |
| 46 | keep.add(pid) |
| 47 | grew = True |
| 48 | return keep |
| 49 | |
| 50 | |
| 51 | s0 = snap() |
| 52 | time.sleep(a.window) |
| 53 | s1 = snap() |
| 54 | mine = tree(s1, a.exclude_tree + [os.getpid()]) |
| 55 | busy, harness = [], [] |
| 56 | for pid, (comm, _, t1) in s1.items(): |
| 57 | if pid in mine or comm.startswith("nvidia-modeset") or pid not in s0: |
| 58 | continue |
| 59 | d = t1 - s0[pid][2] |
| 60 | if comm == "omp": # the agent harness hosting this lane: recorded, not gated (amendment A2) |
| 61 | harness.append({"pid": pid, "ticks": d}) |
| 62 | continue |
| 63 | if d * 5 > a.window * TCK: |
| 64 | busy.append({"pid": pid, "comm": comm, "ticks": d}) |
| 65 | load1 = float(open("/proc/loadavg").read().split()[0]) |
| 66 | ok = not busy and (a.max_load <= 0 or load1 < a.max_load) |
| 67 | busy.sort(key=lambda b: -b["ticks"]) |
| 68 | print(json.dumps({"quiet": ok, "load1": load1, "window_s": a.window, "clk_tck": TCK, |
| 69 | "busy": busy[:5], "omp_harness": harness, |
| 70 | "utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())})) |
| 71 | sys.exit(0 if ok else 1) |