diff options
author | Justin Clark-Casey (justincc) | 2012-11-16 05:02:06 +0000 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2012-11-16 05:02:06 +0000 |
commit | 55f889cc66d78268c530c65a5fd111395cc26dc5 (patch) | |
tree | 8b8ba657ec3c7d9f213c90652844795d615bb4e8 /OpenSim/Framework/Monitoring/Stats/Stat.cs | |
parent | Merge branch 'master' of ssh://opensimulator.org/var/git/opensim (diff) | |
download | opensim-SC_OLD-55f889cc66d78268c530c65a5fd111395cc26dc5.zip opensim-SC_OLD-55f889cc66d78268c530c65a5fd111395cc26dc5.tar.gz opensim-SC_OLD-55f889cc66d78268c530c65a5fd111395cc26dc5.tar.bz2 opensim-SC_OLD-55f889cc66d78268c530c65a5fd111395cc26dc5.tar.xz |
refactor: Move stat classes out of StatManager and into their own files for clarity.
Diffstat (limited to 'OpenSim/Framework/Monitoring/Stats/Stat.cs')
-rw-r--r-- | OpenSim/Framework/Monitoring/Stats/Stat.cs | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/OpenSim/Framework/Monitoring/Stats/Stat.cs b/OpenSim/Framework/Monitoring/Stats/Stat.cs new file mode 100644 index 0000000..f91251b --- /dev/null +++ b/OpenSim/Framework/Monitoring/Stats/Stat.cs | |||
@@ -0,0 +1,238 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Text; | ||
31 | |||
32 | namespace OpenSim.Framework.Monitoring | ||
33 | { | ||
34 | /// <summary> | ||
35 | /// Holds individual statistic details | ||
36 | /// </summary> | ||
37 | public class Stat | ||
38 | { | ||
39 | /// <summary> | ||
40 | /// Category of this stat (e.g. cache, scene, etc). | ||
41 | /// </summary> | ||
42 | public string Category { get; private set; } | ||
43 | |||
44 | /// <summary> | ||
45 | /// Containing name for this stat. | ||
46 | /// FIXME: In the case of a scene, this is currently the scene name (though this leaves | ||
47 | /// us with a to-be-resolved problem of non-unique region names). | ||
48 | /// </summary> | ||
49 | /// <value> | ||
50 | /// The container. | ||
51 | /// </value> | ||
52 | public string Container { get; private set; } | ||
53 | |||
54 | public StatType StatType { get; private set; } | ||
55 | |||
56 | public MeasuresOfInterest MeasuresOfInterest { get; private set; } | ||
57 | |||
58 | /// <summary> | ||
59 | /// Action used to update this stat when the value is requested if it's a pull type. | ||
60 | /// </summary> | ||
61 | public Action<Stat> PullAction { get; private set; } | ||
62 | |||
63 | public StatVerbosity Verbosity { get; private set; } | ||
64 | public string ShortName { get; private set; } | ||
65 | public string Name { get; private set; } | ||
66 | public string Description { get; private set; } | ||
67 | public virtual string UnitName { get; private set; } | ||
68 | |||
69 | public virtual double Value | ||
70 | { | ||
71 | get | ||
72 | { | ||
73 | // Asking for an update here means that the updater cannot access this value without infinite recursion. | ||
74 | // XXX: A slightly messy but simple solution may be to flick a flag so we can tell if this is being | ||
75 | // called by the pull action and just return the value. | ||
76 | if (StatType == StatType.Pull) | ||
77 | PullAction(this); | ||
78 | |||
79 | return m_value; | ||
80 | } | ||
81 | |||
82 | set | ||
83 | { | ||
84 | m_value = value; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | private double m_value; | ||
89 | |||
90 | /// <summary> | ||
91 | /// Historical samples for calculating measures of interest average. | ||
92 | /// </summary> | ||
93 | /// <remarks> | ||
94 | /// Will be null if no measures of interest require samples. | ||
95 | /// </remarks> | ||
96 | private static Queue<double> m_samples; | ||
97 | |||
98 | /// <summary> | ||
99 | /// Maximum number of statistical samples. | ||
100 | /// </summary> | ||
101 | /// <remarks> | ||
102 | /// At the moment this corresponds to 1 minute since the sampling rate is every 2.5 seconds as triggered from | ||
103 | /// the main Watchdog. | ||
104 | /// </remarks> | ||
105 | private static int m_maxSamples = 24; | ||
106 | |||
107 | public Stat( | ||
108 | string shortName, | ||
109 | string name, | ||
110 | string description, | ||
111 | string unitName, | ||
112 | string category, | ||
113 | string container, | ||
114 | StatType type, | ||
115 | Action<Stat> pullAction, | ||
116 | StatVerbosity verbosity) | ||
117 | : this( | ||
118 | shortName, | ||
119 | name, | ||
120 | description, | ||
121 | unitName, | ||
122 | category, | ||
123 | container, | ||
124 | type, | ||
125 | MeasuresOfInterest.None, | ||
126 | pullAction, | ||
127 | verbosity) | ||
128 | { | ||
129 | } | ||
130 | |||
131 | /// <summary> | ||
132 | /// Constructor | ||
133 | /// </summary> | ||
134 | /// <param name='shortName'>Short name for the stat. Must not contain spaces. e.g. "LongFrames"</param> | ||
135 | /// <param name='name'>Human readable name for the stat. e.g. "Long frames"</param> | ||
136 | /// <param name='description'>Description of stat</param> | ||
137 | /// <param name='unitName'> | ||
138 | /// Unit name for the stat. Should be preceeded by a space if the unit name isn't normally appeneded immediately to the value. | ||
139 | /// e.g. " frames" | ||
140 | /// </param> | ||
141 | /// <param name='category'>Category under which this stat should appear, e.g. "scene". Do not capitalize.</param> | ||
142 | /// <param name='container'>Entity to which this stat relates. e.g. scene name if this is a per scene stat.</param> | ||
143 | /// <param name='type'>Push or pull</param> | ||
144 | /// <param name='pullAction'>Pull stats need an action to update the stat on request. Push stats should set null here.</param> | ||
145 | /// <param name='moi'>Measures of interest</param> | ||
146 | /// <param name='verbosity'>Verbosity of stat. Controls whether it will appear in short stat display or only full display.</param> | ||
147 | public Stat( | ||
148 | string shortName, | ||
149 | string name, | ||
150 | string description, | ||
151 | string unitName, | ||
152 | string category, | ||
153 | string container, | ||
154 | StatType type, | ||
155 | MeasuresOfInterest moi, | ||
156 | Action<Stat> pullAction, | ||
157 | StatVerbosity verbosity) | ||
158 | { | ||
159 | if (StatsManager.SubCommands.Contains(category)) | ||
160 | throw new Exception( | ||
161 | string.Format("Stat cannot be in category '{0}' since this is reserved for a subcommand", category)); | ||
162 | |||
163 | ShortName = shortName; | ||
164 | Name = name; | ||
165 | Description = description; | ||
166 | UnitName = unitName; | ||
167 | Category = category; | ||
168 | Container = container; | ||
169 | StatType = type; | ||
170 | |||
171 | if (StatType == StatType.Push && pullAction != null) | ||
172 | throw new Exception("A push stat cannot have a pull action"); | ||
173 | else | ||
174 | PullAction = pullAction; | ||
175 | |||
176 | MeasuresOfInterest = moi; | ||
177 | |||
178 | if ((moi & MeasuresOfInterest.AverageChangeOverTime) == MeasuresOfInterest.AverageChangeOverTime) | ||
179 | m_samples = new Queue<double>(m_maxSamples); | ||
180 | |||
181 | Verbosity = verbosity; | ||
182 | } | ||
183 | |||
184 | /// <summary> | ||
185 | /// Record a value in the sample set. | ||
186 | /// </summary> | ||
187 | /// <remarks> | ||
188 | /// Do not call this if MeasuresOfInterest.None | ||
189 | /// </remarks> | ||
190 | public void RecordValue() | ||
191 | { | ||
192 | double newValue = Value; | ||
193 | |||
194 | lock (m_samples) | ||
195 | { | ||
196 | if (m_samples.Count >= m_maxSamples) | ||
197 | m_samples.Dequeue(); | ||
198 | |||
199 | m_samples.Enqueue(newValue); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | public virtual string ToConsoleString() | ||
204 | { | ||
205 | StringBuilder sb = new StringBuilder(); | ||
206 | sb.AppendFormat("{0}.{1}.{2} : {3}{4}", Category, Container, ShortName, Value, UnitName); | ||
207 | |||
208 | AppendMeasuresOfInterest(sb); | ||
209 | |||
210 | return sb.ToString(); | ||
211 | } | ||
212 | |||
213 | protected void AppendMeasuresOfInterest(StringBuilder sb) | ||
214 | { | ||
215 | if ((MeasuresOfInterest & MeasuresOfInterest.AverageChangeOverTime) | ||
216 | == MeasuresOfInterest.AverageChangeOverTime) | ||
217 | { | ||
218 | double totalChange = 0; | ||
219 | double? lastSample = null; | ||
220 | |||
221 | lock (m_samples) | ||
222 | { | ||
223 | foreach (double s in m_samples) | ||
224 | { | ||
225 | if (lastSample != null) | ||
226 | totalChange += s - (double)lastSample; | ||
227 | |||
228 | lastSample = s; | ||
229 | } | ||
230 | } | ||
231 | |||
232 | int divisor = m_samples.Count <= 1 ? 1 : m_samples.Count - 1; | ||
233 | |||
234 | sb.AppendFormat(", {0:0.##}{1}/s", totalChange / divisor / (Watchdog.WATCHDOG_INTERVAL_MS / 1000), UnitName); | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } \ No newline at end of file | ||