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
+23
View File
@@ -250,6 +250,29 @@ own (the active node always answers Healthy), so the only event this can ever su
reporting that it is a standby. The verdict remains fully observable through the HTTP status of reporting that it is a standby. The verdict remains fully observable through the HTTP status of
`/health/active` itself, the readiness probe's cluster view, and the health dashboard. `/health/active` itself, the readiness probe's cluster view, and the health dashboard.
### Runtime thread-pool settings
Some settings belong to the .NET runtime, not to `ScadaBridge:*` options. They ship as
`RuntimeHostConfigurationOption` items in the Host csproj, which write them into
`ZB.MOM.WW.ScadaBridge.Host.runtimeconfig.json`. Publish carries that file, so the Windows service
install and the Docker image (`docker/Dockerfile` copies the publish output) get it with no extra
environment variables. `RuntimeConfigTests` pins it.
| 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. Akka's default dispatcher, gRPC/Kestrel and every async continuation run on the pool (`ScriptExecutionScheduler` has its own threads), so a steady trickle of small work items, such as MxGateway events, turns those spins into context switches. With `10`, idle workers spin briefly and then park. Mirror of mxaccessgw#237 (#146). |
What has been measured: on DEV vd03 at ~240 MxGateway events/s, a WPR trace showed 229k context
switches in the Host. That trace predates this setting and was not attributed by call stack here;
the attribution comes from the same pattern in the gateway. A synthetic local trickle benchmark (about 185
work-item chains per second, each a few pool hops, macOS, 3 interleaved runs per value) went from
17–28k to 6–13k involuntary context switches and from 430–560 ms to 200–295 ms CPU per 20 s at
`10`. The before/after WPR comparison on vd03 is still to be done at the next vd03 deploy.
To override on one host without rebuilding, set `DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=<n>`
in the service or container environment. It takes precedence over runtimeconfig. `70` restores
the runtime default, and `0` disables the spin entirely.
--- ---
## Secrets (2026-09-28) ## Secrets (2026-09-28)
@@ -11,6 +11,19 @@
<InternalsVisibleTo Include="ZB.MOM.WW.ScadaBridge.Host.Tests" /> <InternalsVisibleTo Include="ZB.MOM.WW.ScadaBridge.Host.Tests" />
</ItemGroup> </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> <ItemGroup>
<!-- Bootstrap is a hand-assembled, injection-safe HOCON document <!-- Bootstrap is a hand-assembled, injection-safe HOCON document
(AkkaHostedService.BuildHocon) + ActorSystem.Create, NOT Akka.Hosting's (AkkaHostedService.BuildHocon) + ActorSystem.Create, NOT Akka.Hosting's
@@ -0,0 +1,41 @@
using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
/// <summary>
/// Pins the .NET runtime settings the Host ships in its <c>*.runtimeconfig.json</c>
/// (Component-Host.md -> Runtime thread-pool settings). The Host's runtimeconfig is copied
/// next to this test assembly because the test project references the Host executable, so
/// the file read here is the one the build produces — the same file publish (and therefore
/// the Docker image and the Windows service install) carries.
/// </summary>
public class RuntimeConfigTests
{
private static JsonElement ReadHostConfigProperties()
{
var path = Path.Combine(AppContext.BaseDirectory, "ZB.MOM.WW.ScadaBridge.Host.runtimeconfig.json");
Assert.True(File.Exists(path), $"Host runtimeconfig not found at {path}");
using var doc = JsonDocument.Parse(File.ReadAllText(path));
return doc.RootElement
.GetProperty("runtimeOptions")
.GetProperty("configProperties")
.Clone();
}
/// <summary>
/// #146 (mirror of mxaccessgw#237): the thread pool's idle spin is capped at 10 steps
/// (runtime default 70) so pool workers that find no work park instead of yielding
/// with <c>Sleep(0)</c> dozens of times per work item.
/// </summary>
[Fact]
public void Host_runtimeconfig_caps_thread_pool_unfair_semaphore_spin_at_10()
{
var props = ReadHostConfigProperties();
Assert.True(
props.TryGetProperty("System.Threading.ThreadPool.UnfairSemaphoreSpinLimit", out var value),
"System.Threading.ThreadPool.UnfairSemaphoreSpinLimit missing from the Host runtimeconfig");
Assert.Equal(10, value.GetInt32());
}
}