StatsManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.Linq;
  30. using System.Text;
  31. using OpenMetaverse.StructuredData;
  32. namespace OpenSim.Framework.Monitoring
  33. {
  34. /// <summary>
  35. /// Singleton used to provide access to statistics reporters
  36. /// </summary>
  37. public class StatsManager
  38. {
  39. // Subcommand used to list other stats.
  40. public const string AllSubCommand = "all";
  41. // Subcommand used to list other stats.
  42. public const string ListSubCommand = "list";
  43. // All subcommands
  44. public static HashSet<string> SubCommands = new HashSet<string> { AllSubCommand, ListSubCommand };
  45. /// <summary>
  46. /// Registered stats categorized by category/container/shortname
  47. /// </summary>
  48. /// <remarks>
  49. /// Do not add or remove directly from this dictionary.
  50. /// </remarks>
  51. public static SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>> RegisteredStats
  52. = new SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>>();
  53. // private static AssetStatsCollector assetStats;
  54. // private static UserStatsCollector userStats;
  55. // private static SimExtraStatsCollector simExtraStats = new SimExtraStatsCollector();
  56. // public static AssetStatsCollector AssetStats { get { return assetStats; } }
  57. // public static UserStatsCollector UserStats { get { return userStats; } }
  58. public static SimExtraStatsCollector SimExtraStats { get; set; }
  59. public static void RegisterConsoleCommands(ICommandConsole console)
  60. {
  61. console.Commands.AddCommand(
  62. "General",
  63. false,
  64. "show stats",
  65. "show stats [list|all|(<category>[.<container>])+",
  66. "Show statistical information for this server",
  67. "If no final argument is specified then legacy statistics information is currently shown.\n"
  68. + "'list' argument will show statistic categories.\n"
  69. + "'all' will show all statistics.\n"
  70. + "A <category> name will show statistics from that category.\n"
  71. + "A <category>.<container> name will show statistics from that category in that container.\n"
  72. + "More than one name can be given separated by spaces.\n"
  73. + "THIS STATS FACILITY IS EXPERIMENTAL AND DOES NOT YET CONTAIN ALL STATS",
  74. HandleShowStatsCommand);
  75. }
  76. public static void HandleShowStatsCommand(string module, string[] cmd)
  77. {
  78. ICommandConsole con = MainConsole.Instance;
  79. if (cmd.Length > 2)
  80. {
  81. foreach (string name in cmd.Skip(2))
  82. {
  83. string[] components = name.Split('.');
  84. string categoryName = components[0];
  85. string containerName = components.Length > 1 ? components[1] : null;
  86. if (categoryName == AllSubCommand)
  87. {
  88. OutputAllStatsToConsole(con);
  89. }
  90. else if (categoryName == ListSubCommand)
  91. {
  92. con.Output("Statistic categories available are:");
  93. foreach (string category in RegisteredStats.Keys)
  94. con.OutputFormat(" {0}", category);
  95. }
  96. else
  97. {
  98. SortedDictionary<string, SortedDictionary<string, Stat>> category;
  99. if (!RegisteredStats.TryGetValue(categoryName, out category))
  100. {
  101. con.OutputFormat("No such category as {0}", categoryName);
  102. }
  103. else
  104. {
  105. if (String.IsNullOrEmpty(containerName))
  106. {
  107. OutputCategoryStatsToConsole(con, category);
  108. }
  109. else
  110. {
  111. SortedDictionary<string, Stat> container;
  112. if (category.TryGetValue(containerName, out container))
  113. {
  114. OutputContainerStatsToConsole(con, container);
  115. }
  116. else
  117. {
  118. con.OutputFormat("No such container {0} in category {1}", containerName, categoryName);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. else
  126. {
  127. // Legacy
  128. if (SimExtraStats != null)
  129. con.Output(SimExtraStats.Report());
  130. else
  131. OutputAllStatsToConsole(con);
  132. }
  133. }
  134. private static void OutputAllStatsToConsole(ICommandConsole con)
  135. {
  136. foreach (var category in RegisteredStats.Values)
  137. {
  138. OutputCategoryStatsToConsole(con, category);
  139. }
  140. }
  141. private static void OutputCategoryStatsToConsole(
  142. ICommandConsole con, SortedDictionary<string, SortedDictionary<string, Stat>> category)
  143. {
  144. foreach (var container in category.Values)
  145. {
  146. OutputContainerStatsToConsole(con, container);
  147. }
  148. }
  149. private static void OutputContainerStatsToConsole( ICommandConsole con, SortedDictionary<string, Stat> container)
  150. {
  151. foreach (Stat stat in container.Values)
  152. {
  153. con.Output(stat.ToConsoleString());
  154. }
  155. }
  156. // Creates an OSDMap of the format:
  157. // { categoryName: {
  158. // containerName: {
  159. // statName: {
  160. // "Name": name,
  161. // "ShortName": shortName,
  162. // ...
  163. // },
  164. // statName: {
  165. // "Name": name,
  166. // "ShortName": shortName,
  167. // ...
  168. // },
  169. // ...
  170. // },
  171. // containerName: {
  172. // ...
  173. // },
  174. // ...
  175. // },
  176. // categoryName: {
  177. // ...
  178. // },
  179. // ...
  180. // }
  181. // The passed in parameters will filter the categories, containers and stats returned. If any of the
  182. // parameters are either EmptyOrNull or the AllSubCommand value, all of that type will be returned.
  183. // Case matters.
  184. public static OSDMap GetStatsAsOSDMap(string pCategoryName, string pContainerName, string pStatName)
  185. {
  186. OSDMap map = new OSDMap();
  187. foreach (string catName in RegisteredStats.Keys)
  188. {
  189. // Do this category if null spec, "all" subcommand or category name matches passed parameter.
  190. // Skip category if none of the above.
  191. if (!(String.IsNullOrEmpty(pCategoryName) || pCategoryName == AllSubCommand || pCategoryName == catName))
  192. continue;
  193. OSDMap contMap = new OSDMap();
  194. foreach (string contName in RegisteredStats[catName].Keys)
  195. {
  196. if (!(string.IsNullOrEmpty(pContainerName) || pContainerName == AllSubCommand || pContainerName == contName))
  197. continue;
  198. OSDMap statMap = new OSDMap();
  199. SortedDictionary<string, Stat> theStats = RegisteredStats[catName][contName];
  200. foreach (string statName in theStats.Keys)
  201. {
  202. if (!(String.IsNullOrEmpty(pStatName) || pStatName == AllSubCommand || pStatName == statName))
  203. continue;
  204. statMap.Add(statName, theStats[statName].ToOSDMap());
  205. }
  206. contMap.Add(contName, statMap);
  207. }
  208. map.Add(catName, contMap);
  209. }
  210. return map;
  211. }
  212. // /// <summary>
  213. // /// Start collecting statistics related to assets.
  214. // /// Should only be called once.
  215. // /// </summary>
  216. // public static AssetStatsCollector StartCollectingAssetStats()
  217. // {
  218. // assetStats = new AssetStatsCollector();
  219. //
  220. // return assetStats;
  221. // }
  222. //
  223. // /// <summary>
  224. // /// Start collecting statistics related to users.
  225. // /// Should only be called once.
  226. // /// </summary>
  227. // public static UserStatsCollector StartCollectingUserStats()
  228. // {
  229. // userStats = new UserStatsCollector();
  230. //
  231. // return userStats;
  232. // }
  233. /// <summary>
  234. /// Registers a statistic.
  235. /// </summary>
  236. /// <param name='stat'></param>
  237. /// <returns></returns>
  238. public static bool RegisterStat(Stat stat)
  239. {
  240. SortedDictionary<string, SortedDictionary<string, Stat>> category = null, newCategory;
  241. SortedDictionary<string, Stat> container = null, newContainer;
  242. lock (RegisteredStats)
  243. {
  244. // Stat name is not unique across category/container/shortname key.
  245. // XXX: For now just return false. This is to avoid problems in regression tests where all tests
  246. // in a class are run in the same instance of the VM.
  247. if (TryGetStat(stat, out category, out container))
  248. return false;
  249. // We take a copy-on-write approach here of replacing dictionaries when keys are added or removed.
  250. // This means that we don't need to lock or copy them on iteration, which will be a much more
  251. // common operation after startup.
  252. if (container != null)
  253. newContainer = new SortedDictionary<string, Stat>(container);
  254. else
  255. newContainer = new SortedDictionary<string, Stat>();
  256. if (category != null)
  257. newCategory = new SortedDictionary<string, SortedDictionary<string, Stat>>(category);
  258. else
  259. newCategory = new SortedDictionary<string, SortedDictionary<string, Stat>>();
  260. newContainer[stat.ShortName] = stat;
  261. newCategory[stat.Container] = newContainer;
  262. RegisteredStats[stat.Category] = newCategory;
  263. }
  264. return true;
  265. }
  266. /// <summary>
  267. /// Deregister a statistic
  268. /// </summary>>
  269. /// <param name='stat'></param>
  270. /// <returns></returns>
  271. public static bool DeregisterStat(Stat stat)
  272. {
  273. SortedDictionary<string, SortedDictionary<string, Stat>> category = null, newCategory;
  274. SortedDictionary<string, Stat> container = null, newContainer;
  275. lock (RegisteredStats)
  276. {
  277. if (!TryGetStat(stat, out category, out container))
  278. return false;
  279. newContainer = new SortedDictionary<string, Stat>(container);
  280. newContainer.Remove(stat.ShortName);
  281. newCategory = new SortedDictionary<string, SortedDictionary<string, Stat>>(category);
  282. newCategory.Remove(stat.Container);
  283. newCategory[stat.Container] = newContainer;
  284. RegisteredStats[stat.Category] = newCategory;
  285. return true;
  286. }
  287. }
  288. public static bool TryGetStats(string category, out SortedDictionary<string, SortedDictionary<string, Stat>> stats)
  289. {
  290. return RegisteredStats.TryGetValue(category, out stats);
  291. }
  292. public static bool TryGetStat(
  293. Stat stat,
  294. out SortedDictionary<string, SortedDictionary<string, Stat>> category,
  295. out SortedDictionary<string, Stat> container)
  296. {
  297. category = null;
  298. container = null;
  299. lock (RegisteredStats)
  300. {
  301. if (RegisteredStats.TryGetValue(stat.Category, out category))
  302. {
  303. if (category.TryGetValue(stat.Container, out container))
  304. {
  305. if (container.ContainsKey(stat.ShortName))
  306. return true;
  307. }
  308. }
  309. }
  310. return false;
  311. }
  312. public static void RecordStats()
  313. {
  314. lock (RegisteredStats)
  315. {
  316. foreach (SortedDictionary<string, SortedDictionary<string, Stat>> category in RegisteredStats.Values)
  317. {
  318. foreach (SortedDictionary<string, Stat> container in category.Values)
  319. {
  320. foreach (Stat stat in container.Values)
  321. {
  322. if (stat.MeasuresOfInterest != MeasuresOfInterest.None)
  323. stat.RecordValue();
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// Stat type.
  332. /// </summary>
  333. /// <remarks>
  334. /// A push stat is one which is continually updated and so it's value can simply by read.
  335. /// A pull stat is one where reading the value triggers a collection method - the stat is not continually updated.
  336. /// </remarks>
  337. public enum StatType
  338. {
  339. Push,
  340. Pull
  341. }
  342. /// <summary>
  343. /// Measures of interest for this stat.
  344. /// </summary>
  345. [Flags]
  346. public enum MeasuresOfInterest
  347. {
  348. None,
  349. AverageChangeOverTime
  350. }
  351. /// <summary>
  352. /// Verbosity of stat.
  353. /// </summary>
  354. /// <remarks>
  355. /// Info will always be displayed.
  356. /// </remarks>
  357. public enum StatVerbosity
  358. {
  359. Debug,
  360. Info
  361. }
  362. }