EventHistogram.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // Create a time histogram of events. The histogram is built in a wrap-around
  35. // array of equally distributed buckets.
  36. // For instance, a minute long histogram of second sized buckets would be:
  37. // new EventHistogram(60, 1000)
  38. public class EventHistogram
  39. {
  40. private int m_timeBase;
  41. private int m_numBuckets;
  42. private int m_bucketMilliseconds;
  43. private int m_lastBucket;
  44. private int m_totalHistogramMilliseconds;
  45. private long[] m_histogram;
  46. private object histoLock = new object();
  47. public EventHistogram(int numberOfBuckets, int millisecondsPerBucket)
  48. {
  49. m_numBuckets = numberOfBuckets;
  50. m_bucketMilliseconds = millisecondsPerBucket;
  51. m_totalHistogramMilliseconds = m_numBuckets * m_bucketMilliseconds;
  52. m_histogram = new long[m_numBuckets];
  53. Zero();
  54. m_lastBucket = 0;
  55. m_timeBase = Util.EnvironmentTickCount();
  56. }
  57. public void Event()
  58. {
  59. this.Event(1);
  60. }
  61. // Record an event at time 'now' in the histogram.
  62. public void Event(int cnt)
  63. {
  64. lock (histoLock)
  65. {
  66. // The time as displaced from the base of the histogram
  67. int bucketTime = Util.EnvironmentTickCountSubtract(m_timeBase);
  68. // If more than the total time of the histogram, we just start over
  69. if (bucketTime > m_totalHistogramMilliseconds)
  70. {
  71. Zero();
  72. m_lastBucket = 0;
  73. m_timeBase = Util.EnvironmentTickCount();
  74. }
  75. else
  76. {
  77. // To which bucket should we add this event?
  78. int bucket = bucketTime / m_bucketMilliseconds;
  79. // Advance m_lastBucket to the new bucket. Zero any buckets skipped over.
  80. while (bucket != m_lastBucket)
  81. {
  82. // Zero from just after the last bucket to the new bucket or the end
  83. for (int jj = m_lastBucket + 1; jj <= Math.Min(bucket, m_numBuckets - 1); jj++)
  84. {
  85. m_histogram[jj] = 0;
  86. }
  87. m_lastBucket = bucket;
  88. // If the new bucket is off the end, wrap around to the beginning
  89. if (bucket > m_numBuckets)
  90. {
  91. bucket -= m_numBuckets;
  92. m_lastBucket = 0;
  93. m_histogram[m_lastBucket] = 0;
  94. m_timeBase += m_totalHistogramMilliseconds;
  95. }
  96. }
  97. }
  98. m_histogram[m_lastBucket] += cnt;
  99. }
  100. }
  101. // Get a copy of the current histogram
  102. public long[] GetHistogram()
  103. {
  104. long[] ret = new long[m_numBuckets];
  105. lock (histoLock)
  106. {
  107. int indx = m_lastBucket + 1;
  108. for (int ii = 0; ii < m_numBuckets; ii++, indx++)
  109. {
  110. if (indx >= m_numBuckets)
  111. indx = 0;
  112. ret[ii] = m_histogram[indx];
  113. }
  114. }
  115. return ret;
  116. }
  117. public OSDMap GetHistogramAsOSDMap()
  118. {
  119. OSDMap ret = new OSDMap();
  120. ret.Add("Buckets", OSD.FromInteger(m_numBuckets));
  121. ret.Add("BucketMilliseconds", OSD.FromInteger(m_bucketMilliseconds));
  122. ret.Add("TotalMilliseconds", OSD.FromInteger(m_totalHistogramMilliseconds));
  123. // Compute a number for the first bucket in the histogram.
  124. // This will allow readers to know how this histogram relates to any previously read histogram.
  125. int baseBucketNum = (m_timeBase / m_bucketMilliseconds) + m_lastBucket + 1;
  126. ret.Add("BaseNumber", OSD.FromInteger(baseBucketNum));
  127. ret.Add("Values", GetHistogramAsOSDArray());
  128. return ret;
  129. }
  130. // Get a copy of the current histogram
  131. public OSDArray GetHistogramAsOSDArray()
  132. {
  133. OSDArray ret = new OSDArray(m_numBuckets);
  134. lock (histoLock)
  135. {
  136. int indx = m_lastBucket + 1;
  137. for (int ii = 0; ii < m_numBuckets; ii++, indx++)
  138. {
  139. if (indx >= m_numBuckets)
  140. indx = 0;
  141. ret[ii] = OSD.FromLong(m_histogram[indx]);
  142. }
  143. }
  144. return ret;
  145. }
  146. // Zero out the histogram
  147. public void Zero()
  148. {
  149. lock (histoLock)
  150. {
  151. for (int ii = 0; ii < m_numBuckets; ii++)
  152. m_histogram[ii] = 0;
  153. }
  154. }
  155. }
  156. }