From d2b666dfc5428b097f78fb6f9b27996ebd76be2e Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Wed, 8 Nov 2023 19:45:31 +0800 Subject: [PATCH 1/2] Fix time step backward for `Stopwatch::elapsedFromLastTime` --- dbms/src/Common/Stopwatch.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/dbms/src/Common/Stopwatch.h b/dbms/src/Common/Stopwatch.h index 853d53f0db8..28763478cf9 100644 --- a/dbms/src/Common/Stopwatch.h +++ b/dbms/src/Common/Stopwatch.h @@ -30,7 +30,7 @@ inline UInt64 clock_gettime_ns(clockid_t clock_type = CLOCK_MONOTONIC) { }; clock_gettime(clock_type, &ts); - return UInt64(ts.tv_sec * 1000000000ULL + ts.tv_nsec); + return static_cast(ts.tv_sec * 1000000000ULL + ts.tv_nsec); } /// Sometimes monotonic clock may not be monotonic (due to bug in kernel?). @@ -64,14 +64,14 @@ class Stopwatch void start() { - start_ns = nanoseconds(); + start_ns = nanosecondsWithBound(start_ns); last_ns = start_ns; is_running = true; } void stop() { - stop_ns = nanoseconds(); + stop_ns = nanosecondsWithBound(start_ns); is_running = false; } @@ -82,14 +82,16 @@ class Stopwatch last_ns = 0; is_running = false; } + void restart() { start(); } - UInt64 elapsed() const { return is_running ? nanoseconds() - start_ns : stop_ns - start_ns; } + + UInt64 elapsed() const { return is_running ? nanosecondsWithBound(start_ns) - start_ns : stop_ns - start_ns; } UInt64 elapsedMilliseconds() const { return elapsed() / 1000000UL; } double elapsedSeconds() const { return static_cast(elapsed()) / 1000000000ULL; } UInt64 elapsedFromLastTime() { - const auto now_ns = nanoseconds(); + const auto now_ns = nanosecondsWithBound(last_ns); if (is_running) { auto rc = now_ns - last_ns; @@ -100,7 +102,7 @@ class Stopwatch { return stop_ns - last_ns; } - }; + } UInt64 elapsedMillisecondsFromLastTime() { return elapsedFromLastTime() / 1000000UL; } double elapsedSecondsFromLastTime() { return static_cast(elapsedFromLastTime()) / 1000000000ULL; } @@ -109,10 +111,12 @@ class Stopwatch UInt64 start_ns = 0; UInt64 stop_ns = 0; UInt64 last_ns = 0; - clockid_t clock_type; + const clockid_t clock_type; bool is_running = false; - UInt64 nanoseconds() const { return clock_gettime_ns_adjusted(start_ns, clock_type); } + // Get current nano seconds, ensuring the return value is not + // less than `lower_bound`. + UInt64 nanosecondsWithBound(UInt64 lower_bound) const { return clock_gettime_ns_adjusted(lower_bound, clock_type); } }; From be4fc0d597ff75ded5bfe36a3b0a037f94c295df Mon Sep 17 00:00:00 2001 From: JaySon Date: Thu, 9 Nov 2023 09:54:01 +0800 Subject: [PATCH 2/2] Fix compile error --- dbms/src/Common/Stopwatch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbms/src/Common/Stopwatch.h b/dbms/src/Common/Stopwatch.h index 28763478cf9..af972610810 100644 --- a/dbms/src/Common/Stopwatch.h +++ b/dbms/src/Common/Stopwatch.h @@ -111,7 +111,7 @@ class Stopwatch UInt64 start_ns = 0; UInt64 stop_ns = 0; UInt64 last_ns = 0; - const clockid_t clock_type; + clockid_t clock_type; bool is_running = false; // Get current nano seconds, ensuring the return value is not