1
0

StatsLogger.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Timers;
  33. using log4net;
  34. namespace OpenSim.Framework.Monitoring
  35. {
  36. /// <summary>
  37. /// Provides a means to continuously log stats for debugging purposes.
  38. /// </summary>
  39. public static class StatsLogger
  40. {
  41. private static readonly ILog m_statsLog = LogManager.GetLogger("special.StatsLogger");
  42. private static Timer m_loggingTimer;
  43. private static int m_statsLogIntervalMs = 5000;
  44. public static void RegisterConsoleCommands(ICommandConsole console)
  45. {
  46. console.Commands.AddCommand(
  47. "General",
  48. false,
  49. "stats record",
  50. "stats record start|stop",
  51. "Control whether stats are being regularly recorded to a separate file.",
  52. "For debug purposes. Experimental.",
  53. HandleStatsRecordCommand);
  54. console.Commands.AddCommand(
  55. "General",
  56. false,
  57. "stats save",
  58. "stats save <path>",
  59. "Save stats snapshot to a file. If the file already exists, then the report is appended.",
  60. "For debug purposes. Experimental.",
  61. HandleStatsSaveCommand);
  62. }
  63. public static void HandleStatsRecordCommand(string module, string[] cmd)
  64. {
  65. ICommandConsole con = MainConsole.Instance;
  66. if (cmd.Length != 3)
  67. {
  68. con.Output("Usage: stats record start|stop");
  69. return;
  70. }
  71. if (cmd[2] == "start")
  72. {
  73. Start();
  74. con.Output("Now recording all stats to file every {0}ms", null, m_statsLogIntervalMs);
  75. }
  76. else if (cmd[2] == "stop")
  77. {
  78. Stop();
  79. con.Output("Stopped recording stats to file.");
  80. }
  81. }
  82. public static void HandleStatsSaveCommand(string module, string[] cmd)
  83. {
  84. ICommandConsole con = MainConsole.Instance;
  85. if (cmd.Length != 3)
  86. {
  87. con.Output("Usage: stats save <path>");
  88. return;
  89. }
  90. string path = cmd[2];
  91. using (StreamWriter sw = new StreamWriter(path, true))
  92. {
  93. foreach (string line in GetReport())
  94. sw.WriteLine(line);
  95. }
  96. MainConsole.Instance.Output("Stats saved to file {0}", null, path);
  97. }
  98. public static void Start()
  99. {
  100. if (m_loggingTimer != null)
  101. Stop();
  102. m_loggingTimer = new Timer(m_statsLogIntervalMs);
  103. m_loggingTimer.AutoReset = false;
  104. m_loggingTimer.Elapsed += Log;
  105. m_loggingTimer.Start();
  106. }
  107. public static void Stop()
  108. {
  109. if (m_loggingTimer != null)
  110. {
  111. m_loggingTimer.Stop();
  112. }
  113. }
  114. private static void Log(object sender, ElapsedEventArgs e)
  115. {
  116. foreach (string line in GetReport())
  117. m_statsLog.Info(line);
  118. m_loggingTimer.Start();
  119. }
  120. private static List<string> GetReport()
  121. {
  122. List<string> lines = new List<string>();
  123. lines.Add(string.Format("*** STATS REPORT AT {0} ***", DateTime.Now));
  124. foreach (string report in StatsManager.GetAllStatsReports())
  125. lines.Add(report);
  126. return lines;
  127. }
  128. }
  129. }