SimExtraStatsReporter.cs 6.0 KB

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