Files
alzeo/dashboard/tests/test_boot_watch.py
T
Hasan BasbunarandClaude Opus 5.5 7ecfd60e1a inkling: what the starts of its installed unit measured
The three starts of its unit on the reference box took 163, 179 and 226 s, where the campaign's containers took 127 to 142
and the seven starts before this version's flags took 229 to 302: the pages, the summary and the cockpit (which expects 180 s)
give the unit's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 21:10:27 +01:00

745 lines
45 KiB
Python

#!/usr/bin/env python3
"""A start, watched: the pure rules of lifecycle.py that say how long it takes, whether it moves, why it may not.
The numbers are the reference box's own: its boot history (cockpit-history.json, 2026-11-30), its telemetry, a line
every 2 s from 2026-10-31 on, and the starts of 2026-11-29 under NVIDIA drivers 615.71.09 and 595.99.02.
"""
import csv
import re
import sys
import time
import unittest
from pathlib import Path
HERE = Path(__file__).resolve()
REPO = HERE.parents[2]
FIX = HERE.parent / "fixtures"
sys.path.insert(0, str(HERE.parents[1]))
import lifecycle as lc # noqa: E402
INKLING = "qwen38-inkling.service"
# what the reference box had recorded on 2026-11-30 (cockpit-history.json), by unit and target
HISTORY = {
"qwen38-inkling.service:boot:inkling": [377.0, 232.9, 231.1, 232.0, 231.9],
"qwen38-sglang.service:boot:stock": [217.5, 216.0, 227.1, 229.8, 208.0, 209.8, 210.6, 211.6, 213.5, 214.6, 209.3, 210.8],
"qwen38-flash.service:boot:flash": [744.0, 963.5, 1469.6, 741.1, 733.7, 896.3, 856.4, 744.7, 766.3, 836.3, 770.6, 943.4],
"qwen38-image.service:boot:image": [72.4, 171.3, 87.0, 75.7, 73.5, 74.1, 89.8, 81.3, 125.2, 191.8, 79.7, 117.8],
"qwen38-lfm3.service:boot:lfm3": [98.3, 95.9, 117.6, 100.2, 92.4, 95.1, 103.1, 95.5, 98.5, 96.5, 95.1, 171.9],
"qwen38-flash-unsloth.service:boot:flash-unsloth": [48.8, 49.5, 49.1, 50.0, 49.5, 48.5, 165.7, 215.3, 167.3, 49.2, 49.2, 50.5],
"qwen38-ornith-122b.service:boot:ornith-122b": [359.1, 151.3, 150.9, 151.2, 151.2, 150.5, 150.9, 150.3, 151.8, 152.9, 151.2, 154.1],
"qwen38-transcribe.service:boot:transcribe": [21.9, 22.1, 21.8, 21.8],
"qwen38-sglang.service:boot:fp8": [489.9, 493.5],
"qwen38-flash-local.service:boot:flash-local": [336.8, 348.3, 338.0, 337.0, 339.8, 341.9, 341.5, 342.0, 339.4, 391.7, 329.2, 384.1],
"qwen38-lfm25.service:boot:lfm25": [293.3, 292.0, 307.2],
}
def usual(unit, target):
return lc.boot_usual(HISTORY, unit, target)
class WhatAStartUsuallyTakes(unittest.TestCase):
def test_it_is_the_median_of_the_starts_recorded_here_and_how_many(self):
self.assertEqual((usual(INKLING, "inkling")["s"], usual(INKLING, "inkling")["n"]), (232.0, 5))
u = usual("qwen38-sglang.service", "stock")
self.assertEqual((round(u["s"], 2), u["n"]), (212.55, 12))
def test_one_start_that_went_wrong_does_not_hide_the_next(self):
# 377 s, its first start, and 171.9 s under driver 615.71.09 are the slowest of their series
self.assertEqual(usual(INKLING, "inkling")["long"], 290.0)
self.assertEqual(usual("qwen38-lfm3.service", "lfm3")["long"], 129.4)
self.assertLess(usual("qwen38-lfm3.service", "lfm3")["long"], 171.9)
self.assertEqual(usual("qwen38-ornith-122b.service", "ornith-122b")["long"], 189.0)
def test_the_spread_of_a_lane_counts(self):
# the flash lane: every start but the slowest, a tenth over; the image lane's 171 s was no accident
self.assertEqual(usual("qwen38-flash.service", "flash")["long"], 1059.9)
self.assertEqual(usual("qwen38-image.service", "image")["long"], 188.4)
# a GGUF read from the disk instead of the page cache takes three times as long, as it did twice
self.assertEqual(usual("qwen38-flash-unsloth.service", "flash-unsloth")["long"], 184.0)
def test_a_steady_lane_gets_a_quarter_and_a_short_one_half_a_minute(self):
self.assertEqual(usual("qwen38-sglang.service", "stock")["long"], 265.7)
self.assertEqual(usual("qwen38-transcribe.service", "transcribe")["long"], 51.9)
self.assertEqual(usual("qwen38-sglang.service", "fp8")["long"], 614.6, "two starts: none is set aside")
def test_a_box_that_recorded_none_gets_the_reference_box_and_twice_that(self):
self.assertEqual(lc.boot_usual({}, INKLING, "inkling"), {"s": 180.0, "n": 0, "long": 360.0})
self.assertEqual(lc.boot_usual({}, "qwen38-new.service"), {"s": 600.0, "n": 0, "long": 1200.0})
# a 15 s lane whose 36.5 GB come from a cold disk the first time
self.assertEqual(lc.boot_usual({}, "qwen38-transcribe.service"), {"s": 15.0, "n": 0, "long": 135.0})
def test_the_units_own_series_speaks_for_a_target_that_has_none(self):
self.assertEqual(lc.boot_usual({INKLING: [100.0, 100.0]}, INKLING, "another"), {"s": 100.0, "n": 2, "long": 130.0})
def test_it_is_never_under_the_median_nor_under_a_start_that_is_not_the_slowest(self):
for key, vals in HISTORY.items():
unit, target = key.split(":boot:")
u = usual(unit, target)
self.assertGreater(u["long"], u["s"], key)
self.assertGreater(u["long"], sorted(vals)[-2], key)
def test_what_is_no_number_in_a_history_is_left_out(self):
self.assertEqual(lc.boot_usual({INKLING: [100.0, None, "x", {"s": 1}, 100.0]}, INKLING)["n"], 2)
def test_the_pages_table_and_the_servers_are_one(self):
base = (REPO / "dashboard/static/js/base.js").read_text()
const = dict(re.findall(r"(\w+) = '(qwen38-[\w-]+\.service)'", base))
line = re.search(r"const READY_DEFAULT = \{(.*?)\};", base).group(1)
page = {const[name]: float(v) for name, v in re.findall(r"\[(\w+)\]: (\d+)", line)}
# a lane beside every lane is none of the GPU's turn: no boot of it is watched, and its Load's words alone use its time
beside = {const[n] for n in re.search(r"const BESIDE_UNITS = new Set\(\[(.*?)\]\)", base).group(1).split(", ")}
self.assertEqual(beside, {"qwen38-embed.service", "qwen38-clipstral.service"})
self.assertLess(beside, set(page))
self.assertEqual({unit: s for unit, s in page.items() if unit not in beside}, lc.REFERENCE_BOOT_S)
self.assertEqual(set(lc.REFERENCE_BOOT_S), set(lc.ENGINE_UNITS))
# each start of the replay: its lane and target
REPLAY = {"inkling-615-1": (INKLING, "inkling"), "inkling-615-2": (INKLING, "inkling"), "inkling-595-low": (INKLING, "inkling"),
"inkling-595": (INKLING, "inkling"), "inkling-1130": (INKLING, "inkling"),
"lfm3-615": ("qwen38-lfm3.service", "lfm3"), "lfm3-1130": ("qwen38-lfm3.service", "lfm3"),
"27b-1130": ("qwen38-sglang.service", "stock"), "flash-local-1130": ("qwen38-flash-local.service", "flash-local"),
"flash-unsloth-1130": ("qwen38-flash-unsloth.service", "flash-unsloth"),
"lfm25-1130": ("qwen38-lfm25.service", "lfm25")}
class TheReferenceBoxsStartsAreReplayed(unittest.TestCase):
"""Eleven starts of the reference box, a row every 4 s (fixtures/boot-replay.csv): the two of Inkling under driver
615.71.09 that never got past their weights and LFM3's that took 171 s for 97; Inkling's under 595.99.02 with the
clock still held, stopped by hand, and after the power cut, which served; and the six lanes started under 595 on
2026-11-30, all normal. Each row: the stage its log proved, how many lines its log had, the box's available memory,
the GPU's power, clock, use and limits. The lane's own disk and memory were not recorded then: they are taken as
still, the worst case for a false alarm (and the truth of the two that were: the box lost 1.2 GiB in 384 s)."""
@classmethod
def setUpClass(cls):
cls.rows = {}
with open(FIX / "boot-replay.csv", newline="") as f:
for r in csv.DictReader(f):
cls.rows.setdefault(r["boot"], []).append(r)
def replay(self, name, **still):
"""[(seconds, state, level, held)] of one start."""
unit, target = REPLAY[name]
use, watch, reads, out, busy_at = usual(unit, target), {}, [], [], None
for r in self.rows[name]:
t = float(r["s"])
reads.append({"t": t, "mhz": int(r["mhz"]), "max_mhz": 3003, "watts": float(r["watts"]), "use": int(r["use"]),
"reasons": int(r["mask"])})
busy = "loading" if r["state"] != "starting" else None
busy_at = (busy_at if busy_at is not None else t) if busy else None
held = lc.gpu_held(reads, t, busy=busy, since=busy_at)
sample = {"t": t, "avail": int(r["avail_mib"]) * 2 ** 20, "read": 0, "written": 0, "own": 0, "gpu_mem": 0,
"use": int(r["use"]), "watts": float(r["watts"]), "line": int(r["lines"]), **still}
w = lc.boot_watch(watch, sample, state=r["state"], elapsed=t, usual=use, fault=bool(held))
out.append((t, r["state"], w["level"], held))
return out
def first(self, out, pred):
return next((t for t, state, level, held in out if pred(level, held)), None)
def weights_at(self, out):
return next(t for t, state, _, _ in out if state == "loading-weights")
def test_the_starts_that_never_got_past_their_weights_are_said_within_a_minute_of_them(self):
for name in ("inkling-615-1", "inkling-615-2"):
with self.subTest(start=name):
out = self.replay(name)
w = self.weights_at(out)
said = self.first(out, lambda level, held: held)
still = self.first(out, lambda level, held: level == "still")
self.assertLessEqual(said - w, 40, "its clock is held: said in tens of seconds")
self.assertLessEqual(still - w, 72, "it barely moves")
tail = [level for t, _, level, _ in out if t >= still]
self.assertEqual(set(tail), {"still"}, "once said, it stays said while nothing moves")
held = out[-1][3]
self.assertEqual((held["mhz"], held["reasons"]), (942, 0x400))
def test_with_no_fault_beside_it_a_still_start_is_said_past_its_usual_time(self):
# the same rows on a GPU whose clock reads high: what moves is the same, nothing
unit, target = REPLAY["inkling-615-1"]
use, watch, out = usual(unit, target), {}, []
for r in self.rows["inkling-615-1"]:
t = float(r["s"])
sample = {"t": t, "avail": int(r["avail_mib"]) * 2 ** 20, "read": 0, "written": 0, "own": 0, "gpu_mem": 0,
"use": int(r["use"]), "watts": float(r["watts"]), "line": int(r["lines"])}
out.append((t, lc.boot_watch(watch, sample, state=r["state"], elapsed=t, usual=use)["level"]))
still = next(t for t, level in out if level == "still")
self.assertGreater(still, use["long"])
self.assertLess(still, use["long"] + 5)
self.assertNotIn("long", [level for t, level in out if t > still])
def test_a_clock_held_low_is_said_of_a_start_that_moves_and_no_more(self):
for name in ("inkling-595-low", "lfm3-615"):
with self.subTest(start=name):
out = self.replay(name)
self.assertIsNotNone(self.first(out, lambda level, held: held))
self.assertIsNone(self.first(out, lambda level, held: level == "still"))
# under 595 the driver gave no reason for the same 942 MHz
self.assertEqual(self.replay("inkling-595-low")[-1][3]["reasons"], 0)
# LFM3 took 171 s where 97 is usual: long, from 129 s on
out = self.replay("lfm3-615")
self.assertEqual(self.first(out, lambda level, held: level == "long"), 132.0)
def test_the_seven_normal_starts_say_nothing_at_all(self):
for name in ("inkling-595", "27b-1130", "inkling-1130", "flash-local-1130", "flash-unsloth-1130", "lfm25-1130",
"lfm3-1130"):
with self.subTest(start=name):
out = self.replay(name)
self.assertGreater(len(out), 10)
self.assertEqual({level for _, _, level, _ in out}, {"normal"})
self.assertEqual([held for _, _, _, held in out if held], [])
def test_what_was_not_read_is_never_taken_for_still(self):
for gone in ("read", "own", "gpu_mem", "avail", "use", "watts", "line"):
with self.subTest(unread=gone):
out = self.replay("inkling-615-1", **{gone: None})
self.assertIsNone(self.first(out, lambda level, held: level == "still"), gone)
def test_a_lane_whose_own_bytes_move_is_not_still(self):
# 168 MB/s of the lane's own memory, as the 27B's loader takes it, the box's memory and its log standing still
unit, target = REPLAY["inkling-615-1"]
for key in ("read", "written", "own", "gpu_mem"):
watch, levels = {}, set()
for r in self.rows["inkling-615-1"]:
t = float(r["s"])
sample = {"t": t, "avail": 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0, "use": 1, "watts": 5.0, "line": 1}
sample[key] = int(168e6 * t)
levels.add(lc.boot_watch(watch, sample, state="loading-weights", elapsed=t, usual=usual(unit, target),
fault=True)["level"])
self.assertNotIn("still", levels, key)
def test_a_silent_compile_a_capture_a_warmup_and_a_held_start_are_never_still(self):
unit, target = REPLAY["inkling-615-1"]
for state, held in (("starting", False), ("allocating-kv", False), ("capturing-graphs", False),
("warming-up", False), ("loading-weights", True)):
watch, levels = {}, []
for t in range(0, 1200, 4):
sample = {"t": float(t), "avail": 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0, "use": 0, "watts": 5.0,
"line": 1}
levels.append(lc.boot_watch(watch, sample, state=state, elapsed=float(t), usual=usual(unit, target),
fault=True, held=held)["level"])
self.assertNotIn("still", levels, state)
self.assertEqual("long" in levels, not held, state)
def test_once_still_another_programs_memory_does_not_unsay_it(self):
watch, levels = {}, []
for t in range(0, 240, 4):
sample = {"t": float(t), "avail": 5 * 2**30 if t > 120 else 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0,
"use": 0, "watts": 5.0, "line": 1}
levels.append((t, lc.boot_watch(watch, sample, state="loading-weights", elapsed=float(t), fault=True,
usual=usual(INKLING, "inkling"))["level"]))
self.assertEqual({x for t, x in levels if t >= 64}, {"still"})
sample["line"] = 2
self.assertEqual(lc.boot_watch(watch, {**sample, "t": 240.0}, state="loading-weights", elapsed=240.0, fault=True,
usual=usual(INKLING, "inkling"))["level"], "normal", "a line of its log does")
def test_a_cockpit_that_stood_still_proves_nothing_of_the_lane(self):
watch, levels = {}, []
for t in list(range(0, 40, 2)) + list(range(90, 130, 2)): # no read for 50 s
sample = {"t": float(t), "avail": 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0, "use": 0, "watts": 5.0, "line": 1}
levels.append(lc.boot_watch(watch, sample, state="loading-weights", elapsed=float(t), fault=True,
usual=usual(INKLING, "inkling"))["level"])
self.assertEqual(set(levels), {"normal"})
class WhatMoves(unittest.TestCase):
def test_the_lanes_counters_that_went_back_are_unread(self):
s = {"t": 0.0, "avail": 0, "read": 10, "written": 0, "own": 0, "gpu_mem": 0, "use": 0, "watts": 5.0, "line": 1}
self.assertIsNone(lc.boot_moved([s, {**s, "t": 2.0, "read": 0}])["disk"])
def test_one_read_of_a_working_gpu_is_work_even_among_reads_that_failed(self):
s = {"t": 0.0, "avail": 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0, "use": None, "watts": None, "line": 1}
self.assertEqual(lc.boot_moved([s, {**s, "watts": 60.0}])["gpu"], "work")
self.assertEqual(lc.boot_moved([s, {**s, "use": 96}])["gpu"], "work")
self.assertIsNone(lc.boot_moved([s, {**s, "use": 0, "watts": 4.0}])["gpu"])
self.assertEqual(lc.boot_moved([]), {"disk": None, "own": None, "gpu_mem": None, "box": None, "gpu": None, "line": None})
def test_the_rates_are_the_lanes_own_and_the_boxs_when_unknown(self):
mk = lambda t, own, avail: {"t": float(t), "read": 5 * 10**8 + 155 * 10**6 * t, "written": 0, "own": own, "avail": avail}
out = lc.boot_rates([mk(t, 168 * 10**6 * t, 10**11) for t in range(0, 30, 2)], 28.0)
self.assertAlmostEqual(out["read"], 155e6)
self.assertAlmostEqual(out["memory"], 168e6)
# a loader fills a shard's buffer and gives it back: what it gained, not what it has
saw = [mk(t, 168 * 10**6 * (t % 10), 0) for t in range(0, 30, 2)]
self.assertGreater(lc.boot_rates(saw, 28.0)["memory"], 1e8)
out = lc.boot_rates([mk(t, None, 10**11 - 3 * 10**6 * t) for t in range(0, 30, 2)], 28.0)
self.assertAlmostEqual(out["memory"], 3e6)
self.assertEqual(lc.boot_rates([mk(0, 0, 0)], 0.0), {"read": None, "memory": None})
self.assertIsNone(lc.boot_rates([mk(t, None, None) for t in range(0, 30, 2)], 28.0)["memory"])
def test_the_pace_is_the_whole_checkpoints_at_the_faster_of_the_two(self):
self.assertEqual(lc.boot_pace({"read": 0.0, "memory": 3.2e6}, 20_400_000_000), 6375)
self.assertEqual(lc.boot_pace({"read": 155e6, "memory": 10e6}, 15_500_000_000), 100)
for rates, size in (({"read": None, "memory": None}, 10**10), ({"read": 0, "memory": 4e5}, 10**10),
({"read": 1e8, "memory": None}, None), ({"read": 1e8, "memory": None}, 0)):
self.assertIsNone(lc.boot_pace(rates, size))
def test_the_last_line_that_says_something(self):
lines = ["[2026-11-29 02:34:53 PID135] Found local HF snapshot for x", "\x1b[32m\x1b[0m", " ", "=====",
'[2026-11-29 17:39:03] INFO: 127.0.0.1:58454 - "GET /health HTTP/1.1" 503 Service Unavailable']
self.assertEqual(lc.last_log_line(lines), lines[0])
self.assertEqual(lc.last_log_line(["shards: 10%\rshards: 40% | 36/90"]), "shards: 40% | 36/90")
self.assertEqual(lc.last_log_line(["\x1b[2mLoading transformer\x1b[0m"]), "Loading transformer")
self.assertEqual(lc.last_log_line([]), "")
self.assertEqual(len(lc.last_log_line(["x" * 900])), 200)
class TheGpusClock(unittest.TestCase):
def test_each_bit_has_nvmls_name_and_one_with_none_its_number(self):
self.assertEqual(lc.clock_reasons(0x400), ["reliability policy (0x400)"])
self.assertEqual(lc.clock_reasons(0x200), ["board limit policy (0x200)"])
self.assertEqual(lc.clock_reasons(0x68), ["hardware slowdown", "software thermal slowdown", "hardware thermal slowdown"])
self.assertEqual(lc.clock_reasons(0x804), ["software power cap", "a reason with no name known (0x800)"])
for none in (0, None, -1, "0x400"):
self.assertEqual(lc.clock_reasons(none), [])
def test_at_rest_idle_and_the_power_cap_are_no_limit(self):
self.assertEqual(lc.clock_limits(0x5, True), [])
self.assertEqual(lc.clock_limits(0x4, False), ["software power cap"])
self.assertEqual(lc.clock_limits(0x400, True), ["reliability policy (0x400)"], "615 gave it at rest too, all day")
self.assertEqual(lc.clock_limits(None, True), [])
def reads(self, **kw):
return [{"t": float(t), "mhz": 942, "max_mhz": 3003, "watts": 5.0, "use": 1, "reasons": 0x400, **kw} for t in range(0, 60, 3)]
def test_held_low_while_a_lane_loads(self):
self.assertEqual(lc.gpu_held(self.reads(), 57.0, busy="loading", since=20.0),
{"mhz": 942, "max_mhz": 3003, "watts": 5.0, "reasons": 0x400})
self.assertIsNone(lc.gpu_held(self.reads(), 57.0, busy=None, since=0.0))
self.assertIsNone(lc.gpu_held(self.reads(), 57.0, busy="loading", since=40.0), "17 s in: not yet")
self.assertIsNone(lc.gpu_held(self.reads(mhz=None), 57.0, busy="loading", since=0.0))
self.assertIsNone(lc.gpu_held(self.reads(mhz=1989), 57.0, busy="loading", since=0.0), "the cap of 2,000 MHz")
one_high = self.reads()
one_high[-4]["mhz"] = 2411
self.assertIsNone(lc.gpu_held(one_high, 57.0, busy="loading", since=0.0))
self.assertIsNotNone(lc.gpu_held(self.reads(max_mhz=None), 57.0, busy="loading", since=0.0))
self.assertIsNone(lc.gpu_held(self.reads()[:2], 57.0, busy="loading", since=0.0), "the last read is 54 s old")
def test_a_lane_that_serves_counts_only_at_work(self):
# LFM3 under 615 (2026-11-29 03:46:05, 2 min 38): 910 to 929 MHz at 7 to 17 W
self.assertIsNotNone(lc.gpu_held(self.reads(mhz=929, watts=8.2, use=96), 57.0, busy="serving", since=0.0))
self.assertIsNone(lc.gpu_held(self.reads(mhz=208, watts=4.0, use=0), 57.0, busy="serving", since=0.0), "at rest")
self.assertIsNone(lc.gpu_held(self.reads(mhz=208, watts=4.0, use=96), 57.0, busy="serving", since=0.0),
"nvidia-smi's frozen use")
class WhyAndWhatToDo(unittest.TestCase):
HELD = {"mhz": 942, "max_mhz": 3003, "watts": 5.1, "reasons": 0x400}
LAST = {"driver": "580.178.04", "kernel": "6.17.0-1032-nvidia", "day": "2026-11-24"}
def causes(self, **kw):
base = dict(held=None, driver="595.99.02", kernel="6.17.0-1032-nvidia", last=None, smi=None, foreign=[], xid=None,
refused=0, disk=None)
return lc.boot_causes(**{**base, **kw})
def test_the_615_case_whole(self):
out = self.causes(held=self.HELD, driver="615.71.09", last=self.LAST)
self.assertEqual(out, ["The GPU's clock is held at 942 MHz of 3,003, 5 W, while this lane loads; the driver says "
"what limits it: reliability policy (0x400).",
"The NVIDIA driver is 615.71.09; this lane last started under 580.178.04, on 2026-11-24."])
todo = lc.boot_todo(held=self.HELD, driver="615.71.09", kernel=self.LAST["kernel"], last=self.LAST)
self.assertEqual(len(todo), 3)
self.assertIn("go back to NVIDIA driver 580.178.04: the one thing read here that changed", todo[1])
self.assertIn("cut its power for a minute", todo[2])
def test_a_driver_and_a_kernel_that_both_changed_are_not_one_change(self):
out = self.causes(driver="615.71.09", kernel="7.0.0-1024-nvidia", last=self.LAST)
self.assertEqual(len(out), 2)
todo = lc.boot_todo(held=None, driver="615.71.09", kernel="7.0.0-1024-nvidia", last=self.LAST)
self.assertIn("it and the kernel are what changed", todo[1])
def test_the_rest_one_fact_each(self):
out = self.causes(held={"mhz": 942, "max_mhz": None, "watts": None, "reasons": 0}, xid={"n": 2, "last": "79, GPU has fallen off the bus"},
refused=1, smi="Failed to initialize NVML: Driver/library version mismatch",
foreign=[{"name": "python3", "pid": 4242, "bytes": 24e9}],
disk={"rate": 3.2e6, "read": 1.2e9, "size": 20.4e9})
self.assertEqual(out, ["The GPU's clock is held at 942 MHz while this lane loads; the driver gives no reason.",
"The GPU driver logged 2 Xid errors since this start, the last: 79, GPU has fallen off the bus.",
"The GPU driver refused 1 allocation since this start (NV_ERR_NO_MEMORY in the kernel log).",
"python3 (pid 4242) holds 24.0 GB of GPU memory and is not this lane's.",
"nvidia-smi does not answer: Failed to initialize NVML: Driver/library version mismatch.",
"The disk gives it 3.2 MB/s: 1.2 GB read of a checkpoint of 20.4 GB."])
def test_nothing_unlike_a_start_that_served_says_nothing(self):
self.assertEqual(self.causes(), [])
self.assertEqual(self.causes(driver=None, kernel=None, last=self.LAST), [])
self.assertEqual(self.causes(driver="580.178.04", last=self.LAST), [])
self.assertEqual(len(lc.boot_todo(held=None, driver="595.99.02", kernel=None, last=None)), 1)
def test_durations_in_words(self):
self.assertEqual([lc.dur_words(s) for s in (41, 89.6, 453, 3600, 4000)], ["41 s", "1 min 30 s", "7 min 33 s", "1 h 00", "1 h 06"])
class TheBoundsOfLongerThanUsual(unittest.TestCase):
"""The rule, at its edges, on series made for it."""
def of(self, *vals):
return lc.boot_usual({"u": list(vals)}, "u")["long"]
def test_the_slowest_is_set_aside_from_four_starts_on(self):
self.assertEqual(self.of(100, 100, 1000), 1100.0, "of three, none is")
self.assertEqual(self.of(100, 100, 100, 1000), 130.0)
self.assertEqual(self.of(100, 100, 100, 100, 100, 200, 300), 220.0, "the second slowest counts, a tenth over")
def test_a_quarter_more_or_half_a_minute_whichever_is_longer(self):
self.assertEqual(self.of(400, 400), 500.0)
self.assertEqual(self.of(20, 20), 50.0)
self.assertEqual(self.of(120, 120), 150.0, "where the two meet")
def test_with_none_recorded_it_is_twice_the_reference_and_two_minutes_at_the_least(self):
for unit, s in lc.REFERENCE_BOOT_S.items():
u = lc.boot_usual({}, unit)
self.assertEqual((u["s"], u["n"]), (s, 0))
self.assertEqual(u["long"], 2 * s if s >= 120 else s + 120, unit)
class AStartsTraceInTheHistory(unittest.TestCase):
U = "qwen38-inkling.service"
def test_a_run_is_recorded_once_and_the_newest_twelve_are_kept(self):
h = {}
for k in range(15):
lc.record_start(h, self.U, {"run": f"r{k}", "at": 1000 + k})
lc.record_start(h, self.U, {"run": f"r{k}", "at": 9000}) # the same activation met again
kept = h[f"{self.U}:starts"]
self.assertEqual([x["run"] for x in kept], [f"r{k}" for k in range(3, 15)])
self.assertEqual(kept[-1]["at"], 1014, "what it said first stays")
self.assertEqual(lc.last_start(h, self.U)["run"], "r14")
def test_the_newest_six_cuts_are_kept(self):
h = {}
for k in range(9):
lc.record_cut(h, self.U, {"run": f"r{k}", "at": k})
self.assertEqual([x["run"] for x in h[f"{self.U}:cuts"]], [f"r{k}" for k in range(3, 9)])
self.assertEqual(lc.last_cut(h, self.U)["run"], "r8")
self.assertEqual((lc.last_cut({}, self.U), lc.last_start({}, self.U)), (None, None))
def test_its_day_is_the_boxs(self):
at = time.mktime((2026, 11, 24, 16, 45, 0, 0, 0, -1))
self.assertEqual(lc.last_start({f"{self.U}:starts": [{"run": "a", "at": at}]}, self.U)["day"], "2026-11-24")
def test_only_a_dated_entry_counts(self):
junk = [None, 5, "x", [], {"run": "no date"}, {"run": "words", "at": "12"}, {"run": "ok", "at": 7.5}]
self.assertEqual(lc.last_start({f"{self.U}:starts": junk}, self.U)["run"], "ok")
for other in ({"at": 1}, "x", 3, None):
self.assertIsNone(lc.last_cut({f"{self.U}:cuts": other}, self.U))
def since(self, start, cut):
h = {}
if start:
h[f"{self.U}:starts"] = [start]
if cut:
h[f"{self.U}:cuts"] = [cut]
return lc.cut_since(h, self.U)
def test_the_last_start_is_the_one_that_ended_last(self):
cut = {"run": "c", "at": 200, "end": 300, "s": 100, "stage": "loading-weights"}
said = "stopped after 1 min 40 s while loading its weights"
self.assertEqual(self.since(None, cut), said)
self.assertIsNone(self.since({"run": "s", "at": 100, "end": 400}, cut), "it served after that one was cut")
self.assertIsNone(self.since({"run": "s", "at": 250, "end": 300}, cut))
self.assertEqual(self.since({"run": "s", "at": 100, "end": 299}, cut), said)
# without an end (an entry of another hand), when it began
self.assertEqual(self.since({"run": "s", "at": 199}, {k: v for k, v in cut.items() if k != "end"}), said)
self.assertIsNone(self.since({"run": "s", "at": 200}, {k: v for k, v in cut.items() if k != "end"}))
self.assertIsNone(self.since({"run": "s", "at": 1}, None))
def test_its_words(self):
self.assertEqual(lc.cut_words({"how": "failed", "s": 22, "stage": "capturing-graphs", "driver": "595.99.02"}),
"failed after 22 s while capturing its CUDA graphs, NVIDIA driver 595.99.02")
self.assertEqual(lc.cut_words({"s": 453, "stage": "loading-weights", "still": True}),
"stopped after 7 min 33 s while loading its weights, barely moving")
self.assertEqual(lc.cut_words({"stage": "a stage of tomorrow"}), "stopped after 0 s while starting")
self.assertEqual(set(lc.STATE_WORDS), set(lc.TRANSITIONAL) - {"stopping"}, "every state a start goes through has its words")
class TheWindowsEdges(unittest.TestCase):
def at(self, *times):
return [{"t": float(t)} for t in times]
def test_it_begins_at_the_last_read_as_old_as_its_span(self):
s = self.at(0, 15, 30, 40, 50, 60)
self.assertEqual([x["t"] for x in lc.boot_window(s, 60.0, 30.0)], [30.0, 40.0, 50.0, 60.0], "exactly as old counts")
self.assertEqual([x["t"] for x in lc.boot_window(s, 61.0, 30.0)], [30.0, 40.0, 50.0, 60.0])
self.assertEqual([x["t"] for x in lc.boot_window(s, 60.0, 60.0)][0], 0.0)
self.assertEqual(lc.boot_window(s[1:], 60.0, 60.0), [], "15 s short of it")
self.assertEqual(lc.boot_window([], 60.0, 60.0), [])
self.assertEqual([x["t"] for x in lc.boot_window(self.at(0, 10), 30.0, 20.0)], [10.0], "the newest of all may be it")
def test_a_hole_of_twenty_seconds_is_bridged_and_a_longer_one_is_not(self):
hole = lc.STILL_HOLE_S
self.assertEqual(len(lc.boot_window(self.at(0, hole, 2 * hole), 2 * hole, 30.0)), 3)
self.assertEqual(lc.boot_window(self.at(0, hole + 1, 2 * hole), 2 * hole, 40.0), [])
# and since the last read
self.assertEqual(len(lc.boot_window(self.at(0, 10, 20), 20 + hole, 30.0)), 2)
self.assertEqual(lc.boot_window(self.at(0, 10, 20), 21 + hole, 30.0), [])
class WhatCountsAsMoving(unittest.TestCase):
S = {"t": 0.0, "avail": 10, "read": 100, "written": 50, "own": 7, "gpu_mem": 3, "use": 0, "watts": 5.0, "line": "a"}
def moved(self, *changes):
return lc.boot_moved([{**self.S, **c} for c in changes])
def test_the_disk_is_what_was_read_and_written_between_the_two_ends(self):
self.assertEqual(self.moved({}, {"read": 130, "written": 54})["disk"], 34)
self.assertEqual(self.moved({}, {"read": 500}, {"read": 100, "written": 51})["disk"], 1)
self.assertEqual(self.moved({}, {})["disk"], 0)
self.assertIsNone(self.moved({}, {"read": 99})["disk"])
for end in (0, 1):
for key in ("read", "written"):
c = [{}, {}]
c[end] = {key: None}
self.assertIsNone(self.moved(*c)["disk"], (end, key))
def test_memory_is_the_most_it_moved_and_nothing_when_a_read_lacks_it(self):
for key, name in (("own", "own"), ("gpu_mem", "gpu_mem"), ("avail", "box")):
self.assertEqual(self.moved({key: 5}, {key: 90}, {key: 20})[name], 85)
self.assertIsNone(self.moved({key: 5}, {key: None}, {key: 20})[name])
def test_the_gpu_works_over_ten_percent_or_from_its_rest_watts(self):
self.assertEqual(self.moved({"use": lc.STILL_USE_PCT}, {"watts": lc.STILL_W - 0.1})["gpu"], "rest")
self.assertEqual(self.moved({}, {"use": lc.STILL_USE_PCT + 1})["gpu"], "work")
self.assertEqual(self.moved({}, {"watts": lc.STILL_W})["gpu"], "work")
self.assertIsNone(self.moved({}, {"use": None})["gpu"])
self.assertIsNone(self.moved({}, {"watts": None})["gpu"])
def test_a_line_that_came_and_went_changed(self):
self.assertFalse(self.moved({}, {})["line"])
self.assertTrue(self.moved({}, {"line": "b"}, {})["line"])
self.assertIsNone(self.moved({"line": None}, {"line": "b"})["line"])
STILL = {"disk": 0, "own": 0, "gpu_mem": 0, "box": 0, "gpu": "rest", "line": False}
def test_still_is_under_each_bound_and_all_read(self):
self.assertTrue(lc.boot_still(self.STILL))
for key in ("disk", "own", "gpu_mem"):
self.assertTrue(lc.boot_still({**self.STILL, key: lc.STILL_B - 1}), key)
self.assertFalse(lc.boot_still({**self.STILL, key: lc.STILL_B}), key)
self.assertTrue(lc.boot_still({**self.STILL, "box": lc.STILL_BOX_B - 1}))
self.assertFalse(lc.boot_still({**self.STILL, "box": lc.STILL_BOX_B}))
self.assertTrue(lc.boot_still({**self.STILL, "box": lc.STILL_BOX_B}, box=False), "another program's memory")
for key in self.STILL:
self.assertFalse(lc.boot_still({**self.STILL, key: None}), f"{key} unread")
self.assertFalse(lc.boot_still({**self.STILL, key: None}, box=False), f"{key} unread")
for key, value in (("gpu", "work"), ("line", True)):
self.assertFalse(lc.boot_still({**self.STILL, key: value}))
def test_the_gpu_at_rest_is_the_panels_own_bound(self):
self.assertEqual((lc.STILL_USE_PCT, lc.STILL_W), (10, 25.0))
def test_the_bounds_are_four_mebibytes_a_second_over_a_minute_and_a_gibibyte_of_the_box(self):
self.assertEqual((lc.STILL_S, lc.STILL_B // 2**20, lc.STILL_BOX_B, lc.STILL_HOLE_S), (60.0, 256, 2**30, 20.0))
self.assertEqual(round(lc.STILL_B / lc.STILL_S / 2**20, 2), 4.27)
class TheRates(unittest.TestCase):
def row(self, t, **kw):
return {"t": float(t), "read": 0, "own": 0, "avail": 0, **kw}
def test_over_the_last_twenty_seconds_alone(self):
rows = [self.row(t, read=10**9 * t if t <= 20 else 10**9 * 20 + 10**6 * (t - 20), own=0) for t in range(0, 41, 4)]
self.assertEqual(lc.boot_rates(rows, 40.0)["read"], 1e6, "not the 1 GB/s of before")
def test_none_before_they_cover_it_and_from_counters_that_went_back(self):
self.assertEqual(lc.boot_rates([self.row(0)], 25.0), {"read": None, "memory": None})
self.assertEqual(lc.boot_rates([self.row(10), self.row(20)], 20.0)["read"], None, "ten seconds of reads")
self.assertEqual(lc.boot_rates([self.row(5, read=9), self.row(5, read=10)], 25.0), {"read": None, "memory": None})
for between in (50, 150):
rows = [self.row(0, read=100), self.row(10, read=between), self.row(20, read=99)]
self.assertIsNone(lc.boot_rates(rows, 20.0)["read"])
self.assertEqual(lc.boot_rates([self.row(0, read=100), self.row(20, read=100)], 20.0)["read"], 0.0)
for end in (0, 1):
rows = [self.row(0), self.row(20)]
rows[end]["read"] = None
self.assertIsNone(lc.boot_rates(rows, 20.0)["read"])
def test_memory_is_what_the_lane_gained_then_what_the_box_lost(self):
own = [0, 100, 40, 90] # gained 100, gave back 60, gained 50
rows = [self.row(t, own=o, avail=-999 * t) for t, o in zip((0, 10, 15, 20), own)]
self.assertEqual(lc.boot_rates(rows, 20.0)["memory"], 150 / 20)
rows[2]["own"] = None
self.assertEqual(lc.boot_rates(rows, 20.0)["memory"], 999.0, "its own unknown in one read: the box's")
rows[1]["avail"] = None
self.assertIsNone(lc.boot_rates(rows, 20.0)["memory"])
def test_the_pace_begins_at_half_a_megabyte_a_second(self):
self.assertEqual(lc.boot_pace({"read": 5e5}, 10**9), 2000)
self.assertIsNone(lc.boot_pace({"read": 4.99e5}, 10**9))
self.assertEqual(lc.boot_pace({"read": None, "memory": 1e6}, 10**9), 1000)
self.assertIsNone(lc.boot_pace({}, 10**9))
class TheWatchAtItsEdges(unittest.TestCase):
USUAL = {"s": 232.0, "n": 5, "long": 290.0}
def sample(self, t, **kw):
return {"t": float(t), "avail": 0, "read": 0, "written": 0, "own": 0, "gpu_mem": 0, "use": 0, "watts": 5.0, "line": 1, **kw}
def go(self, watch, t, state="loading-weights", elapsed=None, fault=True, **kw):
return lc.boot_watch(watch, self.sample(t, **kw), state=state, elapsed=elapsed, usual=self.USUAL, fault=fault)
def test_still_is_said_once_a_minute_is_covered_and_counts_from_its_beginning(self):
watch, said = {}, {}
for t in range(100, 200, 2):
said[t] = self.go(watch, t)
self.assertEqual((said[158]["level"], said[158]["still_s"]), ("normal", None))
self.assertEqual((said[160]["level"], said[160]["still_s"]), ("still", 60))
self.assertEqual(said[190]["still_s"], 90)
def test_a_new_stage_starts_again(self):
watch = {}
for t in range(0, 70, 2):
self.go(watch, t)
self.assertEqual(self.go(watch, 70)["level"], "still")
out = self.go(watch, 72, state="loading-draft")
self.assertEqual((out["level"], out["still_s"]), ("normal", None))
self.assertEqual(len(watch["samples"]), 1, "what did not move before says nothing of the draft")
for t in range(74, 132, 2):
self.assertEqual(self.go(watch, t, state="loading-draft")["level"], "normal")
self.assertEqual(self.go(watch, 132, state="loading-draft")["level"], "still")
def test_long_is_past_its_usual_not_at_it(self):
for elapsed, level in ((290.0, "normal"), (290.1, "long"), (None, "normal")):
self.assertEqual(self.go({}, 0, state="capturing-graphs", elapsed=elapsed)["level"], level)
held = lc.boot_watch({}, self.sample(0), state="starting", elapsed=9999.0, usual=self.USUAL, held=True)
self.assertEqual(held["level"], "normal", "the guard's wait is said as one")
def test_still_needs_a_fault_or_its_usual_time_past(self):
for fault, elapsed, level in ((False, 289.0, "normal"), (True, 10.0, "still"), (False, 291.0, "still")):
watch = {}
for t in range(0, 62, 2):
out = self.go(watch, t, fault=fault, elapsed=elapsed)
self.assertEqual(out["level"], level, (fault, elapsed))
def test_what_moved_is_said_of_the_weights_alone(self):
for state, want in (("loading-weights", 0), ("loading-draft", 0), ("capturing-graphs", None)):
watch = {}
for t in range(0, 62, 2):
out = self.go(watch, t, state=state)
self.assertEqual(out["moved"]["own"], want, state)
def test_the_reads_kept_cover_the_window_and_no_more(self):
watch = {}
for t in range(0, 600, 2):
self.go(watch, t)
times = [s["t"] for s in watch["samples"]]
self.assertLessEqual(times[0], 598 - lc.STILL_S)
self.assertGreaterEqual(times[0], 598 - lc.STILL_S - lc.STILL_HOLE_S - 2, "and none of its first minutes")
self.assertGreaterEqual(times[1], 598 - lc.STILL_S - lc.STILL_HOLE_S)
# two reads far apart: both are kept, the older one is what the window begins at
watch = {}
for t in (0, 1000):
self.go(watch, t)
self.assertEqual(len(watch["samples"]), 2)
watch = {}
for t in (0, 5, 1000):
self.go(watch, t)
self.assertEqual([s["t"] for s in watch["samples"]], [5.0, 1000.0])
class TheClockAtItsEdges(unittest.TestCase):
def reads(self, n=12, **kw):
return [{"t": 3.0 * k, "mhz": 942, "max_mhz": 3000, "watts": 5.0, "use": 1, "reasons": 0, **kw} for k in range(n)]
def held(self, reads, busy="loading", since=0.0, now=33.0):
return lc.gpu_held(reads, now, busy=busy, since=since)
def test_half_a_minute_of_it_and_no_less(self):
self.assertIsNotNone(self.held(self.reads(), since=3.0), "thirty seconds in")
self.assertIsNone(self.held(self.reads(), since=3.1))
self.assertIsNone(self.held(self.reads(), since=None))
def test_two_reads_that_cover_it_are_enough(self):
two = [self.reads(1)[0], {**self.reads(1)[0], "t": 15.0}]
self.assertIsNotNone(self.held(two, now=30.0))
self.assertIsNone(self.held(two[:1], now=30.0), "one is not")
def test_forty_percent_of_its_maximum_or_1200_mhz_with_none(self):
self.assertIsNotNone(self.held(self.reads(mhz=1199)))
self.assertIsNone(self.held(self.reads(mhz=1200)), "40 % of 3,000")
self.assertIsNotNone(self.held(self.reads(mhz=1199, max_mhz=None)))
self.assertIsNone(self.held(self.reads(mhz=1200, max_mhz=None)))
self.assertIsNotNone(self.held(self.reads(mhz=1300, max_mhz=4000)), "40 % of its own maximum")
def test_serving_counts_at_half_its_use_and_seven_watts(self):
self.assertIsNotNone(self.held(self.reads(use=50, watts=7.0), busy="serving"))
for low in ({"use": 49, "watts": 7.0}, {"use": 50, "watts": 6.9}, {"use": None, "watts": 9.0}, {"use": 96, "watts": None}):
self.assertIsNone(self.held(self.reads(**low), busy="serving"), low)
self.assertIsNotNone(self.held(self.reads(use=0, watts=4.0)), "a lane that loads holds its GPU at rest")
def test_what_it_says(self):
reads = self.reads()
reads[-1].update(mhz=936, watts=9.5, reasons=0x400, max_mhz=None)
reads[-2].update(reasons=0x20, watts=None)
self.assertEqual(self.held(reads), {"mhz": 942, "max_mhz": 3000, "watts": 9.5, "reasons": 0x420})
self.assertIsNone(self.held(self.reads(watts=None))["watts"])
self.assertEqual(self.held(self.reads(reasons=None))["reasons"], 0)
def test_each_bit_by_its_name(self):
names = ["idle", "applications clocks setting", "software power cap", "hardware slowdown", "sync boost",
"software thermal slowdown", "hardware thermal slowdown", "hardware power brake", "display clock setting",
"board limit policy (0x200)", "reliability policy (0x400)"]
for k, name in enumerate(names):
self.assertEqual(lc.clock_reasons(1 << k), [name])
self.assertEqual(lc.clock_reasons((1 << 11) - 1), names)
self.assertEqual(lc.clock_reasons(0x1800), ["a reason with no name known (0x1800)"])
self.assertEqual(lc.clock_limits(0x7, True), ["applications clocks setting"], "idle and the power cap, at rest only")
self.assertEqual(lc.clock_limits(0x5, False), ["idle", "software power cap"])
class TheWordsAtTheirEdges(unittest.TestCase):
def test_durations(self):
for s, said in ((-5, "0 s"), (0, "0 s"), (89, "89 s"), (89.4, "89 s"), (90, "1 min 30 s"), (119.6, "2 min 00 s"),
(3599, "59 min 59 s"), (3600, "1 h 00"), (3661, "1 h 01"), (7140, "1 h 59"), (7325, "2 h 02")):
self.assertEqual(lc.dur_words(s), said, s)
def test_the_clock_held_in_a_sentence(self):
h = {"mhz": 1011, "max_mhz": 3003, "watts": 5.6, "reasons": 0x420}
self.assertEqual(lc.held_words(h, "this lane loads"), "The GPU's clock is held at 1,011 MHz of 3,003, 6 W, while this lane "
"loads; the driver says what limits it: software thermal slowdown, reliability policy (0x400).")
def causes(self, **kw):
base = dict(held=None, driver=None, kernel=None, last=None, smi=None, foreign=[], xid=None, refused=0, disk=None)
return lc.boot_causes(**{**base, **kw})
def test_one_and_several(self):
self.assertIn("logged 1 Xid error since", self.causes(xid={"n": 1, "last": "x"})[0])
self.assertIn("logged 2 Xid errors since", self.causes(xid={"n": 2, "last": "x"})[0])
self.assertIn("refused 1 allocation since", self.causes(refused=1)[0])
self.assertIn("refused 2 allocations since", self.causes(refused=2)[0])
self.assertEqual(self.causes(xid={"n": 0, "last": None}), [])
def test_a_program_with_no_name_and_its_gigabytes(self):
self.assertEqual(self.causes(foreign=[{"name": "", "pid": 9, "bytes": 2_449_999_999}]),
["A program (pid 9) holds 2.4 GB of GPU memory and is not this lane's."])
def test_the_driver_and_the_kernel_each_when_both_are_known(self):
last = {"driver": "1.0", "kernel": "6.1", "day": "2026-11-24"}
self.assertEqual(self.causes(driver="2.0", kernel="6.1", last=last),
["The NVIDIA driver is 2.0; this lane last started under 1.0, on 2026-11-24."])
self.assertEqual(self.causes(driver="1.0", kernel="6.2", last=last),
["The kernel is 6.2; this lane last started under 6.1, on 2026-11-24."])
for last in ({"driver": None, "kernel": None, "day": "x"}, {"day": "x"}):
self.assertEqual(self.causes(driver="2.0", kernel="6.2", last=last), [])
def todo(self, **kw):
return lc.boot_todo(**{**dict(held=None, driver="2.0", kernel="6.1", last={"driver": "1.0", "kernel": "6.1"}), **kw})
def test_going_back_is_proposed_for_a_driver_that_changed_and_says_what_else_did(self):
one, both = "the one thing read here that changed", "it and the kernel are what changed"
self.assertIn(one, self.todo()[1])
self.assertIn(both, self.todo(kernel="6.2")[1])
self.assertIn(one, self.todo(kernel=None)[1], "a kernel not read is not one that changed")
self.assertIn(one, self.todo(last={"driver": "1.0"})[1])
for none in ({"driver": "1.0"}, {"driver": None}, {"last": None}, {"last": {"driver": None, "kernel": "6.0"}}):
self.assertEqual(len(self.todo(**none)), 1, none)
self.assertEqual(len(self.todo(held={"mhz": 942})), 3)
self.assertIn("cut its power", self.todo(driver="1.0", held={"mhz": 942})[1])
if __name__ == "__main__":
unittest.main()