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:
@@ -807,6 +807,21 @@ Worst-case on-disk log footprint is therefore about `retainedFileCountLimit x fi
|
||||
Raise or lower these in the deployed `appsettings.json` (or via `Serilog__WriteTo__1__Args__*` environment
|
||||
variables) to fit the host's disk budget.
|
||||
|
||||
## Runtime Settings (runtimeconfig)
|
||||
|
||||
Some settings belong to the .NET runtime rather than to `MxGateway:*` options. They ship in the
|
||||
build through `RuntimeHostConfigurationOption` items in the Server csproj, which write them into
|
||||
`ZB.MOM.WW.MxGateway.Server.runtimeconfig.json`. They therefore travel with every deploy and need
|
||||
no NSSM environment variables.
|
||||
|
||||
| Setting | Shipped value | Why |
|
||||
|---|---|---|
|
||||
| `System.Threading.ThreadPool.UnfairSemaphoreSpinLimit` | `10` (runtime default `70`) | A pool worker that finds no work spins before parking, and past step 10 it calls `Sleep(0)` on every other step. The event relay queues several pool work items per event, so on a busy host those spins became ~93% of the gateway's context switches (WPR trace on a 4-CPU production host, ~240 events/s). With `10`, idle workers spin briefly and then park. Measured on the dev box at ~240 events/s: gateway CPU −15%. |
|
||||
|
||||
To override a setting on one host without rebuilding, use the environment-variable form, which
|
||||
takes precedence over runtimeconfig: `DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=<n>`. `0`
|
||||
disables the spin entirely.
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [Gateway Process Detailed Design](./GatewayProcessDesign.md)
|
||||
|
||||
@@ -479,6 +479,32 @@ Creation rules:
|
||||
- Never marshal the raw COM object to pipe reader/writer threads.
|
||||
- Capture COM creation HRESULT or exception details.
|
||||
|
||||
**Record-info pin.** Before the first `LMXProxyServerClass` is created,
|
||||
`MxAccessComObjectFactory.Create()` calls `MxStatusProxyRecordInfoPin.EnsurePinned()`. Once per
|
||||
process, this loads the LMXProxy type library and the `MXSTATUS_PROXY` record info, and holds
|
||||
both references until exit. The GUIDs and version come from the interop assembly's metadata:
|
||||
library `{77e896ec-b3e3-4de4-b96f-f32570164211}` v3.2 (`MXAccess32.tlb`), record
|
||||
`{A162D9E4-9019-42F3-A087-79264007E542}`.
|
||||
|
||||
The pin exists because every `OnDataChange` carries a SAFEARRAY of `MXSTATUS_PROXY` records, and
|
||||
`lmxproxy.dll` resolves that record type with `GetRecordInfoFromGuids` each time it builds one.
|
||||
With no live reference, oleaut32 re-reads the registry and reloads the type library on every
|
||||
call:
|
||||
|
||||
- about 1 ms per event on the dev box;
|
||||
- worse under WOW64 registry redirection plus an EDR registry filter, where a production WPR trace
|
||||
put it at ~70% of the STA's CPU.
|
||||
|
||||
While one `IRecordInfo` is held, the same call returns oleaut32's cached instance in under a
|
||||
microsecond. Measured at ~240 events/s on the dev box, worker CPU fell from 67% to 10% of a core
|
||||
with the pin alone.
|
||||
|
||||
The pin only warms a cache. It changes no MXAccess behaviour, and a failure is never fatal. The
|
||||
outcome is in `MxStatusProxyRecordInfoPin.Status`, and the worker logs it as `WorkerRecordInfoPin`
|
||||
just before `WorkerReady`. Worker logs go to stderr, which the gateway does not redirect, so the
|
||||
line lands wherever the host captures the gateway's stderr (the NSSM stderr log on deployed
|
||||
boxes).
|
||||
|
||||
If COM creation fails, the worker should send a structured fault with:
|
||||
|
||||
- fault category,
|
||||
@@ -564,9 +590,27 @@ everything that arrived — the loop must therefore re-check `DrainFault()` and
|
||||
re-drain after every wait, never treat a wake as "exactly one event". The 25 ms
|
||||
`EventDrainInterval` survives as the **fallback ceiling** on an unsignalled wait,
|
||||
not as a latency floor: an event arriving at an idle worker is framed at signal
|
||||
latency instead of waiting out a tick, an idle worker parks instead of waking 40
|
||||
times a second, and the interval only bounds how long the loop may sleep if some
|
||||
future path mutates the queue without signalling.
|
||||
latency instead of waiting out a tick, and the interval only bounds how long the
|
||||
loop may sleep if some future path mutates the queue without signalling. An idle
|
||||
worker still wakes at that ceiling, but a wake on an empty queue is cheap.
|
||||
|
||||
The loop runs on a **dedicated background thread** (`mxgw-event-drain`), not on the
|
||||
CLR thread pool:
|
||||
|
||||
- It parks in the blocking `IWorkerRuntimeSession.WaitForEvents`, which uses the
|
||||
same coalesced signal and single-waiter contract as `WaitForEventsAsync`.
|
||||
- It blocks on the frame writer's batch write.
|
||||
- `RunEventDrainLoopAsync` starts the thread and returns a task that completes,
|
||||
faults or cancels exactly as the loop does, so the message loop's `Task.WhenAny`
|
||||
supervision is unchanged.
|
||||
|
||||
The reason is .NET Framework's thread pool. As an async loop, every enqueue's
|
||||
semaphore release queued a continuation to the pool, and each pool worker that ran
|
||||
one then spun in `SleepEx` before parking. A production WPR trace measured ~150
|
||||
context switches per event and ~26% of the worker's CPU in that spin. With the
|
||||
dedicated thread and the frame writer's synchronous flush, the steady-state event
|
||||
path schedules no managed pool work. The overlapped pipe write still completes on
|
||||
an I/O completion thread.
|
||||
|
||||
A single event whose envelope exceeds the negotiated frame maximum is
|
||||
**undeliverable end to end** — the pipe maximum sits only the envelope-overhead
|
||||
|
||||
@@ -135,7 +135,7 @@ Two failure shapes are distinguished during a drain pass:
|
||||
|
||||
Flushes are coalesced across a *run of same-class frames* inside a drain
|
||||
pass: each frame in the run is written to the stream without an individual
|
||||
flush, then one `FlushAsync` runs — at the end of the pass, and additionally
|
||||
flush, then one flush runs — at the end of the pass, and additionally
|
||||
at every control-to-event boundary — and only then does every
|
||||
successfully-written frame of that run resolve its completion. A caller's
|
||||
`WriteAsync` therefore still does not complete until its bytes are both
|
||||
@@ -166,11 +166,18 @@ end-of-pass flush, and additionally fails the event frame the drain had
|
||||
already claimed off its queue (nothing else would ever complete it) along
|
||||
with every frame still queued.
|
||||
|
||||
The flush is a synchronous `Stream.Flush()`, not `FlushAsync`. `PipeStream` does not
|
||||
override `FlushAsync`, so on .NET Framework the base implementation runs `Flush()`
|
||||
through `Task.Factory.StartNew`. That is one thread-pool work item per drain pass for
|
||||
a `PipeStream.Flush` that does no I/O, because it never calls `FlushFileBuffers`.
|
||||
Calling `Flush()` inline keeps the same written-and-flushed contract for any stream.
|
||||
|
||||
Note the ordering all of this implies at the peer: the frames reach the pipe
|
||||
before the flush that follows them, so the gateway can read a whole batch
|
||||
while the writer has not yet flushed it. Anything observing the flush itself
|
||||
(a test counting flushes, for instance) must wait for the flush, not infer it
|
||||
from frames arriving. The event drain loop (`WorkerPipeSession.RunEventDrainLoopAsync`)
|
||||
from frames arriving. The event drain loop (`WorkerPipeSession.RunEventDrainLoopAsync`, which
|
||||
runs on a dedicated thread; see `MxAccessWorkerInstanceDesign.md`)
|
||||
submits a whole drained event batch through `WriteBatchAsync`, which enqueues
|
||||
every frame under one `_gate` acquisition, takes the write lock once, and
|
||||
drains them together, so a burst of N events costs one flush rather than N —
|
||||
|
||||
Reference in New Issue
Block a user