From 37967e8b9b6b73f6a95880eaa03d66e98e1c37a5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 22 Sep 2026 01:59:37 +0200 Subject: [PATCH] Probe the host's timeout at 15, 200 and 7300 ms, and stress the tolerance Soul's sixth pass (N2): with probes at 150, 200 and 5000 ms, `min(t,5000)`, `min(t,4990)`, `t*11/10` and `t+70` survived both targets, `max(t,220)` survived linux-x64, and `max(t,250)` and `t+80` died by 22 and 2 ms. "5000 is above any plausible ceiling" was wrong: 5000 is a round ceiling. - A 15 ms probe, asked ten times with the fastest checked against 30 ms. It is the only place a floor or an added constant shows. Scheduler delay only adds, so noise has to delay all ten attempts to fail the bridge. - The long probe is 7300 ms, which is not a round number. - A single poll gets 60 ms plus a sixteenth of the timeout. - Each timed scenario prints its worst overshoot per probe. - Three harness entries: a round clamp `min(t,5000)`, a floor `max(t,100)` and an offset `t+100`. Stressed with two busy threads per logical CPU (16 on 8), five rounds of every timed scenario on both targets. At normal priority, Windows put a single poll up to 127 ms late and the fastest of ten 15 ms attempts up to 87 ms late, and a 45 ms tolerance failed the unmutated bridge in four rounds of five. The timed scenarios therefore raise their own process to HIGH_PRIORITY_CLASS on win32-x64, which needs no privilege. With that, the worst single-poll overshoot was 15 ms on win32-x64 and 41 ms on linux-x64, at normal priority there. The fastest 15 ms attempt was 0 ms late on both. The limit in source is measured, not asserted. Each boundary mutant was compiled in and run on both targets: - `t+25` survived and `t+40` died; - `max(t,40)` survived and `max(t,50)` died, on linux-x64 by 5 ms; - `t*106/100` survived and `t*108/100` died; - `min(t,7285)` survived and `min(t,7275)` died. Co-Authored-By: Claude Opus 5 --- .../tests/cultmesh_quic_native_tests.cpp | 171 +++++++++++++----- scripts/mutate-cultmesh.mjs | 38 +++- 2 files changed, 159 insertions(+), 50 deletions(-) diff --git a/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp b/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp index ad4e0be3..bf53ad42 100644 --- a/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp +++ b/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp @@ -47,9 +47,17 @@ #include +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include +#endif + +#include #include #include #include +#include #include #include #include @@ -153,44 +161,90 @@ std::string CloseRaceOnce(int pollers) { return {}; } -// The two timeouts `polltimeout` asks for. Two, and not one, because a single +// The three timeouts `polltimeout` asks for. More than one, because a single // value cannot tell a bridge that honours the host's timeout from one that waits -// on that same number of its own: a constant has to satisfy both bands, and no -// constant is in both. +// on that same number of its own: a constant has to satisfy every band, and no +// constant is in two of them. // // A constant was never the hard case, though, and the numbers are chosen for the // waits that are computed from the argument instead. Any mapping that is the -// identity at both of these passes for free, so the two are picked to leave the -// ordinary ones nowhere to be identity: +// identity at every probe passes for free, so each probe is placed to leave one +// family of ordinary mappings nowhere to be identity: // -// - 5000 is above any ceiling a bridge would plausibly clamp a host's timeout -// to. A wait of `min(timeout, 1000)` — cap it so a shutdown gets noticed — is -// the most ordinary spelling this line will ever be given, and it returns -// from this probe four seconds early. -// - 200 is low enough that an added constant shows as a proportion. A wait of -// `timeout + 150` is 75% late here, and the late tolerance below is a -// fraction of what was asked for rather than a flat number it could hide in. +// - 15 ms is as small as the OS timer lets a measurement be honest about. A +// floor — `max(timeout, 100)`, so a polling host cannot spin — and an added +// constant — `timeout + 100` of grace — are both the whole of the wait here, +// and nothing larger can see either one under the scheduler's noise. +// - 200 is the ordinary host poll, and sits between the other two so that a +// constant cannot satisfy both of its neighbours. +// - 7300 is long and deliberately not round. A clamp — `min(timeout, 1000)` to +// notice a shutdown, `min(timeout, 5000)` because five seconds is surely +// enough — is identity at any probe at or under its ceiling, and a round +// probe is exactly where somebody's round ceiling sits. +constexpr int32_t kShortestPollMs = 15; constexpr int32_t kShortPollMs = 200; -constexpr int32_t kLongPollMs = 5000; +constexpr int32_t kLongPollMs = 7300; // How much sooner than the timeout a poll may return. A wait may be late; it may // not be early, because returning early is exactly what waiting on a shorter // duration looks like. This is scheduler granularity, not slack. constexpr int kEarlyToleranceMs = 20; -// How much later than the timeout a poll may return: an allowance for scheduler -// granularity plus an eighth of what was asked for. +// How much later than the timeout a single poll may return: an allowance for +// the scheduler, plus a sixteenth of what was asked for. // -// Proportional, and not the flat 300 ms this used to be, because a flat -// tolerance is a gap a mapping hides in. `timeout + k` for any k under the flat -// value passes at every probe, at every value, forever — and the measured -// overshoot on both targets is about 10 ms, so a flat 300 was 290 ms of room -// for a bridge to add a little of its own and be believed. +// This is what the probes that look for gross failures use: a constant of the +// bridge's own, a clamp, a hold that parks a call until it is released, a wait +// that restarts on every wake. Each of those is wrong by far more than this. +constexpr int kSchedulerSlackMs = 60; +constexpr int LateToleranceMs(int32_t timeout_ms) { return kSchedulerSlackMs + timeout_ms / 16; } + +// The 15 ms probe is held to something much tighter, because it is the only +// place a floor or an added constant shows and a scheduler allowance would hide +// both. Scheduler delay only ever adds, so the probe is asked +// `kShortestAttempts` times and the fastest is what is checked: noise has to +// delay every attempt to fail the bridge, and a floor or offset delays every +// attempt by construction. Every attempt is still checked for being early. +constexpr int kShortestAttempts = 10; +constexpr int kTimerSlackMs = 30; + +// Both allowances were checked against a loaded machine, two busy threads per +// logical CPU for five rounds of all three timed scenarios, and not a quiet one. +// Worst overshoot seen, win32-x64 and linux-x64 on 8 logical CPUs: +// - a single poll: 15 and 41 ms; +// - the fastest of the 15 ms attempts: 0 and 0 ms. +// On win32-x64 that needs `MeasureAboveTheLoad` below. At normal priority under +// the same load a single poll landed up to 127 ms late and the fastest of ten +// 15 ms attempts up to 87 ms late — Windows makes a woken thread wait out busy +// threads' time slices — and a tolerance of 45 ms plus a sixteenth failed the +// unmutated bridge in four rounds of five. // -// The honest limit: a scaling smaller than an eighth survives this, and this -// does not claim otherwise. An eighth is under the smallest scaling anyone -// writes on purpose, and 60 ms is six times the overshoot either target shows. -constexpr int LateToleranceMs(int32_t timeout_ms) { return 60 + timeout_ms / 8; } +// The honest limit. Each of these was compiled into the bridge and run through +// all three timed scenarios on both targets; what survives is not claimed: +// - an added constant: `timeout + 25` survived and `timeout + 40` died, on the +// 15 ms probe alone; +// - a floor: `max(timeout, 40)` survived and `max(timeout, 50)` died, on the +// 15 ms probe alone and on linux-x64 by 5 ms, so a floor near 50 is a kill +// that rests on timing; +// - a scaling: `timeout * 106 / 100` survived and `timeout * 108 / 100` died, +// on the 7300 ms probe alone; +// - a clamp: `min(timeout, 7285)` survived and `min(timeout, 7275)` died; a +// clamp at or above 7300 is the identity at every probe; +// - a shortening of up to `kEarlyToleranceMs` at any probe. +// Each run prints its worst overshoot per probe, so the allowances can be +// rechecked on another machine rather than trusted. + +// The timed scenarios measure the bridge's wait, and on a loaded Windows machine +// what they would otherwise measure is the scheduler's: a thread woken at the +// end of its wait queues behind busy threads of equal priority for whole time +// slices. Raising this process above them takes that out of the measurement, +// and it needs no privilege on Windows. Linux schedules a woken thread promptly +// at normal priority under the same load, so nothing is asked of it there. +void MeasureAboveTheLoad() { +#if defined(_WIN32) + SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); +#endif +} struct TimedPoll { int32_t result; @@ -215,31 +269,53 @@ TimedPoll PollFor(void* runtime, int32_t timeout_ms) { // bridge could wait on any duration at all and every assertion would still hold. // A runtime with no listener and no connection has nothing to deliver, so the // timeout is what ends this poll, and the elapsed time is what it waited on. -std::string PollTimeoutOnce() { +// The bands every timed poll is held to, and the worst overshoot seen, which is +// printed so a tolerance can be checked against a machine under load rather than +// trusted. +struct Overshoot { + long long worst_ms = LLONG_MIN; + void Record(const TimedPoll& poll, int32_t timeout_ms) { + worst_ms = (std::max)(worst_ms, poll.elapsed_ms - timeout_ms); + } +}; + +std::string CheckTimedPoll(const TimedPoll& poll, int32_t timeout_ms, int late_tolerance_ms) { + const std::string asked = "a poll asking for " + std::to_string(timeout_ms) + " ms "; + if (poll.result != 0) + return asked + "returned " + std::to_string(poll.result) + ", not the 0 an idle runtime owes it"; + if (poll.elapsed_ms < timeout_ms - kEarlyToleranceMs) + return asked + "returned after " + std::to_string(poll.elapsed_ms) + + " ms: the wait ended on some shorter duration than the one it was given"; + if (poll.elapsed_ms > static_cast(timeout_ms) + late_tolerance_ms) + return asked + "returned after " + std::to_string(poll.elapsed_ms) + + " ms: the wait ended on some longer duration than the one it was given"; + return {}; +} + +constexpr std::array kTimedProbesMs{kShortestPollMs, kShortPollMs, kLongPollMs}; + +std::string PollTimeoutOnce(std::array& overshoot) { void* runtime = nullptr; const int32_t opened = cultmesh_quic_runtime_open("cultmesh-quic-native-tests", &runtime); if (opened != 0 || runtime == nullptr) return "cultmesh_quic_runtime_open returned " + std::to_string(opened); std::string failure; - for (const int32_t timeout_ms : {kShortPollMs, kLongPollMs}) { - const TimedPoll poll = PollFor(runtime, timeout_ms); - const std::string asked = "a poll asking for " + std::to_string(timeout_ms) + " ms "; - if (poll.result != 0) { - failure = asked + "returned " + std::to_string(poll.result) + - ", not the 0 an idle runtime owes it"; - break; - } - if (poll.elapsed_ms < timeout_ms - kEarlyToleranceMs) { - failure = asked + "returned after " + std::to_string(poll.elapsed_ms) + - " ms: the wait ended on some shorter duration than the one it was given"; - break; - } - if (poll.elapsed_ms > timeout_ms + LateToleranceMs(timeout_ms)) { - failure = asked + "returned after " + std::to_string(poll.elapsed_ms) + - " ms: the wait ended on some longer duration than the one it was given"; - break; - } + // The fastest of several, each checked for being early; see kShortestAttempts. + TimedPoll fastest{0, LLONG_MAX}; + for (int attempt = 0; attempt < kShortestAttempts && failure.empty(); ++attempt) { + const TimedPoll poll = PollFor(runtime, kShortestPollMs); + failure = CheckTimedPoll(poll, kShortestPollMs, INT_MAX); + if (poll.elapsed_ms < fastest.elapsed_ms) fastest = poll; + } + if (failure.empty()) { + overshoot[0].Record(fastest, kShortestPollMs); + failure = CheckTimedPoll(fastest, kShortestPollMs, kTimerSlackMs); + } + for (size_t probe = 1; probe < kTimedProbesMs.size() && failure.empty(); ++probe) { + const TimedPoll poll = PollFor(runtime, kTimedProbesMs[probe]); + overshoot[probe].Record(poll, kTimedProbesMs[probe]); + failure = CheckTimedPoll(poll, kTimedProbesMs[probe], LateToleranceMs(kTimedProbesMs[probe])); } cultmesh_quic_runtime_close(runtime); @@ -247,12 +323,16 @@ std::string PollTimeoutOnce() { } int PollTimeout(int iterations) { + MeasureAboveTheLoad(); + std::array overshoot{}; for (int iteration = 0; iteration < iterations; ++iteration) { - const std::string failure = PollTimeoutOnce(); + const std::string failure = PollTimeoutOnce(overshoot); if (!failure.empty()) return Fail("polltimeout iteration " + std::to_string(iteration) + ": " + failure); } - std::printf("polltimeout %dx: ok\n", iterations); + std::printf("polltimeout %dx: ok (worst overshoot %lld ms at %d best of %d, %lld ms at %d, %lld ms at %d)\n", + iterations, overshoot[0].worst_ms, kTimedProbesMs[0], kShortestAttempts, overshoot[1].worst_ms, kTimedProbesMs[1], + overshoot[2].worst_ms, kTimedProbesMs[2]); return 0; } @@ -628,6 +708,7 @@ std::string HoldTimeoutOnce() { } int HoldTimeout(int iterations) { + MeasureAboveTheLoad(); for (int iteration = 0; iteration < iterations; ++iteration) { const std::string failure = HoldTimeoutOnce(); if (!failure.empty()) diff --git a/scripts/mutate-cultmesh.mjs b/scripts/mutate-cultmesh.mjs index 08a3f1af..b362a12a 100644 --- a/scripts/mutate-cultmesh.mjs +++ b/scripts/mutate-cultmesh.mjs @@ -478,17 +478,17 @@ const mutations = [ // for five seconds silently got two, and every consumer's poll loop spun at two // and a half times the rate it asked for. // - // `polltimeout` asks twice and measures. That is what makes a constant - // unsurvivable rather than merely unlucky: no single value sits in both bands, + // `polltimeout` asks three times and measures. That is what makes a constant + // unsurvivable rather than merely unlucky: no single value sits in two bands, // so the first two entries below are two sides of one rule and not two numbers // to be tuned against. // // A constant was never the hard case, though. A wait computed from the - // argument is identity wherever the probes are, and passes for free: the two + // argument is identity wherever the probes are, and passes for free: the // bands rule out a constant and rule out nothing else. Clamping, adding and // scaling all survived the whole matrix once, on bands of 200 and 900 with a // flat late tolerance of 300 ms. The scenario's numbers are what closed that, - // and the third entry is here so nothing reopens it quietly — a table of + // and the entries after the constants are here so nothing reopens it quietly — a table of // constants would go green again the moment those numbers drifted back. { target: "native", @@ -514,11 +514,39 @@ const mutations = [ // so a shutdown gets noticed. Its failure is verbatim the one the scenario // exists for — a host asking for five seconds gets one, forever, with // nothing reporting it — and it is identity at any probe below the cap, so - // it is the long probe's height that kills it rather than the two bands. + // it is the long probe's height that kills it rather than the bands. rule: "the wait is on the host's timeout (loosening: the bridge caps it at a second)", old: "std::chrono::milliseconds(timeout_ms),", new: "std::chrono::milliseconds((std::min)(timeout_ms, 1000)),", }, + // The same clamp at a round ceiling nobody would call short. It survived both + // targets while the long probe was 5000, where it is the identity; the probe is + // 7300 now so that no round ceiling sits on it. + { + target: "native", + honestOn: ["linux-x64", "win32-x64"], + rule: "the wait is on the host's timeout (loosening: the bridge caps it at a round five seconds)", + old: "std::chrono::milliseconds(timeout_ms),", + new: "std::chrono::milliseconds((std::min)(timeout_ms, 5000)),", + }, + // A floor and an added constant are the whole wait at a small timeout and a + // rounding error at a large one, so only the 15 ms probe sees them. Both + // survived every probe while the smallest was 150 ms. The scenario's source + // states how small a floor or offset still survives. + { + target: "native", + honestOn: ["linux-x64", "win32-x64"], + rule: "the wait is on the host's timeout (loosening: a floor, so a polling host cannot spin)", + old: "std::chrono::milliseconds(timeout_ms),", + new: "std::chrono::milliseconds((std::max)(timeout_ms, 100)),", + }, + { + target: "native", + honestOn: ["linux-x64", "win32-x64"], + rule: "the wait is on the host's timeout (loosening: a little grace added to it)", + old: "std::chrono::milliseconds(timeout_ms),", + new: "std::chrono::milliseconds(timeout_ms + 100),", + }, // The seam's own rule, and the only non-comment source change of the last pass // that nothing pinned. It is development-only and folds away in release, so // nothing shipped was at risk; it is still the shape this campaign keeps