Profile.hx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package hl;
  2. typedef Symbol = hl.Abstract<"hl_symbol">;
  3. class Allocation {
  4. public var t : hl.Type;
  5. public var count : Int;
  6. public var size : Int;
  7. public var stack : Array<String>;
  8. public function new(t, count, size) {
  9. this.t = t;
  10. this.count = count;
  11. this.size = size;
  12. }
  13. @:keep public function toString() {
  14. return t + "(" + count + ")";
  15. }
  16. }
  17. @:hlNative("std")
  18. class Profile {
  19. public static var enable(get, set) : Bool;
  20. public static function getData( sortBySize = false ) {
  21. var old = enable;
  22. enable = false;
  23. var maxDepth = 0;
  24. var count = track_count(maxDepth);
  25. var arr = new hl.NativeArray<Symbol>(maxDepth);
  26. var out = [];
  27. for( i in 0...count ) {
  28. var t : hl.Type = null, count = 0, size = 0;
  29. track_entry(i, t, count, size, arr);
  30. if( count == 0 ) continue;
  31. var a = new Allocation(t, count, size);
  32. a.stack = [for( a in arr ) resolveSymbol(a)];
  33. out.push(a);
  34. }
  35. if( sortBySize )
  36. out.sort(function(a1, a2) return a2.size - a1.size);
  37. else
  38. out.sort(function(a1, a2) return a2.count - a1.count);
  39. enable = old;
  40. return out;
  41. }
  42. public static function dump( fileName = "alloc.dump", sortBySize = false ) {
  43. var old = enable;
  44. enable = false;
  45. var f = sys.io.File.write(fileName);
  46. var data = getData(sortBySize);
  47. var count = 0, size = 0;
  48. for( o in data ) {
  49. count += o.count;
  50. size += o.size;
  51. }
  52. f.writeString(count +" total allocs (" + size+" bytes)\n");
  53. for( o in data ) {
  54. f.writeString(o.count+" "+o.t + " (" + o.size + " bytes)\n");
  55. for( s in o.stack )
  56. f.writeString("\t" + s + "\n");
  57. }
  58. f.close();
  59. enable = old;
  60. }
  61. static var BUFSIZE = 512;
  62. static var buf = new hl.Bytes(BUFSIZE*2);
  63. static function resolveSymbol( s : Symbol ) {
  64. var size = BUFSIZE;
  65. var bytes = resolve_symbol(s, buf, size);
  66. if( bytes == null ) return "<???>";
  67. return @:privateAccess String.fromUCS2(bytes.sub(0,(size+1)*2));
  68. }
  69. static function get_enable() return Gc.flags.has(Track);
  70. static function set_enable(v) {
  71. var flags = Gc.flags;
  72. if( v ) flags.set(Track) else flags.unset(Track);
  73. Gc.flags = flags;
  74. return v;
  75. }
  76. static function resolve_symbol( s : Symbol, buf : hl.Bytes, bufSize : hl.Ref<Int> ) : hl.Bytes { return null; }
  77. static function track_count( maxDepth : hl.Ref<Int> ) : Int { return 0; }
  78. static function track_entry( id : Int, type : hl.Ref<hl.Type>, count : hl.Ref<Int>, size : hl.Ref<Int>, stack : NativeArray<Symbol> ) : Void {}
  79. static function track_init() : Void {}
  80. static function __init__() track_init();
  81. }