SimStatsReporter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. // Determines the size of the array that is used to collect StatBlocks
  55. // for sending viewer compatible stats must be conform with sb array filling below
  56. private const int m_statisticViewerArraySize = 38;
  57. // size of LastReportedSimFPS with extra stats.
  58. private const int m_statisticExtraArraySize = (int)(Stats.SimExtraCountEnd - Stats.SimExtraCountStart);
  59. /// <summary>
  60. /// These are the IDs of stats sent in the StatsPacket to the viewer.
  61. /// </summary>
  62. /// <remarks>
  63. /// Some of these are not relevant to OpenSimulator since it is architected differently to other simulators
  64. /// (e.g. script instructions aren't executed as part of the frame loop so 'script time' is tricky).
  65. /// </remarks>
  66. public enum Stats : uint
  67. {
  68. // viewers defined IDs
  69. TimeDilation = 0,
  70. SimFPS = 1,
  71. PhysicsFPS = 2,
  72. AgentUpdates = 3,
  73. FrameMS = 4,
  74. NetMS = 5,
  75. OtherMS = 6,
  76. PhysicsMS = 7,
  77. AgentMS = 8,
  78. ImageMS = 9,
  79. ScriptMS = 10,
  80. TotalPrim = 11,
  81. ActivePrim = 12,
  82. Agents = 13,
  83. ChildAgents = 14,
  84. ActiveScripts = 15,
  85. LSLScriptLinesPerSecond = 16, // viewers don't like this anymore
  86. InPacketsPerSecond = 17,
  87. OutPacketsPerSecond = 18,
  88. PendingDownloads = 19,
  89. PendingUploads = 20,
  90. VirtualSizeKb = 21,
  91. ResidentSizeKb = 22,
  92. PendingLocalUploads = 23,
  93. UnAckedBytes = 24,
  94. PhysicsPinnedTasks = 25,
  95. PhysicsLodTasks = 26,
  96. SimPhysicsStepMs = 27,
  97. SimPhysicsShapeMs = 28,
  98. SimPhysicsOtherMs = 29,
  99. SimPhysicsMemory = 30,
  100. ScriptEps = 31,
  101. SimSpareMs = 32,
  102. SimSleepMs = 33,
  103. SimIoPumpTime = 34,
  104. SimPCTSscriptsRun = 35,
  105. SimRegionIdle = 36, // dataserver only
  106. SimRegionIdlePossible = 37, // dataserver only
  107. SimAIStepTimeMS = 38,
  108. SimSkippedSillouet_PS = 39,
  109. SimSkippedCharsPerC = 40,
  110. // extra stats IDs irrelevant, just far from viewer defined ones
  111. SimExtraCountStart = 1000,
  112. internalLSLScriptLinesPerSecond = 1000,
  113. FrameDilation2 = 1001,
  114. UsersLoggingIn = 1002,
  115. TotalGeoPrim = 1003,
  116. TotalMesh = 1004,
  117. ThreadCount = 1005,
  118. SimExtraCountEnd = 1006
  119. }
  120. /// <summary>
  121. /// This is for llGetRegionFPS
  122. /// </summary>
  123. public float LastReportedSimFPS
  124. {
  125. get { return lastReportedSimFPS; }
  126. }
  127. /// <summary>
  128. /// Number of object updates performed in the last stats cycle
  129. /// </summary>
  130. /// <remarks>
  131. /// This isn't sent out to the client but it is very useful data to detect whether viewers are being sent a
  132. /// large number of object updates.
  133. /// </remarks>
  134. public float LastReportedObjectUpdates { get; private set; }
  135. public float[] LastReportedSimStats
  136. {
  137. get { return lastReportedSimStats; }
  138. }
  139. /// <summary>
  140. /// Number of frames that have taken longer to process than Scene.MIN_FRAME_TIME
  141. /// </summary>
  142. public Stat SlowFramesStat { get; private set; }
  143. /// <summary>
  144. /// The threshold at which we log a slow frame.
  145. /// </summary>
  146. public int SlowFramesStatReportThreshold { get; private set; }
  147. /// <summary>
  148. /// Extra sim statistics that are used by monitors but not sent to the client.
  149. /// </summary>
  150. /// <value>
  151. /// The keys are the stat names.
  152. /// </value>
  153. private Dictionary<string, float> m_lastReportedExtraSimStats = new Dictionary<string, float>();
  154. // Sending a stats update every 3 seconds-
  155. private int m_statsUpdatesEveryMS = 3000;
  156. private float m_statsUpdateFactor;
  157. private float m_timeDilation;
  158. private int m_fps;
  159. /// <summary>
  160. /// Number of the last frame on which we processed a stats udpate.
  161. /// </summary>
  162. private uint m_lastUpdateFrame;
  163. /// <summary>
  164. /// Parameter to adjust reported scene fps
  165. /// </summary>
  166. /// <remarks>
  167. /// Our scene loop runs slower than other server implementations, apparantly because we work somewhat differently.
  168. /// However, we will still report an FPS that's closer to what people are used to seeing. A lower FPS might
  169. /// affect clients and monitoring scripts/software.
  170. /// </remarks>
  171. private float m_reportedFpsCorrectionFactor = 1.0f;
  172. // saved last reported value so there is something available for llGetRegionFPS
  173. private float lastReportedSimFPS;
  174. private float[] lastReportedSimStats = new float[m_statisticExtraArraySize + m_statisticViewerArraySize];
  175. private float m_pfps;
  176. /// <summary>
  177. /// Number of agent updates requested in this stats cycle
  178. /// </summary>
  179. private int m_agentUpdates;
  180. /// <summary>
  181. /// Number of object updates requested in this stats cycle
  182. /// </summary>
  183. private int m_objectUpdates;
  184. private float m_frameMS;
  185. private float m_netMS;
  186. private float m_agentMS;
  187. private float m_physicsMS;
  188. private float m_imageMS;
  189. private float m_otherMS;
  190. private float m_sleeptimeMS;
  191. private int m_rootAgents;
  192. private int m_childAgents;
  193. private int m_numPrim;
  194. private int m_numGeoPrim;
  195. private int m_numMesh;
  196. private int m_inPacketsPerSecond;
  197. private int m_outPacketsPerSecond;
  198. private int m_activePrim;
  199. private int m_unAckedBytes;
  200. private int m_pendingDownloads;
  201. private int m_pendingUploads = 0; // FIXME: Not currently filled in
  202. private int m_activeScripts;
  203. private int m_scriptLinesPerSecond;
  204. private int m_scriptEventsPerSecond;
  205. private int m_objectCapacity = 45000;
  206. // The current number of users attempting to login to the region
  207. private int m_usersLoggingIn;
  208. // The last reported value of threads from the SmartThreadPool inside of
  209. // XEngine
  210. private int m_inUseThreads;
  211. private Scene m_scene;
  212. private RegionInfo ReportingRegion;
  213. private Timer m_report = new Timer();
  214. private IEstateModule estateModule;
  215. public SimStatsReporter(Scene scene)
  216. {
  217. m_scene = scene;
  218. m_statsUpdateFactor = (float)(m_statsUpdatesEveryMS / 1000.0f);
  219. ReportingRegion = scene.RegionInfo;
  220. m_objectCapacity = scene.RegionInfo.ObjectCapacity;
  221. m_report.AutoReset = true;
  222. m_report.Interval = m_statsUpdatesEveryMS;
  223. m_report.Elapsed += TriggerStatsHeartbeat;
  224. m_report.Enabled = true;
  225. if (StatsManager.SimExtraStats != null)
  226. OnSendStatsResult += StatsManager.SimExtraStats.ReceiveClassicSimStatsPacket;
  227. /// At the moment, we'll only report if a frame is over 120% of target, since commonly frames are a bit
  228. /// longer than ideal (which in itself is a concern).
  229. SlowFramesStatReportThreshold = (int)Math.Ceiling(m_scene.MinFrameTime * 1000 * 1.2);
  230. SlowFramesStat
  231. = new Stat(
  232. "SlowFrames",
  233. "Slow Frames",
  234. "Number of frames where frame time has been significantly longer than the desired frame time.",
  235. " frames",
  236. "scene",
  237. m_scene.Name,
  238. StatType.Push,
  239. null,
  240. StatVerbosity.Info);
  241. StatsManager.RegisterStat(SlowFramesStat);
  242. }
  243. public void Close()
  244. {
  245. m_report.Elapsed -= TriggerStatsHeartbeat;
  246. m_report.Close();
  247. }
  248. /// <summary>
  249. /// Sets the number of milliseconds between stat updates.
  250. /// </summary>
  251. /// <param name='ms'></param>
  252. public void SetUpdateMS(int ms)
  253. {
  254. m_statsUpdatesEveryMS = ms;
  255. m_statsUpdateFactor = (float)(m_statsUpdatesEveryMS / 1000.0f);
  256. m_report.Interval = m_statsUpdatesEveryMS;
  257. }
  258. private void TriggerStatsHeartbeat(object sender, EventArgs args)
  259. {
  260. try
  261. {
  262. statsHeartBeat(sender, args);
  263. }
  264. catch (Exception e)
  265. {
  266. m_log.Warn(string.Format(
  267. "[SIM STATS REPORTER] Update for {0} failed with exception ",
  268. m_scene.RegionInfo.RegionName), e);
  269. }
  270. }
  271. private void statsHeartBeat(object sender, EventArgs e)
  272. {
  273. if (!m_scene.Active)
  274. return;
  275. SimStatsPacket.StatBlock[] sb = new SimStatsPacket.StatBlock[m_statisticViewerArraySize];
  276. SimStatsPacket.StatBlock[] sbex = new SimStatsPacket.StatBlock[m_statisticExtraArraySize];
  277. SimStatsPacket.RegionBlock rb = new SimStatsPacket.RegionBlock();
  278. // Know what's not thread safe in Mono... modifying timers.
  279. // m_log.Debug("Firing Stats Heart Beat");
  280. lock (m_report)
  281. {
  282. uint regionFlags = 0;
  283. try
  284. {
  285. if (estateModule == null)
  286. estateModule = m_scene.RequestModuleInterface<IEstateModule>();
  287. regionFlags = estateModule != null ? estateModule.GetRegionFlags() : (uint) 0;
  288. }
  289. catch (Exception)
  290. {
  291. // leave region flags at 0
  292. }
  293. #region various statistic googly moogly
  294. // factor to consider updates integration time
  295. float updateFactor = 1.0f / m_statsUpdateFactor;
  296. // the nominal frame time, corrected by reporting multiplier
  297. float TargetFrameTime = 1000.0f * m_scene.MinFrameTime / m_reportedFpsCorrectionFactor;
  298. // acumulated fps scaled by reporting multiplier
  299. float reportedFPS = (m_fps * m_reportedFpsCorrectionFactor);
  300. if (reportedFPS <= 0)
  301. reportedFPS = 1;
  302. // factor to calculate per frame values
  303. float perframefactor = 1.0f / (float)reportedFPS;
  304. // fps considering the integration time
  305. reportedFPS = reportedFPS * updateFactor;
  306. // save the reported value so there is something available for llGetRegionFPS
  307. lastReportedSimFPS = reportedFPS;
  308. #endregion
  309. m_rootAgents = m_scene.SceneGraph.GetRootAgentCount();
  310. m_childAgents = m_scene.SceneGraph.GetChildAgentCount();
  311. m_numPrim = m_scene.SceneGraph.GetTotalObjectsCount();
  312. m_numGeoPrim = m_scene.SceneGraph.GetTotalPrimObjectsCount();
  313. m_numMesh = m_scene.SceneGraph.GetTotalMeshObjectsCount();
  314. m_activePrim = m_scene.SceneGraph.GetActiveObjectsCount();
  315. m_activeScripts = m_scene.SceneGraph.GetActiveScriptsCount();
  316. float physfps = m_pfps;
  317. if (physfps < 0)
  318. physfps = 0;
  319. float sparetime;
  320. float sleeptime;
  321. float TotalFrameTime = m_frameMS * perframefactor;
  322. sleeptime = m_sleeptimeMS * perframefactor;
  323. sparetime = m_frameMS - m_sleeptimeMS; // total time minus sleep
  324. sparetime *= perframefactor; // average per frame
  325. sparetime = TargetFrameTime - sparetime; // real spare
  326. if (sparetime < 0)
  327. sparetime = 0;
  328. else if (sparetime > TotalFrameTime)
  329. sparetime = TotalFrameTime;
  330. // FIXME: Checking for stat sanity is a complex approach. What we really need to do is fix the code
  331. // so that stat numbers are always consistent.
  332. CheckStatSanity();
  333. // other MS is actually simulation time
  334. // m_otherMS = m_frameMS - m_physicsMS - m_imageMS - m_netMS - m_agentMS;
  335. // m_imageMS m_netMS are not included in m_frameMS
  336. m_otherMS = m_frameMS - m_physicsMS - m_agentMS - m_sleeptimeMS;
  337. if (m_otherMS < 0)
  338. m_otherMS = 0;
  339. for (int i = 0; i < m_statisticViewerArraySize; i++)
  340. {
  341. sb[i] = new SimStatsPacket.StatBlock();
  342. }
  343. sb[0].StatID = (uint) Stats.TimeDilation;
  344. sb[0].StatValue = (Single.IsNaN(m_timeDilation)) ? 0.1f : m_timeDilation ;
  345. sb[1].StatID = (uint) Stats.SimFPS;
  346. sb[1].StatValue = reportedFPS;
  347. sb[2].StatID = (uint) Stats.PhysicsFPS;
  348. sb[2].StatValue = physfps * updateFactor;
  349. sb[3].StatID = (uint) Stats.AgentUpdates;
  350. sb[3].StatValue = m_agentUpdates * updateFactor;
  351. sb[4].StatID = (uint) Stats.Agents;
  352. sb[4].StatValue = m_rootAgents;
  353. sb[5].StatID = (uint) Stats.ChildAgents;
  354. sb[5].StatValue = m_childAgents;
  355. sb[6].StatID = (uint) Stats.TotalPrim;
  356. sb[6].StatValue = m_numPrim;
  357. sb[7].StatID = (uint) Stats.ActivePrim;
  358. sb[7].StatValue = m_activePrim;
  359. sb[8].StatID = (uint)Stats.FrameMS;
  360. sb[8].StatValue = TotalFrameTime;
  361. sb[9].StatID = (uint)Stats.NetMS;
  362. sb[9].StatValue = m_netMS * perframefactor;
  363. sb[10].StatID = (uint)Stats.PhysicsMS;
  364. sb[10].StatValue = m_physicsMS * perframefactor;
  365. sb[11].StatID = (uint)Stats.ImageMS ;
  366. sb[11].StatValue = m_imageMS * perframefactor;
  367. sb[12].StatID = (uint)Stats.OtherMS;
  368. sb[12].StatValue = m_otherMS * perframefactor;
  369. sb[13].StatID = (uint)Stats.InPacketsPerSecond;
  370. sb[13].StatValue = (m_inPacketsPerSecond * updateFactor);
  371. sb[14].StatID = (uint)Stats.OutPacketsPerSecond;
  372. sb[14].StatValue = (m_outPacketsPerSecond * updateFactor);
  373. sb[15].StatID = (uint)Stats.UnAckedBytes;
  374. sb[15].StatValue = m_unAckedBytes;
  375. sb[16].StatID = (uint)Stats.AgentMS;
  376. sb[16].StatValue = m_agentMS * perframefactor;
  377. sb[17].StatID = (uint)Stats.PendingDownloads;
  378. sb[17].StatValue = m_pendingDownloads;
  379. sb[18].StatID = (uint)Stats.PendingUploads;
  380. sb[18].StatValue = m_pendingUploads;
  381. sb[19].StatID = (uint)Stats.ActiveScripts;
  382. sb[19].StatValue = m_activeScripts;
  383. sb[20].StatID = (uint)Stats.SimSleepMs;
  384. sb[20].StatValue = sleeptime;
  385. sb[21].StatID = (uint)Stats.SimSpareMs;
  386. sb[21].StatValue = sparetime;
  387. // this should came from phys engine
  388. sb[22].StatID = (uint)Stats.SimPhysicsStepMs;
  389. sb[22].StatValue = 20;
  390. // send the ones we dont have as zeros, to clean viewers state
  391. // specially arriving from regions with wrond IDs in use
  392. sb[23].StatID = (uint)Stats.VirtualSizeKb;
  393. sb[23].StatValue = 0;
  394. sb[24].StatID = (uint)Stats.ResidentSizeKb;
  395. sb[24].StatValue = 0;
  396. sb[25].StatID = (uint)Stats.PendingLocalUploads;
  397. sb[25].StatValue = 0;
  398. sb[26].StatID = (uint)Stats.PhysicsPinnedTasks;
  399. sb[26].StatValue = 0;
  400. sb[27].StatID = (uint)Stats.PhysicsLodTasks;
  401. sb[27].StatValue = 0;
  402. sb[28].StatID = (uint)Stats.ScriptEps; // we actuall have this, but not messing array order AGAIN
  403. sb[28].StatValue = m_scriptEventsPerSecond * updateFactor;
  404. sb[29].StatID = (uint)Stats.SimAIStepTimeMS;
  405. sb[29].StatValue = 0;
  406. sb[30].StatID = (uint)Stats.SimIoPumpTime;
  407. sb[30].StatValue = 0;
  408. sb[31].StatID = (uint)Stats.SimPCTSscriptsRun;
  409. sb[31].StatValue = 0;
  410. sb[32].StatID = (uint)Stats.SimRegionIdle;
  411. sb[32].StatValue = 0;
  412. sb[33].StatID = (uint)Stats.SimRegionIdlePossible;
  413. sb[33].StatValue = 0;
  414. sb[34].StatID = (uint)Stats.SimSkippedSillouet_PS;
  415. sb[34].StatValue = 0;
  416. sb[35].StatID = (uint)Stats.SimSkippedCharsPerC;
  417. sb[35].StatValue = 0;
  418. sb[36].StatID = (uint)Stats.SimPhysicsMemory;
  419. sb[36].StatValue = 0;
  420. sb[37].StatID = (uint)Stats.ScriptMS;
  421. sb[37].StatValue = 0;
  422. for (int i = 0; i < m_statisticViewerArraySize; i++)
  423. {
  424. lastReportedSimStats[i] = sb[i].StatValue;
  425. }
  426. // add extra stats for internal use
  427. for (int i = 0; i < m_statisticExtraArraySize; i++)
  428. {
  429. sbex[i] = new SimStatsPacket.StatBlock();
  430. }
  431. sbex[0].StatID = (uint)Stats.LSLScriptLinesPerSecond;
  432. sbex[0].StatValue = m_scriptLinesPerSecond * updateFactor;
  433. lastReportedSimStats[38] = m_scriptLinesPerSecond * updateFactor;
  434. sbex[1].StatID = (uint)Stats.FrameDilation2;
  435. sbex[1].StatValue = (Single.IsNaN(m_timeDilation)) ? 0.1f : m_timeDilation;
  436. lastReportedSimStats[39] = (Single.IsNaN(m_timeDilation)) ? 0.1f : m_timeDilation;
  437. sbex[2].StatID = (uint)Stats.UsersLoggingIn;
  438. sbex[2].StatValue = m_usersLoggingIn;
  439. lastReportedSimStats[40] = m_usersLoggingIn;
  440. sbex[3].StatID = (uint)Stats.TotalGeoPrim;
  441. sbex[3].StatValue = m_numGeoPrim;
  442. lastReportedSimStats[41] = m_numGeoPrim;
  443. sbex[4].StatID = (uint)Stats.TotalMesh;
  444. sbex[4].StatValue = m_numMesh;
  445. lastReportedSimStats[42] = m_numMesh;
  446. sbex[5].StatID = (uint)Stats.ThreadCount;
  447. sbex[5].StatValue = m_inUseThreads;
  448. lastReportedSimStats[43] = m_inUseThreads;
  449. SimStats simStats
  450. = new SimStats(
  451. ReportingRegion.RegionLocX, ReportingRegion.RegionLocY, regionFlags, (uint)m_objectCapacity,
  452. rb, sb, sbex, m_scene.RegionInfo.originRegionID);
  453. handlerSendStatResult = OnSendStatsResult;
  454. if (handlerSendStatResult != null)
  455. {
  456. handlerSendStatResult(simStats);
  457. }
  458. // Extra statistics that aren't currently sent to clients
  459. if (m_scene.PhysicsScene != null)
  460. {
  461. lock (m_lastReportedExtraSimStats)
  462. {
  463. m_lastReportedExtraSimStats[LastReportedObjectUpdateStatName] = m_objectUpdates * updateFactor;
  464. m_lastReportedExtraSimStats[SlowFramesStat.ShortName] = (float)SlowFramesStat.Value;
  465. Dictionary<string, float> physicsStats = m_scene.PhysicsScene.GetStats();
  466. if (physicsStats != null)
  467. {
  468. foreach (KeyValuePair<string, float> tuple in physicsStats)
  469. {
  470. // FIXME: An extremely dirty hack to divide MS stats per frame rather than per second
  471. // Need to change things so that stats source can indicate whether they are per second or
  472. // per frame.
  473. if (tuple.Key.EndsWith("MS"))
  474. m_lastReportedExtraSimStats[tuple.Key] = tuple.Value * perframefactor;
  475. else
  476. m_lastReportedExtraSimStats[tuple.Key] = tuple.Value * updateFactor;
  477. }
  478. }
  479. }
  480. }
  481. // LastReportedObjectUpdates = m_objectUpdates / m_statsUpdateFactor;
  482. ResetValues();
  483. }
  484. }
  485. private void ResetValues()
  486. {
  487. // Reset the number of frames that the physics library has
  488. // processed since the last stats report
  489. m_timeDilation = 0;
  490. m_fps = 0;
  491. m_pfps = 0;
  492. m_agentUpdates = 0;
  493. m_objectUpdates = 0;
  494. //m_inPacketsPerSecond = 0;
  495. //m_outPacketsPerSecond = 0;
  496. m_unAckedBytes = 0;
  497. m_scriptLinesPerSecond = 0;
  498. m_scriptEventsPerSecond = 0;
  499. m_frameMS = 0;
  500. m_agentMS = 0;
  501. m_netMS = 0;
  502. m_physicsMS = 0;
  503. m_imageMS = 0;
  504. m_otherMS = 0;
  505. m_sleeptimeMS = 0;
  506. //Ckrinke This variable is not used, so comment to remove compiler warning until it is used.
  507. //Ckrinke m_scriptMS = 0;
  508. }
  509. # region methods called from Scene
  510. // The majority of these functions are additive
  511. // so that you can easily change the amount of
  512. // seconds in between sim stats updates
  513. public void AddTimeDilation(float td)
  514. {
  515. //float tdsetting = td;
  516. //if (tdsetting > 1.0f)
  517. //tdsetting = (tdsetting - (tdsetting - 0.91f));
  518. //if (tdsetting < 0)
  519. //tdsetting = 0.0f;
  520. m_timeDilation = td;
  521. }
  522. internal void CheckStatSanity()
  523. {
  524. if (m_rootAgents < 0 || m_childAgents < 0)
  525. {
  526. handlerStatsIncorrect = OnStatsIncorrect;
  527. if (handlerStatsIncorrect != null)
  528. {
  529. handlerStatsIncorrect();
  530. }
  531. }
  532. if (m_rootAgents == 0 && m_childAgents == 0)
  533. {
  534. m_unAckedBytes = 0;
  535. }
  536. }
  537. public void AddFPS(int frames)
  538. {
  539. m_fps += frames;
  540. }
  541. public void AddPhysicsFPS(float frames)
  542. {
  543. m_pfps += frames;
  544. }
  545. public void AddObjectUpdates(int numUpdates)
  546. {
  547. m_objectUpdates += numUpdates;
  548. }
  549. public void AddAgentUpdates(int numUpdates)
  550. {
  551. m_agentUpdates += numUpdates;
  552. }
  553. public void AddInPackets(int numPackets)
  554. {
  555. m_inPacketsPerSecond = numPackets;
  556. }
  557. public void AddOutPackets(int numPackets)
  558. {
  559. m_outPacketsPerSecond = numPackets;
  560. }
  561. public void AddunAckedBytes(int numBytes)
  562. {
  563. m_unAckedBytes += numBytes;
  564. if (m_unAckedBytes < 0) m_unAckedBytes = 0;
  565. }
  566. public void addFrameMS(float ms)
  567. {
  568. m_frameMS += ms;
  569. // At the moment, we'll only report if a frame is over 120% of target, since commonly frames are a bit
  570. // longer than ideal due to the inaccuracy of the Sleep in Scene.Update() (which in itself is a concern).
  571. if (ms > SlowFramesStatReportThreshold)
  572. SlowFramesStat.Value++;
  573. }
  574. public void addNetMS(float ms)
  575. {
  576. m_netMS += ms;
  577. }
  578. public void addAgentMS(float ms)
  579. {
  580. m_agentMS += ms;
  581. }
  582. public void addPhysicsMS(float ms)
  583. {
  584. m_physicsMS += ms;
  585. }
  586. public void addImageMS(float ms)
  587. {
  588. m_imageMS += ms;
  589. }
  590. public void addOtherMS(float ms)
  591. {
  592. m_otherMS += ms;
  593. }
  594. public void addSleepMS(float ms)
  595. {
  596. m_sleeptimeMS += ms;
  597. }
  598. public void AddPendingDownloads(int count)
  599. {
  600. m_pendingDownloads += count;
  601. if (m_pendingDownloads < 0)
  602. m_pendingDownloads = 0;
  603. //m_log.InfoFormat("[stats]: Adding {0} to pending downloads to make {1}", count, m_pendingDownloads);
  604. }
  605. public void addScriptLines(int count)
  606. {
  607. m_scriptLinesPerSecond += count;
  608. }
  609. public void addScriptEvents(int count)
  610. {
  611. m_scriptEventsPerSecond += count;
  612. }
  613. public void AddPacketsStats(int inPackets, int outPackets, int unAckedBytes)
  614. {
  615. AddInPackets(inPackets);
  616. AddOutPackets(outPackets);
  617. AddunAckedBytes(unAckedBytes);
  618. }
  619. public void UpdateUsersLoggingIn(bool isLoggingIn)
  620. {
  621. // Determine whether the user has started logging in or has completed
  622. // logging into the region
  623. if (isLoggingIn)
  624. {
  625. // The user is starting to login to the region so increment the
  626. // number of users attempting to login to the region
  627. m_usersLoggingIn++;
  628. }
  629. else
  630. {
  631. // The user has finished logging into the region so decrement the
  632. // number of users logging into the region
  633. m_usersLoggingIn--;
  634. }
  635. }
  636. public void SetThreadCount(int inUseThreads)
  637. {
  638. // Save the new number of threads to our member variable to send to
  639. // the extra stats collector
  640. m_inUseThreads = inUseThreads;
  641. }
  642. #endregion
  643. public Dictionary<string, float> GetExtraSimStats()
  644. {
  645. lock (m_lastReportedExtraSimStats)
  646. return new Dictionary<string, float>(m_lastReportedExtraSimStats);
  647. }
  648. }
  649. }