SimStatsReporter.cs 30 KB

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