SimExtraStatsReporter.cs 6.0 KB

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