From b861f0457ba52c027384b14f20585166feddd3fe Mon Sep 17 00:00:00 2001 From: Baber Nawaz Date: Mon, 1 Dec 2025 15:17:49 +0000 Subject: [PATCH] chore: Using Mono clock for Reactor time thresholds SDB-10154 --- toolbox/io/Reactor.cpp | 13 +++++++------ toolbox/io/Reactor.hpp | 8 ++++---- toolbox/io/Reactor.ut.cpp | 18 +++++++++--------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/toolbox/io/Reactor.cpp b/toolbox/io/Reactor.cpp index 1c1d943f..9c56c6b7 100644 --- a/toolbox/io/Reactor.cpp +++ b/toolbox/io/Reactor.cpp @@ -97,7 +97,8 @@ int Reactor::poll(CyclTime now, Duration timeout) } // Update cycle time after epoll() returns. now = CyclTime::now(); - last_time_priority_io_polled_ = now.wall_time(); + last_time_priority_io_polled_ = now.mono_time(); + last_time_user_hook_polled_ = now.mono_time(); if (ec) { if (ec.value() != EINTR) { @@ -157,12 +158,12 @@ void Reactor::yield() noexcept return; } - WallTime now = WallClock::now(); + MonoTime now = MonoClock::now(); cycle_work_ += do_io_priority_poll(now); cycle_work_ += do_user_priority_poll(now); } -int Reactor::do_io_priority_poll(WallTime now) noexcept +int Reactor::do_io_priority_poll(MonoTime now) noexcept { int ret = 0; try { @@ -172,7 +173,7 @@ int Reactor::do_io_priority_poll(WallTime now) noexcept if (enabled && breached) { const auto update_poll_time = make_finally([this]() noexcept { - last_time_priority_io_polled_ = WallClock::now(); + last_time_priority_io_polled_ = MonoClock::now(); }); error_code ec; @@ -195,7 +196,7 @@ int Reactor::do_io_priority_poll(WallTime now) noexcept return ret; } -int Reactor::do_user_priority_poll(WallTime now) noexcept +int Reactor::do_user_priority_poll(MonoTime now) noexcept { int ret = 0; try { @@ -206,7 +207,7 @@ int Reactor::do_user_priority_poll(WallTime now) noexcept if (enabled && breached) { const auto update_poll_time = make_finally([this]() noexcept { - last_time_user_hook_polled_ = WallClock::now(); + last_time_user_hook_polled_ = MonoClock::now(); }); currently_handling_priority_events_ = true; diff --git a/toolbox/io/Reactor.hpp b/toolbox/io/Reactor.hpp index ee3176a1..8c4ad3ef 100644 --- a/toolbox/io/Reactor.hpp +++ b/toolbox/io/Reactor.hpp @@ -195,8 +195,8 @@ class TOOLBOX_API Reactor : public Waker { void set_events(int fd, int sid, unsigned events); void unsubscribe(int fd, int sid) noexcept; void set_io_priority(int fd, int sid, Priority priority) noexcept; - int do_io_priority_poll(WallTime now) noexcept; - int do_user_priority_poll(WallTime now) noexcept; + int do_io_priority_poll(MonoTime now) noexcept; + int do_user_priority_poll(MonoTime now) noexcept; struct Data { int sid{}; @@ -215,8 +215,8 @@ class TOOLBOX_API Reactor : public Waker { HookList end_of_cycle_no_wait_hooks, end_of_event_dispatch_hooks_; Micros priority_io_poll_threshold_ = Micros::max(); Micros user_hook_poll_threshold_ = Micros::max(); - WallTime last_time_priority_io_polled_{}; - WallTime last_time_user_hook_polled_{}; + MonoTime last_time_priority_io_polled_{}; + MonoTime last_time_user_hook_polled_{}; PollSlot priority_poll_user_hook_; int cycle_work_{0}; bool currently_handling_priority_events_{false}; diff --git a/toolbox/io/Reactor.ut.cpp b/toolbox/io/Reactor.ut.cpp index 760dccdd..58a00d4d 100644 --- a/toolbox/io/Reactor.ut.cpp +++ b/toolbox/io/Reactor.ut.cpp @@ -153,16 +153,16 @@ BOOST_AUTO_TEST_CASE(ReactorHighPriorityHook) auto [s1, s2] = socketpair(UnixStreamProtocol{}); - // This is handler for data on s1. It is an expensive function taking ~100ms + // This is handler for data on s1. It is an expensive function taking ~250ms std::size_t yield_count = 0; auto on_data_received = [&](CyclTime, int, unsigned) { - WallTime now = WallClock::now(); - WallTime end = now + 500ms; + MonoTime now = MonoClock::now(); + MonoTime end = now + 250ms; while (now < end) { - // wait for 100us, then yield - auto next_stop = now + 1ms; + // wait for 1ms, then yield + auto next_stop = MonoClock::now() + 1ms; while (now < next_stop) { - now = WallClock::now(); + now = MonoClock::now(); } r.yield(); yield_count++; @@ -211,13 +211,13 @@ BOOST_AUTO_TEST_CASE(ReactorHighPriorityYield) // each invocation will take ~100ms auto spin_and_yield_periodically = [&r]() { - WallTime now = WallClock::now(); - WallTime end = now + 100ms; + MonoTime now = MonoClock::now(); + MonoTime end = now + 100ms; while (now < end) { // wait for 10us, then yield auto next_stop = now + 10us; while (now < next_stop) { - now = WallClock::now(); + now = MonoClock::now(); } r.yield(); };