From 84800cbf259a1ef0dbd24200ef7c8efbf0a1aa9d Mon Sep 17 00:00:00 2001 From: Jonathan Bendes Date: Wed, 22 Oct 2025 13:27:15 -0400 Subject: [PATCH 1/3] Added configurable use of montonic clock source --- util/TimeUtil.hpp | 6 ++++++ wscript | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/util/TimeUtil.hpp b/util/TimeUtil.hpp index 15f3a7ebe..f95f18dc4 100644 --- a/util/TimeUtil.hpp +++ b/util/TimeUtil.hpp @@ -6,8 +6,14 @@ namespace TimeUtil { static u64 utime() { +#ifdef MONOTONIC_CLOCK + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); // or CLOCK_MONOTONIC + return (uint64_t)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL; +#else struct timeval tv; gettimeofday(&tv, NULL); return (u64)tv.tv_sec * 1000000 + tv.tv_usec; +#endif } } diff --git a/wscript b/wscript index 1c97d910b..c107e4d53 100644 --- a/wscript +++ b/wscript @@ -66,6 +66,10 @@ def add_zcm_configure_options(ctx): choices=['true', 'false'], help='Include the zcmtype name in the hash generation') + gr.add_option('--monotonic-clock', dest='monotonic_clock', default='true', + choices=['true', 'false'], + help='Use a monotonic clock source for all utimes internally') + add_use_option('dev', 'Enable all dev tools') add_use_option('build-cache', 'Enable the build cache for faster rebuilds') add_use_option('clang', 'Enable build using clang sanitizers') @@ -179,6 +183,8 @@ def process_zcm_configure_options(ctx): env.HASH_TYPENAME = getattr(opt, 'hash_typename') env.HASH_MEMBER_NAMES = getattr(opt, 'hash_member_names') + env.MONOTONIC_CLOCK = getattr(opt, 'monotonic_clock') + env.USING_CACHE = hasoptDev('use_build_cache') and attempt_use_cache(ctx) env.USING_CLANG = hasoptDev('use_clang') and attempt_use_clang(ctx) env.USING_CXXTEST = hasoptDev('use_cxxtest') and attempt_use_cxxtest(ctx) @@ -228,6 +234,7 @@ def process_zcm_configure_options(ctx): Logs.pprint('BLUE', '\nType Configuration:') print_entry("hash-typename", env.HASH_TYPENAME == 'true') print_entry("hash-member-names", env.HASH_MEMBER_NAMES == 'true', True) + print_entry("monotonic-clock", env.MONOTONIC_CLOCK == 'true', True) Logs.pprint('BLUE', '\nDev Configuration:') print_entry("Build Cache", env.USING_CACHE) @@ -420,6 +427,9 @@ def setup_environment(ctx): if ctx.env.HASH_MEMBER_NAMES == 'true': ctx.env.DEFINES_default.append("ENABLE_MEMBERNAME_HASHING") + if ctx.env.MONOTONIC_CLOCK == 'true': + ctx.env.DEFINES_default.append("MONOTONIC_CLOCK") + if ctx.env.TRACK_TRAFFIC_TOPOLOGY == 'true': ctx.env.DEFINES_default.append("TRACK_TRAFFIC_TOPOLOGY") From 1600d1ee0002f1e81d5d2aa838c74fdf9e349e98 Mon Sep 17 00:00:00 2001 From: Jakob Hoellerbauer Date: Mon, 27 Oct 2025 12:57:57 -0400 Subject: [PATCH 2/3] Include header for clock_gettime if necessary --- util/TimeUtil.hpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/util/TimeUtil.hpp b/util/TimeUtil.hpp index f95f18dc4..232867508 100644 --- a/util/TimeUtil.hpp +++ b/util/TimeUtil.hpp @@ -1,19 +1,22 @@ #pragma once -#include #include "util/Types.hpp" +#include + +#ifdef MONOTONIC_CLOCK +#include +#endif -namespace TimeUtil +namespace TimeUtil { +static u64 utime() { - static u64 utime() - { #ifdef MONOTONIC_CLOCK - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC_RAW, &ts); // or CLOCK_MONOTONIC - return (uint64_t)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); // or CLOCK_MONOTONIC + return (uint64_t)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL; #else - struct timeval tv; - gettimeofday(&tv, NULL); - return (u64)tv.tv_sec * 1000000 + tv.tv_usec; + struct timeval tv; + gettimeofday(&tv, NULL); + return (u64)tv.tv_sec * 1000000 + tv.tv_usec; #endif - } } +} // namespace TimeUtil From a206842bf6ce0a9314e6dc2fb7fa2c2ac9e586cb Mon Sep 17 00:00:00 2001 From: Jakob Hoellerbauer Date: Mon, 27 Oct 2025 14:19:05 -0400 Subject: [PATCH 3/3] review --- wscript | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wscript b/wscript index c107e4d53..31df5ddba 100644 --- a/wscript +++ b/wscript @@ -66,9 +66,12 @@ def add_zcm_configure_options(ctx): choices=['true', 'false'], help='Include the zcmtype name in the hash generation') + # RRR (Jakob H.): Shouldn't this default to false so that the previous default behavior + # is preserved? gr.add_option('--monotonic-clock', dest='monotonic_clock', default='true', choices=['true', 'false'], - help='Use a monotonic clock source for all utimes internally') + help='Instead of using the system clock, use a clock source that is guaranteed ' + 'to be monotonic for all utimes internally') add_use_option('dev', 'Enable all dev tools') add_use_option('build-cache', 'Enable the build cache for faster rebuilds')