From 11d31571b3696be102e51775219afe29081f1e5c Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Thu, 17 Sep 2015 22:59:32 +0300
Subject: [PATCH] gh-872 (clock_gettime() bindings): review fixes

* rename 'time' to 'clock'
* remove time.bench64, I can't think of a use case
---
 CMakeLists.txt                         |  8 ++++-
 src/CMakeLists.txt                     |  2 +-
 src/lua/clock.lua                      | 40 +++++++++++++++++++++
 src/lua/init.cc                        |  4 +--
 src/lua/time.lua                       | 49 --------------------------
 test/app/{time.result => clock.result} |  2 +-
 test/app/clock.test.lua                | 16 +++++++++
 test/app/time.test.lua                 | 16 ---------
 8 files changed, 67 insertions(+), 70 deletions(-)
 create mode 100644 src/lua/clock.lua
 delete mode 100644 src/lua/time.lua
 rename test/app/{time.result => clock.result} (83%)
 create mode 100755 test/app/clock.test.lua
 delete mode 100755 test/app/time.test.lua

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb6e17c5a6..5a129577ed 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,6 +25,7 @@ find_program(LYNX lynx)
 find_program(CAT cat)
 find_program(GIT git)
 find_program(LD ld)
+find_program(CTAGS ctags)
 
 # Define PACKAGE macro in tarantool/config.h
 set (PACKAGE "Tarantool")
@@ -88,6 +89,10 @@ check_function_exists(fmemopen HAVE_FMEMOPEN)
 check_function_exists(funopen HAVE_FUNOPEN)
 check_function_exists(fopencookie HAVE_FOPENCOOKIE)
 check_function_exists(uuidgen HAVE_UUIDGEN)
+#
+# clock_gettime() is not defined on Mac OS X,
+# or own (slow) implementation is in third_party/clock_gettime.c
+#
 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
 
 # Checks for libev
@@ -106,8 +111,9 @@ check_library_exists("" __libc_stack_end "" HAVE_LIBC_STACK_END)
 #
 # Enable 'make tags' target.
 #
