use the linux monotonic clock instead of gettimeofday

This commit is contained in:
2022-01-05 13:24:12 -05:00
parent 0833662539
commit 3306281dcc
3 changed files with 29 additions and 24 deletions

View File

@@ -117,6 +117,32 @@ SOCKET listen_on_port(int port, std::string &err) {
return sock; 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 { class Driver {
public: public:
enum ChanState { enum ChanState {
@@ -135,15 +161,6 @@ public:
bool short_sleep_; bool short_sleep_;
std::map<int, SOCKET> listen_sockets_; std::map<int, SOCKET> listen_sockets_;
std::unique_ptr<char[]> chbuf; std::unique_ptr<char[]> 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) { void init(DrivenEngine *de) {
driven_ = de; driven_ = de;
@@ -228,8 +245,7 @@ public:
} }
void handle_clock() { void handle_clock() {
int64_t now = get_now() - basetime_; driven_->drv_set_clock(monoclock.get());
driven_->drv_set_clock(double(now) / 1000000.0);
} }
void close_channel(int chid, const std::string err) { void close_channel(int chid, const std::string err) {
@@ -340,7 +356,6 @@ public:
enableRawMode(); enableRawMode();
init(de); init(de);
DrivenEngine::set(de); DrivenEngine::set(de);
basetime_ = get_now();
driven_->drv_set_lua_source(util::read_lua_source("lua")); driven_->drv_set_lua_source(util::read_lua_source("lua"));
driven_->drv_invoke_event_init(argc, argv); driven_->drv_invoke_event_init(argc, argv);
handle_listen_ports(); handle_listen_ports();

View File

@@ -20,16 +20,6 @@
namespace util { 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) { static bool ascii_isalpha(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
} }

View File

@@ -37,8 +37,8 @@ using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
using HashValue = std::pair<uint64_t, uint64_t>; using HashValue = std::pair<uint64_t, uint64_t>;
using IdVector = std::vector<int64_t>; using IdVector = std::vector<int64_t>;
// Return nanoseconds elapsed, for profiling purposes. // Return seconds elapsed, for profiling purposes.
int64_t profiling_clock(); double profiling_clock();
// Return true if the string is a valid lua identifier. // Return true if the string is a valid lua identifier.
bool is_identifier(const std::string &str); bool is_identifier(const std::string &str);