Construct query_start_time(_microseconds) from same timespec

This commit is contained in:
bharatnc 2020-09-14 19:37:44 -07:00
parent 7aa3f86ab9
commit e4e3105014
2 changed files with 17 additions and 5 deletions

View File

@ -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);

View File

@ -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<ASTPtr, BlockIO> 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.