SimExtraStatsCollector.cs 17 KB

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