XMRHeapTracker.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 OpenSimulator 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.Reflection;
  29. using System.Reflection.Emit;
  30. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  31. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  32. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  33. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  34. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  35. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  36. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  37. namespace OpenSim.Region.ScriptEngine.Yengine
  38. {
  39. /**
  40. * One instance of this class for lsl base objects that take a variable
  41. * amount of memory. They are what the script-visible list,object,string
  42. * variables are declared as at the CIL level. Generally, temp vars used
  43. * by the compiler get their basic type (list,object,string).
  44. *
  45. * Note that the xmr arrays and script-defined objects have their own
  46. * heap tracking built in so do not need any of this stuff.
  47. */
  48. public class HeapTrackerBase
  49. {
  50. protected int usage; // num bytes used by object
  51. protected XMRInstAbstract instance; // what script it is in
  52. public HeapTrackerBase(XMRInstAbstract inst)
  53. {
  54. if(inst == null)
  55. throw new ArgumentNullException("inst");
  56. instance = inst;
  57. usage = 0;
  58. }
  59. }
  60. /**
  61. * Wrapper around lists to keep track of how much memory they use.
  62. */
  63. public class HeapTrackerList: HeapTrackerBase
  64. {
  65. private static FieldInfo listValueField = typeof(HeapTrackerList).GetField("value");
  66. private static MethodInfo listSaveMethod = typeof(HeapTrackerList).GetMethod("Save");
  67. private static MethodInfo listRestoreMethod = typeof(HeapTrackerList).GetMethod("Restore");
  68. public LSL_List value;
  69. public HeapTrackerList(XMRInstAbstract inst) : base(inst) {}
  70. // generate CIL code to pop the value ie store in value
  71. // input:
  72. // 'this' pointer already pushed on CIL stack
  73. // new value
  74. // output:
  75. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  76. {
  77. ilGen.Emit(errorAt, OpCodes.Call, listSaveMethod);
  78. }
  79. public static void GenRestore(Token errorAt, ScriptMyILGen ilGen)
  80. {
  81. ilGen.Emit(errorAt, OpCodes.Call, listRestoreMethod);
  82. }
  83. // generate CIL code to push the value on the CIL stack
  84. // input:
  85. // 'this' pointer already pushed on CIL stack
  86. // output:
  87. // 'this' pointer popped from stack
  88. // value pushed on CIL stack replacing 'this' pointer
  89. // returns typeof value pushed on stack
  90. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  91. {
  92. ilGen.Emit(errorAt, OpCodes.Ldfld, listValueField);
  93. return typeof(LSL_List);
  94. }
  95. public void Save(LSL_List lis)
  96. {
  97. usage = (lis is null) ? instance.UpdateLocalsHeapUse(usage, 0) : instance.UpdateLocalsHeapUse(usage, lis.Size);
  98. value = lis;
  99. }
  100. public void Restore(LSL_List lis)
  101. {
  102. value = lis;
  103. usage = (lis is null) ? 0 : lis.Size;
  104. }
  105. public static int Size(LSL_List lis)
  106. {
  107. return lis is null ? 0 : lis.Size;
  108. }
  109. }
  110. /**
  111. * Wrapper around objects to keep track of how much memory they use.
  112. */
  113. public class HeapTrackerObject: HeapTrackerBase
  114. {
  115. private static FieldInfo objectValueField = typeof(HeapTrackerObject).GetField("value");
  116. private static MethodInfo objectSaveMethod = typeof(HeapTrackerObject).GetMethod("Save");
  117. private static MethodInfo objectRestoreMethod = typeof(HeapTrackerObject).GetMethod("Restore");
  118. public const int HT_CHAR = 2;
  119. public const int HT_DELE = 8;
  120. public const int HT_DOUB = 8;
  121. public const int HT_SING = 4;
  122. public const int HT_SFLT = 4;
  123. public const int HT_INT = 4;
  124. public const int HT_VEC = HT_DOUB * 3;
  125. public const int HT_ROT = HT_DOUB * 4;
  126. public object value;
  127. public HeapTrackerObject(XMRInstAbstract inst) : base(inst) { }
  128. // generate CIL code to pop the value from the CIL stack
  129. // input:
  130. // 'this' pointer already pushed on CIL stack
  131. // new value pushed on CIL stack
  132. // output:
  133. // 'this' pointer popped from stack
  134. // new value popped from CIL stack
  135. // heap usage updated
  136. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  137. {
  138. ilGen.Emit(errorAt, OpCodes.Call, objectSaveMethod);
  139. }
  140. public static void GenRestore(Token errorAt, ScriptMyILGen ilGen)
  141. {
  142. ilGen.Emit(errorAt, OpCodes.Call, objectRestoreMethod);
  143. }
  144. // generate CIL code to push the value on the CIL stack
  145. // input:
  146. // 'this' pointer already pushed on CIL stack
  147. // output:
  148. // 'this' pointer popped from stack
  149. // value pushed on CIL stack replacing 'this' pointer
  150. // returns typeof value pushed on stack
  151. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  152. {
  153. ilGen.Emit(errorAt, OpCodes.Ldfld, objectValueField);
  154. return typeof(object);
  155. }
  156. public void Save(object obj)
  157. {
  158. usage = instance.UpdateLocalsHeapUse(usage, Size(obj));
  159. value = obj;
  160. }
  161. public void Restore(object obj)
  162. {
  163. value = obj;
  164. usage = Size(obj);
  165. }
  166. // public so it can be used by XMRArray
  167. public static int Size(object obj)
  168. {
  169. if(obj == null)
  170. return 0;
  171. if(obj is char)
  172. return HT_CHAR;
  173. if(obj is Delegate)
  174. return HT_DELE;
  175. if(obj is double)
  176. return HT_DOUB;
  177. if(obj is float)
  178. return HT_SING;
  179. if(obj is int)
  180. return HT_INT;
  181. if(obj is LSL_Float) // lsl floats are stupid doubles
  182. return HT_DOUB;
  183. if(obj is LSL_Integer)
  184. return HT_INT;
  185. if(obj is LSL_List)
  186. return ((LSL_List)obj).Size;
  187. if(obj is LSL_Rotation)
  188. return HT_ROT;
  189. if(obj is LSL_String)
  190. return ((LSL_String)obj).m_string.Length * HT_CHAR;
  191. if(obj is LSL_Vector)
  192. return HT_VEC;
  193. if(obj is string)
  194. return ((string)obj).Length * HT_CHAR;
  195. if(obj is XMR_Array)
  196. return 0;
  197. if(obj is XMRArrayListKey)
  198. return ((XMRArrayListKey)obj).Size;
  199. if(obj is XMRSDTypeClObj)
  200. return 0;
  201. if(obj is Array)
  202. {
  203. Array ar = (Array)obj;
  204. int len = ar.Length;
  205. if(len == 0)
  206. return 0;
  207. Type et = ar.GetType().GetElementType();
  208. if(et.IsValueType)
  209. return Size(ar.GetValue(0)) * len;
  210. int size = 0;
  211. for(int i = 0; i < len; i++)
  212. {
  213. size += Size(ar.GetValue(i));
  214. }
  215. return size;
  216. }
  217. throw new Exception("unknown size of type " + obj.GetType().Name);
  218. }
  219. }
  220. /**
  221. * Wrapper around strings to keep track of how much memory they use.
  222. */
  223. public class HeapTrackerString: HeapTrackerBase
  224. {
  225. private static FieldInfo stringValueField = typeof(HeapTrackerString).GetField("value");
  226. private static MethodInfo stringRestoreMethod = typeof(HeapTrackerString).GetMethod("Restore");
  227. private static MethodInfo stringSaveMethod = typeof(HeapTrackerString).GetMethod("Save");
  228. public string value;
  229. public HeapTrackerString(XMRInstAbstract inst) : base(inst) { }
  230. // generate CIL code to pop the value from the CIL stack
  231. // input:
  232. // 'this' pointer already pushed on CIL stack
  233. // new value pushed on CIL stack
  234. // output:
  235. // 'this' pointer popped from stack
  236. // new value popped from CIL stack
  237. // heap usage updated
  238. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  239. {
  240. ilGen.Emit(errorAt, OpCodes.Call, stringSaveMethod);
  241. }
  242. public static void GenRestore(Token errorAt, ScriptMyILGen ilGen)
  243. {
  244. ilGen.Emit(errorAt, OpCodes.Call, stringRestoreMethod);
  245. }
  246. // generate CIL code to push the value on the CIL stack
  247. // input:
  248. // 'this' pointer already pushed on CIL stack
  249. // output:
  250. // 'this' pointer popped from stack
  251. // value pushed on CIL stack replacing 'this' pointer
  252. // returns typeof value pushed on stack
  253. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  254. {
  255. ilGen.Emit(errorAt, OpCodes.Ldfld, stringValueField);
  256. return typeof(string);
  257. }
  258. public void Save(string str)
  259. {
  260. usage = instance.UpdateLocalsHeapUse(usage, Size(str));
  261. value = str;
  262. }
  263. public void Restore(string str)
  264. {
  265. value = str;
  266. usage = Size(str);
  267. }
  268. public static int Size(string str)
  269. {
  270. return string.IsNullOrWhiteSpace(str) ? 0 : str.Length * HeapTrackerObject.HT_CHAR;
  271. }
  272. }
  273. }