Skip to content

Commit

Permalink
Add '-p file' command line syntax for VMProfile
Browse files Browse the repository at this point in the history
Now it is possible to enable VMProfile trace profiling to a file from
the command line.
  • Loading branch information
lukego committed Dec 19, 2017
1 parent 8f94abb commit 7844c5f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/lj_vmprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/lj_vmprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifndef _LJ_VMPROFILE_H
#define _LJ_VMPROFILE_H

#include <stdint.h>
#include "lj_obj.h"

/* Counters are 64-bit to avoid overflow even in long running processes. */
typedef uint64_t VMProfileCount;

Expand All @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions src/luajit.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "luajit.h"
#include "lj_vmprofile.h"

#include "lj_arch.h"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 7844c5f

Please sign in to comment.