StatsLogger.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Reflection;
  29. using System.Timers;
  30. using log4net;
  31. namespace OpenSim.Framework.Monitoring
  32. {
  33. /// <summary>
  34. /// Provides a means to continuously log stats for debugging purposes.
  35. /// </summary>
  36. public static class StatsLogger
  37. {
  38. private static readonly ILog m_statsLog = LogManager.GetLogger("special.StatsLogger");
  39. private static Timer m_loggingTimer;
  40. private static int m_statsLogIntervalMs = 5000;
  41. public static void RegisterConsoleCommands(ICommandConsole console)
  42. {
  43. console.Commands.AddCommand(
  44. "Debug",
  45. false,
  46. "debug stats record",
  47. "debug stats record start|stop",
  48. "Control whether stats are being regularly recorded to a separate file.",
  49. "For debug purposes. Experimental.",
  50. HandleStatsRecordCommand);
  51. }
  52. public static void HandleStatsRecordCommand(string module, string[] cmd)
  53. {
  54. ICommandConsole con = MainConsole.Instance;
  55. if (cmd.Length != 4)
  56. {
  57. con.Output("Usage: debug stats record start|stop");
  58. return;
  59. }
  60. if (cmd[3] == "start")
  61. {
  62. Start();
  63. con.OutputFormat("Now recording all stats very {0}ms to file", m_statsLogIntervalMs);
  64. }
  65. else if (cmd[3] == "stop")
  66. {
  67. Stop();
  68. con.Output("Stopped recording stats to file.");
  69. }
  70. }
  71. public static void Start()
  72. {
  73. if (m_loggingTimer != null)
  74. Stop();
  75. m_loggingTimer = new Timer(m_statsLogIntervalMs);
  76. m_loggingTimer.AutoReset = false;
  77. m_loggingTimer.Elapsed += Log;
  78. m_loggingTimer.Start();
  79. }
  80. public static void Stop()
  81. {
  82. if (m_loggingTimer != null)
  83. {
  84. m_loggingTimer.Stop();
  85. }
  86. }
  87. private static void Log(object sender, ElapsedEventArgs e)
  88. {
  89. m_statsLog.InfoFormat("*** STATS REPORT AT {0} ***", DateTime.Now);
  90. foreach (string report in StatsManager.GetAllStatsReports())
  91. m_statsLog.Info(report);
  92. m_loggingTimer.Start();
  93. }
  94. }
  95. }