From 400fa0d889a8a38ca69f36d5750dfb572fc6018e Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com> Date: Wed, 20 Apr 2022 23:39:14 +0100 Subject: [PATCH] Always increment timer on record (#2298) Can drop this after rebase on commit baa2a36 "Always increment timer on record (#2298)", first released in 8.0.0 --- datafusion/core/src/physical_plan/metrics/value.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/datafusion/core/src/physical_plan/metrics/value.rs b/datafusion/core/src/physical_plan/metrics/value.rs index ffb4ebb3f655..feab135193f9 100644 --- a/datafusion/core/src/physical_plan/metrics/value.rs +++ b/datafusion/core/src/physical_plan/metrics/value.rs @@ -176,14 +176,23 @@ impl Time { } /// Add duration of time to self + /// + /// Note: this will always increment the recorded time by at least 1 nanosecond + /// to distinguish between the scenario of no values recorded, in which + /// case the value will be 0, and no measurable amount of time having passed, + /// in which case the value will be small but not 0. + /// + /// This is based on the assumption that the timing logic in most cases is likely + /// to take at least a nanosecond, and so this is reasonable mechanism to avoid + /// ambiguity, especially on systems with low-resolution monotonic clocks pub fn add_duration(&self, duration: Duration) { let more_nanos = duration.as_nanos() as usize; - self.nanos.fetch_add(more_nanos, Ordering::Relaxed); + self.nanos.fetch_add(more_nanos.max(1), Ordering::Relaxed); } /// Add the number of nanoseconds of other `Time` to self pub fn add(&self, other: &Time) { - self.nanos.fetch_add(other.value(), Ordering::Relaxed); + self.add_duration(Duration::from_nanos(other.value() as u64)) } /// return a scoped guard that adds the amount of time elapsed