Stat.cs 8.8 KB

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