From 75cfe2c3bbcf736f28e93377548668867a6d6abe Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Mon, 7 Dec 2015 14:45:35 +0100 Subject: [PATCH] Moved GetThreadCount into KestrelServerInformation --- .../KestrelServerInformation.cs | 28 +++++++++++++++++-- .../ServerFactory.cs | 27 +----------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs index ec31f4fbf6..9619df7c1d 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs @@ -11,10 +11,10 @@ namespace Microsoft.AspNet.Server.Kestrel { public class KestrelServerInformation : IKestrelServerInformation, IServerAddressesFeature { - public KestrelServerInformation(IConfiguration configuration, int threadCount) + public KestrelServerInformation(IConfiguration configuration) { Addresses = GetAddresses(configuration); - ThreadCount = threadCount; + ThreadCount = GetThreadCount(configuration); NoDelay = true; } @@ -39,5 +39,29 @@ namespace Microsoft.AspNet.Server.Kestrel return addresses; } + + private static int GetThreadCount(IConfiguration configuration) + { + // 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 + return 1; + } + + 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... + return 16; + } + + return threadCount; + } } } diff --git a/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs b/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs index 284823b849..d7f95ce25c 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs @@ -27,36 +27,11 @@ namespace Microsoft.AspNet.Server.Kestrel public IServer CreateServer(IConfiguration configuration) { - var threadCount = GetThreadCount(); - var information = new KestrelServerInformation(configuration, threadCount); + var information = new KestrelServerInformation(configuration); var serverFeatures = new FeatureCollection(); serverFeatures.Set(information); serverFeatures.Set(information); return new KestrelServer(serverFeatures, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNet.Server.Kestrel")); } - - private static int GetThreadCount() - { - // 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 - return 1; - } - - 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... - return 16; - } - - return threadCount; - } } }