ScadaBridge: cap .NET thread-pool spin (UnfairSemaphoreSpinLimit) #237

Closed
opened 2026-11-21 08:01:08 -05:00 by dohertj2 · 3 comments
Owner

Cross-repo item from the vd03 CPU investigation. In the WPR trace on wonder-app-vd03, ZB.MOM.WW.ScadaBridge.Host had 229k context switches — almost as many as MxGateway.Server (240k), and more than the worker. In the gateway, 93% of switches were the .NET PortableThreadPool LowLevelLifoSemaphore spin-waiting with Sleep(0) yields after each per-event work item; ScadaBridge consumes the same event stream and likely has the same pattern.

mxaccessgw fixed its side in 659bb831 with
<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.UnfairSemaphoreSpinLimit" Value="10" /> in the Server csproj (env form DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=10). windev bench: gateway CPU 8.1% → 6.9% of a core at ~242 ev/s.

To do (in ScadaBridge):

  • Confirm with a profile or counters that the ScadaBridge host's switches come from the same pool spin.
  • Apply the same runtimeconfig setting to the ScadaBridge host, measure, and document it.
  • Optionally review the MxGateway adapter's per-event hops (the same channel / continuation pattern as the gateway's relay).
Cross-repo item from the vd03 CPU investigation. In the WPR trace on wonder-app-vd03, `ZB.MOM.WW.ScadaBridge.Host` had 229k context switches — almost as many as `MxGateway.Server` (240k), and more than the worker. In the gateway, 93% of switches were the .NET PortableThreadPool `LowLevelLifoSemaphore` spin-waiting with `Sleep(0)` yields after each per-event work item; ScadaBridge consumes the same event stream and likely has the same pattern. mxaccessgw fixed its side in 659bb831 with `<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.UnfairSemaphoreSpinLimit" Value="10" />` in the Server csproj (env form `DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=10`). windev bench: gateway CPU 8.1% → 6.9% of a core at ~242 ev/s. To do (in ScadaBridge): - Confirm with a profile or counters that the ScadaBridge host's switches come from the same pool spin. - Apply the same runtimeconfig setting to the ScadaBridge host, measure, and document it. - Optionally review the MxGateway adapter's per-event hops (the same channel / continuation pattern as the gateway's relay).
dohertj2 added the priority:p2type:followupcross-repo labels 2026-11-21 08:01:08 -05:00
dohertj2 changed title from ScadaBridge: cap .NET thread-pool spin (UnfairSemaphoreSpinLimit) — ScadaBridge#? to ScadaBridge: cap .NET thread-pool spin (UnfairSemaphoreSpinLimit) 2026-11-21 08:01:12 -05:00
Author
Owner

Mirrored in ScadaBridge#146.

Mirrored in ScadaBridge#146.
Author
Owner

Gateway-side numbers to compare against when ScadaBridge measures: with the spin limit, the mxgw gateway went 8.1% → 6.9% of a core on windev. Removing the relay hops (#235, branch feat/gateway-relay-hops) took it a further −17.5%. If ScadaBridge's MxGateway adapter shows the same pattern, the same two patterns apply: synchronous continuations on the internal channel hand-off, and not waking idle consumers per event.

Gateway-side numbers to compare against when ScadaBridge measures: with the spin limit, the mxgw gateway went 8.1% → 6.9% of a core on windev. Removing the relay hops (#235, branch `feat/gateway-relay-hops`) took it a further −17.5%. If ScadaBridge's MxGateway adapter shows the same pattern, the same two patterns apply: synchronous continuations on the internal channel hand-off, and not waking idle consumers per event.
Author
Owner

Done on the ScadaBridge side. ScadaBridge#146 is closed. ScadaBridge main b47002b4 adds UnfairSemaphoreSpinLimit=10 to the Host csproj, with a RuntimeConfigTests guard and a "Runtime thread-pool settings" section in docs/requirements/Component-Host.md. Host is the only ScadaBridge process that consumes the MxGateway event stream.

Adapter review: no relay-hop pattern. RealMxGatewayClient runs one await foreach over StreamEventsAsync and invokes callbacks inline. There are no internal channels and no per-event Task.Run, only the inherent Akka Tell into DataConnectionActor. The mxgw .NET client (clients/dotnet) has no Channel.Create/Task.Run on the stream path either.

Remaining: measure the context-switch reduction on vd03 once a ScadaBridge build with b47002b4 is deployed. That belongs with the normal ScadaBridge deploy, not this issue. Closing.

Done on the ScadaBridge side. ScadaBridge#146 is closed. ScadaBridge `main` `b47002b4` adds `UnfairSemaphoreSpinLimit=10` to the Host csproj, with a `RuntimeConfigTests` guard and a "Runtime thread-pool settings" section in `docs/requirements/Component-Host.md`. Host is the only ScadaBridge process that consumes the MxGateway event stream. Adapter review: no relay-hop pattern. `RealMxGatewayClient` runs one `await foreach` over `StreamEventsAsync` and invokes callbacks inline. There are no internal channels and no per-event `Task.Run`, only the inherent Akka `Tell` into `DataConnectionActor`. The mxgw .NET client (`clients/dotnet`) has no `Channel.Create`/`Task.Run` on the stream path either. Remaining: measure the context-switch reduction on vd03 once a ScadaBridge build with `b47002b4` is deployed. That belongs with the normal ScadaBridge deploy, not this issue. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#237