Skip to content

Instantly share code, notes, and snippets.

@russcam
Created August 24, 2020 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russcam/12a2f83e123d055dfa6520e95c130819 to your computer and use it in GitHub Desktop.
Save russcam/12a2f83e123d055dfa6520e95c130819 to your computer and use it in GitHub Desktop.
Get Threadpool, worker and IOCP thread statistics
using System;
using System.Text;
using System.Threading;
public static class ThreadpoolStats
{
public static string GetThreadPoolStats()
{
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoThreads);
ThreadPool.GetAvailableThreads(out int freeWorkerThreads, out int freeIoThreads);
ThreadPool.GetMinThreads(out int minWorkerThreads, out int minIoThreads);
int busyIoThreads = maxIoThreads - freeIoThreads;
int busyWorkerThreads = maxWorkerThreads - freeWorkerThreads;
var builder = new StringBuilder()
.AppendLine("Threadpool Statistics:")
.AppendLine($" Thread Count: {ThreadPool.ThreadCount}")
.AppendLine($" Pending WorkItem Count: {ThreadPool.PendingWorkItemCount}")
.AppendLine($" Completed WorkItem Count: {ThreadPool.CompletedWorkItemCount}")
.AppendLine(" IOCP threads:")
.AppendLine($" Busy: {busyIoThreads}")
.AppendLine($" Free: {freeIoThreads}")
.AppendLine($" Min: {minIoThreads}")
.AppendLine($" Max: {maxIoThreads}")
.AppendLine(" Worker threads:")
.AppendLine($" Busy: {busyWorkerThreads}")
.AppendLine($" Free: {freeWorkerThreads}")
.AppendLine($" Min: {minWorkerThreads}")
.AppendLine($" Max: {maxWorkerThreads}");
return builder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment