Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Including background worker threads in the status response.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreycoto committed Mar 19, 2015
1 parent 6256d18 commit 6dae437
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/mindtouch.core/DreamHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,17 +690,21 @@ private Yield GetStatus(DreamContext context, DreamMessage request, Result<Dream
int workerThreads;
int completionThreads;
int dispatcherThreads;
AsyncUtil.GetAvailableThreads(out workerThreads, out completionThreads, out dispatcherThreads);
int backgroundWorkerThreads;
AsyncUtil.GetAvailableThreads(out workerThreads, out completionThreads, out dispatcherThreads, out backgroundWorkerThreads);
int maxWorkerThreads;
int maxCompletionThreads;
int maxDispatcherThreads;
AsyncUtil.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads, out maxDispatcherThreads);
int maxBackgroundWorkerThreads;
AsyncUtil.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads, out maxDispatcherThreads, out maxBackgroundWorkerThreads);
result.Elem("workerthreads.max", maxWorkerThreads);
result.Elem("workerthreads.used", maxWorkerThreads - workerThreads);
result.Elem("completionthreads.max", maxCompletionThreads);
result.Elem("completionthreads.used", maxCompletionThreads - completionThreads);
result.Elem("dispatcherthreads.max", maxDispatcherThreads);
result.Elem("dispatcherthreads.used", maxDispatcherThreads - dispatcherThreads);
result.Elem("backgroundworkerthreads.max", maxBackgroundWorkerThreads);
result.Elem("backgroundworkerthreads.used", maxBackgroundWorkerThreads - backgroundWorkerThreads);

// timer information
var taskTimerStats = TaskTimerFactory.GetStatistics();
Expand Down Expand Up @@ -1067,7 +1071,8 @@ public void Initialize(XDoc config) {
int maxThreads;
int maxPorts;
int maxDispatchers;
AsyncUtil.GetMaxThreads(out maxThreads, out maxPorts, out maxDispatchers);
int maxBackgroundThreads;
AsyncUtil.GetMaxThreads(out maxThreads, out maxPorts, out maxDispatchers, out maxBackgroundThreads);
if(maxDispatchers > 0) {
_connectionLimit = maxDispatchers + _connectionLimit;
} else {
Expand Down
30 changes: 20 additions & 10 deletions src/mindtouch.dream/Tasking/AsyncUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static class AsyncUtil {

//--- Types ---
private delegate void AvailableThreadsDelegate(out int availableThreads, out int availablePorts);
private delegate void AvailableBackgroundThreadsDelegate(out int availableThreads);

private class BoxedObject {

Expand All @@ -87,7 +88,10 @@ private class BoxedObject {
private static readonly int _maxThreads;
private static readonly int _minPorts;
private static readonly int _maxPorts;
private static readonly int _minBackgroundThreads;
private static readonly int _maxBackgroundThreads;
private static readonly AvailableThreadsDelegate _availableThreadsCallback;
private static readonly AvailableBackgroundThreadsDelegate _availableBackgroundThreadsCallback;
private static readonly int? _maxStackSize;
private static readonly Dictionary<int, KeyValuePair<Thread, BoxedObject>> _threads = new Dictionary<int, KeyValuePair<Thread, BoxedObject>>();

Expand All @@ -113,13 +117,11 @@ static AsyncUtil() {
}

// Background Thread Pool
int minBackgroundThreads;
if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["background-threadpool-min"], out minBackgroundThreads)) {
minBackgroundThreads = 4;
if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["background-threadpool-min"], out _minBackgroundThreads)) {
_minBackgroundThreads = 4;
}
int maxBackgroundThreads;
if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["background-threadpool-max"], out maxBackgroundThreads)) {
maxBackgroundThreads = 20;
if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["background-threadpool-max"], out _maxBackgroundThreads)) {
_maxBackgroundThreads = 20;
}

// check which global dispatch queue implementation to use
Expand All @@ -130,16 +132,20 @@ static AsyncUtil() {
ThreadPool.GetMinThreads(out dummy, out _minPorts);
ThreadPool.GetMaxThreads(out dummy, out _maxPorts);
_log.DebugFormat("Using Global ElasticThreadPool with {0}min / {1}max", _minThreads, _maxThreads);
_log.DebugFormat("Using Background ElasticThreadPool with {0}min / {1}max", minBackgroundThreads, maxBackgroundThreads);
_log.DebugFormat("Using Background ElasticThreadPool with {0}min / {1}max", _minBackgroundThreads, _maxBackgroundThreads);
var elasticThreadPool = new ElasticThreadPool(_minThreads, _maxThreads);
GlobalDispatchQueue = elasticThreadPool;
_backgroundDispatchQueue = new ElasticThreadPool(minBackgroundThreads, maxBackgroundThreads);
var backgroundThreadPool = new ElasticThreadPool(_minBackgroundThreads, _maxBackgroundThreads);
_backgroundDispatchQueue = backgroundThreadPool;
_inplaceActivation = false;
_availableThreadsCallback = delegate(out int threads, out int ports) {
int dummy2;
ThreadPool.GetAvailableThreads(out dummy2, out ports);
threads = elasticThreadPool.MaxParallelThreads - elasticThreadPool.ThreadCount;
};
_availableBackgroundThreadsCallback = delegate(out int threads) {
threads = backgroundThreadPool.MaxParallelThreads - backgroundThreadPool.ThreadCount;
};
break;
case "legacy":
ThreadPool.GetMinThreads(out dummy, out _minPorts);
Expand Down Expand Up @@ -207,10 +213,12 @@ public static object ThreadInfo {
/// <param name="threads">Number of threads allowed.</param>
/// <param name="ports">Number of completion ports allowed.</param>
/// <param name="dispatchers">Number of dispatchers allowed.</param>
public static void GetMaxThreads(out int threads, out int ports, out int dispatchers) {
/// <param name="backgroundThreads">Number of background threads allowed.</param>
public static void GetMaxThreads(out int threads, out int ports, out int dispatchers, out int backgroundThreads) {
threads = _maxThreads;
ports = _maxPorts;
dispatchers = DispatchThreadScheduler.MaxThreadCount;
backgroundThreads = _maxBackgroundThreads;
}

/// <summary>
Expand All @@ -219,9 +227,11 @@ public static void GetMaxThreads(out int threads, out int ports, out int dispatc
/// <param name="threads">Minimum number of threads allocated.</param>
/// <param name="ports">Minimum number of completion ports allocated.</param>
/// <param name="dispatchers">Minimum number of dispatchers allocated.</param>
public static void GetAvailableThreads(out int threads, out int ports, out int dispatchers) {
/// <param name="backgroundThreads">Minimum number of background threads allocated.</param>
public static void GetAvailableThreads(out int threads, out int ports, out int dispatchers, out int backgroundThreads) {
_availableThreadsCallback(out threads, out ports);
dispatchers = DispatchThreadScheduler.AvailableThreadCount;
_availableBackgroundThreadsCallback(out backgroundThreads);
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions src/mindtouch.host/ConsoleHostProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,17 @@ private static int Main(string[] args) {
int workerThreads;
int completionThreads;
int dispatcherThreads;
AsyncUtil.GetAvailableThreads(out workerThreads, out completionThreads, out dispatcherThreads);
int backgroundThreads;
AsyncUtil.GetAvailableThreads(out workerThreads, out completionThreads, out dispatcherThreads, out backgroundThreads);
int maxWorkerThreads;
int maxCompletionThreads;
int maxDispatcherThreads;
AsyncUtil.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads, out maxDispatcherThreads);
int maxBackgroundThreads;
AsyncUtil.GetMaxThreads(out maxWorkerThreads, out maxCompletionThreads, out maxDispatcherThreads, out maxBackgroundThreads);
Console.WriteLine("Thread-pool worker threads available: {0} (max: {1})", workerThreads, maxWorkerThreads);
Console.WriteLine("Thread-pool completion threads available: {0} (max: {1})", completionThreads, maxCompletionThreads);
Console.WriteLine("Dispatcher threads available: {0} (max: {1})", dispatcherThreads, maxDispatcherThreads);
Console.WriteLine("Thread-pool background worker threads available: {0} (max: {1})", backgroundThreads, maxBackgroundThreads);

// show pending/waiting timers
var taskTimerStats = Tasking.TaskTimerFactory.GetStatistics();
Expand Down

0 comments on commit 6dae437

Please sign in to comment.