ModernUO/Projects/Server.Tests/Tests/Network/EventLoopIdleTests.cs
Kamron Batman 6aedbbe2ef
perf(core): sleep the event loop when idle
The loop span through its body regardless of whether there was anything
to do -- ~10% of a desktop core for an empty shard, ~70% of a small VPS
core, and a process that never idles is exactly what burstable vCPU
plans throttle.

The loop now blocks in NetState.WaitForCompletion whenever every queue
it drains is empty, waking on the next timer tick or the moment work
arrives. Receive completions, new connections, and cross-thread
LoopContext.Post (via IORingGroup 1.0.10's sticky Wake) are all in the
wait set, so sleeping adds no latency to any of them; only timer-driven
logic sees wheel lag, bounded by server.eventLoopIdleWaitMs (default
2ms, 0 = never sleep).

Measured on a real world of 190k items / 33k mobiles: 10.4% of a core
to 0.8-1.0%, with peak tick lag unchanged. Spin mode independently
gained 7x the iterations per core from the ring's AcceptEx rework.
Sleeping also gives the GC natural pause points, which the old spin
loop denied it -- memory no longer climbs until a save forces a
collection.

A sleep is bounded by the next wheel turn, so a correctly honoured
sleep can never miss a deadline; the only way sleeping harms the wheel
is the host returning the wait late. That overshoot is measured on
every sleep, and an escalating backoff (server.lateWakeThreshold)
suspends sleeping when it persists -- server work like saves or heavy
commands cannot trip it by construction. Hosts without high-resolution
waits are detected once at startup and spin instead. The admin gump
shows the verdict instead of the now-meaningless CPS figure, which is
removed.

Time accounting for diagnosis is compiled out of normal builds: build
with -p:EventLoopProfiling=true to enable EventLoopProfiler (per-phase
wall time, sleep overshoot, GC pauses, stolen-time residual, ~15min
ring buffer) and the [LoopStats command with CSV dump. See
dev-docs/debugging-event-loop.md for the diagnosis funnel.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-09 12:53:48 -07:00

69 lines
1.7 KiB
C#

using Xunit;
namespace Server.Tests;
/// <summary>
/// The event loop only sleeps when every queue it drains is empty. These drains are deliberately
/// bounded -- ExecuteTasks stops at its per-frame cap -- so leftover work is normal and must keep
/// the loop awake. Getting this wrong strands queued work for the length of a sleep.
/// </summary>
[Collection("Sequential Server Tests")]
public class EventLoopIdleTests
{
[Fact]
public void FreshContextIsEmpty()
{
var context = new EventLoopContext();
Assert.True(context.IsEmpty);
}
[Fact]
public void PostedWorkMakesContextNonEmpty()
{
var context = new EventLoopContext();
context.Post(() => { });
Assert.False(context.IsEmpty);
}
[Fact]
public void PriorityWorkMakesContextNonEmpty()
{
var context = new EventLoopContext();
context.Post(() => { }, EventLoopContext.Priority.High);
Assert.False(context.IsEmpty);
}
[Fact]
public void ContextIsEmptyAgainOnceDrained()
{
var context = new EventLoopContext();
context.Post(() => { });
context.ExecuteTasks();
Assert.True(context.IsEmpty);
}
[Fact]
public void WorkBeyondThePerFrameCapKeepsContextNonEmpty()
{
// The cap is what makes IsEmpty necessary: a single ExecuteTasks pass cannot be assumed
// to have drained everything, so the loop must not treat "I just ran tasks" as "idle".
const int perFrameCap = 128;
var context = new EventLoopContext(perFrameCap);
for (var i = 0; i < perFrameCap + 10; i++)
{
context.Post(() => { });
}
context.ExecuteTasks();
Assert.False(context.IsEmpty);
}
}