SimStatsReporter.cs 15 KB

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