29 using System.Collections.Generic;
31 using System.Reflection;
34 using OpenMetaverse.StructuredData;
36 namespace OpenSim.Framework.Monitoring
41 public class Stat : IDisposable
45 public static readonly
char[] DisallowedShortNameCharacters = {
'.' };
50 public string Category {
get;
private set; }
60 public string Container {
get;
private set; }
69 public Action<Stat> PullAction {
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; }
77 public virtual double Value
96 private double m_value;
104 private Queue<double> m_samples;
113 private static int m_maxSamples = 24;
123 Action<Stat> pullAction,
164 Action<Stat> pullAction,
167 if (StatsManager.SubCommands.Contains(category))
169 string.Format(
"Stat cannot be in category '{0}' since this is reserved for a subcommand", category));
171 foreach (
char c
in DisallowedShortNameCharacters)
173 if (shortName.IndexOf(c) != -1)
174 shortName = shortName.Replace(c,
'#');
178 ShortName = shortName;
180 Description = description;
183 Container = container;
187 throw new Exception(
"A push stat cannot have a pull action");
189 PullAction = pullAction;
194 m_samples =
new Queue<double>(m_maxSamples);
196 Verbosity = verbosity;
213 double newValue = Value;
217 if (m_samples.Count >= m_maxSamples)
222 m_samples.Enqueue(newValue);
228 StringBuilder sb =
new StringBuilder();
230 "{0}.{1}.{2} : {3}{4}",
235 string.IsNullOrEmpty(UnitName) ?
"" :
string.Format(
" {0}", UnitName));
237 AppendMeasuresOfInterest(sb);
239 return sb.ToString();
246 ret.Add(
"Value", OSD.FromReal(Value));
248 double lastChangeOverTime, averageChangeOverTime;
256 ret.Add(
"StatType",
"Stat");
258 ret.Add(
"Category", OSD.FromString(Category));
259 ret.Add(
"Container", OSD.FromString(Container));
260 ret.Add(
"ShortName", OSD.FromString(ShortName));
261 ret.Add(
"Name", OSD.FromString(Name));
262 ret.Add(
"Description", OSD.FromString(Description));
263 ret.Add(
"UnitName", OSD.FromString(UnitName));
264 ret.Add(
"Value", OSD.FromReal(Value));
266 double lastChangeOverTime, averageChangeOverTime;
267 if (ComputeMeasuresOfInterest(out lastChangeOverTime, out averageChangeOverTime))
269 ret.Add(
"LastChangeOverTime", OSD.FromReal(lastChangeOverTime));
270 ret.Add(
"AverageChangeOverTime", OSD.FromReal(averageChangeOverTime));
281 lastChangeOverTime = 0;
282 averageChangeOverTime = 0;
286 double totalChange = 0;
287 double? penultimateSample = null;
288 double? lastSample = null;
296 foreach (
double s
in m_samples)
298 if (lastSample != null)
299 totalChange += s - (double)lastSample;
301 penultimateSample = lastSample;
306 if (lastSample != null && penultimateSample != null)
309 = ((double)lastSample - (
double)penultimateSample) / (Watchdog.WATCHDOG_INTERVAL_MS / 1000);
312 int divisor = m_samples.Count <= 1 ? 1 : m_samples.Count - 1;
314 averageChangeOverTime = totalChange / divisor / (Watchdog.WATCHDOG_INTERVAL_MS / 1000);
323 double lastChangeOverTime = 0;
324 double averageChangeOverTime = 0;
326 if (ComputeMeasuresOfInterest(out lastChangeOverTime, out averageChangeOverTime))
329 ", {0:0.##}{1}/s, {2:0.##}{3}/s",
331 string.IsNullOrEmpty(UnitName) ?
"" :
string.Format(
" {0}", UnitName),
332 averageChangeOverTime,
333 string.IsNullOrEmpty(UnitName) ?
"" :
string.Format(
" {0}", UnitName));
virtual OSDMap ToBriefOSDMap()
virtual string ToConsoleString()
OpenMetaverse.StructuredData.OSDMap OSDMap
Holds individual statistic details
Stat(string shortName, string name, string description, string unitName, string category, string container, StatType type, MeasuresOfInterest moi, Action< Stat > pullAction, StatVerbosity verbosity)
Constructor
virtual OSDMap ToOSDMap()
void AppendMeasuresOfInterest(StringBuilder sb)
Stat(string shortName, string name, string description, string unitName, string category, string container, StatType type, Action< Stat > pullAction, StatVerbosity verbosity)
void RecordValue()
Record a value in the sample set.
StatVerbosity
Verbosity of stat.
bool ComputeMeasuresOfInterest(out double lastChangeOverTime, out double averageChangeOverTime)
MeasuresOfInterest
Measures of interest for this stat.