Skip to content

Commit

Permalink
We really want to use system threads because ideally this all becomes…
Browse files Browse the repository at this point in the history
… cases where we use the language construct async/await and we dont want conflicts. Also some of the stats interfaces in use are deprecated and no sense in fixing them.
  • Loading branch information
mdickson committed Nov 16, 2024
1 parent c75a30c commit 08197ec
Show file tree
Hide file tree
Showing 63 changed files with 3 additions and 7,156 deletions.
10 changes: 0 additions & 10 deletions Source/OpenSim.Framework.Monitoring/ServerStatsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,6 @@ public void RegisterServerStats()
s.Value = iocpThreads;
});

if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool && Util.GetSmartThreadPoolInfo() != null)
{
MakeStat("STPMaxThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MaxThreads);
MakeStat("STPMinThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MinThreads);
MakeStat("STPConcurrency", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MaxConcurrentWorkItems);
MakeStat("STPActiveThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().ActiveThreads);
MakeStat("STPInUseThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().InUseThreads);
MakeStat("STPWorkItemsWaiting", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().WaitingCallbacks);
}

MakeStat(
"HTTPRequestsMade",
"Number of outbound HTTP requests made",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public override OSDMap OReport(string uptime, string version, string scene)
else
args["Error"] = "No Region data";

args["Util Thread Count"] = OSD.FromString(String.Format("{0:0.##}", Util.GetSmartThreadPoolInfo().InUseThreads));
args["System Thread Count"] = OSD.FromString(String.Format("{0:0.##}", numberThreads));
args["System Thread Active"] = OSD.FromString(String.Format("{0:0.##}", numberThreadsRunning));
args["ProcMem"] = OSD.FromString(String.Format("{0:0.##}", memUsage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections;
using System.Threading;
using System.Reflection;
using log4net;
using OpenSim.Framework.Monitoring;
using Amib.Threading;
using System.Collections.Generic;
using System.Collections.Concurrent;

namespace OpenSim.Framework.Servers.HttpServer
Expand Down
1 change: 0 additions & 1 deletion Source/OpenSim.Framework.Servers/BaseOpenSimServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ protected override void ShutdownSpecific()
MainServer.Stop();

Thread.Sleep(500);
Util.StopThreadPool();
WorkManager.Stop();

RemovePIDFile();
Expand Down
16 changes: 0 additions & 16 deletions Source/OpenSim.Framework.Servers/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,22 +890,6 @@ public static string GetThreadPoolReport()
int allocatedThreads = 0;
int inUseThreads = 0;
int waitingCallbacks = 0;

if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
{
STPInfo stpi = Util.GetSmartThreadPoolInfo();

// ROBUST currently leaves this the FireAndForgetMethod but never actually initializes the threadpool.
if (stpi != null)
{
threadPoolUsed = "SmartThreadPool";
maxThreads = stpi.MaxThreads;
minThreads = stpi.MinThreads;
inUseThreads = stpi.InUseThreads;
allocatedThreads = stpi.ActiveThreads;
waitingCallbacks = stpi.WaitingCallbacks;
}
}

if (threadPoolUsed != null)
{
Expand Down
3 changes: 0 additions & 3 deletions Source/OpenSim.Framework/OpenSim.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
<HintPath>..\..\Library\XMLRPC.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\ThirdParty\SmartThreadPool\SmartThreadPool.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="3.0.1" />
<PackageReference Include="Mono.Addins" Version="1.4.1" />
Expand Down
102 changes: 3 additions & 99 deletions Source/OpenSim.Framework/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
using Nwc.XmlRpc;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
using Amib.Threading;
using System.Collections.Concurrent;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -113,29 +112,9 @@ public enum FireAndForgetMethod
None,
RegressionTest,
QueueUserWorkItem,
SmartThreadPool,
Thread
}

/// <summary>
/// Class for delivering SmartThreadPool statistical information
/// </summary>
/// <remarks>
/// We do it this way so that we do not directly expose STP.
/// </remarks>
public class STPInfo
{
public string Name;
public bool IsIdle;
public bool IsShuttingDown;
public int MaxThreads;
public int MinThreads;
public int InUseThreads;
public int ActiveThreads;
public int WaitingCallbacks;
public int MaxConcurrentWorkItems;
}

/// <summary>
/// Miscellaneous utility functions
/// </summary>
Expand Down Expand Up @@ -192,11 +171,6 @@ static Util()
MaxCharactersInDocument = 10_000_000
};

/// <summary>
/// Thread pool used for Util.FireAndForget if FireAndForgetMethod.SmartThreadPool is used
/// </summary>
private static SmartThreadPool m_ThreadPool;

// Watchdog timer that aborts threads that have timed-out
private static Timer m_threadPoolWatchdog;

Check warning on line 175 in Source/OpenSim.Framework/Util.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

The field 'Util.m_threadPoolWatchdog' is never used

Check warning on line 175 in Source/OpenSim.Framework/Util.cs

View workflow job for this annotation

GitHub Actions / build (9.0.x)

The field 'Util.m_threadPoolWatchdog' is never used

Expand All @@ -209,7 +183,7 @@ private static readonly string rawUUIDPattern
public static readonly Regex PermissiveUUIDPattern = new(rawUUIDPattern);
public static readonly Regex UUIDPattern = new(string.Format("^{0}$", rawUUIDPattern));

public static FireAndForgetMethod DefaultFireAndForgetMethod = FireAndForgetMethod.SmartThreadPool;
public static FireAndForgetMethod DefaultFireAndForgetMethod = FireAndForgetMethod.Thread;
public static FireAndForgetMethod FireAndForgetMethod = DefaultFireAndForgetMethod;

public static readonly string UUIDZeroString = UUID.Zero.ToString();
Expand Down Expand Up @@ -3245,33 +3219,6 @@ public static void DisableTimerThrottling()

#region FireAndForget Threading Pattern

public static void InitThreadPool(int minThreads, int maxThreads)
{
if (maxThreads < 2)
throw new ArgumentOutOfRangeException(nameof(maxThreads), "maxThreads must be greater than 2");

if (minThreads > maxThreads || minThreads < 2)
throw new ArgumentOutOfRangeException(nameof(minThreads), "minThreads must be greater than 2 and less than or equal to maxThreads");

if (m_ThreadPool != null)
{
m_log.Warn("SmartThreadPool is already initialized. Ignoring request.");
return;
}

STPStartInfo startInfo = new()
{
ThreadPoolName = "Util",
IdleTimeout = 20000,
MaxWorkerThreads = maxThreads,
MinWorkerThreads = minThreads,
SuppressFlow = true
};

m_ThreadPool = new SmartThreadPool(startInfo);
m_threadPoolWatchdog = new Timer(ThreadPoolWatchdog, null, 0, 1000);
}

public static int FireAndForgetCount()
{
const int MAX_SYSTEM_THREADS = 200;
Expand All @@ -3281,13 +3228,13 @@ public static int FireAndForgetCount()
case FireAndForgetMethod.QueueUserWorkItem:
ThreadPool.GetAvailableThreads(out int workerThreads, out _);
return workerThreads;
case FireAndForgetMethod.SmartThreadPool:
return m_ThreadPool.MaxThreads - m_ThreadPool.InUseThreads;

case FireAndForgetMethod.Thread:
{
using Process p = System.Diagnostics.Process.GetCurrentProcess();
return MAX_SYSTEM_THREADS - p.Threads.Count;
}

default:
throw new NotImplementedException();
}
Expand All @@ -3303,8 +3250,6 @@ private class ThreadInfo
public string StackTrace { get; set; }
private readonly string context;
public bool LogThread { get; set; }

public IWorkItemResult WorkItem { get; set; }
public Thread Thread { get; set; }
public bool Running { get; set; }
public bool Aborted { get; set; }
Expand Down Expand Up @@ -3342,7 +3287,6 @@ public int Elapsed()
public void Abort()
{
Aborted = true;
WorkItem.Cancel(true);
}

/// <summary>
Expand Down Expand Up @@ -3504,11 +3448,6 @@ public static void FireAndForget(System.Threading.WaitCallback callback, object
case FireAndForgetMethod.QueueUserWorkItem:
ThreadPool.UnsafeQueueUserWorkItem(realCallback, obj);
break;
case FireAndForgetMethod.SmartThreadPool:
if (m_ThreadPool == null)
InitThreadPool(2, 15);
threadInfo.WorkItem = m_ThreadPool.QueueWorkItem(realCallback, obj);
break;
case FireAndForgetMethod.Thread:
Thread thread = new(delegate (object o) { realCallback(o); realCallback = null; });
thread.Start(obj);
Expand Down Expand Up @@ -3674,41 +3613,6 @@ can cause deadlocks etc.
*/
}

/// <summary>
/// Get information about the current state of the smart thread pool.
/// </summary>
/// <returns>
/// null if this isn't the pool being used for non-scriptengine threads.
/// </returns>
public static STPInfo GetSmartThreadPoolInfo()
{
if (m_ThreadPool == null)
return null;

return new STPInfo()
{
Name = m_ThreadPool.Name,
IsIdle = m_ThreadPool.IsIdle,
IsShuttingDown = m_ThreadPool.IsShuttingdown,
MaxThreads = m_ThreadPool.MaxThreads,
MinThreads = m_ThreadPool.MinThreads,
InUseThreads = m_ThreadPool.InUseThreads,
ActiveThreads = m_ThreadPool.ActiveThreads,
WaitingCallbacks = m_ThreadPool.WaitingCallbacks,
MaxConcurrentWorkItems = m_ThreadPool.Concurrency
};
}

public static void StopThreadPool()
{
if (m_ThreadPool == null)
return;
SmartThreadPool pool = m_ThreadPool;
m_ThreadPool = null;

try { pool.Shutdown(); } catch { }
}

#endregion FireAndForget Threading Pattern

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Reflection;
using OpenSim.Framework;
using OpenSim.Region.Framework.Scenes;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.ScriptEngine.Interfaces;
using OpenSim.Region.ScriptEngine.Shared;
using Amib.Threading;
using log4net;
using Nini.Config;
using OpenMetaverse;

Expand Down
1 change: 0 additions & 1 deletion Source/OpenSim.Server.Base/ServicesServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ protected override void ShutdownSpecific()
Watchdog.Enabled = false;
WorkManager.Stop();
RemovePIDFile();
Util.StopThreadPool();

Environment.Exit(0);
}
Expand Down
3 changes: 0 additions & 3 deletions Source/OpenSim.Server.RegionServer/OpenSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ protected override void ReadExtraConfigSettings()
try { ServicePointManager.DnsRefreshTimeout = dnsTimeout; } catch { }
}

if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
Util.InitThreadPool(stpMinThreads, stpMaxThreads);

m_log.Info("[OPENSIM MAIN]: Using async_call_method " + Util.FireAndForgetMethod);

m_log.InfoFormat("[OPENSIM MAIN] Running GC in {0} mode", GCSettings.IsServerGC ? "server":"workstation");
Expand Down
2 changes: 0 additions & 2 deletions Source/OpenSim.Server.RobustServer/ServerMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ public static int Main(string[] args)

m_Server?.Shutdown();

Util.StopThreadPool();

Environment.Exit(res);

return 0;
Expand Down
14 changes: 0 additions & 14 deletions ThirdParty/SmartThreadPool/CanceledWorkItemsGroup.cs

This file was deleted.

68 changes: 0 additions & 68 deletions ThirdParty/SmartThreadPool/Exceptions.cs

This file was deleted.

Loading

0 comments on commit 08197ec

Please sign in to comment.