-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
time: POC for fine-grained locks on timer wheel #7668
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
Closed
+661
−19
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3df9c4e
benches: add benchmark demonstrating timer drop contention
yakryder 24a53ea
runtime: reduce timer drop contention with per-worker timers
yakryder 2f76baf
benches: improve many timer benchmark
yakryder c5315d3
benches: rename many timer benchmark
yakryder 3ebc1bc
benches: do housekeeping
yakryder 54f89f9
Revert "runtime: reduce timer drop contention with per-worker timers"
yakryder 0e9a67c
Merge branch 'master' into reduce-timer-contention
yakryder 4fcc8ff
time: add timer buckets to reduce lock contention
yakryder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
| /// Benchmark measuring timer lifecycle performance (Issue #6504) | ||
| /// | ||
| /// This benchmark creates many timers, polls them once to register with the timer | ||
| /// system, then drops them before they fire. This simulates the common case of | ||
| /// timeouts that don't fire (e.g., operations completing before timeout). | ||
| /// | ||
| /// The benchmark compares single-threaded vs multi-threaded performance to reveal | ||
| /// contention in timer registration and deregistration. | ||
| use std::future::{poll_fn, Future}; | ||
| use std::iter::repeat; | ||
| use std::time::{Duration, Instant}; | ||
| use tokio::time::sleep; | ||
|
|
||
| const TIMER_COUNT: usize = 1_000_000; | ||
|
|
||
| struct TimerDistribution { | ||
| duration: Duration, | ||
| percentage: f64, | ||
| } | ||
|
|
||
| const fn from_secs(s: u64) -> Duration { | ||
| Duration::from_secs(s) | ||
| } | ||
|
|
||
| const TIMER_DISTRIBUTIONS: &[TimerDistribution] = &[ | ||
| TimerDistribution { | ||
| duration: from_secs(1), | ||
| percentage: 0.40, | ||
| }, | ||
| TimerDistribution { | ||
| duration: from_secs(10), | ||
| percentage: 0.30, | ||
| }, | ||
| TimerDistribution { | ||
| duration: from_secs(60), | ||
| percentage: 0.20, | ||
| }, | ||
| TimerDistribution { | ||
| duration: from_secs(300), | ||
| percentage: 0.10, | ||
| }, | ||
| ]; | ||
|
|
||
| /// Each timer is polled once to register, then dropped before firing. | ||
| async fn create_and_drop_timers_instrumented(count: usize, concurrent_tasks: usize) -> Duration { | ||
| let handles: Vec<_> = (0..concurrent_tasks) | ||
| .map(|_| { | ||
| tokio::spawn(async move { | ||
| // Create all sleep futures with realistic distribution | ||
| let sleeps: Vec<_> = TIMER_DISTRIBUTIONS | ||
| .iter() | ||
| .flat_map(|td| { | ||
| repeat(td.duration).take((TIMER_COUNT as f64 * td.percentage) as usize) | ||
| }) | ||
| .cycle() | ||
| .take(count / concurrent_tasks) | ||
| .map(|timeout| Box::pin(sleep(timeout))) | ||
| .collect(); | ||
|
|
||
| // Start timing - poll and drop (METERED) | ||
| let start = Instant::now(); | ||
| for mut sleep in sleeps { | ||
| // Poll once to register | ||
| poll_fn(|cx| { | ||
| let _ = sleep.as_mut().poll(cx); | ||
| std::task::Poll::Ready(()) | ||
| }) | ||
| .await; | ||
|
|
||
| // Drop to deregister | ||
| black_box(drop(sleep)); | ||
| } | ||
| let elapsed = start.elapsed(); | ||
|
|
||
| elapsed | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| let wall_clock_start = Instant::now(); | ||
|
|
||
| for handle in handles { | ||
| handle.await.unwrap(); | ||
| } | ||
|
|
||
| wall_clock_start.elapsed() | ||
| } | ||
|
|
||
| fn bench_many_timers(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("many_timers"); | ||
|
|
||
| // Single-threaded baseline | ||
| let runtime = tokio::runtime::Builder::new_current_thread() | ||
| .enable_all() | ||
| .build() | ||
| .unwrap(); | ||
| group.bench_function("single_thread", |b| { | ||
| b.iter_custom(|_iters| { | ||
| let wall_clock = runtime | ||
| .block_on(async { create_and_drop_timers_instrumented(TIMER_COUNT, 1).await }); | ||
|
|
||
| wall_clock | ||
| }) | ||
| }); | ||
|
|
||
| // Multi-threaded with 8 workers | ||
| let runtime_multi = tokio::runtime::Builder::new_multi_thread() | ||
| .enable_all() | ||
| .worker_threads(8) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| group.bench_function("multi_thread", |b| { | ||
| b.iter_custom(|_iters| { | ||
| let wall_clock = runtime_multi | ||
| .block_on(async { create_and_drop_timers_instrumented(TIMER_COUNT, 8).await }); | ||
|
|
||
| wall_clock | ||
| }) | ||
| }); | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!(many_timers, bench_many_timers); | ||
|
|
||
| criterion_main!(many_timers); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this atomic load require extra synchronization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above 👍🏻