Skip to content

Commit

Permalink
lj_vmprofile: Port & adapt revisions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukego committed Jun 18, 2017
1 parent 24f82aa commit 65675f5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/lib_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -227,8 +224,6 @@ static int luaopen_jit_vmprofile(lua_State *L)
return 1;
}

#endif

/* -- JIT compiler initialization ----------------------------------------- */

/* Default values for JIT parameters. */
Expand Down
2 changes: 2 additions & 0 deletions src/lj_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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). */
Expand Down
3 changes: 2 additions & 1 deletion src/lj_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
16 changes: 8 additions & 8 deletions src/lj_vmprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#define lj_vmprofile_c
#define LUA_CORE

#ifdef LUAJIT_VMPROFILE

#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>
Expand Down Expand Up @@ -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;
}

Expand All @@ -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++;
Expand Down Expand Up @@ -110,4 +111,3 @@ LUA_API void luaJIT_vmprofile_stop(lua_State *L)
stop_timer();
}

#endif
3 changes: 2 additions & 1 deletion src/lj_vmprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 65675f5

Please sign in to comment.