"""Exact hypothetical serial scheduling; Python 3, standard library only. Units: MiB and milliseconds. No firmware, parallelism, seek or retry model. Inputs are integers, rational strings, or Fraction objects, never floats. """ from fractions import Fraction as F import json from pathlib import Path import sys def rational(value, name, positive=False): if isinstance(value, bool) or not isinstance(value, (int, str, F)): raise ValueError(name + " must be an exact rational") try: result = F(value) except (ValueError, ZeroDivisionError) as error: raise ValueError(name + " must be finite") from error if result < 0 or (positive and result == 0): raise ValueError(name + " is outside the model domain") return result def delay_bound(rate, latency, low_max, high_burst, high_rate=0): rate = rational(rate, "rate", True) latency = rational(latency, "latency") low_max = rational(low_max, "low_max") high_burst = rational(high_burst, "high_burst", True) high_rate = rational(high_rate, "high_rate") if high_rate > rate: raise ValueError("high_rate exceeds guaranteed rate") return latency + (low_max + high_burst) / rate def schedule(low_work, high_arrival, high_work, rate=1): """All low jobs arrive at zero; one high job wins dispatch-time ties.""" low = [rational(x, "low work", True) for x in low_work] arrival = rational(high_arrival, "arrival") work = rational(high_work, "high work", True) rate = rational(rate, "rate", True) clock, trace, served = F(0), [], False for index, amount in enumerate(low): if not served and arrival <= clock: trace.append(dict(job="H", start=clock, end=clock + work / rate)) clock += work / rate served = True trace.append(dict(job="L" + str(index), start=clock, end=clock + amount / rate)) clock += amount / rate if not served: clock = max(clock, arrival) trace.append(dict(job="H", start=clock, end=clock + work / rate)) high = next(event for event in trace if event["job"] == "H") return dict(wait_ms=high["start"] - arrival, response_ms=high["end"] - arrival, trace=trace) def results(): cases = [] for label, low in [("one command", [8]), ("four equal commands", [2, 2, 2, 2]), ("two unequal commands", [7, 1])]: observation = schedule(low, "1/2", 1) cases.append(dict(label=label, low_work_MiB=low, rate_MiB_per_ms=1, high_arrival_ms="1/2", high_work_MiB=1, bound_ms=delay_bound(1, 0, max(low), 1), display={key: format(float(observation[key]), ".1f") for key in ("wait_ms", "response_ms")}, **observation)) return dict(schema_version=1, kind="exact hypothetical scheduling", assumptions=["Serial work-conserving server at 1 MiB/ms.", "All low-priority commands arrive at zero.", "One high-priority request arrives at 0.5 ms.", "Priority is reconsidered at every command boundary.", "No overhead, hidden queue, seek, retries or parallelism.", "Bounds cover high-class completion delay, not a percentile."], formula="T + (L + b_H)/R, with r_H <= R and a strict aggregate contract", cases=cases) def encoded(): return json.dumps(results(), indent=2, default=str) + "\n" if __name__ == "__main__": path = Path(__file__).with_name("results.json") if sys.argv[1:] == ["--write"]: path.write_text(encoded()) elif sys.argv[1:] == ["--check"]: if path.read_text() != encoded(): raise SystemExit("Generated results differ") print("Exact results match") elif not sys.argv[1:]: print(encoded(), end="") else: raise SystemExit("Use model.py [--write|--check]")