print_stats.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. const print_stats = {
  3. stats_to_string: function(cpu)
  4. {
  5. return print_stats.print_misc_stats(cpu) +
  6. print_stats.print_instruction_counts(cpu);
  7. },
  8. print_misc_stats: function(cpu)
  9. {
  10. let text = "";
  11. const stat_names = [
  12. "COMPILE",
  13. "COMPILE_SUCCESS",
  14. "COMPILE_WRONG_ADDRESS_SPACE",
  15. "COMPILE_CUT_OFF_AT_END_OF_PAGE",
  16. "COMPILE_WITH_LOOP_SAFETY",
  17. "COMPILE_PAGE",
  18. "COMPILE_BASIC_BLOCK",
  19. "COMPILE_DUPLICATED_BASIC_BLOCK",
  20. "COMPILE_WASM_BLOCK",
  21. "COMPILE_WASM_LOOP",
  22. "COMPILE_DISPATCHER",
  23. "COMPILE_ENTRY_POINT",
  24. "COMPILE_WASM_TOTAL_BYTES",
  25. "JIT_CACHE_OVERRIDE",
  26. "JIT_CACHE_OVERRIDE_DIFFERENT_STATE_FLAGS",
  27. "RUN_INTERPRETED",
  28. "RUN_INTERPRETED_PENDING",
  29. "RUN_INTERPRETED_NEAR_END_OF_PAGE",
  30. "RUN_INTERPRETED_DIFFERENT_STATE",
  31. "RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED",
  32. "RUN_INTERPRETED_MISSED_COMPILED_ENTRY_LOOKUP",
  33. "RUN_INTERPRETED_STEPS",
  34. "RUN_FROM_CACHE",
  35. "RUN_FROM_CACHE_STEPS",
  36. "DIRECT_EXIT",
  37. "INDIRECT_JUMP",
  38. "INDIRECT_JUMP_NO_ENTRY",
  39. "NORMAL_PAGE_CHANGE",
  40. "NORMAL_FALLTHRU",
  41. "NORMAL_FALLTHRU_WITH_TARGET_BLOCK",
  42. "NORMAL_BRANCH",
  43. "NORMAL_BRANCH_WITH_TARGET_BLOCK",
  44. "CONDITIONAL_JUMP",
  45. "CONDITIONAL_JUMP_PAGE_CHANGE",
  46. "CONDITIONAL_JUMP_EXIT",
  47. "CONDITIONAL_JUMP_FALLTHRU",
  48. "CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK",
  49. "CONDITIONAL_JUMP_BRANCH",
  50. "CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK",
  51. "DISPATCHER_SMALL",
  52. "DISPATCHER_LARGE",
  53. "LOOP",
  54. "FAILED_PAGE_CHANGE",
  55. "SAFE_READ_FAST",
  56. "SAFE_READ_SLOW_PAGE_CROSSED",
  57. "SAFE_READ_SLOW_NOT_VALID",
  58. "SAFE_READ_SLOW_NOT_USER",
  59. "SAFE_READ_SLOW_IN_MAPPED_RANGE",
  60. "SAFE_WRITE_FAST",
  61. "SAFE_WRITE_SLOW_PAGE_CROSSED",
  62. "SAFE_WRITE_SLOW_NOT_VALID",
  63. "SAFE_WRITE_SLOW_NOT_USER",
  64. "SAFE_WRITE_SLOW_IN_MAPPED_RANGE",
  65. "SAFE_WRITE_SLOW_READ_ONLY",
  66. "SAFE_WRITE_SLOW_HAS_CODE",
  67. "SAFE_READ_WRITE_FAST",
  68. "SAFE_READ_WRITE_SLOW_PAGE_CROSSED",
  69. "SAFE_READ_WRITE_SLOW_NOT_VALID",
  70. "SAFE_READ_WRITE_SLOW_NOT_USER",
  71. "SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE",
  72. "SAFE_READ_WRITE_SLOW_READ_ONLY",
  73. "SAFE_READ_WRITE_SLOW_HAS_CODE",
  74. "PAGE_FAULT",
  75. "TLB_MISS",
  76. "DO_RUN",
  77. "DO_MANY_CYCLES",
  78. "CYCLE_INTERNAL",
  79. "INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES",
  80. "INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED",
  81. "INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE",
  82. "INVALIDATE_MODULE_DIRTY_PAGE",
  83. "INVALIDATE_PAGE_HAD_CODE",
  84. "INVALIDATE_PAGE_HAD_ENTRY_POINTS",
  85. "DIRTY_PAGE_DID_NOT_HAVE_CODE",
  86. "RUN_FROM_CACHE_EXIT_SAME_PAGE",
  87. "RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE",
  88. "RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE",
  89. "CLEAR_TLB",
  90. "FULL_CLEAR_TLB",
  91. "TLB_FULL",
  92. "TLB_GLOBAL_FULL",
  93. "MODRM_SIMPLE_REG",
  94. "MODRM_SIMPLE_REG_WITH_OFFSET",
  95. "MODRM_SIMPLE_CONST_OFFSET",
  96. "MODRM_COMPLEX",
  97. "SEG_OFFSET_OPTIMISED",
  98. "SEG_OFFSET_NOT_OPTIMISED",
  99. ];
  100. for(let i = 0; i < stat_names.length; i++)
  101. {
  102. let stat = cpu.wm.exports["profiler_stat_get"](i);
  103. stat = stat >= 100e6 ? Math.round(stat / 1e6) + "m" : stat >= 100e3 ? Math.round(stat / 1e3) + "k" : stat;
  104. text += stat_names[i] + "=" + stat + "\n";
  105. }
  106. text += "\n";
  107. const tlb_entries = cpu.wm.exports["get_valid_tlb_entries_count"]();
  108. const global_tlb_entries = cpu.wm.exports["get_valid_global_tlb_entries_count"]();
  109. const nonglobal_tlb_entries = tlb_entries - global_tlb_entries;
  110. text += "TLB_ENTRIES=" + tlb_entries + " (" + global_tlb_entries + " global, " + nonglobal_tlb_entries + " non-global)\n";
  111. text += "WASM_TABLE_FREE=" + cpu.wm.exports["jit_get_wasm_table_index_free_list_count"]() + "\n";
  112. text += "JIT_CACHE_SIZE=" + cpu.wm.exports["jit_get_cache_size"]() + "\n";
  113. text += "FLAT_SEGMENTS=" + cpu.wm.exports["has_flat_segmentation"]() + "\n";
  114. text += "do_many_cycles avg: " + (do_many_cycles_total / do_many_cycles_count || 0) + "\n";
  115. text += "wasm memory size: " + (cpu.wasm_memory.buffer.byteLength >> 20) + "m\n";
  116. return text;
  117. },
  118. print_instruction_counts: function(cpu)
  119. {
  120. return [
  121. print_stats.print_instruction_counts_offset(cpu, false, false, false, false),
  122. print_stats.print_instruction_counts_offset(cpu, true, false, false, false),
  123. print_stats.print_instruction_counts_offset(cpu, false, true, false, false),
  124. print_stats.print_instruction_counts_offset(cpu, false, false, true, false),
  125. print_stats.print_instruction_counts_offset(cpu, false, false, false, true),
  126. ].join("\n\n");
  127. },
  128. print_instruction_counts_offset: function(cpu, compiled, jit_exit, unguarded_register, wasm_size)
  129. {
  130. let text = "";
  131. const counts = [];
  132. const label =
  133. compiled ? "compiled" :
  134. jit_exit ? "jit exit" :
  135. unguarded_register ? "unguarded register" :
  136. wasm_size ? "wasm size" :
  137. "executed";
  138. for(let opcode = 0; opcode < 0x100; opcode++)
  139. {
  140. for(let fixed_g = 0; fixed_g < 8; fixed_g++)
  141. {
  142. for(let is_mem of [false, true])
  143. {
  144. const count = cpu.wm.exports["get_opstats_buffer"](compiled, jit_exit, unguarded_register, wasm_size, opcode, false, is_mem, fixed_g);
  145. counts.push({ opcode, count, is_mem, fixed_g });
  146. const count_0f = cpu.wm.exports["get_opstats_buffer"](compiled, jit_exit, unguarded_register, wasm_size, opcode, true, is_mem, fixed_g);
  147. counts.push({ opcode: 0x0f00 | opcode, count: count_0f, is_mem, fixed_g });
  148. }
  149. }
  150. }
  151. let total = 0;
  152. const prefixes = new Set([
  153. 0x26, 0x2E, 0x36, 0x3E,
  154. 0x64, 0x65, 0x66, 0x67,
  155. 0xF0, 0xF2, 0xF3,
  156. ]);
  157. for(let { count, opcode } of counts)
  158. {
  159. if(!prefixes.has(opcode))
  160. {
  161. total += count;
  162. }
  163. }
  164. if(total === 0)
  165. {
  166. return "";
  167. }
  168. const per_opcode = new Uint32Array(0x100);
  169. const per_opcode0f = new Uint32Array(0x100);
  170. for(let { opcode, count } of counts)
  171. {
  172. if((opcode & 0xFF00) == 0x0F00)
  173. {
  174. per_opcode0f[opcode & 0xFF] += count;
  175. }
  176. else
  177. {
  178. per_opcode[opcode & 0xFF] += count;
  179. }
  180. }
  181. text += "------------------\n";
  182. text += "Total: " + total + "\n";
  183. const factor = total > 1e7 ? 1000 : 1;
  184. const max_count = Math.max.apply(Math,
  185. counts.map(({ count }) => Math.round(count / factor))
  186. );
  187. const pad_length = String(max_count).length;
  188. text += `Instruction counts ${label} (in ${factor}):\n`;
  189. for(let i = 0; i < 0x100; i++)
  190. {
  191. text += h(i, 2).slice(2) + ":" + v86util.pads(Math.round(per_opcode[i] / factor), pad_length);
  192. if(i % 16 == 15)
  193. text += "\n";
  194. else
  195. text += " ";
  196. }
  197. text += "\n";
  198. text += `Instruction counts ${label} (0f, in ${factor}):\n`;
  199. for(let i = 0; i < 0x100; i++)
  200. {
  201. text += h(i & 0xFF, 2).slice(2) + ":" + v86util.pads(Math.round(per_opcode0f[i] / factor), pad_length);
  202. if(i % 16 == 15)
  203. text += "\n";
  204. else
  205. text += " ";
  206. }
  207. text += "\n";
  208. const top_counts = counts.filter(({ count }) => count).sort(({ count: count1 }, { count: count2 }) => count2 - count1);
  209. for(let { opcode, is_mem, fixed_g, count } of top_counts.slice(0, 200))
  210. {
  211. let opcode_description = opcode.toString(16) + "_" + fixed_g + (is_mem ? "_m" : "_r");
  212. text += opcode_description + ":" + (count / total * 100).toFixed(2) + " ";
  213. }
  214. text += "\n";
  215. return text;
  216. },
  217. };
  218. if(typeof module !== "undefined" && typeof module.exports !== "undefined")
  219. {
  220. module.exports["print_stats"] = print_stats;
  221. }