1
0

AppDomainManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL;
  32. namespace OpenSim.Grid.ScriptEngine.DotNetEngine
  33. {
  34. public class AppDomainManager
  35. {
  36. private int maxScriptsPerAppDomain = 1;
  37. /// <summary>
  38. /// Internal list of all AppDomains
  39. /// </summary>
  40. private List<AppDomainStructure> appDomains = new List<AppDomainStructure>();
  41. /// <summary>
  42. /// Structure to keep track of data around AppDomain
  43. /// </summary>
  44. private class AppDomainStructure
  45. {
  46. /// <summary>
  47. /// The AppDomain itself
  48. /// </summary>
  49. public AppDomain CurrentAppDomain;
  50. /// <summary>
  51. /// Number of scripts loaded into AppDomain
  52. /// </summary>
  53. public int ScriptsLoaded;
  54. /// <summary>
  55. /// Number of dead scripts
  56. /// </summary>
  57. public int ScriptsWaitingUnload;
  58. }
  59. /// <summary>
  60. /// Current AppDomain
  61. /// </summary>
  62. private AppDomainStructure currentAD;
  63. private object getLock = new object(); // Mutex
  64. private object freeLock = new object(); // Mutex
  65. //private ScriptEngine m_scriptEngine;
  66. //public AppDomainManager(ScriptEngine scriptEngine)
  67. public AppDomainManager()
  68. {
  69. //m_scriptEngine = scriptEngine;
  70. }
  71. /// <summary>
  72. /// Find a free AppDomain, creating one if necessary
  73. /// </summary>
  74. /// <returns>Free AppDomain</returns>
  75. private AppDomainStructure GetFreeAppDomain()
  76. {
  77. Console.WriteLine("Finding free AppDomain");
  78. lock (getLock)
  79. {
  80. // Current full?
  81. if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
  82. {
  83. // Add it to AppDomains list and empty current
  84. appDomains.Add(currentAD);
  85. currentAD = null;
  86. }
  87. // No current
  88. if (currentAD == null)
  89. {
  90. // Create a new current AppDomain
  91. currentAD = new AppDomainStructure();
  92. currentAD.CurrentAppDomain = PrepareNewAppDomain();
  93. }
  94. Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded);
  95. return currentAD;
  96. }
  97. }
  98. private int AppDomainNameCount;
  99. /// <summary>
  100. /// Create and prepare a new AppDomain for scripts
  101. /// </summary>
  102. /// <returns>The new AppDomain</returns>
  103. private AppDomain PrepareNewAppDomain()
  104. {
  105. // Create and prepare a new AppDomain
  106. AppDomainNameCount++;
  107. // TODO: Currently security match current appdomain
  108. // Construct and initialize settings for a second AppDomain.
  109. AppDomainSetup ads = new AppDomainSetup();
  110. ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
  111. ads.DisallowBindingRedirects = false;
  112. ads.DisallowCodeDownload = true;
  113. ads.LoaderOptimization = LoaderOptimization.MultiDomain; // Sounds good ;)
  114. ads.ShadowCopyFiles = "true"; // Enabled shadowing
  115. ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  116. AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads);
  117. Console.WriteLine("Loading: " +
  118. AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll").ToString());
  119. AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll"));
  120. // Return the new AppDomain
  121. return AD;
  122. }
  123. /// <summary>
  124. /// Unload appdomains that are full and have only dead scripts
  125. /// </summary>
  126. private void UnloadAppDomains()
  127. {
  128. lock (freeLock)
  129. {
  130. // Go through all
  131. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  132. {
  133. // Don't process current AppDomain
  134. if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
  135. {
  136. // Not current AppDomain
  137. // Is number of unloaded bigger or equal to number of loaded?
  138. if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
  139. {
  140. Console.WriteLine("Found empty AppDomain, unloading");
  141. // Remove from internal list
  142. appDomains.Remove(ads);
  143. #if DEBUG
  144. long m = GC.GetTotalMemory(true);
  145. #endif
  146. // Unload
  147. AppDomain.Unload(ads.CurrentAppDomain);
  148. #if DEBUG
  149. Console.WriteLine("AppDomain unload freed " + (m - GC.GetTotalMemory(true)) +
  150. " bytes of memory");
  151. #endif
  152. }
  153. }
  154. }
  155. }
  156. }
  157. public LSL_BaseClass LoadScript(string FileName)
  158. {
  159. // Find next available AppDomain to put it in
  160. AppDomainStructure FreeAppDomain = GetFreeAppDomain();
  161. Console.WriteLine("Loading into AppDomain: " + FileName);
  162. LSL_BaseClass mbrt =
  163. (LSL_BaseClass)
  164. FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script");
  165. //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));
  166. FreeAppDomain.ScriptsLoaded++;
  167. return mbrt;
  168. }
  169. /// <summary>
  170. /// Increase "dead script" counter for an AppDomain
  171. /// </summary>
  172. /// <param name="ad"></param>
  173. //[Obsolete("Needs fixing, needs a real purpose in life!!!")]
  174. public void StopScript(AppDomain ad)
  175. {
  176. lock (freeLock)
  177. {
  178. Console.WriteLine("Stopping script in AppDomain");
  179. // Check if it is current AppDomain
  180. if (currentAD.CurrentAppDomain == ad)
  181. {
  182. // Yes - increase
  183. currentAD.ScriptsWaitingUnload++;
  184. return;
  185. }
  186. // Lopp through all AppDomains
  187. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  188. {
  189. if (ads.CurrentAppDomain == ad)
  190. {
  191. // Found it
  192. ads.ScriptsWaitingUnload++;
  193. break;
  194. }
  195. }
  196. }
  197. UnloadAppDomains(); // Outsite lock, has its own GetLock
  198. }
  199. }
  200. }