Skip to content

Commit

Permalink
Merge branch 'raptorjit/master' into auditlog
Browse files Browse the repository at this point in the history
  • Loading branch information
lukego committed Dec 20, 2017
2 parents de0f442 + 02d596c commit c3b59a7
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"
#include "lj_auditlog.h"
Expand Down Expand Up @@ -57,6 +58,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"
" -a path Enable auditlog at path.\n"
Expand Down Expand Up @@ -407,6 +409,7 @@ static int collectargs(char **argv, int *flags)
case 'a': /* RaptorJIT extension */
case 'j': /* LuaJIT extension */
case 'l':
case 'p': /* RaptorJIT extension */
*flags |= FLAGS_OPTION;
if (argv[i][2] == '\0') {
i++;
Expand Down Expand Up @@ -469,6 +472,17 @@ static int runargs(lua_State *L, char **argv, int argn)
fprintf(stderr, "unable to open auditlog\n");
fflush(stderr);
}
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 c3b59a7

Please sign in to comment.