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;
}
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<int, SOCKET> listen_sockets_;
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) {
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();