use the linux monotonic clock instead of gettimeofday
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
|
||||
using HashValue = std::pair<uint64_t, uint64_t>;
|
||||
using IdVector = std::vector<int64_t>;
|
||||
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user