SimExtraStatsCollector.cs 22 KB

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