Sfoglia il codice sorgente

Add profiling annotations

Fabian 6 anni fa
parent
commit
cbb056726a
6 ha cambiato i file con 43 aggiunte e 13 eliminazioni
  1. 2 2
      Makefile
  2. 8 1
      src/const.js
  3. 3 0
      src/cpu.js
  4. 12 3
      src/native/cpu.c
  5. 3 2
      src/native/profiler/profiler.c
  6. 15 5
      src/native/profiler/profiler.h

+ 2 - 2
Makefile

@@ -135,7 +135,7 @@ build/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
 		--js $(BROWSER_FILES)\
 		--js $(LIB_FILES)
 
-build/v86.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/codegen/*.h
+build/v86.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/codegen/*.h src/native/profiler/*
 	mkdir -p build
 	-ls -lh build/v86.wasm
 	# --llvm-opts 3
@@ -159,7 +159,7 @@ build/v86.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/
 	    -s WASM=1 -s SIDE_MODULE=1 -o build/v86.wasm
 	ls -lh build/v86.wasm
 
-build/v86-debug.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/codegen/*.h
+build/v86-debug.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/codegen/*.h src/native/profiler/*
 	mkdir -p build
 	emcc src/native/all.c src/native/codegen/codegen.c src/native/call-indirect.ll \
 	    -Isrc/native/ -Isrc/native/profiler/ \

+ 8 - 1
src/const.js

@@ -353,7 +353,14 @@ var MXCSR_MASK = (0xFFFF & ~(1 << 6));
 /** @const */
 const P_IDLE = 0;
 /** @const */
-const P_DO_MANY_CYCLES = 2;
+const P_DO_MANY_CYCLES = 1;
+/** @const */
+const P_GEN_INSTR = 2;
+/** @const */
+const P_RUN_FROM_CACHE = 3;
+/** @const */
+const P_RUN_INTERPRETED = 4;
+
 
 var WASM_TABLE_SIZE = 0x10000;
 

+ 3 - 0
src/cpu.js

@@ -1254,6 +1254,9 @@ CPU.prototype.do_many_cycles = function()
     }
 
     this.wm.exports["_profiler_end"](P_DO_MANY_CYCLES);
+    this.wm.exports["_profiler_end"](P_GEN_INSTR);
+    this.wm.exports["_profiler_end"](P_RUN_FROM_CACHE);
+    this.wm.exports["_profiler_end"](P_RUN_INTERPRETED);
 };
 
 CPU.prototype.do_many_cycles_unsafe = function()

+ 12 - 3
src/native/cpu.c

@@ -302,9 +302,10 @@ void cycle_internal()
         //jit_clear_func(addr_index);
     }
 
-    if(!JIT_DONT_USE_CACHE &&
-       cached && clean)
+    if(!JIT_DONT_USE_CACHE && cached && clean)
     {
+        profiler_start(P_RUN_FROM_CACHE);
+
         // XXX: With the code-generation, we need to figure out how we
         // would call the function from the other module here; likely
         // through a handler in JS. For now:
@@ -328,12 +329,16 @@ void cycle_internal()
         // XXX: Try to find an assert to detect self-modifying code
         // JIT compiled self-modifying basic blocks may trigger this assert
         // assert(entry->group_status != group_dirtiness[entry->start_addr >> DIRTY_ARR_SHIFT]);
-        *cache_hit = *cache_hit + 1;
+
+        //*cache_hit = *cache_hit + 1;
+
+        profiler_end(P_RUN_FROM_CACHE);
     }
     // A jump just occured indicating the start of a basic block + the
     // address is hot; let's JIT compile it
     else if(JIT_ALWAYS || jit_jump == 1 && ++hot_code_addresses[jit_hot_hash(phys_addr)] > JIT_THRESHOLD)
     {
+        profiler_start(P_GEN_INSTR);
         int32_t start_addr = *instruction_pointer;
         jit_in_progress = false;
 
@@ -383,14 +388,18 @@ void cycle_internal()
         //{
         entry->group_status = group_dirtiness[phys_addr >> DIRTY_ARR_SHIFT];
         //}
+
+        profiler_end(P_GEN_INSTR);
     }
     // Regular un-hot code execution
     else
     {
+        profiler_start(P_RUN_INTERPRETED);
         jit_jump = 0;
         int32_t opcode = read_imm8();
         run_instruction(opcode | !!*is_32 << 8);
         (*timestamp_counter)++;
+        profiler_end(P_RUN_INTERPRETED);
     }
 
 #else

+ 3 - 2
src/native/profiler/profiler.c

@@ -41,10 +41,11 @@ void profiler_end(enum profile_name name)
 void profiler_print()
 {
     double init_elapsed = get_time() - profiler_init_time;
-    printf("Elapsed: %d\n", (int32_t) init_elapsed);
-    for(uint32_t i = 0; i < PROFILER_NAME_COUNT; i++)
+    printf("\nElapsed: %d\n", (int32_t) init_elapsed);
+    for(int32_t i = 0; i < PROFILER_NAME_COUNT; i++)
     {
         double cur_total = profiler_arr[i].total;
+        printf(profiler_names[i]);
         printf(
             "\nIndex:\t%d"
             "\nTotal:\t%d"

+ 15 - 5
src/native/profiler/profiler.h

@@ -2,21 +2,31 @@
 #ifndef _PROFILER_H
 #define _PROFILER_H
 
+
+#define PROFILER_NAME_COUNT 5
+
 struct profiler_data {
     double total;
     double current_start;
     bool capturing;
-} profiler_arr[4] = {{0, 0, false}};
+} profiler_arr[PROFILER_NAME_COUNT] = {{0, 0, false}};
+
+const char *profiler_names[] = {
+    "IDLE",
+    "DO_MANY_CYCLES",
+    "GEN_INSTR",
+    "RUN_FROM_CACHE",
+    "RUN_INTERPRETED",
+};
 
 enum profile_name {
     P_IDLE,
-    P_GEN_INSTR,
     P_DO_MANY_CYCLES,
-    P_RUN_FROM_CACHE
+    P_GEN_INSTR,
+    P_RUN_FROM_CACHE,
+    P_RUN_INTERPRETED,
 };
 
-#define PROFILER_NAME_COUNT 4
-
 
 void profiler_init();
 void profiler_start(enum profile_name name);