-add_custom_target(tags COMMAND ctags -R -f tags
+add_custom_target(tags COMMAND ${CTAGS} -R -f tags
     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
+add_custom_target(ctags DEPENDS tags)
 
 #
 # Define PACKAGE_VERSION -- a string constant with tarantool version.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d330b1b009..e2443e352b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -30,7 +30,7 @@ lua_source(lua_sources lua/tap.lua)
 lua_source(lua_sources lua/fio.lua)
 lua_source(lua_sources lua/csv.lua)
 lua_source(lua_sources lua/strict.lua)
-lua_source(lua_sources lua/time.lua)
+lua_source(lua_sources lua/clock.lua)
 lua_source(lua_sources ../third_party/luafun/fun.lua)
 # LuaJIT jit.* library
 lua_source(lua_sources "${CMAKE_BINARY_DIR}/third_party/luajit/src/jit/bc.lua")
diff --git a/src/lua/clock.lua b/src/lua/clock.lua
new file mode 100644
index 0000000000..60c78663ca
--- /dev/null
+++ b/src/lua/clock.lua
@@ -0,0 +1,40 @@
+-- clock.lua -- internal file
+local clock = {}
+local ffi = require('ffi')
+
+ffi.cdef[[
+    double clock_realtime(void);
+    double clock_monotonic(void);
+    double clock_process(void);
+    double clock_thread(void);
+    uint64_t clock_realtime64(void);
+    uint64_t clock_monotonic64(void);
+    uint64_t clock_process64(void);
+    uint64_t clock_thread64(void);
+]]
+
+local C = ffi.C
+
+clock.realtime = C.clock_realtime
+clock.monotonic = C.clock_monotonic
+clock.proc = C.clock_process
+clock.thread = C.clock_thread
+
+clock.realtime64 = C.clock_realtime64
+clock.monotonic64 = C.clock_monotonic64
+clock.proc64 = C.clock_process64
+clock.thread64 = C.clock_thread64
+
+clock.time = clock.realtime
+clock.time64 = clock.realtime64
+
+clock.bench = function(fun, ...)
+    local overhead = clock.proc()
+    overhead = clock.proc() - overhead
+    local start_time = clock.proc()
+    local res = {0, fun(...)}
+    res[1] = clock.proc() - start_time - overhead, res
+    return res
+end
+
+return clock
diff --git a/src/lua/init.cc b/src/lua/init.cc
index 70328eb172..032ad8e9a8 100644
--- a/src/lua/init.cc
+++ b/src/lua/init.cc
@@ -98,7 +98,7 @@ extern char strict_lua[],
 	dump_lua[],
 	csv_lua[],
 	v_lua[],
-	time_lua[];
+	clock_lua[];
 
 #if LUAJIT_VERSION_NUM >= 20100 /* LuaJIT 2.1+ */
 extern char p_lua[], zone_lua[];
@@ -118,7 +118,7 @@ static const char *lua_modules[] = {
 	"uri", uri_lua,
 	"fio", fio_lua,
 	"csv", csv_lua,
-	"time", time_lua,
+	"clock", clock_lua,
 	"socket", bsdsocket_lua,
 	"console", console_lua,
 	"tap", tap_lua,
diff --git a/src/lua/time.lua b/src/lua/time.lua
deleted file mode 100644
index 35f6afee6d..0000000000
--- a/src/lua/time.lua
+++ /dev/null
@@ -1,49 +0,0 @@
--- init.lua -- internal file
-local time = {}
-local ffi = require('ffi')
-
-ffi.cdef[[
-    double clock_realtime(void);
-    double clock_monotonic(void);
-    double clock_process(void);
-    double clock_thread(void);
-    uint64_t clock_realtime64(void);
-    uint64_t clock_monotonic64(void);
-    uint64_t clock_process64(void);
-    uint64_t clock_thread64(void);
-]]
-
-local C = ffi.C
-
-time.realtime = C.clock_realtime
-time.monotonic = C.clock_monotonic
-time.proc = C.clock_process
-time.thread = C.clock_thread
-
-time.realtime64 = C.clock_realtime64
-time.monotonic64 = C.clock_monotonic64
-time.proc64 = C.clock_process64
-time.thread64 = C.clock_thread64
-
-time.time = time.realtime
-time.time64 = time.realtime64
-
-time.bench = function(fun, ...)
-    local overhead = time.proc()
-    overhead = time.proc() - overhead
-    local start_time = time.proc()
-    local res = {0, fun(...)}
-    res[1] = time.proc() - start_time - overhead, res
-    return res
-end
-
-time.bench64 = function(fun, ...)
-    local overhead = time.proc64()
-    overhead = time.proc64() - overhead
-    local start_time = time.proc64()
-    local res = {0, fun(...)}
-    res[1] = time.proc64() - start_time - overhead, res
-    return res
-end
-
-return time
diff --git a/test/app/time.result b/test/app/clock.result
similarity index 83%
rename from test/app/time.result
rename to test/app/clock.result
index ab0357af1a..bc3cba192b 100644
--- a/test/app/time.result
+++ b/test/app/clock.result
@@ -9,4 +9,4 @@ ok - thread64
 ok - monotonic64
 ok - proc64
 ok - time is monotonic
-ok - time.realtime ~ os.time
+ok - clock.realtime ~ os.time
diff --git a/test/app/clock.test.lua b/test/app/clock.test.lua
new file mode 100755
index 0000000000..dbfaddd79d
--- /dev/null
+++ b/test/app/clock.test.lua
@@ -0,0 +1,16 @@
+#!/usr/bin/env tarantool
+
+clock = require("clock")
+test = require("tap").test("csv")
+test:plan(9)
+test:ok(clock.realtime() > 0, "realtime")
+test:ok(clock.thread() > 0, "thread")
+test:ok(clock.monotonic() > 0, "monotonic")
+test:ok(clock.proc() > 0, "proc")
+test:ok(clock.realtime64() > 0, "realtime64")
+test:ok(clock.thread64() > 0, "thread64")
+test:ok(clock.monotonic64() > 0, "monotonic64")
+test:ok(clock.proc64() > 0, "proc64")
+
+test:ok(clock.monotonic() < clock.monotonic(), "time is monotonic")
+test:ok(math.abs(clock.realtime() - os.time()) < 2, "clock.realtime ~ os.time")
diff --git a/test/app/time.test.lua b/test/app/time.test.lua
deleted file mode 100755
index 9c172d13d7..0000000000
--- a/test/app/time.test.lua
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/usr/bin/env tarantool
-
-time = require("time")
-test = require("tap").test("csv")
-test:plan(9)
-test:ok(time.realtime() > 0, "realtime")
-test:ok(time.thread() > 0, "thread")
-test:ok(time.monotonic() > 0, "monotonic")
-test:ok(time.proc() > 0, "proc")
-test:ok(time.realtime64() > 0, "realtime64")
-test:ok(time.thread64() > 0, "thread64")
-test:ok(time.monotonic64() > 0, "monotonic64")
-test:ok(time.proc64() > 0, "proc64")
-
-test:ok(time.monotonic() < time.monotonic(), "time is monotonic")
-test:ok(math.abs(time.realtime() - os.time()) < 2, "time.realtime ~ os.time")
-- 
GitLab