BinaryLoggingModule.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Packets;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.CoreModules.Avatar.Attachments
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BinaryLoggingModule")]
  43. public class BinaryLoggingModule : INonSharedRegionModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. protected bool m_collectStats;
  47. protected Scene m_scene = null;
  48. public string Name { get { return "Binary Statistics Logging Module"; } }
  49. public Type ReplaceableInterface { get { return null; } }
  50. public void Initialise(IConfigSource source)
  51. {
  52. try
  53. {
  54. IConfig statConfig = source.Configs["Statistics.Binary"];
  55. if (statConfig.Contains("enabled") && statConfig.GetBoolean("enabled"))
  56. {
  57. if (statConfig.Contains("collect_region_stats"))
  58. {
  59. if (statConfig.GetBoolean("collect_region_stats"))
  60. {
  61. m_collectStats = true;
  62. }
  63. }
  64. if (statConfig.Contains("region_stats_period_seconds"))
  65. {
  66. m_statLogPeriod = TimeSpan.FromSeconds(statConfig.GetInt("region_stats_period_seconds"));
  67. }
  68. if (statConfig.Contains("stats_dir"))
  69. {
  70. m_statsDir = statConfig.GetString("stats_dir");
  71. }
  72. }
  73. }
  74. catch
  75. {
  76. // if it doesn't work, we don't collect anything
  77. }
  78. }
  79. public void AddRegion(Scene scene)
  80. {
  81. m_scene = scene;
  82. }
  83. public void RemoveRegion(Scene scene)
  84. {
  85. }
  86. public void RegionLoaded(Scene scene)
  87. {
  88. if (m_collectStats)
  89. m_scene.StatsReporter.OnSendStatsResult += LogSimStats;
  90. }
  91. public void Close()
  92. {
  93. }
  94. public class StatLogger
  95. {
  96. public DateTime StartTime;
  97. public string Path;
  98. public System.IO.BinaryWriter Log;
  99. }
  100. static StatLogger m_statLog = null;
  101. static TimeSpan m_statLogPeriod = TimeSpan.FromSeconds(300);
  102. static string m_statsDir = String.Empty;
  103. static Object m_statLockObject = new Object();
  104. private void LogSimStats(SimStats stats)
  105. {
  106. SimStatsPacket pack = new SimStatsPacket();
  107. pack.Region = new SimStatsPacket.RegionBlock();
  108. pack.Region.RegionX = stats.RegionX;
  109. pack.Region.RegionY = stats.RegionY;
  110. pack.Region.RegionFlags = stats.RegionFlags;
  111. pack.Region.ObjectCapacity = stats.ObjectCapacity;
  112. //pack.Region = //stats.RegionBlock;
  113. pack.Stat = stats.StatsBlock;
  114. pack.Header.Reliable = false;
  115. // note that we are inside the reporter lock when called
  116. DateTime now = DateTime.Now;
  117. // hide some time information into the packet
  118. pack.Header.Sequence = (uint)now.Ticks;
  119. lock (m_statLockObject) // m_statLog is shared so make sure there is only executer here
  120. {
  121. try
  122. {
  123. if (m_statLog == null || now > m_statLog.StartTime + m_statLogPeriod)
  124. {
  125. // First log file or time has expired, start writing to a new log file
  126. if (m_statLog != null && m_statLog.Log != null)
  127. {
  128. m_statLog.Log.Close();
  129. }
  130. m_statLog = new StatLogger();
  131. m_statLog.StartTime = now;
  132. m_statLog.Path = (m_statsDir.Length > 0 ? m_statsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "")
  133. + String.Format("stats-{0}.log", now.ToString("yyyyMMddHHmmss"));
  134. m_statLog.Log = new BinaryWriter(File.Open(m_statLog.Path, FileMode.Append, FileAccess.Write));
  135. }
  136. // Write the serialized data to disk
  137. if (m_statLog != null && m_statLog.Log != null)
  138. m_statLog.Log.Write(pack.ToBytes());
  139. }
  140. catch (Exception ex)
  141. {
  142. m_log.Error("statistics gathering failed: " + ex.Message, ex);
  143. if (m_statLog != null && m_statLog.Log != null)
  144. {
  145. m_statLog.Log.Close();
  146. }
  147. m_statLog = null;
  148. }
  149. }
  150. return;
  151. }
  152. }
  153. }