BaseOpenSimServer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Timers;
  34. using log4net;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Statistics;
  37. namespace OpenSim.Framework.Servers
  38. {
  39. /// <summary>
  40. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  41. /// </summary>
  42. public abstract class BaseOpenSimServer
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  47. /// server.
  48. /// </summary>
  49. private System.Timers.Timer m_periodicDiagnosticsTimer = new System.Timers.Timer(60 * 60 * 1000);
  50. protected ConsoleBase m_console;
  51. /// <summary>
  52. /// Time at which this server was started
  53. /// </summary>
  54. protected DateTime m_startuptime;
  55. /// <summary>
  56. /// Record the initial startup directory for info purposes
  57. /// </summary>
  58. protected string m_startupDirectory = Environment.CurrentDirectory;
  59. /// <summary>
  60. /// Server version information. Usually VersionInfo + information about svn revision, operating system, etc.
  61. /// </summary>
  62. protected string m_version;
  63. protected BaseHttpServer m_httpServer;
  64. public BaseHttpServer HttpServer
  65. {
  66. get { return m_httpServer; }
  67. }
  68. /// <summary>
  69. /// Holds the non-viewer statistics collection object for this service/server
  70. /// </summary>
  71. protected IStatsCollector m_stats;
  72. public BaseOpenSimServer()
  73. {
  74. m_startuptime = DateTime.Now;
  75. m_version = VersionInfo.Version;
  76. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  77. m_periodicDiagnosticsTimer.Enabled = true;
  78. }
  79. /// <summary>
  80. /// Print statistics to the logfile, if they are active
  81. /// </summary>
  82. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  83. {
  84. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  85. sb.Append(GetUptimeReport());
  86. if (m_stats != null)
  87. {
  88. sb.Append(m_stats.Report());
  89. }
  90. m_log.Debug(sb);
  91. }
  92. /// <summary>
  93. /// Return a report about the uptime of this server
  94. /// </summary>
  95. /// <returns></returns>
  96. protected string GetUptimeReport()
  97. {
  98. StringBuilder sb = new StringBuilder(String.Format("Time now is {0}\n", DateTime.Now));
  99. sb.Append(String.Format("Server has been running since {0}, {1}\n", m_startuptime.DayOfWeek, m_startuptime));
  100. sb.Append(String.Format("That is an elapsed time of {0}\n", DateTime.Now - m_startuptime));
  101. return sb.ToString();
  102. }
  103. /// <summary>
  104. /// Performs initialisation of the scene, such as loading configuration from disk.
  105. /// </summary>
  106. public virtual void Startup()
  107. {
  108. m_log.Info("[STARTUP]: Beginning startup processing");
  109. EnhanceVersionInformation();
  110. m_log.Info("[STARTUP]: Version " + m_version + "\n");
  111. }
  112. /// <summary>
  113. /// Should be overriden and referenced by descendents if they need to perform extra shutdown processing
  114. /// </summary>
  115. public virtual void Shutdown()
  116. {
  117. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  118. Environment.Exit(0);
  119. }
  120. /// <summary>
  121. /// Runs commands issued by the server console from the operator
  122. /// </summary>
  123. /// <param name="command">The first argument of the parameter (the command)</param>
  124. /// <param name="cmdparams">Additional arguments passed to the command</param>
  125. public virtual void RunCmd(string command, string[] cmdparams)
  126. {
  127. switch (command)
  128. {
  129. case "help":
  130. Notice("quit - equivalent to shutdown.");
  131. Notice("show info - show server information (e.g. startup path).");
  132. if (m_stats != null)
  133. Notice("show stats - show statistical information for this server");
  134. Notice("show threads - list tracked threads");
  135. Notice("show uptime - show server startup time and uptime.");
  136. Notice("show version - show server version.");
  137. Notice("shutdown - shutdown the server.\n");
  138. break;
  139. case "show":
  140. if (cmdparams.Length > 0)
  141. {
  142. Show(cmdparams[0]);
  143. }
  144. break;
  145. case "quit":
  146. case "shutdown":
  147. Shutdown();
  148. break;
  149. }
  150. }
  151. /// <summary>
  152. /// Outputs to the console information about the region
  153. /// </summary>
  154. /// <param name="ShowWhat">What information to display (valid arguments are "uptime", "users")</param>
  155. public virtual void Show(string ShowWhat)
  156. {
  157. switch (ShowWhat)
  158. {
  159. case "info":
  160. Notice("Version: " + m_version);
  161. Notice("Startup directory: " + m_startupDirectory);
  162. break;
  163. case "stats":
  164. if (m_stats != null)
  165. {
  166. Notice(m_stats.Report());
  167. }
  168. break;
  169. case "threads":
  170. // m_console.Notice("THREAD", Process.GetCurrentProcess().Threads.Count + " threads running:");
  171. // int _tc = 0;
  172. // foreach (ProcessThread pt in Process.GetCurrentProcess().Threads)
  173. // {
  174. // _tc++;
  175. // m_console.Notice("THREAD", _tc + ": ID: " + pt.Id + ", Started: " + pt.StartTime.ToString() + ", CPU time: " + pt.TotalProcessorTime + ", Pri: " + pt.BasePriority.ToString() + ", State: " + pt.ThreadState.ToString());
  176. // }
  177. List<Thread> threads = ThreadTracker.GetThreads();
  178. if (threads == null)
  179. {
  180. Notice("Thread tracking is only enabled in DEBUG mode.");
  181. }
  182. else
  183. {
  184. int tc = 0;
  185. Notice(threads.Count + " threads are being tracked:");
  186. foreach (Thread t in threads)
  187. {
  188. tc++;
  189. Notice(tc + ": ID: " + t.ManagedThreadId.ToString() + ", Name: " + t.Name + ", Alive: " + t.IsAlive.ToString() + ", Pri: " + t.Priority.ToString() + ", State: " + t.ThreadState.ToString());
  190. }
  191. }
  192. break;
  193. case "uptime":
  194. Notice(GetUptimeReport());
  195. break;
  196. case "version":
  197. Notice("Version: " + m_version);
  198. break;
  199. }
  200. }
  201. /// <summary>
  202. /// Console output is only possible if a console has been established.
  203. /// That is something that cannot be determined within this class. So
  204. /// all attempts to use the console MUST be verified.
  205. /// </summary>
  206. private void Notice(string msg)
  207. {
  208. if (m_console != null)
  209. {
  210. m_console.Notice(msg);
  211. }
  212. }
  213. /// <summary>
  214. /// Enhance the version string with extra information if it's available.
  215. /// </summary>
  216. protected void EnhanceVersionInformation()
  217. {
  218. string buildVersion = string.Empty;
  219. // Add subversion revision information if available
  220. // Try file "svn_revision" in the current directory first, then the .svn info.
  221. // This allows to make the revision available in simulators not running from the source tree.
  222. // FIXME: Making an assumption about the directory we're currently in - we do this all over the place
  223. // elsewhere as well
  224. string svnRevisionFileName = "svn_revision";
  225. string svnFileName = "../.svn/entries";
  226. string inputLine;
  227. int strcmp;
  228. if (File.Exists(svnRevisionFileName))
  229. {
  230. StreamReader RevisionFile = File.OpenText(svnRevisionFileName);
  231. buildVersion = RevisionFile.ReadLine();
  232. buildVersion.Trim();
  233. RevisionFile.Close();
  234. }
  235. if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName))
  236. {
  237. StreamReader EntriesFile = File.OpenText(svnFileName);
  238. inputLine = EntriesFile.ReadLine();
  239. while (inputLine != null)
  240. {
  241. // using the dir svn revision at the top of entries file
  242. strcmp = String.Compare(inputLine, "dir");
  243. if (strcmp == 0)
  244. {
  245. buildVersion = EntriesFile.ReadLine();
  246. break;
  247. }
  248. else
  249. {
  250. inputLine = EntriesFile.ReadLine();
  251. }
  252. }
  253. EntriesFile.Close();
  254. }
  255. if (!string.IsNullOrEmpty(buildVersion))
  256. {
  257. m_version += ", SVN build r" + buildVersion;
  258. }
  259. else
  260. {
  261. m_version += ", SVN build revision not available";
  262. }
  263. // Add operating system information if available
  264. string OSString = "";
  265. if (System.Environment.OSVersion.Platform != PlatformID.Unix)
  266. {
  267. OSString = System.Environment.OSVersion.ToString();
  268. }
  269. else
  270. {
  271. OSString = Util.ReadEtcIssue();
  272. }
  273. if (OSString.Length > 45)
  274. {
  275. OSString = OSString.Substring(0, 45);
  276. }
  277. m_version += ", OS " + OSString;
  278. }
  279. }
  280. }