From 3306281dcc77de7560ecec7ac286085a01422152 Mon Sep 17 00:00:00 2001 From: jyelon Date: Wed, 5 Jan 2022 13:24:12 -0500 Subject: [PATCH] use the linux monotonic clock instead of gettimeofday --- luprex/core/cpp/driver-linux.cpp | 39 ++++++++++++++++++++++---------- luprex/core/cpp/util.cpp | 10 -------- luprex/core/cpp/util.hpp | 4 ++-- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/luprex/core/cpp/driver-linux.cpp b/luprex/core/cpp/driver-linux.cpp index 8bcd12bb..d11e9ac8 100644 --- a/luprex/core/cpp/driver-linux.cpp +++ b/luprex/core/cpp/driver-linux.cpp @@ -117,6 +117,32 @@ SOCKET listen_on_port(int port, std::string &err) { return sock; } +class MonoClock { +private: + struct timespec base_; +public: + MonoClock() { + int status = clock_gettime(CLOCK_MONOTONIC, &base_); + assert(status == 0); + } + double get() { + struct timespec t; + int status = clock_gettime(CLOCK_MONOTONIC, &t); + assert(status == 0); + double tv_sec = t.tv_sec - base_.tv_sec; + double tv_nsec = t.tv_nsec - base_.tv_nsec; + return tv_sec + (tv_nsec * 1.0E-9); + } +}; + +static MonoClock monoclock; + +namespace util { + double profiling_clock() { + return monoclock.get(); + } +} + class Driver { public: enum ChanState { @@ -135,15 +161,6 @@ public: bool short_sleep_; std::map listen_sockets_; std::unique_ptr chbuf; - int64_t basetime_; - - int64_t get_now() { - struct timeval tv; - gettimeofday(&tv, nullptr); - int64_t tv_sec = tv.tv_sec; - int64_t tv_usec = tv.tv_usec; - return tv_sec * 1000000 + tv_usec; - } void init(DrivenEngine *de) { driven_ = de; @@ -228,8 +245,7 @@ public: } void handle_clock() { - int64_t now = get_now() - basetime_; - driven_->drv_set_clock(double(now) / 1000000.0); + driven_->drv_set_clock(monoclock.get()); } void close_channel(int chid, const std::string err) { @@ -340,7 +356,6 @@ public: enableRawMode(); init(de); DrivenEngine::set(de); - basetime_ = get_now(); driven_->drv_set_lua_source(util::read_lua_source("lua")); driven_->drv_invoke_event_init(argc, argv); handle_listen_ports(); diff --git a/luprex/core/cpp/util.cpp b/luprex/core/cpp/util.cpp index 5ef0f848..1e56a5d7 100644 --- a/luprex/core/cpp/util.cpp +++ b/luprex/core/cpp/util.cpp @@ -20,16 +20,6 @@ namespace util { -int64_t profiling_clock() { -#ifdef WIN32 - return 0.0; -#else - struct timespec ts; - assert(0 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); - return int64_t(ts.tv_nsec) + (int64_t(ts.tv_sec)*int64_t(1000000000)); -#endif -} - static bool ascii_isalpha(char c) { return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); } diff --git a/luprex/core/cpp/util.hpp b/luprex/core/cpp/util.hpp index 999d0b56..e0f905c0 100644 --- a/luprex/core/cpp/util.hpp +++ b/luprex/core/cpp/util.hpp @@ -37,8 +37,8 @@ using LuaSourcePtr = std::unique_ptr; using HashValue = std::pair; using IdVector = std::vector; -// Return nanoseconds elapsed, for profiling purposes. -int64_t profiling_clock(); +// Return seconds elapsed, for profiling purposes. +double profiling_clock(); // Return true if the string is a valid lua identifier. bool is_identifier(const std::string &str);