From 65675f511bcaaa9fc4cea7443c792d4df6e6b6cc Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Sun, 18 Jun 2017 09:23:44 +0000 Subject: [PATCH] lj_vmprofile: Port & adapt revisions Adapt to RaptorJIT style e.g. assume the feature is enabled without needing the #ifdef. Port improvement from Snabb LuaJIT branch: tracking GC time separately for each trace. --- src/lib_jit.c | 5 ----- src/lj_gc.c | 2 ++ src/lj_obj.h | 3 ++- src/lj_vmprofile.c | 16 ++++++++-------- src/lj_vmprofile.h | 3 ++- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/lib_jit.c b/src/lib_jit.c index 362e52b1c6..d019b252d0 100644 --- a/src/lib_jit.c +++ b/src/lib_jit.c @@ -200,11 +200,8 @@ LJLIB_CF(jit_opt_start) #include "lj_libdef.h" - /* -- jit.vmprofile module ----------------------------------------------- */ -#ifdef LUAJIT_VMPROFILE - #define LJLIB_MODULE_jit_vmprofile LJLIB_CF(jit_vmprofile_start) @@ -227,8 +224,6 @@ static int luaopen_jit_vmprofile(lua_State *L) return 1; } -#endif - /* -- JIT compiler initialization ----------------------------------------- */ /* Default values for JIT parameters. */ diff --git a/src/lj_gc.c b/src/lj_gc.c index c4a9ab5803..7347b23518 100644 --- a/src/lj_gc.c +++ b/src/lj_gc.c @@ -648,6 +648,7 @@ int lj_gc_step(lua_State *L) global_State *g = G(L); GCSize lim; int32_t ostate = g->vmstate; + g->gcvmstate = ostate; setvmstate(g, GC); lim = (GCSTEPSIZE/100) * g->gc.stepmul; if (lim == 0) @@ -698,6 +699,7 @@ void lj_gc_fullgc(lua_State *L) { global_State *g = G(L); int32_t ostate = g->vmstate; + g->gcvmstate = ostate; setvmstate(g, GC); if (g->gc.state <= GCSatomic) { /* Caught somewhere in the middle. */ setmref(g->gc.sweep, &g->gc.root); /* Sweep everything (preserving it). */ diff --git a/src/lj_obj.h b/src/lj_obj.h index 12403d503e..4025177843 100644 --- a/src/lj_obj.h +++ b/src/lj_obj.h @@ -516,7 +516,8 @@ typedef struct global_State { lua_Alloc allocf; /* Memory allocator. */ void *allocd; /* Memory allocator data. */ GCState gc; /* Garbage collector. */ - volatile int32_t vmstate; /* VM state or current JIT code trace number. */ + volatile int32_t vmstate; /* VM state or current JIT code trace number. */ + volatile int32_t gcvmstate; /* Previous VM state (only when state is GC). */ SBuf tmpbuf; /* Temporary string buffer. */ GCstr strempty; /* Empty string. */ uint8_t stremptyz; /* Zero terminator of empty string. */ diff --git a/src/lj_vmprofile.c b/src/lj_vmprofile.c index 3025cc5d39..3f625761e7 100644 --- a/src/lj_vmprofile.c +++ b/src/lj_vmprofile.c @@ -6,8 +6,6 @@ #define lj_vmprofile_c #define LUA_CORE -#ifdef LUAJIT_VMPROFILE - #define _GNU_SOURCE 1 #include #include @@ -42,7 +40,7 @@ int vmprofile_get_profile_size() { void vmprofile_set_profile(void *counters) { profile = (VMProfile*)counters; profile->magic = 0x1d50f007; - profile->major = 1; + profile->major = 2; profile->minor = 0; } @@ -54,16 +52,19 @@ static void vmprofile_signal(int sig, siginfo_t *si, void *data) if (profile != NULL) { lua_State *L = gco2th(gcref(state.g->cur_L)); int vmstate = state.g->vmstate; + int trace = ~vmstate == LJ_VMST_GC ? state.g->gcvmstate : vmstate; /* Not in a trace */ - if (vmstate < 0) { + if (trace < 0) { profile->vm[~vmstate]++; } else { - int bucket = vmstate > LJ_VMPROFILE_TRACE_MAX ? 0 : vmstate; + int bucket = trace > LJ_VMPROFILE_TRACE_MAX ? 0 : trace; VMProfileTraceCount *count = &profile->trace[bucket]; - GCtrace *T = traceref(L2J(L), (TraceNo)vmstate); + GCtrace *T = traceref(L2J(L), (TraceNo)trace); intptr_t ip = (intptr_t)((ucontext_t*)data)->uc_mcontext.gregs[REG_RIP]; ptrdiff_t mcposition = ip - (intptr_t)T->mcode; - if ((mcposition < 0) || (mcposition >= T->szmcode)) { + if (~vmstate == LJ_VMST_GC) { + count->gc++; + } else if ((mcposition < 0) || (mcposition >= T->szmcode)) { count->other++; } else if ((T->mcloop != 0) && (mcposition >= T->mcloop)) { count->loop++; @@ -110,4 +111,3 @@ LUA_API void luaJIT_vmprofile_stop(lua_State *L) stop_timer(); } -#endif diff --git a/src/lj_vmprofile.h b/src/lj_vmprofile.h index e846306f3b..b03c75e109 100644 --- a/src/lj_vmprofile.h +++ b/src/lj_vmprofile.h @@ -19,12 +19,13 @@ typedef struct VMProfileTraceCount { VMProfileCount head; /* Head of the trace (non-looping part) */ VMProfileCount loop; /* Loop of the trace */ VMProfileCount other; /* Outside the trace mcode (unidentified) */ + VMProfileCount gc; /* Garbage collection from this trace. */ } VMProfileTraceCount; /* Complete set of counters for VM and traces. */ typedef struct VMProfile { uint32_t magic; /* 0x1d50f007 */ - uint16_t major, minor; /* 1, 0 */ + uint16_t major, minor; /* 2, 0 */ VMProfileCount vm[LJ_VMST__MAX]; VMProfileTraceCount trace[LJ_VMPROFILE_TRACE_MAX+1]; } VMProfile;