Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single epoll thread per 28 cores #35800

Merged
merged 15 commits into from
May 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,29 @@ public bool TryRegister(SafeSocketHandle socket, out Interop.Error error)

private static int GetEnginesCount()
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
// The responsibility of SocketAsyncEngine is to get notifications from epoll|kqueue
// and schedule corresponding work items to ThreadPool (socket reads and writes).
//
// Using TechEmpower benchmarks that generate a LOT of SMALL socket reads and writes under a VERY HIGH load
// we have observed that a single engine is capable of keeping busy up to thirty x64 and eight ARM64 CPU Cores.
//
// The vast majority of real-life scenarios is never going to generate such a huge load (hundreds of thousands of requests per second)
// and having a single producer should be almost always enough.
//
// We want to be sure that we can handle extreme loads and that's why we have decided to use these values.
//
// It's impossible to predict all possible scenarios so we have added a possibility to configure this value using environment variables.
if (uint.TryParse(Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_SOCKETS_THREAD_COUNT"), out uint count))
{
return (int)count;
}

// the data that stands behind this heuristic can be found at https://github.com/dotnet/runtime/pull/35800#issuecomment-624719500
// the goal is to have a single epoll thread per every 28 cores
const int coresPerSingleEpollThread = 28;
int result = Environment.ProcessorCount / coresPerSingleEpollThread;

// and "round" it up, in a way that 29 cores gets 2 epoll threads
if (Environment.ProcessorCount % coresPerSingleEpollThread != 0)
{
result += 1;
}

// and double that for ARM where some operations like memory barriers are more expensive and we need more producer threads
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64 || RuntimeInformation.ProcessArchitecture == Architecture.Arm)
{
result *= 2;
}
Architecture architecture = RuntimeInformation.ProcessArchitecture;
int coresPerEngine = architecture == Architecture.Arm64 || architecture == Architecture.Arm
? 8
: 30;

return Math.Min(result, Environment.ProcessorCount / 2);
return Math.Max(1, (int)Math.Round(Environment.ProcessorCount / (double)coresPerEngine));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and "round" it up, in a way that 29 cores gets 2 epoll threads

So now anyting below 44 cores on x64 will get 1 thread? Then 2 after 76 cores ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and this should be enough for vast majorty of real-life scenarios.

}

//
Expand Down