perf(host): cap thread-pool spin (UnfairSemaphoreSpinLimit=10) (#146)

Mirror of mxaccessgw#237 (659bb831). DEV vd03 showed 229k context switches
in ZB.MOM.WW.ScadaBridge.Host at ~240 MxGateway events/s. Akka's default
dispatcher, gRPC/Kestrel and async continuations all run on the .NET thread
pool, so a trickle of small work items makes idle pool workers spin with
Sleep(0) (default limit 70) before parking.

- Host csproj: RuntimeHostConfigurationOption
  System.Threading.ThreadPool.UnfairSemaphoreSpinLimit=10, written into
  ZB.MOM.WW.ScadaBridge.Host.runtimeconfig.json (verified in the publish
  output, which docker/Dockerfile copies into the image).
- RuntimeConfigTests pins the value in the built runtimeconfig.
- Component-Host.md: new "Runtime thread-pool settings" section (what, why,
  override/revert via DOTNET_ThreadPool_UnfairSemaphoreSpinLimit).

Local synthetic trickle bench (macOS, ~185 chains/s): involuntary context
switches 17-28k -> 6-13k, CPU 430-560 -> 200-295 ms per 20 s. The vd03
before/after WPR measurement is pending the next vd03 deploy.
This commit is contained in:
Joseph Doherty
2026-11-28 09:01:52 -05:00
parent d3067ca036
commit b47002b45e
3 changed files with 77 additions and 0 deletions
@@ -11,6 +11,19 @@
<InternalsVisibleTo Include="ZB.MOM.WW.ScadaBridge.Host.Tests" />
</ItemGroup>
<ItemGroup>
<!--
Cap the thread pool's idle spin (#146, mirror of mxaccessgw#237). The default (70 steps,
Sleep(0) on every other step past the tenth) makes a pool worker that finds no work yield
~30 times before parking. Akka dispatchers, gRPC/Kestrel and every async continuation run on
the pool, so a steady trickle of small work items (MxGateway events) turns those yields into
context switches. 10 keeps a short busy-spin and then parks. Lands in the published
*.runtimeconfig.json (Windows service and Docker image alike); override per host with
DOTNET_ThreadPool_UnfairSemaphoreSpinLimit. See Component-Host.md -> Runtime thread-pool settings.
-->
<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.UnfairSemaphoreSpinLimit" Value="10" />
</ItemGroup>
<ItemGroup>
<!-- Bootstrap is a hand-assembled, injection-safe HOCON document
(AkkaHostedService.BuildHocon) + ActorSystem.Create, NOT Akka.Hosting's