SimExtraStatsCollector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /// <summary>
  35. /// Collects sim statistics which aren't already being collected for the linden viewer's statistics pane
  36. /// </summary>
  37. public class SimExtraStatsCollector : BaseStatsCollector
  38. {
  39. private long abnormalClientThreadTerminations;
  40. private long assetsInCache;
  41. private long texturesInCache;
  42. private long assetCacheMemoryUsage;
  43. private long textureCacheMemoryUsage;
  44. private long blockedMissingTextureRequests;
  45. private long assetServiceRequestFailures;
  46. private long inventoryServiceRetrievalFailures;
  47. /// <summary>
  48. /// Number of times that a client thread terminated because of an exception
  49. /// </summary>
  50. public long AbnormalClientThreadTerminations { get { return abnormalClientThreadTerminations; } }
  51. /// <summary>
  52. /// These statistics are being collected by push rather than pull. Pull would be simpler, but I had the
  53. /// notion of providing some flow statistics (which pull wouldn't give us). Though admittedly these
  54. /// haven't yet been implemented... :)
  55. /// </summary>
  56. public long AssetsInCache { get { return assetsInCache; } }
  57. public long TexturesInCache { get { return texturesInCache; } }
  58. public long AssetCacheMemoryUsage { get { return assetCacheMemoryUsage; } }
  59. public long TextureCacheMemoryUsage { get { return textureCacheMemoryUsage; } }
  60. /// <summary>
  61. /// Number of persistent requests for missing textures we have started blocking from clients. To some extent
  62. /// this is just a temporary statistic to keep this problem in view - the root cause of this lies either
  63. /// in a mishandling of the reply protocol, related to avatar appearance or may even originate in graphics
  64. /// driver bugs on clients (though this seems less likely).
  65. /// </summary>
  66. public long BlockedMissingTextureRequests { get { return blockedMissingTextureRequests; } }
  67. /// <summary>
  68. /// Record the number of times that an asset request has failed. Failures are effectively exceptions, such as
  69. /// request timeouts. If an asset service replies that a particular asset cannot be found, this is not counted
  70. /// as a failure
  71. /// </summary>
  72. public long AssetServiceRequestFailures { get { return assetServiceRequestFailures; } }
  73. /// <summary>
  74. /// Number of known failures to retrieve avatar inventory from the inventory service. This does not
  75. /// cover situations where the inventory service accepts the request but never returns any data, since
  76. /// we do not yet timeout this situation.
  77. /// </summary>
  78. public long InventoryServiceRetrievalFailures { get { return inventoryServiceRetrievalFailures; } }
  79. /// <summary>
  80. /// Retain a dictionary of all packet queues stats reporters
  81. /// </summary>
  82. private IDictionary<LLUUID, PacketQueueStatsCollector> packetQueueStatsCollectors
  83. = new Dictionary<LLUUID, PacketQueueStatsCollector>();
  84. public void AddAbnormalClientThreadTermination()
  85. {
  86. abnormalClientThreadTerminations++;
  87. }
  88. public void AddAsset(AssetBase asset)
  89. {
  90. assetsInCache++;
  91. assetCacheMemoryUsage += asset.Data.Length;
  92. }
  93. public void AddTexture(AssetBase image)
  94. {
  95. if (image.Data != null)
  96. {
  97. texturesInCache++;
  98. // This could have been a pull stat, though there was originally a nebulous idea to measure flow rates
  99. textureCacheMemoryUsage += image.Data.Length;
  100. }
  101. }
  102. /// <summary>
  103. /// Signal that the asset cache can be cleared.
  104. /// </summary>
  105. public void ClearAssetCacheStatistics()
  106. {
  107. assetsInCache = 0;
  108. assetCacheMemoryUsage = 0;
  109. texturesInCache = 0;
  110. textureCacheMemoryUsage = 0;
  111. }
  112. public void AddBlockedMissingTextureRequest()
  113. {
  114. blockedMissingTextureRequests++;
  115. }
  116. public void AddAssetServiceRequestFailure()
  117. {
  118. assetServiceRequestFailures++;
  119. }
  120. public void AddInventoryServiceRetrievalFailure()
  121. {
  122. inventoryServiceRetrievalFailures++;
  123. }
  124. /// <summary>
  125. /// Register as a packet queue stats provider
  126. /// </summary>
  127. /// <param name="uuid">An agent LLUUID</param>
  128. /// <param name="provider"></param>
  129. public void RegisterPacketQueueStatsProvider(LLUUID uuid, IPullStatsProvider provider)
  130. {
  131. lock (packetQueueStatsCollectors)
  132. {
  133. // FIXME: If the region service is providing more than one region, then the child and root agent
  134. // queues are wrongly replacing each other here.
  135. packetQueueStatsCollectors[uuid] = new PacketQueueStatsCollector(provider);
  136. }
  137. }
  138. /// <summary>
  139. /// Deregister a packet queue stats provider
  140. /// </summary>
  141. /// <param name="uuid">An agent LLUUID</param>
  142. public void DeregisterPacketQueueStatsProvider(LLUUID uuid)
  143. {
  144. lock (packetQueueStatsCollectors)
  145. {
  146. packetQueueStatsCollectors.Remove(uuid);
  147. }
  148. }
  149. /// <summary>
  150. /// Report back collected statistical information.
  151. /// </summary>
  152. /// <returns></returns>
  153. public override string Report()
  154. {
  155. StringBuilder sb = new StringBuilder(Environment.NewLine);
  156. sb.Append("ASSET STATISTICS");
  157. sb.Append(Environment.NewLine);
  158. sb.Append(
  159. string.Format(
  160. @"Asset cache contains {0,6} non-texture assets using {1,10} K
  161. Texture cache contains {2,6} texture assets using {3,10} K
  162. Blocked client requests for missing textures: {4}
  163. Asset service request failures: {5}"+ Environment.NewLine,
  164. AssetsInCache, Math.Round(AssetCacheMemoryUsage / 1024.0),
  165. TexturesInCache, Math.Round(TextureCacheMemoryUsage / 1024.0),
  166. BlockedMissingTextureRequests,
  167. AssetServiceRequestFailures));
  168. sb.Append(Environment.NewLine);
  169. sb.Append("CONNECTION STATISTICS");
  170. sb.Append(Environment.NewLine);
  171. sb.Append(
  172. string.Format(
  173. "Abnormal client thread terminations: {0}" + Environment.NewLine,
  174. abnormalClientThreadTerminations));
  175. sb.Append(Environment.NewLine);
  176. sb.Append("INVENTORY STATISTICS");
  177. sb.Append(Environment.NewLine);
  178. sb.Append(
  179. string.Format(
  180. "Initial inventory caching failures: {0}" + Environment.NewLine,
  181. InventoryServiceRetrievalFailures));
  182. sb.Append(Environment.NewLine);
  183. sb.Append("PACKET QUEUE STATISTICS");
  184. sb.Append(Environment.NewLine);
  185. sb.Append("Agent UUID ");
  186. sb.Append(
  187. string.Format(
  188. " {0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7}",
  189. "Send", "In", "Out", "Resend", "Land", "Wind", "Cloud", "Task", "Texture", "Asset"));
  190. sb.Append(Environment.NewLine);
  191. foreach (LLUUID key in packetQueueStatsCollectors.Keys)
  192. {
  193. sb.Append(string.Format("{0}: ", key));
  194. sb.Append(packetQueueStatsCollectors[key].Report());
  195. sb.Append(Environment.NewLine);
  196. }
  197. sb.Append(base.Report());
  198. return sb.ToString();
  199. }
  200. }
  201. /// <summary>
  202. /// Pull packet queue stats from packet queues and report
  203. /// </summary>
  204. public class PacketQueueStatsCollector : IStatsCollector
  205. {
  206. private IPullStatsProvider m_statsProvider;
  207. public PacketQueueStatsCollector(IPullStatsProvider provider)
  208. {
  209. m_statsProvider = provider;
  210. }
  211. /// <summary>
  212. /// Report back collected statistical information.
  213. /// </summary>
  214. /// <returns></returns>
  215. public string Report()
  216. {
  217. return m_statsProvider.GetStats();
  218. }
  219. }
  220. }