aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/MemoryWatchdog.cs
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2012-07-25 22:38:28 +0100
committerJustin Clark-Casey (justincc)2012-07-25 22:38:28 +0100
commit227126adb784ed70739f42aec0072bdd8f06de9a (patch)
treec5f0b286d9288d161faac9b9aabbf49b22080a61 /OpenSim/Framework/MemoryWatchdog.cs
parentCorrect churn stat from MB/s from KB/s (diff)
downloadopensim-SC_OLD-227126adb784ed70739f42aec0072bdd8f06de9a.zip
opensim-SC_OLD-227126adb784ed70739f42aec0072bdd8f06de9a.tar.gz
opensim-SC_OLD-227126adb784ed70739f42aec0072bdd8f06de9a.tar.bz2
opensim-SC_OLD-227126adb784ed70739f42aec0072bdd8f06de9a.tar.xz
Add MemoryWatchdog class missing from git master a1e9964
Diffstat (limited to 'OpenSim/Framework/MemoryWatchdog.cs')
-rw-r--r--OpenSim/Framework/MemoryWatchdog.cs129
1 files changed, 129 insertions, 0 deletions
diff --git a/OpenSim/Framework/MemoryWatchdog.cs b/OpenSim/Framework/MemoryWatchdog.cs
new file mode 100644
index 0000000..1e93077
--- /dev/null
+++ b/OpenSim/Framework/MemoryWatchdog.cs
@@ -0,0 +1,129 @@
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
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Reflection;
32using System.Threading;
33using log4net;
34
35namespace OpenSim.Framework
36{
37 /// <summary>
38 /// Experimental watchdog for memory usage.
39 /// </summary>
40 public static class MemoryWatchdog
41 {
42// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43
44 /// <summary>
45 /// Is this watchdog active?
46 /// </summary>
47 public static bool Enabled
48 {
49 get { return m_enabled; }
50 set
51 {
52// m_log.DebugFormat("[MEMORY WATCHDOG]: Setting MemoryWatchdog.Enabled to {0}", value);
53
54 if (value && !m_enabled)
55 UpdateLastRecord(GC.GetTotalMemory(false), Util.EnvironmentTickCount());
56
57 m_enabled = value;
58 }
59 }
60 private static bool m_enabled;
61
62 /// <summary>
63 /// Average memory churn in bytes per millisecond.
64 /// </summary>
65 public static double AverageMemoryChurn
66 {
67 get { if (m_samples.Count > 0) return m_samples.Average(); else return 0; }
68 }
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
79 /// <summary>
80 /// Time when the watchdog was last updated.
81 /// </summary>
82 private static int m_lastUpdateTick;
83
84 /// <summary>
85 /// Memory used at time of last watchdog update.
86 /// </summary>
87 private static long m_lastUpdateMemory;
88
89 /// <summary>
90 /// Memory churn rate per millisecond.
91 /// </summary>
92 private static double m_churnRatePerMillisecond;
93
94 /// <summary>
95 /// Historical samples for calculating moving average.
96 /// </summary>
97 private static Queue<double> m_samples = new Queue<double>(m_maxSamples);
98
99 public static void Update()
100 {
101 int now = Util.EnvironmentTickCount();
102 long memoryNow = GC.GetTotalMemory(false);
103 long memoryDiff = memoryNow - m_lastUpdateMemory;
104
105 if (memoryDiff >= 0)
106 {
107 if (m_samples.Count >= m_maxSamples)
108 m_samples.Dequeue();
109
110 double elapsed = Util.EnvironmentTickCountSubtract(now, m_lastUpdateTick);
111
112 // This should never happen since it's not useful for updates to occur with no time elapsed, but
113 // protect ourselves from a divide-by-zero just in case.
114 if (elapsed == 0)
115 return;
116
117 m_samples.Enqueue(memoryDiff / (double)elapsed);
118 }
119
120 UpdateLastRecord(memoryNow, now);
121 }
122
123 private static void UpdateLastRecord(long memoryNow, int timeNow)
124 {
125 m_lastUpdateMemory = memoryNow;
126 m_lastUpdateTick = timeNow;
127 }
128 }
129} \ No newline at end of file