SimExtraStatsCollector.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 libsecondlife.Packets;
  32. using OpenSim.Framework.Statistics.Interfaces;
  33. namespace OpenSim.Framework.Statistics
  34. {
  35. /// <summary>
  36. /// Collects sim statistics which aren't already being collected for the linden viewer's statistics pane
  37. /// </summary>
  38. public class SimExtraStatsCollector : BaseStatsCollector
  39. {
  40. private long abnormalClientThreadTerminations;
  41. private long assetsInCache;
  42. private long texturesInCache;
  43. private long assetCacheMemoryUsage;
  44. private long textureCacheMemoryUsage;
  45. private long blockedMissingTextureRequests;
  46. private long assetServiceRequestFailures;
  47. private long inventoryServiceRetrievalFailures;
  48. private float timeDilation;
  49. private float simFps;
  50. private float physicsFps;
  51. private float agentUpdates;
  52. private float rootAgents;
  53. private float childAgents;
  54. private float totalPrims;
  55. private float activePrims;
  56. private float totalFrameTime;
  57. private float netFrameTime;
  58. private float physicsFrameTime;
  59. private float otherFrameTime;
  60. private float imageFrameTime;
  61. private float inPacketsPerSecond;
  62. private float outPacketsPerSecond;
  63. private float unackedBytes;
  64. private float agentFrameTime;
  65. private float pendingDownloads;
  66. private float pendingUploads;
  67. private float activeScripts;
  68. private float scriptLinesPerSecond;
  69. /// <summary>
  70. /// Number of times that a client thread terminated because of an exception
  71. /// </summary>
  72. public long AbnormalClientThreadTerminations { get { return abnormalClientThreadTerminations; } }
  73. /// <summary>
  74. /// These statistics are being collected by push rather than pull. Pull would be simpler, but I had the
  75. /// notion of providing some flow statistics (which pull wouldn't give us). Though admittedly these
  76. /// haven't yet been implemented... :)
  77. /// </summary>
  78. public long AssetsInCache { get { return assetsInCache; } }
  79. public long TexturesInCache { get { return texturesInCache; } }
  80. public long AssetCacheMemoryUsage { get { return assetCacheMemoryUsage; } }
  81. public long TextureCacheMemoryUsage { get { return textureCacheMemoryUsage; } }
  82. /// <summary>
  83. /// Number of persistent requests for missing textures we have started blocking from clients. To some extent
  84. /// this is just a temporary statistic to keep this problem in view - the root cause of this lies either
  85. /// in a mishandling of the reply protocol, related to avatar appearance or may even originate in graphics
  86. /// driver bugs on clients (though this seems less likely).
  87. /// </summary>
  88. public long BlockedMissingTextureRequests { get { return blockedMissingTextureRequests; } }
  89. /// <summary>
  90. /// Record the number of times that an asset request has failed. Failures are effectively exceptions, such as
  91. /// request timeouts. If an asset service replies that a particular asset cannot be found, this is not counted
  92. /// as a failure
  93. /// </summary>
  94. public long AssetServiceRequestFailures { get { return assetServiceRequestFailures; } }
  95. /// <summary>
  96. /// Number of known failures to retrieve avatar inventory from the inventory service. This does not
  97. /// cover situations where the inventory service accepts the request but never returns any data, since
  98. /// we do not yet timeout this situation.
  99. /// </summary>
  100. public long InventoryServiceRetrievalFailures { get { return inventoryServiceRetrievalFailures; } }
  101. /// <summary>
  102. /// Retrieve the total frame time (in ms) of the last frame
  103. /// </summary>
  104. //public float TotalFrameTime { get { return totalFrameTime; } }
  105. /// <summary>
  106. /// Retrieve the physics update component (in ms) of the last frame
  107. /// </summary>
  108. //public float PhysicsFrameTime { get { return physicsFrameTime; } }
  109. /// <summary>
  110. /// Retain a dictionary of all packet queues stats reporters
  111. /// </summary>
  112. private IDictionary<LLUUID, PacketQueueStatsCollector> packetQueueStatsCollectors
  113. = new Dictionary<LLUUID, PacketQueueStatsCollector>();
  114. public void AddAbnormalClientThreadTermination()
  115. {
  116. abnormalClientThreadTerminations++;
  117. }
  118. public void AddAsset(AssetBase asset)
  119. {
  120. assetsInCache++;
  121. assetCacheMemoryUsage += asset.Data.Length;
  122. }
  123. public void AddTexture(AssetBase image)
  124. {
  125. if (image.Data != null)
  126. {
  127. texturesInCache++;
  128. // This could have been a pull stat, though there was originally a nebulous idea to measure flow rates
  129. textureCacheMemoryUsage += image.Data.Length;
  130. }
  131. }
  132. /// <summary>
  133. /// Signal that the asset cache can be cleared.
  134. /// </summary>
  135. public void ClearAssetCacheStatistics()
  136. {
  137. assetsInCache = 0;
  138. assetCacheMemoryUsage = 0;
  139. texturesInCache = 0;
  140. textureCacheMemoryUsage = 0;
  141. }
  142. public void AddBlockedMissingTextureRequest()
  143. {
  144. blockedMissingTextureRequests++;
  145. }
  146. public void AddAssetServiceRequestFailure()
  147. {
  148. assetServiceRequestFailures++;
  149. }
  150. public void AddInventoryServiceRetrievalFailure()
  151. {
  152. inventoryServiceRetrievalFailures++;
  153. }
  154. /// <summary>
  155. /// Register as a packet queue stats provider
  156. /// </summary>
  157. /// <param name="uuid">An agent LLUUID</param>
  158. /// <param name="provider"></param>
  159. public void RegisterPacketQueueStatsProvider(LLUUID uuid, IPullStatsProvider provider)
  160. {
  161. lock (packetQueueStatsCollectors)
  162. {
  163. // FIXME: If the region service is providing more than one region, then the child and root agent
  164. // queues are wrongly replacing each other here.
  165. packetQueueStatsCollectors[uuid] = new PacketQueueStatsCollector(provider);
  166. }
  167. }
  168. /// <summary>
  169. /// Deregister a packet queue stats provider
  170. /// </summary>
  171. /// <param name="uuid">An agent LLUUID</param>
  172. public void DeregisterPacketQueueStatsProvider(LLUUID uuid)
  173. {
  174. lock (packetQueueStatsCollectors)
  175. {
  176. packetQueueStatsCollectors.Remove(uuid);
  177. }
  178. }
  179. /// <summary>
  180. /// This is the method on which the classic sim stats reporter (which collects stats for
  181. /// client purposes) sends information to listeners.
  182. /// </summary>
  183. /// <param name="pack"></param>
  184. public void ReceiveClassicSimStatsPacket(SimStatsPacket statsPacket)
  185. {
  186. // FIXME: Really shouldn't rely on the probably arbitrary order in which
  187. // stats are packed into the packet
  188. timeDilation = statsPacket.Stat[0].StatValue;
  189. simFps = statsPacket.Stat[1].StatValue;
  190. physicsFps = statsPacket.Stat[2].StatValue;
  191. agentUpdates = statsPacket.Stat[3].StatValue;
  192. rootAgents = statsPacket.Stat[4].StatValue;
  193. childAgents = statsPacket.Stat[5].StatValue;
  194. totalPrims = statsPacket.Stat[6].StatValue;
  195. activePrims = statsPacket.Stat[7].StatValue;
  196. totalFrameTime = statsPacket.Stat[8].StatValue;
  197. netFrameTime = statsPacket.Stat[9].StatValue;
  198. physicsFrameTime = statsPacket.Stat[10].StatValue;
  199. otherFrameTime = statsPacket.Stat[11].StatValue;
  200. imageFrameTime = statsPacket.Stat[12].StatValue;
  201. inPacketsPerSecond = statsPacket.Stat[13].StatValue;
  202. outPacketsPerSecond = statsPacket.Stat[14].StatValue;
  203. unackedBytes = statsPacket.Stat[15].StatValue;
  204. agentFrameTime = statsPacket.Stat[16].StatValue;
  205. pendingDownloads = statsPacket.Stat[17].StatValue;
  206. pendingUploads = statsPacket.Stat[18].StatValue;
  207. activeScripts = statsPacket.Stat[19].StatValue;
  208. scriptLinesPerSecond = statsPacket.Stat[20].StatValue;
  209. }
  210. /// <summary>
  211. /// Report back collected statistical information.
  212. /// </summary>
  213. /// <returns></returns>
  214. public override string Report()
  215. {
  216. StringBuilder sb = new StringBuilder(Environment.NewLine);
  217. sb.Append("ASSET STATISTICS");
  218. sb.Append(Environment.NewLine);
  219. sb.Append(
  220. string.Format(
  221. @"Asset cache contains {0,6} non-texture assets using {1,10} K
  222. Texture cache contains {2,6} texture assets using {3,10} K
  223. Blocked client requests for missing textures: {4}
  224. Asset service request failures: {5}"+ Environment.NewLine,
  225. AssetsInCache, Math.Round(AssetCacheMemoryUsage / 1024.0),
  226. TexturesInCache, Math.Round(TextureCacheMemoryUsage / 1024.0),
  227. BlockedMissingTextureRequests,
  228. AssetServiceRequestFailures));
  229. sb.Append(Environment.NewLine);
  230. sb.Append("CONNECTION STATISTICS");
  231. sb.Append(Environment.NewLine);
  232. sb.Append(
  233. string.Format(
  234. "Abnormal client thread terminations: {0}" + Environment.NewLine,
  235. abnormalClientThreadTerminations));
  236. sb.Append(Environment.NewLine);
  237. sb.Append("INVENTORY STATISTICS");
  238. sb.Append(Environment.NewLine);
  239. sb.Append(
  240. string.Format(
  241. "Initial inventory caching failures: {0}" + Environment.NewLine,
  242. InventoryServiceRetrievalFailures));
  243. sb.Append(Environment.NewLine);
  244. sb.Append("FRAME STATISTICS");
  245. sb.Append(Environment.NewLine);
  246. sb.Append("Dilatn SimFPS PhyFPS AgntUp RootAg ChldAg Prims AtvPrm AtvScr ScrLPS");
  247. sb.Append(Environment.NewLine);
  248. sb.Append(
  249. string.Format(
  250. "{0,6:0.00} {1,6:0} {2,6:0.0} {3,6:0.0} {4,6:0} {5,6:0} {6,6:0} {7,6:0} {8,6:0} {9,6:0}",
  251. timeDilation, simFps, physicsFps, agentUpdates, rootAgents,
  252. childAgents, totalPrims, activePrims, activeScripts, scriptLinesPerSecond));
  253. sb.Append(Environment.NewLine);
  254. sb.Append(Environment.NewLine);
  255. // There is no script frame time currently because we don't yet collect it
  256. sb.Append("PktsIn PktOut PendDl PendUl UnackB TotlFt NetFt PhysFt OthrFt AgntFt ImgsFt");
  257. sb.Append(Environment.NewLine);
  258. sb.Append(
  259. string.Format(
  260. "{0,6:0} {1,6:0} {2,6:0} {3,6:0} {4,6:0} {5,6:0.0} {6,6:0.0} {7,6:0.0} {8,6:0.0} {9,6:0.0} {10,6:0.0}",
  261. inPacketsPerSecond, outPacketsPerSecond, pendingDownloads, pendingUploads, unackedBytes, totalFrameTime,
  262. netFrameTime, physicsFrameTime, otherFrameTime, agentFrameTime, imageFrameTime));
  263. sb.Append(Environment.NewLine);
  264. /*
  265. sb.Append(Environment.NewLine);
  266. sb.Append("PACKET QUEUE STATISTICS");
  267. sb.Append(Environment.NewLine);
  268. sb.Append("Agent UUID ");
  269. sb.Append(
  270. string.Format(
  271. " {0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7}",
  272. "Send", "In", "Out", "Resend", "Land", "Wind", "Cloud", "Task", "Texture", "Asset"));
  273. sb.Append(Environment.NewLine);
  274. foreach (LLUUID key in packetQueueStatsCollectors.Keys)
  275. {
  276. sb.Append(string.Format("{0}: ", key));
  277. sb.Append(packetQueueStatsCollectors[key].Report());
  278. sb.Append(Environment.NewLine);
  279. }
  280. */
  281. sb.Append(base.Report());
  282. return sb.ToString();
  283. }
  284. }
  285. /// <summary>
  286. /// Pull packet queue stats from packet queues and report
  287. /// </summary>
  288. public class PacketQueueStatsCollector : IStatsCollector
  289. {
  290. private IPullStatsProvider m_statsProvider;
  291. public PacketQueueStatsCollector(IPullStatsProvider provider)
  292. {
  293. m_statsProvider = provider;
  294. }
  295. /// <summary>
  296. /// Report back collected statistical information.
  297. /// </summary>
  298. /// <returns></returns>
  299. public string Report()
  300. {
  301. return m_statsProvider.GetStats();
  302. }
  303. }
  304. }