From e4e310501447bf0cdea238b984044f7762126bd0 Mon Sep 17 00:00:00 2001 From: bharatnc Date: Mon, 14 Sep 2020 19:37:44 -0700 Subject: [PATCH] Construct query_start_time(_microseconds) from same timespec --- src/Interpreters/ThreadStatusExt.cpp | 11 ++++++++--- src/Interpreters/executeQuery.cpp | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index 34efdda9097..5766472156a 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -146,9 +146,14 @@ void ThreadStatus::initPerformanceCounters() memory_tracker.resetCounters(); memory_tracker.setDescription("(for thread)"); - query_start_time_nanoseconds = getCurrentTimeNanoseconds(); - query_start_time = time(nullptr); - query_start_time_microseconds = getCurrentTimeMicroseconds(); + // query_start_time_{microseconds, nanoseconds} are all constructed from the same timespec + // to ensure that they are all atelast equal upto the precision of a second. + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + + query_start_time_nanoseconds = UInt64(ts.tv_sec * 1000000000LL + ts.tv_nsec); + query_start_time = ts.tv_sec; + query_start_time_microseconds = UInt64((ts.tv_sec * 1000000LL) + (ts.tv_nsec / 1000)); ++queries_started; *last_rusage = RUsageCounters::current(query_start_time_nanoseconds); diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 478eed65ae1..be18fb20d29 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -196,6 +196,8 @@ static void onExceptionBeforeStart(const String & query_for_logging, Context & c elem.type = QueryLogElementType::EXCEPTION_BEFORE_START; + // the assumption here is that the callers of onExceptionBeforeStart construct both params current_time and the current_time_microseconds + // from the same timespec so that both of the times are equal upto the precision of a second. elem.event_time = current_time; elem.query_start_time = current_time; elem.query_start_time_microseconds = current_time_microseconds; @@ -251,8 +253,13 @@ static std::tuple executeQueryImpl( bool has_query_tail, ReadBuffer * istr) { - time_t current_time = time(nullptr); - UInt64 current_time_microseconds = getCurrentTimeMicroseconds(); + // current_time and current_time_microseconds are both constructed from the same timespec + // to ensure that both the times are equal upto the precision of a second. + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + + time_t current_time = ts.tv_sec; + UInt64 current_time_microseconds = UInt64((ts.tv_sec * 1000000LL) + (ts.tv_nsec / 1000)); /// If we already executing query and it requires to execute internal query, than /// don't replace thread context with given (it can be temporary). Otherwise, attach context to thread.