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 ++++++++++++++------------------ 1 file changed, 37 insertions(+), 45 deletions(-) (limited to 'OpenSim/Region/Framework/Scenes/Scene.cs') 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 /// -- cgit v1.1