Skip to content

fix "zero-time measurements" bug#867

Open
zooko wants to merge 23 commits into
bheisler:masterfrom
zooko:master
Open

fix "zero-time measurements" bug#867
zooko wants to merge 23 commits into
bheisler:masterfrom
zooko:master

Conversation

@zooko

@zooko zooko commented May 25, 2025

Copy link
Copy Markdown

This fixes #862

@zooko

zooko commented May 25, 2025

Copy link
Copy Markdown
Author

Found another place where measurements can come out 0, while running benchmarks of my project.

Noname Thanks and others added 18 commits May 27, 2025 21:56
preliminary port of MachAbsoluteTime and RDPTSCP measurements from
smalloc
half-assed and non-compiling code to debug a bug in the RDPTSCP
measurement
happy

(This version still doesn't compile, but if you're a Criterion
developer, you might want this patch.
and then give up and tell clippy to stop warning about that
…s call to it (which would cause the elapsed duration to be 0) then instead say that the elapsed duration is 1.
…e by a veeeeery small positive float instead
…me is 0 and if so setting it to 1 nanosecond
… other call site I could find in this file that computes a duration -- if the duration is < 1 then set it equal to 1.
…act that it is not allowed to return 0; remove unit test that Criterion allows Measurements to return 0.
…urements: if it is 0, then set it to 1 nanosecond

I don't 100% know that this is a problem, but I've getting irreproducible
failures that mention "NaN", and I investigated and added a bunch of asserts,
and determined that there are a couple of spots that divide by a number that is
sometimes 0, and I guess that number might sometimes be 0 due to elapsed_time
sometimes being 0. So, I don't know this is really a good fix for a specific
bug, but maybe so, and it probably doesn't hurt.
Comment thread src/bencher.rs Outdated
black_box(routine());
}
self.value = self.measurement.end(start);
assert!(self.measurement.to_f64(&self.value) > f64::MIN_POSITIVE, "{}", self.measurement.to_f64(&self.value));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this not be ">=" or just "> 0.0"?

Comment thread src/routine.rs
.first()
.unwrap();
assert!(t_now - t_prev > f64::MIN_POSITIVE);
let t = (t_prev + 2. * t_now) / 5.;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using "git rebase -i HEAD~23" you can change this commit to a fixup of your first commit.

Comment thread Cargo.toml
[lints.clippy]
uninlined_format_args = "allow"

[dependencies]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps move the format_arg changes to a separate PR?

@hkBst

hkBst commented Jun 26, 2025

Copy link
Copy Markdown

@zooko you should really clean up the commit history and remove temporary debug stuff and merge fixup commits with the commit they fix up. Also separate out all unrelated stuff, like clippy fixes. Perhaps then this stands a chance of being accepted.

Comment thread src/measurement.rs
Comment on lines +364 to +369
#[cfg(target_arch = "x86_64")]
pub mod plat_x86_64 {
use cpuid;
use crate::{Throughput, Measurement};
use crate::measurement::ValueFormatter;
use core::arch::x86_64;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there some reshuffling of modules?

Comment thread src/measurement.rs
Comment on lines +279 to +294
/// A slightly better timer for Apple platforms.
pub mod plat_apple {
use mach_sys::mach_time::mach_absolute_time;
use mach_sys::mach_time::{mach_absolute_time, mach_timebase_info};
use mach_sys::kern_return::KERN_SUCCESS;
use crate::measurement::ValueFormatter;
use std::mem::MaybeUninit;
use crate::{Measurement, Throughput};

#[derive(Default)]
struct MachAbsoluteTimeMeasurement { }
/// Use the `mach_absolute_time()` clock, which is slightly better
/// in my [tests](https://github.com/zooko/measure-clocks) than
/// `Instant::now()`.
pub struct MachAbsoluteTimeMeasurement { }

struct MachAbsoluteTimeValueFormatter { }

use mach_sys::mach_time::mach_timebase_info;
use mach_sys::kern_return::KERN_SUCCESS;
/// Formatter
pub struct MachAbsoluteTimeValueFormatter { }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt?

Comment thread src/measurement.rs Outdated
}
fn end(&self, i: &Self::Intermediate) -> Self::Value {
( unsafe { mach_absolute_time() } - i )
( unsafe { mach_absolute_time() } - i )

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt?

Comment thread src/measurement.rs
Comment on lines +358 to 366
fn lt(&self, val: &Self::Value, other: &Self::Value) -> bool {
val < other
}
fn debugprint(&self, val: &Self::Intermediate, other: &Self::Value) {
eprintln!("val: {val:?}, other: {other:?}");
}
fn to_f64(&self, val: &Self::Value) -> f64 {
*val as f64
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ERROR: At least one measurement of benchmark {} took zero time per iteration.

2 participants