Skip to content

Increase the default number of IO queues on larger machines #56501

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

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/Servers/Kestrel/Transport.Sockets/src/Internal/IOQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;

internal sealed class IOQueue : PipeScheduler, IThreadPoolWorkItem
{
public static readonly int DefaultCount = DetermineDefaultCount();

private readonly ConcurrentQueue<Work> _workItems = new ConcurrentQueue<Work>();
private int _doingWork;

Expand Down Expand Up @@ -73,4 +75,19 @@ public Work(Action<object?> callback, object? state)
State = state;
}
}

private static int DetermineDefaultCount()
{
// Since each IOQueue schedules one work item to process its work, the number of IOQueues determines the maximum
// parallelism of processing work queued to IOQueues. The default number below is based on the processor count and tries
// to use a high-enough number for that to not be a significant limiting factor for throughput.

int processorCount = Environment.ProcessorCount;
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
int processorCount = Environment.ProcessorCount;
var processorCount = Environment.ProcessorCount;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't the explicit type preferred in this case? I thought that was the case in the runtime repo anyway, not sure about the guidelines in this repo.

Copy link
Member

Choose a reason for hiding this comment

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

Even if var is allowed in this repo, how is it better here?

Copy link
Member

Choose a reason for hiding this comment

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

"var all the things" is convention here (personally I prefer the explicit type, but to be consistent w/ the codebase -> var).

if (OperatingSystem.IsWindows() || processorCount <= 32)
{
return Math.Min(processorCount, 16);
}

return (processorCount + 1) / 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ internal SocketConnectionFactoryOptions(SocketTransportOptions transportOptions)
/// The number of I/O queues used to process requests. Set to 0 to directly schedule I/O to the ThreadPool.
/// </summary>
/// <remarks>
/// Defaults to <see cref="Environment.ProcessorCount" /> rounded down and clamped between 1 and 16.
/// Defaults to a value based on and limited to <see cref="Environment.ProcessorCount" />.
/// </remarks>
public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16);
public int IOQueueCount { get; set; } = Internal.IOQueue.DefaultCount;

/// <summary>
/// Wait until there is data available to allocate a buffer. Setting this to false can increase throughput at the cost of increased memory usage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ static SocketTransportOptions()
/// The number of I/O queues used to process requests. Set to 0 to directly schedule I/O to the ThreadPool.
/// </summary>
/// <remarks>
/// Defaults to <see cref="Environment.ProcessorCount" /> rounded down and clamped between 1 and 16.
/// Defaults to a value based on and limited to <see cref="Environment.ProcessorCount" />.
/// </remarks>
public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16);
public int IOQueueCount { get; set; } = Internal.IOQueue.DefaultCount;

/// <summary>
/// Wait until there is data available to allocate a buffer. Setting this to false can increase throughput at the cost of increased memory usage.
Expand Down
Loading