Implement experimental non-default mechanism to update scene via a timer rather than a persistent thread with sleep.

This is to see if an inaccuracy in sleep times under load is responsible for increase in frame times even when there is spare time still available.
Can currently only be activated by setting "debug scene set update-on-timer true".
Can be switched between timer and thread with sleep updates whilst the scene is running.
ghosts
Justin Clark-Casey (justincc) 2014-08-26 17:37:18 +01:00
parent aeadddf77a
commit 64f640f901
4 changed files with 113 additions and 67 deletions

View File

@ -49,7 +49,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP.Tests
m_regStatus = RegionStatus.Up;
}
public override void Update(int frames) {}
public override bool Update(int frames) { return true; }
public override void LoadWorldMap() {}
public override ISceneAgent AddNewAgent(IClientAPI client, PresenceType type)

View File

@ -328,6 +328,17 @@ namespace OpenSim.Region.Framework.Scenes
private Dictionary<string, string> m_extraSettings;
/// <summary>
/// If true then the next time the scene loop is activated, updates will be performed by firing of a timer
/// rather than on a single thread that sleeps.
/// </summary>
public bool UpdateOnTimer { get; set; }
/// <summary>
/// Only used if we are updating scene on a timer rather than sleeping a thread.
/// </summary>
private Timer m_sceneUpdateTimer;
/// <summary>
/// Current scene frame number
/// </summary>
@ -430,7 +441,8 @@ namespace OpenSim.Region.Framework.Scenes
/// Is the scene active?
/// </summary>
/// <remarks>
/// If false, maintenance and update loops are not being run. Updates can still be triggered manually if
/// If false, maintenance and update loops are not being run, though after setting to false update may still
/// be active for a period (and IsRunning will still be true). Updates can still be triggered manually if
/// the scene is not active.
/// </remarks>
public bool Active
@ -453,8 +465,11 @@ namespace OpenSim.Region.Framework.Scenes
}
private volatile bool m_active;
// private int m_lastUpdate;
// private bool m_firstHeartbeat = true;
/// <summary>
/// If true then updates are running. This may be true for a short period after a scene is de-activated.
/// </summary>
public bool IsRunning { get { return m_isRunning; } }
private volatile bool m_isRunning;
private Timer m_mapGenerationTimer = new Timer();
private bool m_generateMaptiles;
@ -1352,19 +1367,18 @@ namespace OpenSim.Region.Framework.Scenes
/// </param>
public void Start(bool startScripts)
{
if (IsRunning)
return;
m_isRunning = true;
m_active = true;
// m_log.DebugFormat("[SCENE]: Starting Heartbeat timer for {0}", RegionInfo.RegionName);
//m_heartbeatTimer.Enabled = true;
//m_heartbeatTimer.Interval = (int)(m_timespan * 1000);
//m_heartbeatTimer.Elapsed += new ElapsedEventHandler(Heartbeat);
if (m_heartbeatThread != null)
{
m_heartbeatThread.Abort();
m_heartbeatThread = null;
}
// m_lastUpdate = Util.EnvironmentTickCount();
m_heartbeatThread
= Watchdog.StartThread(
@ -1401,15 +1415,6 @@ namespace OpenSim.Region.Framework.Scenes
/// </summary>
private void Heartbeat()
{
// if (!Monitor.TryEnter(m_heartbeatLock))
// {
// Watchdog.RemoveThread();
// return;
// }
// try
// {
m_eventManager.TriggerOnRegionStarted(this);
// The first frame can take a very long time due to physics actors being added on startup. Therefore,
@ -1421,18 +1426,34 @@ namespace OpenSim.Region.Framework.Scenes
Maintenance, string.Format("Maintenance ({0})", RegionInfo.RegionName), ThreadPriority.Normal, false, true);
Watchdog.GetCurrentThreadInfo().AlarmIfTimeout = true;
if (UpdateOnTimer)
{
m_sceneUpdateTimer = new Timer(MinFrameTime * 1000);
m_sceneUpdateTimer.AutoReset = true;
m_sceneUpdateTimer.Elapsed += Update;
m_sceneUpdateTimer.Start();
}
else
{
Update(-1);
// m_lastUpdate = Util.EnvironmentTickCount();
// m_firstHeartbeat = false;
// }
// finally
// {
// Monitor.Pulse(m_heartbeatLock);
// Monitor.Exit(m_heartbeatLock);
// }
Watchdog.RemoveThread();
m_isRunning = false;
}
}
private void Update(object sender, ElapsedEventArgs e)
{
// If the last frame did not complete on time, then immediately start the next update on the same thread
// and ignore further timed updates until we have a frame that had spare time.
while (!Update(1) && Active) {}
if (!Active || m_shuttingDown)
{
m_sceneUpdateTimer.Stop();
m_sceneUpdateTimer = null;
m_isRunning = false;
}
}
private void Maintenance()
@ -1502,7 +1523,7 @@ namespace OpenSim.Region.Framework.Scenes
}
}
public override void Update(int frames)
public override bool Update(int frames)
{
long? endFrame = null;
@ -1652,6 +1673,7 @@ namespace OpenSim.Region.Framework.Scenes
EventManager.TriggerRegionHeartbeatEnd(this);
if (!UpdateOnTimer)
Watchdog.UpdateThread();
previousFrameTick = m_lastFrameTick;
@ -1661,8 +1683,10 @@ namespace OpenSim.Region.Framework.Scenes
if (tmpMS > 0)
{
spareMS = tmpMS;
if (!UpdateOnTimer)
Thread.Sleep(tmpMS);
spareMS += tmpMS;
}
frameMS = Util.EnvironmentTickCountSubtract(maintc);
@ -1693,6 +1717,8 @@ namespace OpenSim.Region.Framework.Scenes
MinFrameTime * 1000,
RegionInfo.RegionName);
}
return spareMS >= 0;
}
public void AddGroupTarget(SceneObjectGroup grp)

