kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ale...@apache.org
Subject [kudu] 03/03: [gutil] introduce CLOCK_MONOTONIC_RAW
Date Fri, 06 Sep 2019 05:52:31 GMT
This is an automated email from the ASF dual-hosted git repository.

alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 41bd37cd13268ab67c701adea6aecc8cc010890e
Author: Alexey Serbin <alexey@apache.org>
AuthorDate: Mon Aug 26 10:08:57 2019 -0700

    [gutil] introduce CLOCK_MONOTONIC_RAW
    This patch adds an accessor method for the CLOCK_MONOTONIC_RAW clock,
    named GetMonoTimeMicrosRaw().  This functionality will be used by
    follow-up changelists which implement built-in Kudu NTP client
    (one of those is https://gerrit.cloudera.org/#/c/7477).
    In essence, CLOCK_MONOTONIC_RAW (CLOCK_UPTIME_RAW on Darwin/macOS)
    is a better clock source than CLOCK_MONOTONIC for NTP-related
    calculations since CLOCK_MONOTONIC_RAW is not subject to adjustments
    by adjtime() and the kernel NTP discipline (see [1]).
    CLOCK_MONOTONIC_RAW clock is marching forward always at the same rate,
    and that's useful when sampling local clock readings to estimate delays
    in NTP packet exchange between the client and the servers, especially
    when counting in the skew of the local clock into the computations.
    On macOS, GetMonoTimeMicrosRaw() behaves exactly as GetMonoTimeMicros()
    since both use mach_absolute_time() which isn't affected by clock
    adjustments.  On newer versions of Darwin (16.0+) and macOS (10.12+),
    GetMonoTimeMicrosRaw() produces the same results as
    clock_gettime(CLOCK_UPTIME_RAW, ...), see [2].
      1. http://man7.org/linux/man-pages/man2/clock_gettime.2.html
      2. http://www.manpagez.com/man/3/clock_gettime/
    Change-Id: I88c787fe7aee6aa55943cbadfd2be4f43053f6ad
    Reviewed-on: http://gerrit.cloudera.org:8080/14172
    Tested-by: Kudu Jenkins
    Reviewed-by: Adar Dembo <adar@cloudera.com>
 src/kudu/gutil/walltime.h | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/kudu/gutil/walltime.h b/src/kudu/gutil/walltime.h
index 4b15f42..eec28fe 100644
--- a/src/kudu/gutil/walltime.h
+++ b/src/kudu/gutil/walltime.h
@@ -41,9 +41,9 @@ typedef double WallTime;
 // Append result to a supplied string.
 // If an error occurs during conversion 'dst' is not modified.
 void StringAppendStrftime(std::string* dst,
-                                 const char* format,
-                                 time_t when,
-                                 bool local);
+                          const char* format,
+                          time_t when,
+                          bool local);
 // Return the local time as a string suitable for user display.
 std::string LocalTimeAsString();
@@ -53,10 +53,10 @@ std::string LocalTimeAsString();
 // time. If local is set to true, the same exact result as
 // WallTime_Parse is returned.
 bool WallTime_Parse_Timezone(const char* time_spec,
-                                    const char* format,
-                                    const struct tm* default_time,
-                                    bool local,
-                                    WallTime* result);
+                             const char* format,
+                             const struct tm* default_time,
+                             bool local,
+                             WallTime* result);
 // Return current time in seconds as a WallTime.
 WallTime WallTime_Now();
@@ -158,15 +158,35 @@ inline MicrosecondsInt64 GetCurrentTimeMicros() {
 // Returns the time since some arbitrary reference point, measured in microseconds.
-// Guaranteed to be monotonic (and therefore useful for measuring intervals)
+// Guaranteed to be monotonic (and therefore useful for measuring intervals),
+// but the underlying clock is subject for adjustment by adjtime() and
+// the kernel's NTP discipline. For example, the underlying clock might
+// be slewed a bit to reach some reference point, time to time adjusted to be
+// of the desired result frequency, etc.
 inline MicrosecondsInt64 GetMonoTimeMicros() {
 #if defined(__APPLE__)
+  // In fact, walltime_internal::GetMonoTimeMicros() is implemented via
+  // mach_absolute_time() which is not actually affected by adjtime()
+  // or the NTP discipline. On Darwin 16.0 and newer (macOS 10.12 and newer),
+  // it's the same as clock_gettime(CLOCK_UPTIME_RAW); see 'man clock_gettime'
+  // on macOS 10.12 and newer.
   return walltime_internal::GetMonoTimeMicros();
   return walltime_internal::GetClockTimeMicros(CLOCK_MONOTONIC);
 #endif  // defined(__APPLE__)
+// Returns the time since some arbitrary reference point, measured in microseconds.
+// Guaranteed to be monotonic and not affected at all by frequency and time
+// adjustments such as adjtime() or the kernel's NTP discipline.
+inline MicrosecondsInt64 GetMonoTimeMicrosRaw() {
+#if defined(__APPLE__)
+  return walltime_internal::GetMonoTimeMicros();
+  return walltime_internal::GetClockTimeMicros(CLOCK_MONOTONIC_RAW);
+#endif  // defined(__APPLE__)
 // Returns the time spent in user CPU on the current thread, measured in microseconds.
 inline MicrosecondsInt64 GetThreadCpuTimeMicros() {
 #if defined(__APPLE__)

View raw message