diff --git a/src/lj_vmprofile.c b/src/lj_vmprofile.c index eb2ef48792..2d614ea62d 100644 --- a/src/lj_vmprofile.c +++ b/src/lj_vmprofile.c @@ -41,6 +41,21 @@ int vmprofile_get_profile_size() { return sizeof(VMProfile); } +/* Open a counter file on disk and returned the mmapped data structure. */ +void *vmprofile_open_file(const char *filename) +{ + int fd; + void *ptr = MAP_FAILED; + if (((fd = open(filename, O_RDWR|O_CREAT, 0666)) != -1) && + ((ftruncate(fd, sizeof(VMProfile))) != -1) && + ((ptr = mmap(NULL, sizeof(VMProfile), PROT_READ|PROT_WRITE, + MAP_SHARED, fd, 0)) != MAP_FAILED)) { + memset(ptr, 0, sizeof(VMProfile)); + } + if (fd != -1) close(fd); + return ptr == MAP_FAILED ? NULL : ptr; +} + /* Set the memory where the next samples will be counted. Size of the memory must match vmprofile_get_profile_size(). */ void vmprofile_set_profile(void *counters) { @@ -122,20 +137,12 @@ static void stop_timer() LUA_API int luaJIT_vmprofile_open(lua_State *L, const char *str) { - int fd; void *ptr; - if (((fd = open(str, O_RDWR|O_CREAT, 0666)) != -1) && - ((ftruncate(fd, sizeof(VMProfile))) != -1) && - ((ptr = mmap(NULL, sizeof(VMProfile), PROT_READ|PROT_WRITE, - MAP_SHARED, fd, 0)) != MAP_FAILED)) { - memset(ptr, 0, sizeof(VMProfile)); + if ((ptr = vmprofile_open_file(str)) != NULL) { setlightudV(L->base, checklightudptr(L, ptr)); } else { setnilV(L->base); } - if (fd != -1) { - close(fd); - } return 1; } diff --git a/src/lj_vmprofile.h b/src/lj_vmprofile.h index 2cdce69640..c9d023a072 100644 --- a/src/lj_vmprofile.h +++ b/src/lj_vmprofile.h @@ -6,6 +6,9 @@ #ifndef _LJ_VMPROFILE_H #define _LJ_VMPROFILE_H +#include +#include "lj_obj.h" + /* Counters are 64-bit to avoid overflow even in long running processes. */ typedef uint64_t VMProfileCount; @@ -30,6 +33,7 @@ typedef struct VMProfile { /* Functions that should be accessed via FFI. */ +void *vmprofile_open_file(const char *filename); void vmprofile_set_profile(void *counters); int vmprofile_get_profile_size(); diff --git a/src/luajit.c b/src/luajit.c index 2f45fa02f5..9fb96f5d09 100644 --- a/src/luajit.c +++ b/src/luajit.c @@ -16,6 +16,7 @@ #include "lauxlib.h" #include "lualib.h" #include "luajit.h" +#include "lj_vmprofile.h" #include "lj_arch.h" @@ -56,6 +57,7 @@ static void print_usage(void) " -j cmd Perform LuaJIT control command.\n" " -O[opt] Control LuaJIT optimizations.\n" " -i Enter interactive mode after executing " LUA_QL("script") ".\n" + " -p file Enable trace profiling to a VMProfile file.\n" " -v Show version information.\n" " -E Ignore environment variables.\n" " -- Stop handling options.\n" @@ -404,6 +406,7 @@ static int collectargs(char **argv, int *flags) *flags |= FLAGS_EXEC; case 'j': /* LuaJIT extension */ case 'l': + case 'p': /* RaptorJIT extension */ *flags |= FLAGS_OPTION; if (argv[i][2] == '\0') { i++; @@ -461,6 +464,17 @@ static int runargs(lua_State *L, char **argv, int argn) break; case 'b': /* LuaJIT extension. */ return dobytecode(L, argv+i); + case 'p': { + void *ptr; + if ((ptr = vmprofile_open_file(argv[++i])) != NULL) { + vmprofile_set_profile(ptr); + luaJIT_vmprofile_start(L); + } else { + fprintf(stderr, "unable to open vmprofile\n"); + fflush(stderr); + } + break; + } default: break; } }