Profile.hx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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() {
  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. out.sort(function(a1, a2) return a2.count - a1.count);
  36. enable = old;
  37. return out;
  38. }
  39. public static function dump( fileName = "alloc.dump" ) {
  40. var d = getData();
  41. var old = enable;
  42. enable = false;
  43. var f = sys.io.File.write(fileName);
  44. var data = getData();
  45. var count = 0, size = 0;
  46. for( o in data ) {
  47. count += o.count;
  48. size += o.size;
  49. }
  50. f.writeString(count +" total allocs (" + size+" bytes)\n");
  51. for( o in data ) {
  52. f.writeString(o.count+" "+o.t + " (" + o.size + " bytes)\n");
  53. for( s in o.stack )
  54. f.writeString("\t" + s + "\n");
  55. }
  56. f.close();
  57. enable = old;
  58. }
  59. static var BUFSIZE = 512;
  60. static var buf = new hl.Bytes(BUFSIZE*2);
  61. static function resolveSymbol( s : Symbol ) {
  62. var size = BUFSIZE;
  63. var bytes = resolve_symbol(s, buf, size);
  64. if( bytes == null ) return "<???>";
  65. return @:privateAccess String.fromUCS2(bytes.sub(0,(size+1)*2));
  66. }
  67. static function get_enable() return Gc.flags.has(Track);
  68. static function set_enable(v) {
  69. var flags = Gc.flags;
  70. if( v ) flags.set(Track) else flags.unset(Track);
  71. Gc.flags = flags;
  72. return v;
  73. }
  74. static function resolve_symbol( s : Symbol, buf : hl.Bytes, bufSize : hl.Ref<Int> ) : hl.Bytes { return null; }
  75. static function track_count( maxDepth : hl.Ref<Int> ) : Int { return 0; }
  76. static function track_entry( id : Int, type : hl.Ref<hl.Type>, count : hl.Ref<Int>, size : hl.Ref<Int>, stack : NativeArray<Symbol> ) : Void {}
  77. static function track_init() : Void {}
  78. static function __init__() track_init();
  79. }