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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user