Optimized heartbeat by calling Update() only on updated objects.

During the heartbeat loop, Update() is called on every SceneObjectGroup which in turn checks if any SceneObjectPart has changed. For large regions (> 100k prims) this work consumes 20-30% of a CPU even though there are only a few objects updating each frame.

There is only one other reason to check every object on every frame, and that is the case where a script has registered the object with an "at target" listener. We can easily track when an object is registered or unregistered with an AtTarget, so this is not a reason to check every object every heartbeat.

In the attached patch, I have added a dictionary to the scene which tracks the objects which have At Targets. Each heartbeat, the AtTarget() function will be called on every object registered with a listener for that event. Also, I added a dictionary to SceneGraph which stores references to objects which have been queued for updates during the heartbeat. At each heartbeat, Update() is called only on the objects which have generated updates during that beat.
prioritization
Dan Lake 2009-10-13 19:13:06 -07:00 committed by John Hurliman
parent e8c1e69a0d
commit 5976ac16b0
3 changed files with 52 additions and 73 deletions

View File

@ -117,6 +117,8 @@ namespace OpenSim.Region.Framework.Scenes
private volatile bool m_backingup = false;
private Dictionary<UUID, ReturnInfo> m_returns = new Dictionary<UUID, ReturnInfo>();
private Dictionary<UUID, SceneObjectGroup> m_groupsWithTargets = new Dictionary<UUID, SceneObjectGroup>();
protected string m_simulatorVersion = "OpenSimulator Server";
@ -246,8 +248,7 @@ namespace OpenSim.Region.Framework.Scenes
private int m_update_physics = 1;
private int m_update_entitymovement = 1;
private int m_update_entities = 1; // Run through all objects checking for updates
private int m_update_entitiesquick = 200; // Run through objects that have scheduled updates checking for updates
private int m_update_objects = 1; // Update objects which have scheduled themselves for updates
private int m_update_presences = 1; // Update scene presence movements
private int m_update_events = 1;
private int m_update_backup = 200;
@ -979,28 +980,7 @@ namespace OpenSim.Region.Framework.Scenes
maintc = Environment.TickCount;
TimeSpan SinceLastFrame = DateTime.Now - m_lastupdate;
// Aquire a lock so only one update call happens at once
//updateLock.WaitOne();
float physicsFPS = 0;
//m_log.Info("sadfadf" + m_neighbours.Count.ToString());
int agentsInScene = m_sceneGraph.GetRootAgentCount() + m_sceneGraph.GetChildAgentCount();
if (agentsInScene > 21)
{
if (m_update_entities == 1)
{
m_update_entities = 5;
StatsReporter.SetUpdateMS(6000);
}
}
else
{
if (m_update_entities == 5)
{
m_update_entities = 1;
StatsReporter.SetUpdateMS(3000);
}
}
frameMS = Environment.TickCount;
try
@ -1013,30 +993,17 @@ namespace OpenSim.Region.Framework.Scenes
m_frame = 0;
otherMS = Environment.TickCount;
// run through all entities looking for updates (slow)
if (m_frame % m_update_entities == 0)
{
/* // Adam Experimental
if (m_updateEntitiesThread == null)
{
m_updateEntitiesThread = new Thread(m_sceneGraph.UpdateEntities);
ThreadTracker.Add(m_updateEntitiesThread);
}
// Check if any objects have reached their targets
CheckAtTargets();
// Update SceneObjectGroups that have scheduled themselves for updates
// Objects queue their updates onto all scene presences
if (m_frame % m_update_objects == 0)
m_sceneGraph.UpdateObjectGroups();
if (m_updateEntitiesThread.ThreadState == ThreadState.Stopped)
m_updateEntitiesThread.Start();
*/
m_sceneGraph.UpdateEntities();
}
// run through entities that have scheduled themselves for
// updates looking for updates(faster)
if (m_frame % m_update_entitiesquick == 0)
m_sceneGraph.ProcessUpdates();
// Run through scenepresences looking for updates
// Run through all ScenePresences looking for updates
// Presence updates and queued object updates for each presence are sent to clients
if (m_frame % m_update_presences == 0)
m_sceneGraph.UpdatePresences();
@ -1140,6 +1107,31 @@ namespace OpenSim.Region.Framework.Scenes
}
}
public void AddGroupTarget(SceneObjectGroup grp)
{
lock(m_groupsWithTargets)
m_groupsWithTargets[grp.UUID] = grp;
}
public void RemoveGroupTarget(SceneObjectGroup grp)
{
lock(m_groupsWithTargets)
m_groupsWithTargets.Remove(grp.UUID);
}
private void CheckAtTargets()
{
lock (m_groupsWithTargets)
{
foreach (KeyValuePair<UUID, SceneObjectGroup> kvp in m_groupsWithTargets)
{
kvp.Value.checkAtTargets();
}
}
}
/// <summary>
/// Send out simstats data to all clients
/// </summary>

