فهرست منبع

Profiler: Record stat counters

Fabian 6 سال پیش
والد
کامیت
cbcbeb6a3a
5فایلهای تغییر یافته به همراه69 افزوده شده و 12 حذف شده
  1. 0 3
      src/cpu.js
  2. 16 5
      src/native/cpu.c
  3. 0 2
      src/native/global_pointers.h
  4. 34 2
      src/native/profiler/profiler.c
  5. 19 0
      src/native/profiler/profiler.h

+ 0 - 3
src/cpu.js

@@ -204,9 +204,6 @@ function CPU(bus, wm, codegen)
     this.reg_mmx8s = new Int8Array(this.reg_mmxs.buffer, 1064, 64);
     this.reg_mmx8 = new Uint8Array(this.reg_mmxs.buffer, 1064, 64);
 
-    this.cache_hit = new Uint32Array(wm.memory.buffer, 1280, 1);
-    this.cache_compile = new Uint32Array(wm.memory.buffer, 1284, 1);
-
     this.reg_xmm32s = new Int32Array(wm.memory.buffer, 828, 8 * 4);
 
     this.mxcsr = new Int32Array(wm.memory.buffer, 824, 1);

+ 16 - 5
src/native/cpu.c

@@ -305,6 +305,7 @@ void cycle_internal()
     if(!JIT_DONT_USE_CACHE && cached && clean)
     {
         profiler_start(P_RUN_FROM_CACHE);
+        profiler_stat_increment(S_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
@@ -330,15 +331,22 @@ void cycle_internal()
         // 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;
-
         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)
     {
+        if(clean && entry->start_addr != 0 && entry->start_addr != phys_addr)
+        {
+            // contains still valid code from different address, about to be overwritten
+            //printf("%x %x", entry->start_addr, phys_addr);
+            profiler_stat_increment(S_CACHE_MISMATCH);
+        }
+
         profiler_start(P_GEN_INSTR);
+        profiler_stat_increment(S_COMPILE);
+
         int32_t start_addr = *instruction_pointer;
         jit_in_progress = false;
 
@@ -350,9 +358,9 @@ void cycle_internal()
         entry->start_addr = phys_addr;
         entry->end_addr = phys_addr + 1;
         entry->is_32 = *is_32;
-        jit_cache_arr[addr_index] = *entry;
 
-        *cache_compile = *cache_compile + 1;
+        //jit_cache_arr[addr_index] = *entry;
+        assert(&jit_cache_arr[addr_index] == entry);
 
         gen_reset();
 
@@ -391,16 +399,19 @@ void cycle_internal()
         entry->group_status = group_dirtiness[phys_addr >> DIRTY_ARR_SHIFT];
         //}
 
+        profiler_stat_increment(S_COMPILE_SUCCESS);
         profiler_end(P_GEN_INSTR);
     }
     // Regular un-hot code execution
     else
     {
         profiler_start(P_RUN_INTERPRETED);
-        jit_jump = 0;
+        profiler_stat_increment(S_RUN_INTERPRETED);
+
         int32_t opcode = read_imm8();
         run_instruction(opcode | !!*is_32 << 8);
         (*timestamp_counter)++;
+
         profiler_end(P_RUN_INTERPRETED);
     }
 

+ 0 - 2
src/native/global_pointers.h

@@ -99,6 +99,4 @@ int32_t* const fpu_st32 = (int32_t* const) 968;
 
 union reg64* const reg_mmx = (union reg64* const) 1064; // length 64
 
-uint32_t* const cache_hit = (uint32_t* const) 1280;
-uint32_t* const cache_compile = (uint32_t* const) 1284;
 #endif

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

@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdint.h>
 #include "profiler.h"
 
 #if ENABLE_PROFILER
@@ -9,6 +10,7 @@ double profiler_init_time = 0;
 void profiler_init()
 {
     profiler_init_time = get_time();
+
     for(uint32_t i = 0; i < PROFILER_NAME_COUNT; i++)
     {
         struct profiler_data *entry = &profiler_arr[i];
@@ -16,6 +18,11 @@ void profiler_init()
         entry->current_start = 0;
         entry->capturing = false;
     }
+
+    for(uint32_t i = 0; i < PROFILER_STAT_COUNT; i++)
+    {
+        profiler_stat_arr[i].count = 0;
+    }
 }
 
 void profiler_start(enum profile_name name)
@@ -57,12 +64,37 @@ void profiler_print()
     }
 }
 
+int32_t profiler_get_time(enum profile_name name)
+{
+    return profiler_arr[name].total;
+}
+
+int32_t profiler_get_total(void)
+{
+    return get_time() - profiler_init_time;
+}
+
+void profiler_stat_increment(enum stat_name stat)
+{
+    profiler_stat_arr[stat].count++;
+}
+
+int32_t profiler_stat_get(enum stat_name stat)
+{
+    return profiler_stat_arr[stat].count;
+}
+
 #else
 // Disable profiler
 
-void profiler_init() {}
+void profiler_init(void) {}
 void profiler_start(enum profile_name name) {}
 void profiler_end(enum profile_name name) {}
-void profiler_print() {}
+void profiler_print(void) {}
+int32_t profiler_get_time(enum profile_name name) { return 0; }
+int32_t profiler_get_total(void) { return 0; }
+void profiler_stat_increment(enum stat_name stat) {}
+int32_t profiler_stat_get(enum stat_name stat) { return 0; }
+
 
 #endif

+ 19 - 0
src/native/profiler/profiler.h

@@ -1,4 +1,6 @@
 #include <stdbool.h>
+#include <stdint.h>
+
 #ifndef _PROFILER_H
 #define _PROFILER_H
 
@@ -28,11 +30,28 @@ enum profile_name {
 };
 
 
+#define PROFILER_STAT_COUNT 5
+
+enum stat_name {
+    S_COMPILE,
+    S_COMPILE_SUCCESS,
+    S_RUN_INTERPRETED,
+    S_RUN_FROM_CACHE,
+    S_CACHE_MISMATCH,
+};
+
+struct profiler_stat {
+    int32_t count;
+} profiler_stat_arr[PROFILER_NAME_COUNT] = {{0}};
+
 void profiler_init();
 void profiler_start(enum profile_name name);
 void profiler_end(enum profile_name name);
 void profiler_print();
 
+void profiler_stat_increment(enum stat_name stat);
+int32_t profiler_stat_get(enum stat_name stat);
+
 // JS import
 double get_time();