interplane.Get Omega
Read in your language

gpu-head-to-head

aien-dev/gpu-head-to-head

Brought by drakestapleton

branch main Branches Tags History

gpu-head-to-head / bench / quiet.py

71 lines · 2452 bytes Raw Blame History
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
6Exit 0 when, over the last S seconds, no process outside the excluded trees used more than 20% of
7one CPU, and (if L > 0) load1 < L at the end of the window. The nvidia-modeset kernel thread is
8excluded (it always spins one core on this host). One JSON line on stdout: the verdict, load1 and
9the busiest foreign processes, in clock ticks (integers; 20% of S seconds is S * CLK_TCK / 5 ticks).
10"""
11import argparse
12import json
13import os
14import sys
15import time
16
17ap = argparse.ArgumentParser()
18ap.add_argument("--window", type=int, default=60)
19ap.add_argument("--max-load", type=float, default=6.0)
20ap.add_argument("--exclude-tree", type=int, nargs="*", default=[])
21a = ap.parse_args()
22TCK = os.sysconf("SC_CLK_TCK")
23
24
25def 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
40def 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
51s0 = snap()
52time.sleep(a.window)
53s1 = snap()
54mine = tree(s1, a.exclude_tree + [os.getpid()])
55busy, harness = [], []
56for 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})
65load1 = float(open("/proc/loadavg").read().split()[0])
66ok = not busy and (a.max_load <= 0 or load1 < a.max_load)
67busy.sort(key=lambda b: -b["ticks"])
68print(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())}))
71sys.exit(0 if ok else 1)