gccjit-ravi534
Dibyendu Majumdar 8 years ago
parent 36c99300a2
commit 0399d6a163

@ -160,6 +160,11 @@ typedef struct global_State {
ravi_Writestring ravi_writestring;
ravi_Writestringerror ravi_writestringerror;
void * ravi_debugger_data;
#if RAVI_BYTECODE_PROFILING_ENABLED
unsigned long long *ravi_profile1;
unsigned long long *ravi_profile2;
unsigned long long ravi_prev_time;
#endif
} global_State;

@ -545,6 +545,7 @@ LUA_API void ravi_set_debuglevel(int level);
#define RAVI_DEBUG_STACK(p) if ((ravi_parser_debug & 8) != 0) {p;} else {}
#define RAVI_ENABLED 1
#define RAVI_BYTECODE_PROFILING_ENABLED 0

@ -4,10 +4,12 @@
#include "lua.h"
#include "lopcodes.h"
LUAI_DDEC unsigned long long raviV_profiledata[NUM_OPCODES];
#if RAVI_BYTECODE_PROFILING_ENABLED
LUAI_FUNC void raviV_init_profiledata(void);
LUAI_FUNC void raviV_add_profiledata(OpCode opcode);
LUAI_FUNC void raviV_print_profiledata(void);
LUAI_FUNC void raviV_init_profiledata(lua_State *L);
LUAI_FUNC void raviV_add_profiledata(lua_State *L, OpCode opcode);
LUAI_FUNC void raviV_destroy_profiledata(lua_State *L);
#endif
#endif

@ -364,7 +364,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
close_state(L);
L = NULL;
}
raviV_init_profiledata();
#if RAVI_BYTECODE_PROFILING_ENABLED
raviV_init_profiledata(L);
#endif
return L;
}
@ -372,8 +374,10 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
LUA_API void lua_close (lua_State *L) {
L = G(L)->mainthread; /* only the main thread can be closed */
lua_lock(L);
#if RAVI_BYTECODE_PROFILING_ENABLED
raviV_destroy_profiledata(L);
#endif
close_state(L);
raviV_print_profiledata();
}

@ -960,7 +960,9 @@ int luaV_execute (lua_State *L) {
Protect(luaG_traceexec(L));
/* WARNING: several calls may realloc the stack and invalidate 'ra' */
OpCode op = GET_OPCODE(i);
if (prevop != -1) raviV_add_profiledata(prevop);
#if RAVI_BYTECODE_PROFILING_ENABLED
if (prevop != -1) raviV_add_profiledata(L, prevop);
#endif
prevop = op;
#if 0
RAVI_DEBUG_STACK(
@ -1359,7 +1361,9 @@ int luaV_execute (lua_State *L) {
in JIT mode (see how b is handled in OP_CALL JIT implementation)
or via luaD_precall() if a JITed function is invoked (see
ldo.c for how luaD_precall() handles this */
raviV_add_profiledata(op);
#if RAVI_BYTECODE_PROFILING_ENABLED
raviV_add_profiledata(L, op);
#endif
return b; /* external invocation: return */
}
else { /* invocation via reentry: continue execution */
@ -1367,7 +1371,9 @@ int luaV_execute (lua_State *L) {
if (b) L->top = ci->top;
lua_assert(isLua(ci));
lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
raviV_add_profiledata(op);
#if RAVI_BYTECODE_PROFILING_ENABLED
raviV_add_profiledata(L, op);
#endif
goto newframe; /* restart luaV_execute over new Lua function */
}
}

@ -1,57 +1,78 @@
#include "ravi_profile.h"
#include <lstate.h>
#include <stdint.h>
#if RAVI_BYTECODE_PROFILING_ENABLED
#ifdef _WIN32
#include <windows.h>
unsigned long long raviV_profiledata[NUM_OPCODES];
/* The number of nanoseconds in one second. */
#define UV__NANOSEC 1000000000
/* Interval (in seconds) of the high-resolution clock. */
static double hrtime_interval_ = 0;
static uint64_t prev_time = 0;
void raviV_init_profiledata(void) {
void raviV_init_profiledata(lua_State *L) {
LARGE_INTEGER perf_frequency;
/* Retrieve high-resolution timer frequency
* and precompute its reciprocal.
*/
if (QueryPerformanceFrequency(&perf_frequency)) {
hrtime_interval_ = 1.0 / perf_frequency.QuadPart;
} else {
hrtime_interval_= 0;
}
else {
hrtime_interval_ = 0;
}
global_State *g = G(L);
g->ravi_profile1 = (unsigned long long *)(calloc(NUM_OPCODES, sizeof(unsigned long long)));
g->ravi_profile2 = (unsigned long long *)(calloc(NUM_OPCODES, sizeof(unsigned long long)));
g->ravi_prev_time = 0;
}
void raviV_add_profiledata(OpCode opcode) {
void raviV_add_profiledata(lua_State *L, OpCode opcode) {
LARGE_INTEGER counter;
/* If the performance interval is zero, there's no support. */
if (hrtime_interval_ == 0) {
return;
}
if (hrtime_interval_ == 0) { return; }
if (!QueryPerformanceCounter(&counter)) {
return;
}
if (!QueryPerformanceCounter(&counter)) { return; }
/* Because we have no guarantee about the order of magnitude of the
* performance counter interval, integer math could cause this computation
* to overflow. Therefore we resort to floating point math.
*/
uint64_t this_time = (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * UV__NANOSEC);
raviV_profiledata[opcode] += (this_time - (prev_time == 0 ? this_time : prev_time));
prev_time = this_time;
uint64_t this_time =
(uint64_t)((double)counter.QuadPart * hrtime_interval_ * UV__NANOSEC);
global_State *g = G(L);
g->ravi_profile1[opcode] +=
(this_time - (g->ravi_prev_time == 0 ? this_time : g->ravi_prev_time));
g->ravi_prev_time = this_time;
g->ravi_profile2[opcode]++;
}
void raviV_print_profiledata(void) {
for (int i = 0; i < NUM_OPCODES; i++) {
printf("PerfStat [%s] %llu\n", luaP_opnames[i], raviV_profiledata[i]);
}
}
void raviV_destroy_profiledata(lua_State *L) {
global_State *g = G(L);
for (int i = 0; i < NUM_OPCODES; i++) {
printf("PerfStat [%s] %llu, %llu\n", luaP_opnames[i], g->ravi_profile1[i], g->ravi_profile2[i]);
}
free(g->ravi_profile1);
free(g->ravi_profile2);
}
#else
void raviV_init_profiledata(lua_State *L) {
/* Not implemented */
}
void raviV_add_profiledata(lua_State *L, OpCode opcode) {
/* Not implemented */
}
void raviV_destroy_profiledata(lua_State *L) {
/* Not implemented */
}
#endif
#endif

Loading…
Cancel
Save