summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwilliam <[email protected]>2014-02-02 17:59:48 -0800
committerwilliam <[email protected]>2014-02-02 17:59:48 -0800
commitff5055150c526141ce670715ba13e41dd0bf3f16 (patch)
tree2eaa0867e9e09746384ad913cc7b8e54885d7b8a
parenta605dc78b1e634d76ce5a3ae087676862c9576ea (diff)
use system monotonic time instead of clock()
-rw-r--r--Makefile7
-rw-r--r--bench.c28
2 files changed, 33 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 9fd5814..9fbaa7f 100644
--- a/Makefile
+++ b/Makefile
@@ -34,8 +34,13 @@ else
SOFLAGS = -shared
endif
+ifeq ($(shell -uname -s), Linux)
+LIBS = -lrt
+endif
+
+
bench.so: bench.c
- $(CC) -o $@ $< $(CPPFLAGS) -DLUA_COMPAT_ALL $(CFLAGS) -Wno-unused-function $(SOFLAGS)
+ $(CC) -o $@ $< $(CPPFLAGS) -DLUA_COMPAT_ALL $(CFLAGS) -Wno-unused-function $(SOFLAGS) $(LIBS)
bench-wheel8.so: CPPFLAGS+=-DWHEEL_BIT=3 -DWHEEL_NUM=$(WHEEL_NUM)
diff --git a/bench.c b/bench.c
index 246895f..a38c46c 100644
--- a/bench.c
+++ b/bench.c
@@ -5,6 +5,10 @@
#include <unistd.h>
#include <dlfcn.h>
+#if __APPLE__
+#include <mach/mach_time.h>
+#endif
+
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
@@ -24,11 +28,29 @@ struct bench {
struct timeout *timeout;
struct benchops ops;
timeout_t curtime;
+
+#if __APPLE__
+ mach_timebase_info_data_t timebase;
+#endif
}; /* struct bench */
static int bench_clock(lua_State *L) {
- lua_pushnumber(L, (double)clock() / CLOCKS_PER_SEC);
+#if __APPLE__
+ struct bench *B = lua_touserdata(L, 1);
+ unsigned long long abt;
+
+ abt = mach_absolute_time();
+ abt = abt * B->timebase.numer / B->timebase.denom;
+
+ lua_pushnumber(L, (double)abt / 1000000000L);
+#else
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+
+ lua_pushnumber(L, (double)ts.tv_sec + ((double)ts.tv_nsec / 1000000000L));
+#endif
return 1;
} /* bench_clock() */
@@ -45,6 +67,10 @@ static int bench_new(lua_State *L) {
B = lua_newuserdata(L, sizeof *B);
memset(B, 0, sizeof *B);
+#if __APPLE__
+ mach_timebase_info(&B->timebase);
+#endif
+
luaL_getmetatable(L, "BENCH*");
lua_setmetatable(L, -2);