From a95318c616e58786843f0a7e938e50317a751fee Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 13 Nov 2015 08:23:13 +0000 Subject: [PATCH] Network thread count defaults --- .../KestrelServer.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelServer.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelServer.cs index ff0ca62422..346808ebc4 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelServer.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelServer.cs @@ -75,14 +75,37 @@ namespace Microsoft.AspNet.Server.Kestrel _disposables.Push(engine); _disposables.Push(dateHeaderValueManager); + // Actual core count would be a better number + // rather than logical cores which includes hyper-threaded cores. + // Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving). + // Can be user overriden using IKestrelServerInformation.ThreadCount + var threadCount = Environment.ProcessorCount >> 1; + + if (threadCount < 1) + { + // Ensure shifted value is at least one + threadCount = 1; + } + else if (threadCount > 16) + { + // Receive Side Scaling RSS Processor count currently maxes out at 16 + // would be better to check the NIC's current hardware queues; but xplat... + threadCount = 16; + } + if (information.ThreadCount < 0) { throw new ArgumentOutOfRangeException(nameof(information.ThreadCount), information.ThreadCount, "ThreadCount cannot be negative"); } + else if (information.ThreadCount > 0) + { + // ThreadCount has been user set, use that value + threadCount = information.ThreadCount; + } - engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount); + engine.Start(threadCount); var atLeastOneListener = false; foreach (var address in information.Addresses)