From a568f06b7faea807149205d0e47454e4883e4836 Mon Sep 17 00:00:00 2001
From: Oren Hurvitz
Date: Thu, 6 Aug 2015 09:54:20 +0300
Subject: When scripts are sleeping, don't count that as execution time
Sleeping doesn't use the CPU.
---
.../Shared/Api/Implementation/LSL_Api.cs | 28 ++++++++++++++++++----
.../Shared/Api/Implementation/LS_Api.cs | 4 +++-
.../Shared/Api/Implementation/MOD_Api.cs | 4 +++-
.../Shared/Api/Implementation/OSSL_Api.cs | 24 +++++++++++++++++--
4 files changed, 51 insertions(+), 9 deletions(-)
(limited to 'OpenSim/Region/ScriptEngine/Shared/Api')
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
index fa762a6..7521ea6 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
@@ -104,6 +104,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
WaitHandle m_coopSleepHandle;
///
+ /// The timer used by the ScriptInstance to measure how long the script has executed.
+ ///
+ private Stopwatch m_executionTimer;
+
+ ///
/// The item that hosts this script
///
protected TaskInventoryItem m_item;
@@ -262,12 +267,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
};
public void Initialize(
- IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle)
+ IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle,
+ Stopwatch executionTimer)
{
m_ScriptEngine = scriptEngine;
m_host = host;
m_item = item;
m_coopSleepHandle = coopSleepHandle;
+ m_executionTimer = executionTimer;
LoadConfig();
@@ -406,10 +413,21 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
protected virtual void Sleep(int delay)
{
- if (m_coopSleepHandle == null)
- System.Threading.Thread.Sleep(delay);
- else
- CheckForCoopTermination(delay);
+ if (m_executionTimer != null)
+ m_executionTimer.Stop(); // sleep time doesn't count as execution time, since it doesn't use the CPU
+
+ try
+ {
+ if (m_coopSleepHandle == null)
+ System.Threading.Thread.Sleep(delay);
+ else
+ CheckForCoopTermination(delay);
+ }
+ finally
+ {
+ if (m_executionTimer != null)
+ m_executionTimer.Start();
+ }
}
///
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LS_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LS_Api.cs
index 3406aea..9bb12d3 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LS_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LS_Api.cs
@@ -51,6 +51,7 @@ using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
+using System.Diagnostics;
namespace OpenSim.Region.ScriptEngine.Shared.Api
{
@@ -63,7 +64,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
internal IScriptModuleComms m_comms = null;
public void Initialize(
- IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle)
+ IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle,
+ Stopwatch executionTimer)
{
m_ScriptEngine = scriptEngine;
m_host = host;
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/MOD_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/MOD_Api.cs
index 92dd813..39505e1 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/MOD_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/MOD_Api.cs
@@ -51,6 +51,7 @@ using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
+using System.Diagnostics;
namespace OpenSim.Region.ScriptEngine.Shared.Api
{
@@ -66,7 +67,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
internal IScriptModuleComms m_comms = null;
public void Initialize(
- IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle)
+ IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle,
+ Stopwatch executionTimer)
{
m_ScriptEngine = scriptEngine;
m_host = host;
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
index 123f8ca..4bb0b74 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
@@ -41,6 +41,7 @@ using OpenMetaverse.StructuredData;
using Nini.Config;
using OpenSim;
using OpenSim.Framework;
+using System.Diagnostics;
using OpenSim.Framework.Console;
using OpenSim.Region.Framework.Interfaces;
@@ -142,14 +143,21 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
internal float m_ScriptDistanceFactor = 1.0f;
internal Dictionary m_FunctionPerms = new Dictionary();
+ ///
+ /// The timer used by the ScriptInstance to measure how long the script has executed.
+ ///
+ private Stopwatch m_executionTimer;
+
protected IUrlModule m_UrlModule = null;
public void Initialize(
- IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle)
+ IScriptEngine scriptEngine, SceneObjectPart host, TaskInventoryItem item, WaitHandle coopSleepHandle,
+ Stopwatch executionTimer)
{
m_ScriptEngine = scriptEngine;
m_host = host;
m_item = item;
+ m_executionTimer = executionTimer;
m_UrlModule = m_ScriptEngine.World.RequestModuleInterface();
@@ -432,7 +440,19 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
delay = (int)((float)delay * m_ScriptDelayFactor);
if (delay == 0)
return;
- System.Threading.Thread.Sleep(delay);
+
+ if (m_executionTimer != null)
+ m_executionTimer.Stop(); // sleep time doesn't count as execution time, since it doesn't use the CPU
+
+ try
+ {
+ System.Threading.Thread.Sleep(delay);
+ }
+ finally
+ {
+ if (m_executionTimer != null)
+ m_executionTimer.Start();
+ }
}
public LSL_Integer osSetTerrainHeight(int x, int y, double val)
--
cgit v1.1