SimStatsReporter.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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.Monitoring;
  33. using OpenSim.Region.Framework.Interfaces;
  34. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. /// <summary>
  37. /// Collect statistics from the scene to send to the client and for access by other monitoring tools.
  38. /// </summary>
  39. /// <remarks>
  40. /// FIXME: This should be a monitoring region module
  41. /// </remarks>
  42. public class SimStatsReporter
  43. {
  44. private static readonly log4net.ILog m_log
  45. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. public const string LastReportedObjectUpdateStatName = "LastReportedObjectUpdates";
  47. public const string SlowFramesStatName = "SlowFrames";
  48. public delegate void SendStatResult(SimStats stats);
  49. public delegate void YourStatsAreWrong();
  50. public event SendStatResult OnSendStatsResult;
  51. public event YourStatsAreWrong OnStatsIncorrect;
  52. private SendStatResult handlerSendStatResult;
  53. private YourStatsAreWrong handlerStatsIncorrect;
  54. /// <summary>
  55. /// These are the IDs of stats sent in the StatsPacket to the viewer.
  56. /// </summary>
  57. /// <remarks>
  58. /// Some of these are not relevant to OpenSimulator since it is architected differently to other simulators
  59. /// (e.g. script instructions aren't executed as part of the frame loop so 'script time' is tricky).
  60. /// </remarks>
  61. public enum Stats : uint
  62. {
  63. TimeDilation = 0,
  64. SimFPS = 1,
  65. PhysicsFPS = 2,
  66. AgentUpdates = 3,
  67. FrameMS = 4,
  68. NetMS = 5,
  69. OtherMS = 6,
  70. PhysicsMS = 7,
  71. AgentMS = 8,
  72. ImageMS = 9,
  73. ScriptMS = 10,
  74. TotalPrim = 11,
  75. ActivePrim = 12,
  76. Agents = 13,
  77. ChildAgents = 14,
  78. ActiveScripts = 15,
  79. ScriptLinesPerSecond = 16,
  80. InPacketsPerSecond = 17,
  81. OutPacketsPerSecond = 18,
  82. PendingDownloads = 19,
  83. PendingUploads = 20,
  84. VirtualSizeKb = 21,
  85. ResidentSizeKb = 22,
  86. PendingLocalUploads = 23,
  87. UnAckedBytes = 24,
  88. PhysicsPinnedTasks = 25,
  89. PhysicsLodTasks = 26,
  90. SimPhysicsStepMs = 27,
  91. SimPhysicsShapeMs = 28,
  92. SimPhysicsOtherMs = 29,
  93. SimPhysicsMemory = 30,
  94. ScriptEps = 31,
  95. SimSpareMs = 32,
  96. SimSleepMs = 33,
  97. SimIoPumpTime = 34
  98. }
  99. /// <summary>
  100. /// This is for llGetRegionFPS
  101. /// </summary>
  102. public float LastReportedSimFPS
  103. {
  104. get { return lastReportedSimFPS; }
  105. }
  106. /// <summary>
  107. /// Number of object updates performed in the last stats cycle
  108. /// </summary>
  109. /// <remarks>
  110. /// This isn't sent out to the client but it is very useful data to detect whether viewers are being sent a
  111. /// large number of object updates.
  112. /// </remarks>
  113. public float LastReportedObjectUpdates { get; private set; }
  114. public float[] LastReportedSimStats
  115. {
  116. get { return lastReportedSimStats; }
  117. }
  118. /// <summary>
  119. /// Number of frames that have taken longer to process than Scene.MIN_FRAME_TIME
  120. /// </summary>
  121. public Stat SlowFramesStat { get; private set; }
  122. /// <summary>
  123. /// The threshold at which we log a slow frame.
  124. /// </summary>
  125. public int SlowFramesStatReportThreshold { get; private set; }
  126. /// <summary>
  127. /// Extra sim statistics that are used by monitors but not sent to the client.
  128. /// </summary>
  129. /// <value>
  130. /// The keys are the stat names.
  131. /// </value>
  132. private Dictionary<string, float> m_lastReportedExtraSimStats = new Dictionary<string, float>();
  133. // Sending a stats update every 3 seconds-
  134. private int m_statsUpdatesEveryMS = 3000;
  135. private float m_statsUpdateFactor;
  136. private float m_timeDilation;
  137. private int m_fps;
  138. /// <summary>
  139. /// Number of the last frame on which we processed a stats udpate.
  140. /// </summary>
  141. private uint m_lastUpdateFrame;
  142. /// <summary>
  143. /// Our nominal fps target, as expected in fps stats when a sim is running normally.
  144. /// </summary>
  145. private float m_nominalReportedFps = 55;
  146. /// <summary>
  147. /// Parameter to adjust reported scene fps
  148. /// </summary>
  149. /// <remarks>
  150. /// Our scene loop runs slower than other server implementations, apparantly because we work somewhat differently.
  151. /// However, we will still report an FPS that's closer to what people are used to seeing. A lower FPS might
  152. /// affect clients and monitoring scripts/software.
  153. /// </remarks>
  154. private float m_reportedFpsCorrectionFactor = 5;
  155. // saved last reported value so there is something available for llGetRegionFPS
  156. private float lastReportedSimFPS;
  157. private float[] lastReportedSimStats = new float[22];
  158. private float m_pfps;
  159. /// <summary>
  160. /// Number of agent updates requested in this stats cycle
  161. /// </summary>
  162. private int m_agentUpdates;
  163. /// <summary>
  164. /// Number of object updates requested in this stats cycle
  165. /// </summary>
  166. private int m_objectUpdates;
  167. private int m_frameMS;
  168. private int m_spareMS;
  169. private int m_netMS;
  170. private int m_agentMS;
  171. private int m_physicsMS;
  172. private int m_imageMS;
  173. private int m_otherMS;
  174. //Ckrinke: (3-21-08) Comment out to remove a compiler warning. Bring back into play when needed.
  175. //Ckrinke private int m_scriptMS = 0;
  176. private int m_rootAgents;
  177. private int m_childAgents;
  178. private int m_numPrim;
  179. private int m_inPacketsPerSecond;
  180. private int m_outPacketsPerSecond;
  181. private int m_activePrim;
  182. private int m_unAckedBytes;
  183. private int m_pendingDownloads;
  184. private int m_pendingUploads = 0; // FIXME: Not currently filled in
  185. private int m_activeScripts;
  186. private int m_scriptLinesPerSecond;
  187. private int m_objectCapacity = 45000;
  188. // This is the number of frames that will be stored and then averaged for
  189. // the Total, Simulation, Physics, and Network Frame Time; It is set to
  190. // 10 by default but can be changed by the OpenSim.ini configuration file
  191. // NumberOfFrames parameter
  192. private int m_numberFramesStored = 10;
  193. // The arrays that will hold the time it took to run the past N frames,
  194. // where N is the num_frames_to_average given by the configuration file
  195. private double[] m_totalFrameTimeMilliseconds;
  196. private double[] m_simulationFrameTimeMilliseconds;
  197. private double[] m_physicsFrameTimeMilliseconds;
  198. private double[] m_networkFrameTimeMilliseconds;
  199. // The location of the next time in milliseconds that will be
  200. // (over)written when the next frame completes
  201. private int m_nextLocation = 0;
  202. // The correct number of frames that have completed since the last stats
  203. // update for physics
  204. private int m_numberPhysicsFrames;
  205. private Scene m_scene;
  206. private RegionInfo ReportingRegion;
  207. private Timer m_report = new Timer();
  208. private IEstateModule estateModule;
  209. public SimStatsReporter(Scene scene)
  210. {
  211. // Initialize the different frame time arrays to the correct sizes
  212. m_totalFrameTimeMilliseconds = new double[m_numberFramesStored];
  213. m_simulationFrameTimeMilliseconds = new
  214. double[m_numberFramesStored];
  215. m_physicsFrameTimeMilliseconds = new double[m_numberFramesStored];
  216. m_networkFrameTimeMilliseconds = new double[m_numberFramesStored];
  217. m_scene = scene;
  218. m_reportedFpsCorrectionFactor = scene.MinFrameSeconds * m_nominalReportedFps;
  219. m_statsUpdateFactor = (float)(m_statsUpdatesEveryMS / 1000);
  220. ReportingRegion = scene.RegionInfo;
  221. m_objectCapacity = scene.RegionInfo.ObjectCapacity;
  222. m_report.AutoReset = true;
  223. m_report.Interval = m_statsUpdatesEveryMS;
  224. m_report.Elapsed += TriggerStatsHeartbeat;
  225. m_report.Enabled = true;
  226. if (StatsManager.SimExtraStats != null)
  227. OnSendStatsResult += StatsManager.SimExtraStats.ReceiveClassicSimStatsPacket;
  228. /// At the moment, we'll only report if a frame is over 120% of target, since commonly frames are a bit
  229. /// longer than ideal (which in itself is a concern).
  230. SlowFramesStatReportThreshold = (int)Math.Ceiling(scene.MinFrameTicks * 1.2);
  231. SlowFramesStat
  232. = new Stat(
  233. "SlowFrames",
  234. "Slow Frames",
  235. "Number of frames where frame time has been significantly longer than the desired frame time.",
  236. " frames",
  237. "scene",
  238. m_scene.Name,
  239. StatType.Push,
  240. null,
  241. StatVerbosity.Info);
  242. StatsManager.RegisterStat(SlowFramesStat);
  243. }
  244. public SimStatsReporter(Scene scene, int numberOfFrames) : this (scene)
  245. {
  246. // Store the number of frames from the OpenSim.ini configuration
  247. // file
  248. m_numberFramesStored = numberOfFrames;
  249. }
  250. public void Close()
  251. {
  252. m_report.Elapsed -= TriggerStatsHeartbeat;
  253. m_report.Close();
  254. }
  255. /// <summary>
  256. /// Sets the number of milliseconds between stat updates.
  257. /// </summary>
  258. /// <param name='ms'></param>
  259. public void SetUpdateMS(int ms)
  260. {
  261. m_statsUpdatesEveryMS = ms;
  262. m_statsUpdateFactor = (float)(m_statsUpdatesEveryMS / 1000);
  263. m_report.Interval = m_statsUpdatesEveryMS;
  264. }
  265. private void TriggerStatsHeartbeat(object sender, EventArgs args)
  266. {
  267. try
  268. {
  269. statsHeartBeat(sender, args);
  270. }
  271. catch (Exception e)
  272. {
  273. m_log.Warn(string.Format(
  274. "[SIM STATS REPORTER] Update for {0} failed with exception ",
  275. m_scene.RegionInfo.RegionName), e);
  276. }
  277. }
  278. private void statsHeartBeat(object sender, EventArgs e)
  279. {
  280. double totalSumFrameTime;
  281. double simulationSumFrameTime;
  282. double physicsSumFrameTime;
  283. double networkSumFrameTime;
  284. if (!m_scene.Active)
  285. return;
  286. SimStatsPacket.StatBlock[] sb = new SimStatsPacket.StatBlock[22];
  287. SimStatsPacket.RegionBlock rb = new SimStatsPacket.RegionBlock();
  288. // Know what's not thread safe in Mono... modifying timers.
  289. // m_log.Debug("Firing Stats Heart Beat");
  290. lock (m_report)
  291. {
  292. uint regionFlags = 0;
  293. try
  294. {
  295. if (estateModule == null)
  296. estateModule = m_scene.RequestModuleInterface<IEstateModule>();
  297. regionFlags = estateModule != null ? estateModule.GetRegionFlags() : (uint) 0;
  298. }
  299. catch (Exception)
  300. {
  301. // leave region flags at 0
  302. }
  303. #region various statistic googly moogly
  304. // ORIGINAL code commented out until we have time to add our own
  305. // statistics to the statistics window, this will be done as a
  306. // new section given the title of our current project
  307. // We're going to lie about the FPS because we've been lying since 2008. The actual FPS is currently
  308. // locked at a maximum of 11. Maybe at some point this can change so that we're not lying.
  309. //int reportedFPS = (int)(m_fps * m_reportedFpsCorrectionFactor);
  310. int reportedFPS = m_fps;
  311. // save the reported value so there is something available for llGetRegionFPS
  312. lastReportedSimFPS = reportedFPS / m_statsUpdateFactor;
  313. // ORIGINAL code commented out until we have time to add our own
  314. // statistics to the statistics window
  315. //float physfps = ((m_pfps / 1000));
  316. float physfps = m_numberPhysicsFrames;
  317. //if (physfps > 600)
  318. //physfps = physfps - (physfps - 600);
  319. if (physfps < 0)
  320. physfps = 0;
  321. #endregion
  322. m_rootAgents = m_scene.SceneGraph.GetRootAgentCount();
  323. m_childAgents = m_scene.SceneGraph.GetChildAgentCount();
  324. m_numPrim = m_scene.SceneGraph.GetTotalObjectsCount();
  325. m_activePrim = m_scene.SceneGraph.GetActiveObjectsCount();
  326. m_activeScripts = m_scene.SceneGraph.GetActiveScriptsCount();
  327. // FIXME: Checking for stat sanity is a complex approach. What we really need to do is fix the code
  328. // so that stat numbers are always consistent.
  329. CheckStatSanity();
  330. //Our time dilation is 0.91 when we're running a full speed,
  331. // therefore to make sure we get an appropriate range,
  332. // we have to factor in our error. (0.10f * statsUpdateFactor)
  333. // multiplies the fix for the error times the amount of times it'll occur a second
  334. // / 10 divides the value by the number of times the sim heartbeat runs (10fps)
  335. // Then we divide the whole amount by the amount of seconds pass in between stats updates.
  336. // 'statsUpdateFactor' is how often stats packets are sent in seconds. Used below to change
  337. // values to X-per-second values.
  338. uint thisFrame = m_scene.Frame;
  339. float framesUpdated = (float)(thisFrame - m_lastUpdateFrame) * m_reportedFpsCorrectionFactor;
  340. m_lastUpdateFrame = thisFrame;
  341. // Avoid div-by-zero if somehow we've not updated any frames.
  342. if (framesUpdated == 0)
  343. framesUpdated = 1;
  344. for (int i = 0; i < 22; i++)
  345. {
  346. sb[i] = new SimStatsPacket.StatBlock();
  347. }
  348. // Resetting the sums of the frame times to prevent any errors
  349. // in calculating the moving average for frame time
  350. totalSumFrameTime = 0;
  351. simulationSumFrameTime = 0;
  352. physicsSumFrameTime = 0;
  353. networkSumFrameTime = 0;
  354. // Loop through all the frames that were stored for the current
  355. // heartbeat to process the moving average of frame times
  356. for (int i = 0; i < m_numberFramesStored; i++)
  357. {
  358. // Sum up each frame time in order to calculate the moving
  359. // average of frame time
  360. totalSumFrameTime += m_totalFrameTimeMilliseconds[i];
  361. simulationSumFrameTime +=
  362. m_simulationFrameTimeMilliseconds[i];
  363. physicsSumFrameTime += m_physicsFrameTimeMilliseconds[i];
  364. networkSumFrameTime += m_networkFrameTimeMilliseconds[i];
  365. }
  366. sb[0].StatID = (uint) Stats.TimeDilation;
  367. sb[0].StatValue = (Single.IsNaN(m_timeDilation)) ? 0.1f : m_timeDilation ; //((((m_timeDilation + (0.10f * statsUpdateFactor)) /10) / statsUpdateFactor));
  368. sb[1].StatID = (uint) Stats.SimFPS;
  369. sb[1].StatValue = reportedFPS / m_statsUpdateFactor;
  370. sb[2].StatID = (uint) Stats.PhysicsFPS;
  371. sb[2].StatValue = physfps / m_statsUpdateFactor;
  372. sb[3].StatID = (uint) Stats.AgentUpdates;
  373. sb[3].StatValue = (m_agentUpdates / m_statsUpdateFactor);
  374. sb[4].StatID = (uint) Stats.Agents;
  375. sb[4].StatValue = m_rootAgents;
  376. sb[5].StatID = (uint) Stats.ChildAgents;
  377. sb[5].StatValue = m_childAgents;
  378. sb[6].StatID = (uint) Stats.TotalPrim;
  379. sb[6].StatValue = m_numPrim;
  380. sb[7].StatID = (uint) Stats.ActivePrim;
  381. sb[7].StatValue = m_activePrim;
  382. // ORIGINAL code commented out until we have time to add our own
  383. // statistics to the statistics window
  384. sb[8].StatID = (uint)Stats.FrameMS;
  385. //sb[8].StatValue = m_frameMS / framesUpdated;
  386. sb[8].StatValue = (float) totalSumFrameTime /
  387. m_numberFramesStored;
  388. sb[9].StatID = (uint)Stats.NetMS;
  389. //sb[9].StatValue = m_netMS / framesUpdated;
  390. sb[9].StatValue = (float) networkSumFrameTime /
  391. m_numberFramesStored;
  392. sb[10].StatID = (uint)Stats.PhysicsMS;
  393. //sb[10].StatValue = m_physicsMS / framesUpdated;
  394. sb[10].StatValue = (float) physicsSumFrameTime /
  395. m_numberFramesStored;
  396. sb[11].StatID = (uint)Stats.ImageMS ;
  397. sb[11].StatValue = m_imageMS / framesUpdated;
  398. sb[12].StatID = (uint)Stats.OtherMS;
  399. //sb[12].StatValue = m_otherMS / framesUpdated;
  400. sb[12].StatValue = (float) simulationSumFrameTime /
  401. m_numberFramesStored;
  402. sb[13].StatID = (uint)Stats.InPacketsPerSecond;
  403. sb[13].StatValue = (m_inPacketsPerSecond / m_statsUpdateFactor);
  404. sb[14].StatID = (uint)Stats.OutPacketsPerSecond;
  405. sb[14].StatValue = (m_outPacketsPerSecond / m_statsUpdateFactor);
  406. sb[15].StatID = (uint)Stats.UnAckedBytes;
  407. sb[15].StatValue = m_unAckedBytes;
  408. sb[16].StatID = (uint)Stats.AgentMS;
  409. sb[16].StatValue = m_agentMS / framesUpdated;
  410. sb[17].StatID = (uint)Stats.PendingDownloads;
  411. sb[17].StatValue = m_pendingDownloads;
  412. sb[18].StatID = (uint)Stats.PendingUploads;
  413. sb[18].StatValue = m_pendingUploads;
  414. sb[19].StatID = (uint)Stats.ActiveScripts;
  415. sb[19].StatValue = m_activeScripts;
  416. sb[20].StatID = (uint)Stats.ScriptLinesPerSecond;
  417. sb[20].StatValue = m_scriptLinesPerSecond / m_statsUpdateFactor;
  418. sb[21].StatID = (uint)Stats.SimSpareMs;
  419. sb[21].StatValue = m_spareMS / framesUpdated;
  420. for (int i = 0; i < 22; i++)
  421. {
  422. lastReportedSimStats[i] = sb[i].StatValue;
  423. }
  424. SimStats simStats
  425. = new SimStats(
  426. ReportingRegion.RegionLocX, ReportingRegion.RegionLocY, regionFlags, (uint)m_objectCapacity,
  427. rb, sb, m_scene.RegionInfo.originRegionID);
  428. handlerSendStatResult = OnSendStatsResult;
  429. if (handlerSendStatResult != null)
  430. {
  431. handlerSendStatResult(simStats);
  432. }
  433. // Extra statistics that aren't currently sent to clients
  434. lock (m_lastReportedExtraSimStats)
  435. {
  436. m_lastReportedExtraSimStats[LastReportedObjectUpdateStatName] = m_objectUpdates / m_statsUpdateFactor;
  437. m_lastReportedExtraSimStats[SlowFramesStat.ShortName] = (float)SlowFramesStat.Value;
  438. Dictionary<string, float> physicsStats = m_scene.PhysicsScene.GetStats();
  439. if (physicsStats != null)
  440. {
  441. foreach (KeyValuePair<string, float> tuple in physicsStats)
  442. {
  443. // FIXME: An extremely dirty hack to divide MS stats per frame rather than per second
  444. // Need to change things so that stats source can indicate whether they are per second or
  445. // per frame.
  446. if (tuple.Key.EndsWith("MS"))
  447. m_lastReportedExtraSimStats[tuple.Key] = tuple.Value / framesUpdated;
  448. else
  449. m_lastReportedExtraSimStats[tuple.Key] = tuple.Value / m_statsUpdateFactor;
  450. }
  451. }
  452. }
  453. ResetValues();
  454. }
  455. }
  456. private void ResetValues()
  457. {
  458. // Reset the number of frames that the physics library has
  459. // processed since the last stats report
  460. m_numberPhysicsFrames = 0;
  461. m_timeDilation = 0;
  462. m_fps = 0;
  463. m_pfps = 0;
  464. m_agentUpdates = 0;
  465. m_objectUpdates = 0;
  466. //m_inPacketsPerSecond = 0;
  467. //m_outPacketsPerSecond = 0;
  468. m_unAckedBytes = 0;
  469. m_scriptLinesPerSecond = 0;
  470. m_frameMS = 0;
  471. m_agentMS = 0;
  472. m_netMS = 0;
  473. m_physicsMS = 0;
  474. m_imageMS = 0;
  475. m_otherMS = 0;
  476. m_spareMS = 0;
  477. //Ckrinke This variable is not used, so comment to remove compiler warning until it is used.
  478. //Ckrinke m_scriptMS = 0;
  479. }
  480. # region methods called from Scene
  481. // The majority of these functions are additive
  482. // so that you can easily change the amount of
  483. // seconds in between sim stats updates
  484. public void AddTimeDilation(float td)
  485. {
  486. //float tdsetting = td;
  487. //if (tdsetting > 1.0f)
  488. //tdsetting = (tdsetting - (tdsetting - 0.91f));
  489. //if (tdsetting < 0)
  490. //tdsetting = 0.0f;
  491. m_timeDilation = td;
  492. }
  493. internal void CheckStatSanity()
  494. {
  495. if (m_rootAgents < 0 || m_childAgents < 0)
  496. {
  497. handlerStatsIncorrect = OnStatsIncorrect;
  498. if (handlerStatsIncorrect != null)
  499. {
  500. handlerStatsIncorrect();
  501. }
  502. }
  503. if (m_rootAgents == 0 && m_childAgents == 0)
  504. {
  505. m_unAckedBytes = 0;
  506. }
  507. }
  508. public void AddFPS(int frames)
  509. {
  510. m_fps += frames;
  511. }
  512. public void AddPhysicsFPS(float frames)
  513. {
  514. m_pfps += frames;
  515. }
  516. public void AddObjectUpdates(int numUpdates)
  517. {
  518. m_objectUpdates += numUpdates;
  519. }
  520. public void AddAgentUpdates(int numUpdates)
  521. {
  522. m_agentUpdates += numUpdates;
  523. }
  524. public void AddInPackets(int numPackets)
  525. {
  526. m_inPacketsPerSecond = numPackets;
  527. }
  528. public void AddOutPackets(int numPackets)
  529. {
  530. m_outPacketsPerSecond = numPackets;
  531. }
  532. public void AddunAckedBytes(int numBytes)
  533. {
  534. m_unAckedBytes += numBytes;
  535. if (m_unAckedBytes < 0) m_unAckedBytes = 0;
  536. }
  537. public void addFrameMS(int ms)
  538. {
  539. m_frameMS += ms;
  540. // At the moment, we'll only report if a frame is over 120% of target, since commonly frames are a bit
  541. // longer than ideal due to the inaccuracy of the Sleep in Scene.Update() (which in itself is a concern).
  542. if (ms > SlowFramesStatReportThreshold)
  543. SlowFramesStat.Value++;
  544. }
  545. public void AddSpareMS(int ms)
  546. {
  547. m_spareMS += ms;
  548. }
  549. public void addNetMS(int ms)
  550. {
  551. m_netMS += ms;
  552. }
  553. public void addAgentMS(int ms)
  554. {
  555. m_agentMS += ms;
  556. }
  557. public void addPhysicsMS(int ms)
  558. {
  559. m_physicsMS += ms;
  560. }
  561. public void addImageMS(int ms)
  562. {
  563. m_imageMS += ms;
  564. }
  565. public void addOtherMS(int ms)
  566. {
  567. m_otherMS += ms;
  568. }
  569. public void addPhysicsFrame(int frames)
  570. {
  571. // Add the number of physics frames to the correct total physics
  572. // frames
  573. m_numberPhysicsFrames += frames;
  574. }
  575. public void addFrameTimeMilliseconds(double total, double simulation,
  576. double physics, double network)
  577. {
  578. // Save the frame times from the current frame into the appropriate
  579. // arrays
  580. m_totalFrameTimeMilliseconds[m_nextLocation] = total;
  581. m_simulationFrameTimeMilliseconds[m_nextLocation] = simulation;
  582. m_physicsFrameTimeMilliseconds[m_nextLocation] = physics;
  583. m_networkFrameTimeMilliseconds[m_nextLocation] = network;
  584. // Update to the next location in the list
  585. m_nextLocation++;
  586. // Since the list will begin to overwrite the oldest frame values
  587. // first, the next location needs to loop back to the beginning of the
  588. // list whenever it reaches the end
  589. m_nextLocation = m_nextLocation % m_numberFramesStored;
  590. }
  591. public void AddPendingDownloads(int count)
  592. {
  593. m_pendingDownloads += count;
  594. if (m_pendingDownloads < 0)
  595. m_pendingDownloads = 0;
  596. //m_log.InfoFormat("[stats]: Adding {0} to pending downloads to make {1}", count, m_pendingDownloads);
  597. }
  598. public void addScriptLines(int count)
  599. {
  600. m_scriptLinesPerSecond += count;
  601. }
  602. public void AddPacketsStats(int inPackets, int outPackets, int unAckedBytes)
  603. {
  604. AddInPackets(inPackets);
  605. AddOutPackets(outPackets);
  606. AddunAckedBytes(unAckedBytes);
  607. }
  608. #endregion
  609. public Dictionary<string, float> GetExtraSimStats()
  610. {
  611. lock (m_lastReportedExtraSimStats)
  612. return new Dictionary<string, float>(m_lastReportedExtraSimStats);
  613. }
  614. }
  615. }