perf(worker,gateway): pin MXSTATUS_PROXY record info, dedicated event-drain thread, cap thread-pool spin

At ~240 events/s the worker burned 8-12% system CPU on wonder-app-vd03. A WPR
trace put 72% of it in lmxproxy's per-event GetRecordInfoFromGuids (type
library reload through the registry, amplified by WOW64 and the EDR registry
filter) and 26% in the .NET Framework thread pool spinning after each
per-event work item.

- Worker: MxStatusProxyRecordInfoPin holds the LMXProxy ITypeLib and the
  MXSTATUS_PROXY IRecordInfo for the process lifetime, so oleaut32 serves the
  per-event lookup from cache (~1 ms -> <1 us per call). Best-effort, never
  fatal; status logged at handshake.
- Worker: the event drain loop runs on a dedicated background thread with a
  synchronous wait and a synchronous pipe flush, removing the per-event
  thread-pool hops.
- Gateway: runtimeconfig sets ThreadPool.UnfairSemaphoreSpinLimit=10 to cut
  PortableThreadPool spin-wait CPU.

windev bench at ~242 ev/s: worker 67% -> 6.2% of one core; gateway 8.1% -> 6.9%.
Worker suite 785 passed / 14 skipped (x86, windev).
This commit is contained in:
Joseph Doherty
2026-11-21 08:00:13 -05:00
parent 08a9cfd68a
commit 659bb831a2
15 changed files with 398 additions and 34 deletions
@@ -375,7 +375,7 @@ public sealed class WorkerFrameProtocolTests
/// <summary>
/// Verifies the writer coalesces the flush across a batch of frames drained together: four frames
/// queued behind an in-progress write drain in a single pass and share one FlushAsync, not four.
/// queued behind an in-progress write drain in a single pass and share one Flush, not four.
/// Every frame still reaches the wire intact.
/// <para>
/// The burst is all-event on purpose. The control-frame completion decoupling made the drain flush
@@ -1386,20 +1386,20 @@ public sealed class WorkerFrameProtocolTests
};
}
// A MemoryStream that counts FlushAsync calls without gating any write, so a batch write can be
// A MemoryStream that counts Flush calls without gating any write, so a batch write can be
// asserted to flush exactly once.
private sealed class FlushCountingStream : MemoryStream
{
private int _flushCount;
/// <summary>Gets the number of <see cref="FlushAsync"/> calls observed so far.</summary>
/// <summary>Gets the number of <see cref="Flush"/> calls observed so far.</summary>
public int FlushCount => Volatile.Read(ref _flushCount);
/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
public override void Flush()
{
Interlocked.Increment(ref _flushCount);
return base.FlushAsync(cancellationToken);
base.Flush();
}
}
@@ -1500,7 +1500,7 @@ public sealed class WorkerFrameProtocolTests
/// <summary>Gets a task that completes once the second gated <see cref="WriteAsync"/> call has started blocking.</summary>
public Task SecondGateWriteStarted => _secondGateWriteStarted.Task;
/// <summary>Gets the number of <see cref="FlushAsync"/> calls observed so far.</summary>
/// <summary>Gets the number of <see cref="Flush"/> calls observed so far.</summary>
public int FlushCount => Volatile.Read(ref _flushCount);
/// <summary>Releases the first blocked write so it can complete.</summary>
@@ -1528,10 +1528,10 @@ public sealed class WorkerFrameProtocolTests
}
/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
public override void Flush()
{
Interlocked.Increment(ref _flushCount);
return base.FlushAsync(cancellationToken);
base.Flush();
}
/// <inheritdoc />
@@ -1547,7 +1547,7 @@ public sealed class WorkerFrameProtocolTests
}
}
// A MemoryStream whose first write blocks until released and whose every FlushAsync throws, so a test
// A MemoryStream whose first write blocks until released and whose every Flush throws, so a test
// can fault the class-boundary flush with control frames already written and an event frame already
// claimed off its queue.
private sealed class FlushFaultingGatedStream : MemoryStream
@@ -1584,9 +1584,9 @@ public sealed class WorkerFrameProtocolTests
}
/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
public override void Flush()
{
return Task.FromException(new IOException(_faultMessage));
throw new IOException(_faultMessage);
}
/// <inheritdoc />
@@ -367,6 +367,45 @@ public sealed class WorkerPipeSessionTests
await SendShutdownAndWaitAsync(pipePair, runTask, cancellation.Token);
}
/// <summary>
/// The event drain loop runs on its own dedicated background thread, not the CLR thread
/// pool: on .NET Framework every pool continuation per event also paid the pool's idle
/// spin, which a production trace measured at ~150 context switches per event. The parked
/// wait is observed from the fake, which records the calling thread.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RunAsync_EventDrainLoop_RunsOnDedicatedNonPoolThread()
{
using CancellationTokenSource cancellation = new(TimeSpan.FromSeconds(10));
using PipePair pipePair = await PipePair.CreateAsync(cancellation.Token);
FakeRuntimeSession runtime = new()
{
WaitForEventsOnSignalOnly = true,
};
WorkerPipeSession session = CreatePipeSession(pipePair.WorkerStream, runtime);
Task runTask = session.RunAsync(cancellation.Token);
await CompleteGatewayHandshakeAsync(pipePair, cancellation.Token);
while (runtime.LastWaitForEventsTimeout is null)
{
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellation.Token);
}
Assert.Equal("mxgw-event-drain", runtime.LastWaitForEventsThreadName);
Assert.False(runtime.LastWaitForEventsOnThreadPool);
// The dedicated thread still ships events and still stops cleanly on shutdown.
runtime.EnqueueEvent(CreateWorkerEvent(sequence: 3));
WorkerEnvelope workerEvent = await ReadUntilAsync(
pipePair.GatewayReader,
WorkerEnvelope.BodyOneofCase.WorkerEvent,
cancellation.Token);
Assert.Equal(3UL, workerEvent.WorkerEvent.Event.WorkerSequence);
await SendShutdownAndWaitAsync(pipePair, runTask, cancellation.Token);
}
/// <summary>
/// Verifies that a Ping control command is answered on the worker side
/// (not dispatched to the STA) with an OK reply that echoes the ping