SimStatsReporter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. */
  28. using System;
  29. using System.Timers;
  30. using libsecondlife.Packets;
  31. using OpenSim.Framework;
  32. namespace OpenSim.Region.Environment.Scenes
  33. {
  34. public class SimStatsReporter
  35. {
  36. public delegate void SendStatResult(SimStatsPacket pack);
  37. public event SendStatResult OnSendStatsResult;
  38. private enum Stats : uint
  39. {
  40. TimeDilation = 0,
  41. SimFPS = 1,
  42. PhysicsFPS = 2,
  43. AgentUpdates = 3,
  44. FrameMS = 4,
  45. NetMS = 5,
  46. OtherMS = 6,
  47. PhysicsMS = 7,
  48. AgentMS = 8,
  49. ImageMS = 9,
  50. ScriptMS = 10,
  51. TotalPrim = 11,
  52. ActivePrim = 12,
  53. Agents = 13,
  54. ChildAgents = 14,
  55. ActiveScripts = 15,
  56. ScriptLinesPerSecond = 16,
  57. InPacketsPerSecond = 17,
  58. OutPacketsPerSecond = 18,
  59. PendingDownloads = 19,
  60. PendingUploads = 20,
  61. UnAckedBytes = 24,
  62. // Havok4 related... May or may not be in upcoming LLclients
  63. // (kelly added them sometime late in January 2008)
  64. NumRCCSLODReduced = 25,
  65. NumRCCSFixed = 26
  66. }
  67. // Sending a stats update every 3 seconds
  68. private int statsUpdatesEveryMS = 3000;
  69. private float statsUpdateFactor = 0;
  70. private float m_timeDilation = 0;
  71. private int m_fps = 0;
  72. private float m_pfps = 0;
  73. private int m_agentUpdates = 0;
  74. private int m_frameMS = 0;
  75. private int m_netMS = 0;
  76. private int m_agentMS = 0;
  77. private int m_physicsMS = 0;
  78. private int m_imageMS = 0;
  79. private int m_otherMS = 0;
  80. private int m_scriptMS = 0;
  81. private int m_rootAgents = 0;
  82. private int m_childAgents = 0;
  83. private int m_numPrim = 0;
  84. private int m_inPacketsPerSecond = 0;
  85. private int m_outPacketsPerSecond = 0;
  86. private int m_activePrim = 0;
  87. private int m_unAckedBytes = 0;
  88. private int m_pendingDownloads = 0;
  89. private int m_pendingUploads = 0;
  90. private int m_activeScripts = 0;
  91. private int m_scriptLinesPerSecond = 0;
  92. SimStatsPacket.StatBlock[] sb = new SimStatsPacket.StatBlock[21];
  93. SimStatsPacket.RegionBlock rb = new SimStatsPacket.RegionBlock();
  94. SimStatsPacket statpack = (SimStatsPacket)PacketPool.Instance.GetPacket(PacketType.SimStats);
  95. private RegionInfo ReportingRegion;
  96. private Timer m_report = new Timer();
  97. public SimStatsReporter(RegionInfo regionData)
  98. {
  99. statsUpdateFactor = (float)(statsUpdatesEveryMS / 1000);
  100. ReportingRegion = regionData;
  101. for (int i = 0; i<21;i++)
  102. {
  103. sb[i] = new SimStatsPacket.StatBlock();
  104. }
  105. m_report.AutoReset = true;
  106. m_report.Interval = statsUpdatesEveryMS;
  107. m_report.Elapsed += new ElapsedEventHandler(statsHeartBeat);
  108. m_report.Enabled = true;
  109. }
  110. public void SetUpdateMS(int ms)
  111. {
  112. statsUpdatesEveryMS = ms;
  113. statsUpdateFactor = (float)(statsUpdatesEveryMS / 1000);
  114. m_report.Interval = statsUpdatesEveryMS;
  115. }
  116. private void statsHeartBeat(object sender, EventArgs e)
  117. {
  118. m_report.Enabled = false;
  119. // Packet is already initialized and ready for data insert
  120. statpack.Region = rb;
  121. statpack.Region.RegionX = ReportingRegion.RegionLocX;
  122. statpack.Region.RegionY = ReportingRegion.RegionLocY;
  123. try
  124. {
  125. statpack.Region.RegionFlags = (uint) ReportingRegion.EstateSettings.regionFlags;
  126. }
  127. catch (Exception)
  128. {
  129. statpack.Region.RegionFlags = (uint) 0;
  130. }
  131. statpack.Region.ObjectCapacity = (uint) 15000;
  132. #region various statistic googly moogly
  133. // Our FPS is actually 10fps, so multiplying by 5 to get the amount that people expect there
  134. // 0-50 is pretty close to 0-45
  135. float simfps = (int) ((m_fps * 5));
  136. //if (simfps > 45)
  137. //simfps = simfps - (simfps - 45);
  138. //if (simfps < 0)
  139. //simfps = 0;
  140. //
  141. float physfps = ((m_pfps / 1000));
  142. //if (physfps > 600)
  143. //physfps = physfps - (physfps - 600);
  144. if (physfps < 0)
  145. physfps = 0;
  146. #endregion
  147. //Our time dilation is 0.91 when we're running a full speed,
  148. // therefore to make sure we get an appropriate range,
  149. // we have to factor in our error. (0.10f * statsUpdateFactor)
  150. // multiplies the fix for the error times the amount of times it'll occur a second
  151. // / 10 divides the value by the number of times the sim heartbeat runs (10fps)
  152. // Then we divide the whole amount by the amount of seconds pass in between stats updates.
  153. sb[0].StatID = (uint) Stats.TimeDilation;
  154. sb[0].StatValue = m_timeDilation ; //((((m_timeDilation + (0.10f * statsUpdateFactor)) /10) / statsUpdateFactor));
  155. sb[1].StatID = (uint) Stats.SimFPS;
  156. sb[1].StatValue = simfps/statsUpdateFactor;
  157. sb[2].StatID = (uint) Stats.PhysicsFPS;
  158. sb[2].StatValue = physfps / statsUpdateFactor;
  159. sb[3].StatID = (uint) Stats.AgentUpdates;
  160. sb[3].StatValue = (m_agentUpdates / statsUpdateFactor);
  161. sb[4].StatID = (uint) Stats.Agents;
  162. sb[4].StatValue = m_rootAgents;
  163. sb[5].StatID = (uint) Stats.ChildAgents;
  164. sb[5].StatValue = m_childAgents;
  165. sb[6].StatID = (uint) Stats.TotalPrim;
  166. sb[6].StatValue = m_numPrim;
  167. sb[7].StatID = (uint) Stats.ActivePrim;
  168. sb[7].StatValue = m_activePrim;
  169. sb[8].StatID = (uint)Stats.FrameMS;
  170. sb[8].StatValue = m_frameMS / statsUpdateFactor;
  171. sb[9].StatID = (uint)Stats.NetMS;
  172. sb[9].StatValue = m_netMS / statsUpdateFactor;
  173. sb[10].StatID = (uint)Stats.PhysicsMS;
  174. sb[10].StatValue = m_physicsMS / statsUpdateFactor;
  175. sb[11].StatID = (uint)Stats.ImageMS ;
  176. sb[11].StatValue = m_imageMS / statsUpdateFactor;
  177. sb[12].StatID = (uint)Stats.OtherMS;
  178. sb[12].StatValue = m_otherMS / statsUpdateFactor;
  179. sb[13].StatID = (uint)Stats.InPacketsPerSecond;
  180. sb[13].StatValue = (m_inPacketsPerSecond);
  181. sb[14].StatID = (uint)Stats.OutPacketsPerSecond;
  182. sb[14].StatValue = (m_outPacketsPerSecond / statsUpdateFactor);
  183. sb[15].StatID = (uint)Stats.UnAckedBytes;
  184. sb[15].StatValue = m_unAckedBytes;
  185. sb[16].StatID = (uint)Stats.AgentMS;
  186. sb[16].StatValue = m_agentMS / statsUpdateFactor;
  187. sb[17].StatID = (uint)Stats.PendingDownloads;
  188. sb[17].StatValue = m_pendingDownloads;
  189. sb[18].StatID = (uint)Stats.PendingUploads;
  190. sb[18].StatValue = m_pendingUploads;
  191. sb[19].StatID = (uint)Stats.ActiveScripts;
  192. sb[19].StatValue = m_activeScripts;
  193. sb[20].StatID = (uint)Stats.ScriptLinesPerSecond;
  194. sb[20].StatValue = m_scriptLinesPerSecond / statsUpdateFactor;
  195. statpack.Stat = sb;
  196. if (OnSendStatsResult != null)
  197. {
  198. OnSendStatsResult(statpack);
  199. }
  200. resetvalues();
  201. m_report.Enabled = true;
  202. }
  203. private void resetvalues()
  204. {
  205. m_timeDilation = 0;
  206. m_fps = 0;
  207. m_pfps = 0;
  208. m_agentUpdates = 0;
  209. m_inPacketsPerSecond = 0;
  210. m_outPacketsPerSecond = 0;
  211. m_unAckedBytes = 0;
  212. m_scriptLinesPerSecond = 0;
  213. m_frameMS = 0;
  214. m_agentMS = 0;
  215. m_netMS = 0;
  216. m_physicsMS = 0;
  217. m_imageMS = 0;
  218. m_otherMS = 0;
  219. m_scriptMS = 0;
  220. }
  221. # region methods called from Scene
  222. // The majority of these functions are additive
  223. // so that you can easily change the amount of
  224. // seconds in between sim stats updates
  225. public void AddTimeDilation(float td)
  226. {
  227. //float tdsetting = td;
  228. //if (tdsetting > 1.0f)
  229. //tdsetting = (tdsetting - (tdsetting - 0.91f));
  230. //if (tdsetting < 0)
  231. //tdsetting = 0.0f;
  232. m_timeDilation = td;
  233. }
  234. public void SetRootAgents(int rootAgents)
  235. {
  236. m_rootAgents = rootAgents;
  237. }
  238. public void SetChildAgents(int childAgents)
  239. {
  240. m_childAgents = childAgents;
  241. }
  242. public void SetObjects(int objects)
  243. {
  244. m_numPrim = objects;
  245. }
  246. public void SetActiveObjects(int objects)
  247. {
  248. m_activePrim = objects;
  249. }
  250. public void AddFPS(int frames)
  251. {
  252. m_fps += frames;
  253. }
  254. public void AddPhysicsFPS(float frames)
  255. {
  256. m_pfps += frames;
  257. }
  258. public void AddAgentUpdates(int numUpdates)
  259. {
  260. m_agentUpdates += numUpdates;
  261. }
  262. public void AddInPackets(int numPackets)
  263. {
  264. m_inPacketsPerSecond += numPackets;
  265. }
  266. public void AddOutPackets(int numPackets)
  267. {
  268. m_outPacketsPerSecond += numPackets;
  269. }
  270. public void AddunAckedBytes(int numBytes)
  271. {
  272. m_unAckedBytes += numBytes;
  273. }
  274. public void addFrameMS(int ms)
  275. {
  276. m_frameMS += ms;
  277. }
  278. public void addNetMS(int ms)
  279. {
  280. m_netMS += ms;
  281. }
  282. public void addAgentMS(int ms)
  283. {
  284. m_agentMS += ms;
  285. }
  286. public void addPhysicsMS(int ms)
  287. {
  288. m_physicsMS += ms;
  289. }
  290. public void addImageMS(int ms)
  291. {
  292. m_imageMS += ms;
  293. }
  294. public void addOtherMS(int ms)
  295. {
  296. m_otherMS += ms;
  297. }
  298. public void addPendingDownload(int count)
  299. {
  300. m_pendingDownloads += count;
  301. }
  302. public void addScriptLines(int count)
  303. {
  304. m_scriptLinesPerSecond += count;
  305. }
  306. public void SetActiveScripts(int count)
  307. {
  308. m_activeScripts = count;
  309. }
  310. #endregion
  311. }
  312. }