XMRHeapTracker.cs 13 KB

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