From 5976ac16b0eedaeca2360010a3d6f7be4213700a Mon Sep 17 00:00:00 2001 From: Dan Lake Date: Tue, 13 Oct 2009 19:13:06 -0700 Subject: 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. --- OpenSim/Region/Framework/Scenes/Scene.cs | 82 ++++++++++------------ OpenSim/Region/Framework/Scenes/SceneGraph.cs | 28 +++----- .../Region/Framework/Scenes/SceneObjectGroup.cs | 15 ++-- 3 files changed, 52 insertions(+), 73 deletions(-) (limited to 'OpenSim/Region/Framework') diff --git a/OpenSim/Region/Framework/Scenes/Scene.cs b/OpenSim/Region/Framework/Scenes/Scene.cs index 7944548..1ca0267 100644 --- a/OpenSim/Region/Framework/Scenes/Scene.cs +++ b/OpenSim/Region/Framework/Scenes/Scene.cs @@ -117,6 +117,8 @@ namespace OpenSim.Region.Framework.Scenes private volatile bool m_backingup = false; private Dictionary m_returns = new Dictionary(); + + private Dictionary m_groupsWithTargets = new Dictionary(); 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); - } - - if (m_updateEntitiesThread.ThreadState == ThreadState.Stopped) - m_updateEntitiesThread.Start(); - */ - - m_sceneGraph.UpdateEntities(); - } + // 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(); - // 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 kvp in m_groupsWithTargets) + { + kvp.Value.checkAtTargets(); + } + } + } + + /// /// Send out simstats data to all clients /// diff --git a/OpenSim/Region/Framework/Scenes/SceneGraph.cs b/OpenSim/Region/Framework/Scenes/SceneGraph.cs index 54ac792..9cd2247 100644 --- a/OpenSim/Region/Framework/Scenes/SceneGraph.cs +++ b/OpenSim/Region/Framework/Scenes/SceneGraph.cs @@ -77,7 +77,7 @@ namespace OpenSim.Region.Framework.Scenes protected RegionInfo m_regInfo; protected Scene m_parentScene; - protected Dictionary m_updateList = new Dictionary(); + protected Dictionary m_updateList = new Dictionary(); 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 updateEntities = GetEntities(); - - foreach (EntityBase entity in updateEntities) - { - entity.Update(); - } - } - protected internal void UpdatePresences() { List updateScenePresences = GetScenePresences(); @@ -365,12 +355,12 @@ namespace OpenSim.Region.Framework.Scenes } /// - /// 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 /// /// - /// A + /// A /// - protected internal void AddToUpdateList(EntityBase obj) + protected internal void AddToUpdateList(SceneObjectGroup obj) { lock (m_updateList) { @@ -381,18 +371,18 @@ namespace OpenSim.Region.Framework.Scenes /// /// Process all pending updates /// - protected internal void ProcessUpdates() + protected internal void UpdateObjectGroups() { - Dictionary updates; + Dictionary 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(m_updateList); + updates = new Dictionary(m_updateList); m_updateList.Clear(); } - // Go through all timers - foreach (KeyValuePair kvp in updates) + // Go through all updates + foreach (KeyValuePair kvp in updates) { // Don't abort the whole update if one entity happens to give us an exception. try diff --git a/OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs b/OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs index 6a10618..d4cef7d 100644 --- a/OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs +++ b/OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs @@ -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) { -- cgit v1.1