SimStatsReporter.cs 17 KB

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