Replace script-lines-per-second with the script execution time scaled by its measurement period and an idealised frame time.

The previous lines-per-second measurement used for top scripts report was inaccurate, since lines executed does not reflect time taken to execute.
Also, every fetch of the report would reset all the numbers limiting its usefulness and we weren't even guaranteed to see the top 100.
The actual measurement value should be script execution time per frame but XEngine does not work this way.
Therefore, we use actual script execution time scaled by the measurement period and an idealised frame time.
This is still not ideal but gives reasonable results and allows scripts to be compared.
This commit moves script execution time calculations from SceneGraph into IScriptModule implementations.
0.7.4.1
Justin Clark-Casey (justincc) 2012-03-16 00:34:30 +00:00
parent 0548eeb571
commit a4b01ef38a
7 changed files with 145 additions and 70 deletions

View File

@ -26,8 +26,10 @@
*/ */
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security; using System.Security;
using log4net; using log4net;
@ -876,52 +878,67 @@ namespace OpenSim.Region.CoreModules.World.Estate
if (!Scene.Permissions.CanIssueEstateCommand(remoteClient.AgentId, false)) if (!Scene.Permissions.CanIssueEstateCommand(remoteClient.AgentId, false))
return; return;
Dictionary<uint, float> SceneData = new Dictionary<uint,float>(); Dictionary<uint, float> sceneData = null;
List<UUID> uuidNameLookupList = new List<UUID>(); List<UUID> uuidNameLookupList = new List<UUID>();
if (reportType == 1) if (reportType == 1)
{ {
SceneData = Scene.PhysicsScene.GetTopColliders(); sceneData = Scene.PhysicsScene.GetTopColliders();
} }
else if (reportType == 0) else if (reportType == 0)
{ {
SceneData = Scene.SceneGraph.GetTopScripts(); IScriptModule scriptModule = Scene.RequestModuleInterface<IScriptModule>();
if (scriptModule != null)
sceneData = scriptModule.GetObjectScriptsExecutionTimes();
} }
List<LandStatReportItem> SceneReport = new List<LandStatReportItem>(); List<LandStatReportItem> SceneReport = new List<LandStatReportItem>();
lock (SceneData) if (sceneData != null)
{ {
foreach (uint obj in SceneData.Keys) var sortedSceneData
= sceneData.Select(
item => new { Measurement = item.Value, Part = Scene.GetSceneObjectPart(item.Key) });
sortedSceneData.OrderBy(item => item.Measurement);
int items = 0;
foreach (var entry in sortedSceneData)
{ {
SceneObjectPart prt = Scene.GetSceneObjectPart(obj); if (entry.Part == null)
if (prt != null) continue;
items++;
SceneObjectGroup so = entry.Part.ParentGroup;
LandStatReportItem lsri = new LandStatReportItem();
lsri.LocationX = so.AbsolutePosition.X;
lsri.LocationY = so.AbsolutePosition.Y;
lsri.LocationZ = so.AbsolutePosition.Z;
lsri.Score = entry.Measurement;
lsri.TaskID = so.UUID;
lsri.TaskLocalID = so.LocalId;
lsri.TaskName = entry.Part.Name;
lsri.OwnerName = "waiting";
lock (uuidNameLookupList)
uuidNameLookupList.Add(so.OwnerID);
if (filter.Length != 0)
{ {
SceneObjectGroup sog = prt.ParentGroup; if ((lsri.OwnerName.Contains(filter) || lsri.TaskName.Contains(filter)))
LandStatReportItem lsri = new LandStatReportItem();
lsri.LocationX = sog.AbsolutePosition.X;
lsri.LocationY = sog.AbsolutePosition.Y;
lsri.LocationZ = sog.AbsolutePosition.Z;
lsri.Score = SceneData[obj];
lsri.TaskID = sog.UUID;
lsri.TaskLocalID = sog.LocalId;
lsri.TaskName = sog.GetPartName(obj);
lsri.OwnerName = "waiting";
lock (uuidNameLookupList)
uuidNameLookupList.Add(sog.OwnerID);
if (filter.Length != 0)
{ {
if ((lsri.OwnerName.Contains(filter) || lsri.TaskName.Contains(filter)))
{
}
else
{
continue;
}
} }
else
SceneReport.Add(lsri); {
continue;
}
} }
SceneReport.Add(lsri);
if (items >= 100)
break;
} }
} }

