From 824283ca3c2ab54868ed61fdb0a329221d69e5fa Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Fri, 26 Sep 2008 13:16:11 +0000 Subject: Remove all the subclassing complexity and script server interfaces from DNE and move all of DNE into the DotNetEngine directory. Remove references that would cause the script runtime to load the entire engine + scene into each script appdomain. This might help DNE memory consumption. --- .../ScriptEngine/DotNetEngine/ScriptManager.cs | 460 ++++++++++++++++++++- 1 file changed, 452 insertions(+), 8 deletions(-) (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs') diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs index 8ff3bfd..12a8fe4 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs @@ -34,22 +34,66 @@ using OpenSim.Region.Environment.Scenes; using OpenSim.Region.ScriptEngine.Common; using OpenSim.Region.ScriptEngine.Shared; using OpenSim.Region.ScriptEngine.Shared.Api; -using OpenSim.Region.ScriptEngine.Common.ScriptEngineBase; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; +using System.Threading; namespace OpenSim.Region.ScriptEngine.DotNetEngine { - public class ScriptManager : Common.ScriptEngineBase.ScriptManager + public class ScriptManager { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - public ScriptManager(Common.ScriptEngineBase.ScriptEngine scriptEngine) - : base(scriptEngine) + #region Declares + + private Thread scriptLoadUnloadThread; + private static Thread staticScriptLoadUnloadThread; + // private int scriptLoadUnloadThread_IdleSleepms; + private Queue LUQueue = new Queue(); + private static bool PrivateThread; + private int LoadUnloadMaxQueueSize; + private Object scriptLock = new Object(); + private bool m_started = false; + private Dictionary detparms = new Dictionary(); + + // Load/Unload structure + private struct LUStruct + { + public uint localID; + public UUID itemID; + public string script; + public LUType Action; + public int startParam; + public bool postOnRez; + } + + private enum LUType + { + Unknown = 0, + Load = 1, + Unload = 2 + } + + // Xantor 20080525: Keep a list of compiled scripts this session for reuse + public Dictionary scriptList = new Dictionary(); + + // Object> + // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory. + // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead! + public Dictionary> Scripts = + new Dictionary>(); + + + public Scene World { - base.m_scriptEngine = scriptEngine; + get { return m_scriptEngine.World; } } + + #endregion private Compiler.LSL.Compiler LSLCompiler; - public override void Initialize() + public void Initialize() { // Create our compiler LSLCompiler = new Compiler.LSL.Compiler(m_scriptEngine); @@ -62,7 +106,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine // PROVIDE SCRIPT WITH ITS INTERFACE TO OpenSim - public override void _StartScript(uint localID, UUID itemID, string Script, int startParam, bool postOnRez) + public void _StartScript(uint localID, UUID itemID, string Script, int startParam, bool postOnRez) { m_log.DebugFormat( "[{0}]: ScriptManager StartScript: localID: {1}, itemID: {2}", @@ -173,7 +217,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine } } - public override void _StopScript(uint localID, UUID itemID) + public void _StopScript(uint localID, UUID itemID) { IScript LSLBC = GetScript(localID, itemID); if (LSLBC == null) @@ -202,5 +246,405 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine ": " + e.ToString()); } } + + public void ReadConfig() + { + // scriptLoadUnloadThread_IdleSleepms = m_scriptEngine.ScriptConfigSource.GetInt("ScriptLoadUnloadLoopms", 30); + // TODO: Requires sharing of all ScriptManagers to single thread + PrivateThread = true; // m_scriptEngine.ScriptConfigSource.GetBoolean("PrivateScriptLoadUnloadThread", false); + LoadUnloadMaxQueueSize = m_scriptEngine.ScriptConfigSource.GetInt("LoadUnloadMaxQueueSize", 100); + } + + #region Object init/shutdown + + public ScriptEngine m_scriptEngine; + + public ScriptManager(ScriptEngine scriptEngine) + { + m_scriptEngine = scriptEngine; + } + public void Setup() + { + ReadConfig(); + Initialize(); + } + public void Start() + { + m_started = true; + + + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); + + // + // CREATE THREAD + // Private or shared + // + if (PrivateThread) + { + // Assign one thread per region + //scriptLoadUnloadThread = StartScriptLoadUnloadThread(); + } + else + { + // Shared thread - make sure one exist, then assign it to the private + if (staticScriptLoadUnloadThread == null) + { + //staticScriptLoadUnloadThread = StartScriptLoadUnloadThread(); + } + scriptLoadUnloadThread = staticScriptLoadUnloadThread; + } + } + +// TODO: unused +// private static int privateThreadCount = 0; +// private Thread StartScriptLoadUnloadThread() +// { +// Thread t = new Thread(ScriptLoadUnloadThreadLoop); +// string name = "ScriptLoadUnloadThread:"; +// if (PrivateThread) +// { +// name += "Private:" + privateThreadCount; +// privateThreadCount++; +// } +// else +// { +// name += "Shared"; +// } +// t.Name = name; +// t.IsBackground = true; +// t.Priority = ThreadPriority.Normal; +// t.Start(); +// OpenSim.Framework.ThreadTracker.Add(t); +// return t; +// } + + ~ScriptManager() + { + // Abort load/unload thread + try + { + //PleaseShutdown = true; + //Thread.Sleep(100); + if (scriptLoadUnloadThread != null && scriptLoadUnloadThread.IsAlive == true) + { + scriptLoadUnloadThread.Abort(); + //scriptLoadUnloadThread.Join(); + } + } + catch + { + } + } + + #endregion + + #region Load / Unload scripts (Thread loop) + +// TODO: unused +// private void ScriptLoadUnloadThreadLoop() +// { +// try +// { +// while (true) +// { +// if (LUQueue.Count == 0) +// Thread.Sleep(scriptLoadUnloadThread_IdleSleepms); +// //if (PleaseShutdown) +// // return; +// DoScriptLoadUnload(); +// } +// } +// catch (ThreadAbortException tae) +// { +// string a = tae.ToString(); +// a = String.Empty; +// // Expected +// } +// } + + public void DoScriptLoadUnload() + { + if (!m_started) + return; + + lock (LUQueue) + { + if (LUQueue.Count > 0) + { +m_scriptEngine.Log.InfoFormat("[{0}]: Loading script", m_scriptEngine.ScriptEngineName); + LUStruct item = LUQueue.Dequeue(); + + if (item.Action == LUType.Unload) + { + _StopScript(item.localID, item.itemID); + RemoveScript(item.localID, item.itemID); + } + else if (item.Action == LUType.Load) + { + _StartScript(item.localID, item.itemID, item.script, item.startParam, item.postOnRez); + } + } + } + } + + #endregion + + #region Helper functions + + private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + //Console.WriteLine("ScriptManager.CurrentDomain_AssemblyResolve: " + args.Name); + return Assembly.GetExecutingAssembly().FullName == args.Name ? Assembly.GetExecutingAssembly() : null; + } + + #endregion + + + + #region Start/Stop/Reset script + + // private readonly Object startStopLock = new Object(); + + /// + /// Fetches, loads and hooks up a script to an objects events + /// + /// + /// + public void StartScript(uint localID, UUID itemID, string Script, int startParam, bool postOnRez) + { + lock (LUQueue) + { + if ((LUQueue.Count >= LoadUnloadMaxQueueSize) && m_started) + { + m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: ERROR: Load/unload queue item count is at " + LUQueue.Count + ". Config variable \"LoadUnloadMaxQueueSize\" is set to " + LoadUnloadMaxQueueSize + ", so ignoring new script."); + return; + } + + LUStruct ls = new LUStruct(); + ls.localID = localID; + ls.itemID = itemID; + ls.script = Script; + ls.Action = LUType.Load; + ls.startParam = startParam; + ls.postOnRez = postOnRez; + LUQueue.Enqueue(ls); +m_scriptEngine.Log.InfoFormat("[{0}]: Queued script for load", m_scriptEngine.ScriptEngineName); + } + } + + /// + /// Disables and unloads a script + /// + /// + /// + public void StopScript(uint localID, UUID itemID) + { + LUStruct ls = new LUStruct(); + ls.localID = localID; + ls.itemID = itemID; + ls.Action = LUType.Unload; + ls.startParam = 0; + ls.postOnRez = false; + lock (LUQueue) + { + LUQueue.Enqueue(ls); + } + } + + // Create a new instance of the compiler (reuse) + //private Compiler.LSL.Compiler LSLCompiler = new Compiler.LSL.Compiler(); + + + #endregion + + #region Perform event execution in script + + /// + /// Execute a LL-event-function in Script + /// + /// Object the script is located in + /// Script ID + /// Name of function + /// Arguments to pass to function + internal void ExecuteEvent(uint localID, UUID itemID, string FunctionName, DetectParams[] qParams, object[] args) + { + //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined + ///#if DEBUG + /// Console.WriteLine("ScriptEngine: Inside ExecuteEvent for event " + FunctionName); + ///#endif + // Execute a function in the script + //m_scriptEngine.Log.Info("[" + ScriptEngineName + "]: Executing Function localID: " + localID + ", itemID: " + itemID + ", FunctionName: " + FunctionName); + //ScriptBaseInterface Script = (ScriptBaseInterface)GetScript(localID, itemID); + IScript Script = GetScript(localID, itemID); + if (Script == null) + { + return; + } + //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined + ///#if DEBUG + /// Console.WriteLine("ScriptEngine: Executing event: " + FunctionName); + ///#endif + // Must be done in correct AppDomain, so leaving it up to the script itself + detparms[Script] = qParams; + Script.Exec.ExecuteEvent(FunctionName, args); + detparms.Remove(Script); + } + + public uint GetLocalID(UUID itemID) + { + foreach (KeyValuePair > k in Scripts) + { + if (k.Value.ContainsKey(itemID)) + return k.Key; + } + return 0; + } + + public int GetStateEventFlags(uint localID, UUID itemID) + { + // Console.WriteLine("GetStateEventFlags for <" + localID + "," + itemID + ">"); + try + { + IScript Script = GetScript(localID, itemID); + if (Script == null) + { + return 0; + } + ExecutorBase.scriptEvents evflags = Script.Exec.GetStateEventFlags(); + return (int)evflags; + } + catch (Exception) + { + } + + return 0; + } + + + #endregion + + #region Internal functions to keep track of script + + public List GetScriptKeys(uint localID) + { + if (Scripts.ContainsKey(localID) == false) + return new List(); + + Dictionary Obj; + Scripts.TryGetValue(localID, out Obj); + + return new List(Obj.Keys); + } + + public IScript GetScript(uint localID, UUID itemID) + { + lock (scriptLock) + { + IScript Script = null; + + if (Scripts.ContainsKey(localID) == false) + return null; + + Dictionary Obj; + Scripts.TryGetValue(localID, out Obj); + if (Obj.ContainsKey(itemID) == false) + return null; + + // Get script + Obj.TryGetValue(itemID, out Script); + return Script; + } + } + + public void SetScript(uint localID, UUID itemID, IScript Script) + { + lock (scriptLock) + { + // Create object if it doesn't exist + if (Scripts.ContainsKey(localID) == false) + { + Scripts.Add(localID, new Dictionary()); + } + + // Delete script if it exists + Dictionary Obj; + Scripts.TryGetValue(localID, out Obj); + if (Obj.ContainsKey(itemID) == true) + Obj.Remove(itemID); + + // Add to object + Obj.Add(itemID, Script); + } + } + + public void RemoveScript(uint localID, UUID itemID) + { + if (localID == 0) + localID = GetLocalID(itemID); + + // Don't have that object? + if (Scripts.ContainsKey(localID) == false) + return; + + // Delete script if it exists + Dictionary Obj; + Scripts.TryGetValue(localID, out Obj); + if (Obj.ContainsKey(itemID) == true) + Obj.Remove(itemID); + } + + #endregion + + + public void ResetScript(uint localID, UUID itemID) + { + IScript s = GetScript(localID, itemID); + string script = s.Source; + StopScript(localID, itemID); + SceneObjectPart part = World.GetSceneObjectPart(localID); + part.GetInventoryItem(itemID).PermsMask = 0; + part.GetInventoryItem(itemID).PermsGranter = UUID.Zero; + StartScript(localID, itemID, script, s.StartParam, false); + } + + + #region Script serialization/deserialization + + public void GetSerializedScript(uint localID, UUID itemID) + { + // Serialize the script and return it + // Should not be a problem + FileStream fs = File.Create("SERIALIZED_SCRIPT_" + itemID); + BinaryFormatter b = new BinaryFormatter(); + b.Serialize(fs, GetScript(localID, itemID)); + fs.Close(); + } + + public void PutSerializedScript(uint localID, UUID itemID) + { + // Deserialize the script and inject it into an AppDomain + + // How to inject into an AppDomain? + } + + #endregion + + ///// + ///// If set to true then threads and stuff should try to make a graceful exit + ///// + //public bool PleaseShutdown + //{ + // get { return _PleaseShutdown; } + // set { _PleaseShutdown = value; } + //} + //private bool _PleaseShutdown = false; + + public DetectParams[] GetDetectParams(IScript script) + { + if (detparms.ContainsKey(script)) + return detparms[script]; + + return null; + } } } -- cgit v1.1