1
0

CounterStat.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // A statistic that wraps a counter.
  35. // Built this way mostly so histograms and history can be created.
  36. public class CounterStat : Stat
  37. {
  38. private SortedDictionary<string, EventHistogram> m_histograms;
  39. private object counterLock = new object();
  40. public CounterStat(
  41. string shortName,
  42. string name,
  43. string description,
  44. string unitName,
  45. string category,
  46. string container,
  47. StatVerbosity verbosity)
  48. : base(shortName, name, description, unitName, category, container, StatType.Push, null, verbosity)
  49. {
  50. m_histograms = new SortedDictionary<string, EventHistogram>();
  51. }
  52. // Histograms are presumably added at intialization time and the list does not change thereafter.
  53. // Thus no locking of the histogram list.
  54. public void AddHistogram(string histoName, EventHistogram histo)
  55. {
  56. m_histograms.Add(histoName, histo);
  57. }
  58. public delegate void ProcessHistogram(string name, EventHistogram histo);
  59. public void ForEachHistogram(ProcessHistogram process)
  60. {
  61. foreach (KeyValuePair<string, EventHistogram> kvp in m_histograms)
  62. {
  63. process(kvp.Key, kvp.Value);
  64. }
  65. }
  66. public void Event()
  67. {
  68. this.Event(1);
  69. }
  70. // Count the underlying counter.
  71. public void Event(int cnt)
  72. {
  73. lock (counterLock)
  74. {
  75. base.Value += cnt;
  76. foreach (EventHistogram histo in m_histograms.Values)
  77. {
  78. histo.Event(cnt);
  79. }
  80. }
  81. }
  82. // CounterStat is a basic stat plus histograms
  83. public override OSDMap ToOSDMap()
  84. {
  85. // Get the foundational instance
  86. OSDMap map = base.ToOSDMap();
  87. map["StatType"] = "CounterStat";
  88. // If there are any histograms, add a new field that is an array of histograms as OSDMaps
  89. if (m_histograms.Count > 0)
  90. {
  91. lock (counterLock)
  92. {
  93. if (m_histograms.Count > 0)
  94. {
  95. OSDArray histos = new OSDArray();
  96. foreach (EventHistogram histo in m_histograms.Values)
  97. {
  98. histos.Add(histo.GetHistogramAsOSDMap());
  99. }
  100. map.Add("Histograms", histos);
  101. }
  102. }
  103. }
  104. return map;
  105. }
  106. }
  107. }