View File

@ -27,6 +27,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using OpenMetaverse; using OpenMetaverse;
namespace OpenSim.Region.Framework.Interfaces namespace OpenSim.Region.Framework.Interfaces
@ -74,5 +75,11 @@ namespace OpenSim.Region.Framework.Interfaces
/// Starts the processing threads. /// Starts the processing threads.
/// </summary> /// </summary>
void StartProcessing(); void StartProcessing();
/// <summary>
/// Get the execution times of all scripts in each object.
/// </summary>
/// <returns>A dictionary where the key is a local object ID and the value is an execution time in milliseconds.</returns>
Dictionary<uint, float> GetObjectScriptsExecutionTimes();
} }
} }

View File

@ -733,6 +733,7 @@ namespace OpenSim.Region.Framework.Scenes
#endregion #endregion
#region Get Methods #region Get Methods
/// <summary> /// <summary>
/// Get the controlling client for the given avatar, if there is one. /// Get the controlling client for the given avatar, if there is one.
/// ///
@ -1074,36 +1075,6 @@ namespace OpenSim.Region.Framework.Scenes
return Entities.GetEntities(); return Entities.GetEntities();
} }
public Dictionary<uint, float> GetTopScripts()
{
Dictionary<uint, float> topScripts = new Dictionary<uint, float>();
EntityBase[] EntityList = GetEntities();
int limit = 0;
foreach (EntityBase ent in EntityList)
{
if (ent is SceneObjectGroup)
{
SceneObjectGroup grp = (SceneObjectGroup)ent;
if ((grp.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Scripted) != 0)
{
if (grp.scriptScore >= 0.01)
{
topScripts.Add(grp.LocalId, grp.scriptScore);
limit++;
if (limit >= 100)
{
break;
}
}
grp.scriptScore = 0;
}
}
}
return topScripts;
}
#endregion #endregion
#region Other Methods #region Other Methods

View File

@ -229,8 +229,6 @@ namespace OpenSim.Region.Framework.Scenes
get { return RootPart.VolumeDetectActive; } get { return RootPart.VolumeDetectActive; }
} }
public float scriptScore;
private Vector3 lastPhysGroupPos; private Vector3 lastPhysGroupPos;
private Quaternion lastPhysGroupRot; private Quaternion lastPhysGroupRot;
@ -1184,12 +1182,7 @@ namespace OpenSim.Region.Framework.Scenes
public void AddScriptLPS(int count) public void AddScriptLPS(int count)
{ {
if (scriptScore + count >= float.MaxValue - count) m_scene.SceneGraph.AddToScriptLPS(count);
scriptScore = 0;
scriptScore += (float)count;
SceneGraph d = m_scene.SceneGraph;
d.AddToScriptLPS(count);
} }
public void AddActiveScriptCount(int count) public void AddActiveScriptCount(int count)

View File

@ -78,6 +78,21 @@ namespace OpenSim.Region.ScriptEngine.Interfaces
/// </summary> /// </summary>
string State { get; set; } string State { get; set; }
/// <summary>
/// Time the script was last started
/// </summary>
DateTime TimeStarted { get; }
/// <summary>
/// Tick the last measurement period was started.
/// </summary>
long MeasurementPeriodTickStart { get; }
/// <summary>
/// Ticks spent executing in the last measurement period.
/// </summary>
long MeasurementPeriodExecutionTime { get; }
IScriptEngine Engine { get; } IScriptEngine Engine { get; }
UUID AppDomain { get; set; } UUID AppDomain { get; set; }
string PrimName { get; } string PrimName { get; }

View File

