1
0

SimExtraStatsCollector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.Collections.Concurrent;
  30. using System.Diagnostics;
  31. using System.Linq;
  32. using System.Text;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework.Monitoring.Interfaces;
  36. namespace OpenSim.Framework.Monitoring
  37. {
  38. /// <summary>
  39. /// Collects sim statistics which aren't already being collected for the linden viewer's statistics pane
  40. /// </summary>
  41. public class SimExtraStatsCollector : BaseStatsCollector
  42. {
  43. /// <summary>
  44. /// Retain a dictionary of all packet queues stats reporters
  45. /// </summary>
  46. private readonly Dictionary<UUID, PacketQueueStatsCollector> packetQueueStatsCollectors
  47. = new Dictionary<UUID, PacketQueueStatsCollector>();
  48. /// <summary>
  49. /// Register as a packet queue stats provider
  50. /// </summary>
  51. /// <param name="uuid">An agent UUID</param>
  52. /// <param name="provider"></param>
  53. public void RegisterPacketQueueStatsProvider(UUID uuid, IPullStatsProvider provider)
  54. {
  55. lock (packetQueueStatsCollectors)
  56. {
  57. // FIXME: If the region service is providing more than one region, then the child and root agent
  58. // queues are wrongly replacing each other here.
  59. packetQueueStatsCollectors[uuid] = new PacketQueueStatsCollector(provider);
  60. }
  61. }
  62. /// <summary>
  63. /// Deregister a packet queue stats provider
  64. /// </summary>
  65. /// <param name="uuid">An agent UUID</param>
  66. public void DeregisterPacketQueueStatsProvider(UUID uuid)
  67. {
  68. lock (packetQueueStatsCollectors)
  69. {
  70. packetQueueStatsCollectors.Remove(uuid);
  71. }
  72. }
  73. private UUID firstReceivedRegion;
  74. private readonly ConcurrentDictionary<UUID, SimStats> ReceivedStats = new ConcurrentDictionary<UUID, SimStats>();
  75. private readonly ConcurrentDictionary<string, SimStats> ReceivedStatsByName = new ConcurrentDictionary<string, SimStats>();
  76. /// <summary>
  77. /// This is the method on which the classic sim stats reporter (which collects stats for
  78. /// client purposes) sends information to listeners.
  79. /// </summary>
  80. /// <param name="pack"></param>
  81. public void ReceiveClassicSimStatsPacket(SimStats stats)
  82. {
  83. UUID id = stats.RegionUUID;
  84. if (!id.IsZero())
  85. {
  86. if(ReceivedStats.Count == 0)
  87. firstReceivedRegion = id;
  88. ReceivedStats[id] = stats;
  89. ReceivedStatsByName[stats.RegionName.ToLower()] = stats;
  90. }
  91. }
  92. /// <summary>
  93. /// Report back collected statistical information.
  94. /// </summary>
  95. /// <returns></returns>
  96. public override string Report(IScene scene)
  97. {
  98. SimStats sdata = null;
  99. if (ReceivedStats.Count > 0)
  100. {
  101. if (scene == null)
  102. ReceivedStats.TryGetValue(firstReceivedRegion, out sdata);
  103. else
  104. ReceivedStats.TryGetValue(scene.RegionInfo.RegionID, out sdata);
  105. }
  106. StringBuilder sb = new StringBuilder();
  107. sb.AppendLine("CONNECTION STATISTICS");
  108. List<Stat> stats = StatsManager.GetStatsFromEachContainer("clientstack", "ClientLogoutsDueToNoReceives");
  109. sb.AppendFormat(
  110. "Client logouts due to no data receive timeout: {0}\n\n",
  111. stats != null ? stats.Sum(s => s.Value).ToString() : "unknown");
  112. sb.Append(Environment.NewLine);
  113. if(sdata != null)
  114. {
  115. float[] data = sdata.StatsValues;
  116. sb.AppendFormat("{0} FRAME STATISTICS", sdata.RegionName);
  117. sb.Append(Environment.NewLine);
  118. sb.Append("Dilatn SimFPS PhyFPS AgntUp RootAg ChldAg Prims AtvPrm AtvScr ScrEPS\n");
  119. sb.AppendFormat(
  120. "{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}\n",
  121. data[(int)StatsIndex.TimeDilation], data[(int)StatsIndex.SimFPS],
  122. data[(int)StatsIndex.PhysicsFPS],
  123. data[(int)StatsIndex.AgentUpdates], data[(int)StatsIndex.Agents],
  124. data[(int)StatsIndex.ChildAgents], data[(int)StatsIndex.TotalPrim],
  125. data[(int)StatsIndex.ActivePrim], data[(int)StatsIndex.ActiveScripts],
  126. data[(int)StatsIndex.ScriptEps]);
  127. sb.Append(Environment.NewLine);
  128. // There is no script frame time currently because we don't yet collect it
  129. sb.Append("PktsIn PktOut PendDl PendUl UnackB TotlFt NetFt PhysFt OthrFt AgntFt ImgsFt\n");
  130. sb.AppendFormat(
  131. "{0,6:0} {1,6:0} {2,6:0} {3,6:0} {4,6:0} {5,6:0.00} {6,6:0.00} {7,6:0.00} {8,6:0.00} {9,6:0.00} {10,6:0.00}\n",
  132. data[(int)StatsIndex.InPacketsPerSecond], data[(int)StatsIndex.OutPacketsPerSecond],
  133. data[(int)StatsIndex.PendingDownloads], data[(int)StatsIndex.PendingUploads],
  134. data[(int)StatsIndex.UnAckedBytes], data[(int)StatsIndex.FrameMS],
  135. data[(int)StatsIndex.NetMS], data[(int)StatsIndex.PhysicsMS],
  136. data[(int)StatsIndex.OtherMS] , data[(int)StatsIndex.AgentMS],
  137. data[(int)StatsIndex.ImageMS]);
  138. }
  139. sb.Append(base.Report());
  140. return sb.ToString();
  141. }
  142. /// <summary>
  143. /// Report back collected statistical information as json serialization.
  144. /// </summary>
  145. /// <returns></returns>
  146. public override string XReport(string uptime, string version, string scene)
  147. {
  148. return OSDParser.SerializeJsonString(OReport(uptime, version, scene));
  149. }
  150. /// <summary>
  151. /// Report back collected statistical information as an OSDMap
  152. /// </summary>
  153. /// <returns></returns>
  154. public override OSDMap OReport(string uptime, string version, string scene)
  155. {
  156. // Get the amount of physical memory, allocated with the instance of this program, in kilobytes;
  157. // the working set is the set of memory pages currently visible to this program in physical RAM
  158. // memory and includes both shared (e.g. system libraries) and private data
  159. int numberThreads = 0;
  160. int numberThreadsRunning = 0;
  161. double memUsage = 0;
  162. using(Process p = Process.GetCurrentProcess())
  163. {
  164. memUsage = p.WorkingSet64 / 1024.0;
  165. numberThreads = p.Threads.Count;
  166. // Get the number of threads from the system that are currently
  167. // running
  168. foreach (ProcessThread currentThread in p.Threads)
  169. {
  170. if (currentThread != null && currentThread.ThreadState == ThreadState.Running)
  171. numberThreadsRunning++;
  172. }
  173. }
  174. SimStats sdata = null;
  175. if (ReceivedStats.Count > 0)
  176. {
  177. if (scene == null || string.IsNullOrEmpty(scene))
  178. ReceivedStats.TryGetValue(firstReceivedRegion, out sdata);
  179. else
  180. {
  181. if(UUID.TryParse(scene, out UUID id))
  182. ReceivedStats.TryGetValue(id, out sdata);
  183. else
  184. ReceivedStatsByName.TryGetValue(scene.ToLower(), out sdata);
  185. }
  186. }
  187. OSDMap args = new OSDMap(33);
  188. if(sdata != null && sdata.StatsValues != null)
  189. {
  190. float[] data = sdata.StatsValues;
  191. args["Dilatn"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.TimeDilation]));
  192. args["SimFPS"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.SimFPS]));
  193. args["PhyFPS"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.PhysicsFPS]));
  194. args["AgntUp"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.AgentUpdates]));
  195. args["RootAg"] = OSD.FromString (String.Format ("{0}", (int)data[(int)StatsIndex.Agents]));
  196. args["ChldAg"] = OSD.FromString(String.Format("{0}", (int)data[(int)StatsIndex.ChildAgents]));
  197. args["NPCAg"] = OSD.FromString(String.Format("{0}", (int)data[(int)StatsIndex.NPCs]));
  198. args["Prims"] = OSD.FromString (String.Format ("{0}", (int)data[(int)StatsIndex.TotalPrim]));
  199. args["AtvPrm"] = OSD.FromString (String.Format ("{0}", (int)data[(int)StatsIndex.ActivePrim]));
  200. args["AtvScr"] = OSD.FromString (String.Format ("{0}", (int)data[(int)StatsIndex.ActiveScripts]));
  201. args["ScrLPS"] = OSD.FromString(String.Format("{0:0.##}", data[(int)StatsIndex.LSLScriptLinesPerSecond]));
  202. args["ScrEPS"] = OSD.FromString(String.Format("{0:0.##}", data[(int)StatsIndex.ScriptEps]));
  203. args["PktsIn"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.InPacketsPerSecond]));
  204. args["PktOut"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.OutPacketsPerSecond]));
  205. args["PendDl"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.PendingDownloads]));
  206. args["PendUl"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.PendingUploads]));
  207. args["UnackB"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.UnAckedBytes]));
  208. args["TotlFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.FrameMS]));
  209. args["NetFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.NetMS]));
  210. args["PhysFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.PhysicsMS]));
  211. args["OthrFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.OtherMS]));
  212. args["AgntFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.AgentMS]));
  213. args["ImgsFt"] = OSD.FromString (String.Format ("{0:0.##}", data[(int)StatsIndex.ImageMS]));
  214. args["FrameDilatn"] = OSD.FromString(String.Format("{0:0.#}", data[(int)StatsIndex.FrameDilation2]));
  215. args["Logging in Users"] = OSD.FromString(String.Format("{0:0.#}", data[(int)StatsIndex.UsersLoggingIn]));
  216. args["GeoPrims"] = OSD.FromString(String.Format("{0:0.#}", data[(int)StatsIndex.TotalGeoPrim]));
  217. args["Mesh Objects"] = OSD.FromString(String.Format("{0:0.##}", data[(int)StatsIndex.TotalMesh]));
  218. args["Script Engine Thread Count"] = OSD.FromString(String.Format("{0:0.#}", data[(int)StatsIndex.ScriptEngineThreadCount]));
  219. args["RegionName"] = sdata.RegionName;
  220. }
  221. else
  222. args["Error"] = "No Region data";
  223. args["Util Thread Count"] = OSD.FromString(String.Format("{0:0.##}", Util.GetSmartThreadPoolInfo().InUseThreads));
  224. args["System Thread Count"] = OSD.FromString(String.Format("{0:0.##}", numberThreads));
  225. args["System Thread Active"] = OSD.FromString(String.Format("{0:0.##}", numberThreadsRunning));
  226. args["ProcMem"] = OSD.FromString(String.Format("{0:0.##}", memUsage));
  227. args["Memory"] = OSD.FromString(base.XReport(uptime, version));
  228. args["Uptime"] = OSD.FromString(uptime);
  229. args["Version"] = OSD.FromString(version);
  230. return args;
  231. }
  232. }
  233. /// <summary>
  234. /// Pull packet queue stats from packet queues and report
  235. /// </summary>
  236. public class PacketQueueStatsCollector : IStatsCollector
  237. {
  238. private IPullStatsProvider m_statsProvider;
  239. public PacketQueueStatsCollector(IPullStatsProvider provider)
  240. {
  241. m_statsProvider = provider;
  242. }
  243. /// <summary>
  244. /// Report back collected statistical information.
  245. /// </summary>
  246. /// <returns></returns>
  247. public string Report()
  248. {
  249. return m_statsProvider.GetStats();
  250. }
  251. public string XReport(string uptime, string version)
  252. {
  253. return "";
  254. }
  255. public OSDMap OReport(string uptime, string version)
  256. {
  257. OSDMap ret = new OSDMap();
  258. return ret;
  259. }
  260. }
  261. }