opensim-development – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.Linq; |
||
31 | using System.Reflection; |
||
32 | using System.Text; |
||
33 | using log4net; |
||
34 | using OpenMetaverse.StructuredData; |
||
35 | |||
36 | namespace OpenSim.Framework.Monitoring |
||
37 | { |
||
38 | /// <summary> |
||
39 | /// Holds individual statistic details |
||
40 | /// </summary> |
||
41 | public class Stat : IDisposable |
||
42 | { |
||
43 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
44 | |||
45 | public static readonly char[] DisallowedShortNameCharacters = { '.' }; |
||
46 | |||
47 | /// <summary> |
||
48 | /// Category of this stat (e.g. cache, scene, etc). |
||
49 | /// </summary> |
||
50 | public string Category { get; private set; } |
||
51 | |||
52 | /// <summary> |
||
53 | /// Containing name for this stat. |
||
54 | /// FIXME: In the case of a scene, this is currently the scene name (though this leaves |
||
55 | /// us with a to-be-resolved problem of non-unique region names). |
||
56 | /// </summary> |
||
57 | /// <value> |
||
58 | /// The container. |
||
59 | /// </value> |
||
60 | public string Container { get; private set; } |
||
61 | |||
62 | public StatType StatType { get; private set; } |
||
63 | |||
64 | public MeasuresOfInterest MeasuresOfInterest { get; private set; } |
||
65 | |||
66 | /// <summary> |
||
67 | /// Action used to update this stat when the value is requested if it's a pull type. |
||
68 | /// </summary> |
||
69 | public Action<Stat> PullAction { get; private set; } |
||
70 | |||
71 | public StatVerbosity Verbosity { get; private set; } |
||
72 | public string ShortName { get; private set; } |
||
73 | public string Name { get; private set; } |
||
74 | public string Description { get; private set; } |
||
75 | public virtual string UnitName { get; private set; } |
||
76 | |||
77 | public virtual double Value |
||
78 | { |
||
79 | get |
||
80 | { |
||
81 | // Asking for an update here means that the updater cannot access this value without infinite recursion. |
||
82 | // XXX: A slightly messy but simple solution may be to flick a flag so we can tell if this is being |
||
83 | // called by the pull action and just return the value. |
||
84 | if (StatType == StatType.Pull) |
||
85 | PullAction(this); |
||
86 | |||
87 | return m_value; |
||
88 | } |
||
89 | |||
90 | set |
||
91 | { |
||
92 | m_value = value; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | private double m_value; |
||
97 | |||
98 | /// <summary> |
||
99 | /// Historical samples for calculating measures of interest average. |
||
100 | /// </summary> |
||
101 | /// <remarks> |
||
102 | /// Will be null if no measures of interest require samples. |
||
103 | /// </remarks> |
||
104 | private Queue<double> m_samples; |
||
105 | |||
106 | /// <summary> |
||
107 | /// Maximum number of statistical samples. |
||
108 | /// </summary> |
||
109 | /// <remarks> |
||
110 | /// At the moment this corresponds to 1 minute since the sampling rate is every 2.5 seconds as triggered from |
||
111 | /// the main Watchdog. |
||
112 | /// </remarks> |
||
113 | private static int m_maxSamples = 24; |
||
114 | |||
115 | public Stat( |
||
116 | string shortName, |
||
117 | string name, |
||
118 | string description, |
||
119 | string unitName, |
||
120 | string category, |
||
121 | string container, |
||
122 | StatType type, |
||
123 | Action<Stat> pullAction, |
||
124 | StatVerbosity verbosity) |
||
125 | : this( |
||
126 | shortName, |
||
127 | name, |
||
128 | description, |
||
129 | unitName, |
||
130 | category, |
||
131 | container, |
||
132 | type, |
||
133 | MeasuresOfInterest.None, |
||
134 | pullAction, |
||
135 | verbosity) |
||
136 | { |
||
137 | } |
||
138 | |||
139 | /// <summary> |
||
140 | /// Constructor |
||
141 | /// </summary> |
||
142 | /// <param name='shortName'>Short name for the stat. Must not contain spaces. e.g. "LongFrames"</param> |
||
143 | /// <param name='name'>Human readable name for the stat. e.g. "Long frames"</param> |
||
144 | /// <param name='description'>Description of stat</param> |
||
145 | /// <param name='unitName'> |
||
146 | /// Unit name for the stat. Should be preceeded by a space if the unit name isn't normally appeneded immediately to the value. |
||
147 | /// e.g. " frames" |
||
148 | /// </param> |
||
149 | /// <param name='category'>Category under which this stat should appear, e.g. "scene". Do not capitalize.</param> |
||
150 | /// <param name='container'>Entity to which this stat relates. e.g. scene name if this is a per scene stat.</param> |
||
151 | /// <param name='type'>Push or pull</param> |
||
152 | /// <param name='pullAction'>Pull stats need an action to update the stat on request. Push stats should set null here.</param> |
||
153 | /// <param name='moi'>Measures of interest</param> |
||
154 | /// <param name='verbosity'>Verbosity of stat. Controls whether it will appear in short stat display or only full display.</param> |
||
155 | public Stat( |
||
156 | string shortName, |
||
157 | string name, |
||
158 | string description, |
||
159 | string unitName, |
||
160 | string category, |
||
161 | string container, |
||
162 | StatType type, |
||
163 | MeasuresOfInterest moi, |
||
164 | Action<Stat> pullAction, |
||
165 | StatVerbosity verbosity) |
||
166 | { |
||
167 | if (StatsManager.SubCommands.Contains(category)) |
||
168 | throw new Exception( |
||
169 | string.Format("Stat cannot be in category '{0}' since this is reserved for a subcommand", category)); |
||
170 | |||
171 | foreach (char c in DisallowedShortNameCharacters) |
||
172 | { |
||
173 | if (shortName.IndexOf(c) != -1) |
||
174 | throw new Exception(string.Format("Stat name {0} cannot contain character {1}", shortName, c)); |
||
175 | } |
||
176 | |||
177 | ShortName = shortName; |
||
178 | Name = name; |
||
179 | Description = description; |
||
180 | UnitName = unitName; |
||
181 | Category = category; |
||
182 | Container = container; |
||
183 | StatType = type; |
||
184 | |||
185 | if (StatType == StatType.Push && pullAction != null) |
||
186 | throw new Exception("A push stat cannot have a pull action"); |
||
187 | else |
||
188 | PullAction = pullAction; |
||
189 | |||
190 | MeasuresOfInterest = moi; |
||
191 | |||
192 | if ((moi & MeasuresOfInterest.AverageChangeOverTime) == MeasuresOfInterest.AverageChangeOverTime) |
||
193 | m_samples = new Queue<double>(m_maxSamples); |
||
194 | |||
195 | Verbosity = verbosity; |
||
196 | } |
||
197 | |||
198 | // IDisposable.Dispose() |
||
199 | public virtual void Dispose() |
||
200 | { |
||
201 | return; |
||
202 | } |
||
203 | |||
204 | /// <summary> |
||
205 | /// Record a value in the sample set. |
||
206 | /// </summary> |
||
207 | /// <remarks> |
||
208 | /// Do not call this if MeasuresOfInterest.None |
||
209 | /// </remarks> |
||
210 | public void RecordValue() |
||
211 | { |
||
212 | double newValue = Value; |
||
213 | |||
214 | lock (m_samples) |
||
215 | { |
||
216 | if (m_samples.Count >= m_maxSamples) |
||
217 | m_samples.Dequeue(); |
||
218 | |||
219 | // m_log.DebugFormat("[STAT]: Recording value {0} for {1}", newValue, Name); |
||
220 | |||
221 | m_samples.Enqueue(newValue); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | public virtual string ToConsoleString() |
||
226 | { |
||
227 | StringBuilder sb = new StringBuilder(); |
||
228 | sb.AppendFormat( |
||
229 | "{0}.{1}.{2} : {3}{4}", |
||
230 | Category, |
||
231 | Container, |
||
232 | ShortName, |
||
233 | Value, |
||
234 | string.IsNullOrEmpty(UnitName) ? "" : string.Format(" {0}", UnitName)); |
||
235 | |||
236 | AppendMeasuresOfInterest(sb); |
||
237 | |||
238 | return sb.ToString(); |
||
239 | } |
||
240 | |||
241 | public virtual OSDMap ToOSDMap() |
||
242 | { |
||
243 | OSDMap ret = new OSDMap(); |
||
244 | ret.Add("StatType", "Stat"); // used by overloading classes to denote type of stat |
||
245 | |||
246 | ret.Add("Category", OSD.FromString(Category)); |
||
247 | ret.Add("Container", OSD.FromString(Container)); |
||
248 | ret.Add("ShortName", OSD.FromString(ShortName)); |
||
249 | ret.Add("Name", OSD.FromString(Name)); |
||
250 | ret.Add("Description", OSD.FromString(Description)); |
||
251 | ret.Add("UnitName", OSD.FromString(UnitName)); |
||
252 | ret.Add("Value", OSD.FromReal(Value)); |
||
253 | |||
254 | double lastChangeOverTime, averageChangeOverTime; |
||
255 | if (ComputeMeasuresOfInterest(out lastChangeOverTime, out averageChangeOverTime)) |
||
256 | { |
||
257 | ret.Add("LastChangeOverTime", OSD.FromReal(lastChangeOverTime)); |
||
258 | ret.Add("AverageChangeOverTime", OSD.FromReal(averageChangeOverTime)); |
||
259 | } |
||
260 | |||
261 | return ret; |
||
262 | } |
||
263 | |||
264 | // Compute the averages over time and return same. |
||
265 | // Return 'true' if averages were actually computed. 'false' if no average info. |
||
266 | public bool ComputeMeasuresOfInterest(out double lastChangeOverTime, out double averageChangeOverTime) |
||
267 | { |
||
268 | bool ret = false; |
||
269 | lastChangeOverTime = 0; |
||
270 | averageChangeOverTime = 0; |
||
271 | |||
272 | if ((MeasuresOfInterest & MeasuresOfInterest.AverageChangeOverTime) == MeasuresOfInterest.AverageChangeOverTime) |
||
273 | { |
||
274 | double totalChange = 0; |
||
275 | double? penultimateSample = null; |
||
276 | double? lastSample = null; |
||
277 | |||
278 | lock (m_samples) |
||
279 | { |
||
280 | // m_log.DebugFormat( |
||
281 | // "[STAT]: Samples for {0} are {1}", |
||
282 | // Name, string.Join(",", m_samples.Select(s => s.ToString()).ToArray())); |
||
283 | |||
284 | foreach (double s in m_samples) |
||
285 | { |
||
286 | if (lastSample != null) |
||
287 | totalChange += s - (double)lastSample; |
||
288 | |||
289 | penultimateSample = lastSample; |
||
290 | lastSample = s; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | if (lastSample != null && penultimateSample != null) |
||
295 | { |
||
296 | lastChangeOverTime |
||
297 | = ((double)lastSample - (double)penultimateSample) / (Watchdog.WATCHDOG_INTERVAL_MS / 1000); |
||
298 | } |
||
299 | |||
300 | int divisor = m_samples.Count <= 1 ? 1 : m_samples.Count - 1; |
||
301 | |||
302 | averageChangeOverTime = totalChange / divisor / (Watchdog.WATCHDOG_INTERVAL_MS / 1000); |
||
303 | ret = true; |
||
304 | } |
||
305 | |||
306 | return ret; |
||
307 | } |
||
308 | |||
309 | protected void AppendMeasuresOfInterest(StringBuilder sb) |
||
310 | { |
||
311 | double lastChangeOverTime = 0; |
||
312 | double averageChangeOverTime = 0; |
||
313 | |||
314 | if (ComputeMeasuresOfInterest(out lastChangeOverTime, out averageChangeOverTime)) |
||
315 | { |
||
316 | sb.AppendFormat( |
||
317 | ", {0:0.##}{1}/s, {2:0.##}{3}/s", |
||
318 | lastChangeOverTime, |
||
319 | string.IsNullOrEmpty(UnitName) ? "" : string.Format(" {0}", UnitName), |
||
320 | averageChangeOverTime, |
||
321 | string.IsNullOrEmpty(UnitName) ? "" : string.Format(" {0}", UnitName)); |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } |