SimExtraStatsCollector.cs 15 KB

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