SimStatsReporter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. using System;
  28. using System.Timers;
  29. using OpenMetaverse.Packets;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Statistics;
  32. using OpenSim.Region.Environment.Interfaces;
  33. namespace OpenSim.Region.Environment.Scenes
  34. {
  35. public class SimStatsReporter
  36. {
  37. public delegate void SendStatResult(SimStats stats);
  38. public event SendStatResult OnSendStatsResult;
  39. private SendStatResult handlerSendStatResult = null;
  40. private enum Stats : uint
  41. {
  42. TimeDilation = 0,
  43. SimFPS = 1,
  44. PhysicsFPS = 2,
  45. AgentUpdates = 3,
  46. FrameMS = 4,
  47. NetMS = 5,
  48. OtherMS = 6,
  49. PhysicsMS = 7,
  50. AgentMS = 8,
  51. ImageMS = 9,
  52. ScriptMS = 10,
  53. TotalPrim = 11,
  54. ActivePrim = 12,
  55. Agents = 13,
  56. ChildAgents = 14,
  57. ActiveScripts = 15,
  58. ScriptLinesPerSecond = 16,
  59. InPacketsPerSecond = 17,
  60. OutPacketsPerSecond = 18,
  61. PendingDownloads = 19,
  62. PendingUploads = 20,
  63. UnAckedBytes = 24,
  64. }
  65. // Sending a stats update every 3 seconds
  66. private int statsUpdatesEveryMS = 3000;
  67. private float statsUpdateFactor = 0;
  68. private float m_timeDilation = 0;
  69. private int m_fps = 0;
  70. // saved last reported value so there is something available for llGetRegionFPS
  71. private float lastReportedSimFPS = 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. //Ckrinke: (3-21-08) Comment out to remove a compiler warning. Bring back into play when needed.
  81. //Ckrinke private int m_scriptMS = 0;
  82. private int m_rootAgents = 0;
  83. private int m_childAgents = 0;
  84. private int m_numPrim = 0;
  85. private int m_inPacketsPerSecond = 0;
  86. private int m_outPacketsPerSecond = 0;
  87. private int m_activePrim = 0;
  88. private int m_unAckedBytes = 0;
  89. private int m_pendingDownloads = 0;
  90. private int m_pendingUploads = 0;
  91. private int m_activeScripts = 0;
  92. private int m_scriptLinesPerSecond = 0;
  93. private int objectCapacity = 45000;
  94. private Scene m_scene;
  95. private RegionInfo ReportingRegion;
  96. private Timer m_report = new Timer();
  97. public SimStatsReporter(Scene scene)
  98. {
  99. statsUpdateFactor = (float)(statsUpdatesEveryMS / 1000);
  100. m_scene = scene;
  101. ReportingRegion = scene.RegionInfo;
  102. m_report.AutoReset = true;
  103. m_report.Interval = statsUpdatesEveryMS;
  104. m_report.Elapsed += new ElapsedEventHandler(statsHeartBeat);
  105. m_report.Enabled = true;
  106. if (StatsManager.SimExtraStats != null)
  107. OnSendStatsResult += StatsManager.SimExtraStats.ReceiveClassicSimStatsPacket;
  108. }
  109. public void SetUpdateMS(int ms)
  110. {
  111. statsUpdatesEveryMS = ms;
  112. statsUpdateFactor = (float)(statsUpdatesEveryMS / 1000);
  113. m_report.Interval = statsUpdatesEveryMS;
  114. }
  115. private void statsHeartBeat(object sender, EventArgs e)
  116. {
  117. SimStatsPacket.StatBlock[] sb = new SimStatsPacket.StatBlock[21];
  118. SimStatsPacket.RegionBlock rb = new SimStatsPacket.RegionBlock();
  119. // Know what's not thread safe in Mono... modifying timers.
  120. // System.Console.WriteLine("Firing Stats Heart Beat");
  121. lock (m_report)
  122. {
  123. uint regionFlags = 0;
  124. try
  125. {
  126. IEstateModule estateModule = m_scene.RequestModuleInterface<IEstateModule>();
  127. regionFlags = estateModule != null ? estateModule.GetRegionFlags() : (uint) 0;
  128. }
  129. catch (Exception)
  130. {
  131. // leave region flags at 0
  132. }
  133. #region various statistic googly moogly
  134. // Our FPS is actually 10fps, so multiplying by 5 to get the amount that people expect there
  135. // 0-50 is pretty close to 0-45
  136. float simfps = (int) ((m_fps * 5));
  137. // save the reported value so there is something available for llGetRegionFPS
  138. lastReportedSimFPS = (float)simfps / statsUpdateFactor;
  139. //if (simfps > 45)
  140. //simfps = simfps - (simfps - 45);
  141. //if (simfps < 0)
  142. //simfps = 0;
  143. //
  144. float physfps = ((m_pfps / 1000));
  145. //if (physfps > 600)
  146. //physfps = physfps - (physfps - 600);
  147. if (physfps < 0)
  148. physfps = 0;
  149. #endregion
  150. //Our time dilation is 0.91 when we're running a full speed,
  151. // therefore to make sure we get an appropriate range,
  152. // we have to factor in our error. (0.10f * statsUpdateFactor)
  153. // multiplies the fix for the error times the amount of times it'll occur a second
  154. // / 10 divides the value by the number of times the sim heartbeat runs (10fps)
  155. // Then we divide the whole amount by the amount of seconds pass in between stats updates.
  156. for (int i = 0; i<21;i++)
  157. {
  158. sb[i] = new SimStatsPacket.StatBlock();
  159. }
  160. sb[0].StatID = (uint) Stats.TimeDilation;
  161. sb[0].StatValue = m_timeDilation ; //((((m_timeDilation + (0.10f * statsUpdateFactor)) /10) / statsUpdateFactor));
  162. sb[1].StatID = (uint) Stats.SimFPS;
  163. sb[1].StatValue = simfps/statsUpdateFactor;
  164. sb[2].StatID = (uint) Stats.PhysicsFPS;
  165. sb[2].StatValue = physfps / statsUpdateFactor;
  166. sb[3].StatID = (uint) Stats.AgentUpdates;
  167. sb[3].StatValue = (m_agentUpdates / statsUpdateFactor);
  168. sb[4].StatID = (uint) Stats.Agents;
  169. sb[4].StatValue = m_rootAgents;
  170. sb[5].StatID = (uint) Stats.ChildAgents;
  171. sb[5].StatValue = m_childAgents;
  172. sb[6].StatID = (uint) Stats.TotalPrim;
  173. sb[6].StatValue = m_numPrim;
  174. sb[7].StatID = (uint) Stats.ActivePrim;
  175. sb[7].StatValue = m_activePrim;
  176. sb[8].StatID = (uint)Stats.FrameMS;
  177. sb[8].StatValue = m_frameMS / statsUpdateFactor;
  178. sb[9].StatID = (uint)Stats.NetMS;
  179. sb[9].StatValue = m_netMS / statsUpdateFactor;
  180. sb[10].StatID = (uint)Stats.PhysicsMS;
  181. sb[10].StatValue = m_physicsMS / statsUpdateFactor;
  182. sb[11].StatID = (uint)Stats.ImageMS ;
  183. sb[11].StatValue = m_imageMS / statsUpdateFactor;
  184. sb[12].StatID = (uint)Stats.OtherMS;
  185. sb[12].StatValue = m_otherMS / statsUpdateFactor;
  186. sb[13].StatID = (uint)Stats.InPacketsPerSecond;
  187. sb[13].StatValue = (m_inPacketsPerSecond);
  188. sb[14].StatID = (uint)Stats.OutPacketsPerSecond;
  189. sb[14].StatValue = (m_outPacketsPerSecond / statsUpdateFactor);
  190. sb[15].StatID = (uint)Stats.UnAckedBytes;
  191. sb[15].StatValue = m_unAckedBytes;
  192. sb[16].StatID = (uint)Stats.AgentMS;
  193. sb[16].StatValue = m_agentMS / statsUpdateFactor;
  194. sb[17].StatID = (uint)Stats.PendingDownloads;
  195. sb[17].StatValue = m_pendingDownloads;
  196. sb[18].StatID = (uint)Stats.PendingUploads;
  197. sb[18].StatValue = m_pendingUploads;
  198. sb[19].StatID = (uint)Stats.ActiveScripts;
  199. sb[19].StatValue = m_activeScripts;
  200. sb[20].StatID = (uint)Stats.ScriptLinesPerSecond;
  201. sb[20].StatValue = m_scriptLinesPerSecond / statsUpdateFactor;
  202. SimStats simStats
  203. = new SimStats(
  204. ReportingRegion.RegionLocX, ReportingRegion.RegionLocY, regionFlags, (uint)objectCapacity, rb, sb);
  205. handlerSendStatResult = OnSendStatsResult;
  206. if (handlerSendStatResult != null)
  207. {
  208. handlerSendStatResult(simStats);
  209. }
  210. resetvalues();
  211. }
  212. }
  213. private void resetvalues()
  214. {
  215. m_timeDilation = 0;
  216. m_fps = 0;
  217. m_pfps = 0;
  218. m_agentUpdates = 0;
  219. m_inPacketsPerSecond = 0;
  220. m_outPacketsPerSecond = 0;
  221. m_unAckedBytes = 0;
  222. m_scriptLinesPerSecond = 0;
  223. m_frameMS = 0;
  224. m_agentMS = 0;
  225. m_netMS = 0;
  226. m_physicsMS = 0;
  227. m_imageMS = 0;
  228. m_otherMS = 0;
  229. //Ckrinke This variable is not used, so comment to remove compiler warning until it is used.
  230. //Ckrinke m_scriptMS = 0;
  231. }
  232. # region methods called from Scene
  233. // The majority of these functions are additive
  234. // so that you can easily change the amount of
  235. // seconds in between sim stats updates
  236. public void AddTimeDilation(float td)
  237. {
  238. //float tdsetting = td;
  239. //if (tdsetting > 1.0f)
  240. //tdsetting = (tdsetting - (tdsetting - 0.91f));
  241. //if (tdsetting < 0)
  242. //tdsetting = 0.0f;
  243. m_timeDilation = td;
  244. }
  245. public void SetRootAgents(int rootAgents)
  246. {
  247. m_rootAgents = rootAgents;
  248. }
  249. public void SetChildAgents(int childAgents)
  250. {
  251. m_childAgents = childAgents;
  252. }
  253. public void SetObjects(int objects)
  254. {
  255. m_numPrim = objects;
  256. }
  257. public void SetActiveObjects(int objects)
  258. {
  259. m_activePrim = objects;
  260. }
  261. public void AddFPS(int frames)
  262. {
  263. m_fps += frames;
  264. }
  265. public void AddPhysicsFPS(float frames)
  266. {
  267. m_pfps += frames;
  268. }
  269. public void AddAgentUpdates(int numUpdates)
  270. {
  271. m_agentUpdates += numUpdates;
  272. }
  273. public void AddInPackets(int numPackets)
  274. {
  275. m_inPacketsPerSecond += numPackets;
  276. }
  277. public void AddOutPackets(int numPackets)
  278. {
  279. m_outPacketsPerSecond += numPackets;
  280. }
  281. public void AddunAckedBytes(int numBytes)
  282. {
  283. m_unAckedBytes += numBytes;
  284. }
  285. public void addFrameMS(int ms)
  286. {
  287. m_frameMS += ms;
  288. }
  289. public void addNetMS(int ms)
  290. {
  291. m_netMS += ms;
  292. }
  293. public void addAgentMS(int ms)
  294. {
  295. m_agentMS += ms;
  296. }
  297. public void addPhysicsMS(int ms)
  298. {
  299. m_physicsMS += ms;
  300. }
  301. public void addImageMS(int ms)
  302. {
  303. m_imageMS += ms;
  304. }
  305. public void addOtherMS(int ms)
  306. {
  307. m_otherMS += ms;
  308. }
  309. // private static readonly log4net.ILog m_log
  310. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  311. public void addPendingDownload(int count)
  312. {
  313. m_pendingDownloads += count;
  314. //m_log.InfoFormat("[stats]: Adding {0} to pending downloads to make {1}", count, m_pendingDownloads);
  315. }
  316. public void addScriptLines(int count)
  317. {
  318. m_scriptLinesPerSecond += count;
  319. }
  320. public void SetActiveScripts(int count)
  321. {
  322. m_activeScripts = count;
  323. }
  324. public void SetObjectCapacity(int objects)
  325. {
  326. objectCapacity = objects;
  327. }
  328. /// <summary>
  329. /// This is for llGetRegionFPS
  330. /// </summary>
  331. public float getLastReportedSimFPS()
  332. {
  333. return lastReportedSimFPS;
  334. }
  335. #endregion
  336. }
  337. }