diff options
author | Justin Clark-Casey (justincc) | 2012-11-23 04:40:49 +0000 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2012-11-23 04:40:49 +0000 |
commit | 999cb4b20cc20a24c7b9fbce4c31b13f8bf36cb5 (patch) | |
tree | a73ecf998adad5920e94c570743e6897ae1a87f4 /OpenSim/Framework/Servers/ServerBase.cs | |
parent | Make "force gc" console command available across all servers (diff) | |
download | opensim-SC_OLD-999cb4b20cc20a24c7b9fbce4c31b13f8bf36cb5.zip opensim-SC_OLD-999cb4b20cc20a24c7b9fbce4c31b13f8bf36cb5.tar.gz opensim-SC_OLD-999cb4b20cc20a24c7b9fbce4c31b13f8bf36cb5.tar.bz2 opensim-SC_OLD-999cb4b20cc20a24c7b9fbce4c31b13f8bf36cb5.tar.xz |
Make "show threads" and "thread abort" console commands available on all servers
Diffstat (limited to 'OpenSim/Framework/Servers/ServerBase.cs')
-rw-r--r-- | OpenSim/Framework/Servers/ServerBase.cs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/ServerBase.cs b/OpenSim/Framework/Servers/ServerBase.cs index b8bc27e..47baac8 100644 --- a/OpenSim/Framework/Servers/ServerBase.cs +++ b/OpenSim/Framework/Servers/ServerBase.cs | |||
@@ -27,16 +27,19 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Diagnostics; | ||
30 | using System.IO; | 31 | using System.IO; |
31 | using System.Reflection; | 32 | using System.Reflection; |
32 | using System.Text; | 33 | using System.Text; |
33 | using System.Text.RegularExpressions; | 34 | using System.Text.RegularExpressions; |
35 | using System.Threading; | ||
34 | using log4net; | 36 | using log4net; |
35 | using log4net.Appender; | 37 | using log4net.Appender; |
36 | using log4net.Core; | 38 | using log4net.Core; |
37 | using log4net.Repository; | 39 | using log4net.Repository; |
38 | using Nini.Config; | 40 | using Nini.Config; |
39 | using OpenSim.Framework.Console; | 41 | using OpenSim.Framework.Console; |
42 | using OpenSim.Framework.Monitoring; | ||
40 | 43 | ||
41 | namespace OpenSim.Framework.Servers | 44 | namespace OpenSim.Framework.Servers |
42 | { | 45 | { |
@@ -211,6 +214,22 @@ namespace OpenSim.Framework.Servers | |||
211 | "Run a command script from file", HandleScript); | 214 | "Run a command script from file", HandleScript); |
212 | 215 | ||
213 | m_console.Commands.AddCommand( | 216 | m_console.Commands.AddCommand( |
217 | "General", false, "show threads", | ||
218 | "show threads", | ||
219 | "Show thread status", HandleShow); | ||
220 | |||
221 | m_console.Commands.AddCommand( | ||
222 | "General", false, "threads abort", | ||
223 | "threads abort <thread-id>", | ||
224 | "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort); | ||
225 | |||
226 | m_console.Commands.AddCommand( | ||
227 | "General", false, "threads show", | ||
228 | "threads show", | ||
229 | "Show thread status. Synonym for \"show threads\"", | ||
230 | (string module, string[] args) => Notice(GetThreadsReport())); | ||
231 | |||
232 | m_console.Commands.AddCommand( | ||
214 | "General", false, "force gc", | 233 | "General", false, "force gc", |
215 | "force gc", | 234 | "force gc", |
216 | "Manually invoke runtime garbage collection. For debugging purposes", | 235 | "Manually invoke runtime garbage collection. For debugging purposes", |
@@ -244,6 +263,10 @@ namespace OpenSim.Framework.Servers | |||
244 | case "uptime": | 263 | case "uptime": |
245 | Notice(GetUptimeReport()); | 264 | Notice(GetUptimeReport()); |
246 | break; | 265 | break; |
266 | |||
267 | case "threads": | ||
268 | Notice(GetThreadsReport()); | ||
269 | break; | ||
247 | } | 270 | } |
248 | } | 271 | } |
249 | 272 | ||
@@ -556,6 +579,75 @@ namespace OpenSim.Framework.Servers | |||
556 | } | 579 | } |
557 | 580 | ||
558 | /// <summary> | 581 | /// <summary> |
582 | /// Get a report about the registered threads in this server. | ||
583 | /// </summary> | ||
584 | protected string GetThreadsReport() | ||
585 | { | ||
586 | // This should be a constant field. | ||
587 | string reportFormat = "{0,6} {1,35} {2,16} {3,13} {4,10} {5,30}"; | ||
588 | |||
589 | StringBuilder sb = new StringBuilder(); | ||
590 | Watchdog.ThreadWatchdogInfo[] threads = Watchdog.GetThreadsInfo(); | ||
591 | |||
592 | sb.Append(threads.Length + " threads are being tracked:" + Environment.NewLine); | ||
593 | |||
594 | int timeNow = Environment.TickCount & Int32.MaxValue; | ||
595 | |||
596 | sb.AppendFormat(reportFormat, "ID", "NAME", "LAST UPDATE (MS)", "LIFETIME (MS)", "PRIORITY", "STATE"); | ||
597 | sb.Append(Environment.NewLine); | ||
598 | |||
599 | foreach (Watchdog.ThreadWatchdogInfo twi in threads) | ||
600 | { | ||
601 | Thread t = twi.Thread; | ||
602 | |||
603 | sb.AppendFormat( | ||
604 | reportFormat, | ||
605 | t.ManagedThreadId, | ||
606 | t.Name, | ||
607 | timeNow - twi.LastTick, | ||
608 | timeNow - twi.FirstTick, | ||
609 | t.Priority, | ||
610 | t.ThreadState); | ||
611 | |||
612 | sb.Append("\n"); | ||
613 | } | ||
614 | |||
615 | sb.Append("\n"); | ||
616 | |||
617 | // For some reason mono 2.6.7 returns an empty threads set! Not going to confuse people by reporting | ||
618 | // zero active threads. | ||
619 | int totalThreads = Process.GetCurrentProcess().Threads.Count; | ||
620 | if (totalThreads > 0) | ||
621 | sb.AppendFormat("Total threads active: {0}\n\n", totalThreads); | ||
622 | |||
623 | sb.Append("Main threadpool (excluding script engine pools)\n"); | ||
624 | sb.Append(Util.GetThreadPoolReport()); | ||
625 | |||
626 | return sb.ToString(); | ||
627 | } | ||
628 | |||
629 | public virtual void HandleThreadsAbort(string module, string[] cmd) | ||
630 | { | ||
631 | if (cmd.Length != 3) | ||
632 | { | ||
633 | MainConsole.Instance.Output("Usage: threads abort <thread-id>"); | ||
634 | return; | ||
635 | } | ||
636 | |||
637 | int threadId; | ||
638 | if (!int.TryParse(cmd[2], out threadId)) | ||
639 | { | ||
640 | MainConsole.Instance.Output("ERROR: Thread id must be an integer"); | ||
641 | return; | ||
642 | } | ||
643 | |||
644 | if (Watchdog.AbortThread(threadId)) | ||
645 | MainConsole.Instance.OutputFormat("Aborted thread with id {0}", threadId); | ||
646 | else | ||
647 | MainConsole.Instance.OutputFormat("ERROR - Thread with id {0} not found in managed threads", threadId); | ||
648 | } | ||
649 | |||
650 | /// <summary> | ||
559 | /// Console output is only possible if a console has been established. | 651 | /// Console output is only possible if a console has been established. |
560 | /// That is something that cannot be determined within this class. So | 652 | /// That is something that cannot be determined within this class. So |
561 | /// all attempts to use the console MUST be verified. | 653 | /// all attempts to use the console MUST be verified. |