XMRHeapTracker.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if (lis == null)
  98. usage = instance.UpdateLocalsHeapUse(usage, 0);
  99. else
  100. usage = instance.UpdateLocalsHeapUse(usage, Size(lis));
  101. value = lis;
  102. }
  103. public void Restore(LSL_List lis)
  104. {
  105. value = lis;
  106. if (lis != null)
  107. usage = Size(lis);
  108. else
  109. usage = 0;
  110. }
  111. //private static int counter = 5;
  112. public static int Size(LSL_List lis)
  113. {
  114. try
  115. {
  116. return lis.Size;
  117. }
  118. catch
  119. {
  120. return 0;
  121. }
  122. }
  123. }
  124. /**
  125. * Wrapper around objects to keep track of how much memory they use.
  126. */
  127. public class HeapTrackerObject: HeapTrackerBase
  128. {
  129. private static FieldInfo objectValueField = typeof(HeapTrackerObject).GetField("value");
  130. private static MethodInfo objectSaveMethod = typeof(HeapTrackerObject).GetMethod("Save");
  131. private static MethodInfo objectRestoreMethod = typeof(HeapTrackerObject).GetMethod("Restore");
  132. public const int HT_CHAR = 2;
  133. public const int HT_DELE = 8;
  134. public const int HT_DOUB = 8;
  135. public const int HT_SING = 4;
  136. public const int HT_SFLT = 4;
  137. public const int HT_INT = 4;
  138. public const int HT_VEC = HT_DOUB * 3;
  139. public const int HT_ROT = HT_DOUB * 4;
  140. public object value;
  141. public HeapTrackerObject(XMRInstAbstract inst) : base(inst) { }
  142. // generate CIL code to pop the value from the CIL stack
  143. // input:
  144. // 'this' pointer already pushed on CIL stack
  145. // new value pushed on CIL stack
  146. // output:
  147. // 'this' pointer popped from stack
  148. // new value popped from CIL stack
  149. // heap usage updated
  150. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  151. {
  152. ilGen.Emit(errorAt, OpCodes.Call, objectSaveMethod);
  153. }
  154. public static void GenRestore(Token errorAt, ScriptMyILGen ilGen)
  155. {
  156. ilGen.Emit(errorAt, OpCodes.Call, objectRestoreMethod);
  157. }
  158. // generate CIL code to push the value on the CIL stack
  159. // input:
  160. // 'this' pointer already pushed on CIL stack
  161. // output:
  162. // 'this' pointer popped from stack
  163. // value pushed on CIL stack replacing 'this' pointer
  164. // returns typeof value pushed on stack
  165. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  166. {
  167. ilGen.Emit(errorAt, OpCodes.Ldfld, objectValueField);
  168. return typeof(object);
  169. }
  170. public void Save(object obj)
  171. {
  172. usage = instance.UpdateLocalsHeapUse(usage, Size(obj));
  173. value = obj;
  174. }
  175. public void Restore(object obj)
  176. {
  177. value = obj;
  178. usage = Size(obj);
  179. }
  180. // public so it can be used by XMRArray
  181. public static int Size(object obj)
  182. {
  183. if(obj == null)
  184. return 0;
  185. if(obj is char)
  186. return HT_CHAR;
  187. if(obj is Delegate)
  188. return HT_DELE;
  189. if(obj is double)
  190. return HT_DOUB;
  191. if(obj is float)
  192. return HT_SING;
  193. if(obj is int)
  194. return HT_INT;
  195. if(obj is LSL_Float) // lsl floats are stupid doubles
  196. return HT_DOUB;
  197. if(obj is LSL_Integer)
  198. return HT_INT;
  199. if(obj is LSL_List)
  200. return ((LSL_List)obj).Size;
  201. if(obj is LSL_Rotation)
  202. return HT_ROT;
  203. if(obj is LSL_String)
  204. return ((LSL_String)obj).m_string.Length * HT_CHAR;
  205. if(obj is LSL_Vector)
  206. return HT_VEC;
  207. if(obj is string)
  208. return ((string)obj).Length * HT_CHAR;
  209. if(obj is XMR_Array)
  210. return 0;
  211. if(obj is XMRArrayListKey)
  212. return ((XMRArrayListKey)obj).Size;
  213. if(obj is XMRSDTypeClObj)
  214. return 0;
  215. if(obj is Array)
  216. {
  217. Array ar = (Array)obj;
  218. int len = ar.Length;
  219. if(len == 0)
  220. return 0;
  221. Type et = ar.GetType().GetElementType();
  222. if(et.IsValueType)
  223. return Size(ar.GetValue(0)) * len;
  224. int size = 0;
  225. for(int i = 0; i < len; i++)
  226. {
  227. size += Size(ar.GetValue(i));
  228. }
  229. return size;
  230. }
  231. throw new Exception("unknown size of type " + obj.GetType().Name);
  232. }
  233. }
  234. /**
  235. * Wrapper around strings to keep track of how much memory they use.
  236. */
  237. public class HeapTrackerString: HeapTrackerBase
  238. {
  239. private static FieldInfo stringValueField = typeof(HeapTrackerString).GetField("value");
  240. private static MethodInfo stringRestoreMethod = typeof(HeapTrackerString).GetMethod("Restore");
  241. private static MethodInfo stringSaveMethod = typeof(HeapTrackerString).GetMethod("Save");
  242. public string value;
  243. public HeapTrackerString(XMRInstAbstract inst) : base(inst) { }
  244. // generate CIL code to pop the value from the CIL stack
  245. // input:
  246. // 'this' pointer already pushed on CIL stack
  247. // new value pushed on CIL stack
  248. // output:
  249. // 'this' pointer popped from stack
  250. // new value popped from CIL stack
  251. // heap usage updated
  252. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  253. {
  254. ilGen.Emit(errorAt, OpCodes.Call, stringSaveMethod);
  255. }
  256. public static void GenRestore(Token errorAt, ScriptMyILGen ilGen)
  257. {
  258. ilGen.Emit(errorAt, OpCodes.Call, stringRestoreMethod);
  259. }
  260. // generate CIL code to push the value on the CIL stack
  261. // input:
  262. // 'this' pointer already pushed on CIL stack
  263. // output:
  264. // 'this' pointer popped from stack
  265. // value pushed on CIL stack replacing 'this' pointer
  266. // returns typeof value pushed on stack
  267. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  268. {
  269. ilGen.Emit(errorAt, OpCodes.Ldfld, stringValueField);
  270. return typeof(string);
  271. }
  272. public void Save(string str)
  273. {
  274. usage = instance.UpdateLocalsHeapUse(usage, Size(str));
  275. value = str;
  276. }
  277. public void Restore(string str)
  278. {
  279. value = str;
  280. usage = Size(str);
  281. }
  282. public static int Size(string str)
  283. {
  284. return string.IsNullOrWhiteSpace(str) ? 0 : str.Length * HeapTrackerObject.HT_CHAR;
  285. }
  286. }
  287. }