MXS-2160 Use CLOCK_MONOTONIC_COARSE

We measure time in milliseconds and as CLOCK_MONOTONIC_COARSE
provides 1ms granularity we should use that since it is cheaper.
This commit is contained in:
Johan Wikman 2018-11-13 11:40:57 +02:00
parent ad52834c9b
commit ae1a062a58
2 changed files with 15 additions and 25 deletions

View File

@ -97,7 +97,7 @@ public:
*/
void reset()
{
uint64_t now = get_time();
uint64_t now = get_time_ms();
m_start_time = now;
m_wait_start = 0;
@ -116,7 +116,7 @@ public:
void about_to_wait()
{
about_to_wait(get_time());
about_to_wait(get_time_ms());
}
/**
@ -128,7 +128,7 @@ public:
void about_to_work()
{
about_to_work(get_time());
about_to_work(get_time_ms());
}
/**
@ -170,7 +170,7 @@ public:
*
* @return Current time in milliseconds.
*/
static uint64_t get_time();
static uint64_t get_time_ms();
private:

View File

@ -104,14 +104,17 @@ void WorkerLoad::about_to_work(uint64_t now)
}
// static
uint64_t WorkerLoad::get_time()
uint64_t WorkerLoad::get_time_ms()
{
uint64_t now;
timespec t;
MXB_AT_DEBUG(int rv = ) clock_gettime(CLOCK_MONOTONIC, &t);
mxb_assert(rv == 0);
int rv = clock_gettime(CLOCK_MONOTONIC_COARSE, &t);
if (rv != 0)
{
mxb_assert(errno == EINVAL); // CLOCK_MONOTONIC_COARSE not supported.
rv = clock_gettime(CLOCK_MONOTONIC, &t);
mxb_assert(rv == 0);
}
return t.tv_sec * 1000 + (t.tv_nsec / 1000000);
}
@ -764,7 +767,7 @@ void Worker::poll_waitevents()
atomic::add(&m_statistics.n_polls, 1, atomic::RELAXED);
uint64_t now = Load::get_time();
uint64_t now = Load::get_time_ms();
int timeout = Load::GRANULARITY - (now - m_load.start_time());
if (timeout < 0)
@ -884,22 +887,9 @@ void Worker::poll_waitevents()
} /*< while(1) */
}
namespace
{
int64_t get_current_time_ms()
{
struct timespec ts;
MXB_AT_DEBUG(int rv = ) clock_gettime(CLOCK_MONOTONIC, &ts);
mxb_assert(rv == 0);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
}
void Worker::tick()
{
int64_t now = get_current_time_ms();
int64_t now = WorkerLoad::get_time_ms();
vector<DelayedCall*> repeating_calls;
@ -979,7 +969,7 @@ void Worker::adjust_timer()
{
DelayedCall* pCall = m_sorted_calls.begin()->second;
uint64_t now = get_current_time_ms();
uint64_t now = WorkerLoad::get_time_ms();
int64_t delay = pCall->at() - now;
if (delay <= 0)