Skip to content

Commit

Permalink
lj_auditlog: Improve logging of trace stop/abort
Browse files Browse the repository at this point in the history
* Log jit_State with trace_stop event.
* Add jit_State.final flag to show root/side blacklisting.
* Remove redundant trace_abort logs.
  • Loading branch information
lukego committed Nov 29, 2017
1 parent fdf39d8 commit 238c698
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
34 changes: 25 additions & 9 deletions src/lj_auditlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void log_event(const char *type, int nattributes) {
/* Log objects that define the virtual machine. */
void lj_auditlog_vm_definitions()
{
log_mem("lj_ir_mode", &lj_ir_mode, sizeof(lj_ir_mode));
log_mem("lj_ir_mode", (void*)&lj_ir_mode, sizeof(lj_ir_mode));
}

/* Ensure that the log file is open. */
Expand All @@ -76,24 +76,40 @@ static void ensure_log_open() {

/* -- high-level LuaJIT object logging ------------------------------------ */

/* Log a trace that has just been compiled. */
void lj_auditlog_trace_stop(jit_State *J, GCtrace *T)
static void log_jit_State(jit_State *J)
{
log_mem("BCRecLog[]", J->bclog, J->nbclog * sizeof(*J->bclog));
log_mem("jit_State", J, sizeof(*J));
}

static void log_GCtrace(GCtrace *T)
{
ensure_log_open();
log_mem("GCtrace", T, sizeof(*T));
log_mem("MCode[]", T->mcode, T->szmcode);
log_mem("SnapShot[]", T->snap, T->nsnap * sizeof(*T->snap));
log_mem("SnapEntry[]", T->snapmap, T->nsnapmap * sizeof(*T->snapmap));
log_mem("IRIns[]", &T->ir[T->nk], (T->nins - T->nk + 1) * sizeof(IRIns));
log_event("trace_stop", 1);
str_16("GCtrace"); /* = */ uint_64((uint64_t)T);
log_mem("GCtrace", T, sizeof(*T));
}

/* API functions */

/* Log a trace that has just been compiled. */
void lj_auditlog_trace_stop(jit_State *J, GCtrace *T)
{
ensure_log_open();
log_jit_State(J);
log_GCtrace(T);
log_event("trace_stop", 2);
str_16("GCtrace"); /* = */ uint_64((uint64_t)T);
str_16("jit_State"); /* = */ uint_64((uint64_t)J);
}

void lj_auditlog_trace_abort(jit_State *J, TraceError e) {
ensure_log_open();
log_mem("jit_State", J, sizeof(*J));
log_jit_State(J);
log_GCtrace(&J->cur);
log_event("trace_abort", 2);
str_16("jit_State"); /* = */ uint_64((uint64_t)J);
str_16("TraceError"); /* = */ uint_64(e);
str_16("jit_State"); /* = */ uint_64((uint64_t)J);
}

2 changes: 0 additions & 2 deletions src/lj_auditlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#include "lj_trace.h"

void lj_auditlog_trace_flush(jit_State *J);
void lj_auditlog_trace_start(jit_State *J);
void lj_auditlog_trace_stop(jit_State *J, GCtrace *T);
void lj_auditlog_trace_abort(jit_State *J, TraceError e);
void lj_auditlog_trace_record_bytecode(jit_State *J);

#endif
2 changes: 1 addition & 1 deletion src/lj_jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ typedef struct jit_State {
size_t szallmcarea; /* Total size of all allocated mcode areas. */

TValue errinfo; /* Additional info element for trace errors. */

int8_t final; /* True if trace error is final. */
}
jit_State;

Expand Down
27 changes: 22 additions & 5 deletions src/lj_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "lj_obj.h"


#include "lj_auditlog.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_debug.h"
Expand Down Expand Up @@ -308,8 +307,8 @@ static void blacklist_pc(GCproto *pt, BCIns *pc)
pt->flags |= PROTO_ILOOP;
}

/* Penalize a bytecode instruction. */
static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e)
/* Penalize a bytecode instruction. Return true when blacklisted. */
static int penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e)
{
uint32_t i, val = PENALTY_MIN;
for (i = 0; i < PENALTY_SLOTS; i++)
Expand All @@ -319,7 +318,7 @@ static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e)
LJ_PRNG_BITS(J, PENALTY_RNDBITS);
if (val > PENALTY_MAX) {
blacklist_pc(pt, pc); /* Blacklist it, if that didn't help. */
return;
return 1;
}
goto setpenalty;
}
Expand All @@ -331,8 +330,20 @@ static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e)
J->penalty[i].val = (uint16_t)val;
J->penalty[i].reason = e;
hotcount_set(J2GG(J), pc+1, val);
return 0;
}

/* Check if this is the last attempt to compile a side trace.
** (If so the next attempt will just record a fallback to the interpreter.)
**/
static int last_try(jit_State *J)
{
GCtrace *parent = traceref(J, J->parent);
int count = parent->snap[J->exitno].count;
return count+1 >= J->param[JIT_P_hotexit] + J->param[JIT_P_tryside];
}


/* -- Trace compiler state machine ---------------------------------------- */

/* Start tracing. */
Expand Down Expand Up @@ -483,19 +494,25 @@ static int trace_abort(jit_State *J)
J->state = LJ_TRACE_ASM;
return 1; /* Retry ASM with new MCode area. */
}

/* Penalize or blacklist starting bytecode instruction. */
if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) {
if (J->exitno == 0) {
BCIns *startpc = mref(J->cur.startpc, BCIns);
if (e == LJ_TRERR_RETRY)
hotcount_set(J2GG(J), startpc+1, 1); /* Immediate retry. */
else
penalty_pc(J, &gcref(J->cur.startpt)->pt, startpc, e);
J->final = penalty_pc(J, &gcref(J->cur.startpt)->pt, startpc, e);
} else {
traceref(J, J->exitno)->link = J->exitno; /* Self-link is blacklisted. */
}
}

/* Is this the last attempt at a side trace? */
if (J->parent && last_try(J)) J->final = 1;

lj_auditlog_trace_abort(J, e);

/* Is there anything to abort? */
traceno = J->cur.traceno;
if (traceno) {
Expand Down

0 comments on commit 238c698

Please sign in to comment.