1
0

ServerBase.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 OpenSimulator 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.Diagnostics;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Reflection;
  33. using System.Runtime;
  34. using System.Text;
  35. using System.Text.RegularExpressions;
  36. using System.Threading;
  37. using log4net;
  38. using log4net.Appender;
  39. using log4net.Core;
  40. using log4net.Repository;
  41. using Nini.Config;
  42. using OpenSim.Framework.Console;
  43. using OpenSim.Framework.Monitoring;
  44. namespace OpenSim.Framework.Servers
  45. {
  46. public class ServerBase
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. public IConfigSource Config { get; protected set; }
  50. /// <summary>
  51. /// Console to be used for any command line output. Can be null, in which case there should be no output.
  52. /// </summary>
  53. protected ICommandConsole m_console;
  54. protected OpenSimAppender m_consoleAppender;
  55. protected FileAppender m_logFileAppender;
  56. protected FileAppender m_statsLogFileAppender;
  57. protected DateTime m_startuptime;
  58. protected string m_startupDirectory = Environment.CurrentDirectory;
  59. protected string m_pidFile = String.Empty;
  60. protected ServerStatsCollector m_serverStatsCollector;
  61. /// <summary>
  62. /// Server version information. Usually VersionInfo + information about git commit, operating system, etc.
  63. /// </summary>
  64. protected string m_version;
  65. public ServerBase()
  66. {
  67. m_startuptime = DateTime.Now;
  68. m_version = VersionInfo.Version;
  69. EnhanceVersionInformation();
  70. }
  71. protected void CreatePIDFile(string path)
  72. {
  73. if (File.Exists(path))
  74. m_log.Error($"[SERVER BASE]: Previous pid file {path} still exists on startup. Possibly previously unclean shutdown.");
  75. try
  76. {
  77. string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
  78. using (FileStream fs = File.Create(path))
  79. {
  80. Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
  81. fs.Write(buf, 0, buf.Length);
  82. }
  83. m_pidFile = path;
  84. m_log.InfoFormat("[SERVER BASE]: Created pid file {0}", m_pidFile);
  85. }
  86. catch (Exception e)
  87. {
  88. m_log.Warn(string.Format("[SERVER BASE]: Could not create PID file at {0} ", path), e);
  89. }
  90. }
  91. protected void RemovePIDFile()
  92. {
  93. if (!string.IsNullOrEmpty(m_pidFile))
  94. {
  95. try
  96. {
  97. File.Delete(m_pidFile);
  98. }
  99. catch (Exception e)
  100. {
  101. m_log.Error($"[SERVER BASE]: Error whilst removing {m_pidFile}", e);
  102. }
  103. m_pidFile = String.Empty;
  104. }
  105. }
  106. /// <summary>
  107. /// Log information about the circumstances in which we're running (OpenSimulator version number, CLR details,
  108. /// etc.).
  109. /// </summary>
  110. public void LogEnvironmentInformation()
  111. {
  112. // FIXME: This should be done down in ServerBase but we need to sort out and refactor the log4net
  113. // XmlConfigurator calls first accross servers.
  114. m_log.Info($"[SERVER BASE]: Starting in {m_startupDirectory}");
  115. m_log.Info($"[SERVER BASE]: OpenSimulator version: {m_version}");
  116. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  117. // the clr version number doesn't match the project version number under Mono.
  118. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  119. m_log.Info(
  120. $"[SERVER BASE]: Operating system version: {Environment.OSVersion}, .NET platform {Util.RuntimePlatformStr}, {(Environment.Is64BitProcess ? "64" : "32")}-bit");
  121. }
  122. public void RegisterCommonAppenders(IConfig startupConfig)
  123. {
  124. ILoggerRepository repository = LogManager.GetRepository();
  125. IAppender[] appenders = repository.GetAppenders();
  126. foreach (IAppender appender in appenders)
  127. {
  128. if (appender.Name == "Console")
  129. {
  130. m_consoleAppender = (OpenSimAppender)appender;
  131. }
  132. else if (appender.Name == "LogFileAppender")
  133. {
  134. m_logFileAppender = (FileAppender)appender;
  135. }
  136. else if (appender.Name == "StatsLogFileAppender")
  137. {
  138. m_statsLogFileAppender = (FileAppender)appender;
  139. }
  140. }
  141. if (null == m_consoleAppender)
  142. {
  143. Notice("No appender named Console found (see the log4net config file for this executable)!");
  144. }
  145. else
  146. {
  147. // FIXME: This should be done through an interface rather than casting.
  148. m_consoleAppender.Console = (ConsoleBase)m_console;
  149. // If there is no threshold set then the threshold is effectively everything.
  150. if (m_consoleAppender.Threshold is null)
  151. m_consoleAppender.Threshold = Level.All;
  152. //Notice($"Console log level is {m_consoleAppender.Threshold}");
  153. }
  154. if (m_logFileAppender != null && startupConfig != null)
  155. {
  156. string cfgFileName = startupConfig.GetString("logfile", null);
  157. if (cfgFileName != null)
  158. {
  159. m_logFileAppender.File = cfgFileName;
  160. m_logFileAppender.ActivateOptions();
  161. }
  162. m_log.InfoFormat("[SERVER BASE]: Logging started to file {0}", m_logFileAppender.File);
  163. }
  164. if (m_statsLogFileAppender != null && startupConfig != null)
  165. {
  166. string cfgStatsFileName = startupConfig.GetString("StatsLogFile", null);
  167. if (cfgStatsFileName != null)
  168. {
  169. m_statsLogFileAppender.File = cfgStatsFileName;
  170. m_statsLogFileAppender.ActivateOptions();
  171. }
  172. m_log.InfoFormat("[SERVER BASE]: Stats Logging started to file {0}", m_statsLogFileAppender.File);
  173. }
  174. }
  175. /// <summary>
  176. /// Register common commands once m_console has been set if it is going to be set
  177. /// </summary>
  178. public void RegisterCommonCommands()
  179. {
  180. if (m_console == null)
  181. return;
  182. m_console.Commands.AddCommand(
  183. "General", false, "show info", "show info", "Show general information about the server", HandleShow);
  184. m_console.Commands.AddCommand(
  185. "General", false, "show version", "show version", "Show server version", HandleShow);
  186. m_console.Commands.AddCommand(
  187. "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow);
  188. m_console.Commands.AddCommand(
  189. "General", false, "get log level", "get log level", "Get the current console logging level",
  190. (mod, cmd) => ShowLogLevel());
  191. m_console.Commands.AddCommand(
  192. "General", false, "set log level", "set log level <level>",
  193. "Set the console logging level for this session.", HandleSetLogLevel);
  194. m_console.Commands.AddCommand(
  195. "General", false, "config set",
  196. "config set <section> <key> <value>",
  197. "Set a config option. In most cases this is not useful since changed parameters are not dynamically reloaded. Neither do changed parameters persist - you will have to change a config file manually and restart.", HandleConfig);
  198. m_console.Commands.AddCommand(
  199. "General", false, "config get",
  200. "config get [<section>] [<key>]",
  201. "Synonym for config show",
  202. HandleConfig);
  203. m_console.Commands.AddCommand(
  204. "General", false, "config show",
  205. "config show [<section>] [<key>]",
  206. "Show config information",
  207. "If neither section nor field are specified, then the whole current configuration is printed." + Environment.NewLine
  208. + "If a section is given but not a field, then all fields in that section are printed.",
  209. HandleConfig);
  210. m_console.Commands.AddCommand(
  211. "General", false, "config save",
  212. "config save <path>",
  213. "Save current configuration to a file at the given path", HandleConfig);
  214. m_console.Commands.AddCommand(
  215. "General", false, "command-script",
  216. "command-script <script>",
  217. "Run a command script from file", HandleScript);
  218. m_console.Commands.AddCommand(
  219. "General", false, "show threads",
  220. "show threads",
  221. "Show thread status", HandleShow);
  222. m_console.Commands.AddCommand(
  223. "Debug", false, "threads abort",
  224. "threads abort <thread-id>",
  225. "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort);
  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. m_console.Commands.AddCommand (
  232. "Debug", false, "debug threadpool set",
  233. "debug threadpool set worker|iocp min|max <n>",
  234. "Set threadpool parameters. For debug purposes.",
  235. HandleDebugThreadpoolSet);
  236. m_console.Commands.AddCommand (
  237. "Debug", false, "debug threadpool status",
  238. "debug threadpool status",
  239. "Show current debug threadpool parameters.",
  240. HandleDebugThreadpoolStatus);
  241. m_console.Commands.AddCommand(
  242. "Debug", false, "debug threadpool level",
  243. "debug threadpool level 0.." + Util.MAX_THREADPOOL_LEVEL,
  244. "Turn on logging of activity in the main thread pool.",
  245. "Log levels:\n"
  246. + " 0 = no logging\n"
  247. + " 1 = only first line of stack trace; don't log common threads\n"
  248. + " 2 = full stack trace; don't log common threads\n"
  249. + " 3 = full stack trace, including common threads\n",
  250. HandleDebugThreadpoolLevel);
  251. //m_console.Commands.AddCommand(
  252. // "Debug", false, "show threadpool calls active",
  253. // "show threadpool calls active",
  254. // "Show details about threadpool calls that are still active (currently waiting or in progress)",
  255. // HandleShowThreadpoolCallsActive);
  256. m_console.Commands.AddCommand(
  257. "Debug", false, "show threadpool calls complete",
  258. "show threadpool calls complete",
  259. "Show details about threadpool calls that have been completed.",
  260. HandleShowThreadpoolCallsComplete);
  261. m_console.Commands.AddCommand(
  262. "Debug", false, "force gc",
  263. "force gc",
  264. "Manually invoke runtime garbage collection. For debugging purposes",
  265. HandleForceGc);
  266. m_console.Commands.AddCommand(
  267. "General", false, "quit",
  268. "quit",
  269. "Quit the application", (mod, args) => Shutdown());
  270. m_console.Commands.AddCommand(
  271. "General", false, "shutdown",
  272. "shutdown",
  273. "Quit the application", (mod, args) => Shutdown());
  274. m_console.SetCntrCHandler(Shutdown);
  275. ChecksManager.RegisterConsoleCommands(m_console);
  276. StatsManager.RegisterConsoleCommands(m_console);
  277. }
  278. public void RegisterCommonComponents(IConfigSource configSource)
  279. {
  280. //IConfig networkConfig = configSource.Configs["Network"];
  281. m_serverStatsCollector = new ServerStatsCollector();
  282. m_serverStatsCollector.Initialise(configSource);
  283. m_serverStatsCollector.Start();
  284. }
  285. private void HandleShowThreadpoolCallsActive(string module, string[] args)
  286. {
  287. List<KeyValuePair<string, int>> calls = Util.GetFireAndForgetCallsInProgress().ToList();
  288. calls.Sort((kvp1, kvp2) => kvp2.Value.CompareTo(kvp1.Value));
  289. int namedCalls = 0;
  290. ConsoleDisplayList cdl = new ConsoleDisplayList();
  291. foreach (KeyValuePair<string, int> kvp in calls)
  292. {
  293. if (kvp.Value > 0)
  294. {
  295. cdl.AddRow(kvp.Key, kvp.Value);
  296. namedCalls += kvp.Value;
  297. }
  298. }
  299. cdl.AddRow("TOTAL NAMED", namedCalls);
  300. long allQueuedCalls = Util.TotalQueuedFireAndForgetCalls;
  301. long allRunningCalls = Util.TotalRunningFireAndForgetCalls;
  302. cdl.AddRow("TOTAL QUEUED", allQueuedCalls);
  303. cdl.AddRow("TOTAL RUNNING", allRunningCalls);
  304. cdl.AddRow("TOTAL ANONYMOUS", allQueuedCalls + allRunningCalls - namedCalls);
  305. cdl.AddRow("TOTAL ALL", allQueuedCalls + allRunningCalls);
  306. MainConsole.Instance.Output(cdl.ToString());
  307. }
  308. private void HandleShowThreadpoolCallsComplete(string module, string[] args)
  309. {
  310. List<KeyValuePair<string, int>> calls = Util.GetFireAndForgetCallsMade().ToList();
  311. calls.Sort((kvp1, kvp2) => kvp2.Value.CompareTo(kvp1.Value));
  312. int namedCallsMade = 0;
  313. ConsoleDisplayList cdl = new ConsoleDisplayList();
  314. foreach (KeyValuePair<string, int> kvp in calls)
  315. {
  316. cdl.AddRow(kvp.Key, kvp.Value);
  317. namedCallsMade += kvp.Value;
  318. }
  319. cdl.AddRow("TOTAL NAMED", namedCallsMade);
  320. long allCallsMade = Util.TotalFireAndForgetCallsMade;
  321. cdl.AddRow("TOTAL ANONYMOUS", allCallsMade - namedCallsMade);
  322. cdl.AddRow("TOTAL ALL", allCallsMade);
  323. MainConsole.Instance.Output(cdl.ToString());
  324. }
  325. private void HandleDebugThreadpoolStatus(string module, string[] args)
  326. {
  327. int workerThreads, iocpThreads;
  328. ThreadPool.GetMinThreads(out workerThreads, out iocpThreads);
  329. Notice("Min worker threads: {0}", workerThreads);
  330. Notice("Min IOCP threads: {0}", iocpThreads);
  331. ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  332. Notice("Max worker threads: {0}", workerThreads);
  333. Notice("Max IOCP threads: {0}", iocpThreads);
  334. ThreadPool.GetAvailableThreads(out workerThreads, out iocpThreads);
  335. Notice("Available worker threads: {0}", workerThreads);
  336. Notice("Available IOCP threads: {0}", iocpThreads);
  337. }
  338. private void HandleDebugThreadpoolSet(string module, string[] args)
  339. {
  340. if (args.Length != 6)
  341. {
  342. Notice("Usage: debug threadpool set worker|iocp min|max <n>");
  343. return;
  344. }
  345. if (!ConsoleUtil.TryParseConsoleInt(m_console, args[5], out int newThreads))
  346. return;
  347. string poolType = args[3];
  348. string bound = args[4];
  349. bool fail = false;
  350. int workerThreads, iocpThreads;
  351. if (poolType == "worker")
  352. {
  353. if (bound == "min")
  354. {
  355. ThreadPool.GetMinThreads(out workerThreads, out iocpThreads);
  356. if (!ThreadPool.SetMinThreads(newThreads, iocpThreads))
  357. fail = true;
  358. }
  359. else
  360. {
  361. ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  362. if (!ThreadPool.SetMaxThreads(newThreads, iocpThreads))
  363. fail = true;
  364. }
  365. }
  366. else
  367. {
  368. if (bound == "min")
  369. {
  370. ThreadPool.GetMinThreads(out workerThreads, out iocpThreads);
  371. if (!ThreadPool.SetMinThreads(workerThreads, newThreads))
  372. fail = true;
  373. }
  374. else
  375. {
  376. ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  377. if (!ThreadPool.SetMaxThreads(workerThreads, newThreads))
  378. fail = true;
  379. }
  380. }
  381. if (fail)
  382. {
  383. Notice("ERROR: Could not set {0} {1} threads to {2}", poolType, bound, newThreads);
  384. }
  385. else
  386. {
  387. ThreadPool.GetMinThreads(out int minWorkerThreads, out int minIocpThreads);
  388. ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIocpThreads);
  389. Notice("Min worker threads now {0}", minWorkerThreads);
  390. Notice("Min IOCP threads now {0}", minIocpThreads);
  391. Notice("Max worker threads now {0}", maxWorkerThreads);
  392. Notice("Max IOCP threads now {0}", maxIocpThreads);
  393. }
  394. }
  395. private static void HandleDebugThreadpoolLevel(string module, string[] cmdparams)
  396. {
  397. if (cmdparams.Length < 4)
  398. {
  399. MainConsole.Instance.Output("Usage: debug threadpool level 0.." + Util.MAX_THREADPOOL_LEVEL);
  400. return;
  401. }
  402. string rawLevel = cmdparams[3];
  403. int newLevel;
  404. if (!int.TryParse(rawLevel, out newLevel))
  405. {
  406. MainConsole.Instance.Output("{0} is not a valid debug level", rawLevel);
  407. return;
  408. }
  409. if (newLevel < 0 || newLevel > Util.MAX_THREADPOOL_LEVEL)
  410. {
  411. MainConsole.Instance.Output("{0} is outside the valid debug level range of 0.." + Util.MAX_THREADPOOL_LEVEL, newLevel);
  412. return;
  413. }
  414. Util.LogThreadPool = newLevel;
  415. MainConsole.Instance.Output("LogThreadPool set to {0}", newLevel);
  416. }
  417. private void HandleForceGc(string module, string[] args)
  418. {
  419. Notice("Manually invoking runtime garbage collection");
  420. GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
  421. GC.Collect();
  422. GC.WaitForPendingFinalizers();
  423. GC.Collect();
  424. GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.Default;
  425. }
  426. public virtual void HandleShow(string module, string[] cmd)
  427. {
  428. if(cmd.Length < 2)
  429. return;
  430. switch (cmd[1])
  431. {
  432. case "info":
  433. ShowInfo();
  434. break;
  435. case "version":
  436. Notice(GetVersionText());
  437. break;
  438. case "uptime":
  439. Notice(GetUptimeReport());
  440. break;
  441. case "threads":
  442. Notice(GetThreadsReport());
  443. break;
  444. }
  445. }
  446. /// <summary>
  447. /// Change and load configuration file data.
  448. /// </summary>
  449. /// <param name="module"></param>
  450. /// <param name="cmd"></param>
  451. private void HandleConfig(string module, string[] cmd)
  452. {
  453. if (cmd.Length > 1)
  454. {
  455. string firstParam = cmd[1].ToLower();
  456. switch (firstParam)
  457. {
  458. case "set":
  459. if (cmd.Length < 5)
  460. {
  461. Notice("Syntax: config set <section> <key> <value>");
  462. Notice("Example: config set ScriptEngine.DotNetEngine NumberOfScriptThreads 5");
  463. }
  464. else
  465. {
  466. IConfig c;
  467. IConfigSource source = new IniConfigSource();
  468. c = source.AddConfig(cmd[2]);
  469. if (c != null)
  470. {
  471. string _value = String.Join(" ", cmd, 4, cmd.Length - 4);
  472. c.Set(cmd[3], _value);
  473. Config.Merge(source);
  474. Notice("In section [{0}], set {1} = {2}", c.Name, cmd[3], _value);
  475. }
  476. }
  477. break;
  478. case "get":
  479. case "show":
  480. if (cmd.Length == 2)
  481. {
  482. foreach (IConfig config in Config.Configs)
  483. {
  484. Notice("[{0}]", config.Name);
  485. string[] keys = config.GetKeys();
  486. foreach (string key in keys)
  487. Notice(" {0} = {1}", key, config.GetString(key));
  488. }
  489. }
  490. else if (cmd.Length == 3 || cmd.Length == 4)
  491. {
  492. IConfig config = Config.Configs[cmd[2]];
  493. if (config == null)
  494. {
  495. Notice("Section \"{0}\" does not exist.",cmd[2]);
  496. break;
  497. }
  498. else
  499. {
  500. if (cmd.Length == 3)
  501. {
  502. Notice("[{0}]", config.Name);
  503. foreach (string key in config.GetKeys())
  504. Notice(" {0} = {1}", key, config.GetString(key));
  505. }
  506. else
  507. {
  508. Notice(
  509. "config get {0} {1} : {2}",
  510. cmd[2], cmd[3], config.GetString(cmd[3]));
  511. }
  512. }
  513. }
  514. else
  515. {
  516. Notice("Syntax: config {0} [<section>] [<key>]", firstParam);
  517. Notice("Example: config {0} ScriptEngine.DotNetEngine NumberOfScriptThreads", firstParam);
  518. }
  519. break;
  520. case "save":
  521. if (cmd.Length < 3)
  522. {
  523. Notice("Syntax: config save <path>");
  524. return;
  525. }
  526. string path = cmd[2];
  527. Notice("Saving configuration file: {0}", path);
  528. if (Config is IniConfigSource)
  529. {
  530. IniConfigSource iniCon = (IniConfigSource)Config;
  531. iniCon.Save(path);
  532. }
  533. else if (Config is XmlConfigSource)
  534. {
  535. XmlConfigSource xmlCon = (XmlConfigSource)Config;
  536. xmlCon.Save(path);
  537. }
  538. break;
  539. }
  540. }
  541. }
  542. private void HandleSetLogLevel(string module, string[] cmd)
  543. {
  544. if (cmd.Length != 4)
  545. {
  546. Notice("Usage: set log level <level>");
  547. return;
  548. }
  549. if (null == m_consoleAppender)
  550. {
  551. Notice("No appender named Console found (see the log4net config file for this executable)!");
  552. return;
  553. }
  554. string rawLevel = cmd[3];
  555. ILoggerRepository repository = LogManager.GetRepository();
  556. Level consoleLevel = repository.LevelMap[rawLevel];
  557. if (consoleLevel != null)
  558. m_consoleAppender.Threshold = consoleLevel;
  559. else
  560. Notice(
  561. "{0} is not a valid logging level. Valid logging levels are ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF",
  562. rawLevel);
  563. ShowLogLevel();
  564. }
  565. private void ShowLogLevel()
  566. {
  567. if (null == m_consoleAppender)
  568. {
  569. Notice("No appender named Console found (see the log4net config file for this executable)!");
  570. return;
  571. }
  572. Notice("Console log level is {0}", m_consoleAppender.Threshold);
  573. }
  574. protected virtual void HandleScript(string module, string[] parms)
  575. {
  576. if (parms.Length != 2)
  577. {
  578. Notice("Usage: command-script <path-to-script");
  579. return;
  580. }
  581. RunCommandScript(parms[1]);
  582. }
  583. /// <summary>
  584. /// Run an optional startup list of commands
  585. /// </summary>
  586. /// <param name="fileName"></param>
  587. protected void RunCommandScript(string fileName)
  588. {
  589. if (m_console == null)
  590. return;
  591. if (File.Exists(fileName))
  592. {
  593. m_log.Info("[SERVER BASE]: Running " + fileName);
  594. using (StreamReader readFile = File.OpenText(fileName))
  595. {
  596. string currentCommand;
  597. while ((currentCommand = readFile.ReadLine()) != null)
  598. {
  599. currentCommand = currentCommand.Trim();
  600. if (!(currentCommand.Length == 0
  601. || currentCommand.StartsWith(";")
  602. || currentCommand.StartsWith("//")
  603. || currentCommand.StartsWith("#")))
  604. {
  605. m_log.Info("[SERVER BASE]: Running '" + currentCommand + "'");
  606. m_console.RunCommand(currentCommand);
  607. }
  608. }
  609. }
  610. }
  611. }
  612. /// <summary>
  613. /// Return a report about the uptime of this server
  614. /// </summary>
  615. /// <returns></returns>
  616. protected string GetUptimeReport()
  617. {
  618. StringBuilder sb = new StringBuilder(512);
  619. sb.AppendFormat("Time now is {0}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  620. sb.AppendFormat("Server has been running since {0}, {1}\n", m_startuptime.DayOfWeek, m_startuptime.ToString("yyyy-MM-dd HH:mm:ss"));
  621. sb.AppendFormat("That is an elapsed time of {0}\n", DateTime.Now - m_startuptime);
  622. return sb.ToString();
  623. }
  624. protected void ShowInfo()
  625. {
  626. Notice(GetVersionText());
  627. Notice("Startup directory: " + m_startupDirectory);
  628. if (null != m_consoleAppender)
  629. Notice(String.Format("Console log level: {0}", m_consoleAppender.Threshold));
  630. }
  631. /// <summary>
  632. /// Enhance the version string with extra information if it's available.
  633. /// </summary>
  634. protected void EnhanceVersionInformation()
  635. {
  636. const string manualVersionFileName = ".version";
  637. string buildVersion = string.Empty;
  638. if (File.Exists(manualVersionFileName))
  639. {
  640. using (StreamReader CommitFile = File.OpenText(manualVersionFileName))
  641. buildVersion = CommitFile.ReadLine();
  642. if (!string.IsNullOrEmpty(buildVersion))
  643. {
  644. m_version += buildVersion;
  645. return;
  646. }
  647. }
  648. string gitDir = Path.Combine("..", ".git");
  649. string gitRefPointerPath = Path.Combine(gitDir, "HEAD");
  650. if (File.Exists(gitRefPointerPath))
  651. {
  652. //m_log.DebugFormat("[SERVER BASE]: Found {0}", gitRefPointerPath);
  653. string rawPointer = "";
  654. using (StreamReader pointerFile = File.OpenText(gitRefPointerPath))
  655. rawPointer = pointerFile.ReadLine();
  656. //m_log.DebugFormat("[SERVER BASE]: rawPointer [{0}]", rawPointer);
  657. Match m = Regex.Match(rawPointer, "^ref: (.+)$");
  658. if (m.Success)
  659. {
  660. //m_log.DebugFormat("[SERVER BASE]: Matched [{0}]", m.Groups[1].Value);
  661. string gitRef = m.Groups[1].Value;
  662. string gitRefPath = Path.Combine(gitDir, gitRef);
  663. if (File.Exists(gitRefPath))
  664. {
  665. //m_log.DebugFormat("[SERVER BASE]: Found gitRefPath [{0}]", gitRefPath);
  666. using (StreamReader refFile = File.OpenText(gitRefPath))
  667. buildVersion = refFile.ReadLine();
  668. if (!string.IsNullOrEmpty(buildVersion))
  669. m_version += buildVersion[..7];
  670. }
  671. }
  672. }
  673. }
  674. public string GetVersionText()
  675. {
  676. return $"Version: {m_version} (SIM-{VersionInfo.SimulationServiceVersionSupportedMin}/{VersionInfo.SimulationServiceVersionSupportedMax})";
  677. }
  678. /// <summary>
  679. /// Get a report about the registered threads in this server.
  680. /// </summary>
  681. protected string GetThreadsReport()
  682. {
  683. // This should be a constant field.
  684. string reportFormat = "{0,6} {1,35} {2,16} {3,13} {4,10} {5,30}";
  685. StringBuilder sb = new StringBuilder();
  686. Watchdog.ThreadWatchdogInfo[] threads = Watchdog.GetThreadsInfo();
  687. sb.Append(threads.Length + " threads are being tracked:" + Environment.NewLine);
  688. int timeNow = Environment.TickCount & Int32.MaxValue;
  689. sb.AppendFormat(reportFormat, "ID", "NAME", "LAST UPDATE (MS)", "LIFETIME (MS)", "PRIORITY", "STATE");
  690. sb.Append(Environment.NewLine);
  691. foreach (Watchdog.ThreadWatchdogInfo twi in threads)
  692. {
  693. Thread t = twi.Thread;
  694. sb.AppendFormat(
  695. reportFormat,
  696. t.ManagedThreadId,
  697. t.Name,
  698. timeNow - twi.LastTick,
  699. timeNow - twi.FirstTick,
  700. t.Priority,
  701. t.ThreadState);
  702. sb.Append("\n");
  703. }
  704. sb.Append(GetThreadPoolReport());
  705. sb.Append("\n");
  706. int totalThreads = Process.GetCurrentProcess().Threads.Count;
  707. if (totalThreads > 0)
  708. sb.AppendFormat("Total process threads: {0}\n\n", totalThreads);
  709. return sb.ToString();
  710. }
  711. /// <summary>
  712. /// Get a thread pool report.
  713. /// </summary>
  714. /// <returns></returns>
  715. public static string GetThreadPoolReport()
  716. {
  717. StringBuilder sb = new StringBuilder();
  718. // framework pool is alwasy active
  719. int maxWorkers;
  720. int minWorkers;
  721. int curWorkers;
  722. int maxComp;
  723. int minComp;
  724. int curComp;
  725. try
  726. {
  727. ThreadPool.GetMaxThreads(out maxWorkers, out maxComp);
  728. ThreadPool.GetMinThreads(out minWorkers, out minComp);
  729. ThreadPool.GetAvailableThreads(out curWorkers, out curComp);
  730. curWorkers = maxWorkers - curWorkers;
  731. curComp = maxComp - curComp;
  732. sb.Append("\nFramework main threadpool \n");
  733. sb.AppendFormat("workers: {0} ({1} / {2})\n", curWorkers, maxWorkers, minWorkers);
  734. sb.AppendFormat("Completion: {0} ({1} / {2})\n", curComp, maxComp, minComp);
  735. }
  736. catch { }
  737. if (Util.FireAndForgetMethod == FireAndForgetMethod.QueueUserWorkItem)
  738. {
  739. sb.AppendFormat("\nThread pool used: Framework main threadpool\n");
  740. return sb.ToString();
  741. }
  742. string threadPoolUsed = null;
  743. int maxThreads = 0;
  744. int minThreads = 0;
  745. int allocatedThreads = 0;
  746. int inUseThreads = 0;
  747. int waitingCallbacks = 0;
  748. if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
  749. {
  750. STPInfo stpi = Util.GetSmartThreadPoolInfo();
  751. // ROBUST currently leaves this the FireAndForgetMethod but never actually initializes the threadpool.
  752. if (stpi != null)
  753. {
  754. threadPoolUsed = "SmartThreadPool";
  755. maxThreads = stpi.MaxThreads;
  756. minThreads = stpi.MinThreads;
  757. inUseThreads = stpi.InUseThreads;
  758. allocatedThreads = stpi.ActiveThreads;
  759. waitingCallbacks = stpi.WaitingCallbacks;
  760. }
  761. }
  762. if (threadPoolUsed != null)
  763. {
  764. sb.Append("\nThreadpool (excluding script engine pools)\n");
  765. sb.AppendFormat("Thread pool used : {0}\n", threadPoolUsed);
  766. sb.AppendFormat("Max threads : {0}\n", maxThreads);
  767. sb.AppendFormat("Min threads : {0}\n", minThreads);
  768. sb.AppendFormat("Allocated threads : {0}\n", allocatedThreads < 0 ? "not applicable" : allocatedThreads.ToString());
  769. sb.AppendFormat("In use threads : {0}\n", inUseThreads);
  770. sb.AppendFormat("Work items waiting : {0}\n", waitingCallbacks < 0 ? "not available" : waitingCallbacks.ToString());
  771. }
  772. else
  773. {
  774. sb.AppendFormat("Thread pool not used\n");
  775. }
  776. return sb.ToString();
  777. }
  778. public virtual void HandleThreadsAbort(string module, string[] cmd)
  779. {
  780. if (cmd.Length != 3)
  781. {
  782. MainConsole.Instance.Output("Usage: threads abort <thread-id>");
  783. return;
  784. }
  785. int threadId;
  786. if (!int.TryParse(cmd[2], out threadId))
  787. {
  788. MainConsole.Instance.Output("ERROR: Thread id must be an integer");
  789. return;
  790. }
  791. if (Watchdog.AbortThread(threadId))
  792. MainConsole.Instance.Output("Aborted thread with id {0}", threadId);
  793. else
  794. MainConsole.Instance.Output("ERROR - Thread with id {0} not found in managed threads", threadId);
  795. }
  796. /// <summary>
  797. /// Console output is only possible if a console has been established.
  798. /// That is something that cannot be determined within this class. So
  799. /// all attempts to use the console MUST be verified.
  800. /// </summary>
  801. /// <param name="msg"></param>
  802. protected void Notice(string msg)
  803. {
  804. if (m_console != null)
  805. {
  806. m_console.Output(msg);
  807. }
  808. }
  809. /// <summary>
  810. /// Console output is only possible if a console has been established.
  811. /// That is something that cannot be determined within this class. So
  812. /// all attempts to use the console MUST be verified.
  813. /// </summary>
  814. /// <param name="format"></param>
  815. /// <param name="components"></param>
  816. protected void Notice(string format, params object[] components)
  817. {
  818. if (m_console != null)
  819. m_console.Output(format, components);
  820. }
  821. public virtual void Shutdown()
  822. {
  823. m_serverStatsCollector.Close();
  824. ShutdownSpecific();
  825. }
  826. /// <summary>
  827. /// Should be overriden and referenced by descendents if they need to perform extra shutdown processing
  828. /// </summary>
  829. protected virtual void ShutdownSpecific() {}
  830. }
  831. }