SimExtraStatsReporter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Statistics.Interfaces;
  33. using libsecondlife;
  34. namespace OpenSim.Framework.Statistics
  35. {
  36. public class SimExtraStatsReporter
  37. {
  38. private long assetsInCache;
  39. private long texturesInCache;
  40. private long assetCacheMemoryUsage;
  41. private long textureCacheMemoryUsage;
  42. public long AssetsInCache { get { return assetsInCache; } }
  43. public long TexturesInCache { get { return texturesInCache; } }
  44. public long AssetCacheMemoryUsage { get { return assetCacheMemoryUsage; } }
  45. public long TextureCacheMemoryUsage { get { return textureCacheMemoryUsage; } }
  46. /// <summary>
  47. /// Retain a dictionary of all packet queues stats reporters
  48. /// </summary>
  49. private IDictionary<LLUUID, PacketQueueStatsReporter> packetQueueStatsReporters
  50. = new Dictionary<LLUUID, PacketQueueStatsReporter>();
  51. public void AddAsset(AssetBase asset)
  52. {
  53. assetsInCache++;
  54. assetCacheMemoryUsage += asset.Data.Length;
  55. }
  56. public void AddTexture(AssetBase image)
  57. {
  58. // Tedd: I added null check to avoid exception. Don't know if texturesInCache should ++ anyway?
  59. if (image.Data != null)
  60. {
  61. texturesInCache++;
  62. textureCacheMemoryUsage += image.Data.Length;
  63. }
  64. }
  65. /// <summary>
  66. /// Register as a packet queue stats provider
  67. /// </summary>
  68. /// <param name="uuid">An agent LLUUID</param>
  69. /// <param name="provider"></param>
  70. public void RegisterPacketQueueStatsProvider(LLUUID uuid, IPullStatsProvider provider)
  71. {
  72. lock (packetQueueStatsReporters)
  73. {
  74. packetQueueStatsReporters[uuid] = new PacketQueueStatsReporter(provider);
  75. }
  76. }
  77. /// <summary>
  78. /// Deregister a packet queue stats provider
  79. /// </summary>
  80. /// <param name="uuid">An agent LLUUID</param>
  81. public void DeregisterPacketQueueStatsProvider(LLUUID uuid)
  82. {
  83. lock (packetQueueStatsReporters)
  84. {
  85. packetQueueStatsReporters.Remove(uuid);
  86. }
  87. }
  88. /// <summary>
  89. /// Report back collected statistical information.
  90. /// </summary>
  91. /// <returns></returns>
  92. public string Report()
  93. {
  94. StringBuilder sb = new StringBuilder(Environment.NewLine);
  95. sb.Append("PACKET QUEUE STATISTICS");
  96. sb.Append(Environment.NewLine);
  97. sb.Append(
  98. string.Format(
  99. @"Asset cache contains {0,6} assets using {1,10:0.000}K
  100. Texture cache contains {2,6} textures using {3,10:0.000}K" + Environment.NewLine,
  101. AssetsInCache, AssetCacheMemoryUsage / 1024.0,
  102. TexturesInCache, TextureCacheMemoryUsage / 1024.0));
  103. sb.Append(Environment.NewLine);
  104. sb.Append("PACKET QUEUE STATISTICS");
  105. sb.Append(Environment.NewLine);
  106. sb.Append("Agent UUID ");
  107. sb.Append(" Send In Out Resend ");
  108. sb.Append(" Land Wind Cloud Task Texture Asset");
  109. sb.Append(Environment.NewLine);
  110. foreach (LLUUID key in packetQueueStatsReporters.Keys)
  111. {
  112. sb.Append(string.Format("{0}: ", key));
  113. sb.Append(packetQueueStatsReporters[key].Report());
  114. sb.Append(Environment.NewLine);
  115. }
  116. return sb.ToString();
  117. }
  118. }
  119. /// <summary>
  120. /// Pull packet queue stats from packet queues and report
  121. /// </summary>
  122. public class PacketQueueStatsReporter
  123. {
  124. private IPullStatsProvider m_statsProvider;
  125. public PacketQueueStatsReporter(IPullStatsProvider provider)
  126. {
  127. m_statsProvider = provider;
  128. }
  129. /// <summary>
  130. /// Report back collected statistical information.
  131. /// </summary>
  132. /// <returns></returns>
  133. public string Report()
  134. {
  135. return m_statsProvider.GetStats();
  136. }
  137. }
  138. }