Sim::step phase timing and the CPU-profile bench driver (#147 step 3.1)

Mod side: Sim::step times plan_step, adapt, step_drones, step_moves,
update_sight, step_sams and record, plus the whole step and its untimed
remainder, through sim::profile. The simulation stays SDK-independent (also
built as airdef-core): the adapter installs the host monotonic clock
(game.monotonic-nanos) in init, native tests use std::time::Instant. The
counters are fixed-size, per-thread, read no randomness and change no
decision; /airdef stats reports them as PROFILE chat lines, reset per world.

Driver: raid_bench gains --scheduler in-frame|server-thread (the two
single-player paths, measured separately), HORIZON_MOD_CPU_PROFILE=1, the
per-tick mod-path spans joined to the frame trace (per-frame totals, >25/33 ms
counts, what the worst CPU frames carry), and the mod phase profile read at
idle and battle end with the battle delta. report.json carries mod_cpu,
mod_cpu_ticks, mod_phases_* and the derived correlation. Tests for the new
helpers.

Engine gitlink: mod-path CPU spans + game.monotonic-nanos (API level 32).

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-21 12:29:22 +03:00
parent c08b737296
commit 92c058f606
6 changed files with 368 additions and 28 deletions
+124 -4
View File
@@ -76,6 +76,8 @@ BENCH_SECONDS = {"idle_cold": 4, "idle_warm": 6, "battle": 10, "replay": 12}
SETTLE_TICKS = 500 # 25 с: LOD сходится, как в методике #126
REPLAY_WARM_TICKS = 100 # 5 с записи до окна замера
STATS_RE = re.compile(r"В воздухе: (\d+), ракет: (\d+), ПВО: (\d+), сбито: (\d+), поражений: (\d+)")
# The mod's per-phase CPU profile lines (#147 step 3.1): `PROFILE key=value ...`.
PROFILE_RE = re.compile(r"(\w+)=(-?\d+(?:\.\d+)?)")
class BenchError(RuntimeError):
@@ -114,6 +116,96 @@ def frame_summary(raw):
return out
def parse_profile(lines):
"""Merge the mod's `PROFILE key=value` chat lines into one dict (#147 3.1)."""
out = {}
for line in lines:
if "PROFILE" not in line:
continue
for key, val in PROFILE_RE.findall(line):
out[key] = float(val) if "." in val else int(val)
return out
def profile_delta(base, battle):
"""Battle phase work = cumulative-at-battle-end minus cumulative-at-idle.
Totals and the step count subtract; per-step maxima are kept from the battle
reading (the peak almost always falls in the battle, not the idle warm-up).
"""
if not base or not battle:
return None
out = {}
for key, val in battle.items():
if key.endswith("_max_ms"):
out[key] = val
elif key in base:
out[key] = round(val - base[key], 3) if isinstance(val, float) else val - base[key]
else:
out[key] = val
return out
# The mod-path spans, in the order the engine records them per tick.
MOD_SPANS = ("ground_ms", "world_ms", "snapshot_ms", "wasm_ms", "models_ms", "output_ms", "total_ms")
def mod_cpu_frames(window):
"""Join the per-tick mod-path spans to the frame trace (#147 step 3.1).
A frame may run several game ticks; their spans sum into that frame's index
(`frame` in each record is the index into the raw frame arrays). Returns the
per-frame mod-path total's percentiles, the >25/33 ms frame counts, and what
the worst CPU frames carry — so the mod peaks can be checked against the
frame-time peaks. None when no spans were recorded.
"""
ticks = window.get("mod_cpu_ticks") or []
frames = window.get("frame_ms") or []
if not ticks or not frames:
return None
n = len(frames)
per = [[0.0] * len(MOD_SPANS) for _ in range(n)]
seen = set()
for t in ticks:
f = int(t.get("frame", 0))
if 0 <= f < n:
seen.add(f)
for j, key in enumerate(MOD_SPANS):
per[f][j] += t.get(key, 0.0)
total = [row[MOD_SPANS.index("total_ms")] for row in per]
wasm = [row[MOD_SPANS.index("wasm_ms")] for row in per]
world = [row[MOD_SPANS.index("world_ms")] for row in per]
models = [row[MOD_SPANS.index("models_ms")] for row in per]
out = {
"ticks": len(ticks),
"frames_with_ticks": len(seen),
"per_frame_total_ms": {
"p50_ms": percentile(total, 0.5),
"p95_ms": percentile(total, 0.95),
"p99_ms": percentile(total, 0.99),
"max_ms": max(total),
},
"frames_mod_over_25ms": sum(1 for v in total if v > 25.0),
"frames_mod_over_33ms": sum(1 for v in total if v > 33.0),
}
cpu = window.get("cpu_ms") or []
if cpu:
# The worst 1% of frames by CPU time: how much mod-path work they carry.
order = sorted(range(len(cpu)), key=lambda i: cpu[i], reverse=True)
worst = order[:max(1, len(order) // 100)]
m = len(worst)
out["worst_cpu_frames"] = {
"count": m,
"avg_cpu_ms": sum(cpu[i] for i in worst) / m,
"avg_mod_total_ms": sum(total[i] for i in worst if i < n) / m,
"avg_wasm_ms": sum(wasm[i] for i in worst if i < n) / m,
"avg_world_ms": sum(world[i] for i in worst if i < n) / m,
"avg_models_ms": sum(models[i] for i in worst if i < n) / m,
"with_mod_tick": sum(1 for i in worst if i in seen),
}
return out
def git_sha(cwd):
r = subprocess.run(["git", "rev-parse", "HEAD"], cwd=cwd, capture_output=True, text=True)
return r.stdout.strip() if r.returncode == 0 else None
@@ -127,7 +219,8 @@ def git_branch(cwd):
class Driver:
"""Одна игра на своём сокете; команды horizon-ctl и ожидание по тикам."""
def __init__(self, out, game, resources, mod, extra_env, width, height):
def __init__(self, out, game, resources, mod, extra_env, width, height,
scheduler="in-frame"):
self.out = Path(out).resolve()
self.home = self.out / "home"
self.sock = self.out / "c.sock"
@@ -137,6 +230,8 @@ class Driver:
self.mod = Path(mod).resolve()
self.extra_env = dict(extra_env)
self.width, self.height = width, height
# #147 step 3.1: the two single-player schedulers, measured separately.
self.scheduler = scheduler
self.t0 = None
# --- запуск ---
@@ -163,6 +258,7 @@ class Driver:
"HORIZON_NO_AUDIO": "1",
"HORIZON_LAUNCHER_NO_SELF_UPDATE": "1",
"HORIZON_MOD_FUEL_PROFILE": "1",
"HORIZON_MOD_CPU_PROFILE": "1",
"HORIZON_HD_SCALE": "1",
"HORIZON_SPIKES": "0",
})
@@ -174,8 +270,12 @@ class Driver:
self.sock.unlink()
cmd = [str(CTL), "--socket", str(self.sock), "launch",
"--world", "Polygon", "--creative", "--offscreen", "--wait",
"--in-frame", "--width", str(self.width), "--height", str(self.height),
"--width", str(self.width), "--height", str(self.height),
"--game", str(self.game), "--resources", str(self.resources)]
# in-frame: the window ticks the world itself; server-thread (the default
# players run): the integrated server on its own thread, the window a client.
if self.scheduler == "in-frame":
cmd.append("--in-frame")
with open(self.log, "w") as log:
r = subprocess.run(cmd, text=True, env=self.env(),
cwd=self.home, timeout=600, stdout=log, stderr=subprocess.STDOUT)
@@ -231,6 +331,12 @@ class Driver:
def stats(self, raw=True):
return self.ctl_json("stats", "raw" if raw else "", timeout=120.0)
def mod_profile(self):
"""The mod's cumulative per-phase CPU profile (`/airdef stats` chat lines)."""
self.chat("/airdef stats")
time.sleep(0.3)
return parse_profile(self.chat_log(20))
def bench(self, seconds):
"""bench N: сброс счётчиков, окно N секунд, summary + сырые времена."""
self.ctl("bench", str(seconds), timeout=seconds + 120.0)
@@ -315,7 +421,8 @@ def static_parity(args):
env = dict(kv.split("=", 1) for kv in args.env)
env.update({"HORIZON_HD_TAA": "0", "HORIZON_HD_WIND": "0",
"HORIZON_HD_CLOUDS": "0", "HORIZON_HD_VOLFOG": "0"})
d = Driver(args.out, args.game, args.resources, args.mod, env, args.width, args.height)
d = Driver(args.out, args.game, args.resources, args.mod, env, args.width, args.height,
scheduler=args.scheduler)
d.prepare()
d.launch()
out = Path(args.out).resolve()
@@ -345,11 +452,14 @@ def static_parity(args):
def run(args):
d = Driver(args.out, args.game, args.resources, args.mod,
dict(kv.split("=", 1) for kv in args.env), args.width, args.height)
dict(kv.split("=", 1) for kv in args.env), args.width, args.height,
scheduler=args.scheduler)
d.prepare()
d.launch()
report = {
"issue": 147,
"step": "3.1",
"scheduler": args.scheduler,
"label": args.label,
"timestamp": datetime.datetime.now().isoformat(timespec="seconds"),
"git": {
@@ -382,10 +492,16 @@ def run(args):
"pending_chunks": d.status().get("pending_chunks"),
"lod": d.status().get("lod"),
}
# Cumulative mod phase profile before the battle (the idle baseline).
report["mod_phases_idle"] = d.mod_profile()
# Бой по фиксированным тикам; окно замера в середине боя (в потоке).
fingerprints = d.battle(report)
report["battle_fingerprints"] = fingerprints
# Cumulative mod phase profile after the battle; the delta isolates it.
report["mod_phases_battle"] = d.mod_profile()
report["mod_phases_battle_delta"] = profile_delta(
report["mod_phases_idle"], report["mod_phases_battle"])
# Та же запись в обоих путях, один процесс: бой записан один раз, replay
# проигрывает его идентично в каждом окне. Межпроцессные бои расходятся
@@ -456,6 +572,7 @@ def run(args):
for name, window in report["windows"].items():
raw = {k: window.get(k) for k in ("frame_ms", "cpu_ms", "gpu_ms")}
window["derived"] = frame_summary(raw)
window["derived"]["mod_cpu_frames"] = mod_cpu_frames(window)
print(f"{name}: frames={window.get('frames')} "
f"cpu p95={window['derived']['cpu_ms']['p95_ms']} "
f"p99={window['derived']['cpu_ms']['p99_ms']}")
@@ -554,6 +671,9 @@ def main():
help="подготовленные ресурсы 26.3 (или HORIZON_RESOURCES)")
p.add_argument("--mod", help="собранный airdef.hmod (один и тот же в A/B)")
p.add_argument("--env", action="append", default=[], metavar="КЛЮЧ=ЗНАЧЕНИЕ")
p.add_argument("--scheduler", choices=("in-frame", "server-thread"), default="in-frame",
help="single-player scheduler: in-frame (window ticks the world) или "
"server-thread (интегрированный сервер на своём потоке, путь по умолчанию)")
p.add_argument("--width", type=int, default=1280)
p.add_argument("--height", type=int, default=720)
p.add_argument("--compare", nargs="+", metavar="REPORT",