Profile.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package hl;
  23. typedef Symbol = hl.Abstract<"hl_symbol">;
  24. enum TrackKind {
  25. Alloc;
  26. Cast;
  27. DynField;
  28. DynCall;
  29. }
  30. class Result {
  31. public var t:hl.Type;
  32. public var kind:TrackKind;
  33. public var count:Int;
  34. public var info:Int;
  35. public var stack:Array<String>;
  36. public function new(t, count, info) {
  37. this.t = t;
  38. this.count = count;
  39. this.info = info;
  40. }
  41. @:keep public function toString() {
  42. return t + "(" + count + ")";
  43. }
  44. }
  45. @:hlNative("std")
  46. class Profile {
  47. public static var threadBits(get, set):haxe.EnumFlags<TrackKind>;
  48. public static var globalBits(get, set):haxe.EnumFlags<TrackKind>;
  49. static var KINDS = null;
  50. public static function getData(sortBySize = false, reset = true) {
  51. var old = globalBits;
  52. globalBits = new haxe.EnumFlags();
  53. if (buf == null)
  54. buf = new hl.Bytes(BUFSIZE * 2);
  55. track_lock(true);
  56. var maxDepth = 0;
  57. var count = track_count(maxDepth);
  58. var arr = new hl.NativeArray<Symbol>(maxDepth);
  59. var out = [];
  60. if (KINDS == null)
  61. KINDS = TrackKind.createAll();
  62. for (i in 0...count) {
  63. var t:hl.Type = null, count = 0, info = 0;
  64. var k = track_entry(i, t, count, info, arr);
  65. if (count == 0)
  66. continue;
  67. var a = new Result(t, count, info);
  68. a.kind = KINDS[k];
  69. a.stack = [for (a in arr) resolveSymbol(a)];
  70. out.push(a);
  71. }
  72. out.sort(function(a1, a2) {
  73. if (a1.kind != a2.kind)
  74. return a1.kind.getIndex() - a2.kind.getIndex();
  75. if (sortBySize && a1.kind == Alloc)
  76. return a2.info - a1.info;
  77. return a2.count - a1.count;
  78. });
  79. track_lock(false);
  80. if (reset)
  81. Profile.reset();
  82. globalBits = old;
  83. return out;
  84. }
  85. public static function dump(fileName = "track.dump", sortBySize = false, reset = true) {
  86. var old = globalBits;
  87. globalBits = new haxe.EnumFlags();
  88. var f = sys.io.File.write(fileName);
  89. var data = getData(sortBySize, reset);
  90. var allocCount = 0, allocSize = 0, castCount = 0, dynCount = 0;
  91. for (o in data) {
  92. switch (o.kind) {
  93. case Alloc:
  94. allocCount += o.count;
  95. allocSize += o.info;
  96. case Cast:
  97. castCount += o.count;
  98. case DynCall, DynField:
  99. dynCount += o.count;
  100. }
  101. }
  102. if (data.length == 0)
  103. f.writeString("Nothing\n");
  104. if (allocCount > 0)
  105. f.writeString(allocCount + " total allocs (" + allocSize + " bytes)\n");
  106. if (castCount > 0)
  107. f.writeString(castCount + " total casts\n");
  108. if (dynCount > 0)
  109. f.writeString(dynCount + " total dynamic accesses/calls\n");
  110. for (o in data) {
  111. var pcount = StringTools.lpad("" + o.count, " ", 5);
  112. switch (o.kind) {
  113. case Alloc:
  114. f.writeString("alloc " + pcount + " " + o.t + " (" + o.info + " bytes)\n");
  115. case Cast:
  116. f.writeString("cast " + pcount + " " + o.t + "\n");
  117. case DynCall:
  118. f.writeString("dyncall " + pcount + " " + o.t + "." + getFieldName(o.info) + "()\n");
  119. case DynField:
  120. f.writeString("dynfield " + pcount + " " + o.t + "." + getFieldName(o.info) + "\n");
  121. }
  122. for (s in o.stack)
  123. f.writeString("\t\t\t\t" + s + "\n");
  124. }
  125. f.close();
  126. globalBits = old;
  127. }
  128. /**
  129. Reset accumulated tracked data.
  130. **/
  131. @:hlNative("std", "track_reset") public static function reset() {}
  132. /**
  133. Restart tracking after being stopped.
  134. **/
  135. @:hlNative("std", "track_init") public static function restart() {}
  136. /**
  137. Stop tracking for all threads.
  138. **/
  139. public static function stop() {
  140. globalBits = new haxe.EnumFlags();
  141. }
  142. /**
  143. Set maximum stack depth for reports (default = 10)
  144. **/
  145. @:hlNative("std", "track_set_depth") public static function setMaxDepth(v:Int) {}
  146. static function get_threadBits()
  147. return new haxe.EnumFlags(track_get_bits(true));
  148. static function set_threadBits(v:haxe.EnumFlags<TrackKind>) {
  149. track_set_bits(v.toInt(), true);
  150. return v;
  151. }
  152. static function get_globalBits()
  153. return new haxe.EnumFlags(track_get_bits(false));
  154. static function set_globalBits(v:haxe.EnumFlags<TrackKind>) {
  155. track_set_bits(v.toInt(), false);
  156. return v;
  157. }
  158. static var BUFSIZE = 512;
  159. static var buf:hl.Bytes;
  160. static function resolveSymbol(s:Symbol) {
  161. var size = BUFSIZE;
  162. if (buf == null)
  163. buf = new hl.Bytes(BUFSIZE * 2);
  164. var bytes = resolve_symbol(s, buf, size);
  165. if (bytes == null)
  166. return "<???>";
  167. return @:privateAccess String.fromUCS2(bytes.sub(0, (size + 1) * 2));
  168. }
  169. static function resolve_symbol(s:Symbol, buf:hl.Bytes, bufSize:hl.Ref<Int>):hl.Bytes {
  170. return null;
  171. }
  172. static function track_count(maxDepth:hl.Ref<Int>):Int {
  173. return 0;
  174. }
  175. static function track_entry(id:Int, type:hl.Ref<hl.Type>, count:hl.Ref<Int>, info:hl.Ref<Int>, stack:NativeArray<Symbol>):Int {
  176. return 0;
  177. }
  178. static function track_init():Void {}
  179. static function track_lock(b:Bool):Void {}
  180. static function track_get_bits(thread:Bool):Int {
  181. return 0;
  182. }
  183. static function track_set_bits(bits:Int, thread:Bool):Void {}
  184. static function __init__()
  185. track_init();
  186. // todo : move later to hl.Bytes
  187. @:hlNative("std", "field_name")
  188. public static function getFieldName(hash:Int):Bytes {
  189. return null;
  190. }
  191. @:hlNative(1.11)
  192. static function sys_profile_event( code : Int, data : hl.Bytes, dataLen : Int ) : Void {}
  193. public static function event( code : Int, ?data : String ) @:privateAccess {
  194. sys_profile_event(code,data == null ? null : data.bytes, data == null ? 0 : (data.length<<1));
  195. }
  196. public static function eventBytes( code : Int, data : haxe.io.Bytes ) @:privateAccess {
  197. sys_profile_event(code,data == null ? null : data, data == null ? 0 : data.length);
  198. }
  199. }