@ -172,6 +172,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
public TaskInventoryItem ScriptTask { get; private set; } public TaskInventoryItem ScriptTask { get; private set; }
public DateTime TimeStarted { get; private set; }
public long MeasurementPeriodTickStart { get; private set; }
public long MeasurementPeriodExecutionTime { get; private set; }
public static readonly long MaxMeasurementPeriod = 30 * TimeSpan.TicksPerMinute;
public void ClearQueue() public void ClearQueue()
{ {
m_TimerQueued = false; m_TimerQueued = false;
@ -458,6 +466,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
Running = true; Running = true;
TimeStarted = DateTime.Now;
MeasurementPeriodTickStart = Util.EnvironmentTickCount();
MeasurementPeriodExecutionTime = 0;
if (EventQueue.Count > 0) if (EventQueue.Count > 0)
{ {
if (m_CurrentWorkItem == null) if (m_CurrentWorkItem == null)
@ -710,8 +722,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
m_EventStart = DateTime.Now; m_EventStart = DateTime.Now;
m_InEvent = true; m_InEvent = true;
int start = Util.EnvironmentTickCount();
// Reset the measurement period when we reach the end of the current one.
if (start - MeasurementPeriodTickStart > MaxMeasurementPeriod)
MeasurementPeriodTickStart = start;
m_Script.ExecuteEvent(State, data.EventName, data.Params); m_Script.ExecuteEvent(State, data.EventName, data.Params);
MeasurementPeriodExecutionTime += Util.EnvironmentTickCount() - start;
m_InEvent = false; m_InEvent = false;
m_CurrentEvent = String.Empty; m_CurrentEvent = String.Empty;
@ -720,7 +740,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
// This will be the very first event we deliver // This will be the very first event we deliver
// (state_entry) in default state // (state_entry) in default state
// //
SaveState(m_Assembly); SaveState(m_Assembly);
m_SaveState = false; m_SaveState = false;

View File

@ -1891,6 +1891,59 @@ namespace OpenSim.Region.ScriptEngine.XEngine
} }
} }
public Dictionary<uint, float> GetObjectScriptsExecutionTimes()
{
long tickNow = Util.EnvironmentTickCount();
Dictionary<uint, float> topScripts = new Dictionary<uint, float>();
lock (m_Scripts)
{
foreach (IScriptInstance si in m_Scripts.Values)
{
if (!topScripts.ContainsKey(si.LocalID))
topScripts[si.LocalID] = 0;
// long ticksElapsed = tickNow - si.MeasurementPeriodTickStart;
// float framesElapsed = ticksElapsed / (18.1818 * TimeSpan.TicksPerMillisecond);
// Execution time of the script adjusted by it's measurement period to make scripts started at
// different times comparable.
// float adjustedExecutionTime
// = (float)si.MeasurementPeriodExecutionTime
// / ((float)(tickNow - si.MeasurementPeriodTickStart) / ScriptInstance.MaxMeasurementPeriod)
// / TimeSpan.TicksPerMillisecond;
long ticksElapsed = tickNow - si.MeasurementPeriodTickStart;
// Avoid divide by zerp
if (ticksElapsed == 0)
ticksElapsed = 1;
// Scale execution time to the ideal 55 fps frame time for these reasons.
//
// 1) XEngine does not execute scripts per frame, unlike other script engines. Hence, there is no
// 'script execution time per frame', which is the original purpose of this value.
//
// 2) Giving the raw execution times is misleading since scripts start at different times, making
// it impossible to compare scripts.
//
// 3) Scaling the raw execution time to the time that the script has been running is better but
// is still misleading since a script that has just been rezzed may appear to have been running
// for much longer.
//
// 4) Hence, we scale execution time to an idealised frame time (55 fps). This is also not perfect
// since the figure does not represent actual execution time and very hard running scripts will
// never exceed 18ms (though this is a very high number for script execution so is a warning sign).
float adjustedExecutionTime
= ((float)si.MeasurementPeriodExecutionTime / ticksElapsed) * 18.1818f;
topScripts[si.LocalID] += adjustedExecutionTime;
}
}
return topScripts;
}
public void SuspendScript(UUID itemID) public void SuspendScript(UUID itemID)
{ {
// m_log.DebugFormat("[XEngine]: Received request to suspend script with ID {0}", itemID); // m_log.DebugFormat("[XEngine]: Received request to suspend script with ID {0}", itemID);