SimExtraStatsCollector.cs 22 KB

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