"""Exact illustrative cross-traffic bounds, not measured storage performance. Python standard library only. Work unit: MiB; time: seconds. Run model.py to regenerate results.json or model.py --check to verify it. """ from fractions import Fraction as Q import json from pathlib import Path import sys def quantity(value, *, positive=False): if isinstance(value, bool): raise ValueError("Boolean is not a quantity") try: value = Q(str(value)) except (ValueError, TypeError, ZeroDivisionError) as error: raise ValueError("Expected a finite rational quantity") from error if value < 0 or (positive and value == 0): raise ValueError("Quantity outside its nonnegative domain") return value def leftover(rate, latency, cross_burst, cross_rate, burst, arrival_rate): rate = quantity(rate, positive=True) latency, cross_burst, cross_rate, burst, arrival_rate = map( quantity, (latency, cross_burst, cross_rate, burst, arrival_rate)) residual = rate - cross_rate if residual <= 0: raise ValueError("Cross traffic leaves no positive guaranteed rate") if arrival_rate > residual: raise ValueError("Target envelope exceeds the residual rate") lag = (rate * latency + cross_burst) / residual return {"rate": residual, "latency": lag, "delay": lag + burst / residual, "backlog": burst + arrival_rate * lag} def service(time, rate, latency): time, latency = quantity(time), quantity(latency) return quantity(rate, positive=True) * max(Q(0), time - latency) def counterexample(time): """Finite-rate adaptation of Network Calculus section 6.2.1's sample path.""" t = quantity(time) a1, a2 = min(t, Q(1)), max(Q(0), t - 1) # The min-plus objective is piecewise affine; minima occur at its breakpoints. aggregate_points = {Q(0), t, max(Q(0), t - 1)} aggregate_lower = min(s + service(t - s, 2, 1) for s in aggregate_points) residual_points = {Q(0), t, min(t, Q(1)), max(Q(0), t - 2)} invalid_lower = min(min(s, Q(1)) + service(t - s, 1, 2) for s in residual_points) return {"a1": a1, "a2": a2, "d1": Q(0), "d2": a2, "aggregate_lower": aggregate_lower, "invalid_per_flow_lower": invalid_lower} def document(): result = leftover(100, "0.002", 1, 40, "0.25", 10) naive = Q("0.002") + Q("0.25") / (100 - 40) witness = counterexample(3) return { "kind": "exact-worked-calculation", "units": {"rate": "MiB/s", "latency": "s", "burst": "MiB", "delay": "s", "backlog": "MiB"}, "inputs": {"aggregate_rate": "100", "aggregate_latency": "1/500", "cross_burst": "1", "cross_rate": "40", "target_burst": "1/4", "target_rate": "10"}, "exact": result, "invalid_shortcut_delay": naive, "display": {"rate_mib_s": str(result["rate"]), "latency_ms": str(result["latency"] * 1000), "delay_ms": format(float(result["delay"] * 1000), ".2f"), "backlog_mib": format(float(result["backlog"]), ".2f"), "invalid_shortcut_ms": format(float(naive * 1000), ".2f")}, "strictness_witness": { "units": "normalized work and time, separate from the MiB example", "time": "3", "values": witness, "description": "A1=min(t,1), A2=D2=max(0,t-1), D1=0. " "The aggregate satisfies ordinary beta=2*max(0,t-1), " "but subtraction would incorrectly require D1>=1 at t=3." }, "assumptions": [ "Initially empty, lossless fluid node with strict aggregate rate-latency guarantee.", "Cross traffic and target obey their arrival envelopes on every interval.", "Common conserved work unit; arbitrary arbitration between the two flows.", "Individual target delay additionally assumes FIFO within the target flow." ], "limitations": [ "Not a device benchmark, observed latency, percentile estimate or scheduler implementation.", "Numeric inputs declare a contract; they do not establish that a storage device offers it.", "The leftover curve is ordinary; strictness is not automatically preserved.", "At target rate equal to residual rate, finite bounds do not imply spare drain capacity." ] } def rendered(): return json.dumps(document(), indent=2, default=str) + "\n" if __name__ == "__main__": if sys.argv[1:] not in ([], ["--check"]): raise SystemExit("Use model.py [--check]") path = Path(__file__).with_name("results.json") text = rendered() if sys.argv[1:]: if path.read_text() != text: raise SystemExit("Generated results differ") print("Exact results match") else: path.write_text(text)