View File

@ -77,7 +77,7 @@ namespace OpenSim.Region.Framework.Scenes
protected RegionInfo m_regInfo;
protected Scene m_parentScene;
protected Dictionary<UUID, EntityBase> m_updateList = new Dictionary<UUID, EntityBase>();
protected Dictionary<UUID, SceneObjectGroup> m_updateList = new Dictionary<UUID, SceneObjectGroup>();
protected int m_numRootAgents = 0;
protected int m_numPrim = 0;
protected int m_numChildAgents = 0;
@ -155,16 +155,6 @@ namespace OpenSim.Region.Framework.Scenes
}
}
protected internal void UpdateEntities()
{
List<EntityBase> updateEntities = GetEntities();
foreach (EntityBase entity in updateEntities)
{
entity.Update();
}
}
protected internal void UpdatePresences()
{
List<ScenePresence> updateScenePresences = GetScenePresences();
@ -365,12 +355,12 @@ namespace OpenSim.Region.Framework.Scenes
}
/// <summary>
/// Add an entity to the list of prims to process on the next update
/// Add an object to the list of prims to process on the next update
/// </summary>
/// <param name="obj">
/// A <see cref="EntityBase"/>
/// A <see cref="SceneObjectGroup"/>
/// </param>
protected internal void AddToUpdateList(EntityBase obj)
protected internal void AddToUpdateList(SceneObjectGroup obj)
{
lock (m_updateList)
{
@ -381,18 +371,18 @@ namespace OpenSim.Region.Framework.Scenes
/// <summary>
/// Process all pending updates
/// </summary>
protected internal void ProcessUpdates()
protected internal void UpdateObjectGroups()
{
Dictionary<UUID, EntityBase> updates;
Dictionary<UUID, SceneObjectGroup> updates;
// Some updates add more updates to the updateList.
// Get the current list of updates and clear the list before iterating
lock (m_updateList)
{
updates = new Dictionary<UUID, EntityBase>(m_updateList);
updates = new Dictionary<UUID, SceneObjectGroup>(m_updateList);
m_updateList.Clear();
}
// Go through all timers
foreach (KeyValuePair<UUID, EntityBase> kvp in updates)
// Go through all updates
foreach (KeyValuePair<UUID, SceneObjectGroup> kvp in updates)
{
// Don't abort the whole update if one entity happens to give us an exception.
try

View File

@ -1234,6 +1234,7 @@ namespace OpenSim.Region.Framework.Scenes
{
lock (m_targets)
m_targets.Clear();
m_scene.RemoveGroupTarget(this);
}
ScheduleGroupForFullUpdate();
@ -1864,12 +1865,6 @@ namespace OpenSim.Region.Framework.Scenes
m_rootPart.UpdateFlag = 1;
lastPhysGroupPos = AbsolutePosition;
}
//foreach (SceneObjectPart part in m_parts.Values)
//{
//if (part.UpdateFlag == 0) part.UpdateFlag = 1;
//}
checkAtTargets();
if (UsePhysics && ((Math.Abs(lastPhysGroupRot.W - GroupRotation.W) > 0.1)
|| (Math.Abs(lastPhysGroupRot.X - GroupRotation.X) > 0.1)
@ -3114,6 +3109,7 @@ namespace OpenSim.Region.Framework.Scenes
{
m_targets.Add(handle, waypoint);
}
m_scene.AddGroupTarget(this);
return (int)handle;
}
@ -3121,12 +3117,13 @@ namespace OpenSim.Region.Framework.Scenes
{
lock (m_targets)
{
if (m_targets.ContainsKey((uint)handle))
m_targets.Remove((uint)handle);
m_targets.Remove((uint)handle);
if (m_targets.Count == 0)
m_scene.RemoveGroupTarget(this);
}
}
private void checkAtTargets()
public void checkAtTargets()
{
if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
{