MemoryWatchdog.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Linq;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. namespace OpenSim.Framework.Monitoring
  34. {
  35. /// <summary>
  36. /// Experimental watchdog for memory usage.
  37. /// </summary>
  38. public static class MemoryWatchdog
  39. {
  40. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// Is this watchdog active?
  43. /// </summary>
  44. public static bool Enabled
  45. {
  46. get { return m_enabled; }
  47. set
  48. {
  49. // m_log.DebugFormat("[MEMORY WATCHDOG]: Setting MemoryWatchdog.Enabled to {0}", value);
  50. if (value && !m_enabled)
  51. UpdateLastRecord(GC.GetTotalMemory(false), Util.EnvironmentTickCount());
  52. m_enabled = value;
  53. }
  54. }
  55. private static bool m_enabled;
  56. /// <summary>
  57. /// Last memory churn in bytes per millisecond.
  58. /// </summary>
  59. public static double AverageMemoryChurn
  60. {
  61. get { if (m_samples.Count > 0) return m_samples.Average(); else return 0; }
  62. }
  63. /// <summary>
  64. /// Average memory churn in bytes per millisecond.
  65. /// </summary>
  66. public static double LastMemoryChurn
  67. {
  68. get { if (m_samples.Count > 0) return m_samples.Last(); else return 0; }
  69. }
  70. /// <summary>
  71. /// Maximum number of statistical samples.
  72. /// </summary>
  73. /// <remarks>
  74. /// At the moment this corresponds to 1 minute since the sampling rate is every 2.5 seconds as triggered from
  75. /// the main Watchdog.
  76. /// </remarks>
  77. private static int m_maxSamples = 24;
  78. /// <summary>
  79. /// Time when the watchdog was last updated.
  80. /// </summary>
  81. private static int m_lastUpdateTick;
  82. /// <summary>
  83. /// Memory used at time of last watchdog update.
  84. /// </summary>
  85. private static long m_lastUpdateMemory;
  86. /// <summary>
  87. /// Memory churn rate per millisecond.
  88. /// </summary>
  89. // private static double m_churnRatePerMillisecond;
  90. /// <summary>
  91. /// Historical samples for calculating moving average.
  92. /// </summary>
  93. private static Queue<double> m_samples = new Queue<double>(m_maxSamples);
  94. public static void Update()
  95. {
  96. int now = Util.EnvironmentTickCount();
  97. long memoryNow = GC.GetTotalMemory(false);
  98. long memoryDiff = memoryNow - m_lastUpdateMemory;
  99. if (memoryDiff >= 0)
  100. {
  101. if (m_samples.Count >= m_maxSamples)
  102. m_samples.Dequeue();
  103. double elapsed = Util.EnvironmentTickCountSubtract(now, m_lastUpdateTick);
  104. // This should never happen since it's not useful for updates to occur with no time elapsed, but
  105. // protect ourselves from a divide-by-zero just in case.
  106. if (elapsed == 0)
  107. return;
  108. m_samples.Enqueue(memoryDiff / (double)elapsed);
  109. }
  110. UpdateLastRecord(memoryNow, now);
  111. }
  112. private static void UpdateLastRecord(long memoryNow, int timeNow)
  113. {
  114. m_lastUpdateMemory = memoryNow;
  115. m_lastUpdateTick = timeNow;
  116. }
  117. }
  118. }