SimExtraStatsCollector.cs 21 KB

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