1
0

XMRHeapTracker.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. }
  58. ~HeapTrackerBase()
  59. {
  60. usage = instance.UpdateHeapUse(usage, 0);
  61. }
  62. }
  63. /**
  64. * Wrapper around lists to keep track of how much memory they use.
  65. */
  66. public class HeapTrackerList: HeapTrackerBase
  67. {
  68. private static FieldInfo listValueField = typeof(HeapTrackerList).GetField("value");
  69. private static MethodInfo listSaveMethod = typeof(HeapTrackerList).GetMethod("Save");
  70. public LSL_List value;
  71. public HeapTrackerList(XMRInstAbstract inst) : base(inst) { }
  72. // generate CIL code to pop the value from the CIL stack
  73. // input:
  74. // 'this' pointer already pushed on CIL stack
  75. // new value pushed on CIL stack
  76. // output:
  77. // 'this' pointer popped from stack
  78. // new value popped from CIL stack
  79. // heap usage updated
  80. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  81. {
  82. ilGen.Emit(errorAt, OpCodes.Call, listSaveMethod);
  83. }
  84. // generate CIL code to push the value on the CIL stack
  85. // input:
  86. // 'this' pointer already pushed on CIL stack
  87. // output:
  88. // 'this' pointer popped from stack
  89. // value pushed on CIL stack replacing 'this' pointer
  90. // returns typeof value pushed on stack
  91. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  92. {
  93. ilGen.Emit(errorAt, OpCodes.Ldfld, listValueField);
  94. return typeof(LSL_List);
  95. }
  96. public void Save(LSL_List lis)
  97. {
  98. int newuse = Size(lis);
  99. usage = instance.UpdateHeapUse(usage, newuse);
  100. value = lis;
  101. }
  102. //private static int counter = 5;
  103. public static int Size(LSL_List lis)
  104. {
  105. // VS2017 in debug mode seems to have a problem running this statement quickly:
  106. //SLOW: return (!typeof(LSL_List).IsValueType && (lis == null)) ? 0 : lis.Size;
  107. //FAST: return 33;
  108. //SLOW: return (lis == null) ? 0 : 99;
  109. //FAST: return ++ counter;
  110. // VS2017 in debug mode seems content to run this quickly though:
  111. try
  112. {
  113. return lis.Size;
  114. }
  115. catch
  116. {
  117. return 0;
  118. }
  119. }
  120. }
  121. /**
  122. * Wrapper around objects to keep track of how much memory they use.
  123. */
  124. public class HeapTrackerObject: HeapTrackerBase
  125. {
  126. private static FieldInfo objectValueField = typeof(HeapTrackerObject).GetField("value");
  127. private static MethodInfo objectSaveMethod = typeof(HeapTrackerObject).GetMethod("Save");
  128. public const int HT_CHAR = 2;
  129. public const int HT_DELE = 8;
  130. public const int HT_DOUB = 8;
  131. public const int HT_SING = 4;
  132. public const int HT_SFLT = 4;
  133. public const int HT_INT = 4;
  134. public const int HT_VEC = HT_DOUB * 3;
  135. public const int HT_ROT = HT_DOUB * 4;
  136. public object value;
  137. public HeapTrackerObject(XMRInstAbstract inst) : base(inst) { }
  138. // generate CIL code to pop the value from the CIL stack
  139. // input:
  140. // 'this' pointer already pushed on CIL stack
  141. // new value pushed on CIL stack
  142. // output:
  143. // 'this' pointer popped from stack
  144. // new value popped from CIL stack
  145. // heap usage updated
  146. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  147. {
  148. ilGen.Emit(errorAt, OpCodes.Call, objectSaveMethod);
  149. }
  150. // generate CIL code to push the value on the CIL stack
  151. // input:
  152. // 'this' pointer already pushed on CIL stack
  153. // output:
  154. // 'this' pointer popped from stack
  155. // value pushed on CIL stack replacing 'this' pointer
  156. // returns typeof value pushed on stack
  157. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  158. {
  159. ilGen.Emit(errorAt, OpCodes.Ldfld, objectValueField);
  160. return typeof(object);
  161. }
  162. public void Save(object obj)
  163. {
  164. int newuse = Size(obj);
  165. usage = instance.UpdateHeapUse(usage, newuse);
  166. value = obj;
  167. }
  168. // public so it can be used by XMRArray
  169. public static int Size(object obj)
  170. {
  171. if(obj == null)
  172. return 0;
  173. if(obj is char)
  174. return HT_CHAR;
  175. if(obj is Delegate)
  176. return HT_DELE;
  177. if(obj is double)
  178. return HT_DOUB;
  179. if(obj is float)
  180. return HT_SING;
  181. if(obj is int)
  182. return HT_INT;
  183. if(obj is LSL_Float)
  184. return HT_SFLT;
  185. if(obj is LSL_Integer)
  186. return HT_INT;
  187. if(obj is LSL_List)
  188. return ((LSL_List)obj).Size;
  189. if(obj is LSL_Rotation)
  190. return HT_ROT;
  191. if(obj is LSL_String)
  192. return ((LSL_String)obj).m_string.Length * HT_CHAR;
  193. if(obj is LSL_Vector)
  194. return HT_VEC;
  195. if(obj is string)
  196. return ((string)obj).Length * HT_CHAR;
  197. if(obj is XMR_Array)
  198. return 0;
  199. if(obj is XMRArrayListKey)
  200. return ((XMRArrayListKey)obj).Size;
  201. if(obj is XMRSDTypeClObj)
  202. return 0;
  203. if(obj is Array)
  204. {
  205. Array ar = (Array)obj;
  206. int len = ar.Length;
  207. if(len == 0)
  208. return 0;
  209. Type et = ar.GetType().GetElementType();
  210. if(et.IsValueType)
  211. return Size(ar.GetValue(0)) * len;
  212. int size = 0;
  213. for(int i = 0; i < len; i++)
  214. {
  215. size += Size(ar.GetValue(i));
  216. }
  217. return size;
  218. }
  219. throw new Exception("unknown size of type " + obj.GetType().Name);
  220. }
  221. }
  222. /**
  223. * Wrapper around strings to keep track of how much memory they use.
  224. */
  225. public class HeapTrackerString: HeapTrackerBase
  226. {
  227. private static FieldInfo stringValueField = typeof(HeapTrackerString).GetField("value");
  228. private static MethodInfo stringSaveMethod = typeof(HeapTrackerString).GetMethod("Save");
  229. public string value;
  230. public HeapTrackerString(XMRInstAbstract inst) : base(inst) { }
  231. // generate CIL code to pop the value from the CIL stack
  232. // input:
  233. // 'this' pointer already pushed on CIL stack
  234. // new value pushed on CIL stack
  235. // output:
  236. // 'this' pointer popped from stack
  237. // new value popped from CIL stack
  238. // heap usage updated
  239. public static void GenPop(Token errorAt, ScriptMyILGen ilGen)
  240. {
  241. ilGen.Emit(errorAt, OpCodes.Call, stringSaveMethod);
  242. }
  243. // generate CIL code to push the value on the CIL stack
  244. // input:
  245. // 'this' pointer already pushed on CIL stack
  246. // output:
  247. // 'this' pointer popped from stack
  248. // value pushed on CIL stack replacing 'this' pointer
  249. // returns typeof value pushed on stack
  250. public static Type GenPush(Token errorAt, ScriptMyILGen ilGen)
  251. {
  252. ilGen.Emit(errorAt, OpCodes.Ldfld, stringValueField);
  253. return typeof(string);
  254. }
  255. public void Save(string str)
  256. {
  257. int newuse = Size(str);
  258. usage = instance.UpdateHeapUse(usage, newuse);
  259. value = str;
  260. }
  261. public static int Size(string str)
  262. {
  263. return (str == null) ? 0 : str.Length * HeapTrackerObject.HT_CHAR;
  264. }
  265. }
  266. }