Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Improve Instant::now() perf with test-util #5513

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.0.0"
publish = false
edition = "2018"

[features]
test-util = ["tokio/test-util"]

[dependencies]
tokio = { version = "1.5.0", path = "../tokio", features = ["full"] }
bencher = "0.1.5"
Expand Down Expand Up @@ -67,3 +70,8 @@ harness = false
name = "copy"
path = "copy.rs"
harness = false

[[bench]]
name = "time_now"
path = "time_now.rs"
harness = false
28 changes: 28 additions & 0 deletions benches/time_now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Benchmark spawning a task onto the basic and threaded Tokio executors.
//! This essentially measure the time to enqueue a task in the local and remote
//! case.

#[macro_use]
extern crate bencher;

use bencher::{black_box, Bencher};

fn time_now_current_thread(bench: &mut Bencher) {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();

bench.iter(|| {
rt.block_on(async {
black_box(tokio::time::Instant::now());
})
})
}

bencher::benchmark_group!(
time_now,
time_now_current_thread,
);

bencher::benchmark_main!(time_now);
15 changes: 15 additions & 0 deletions tokio/src/time/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cfg_not_test_util! {
cfg_test_util! {
use crate::time::{Duration, Instant};
use crate::loom::sync::Mutex;
use crate::loom::sync::atomic::{AtomicBool, Ordering};

cfg_rt! {
#[track_caller]
Expand Down Expand Up @@ -65,6 +66,13 @@ cfg_test_util! {
inner: Mutex<Inner>,
}

// Used to track if the clock was ever paused. This is an optimization to
// avoid touching the mutex if `test-util` was accidentally enabled in
// release mode.
//
// A static is used so we can avoid accessing the thread-local as well.
static DID_PAUSE_CLOCK: AtomicBool = AtomicBool::new(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

This fails with loom. How about using the std atomic here and initializing it to true when #[cfg(loom)]?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just switched it to a std bool. Loom tests don't make sense with time right now anyway, so it shouldn't be hit.


#[derive(Debug)]
struct Inner {
/// True if the ability to pause time is enabled.
Expand Down Expand Up @@ -199,6 +207,10 @@ cfg_test_util! {

/// Returns the current instant, factoring in frozen time.
pub(crate) fn now() -> Instant {
if !DID_PAUSE_CLOCK.load(Ordering::Acquire) {
return Instant::from_std(std::time::Instant::now());
}

with_clock(|maybe_clock| {
Ok(if let Some(clock) = maybe_clock {
clock.now()
Expand Down Expand Up @@ -241,6 +253,9 @@ cfg_test_util! {
This is the default Runtime used by `#[tokio::test].");
}

// Track that we paused the clock
DID_PAUSE_CLOCK.store(true, Ordering::Release);

let elapsed = match inner.unfrozen.as_ref() {
Some(v) => v.elapsed(),
None => return Err("time is already frozen")
Expand Down