Skip to content

Commit

Permalink
simple GC benchmark and mlockall/munlockall
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Klotzbuecher committed May 29, 2009
1 parent d14c592 commit 8e33206
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 14 deletions.
8 changes: 5 additions & 3 deletions Makefile
@@ -1,12 +1,14 @@
CC=gcc
CFLAGS=-lrt -Wall
#INCLUDES=$(shell lua-config50 --include)
#LIBS=$(shell lua-config50 --libs)
#INCLUDES=$(shell lua-config --include)
#LIBS=$(shell lua-config --libs)
INCLUDE=-I/usr/include/lua5.1/
# LIBS=-llua

all: rtposix.so

rtposix.o: rtposix.c
${CC} ${CFLAGS} -fpic -c rtposix.c -o $@
${CC} ${CFLAGS} ${INCLUDE} -fpic -c rtposix.c -o $@

rtposix.so: rtposix.o
${CC} ${CFLAGS} -shared ${INCLUDES} ${LIBS} rtposix.o -o rtposix.so
Expand Down
116 changes: 116 additions & 0 deletions gc-test.lua
@@ -0,0 +1,116 @@
require("rtposix")

function gen_garbage(num, tabsize)
for n = 1,num do
local t = {}
for i = 1,tabsize do
table.insert(t,i)
end
t = {}
end
end

-- helpers
function timersub(t1, t0)
local res = {}
if t1.sec == t0.sec then
res.sec = 0
res.nsec = t1.nsec - t0.nsec
else
res.sec = t1.sec - t0.sec
res.nsec = t1.nsec + 1000000 - t0.sec

if res.nsec >= 1000000 then
res.sec = res.sec + 1
res.nsec = res.nsec - 1000000
end
end
return res
end

function timercmp(t1, t2)
if(t1.sec > t2.sec) then
return 1
elseif (t1.sec < t2.sec) then
return -1
elseif (t1.nsec > t2.nsec) then
return 1
elseif (t1.nsec < t2.nsec) then
return -1
else
return 0
end
end

function tv2str(t)
return t.sec .. "s" .." " .. t.nsec .. "ns"
end

function print_stat(s)
print("type: " .. s.type .. ", duration: " .. tv2str(s.dur) .. ", collected: " .. s.mem0 - s.mem1 .. " (" .. s.mem0 .. "/" .. s.mem1 ..")")
end

-- perform a time gc run
-- type is "collect" for full or "step" for incremental
function timed_gc(type)
local stat = {}
local t0 = {}
local t1 = {}

stat.type = type
stat.mem0 = collectgarbage("count")

t0 = rtposix.gettime("MONOTONIC")
collectgarbage("collect")
t1 = rtposix.gettime("MONOTONIC")

stat.mem1 = collectgarbage("count")
stat.dur = timersub(t1,t0)

return stat
end

function stop_gc()
collectgarbage("stop")
print("stopped GC")
end

function start_gc()
print("starting GC")
collectgarbage("start")
end

-- initalize things
stats = {}
stats.dur_min = { sec=math.huge, nsec=math.huge }
stats.dur_max = { sec=0, nsec=0 }

stop_gc()
print("doing initial full collect")
print_stat(timed_gc("collect"))

for i = 1,10000 do
gen_garbage(100, 1000)
s = timed_gc("step")

if timercmp(s.dur, stats.dur_min) < 0 then
stats.dur_min = s.dur
end

if timercmp(s.dur, stats.dur_max) > 0 then
stats.dur_max = s.dur
end

-- print_stat(s)
end

print("Statistics")
print("max duration: " .. tv2str(stats.dur_max))
print("min duration: " .. tv2str(stats.dur_min))


print("doing final full collect")
print_stat(timed_gc("collect"))



36 changes: 36 additions & 0 deletions rtposix-test.lua
@@ -0,0 +1,36 @@
require("rtposix")
dofile("../mylib/misc.lua")

-- clock_getres
print("clock_getres")
pp_table(rtposix.getres("REALTIME"))

-- nanosleep
print("nanosleep")
sec=0
nsec=100000000
print("sleeing for sec=" .. sec .. ", nsec=" .. nsec .. ", retval=", rtposix.nanosleep("REALTIME", "rel", sec, nsec))

-- gettime
print("gettime (sleeping 100ms)")
for i=1,3 do
t0 = rtposix.gettime("MONOTONIC")
rtposix.nanosleep("REALTIME", "rel", sec, nsec)
t1 = rtposix.gettime("MONOTONIC")
print("t0: sec=" .. t0.sec .. ", nsec=" .. t0.nsec)
print("t1: sec=" .. t1.sec .. ", nsec=" .. t1.nsec)
end

-- mlockall/munlockall

print("mlockall: ", rtposix.mlockall("MCL_CURRENT"))

t = {}
for i = 1,100 do
table.insert(t, i)
end

print("munlockall: ", rtposix.munlockall())



62 changes: 60 additions & 2 deletions rtposix.c
@@ -1,5 +1,7 @@
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h> /* mlockall etc. */

#ifdef __cplusplus
extern "C" {
Expand All @@ -9,13 +11,18 @@ extern "C" {
#include <lauxlib.h>
#include <lualib.h>

int luaopen_rtposix(lua_State *L);
int luaopen_rtposix(lua_State *L);

#ifdef __cplusplus
}
#endif

/* helpers */

#define ERRBUF_LEN 30

/*
* helpers
*/

static int clock_num_to_id(const char *name)
{
Expand Down Expand Up @@ -86,6 +93,7 @@ static int lua_getres(lua_State *L)

return 1;
}

/* args: clock_id, flags (rel|abs), sec, nsec */
static int lua_nanosleep(lua_State *L)
{
Expand Down Expand Up @@ -119,11 +127,61 @@ static int lua_nanosleep(lua_State *L)
return 1;
}

/*
* mlockall
*
* args: flag MCL_CURRENT | MCL_FUTURE
*/

static int lua_mlockall(lua_State *L)
{
const char *str_flag;
char errbuf[ERRBUF_LEN];
int flag, ret;

flag = 0;
str_flag = luaL_checkstring(L, 1);

if(!strcmp(str_flag, "MCL_CURRENT"))
flag |= MCL_CURRENT;

if(!strcmp(str_flag, "MCL_FUTURE"))
flag |= MCL_FUTURE;

ret = mlockall(flag);

if(ret < 0) {
strerror_r(errno, errbuf, ERRBUF_LEN);
luaL_error(L, errbuf);
}

lua_pushboolean(L, 1);
return 1;
}

static int lua_munlockall(lua_State *L)
{
int ret;
char errbuf[ERRBUF_LEN];

ret = munlockall();

if(ret < 0) {
strerror_r(errno, errbuf, ERRBUF_LEN);
luaL_error(L, errbuf);
}

lua_pushboolean(L, 1);
return 1;
}


static const struct luaL_Reg rtposix [] = {
{"gettime", lua_gettime},
{"getres", lua_getres},
{"nanosleep", lua_nanosleep},
{"mlockall", lua_mlockall},
{"munlockall", lua_munlockall},
{NULL, NULL}
};

Expand Down
9 changes: 0 additions & 9 deletions test.lua

This file was deleted.

0 comments on commit 8e33206

Please sign in to comment.