View File

@ -196,7 +196,8 @@ namespace OpenSim.Region.Framework.Scenes
/// Number of frames to update. Exits on shutdown even if there are frames remaining.
/// If -1 then updates until shutdown.
/// </param>
public abstract void Update(int frames);
/// <returns>true if update completed within minimum frame time, false otherwise.</returns>
public abstract bool Update(int frames);
#endregion

View File

@ -30,6 +30,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using log4net;
using Mono.Addins;
using Nini.Config;
@ -107,12 +108,13 @@ namespace OpenSim.Region.OptionalModules.Avatar.Attachments
+ "physics - if false then all physics objects are non-physical.\n"
+ "scripting - if false then no scripting operations happen.\n"
+ "teleport - if true then some extra teleport debug information is logged.\n"
+ "update-on-timer - If true then the scene is updated via a timer. If false then a thread with sleep is used.\n"
+ "updates - if true then any frame which exceeds double the maximum desired frame time is logged.",
HandleDebugSceneGetCommand);
scene.AddCommand(
"Debug", this, "debug scene set",
"debug scene set active|collisions|pbackup|physics|scripting|teleport|updates true|false",
"debug scene set <param> <value>",
"Turn on scene debugging options.",
"active - if false then main scene update and maintenance loops are suspended.\n"
+ "animations - if true then extra animations debug information is logged.\n"
@ -128,6 +130,7 @@ namespace OpenSim.Region.OptionalModules.Avatar.Attachments
+ "physics - if false then all physics objects are non-physical.\n"
+ "scripting - if false then no scripting operations happen.\n"
+ "teleport - if true then some extra teleport debug information is logged.\n"
+ "update-on-timer - If true then the scene is updated via a timer. If false then a thread with sleep is used.\n"
+ "updates - if true then any frame which exceeds double the maximum desired frame time is logged.",
HandleDebugSceneSetCommand);
}
@ -163,6 +166,7 @@ namespace OpenSim.Region.OptionalModules.Avatar.Attachments
cdl.AddRow("physics", m_scene.PhysicsEnabled);
cdl.AddRow("scripting", m_scene.ScriptsEnabled);
cdl.AddRow("teleport", m_scene.DebugTeleporting);
cdl.AddRow("update-on-timer", m_scene.UpdateOnTimer);
cdl.AddRow("updates", m_scene.DebugUpdates);
MainConsole.Instance.OutputFormat("Scene {0} options:", m_scene.Name);
@ -304,6 +308,21 @@ namespace OpenSim.Region.OptionalModules.Avatar.Attachments
m_scene.DebugTeleporting = enableTeleportDebugging;
}
if (options.ContainsKey("update-on-timer"))
{
bool enableUpdateOnTimer;
if (bool.TryParse(options["update-on-timer"], out enableUpdateOnTimer))
{
m_scene.UpdateOnTimer = enableUpdateOnTimer;
m_scene.Active = false;
while (m_scene.IsRunning)
Thread.Sleep(20);
m_scene.Active = true;
}
}
if (options.ContainsKey("updates"))
{
bool enableUpdateDebugging;