Repository navigation
test(rwlock): add upgradable lock stage timings #405
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6945416
bench(rwlock): time upgradable acquisition and promotion
ismaeelakin 715d994
bench(rwlock): report upgradable lock stages
ismaeelakin b199028
bench(rwlock): cover queued upgradable lock stages
ismaeelakin 0c52088
docs(benchmarks): clarify reader timing cases
ismaeelakin d48ef62
docs(benchmarks): clarify hand-polled timings
ismaeelakin 69d7c06
bench(rwlock): address stage timing review
ismaeelakin 52325c3
Merge branch 'main' into rwlock-stage-measurements
orthur2 fcef336
refactor(xtask): select default benches in one cargo command
orthur2 a5a62c7
Merge branch 'main' into rwlock-stage-measurements
orthur2 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
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,242 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::hint::black_box; | ||
| use std::pin::pin; | ||
| use std::time::Duration; | ||
| use std::time::Instant; | ||
|
|
||
| use asyncband::rwlock::RwLockUpgradableReadGuard; | ||
| use asyncband::rwlock::UpgradableRwLock; | ||
| use benchmarks::support::bench_context; | ||
| use benchmarks::support::poll_pending; | ||
| use benchmarks::support::poll_pinned_ready; | ||
| use benchmarks::support::poll_ready; | ||
| use clap::Parser; | ||
|
|
||
| #[derive(Parser)] | ||
| #[command(about = "Time upgradable-lock acquisition and promotion separately.")] | ||
| struct Options { | ||
| #[arg(long, default_value_t = 2_000_000)] | ||
| rounds: usize, | ||
| #[arg(long, default_value_t = 10)] | ||
| repeat: usize, | ||
| #[arg(long)] | ||
| test: bool, | ||
| #[arg(long = "bench", hide = true)] | ||
| _bench: bool, | ||
| } | ||
|
|
||
| const READER_COUNTS: &[usize] = &[1, 4, 16]; | ||
|
|
||
| struct Results { | ||
| empty_interval: Vec<Duration>, | ||
| acquisition: Vec<Duration>, | ||
| promotion: Vec<Duration>, | ||
| acquisition_with_reader: Vec<Duration>, | ||
| promotion_with_readers: Vec<Vec<Duration>>, | ||
| promotion_with_waiting_writer: Vec<Duration>, | ||
| acquisition_after_waiting_writer: Vec<Duration>, | ||
| } | ||
|
|
||
| fn measure(rounds: usize, repeat: usize) -> Results { | ||
| let lock = UpgradableRwLock::new(0usize); | ||
| let mut context = bench_context(); | ||
| let mut results = Results { | ||
| empty_interval: Vec::with_capacity(repeat), | ||
| acquisition: Vec::with_capacity(repeat), | ||
| promotion: Vec::with_capacity(repeat), | ||
| acquisition_with_reader: Vec::with_capacity(repeat), | ||
| promotion_with_readers: READER_COUNTS | ||
| .iter() | ||
| .map(|_| Vec::with_capacity(repeat)) | ||
| .collect(), | ||
| promotion_with_waiting_writer: Vec::with_capacity(repeat), | ||
| acquisition_after_waiting_writer: Vec::with_capacity(repeat), | ||
| }; | ||
| let mut readers = Vec::with_capacity(*READER_COUNTS.last().unwrap()); | ||
|
|
||
| for _ in 0..repeat { | ||
| let mut empty_interval = Duration::ZERO; | ||
| let mut acquisition = Duration::ZERO; | ||
| let mut promotion = Duration::ZERO; | ||
| let mut acquisition_with_reader = Duration::ZERO; | ||
| let mut promotion_with_readers = vec![Duration::ZERO; READER_COUNTS.len()]; | ||
| let mut promotion_with_waiting_writer = Duration::ZERO; | ||
| let mut acquisition_after_waiting_writer = Duration::ZERO; | ||
|
|
||
| for _ in 0..rounds { | ||
| let start = Instant::now(); | ||
| empty_interval += start.elapsed(); | ||
|
|
||
| let start = Instant::now(); | ||
| let guard = poll_ready(lock.upgradable_read(), &mut context); | ||
| acquisition += start.elapsed(); | ||
|
|
||
| let start = Instant::now(); | ||
| let writer = poll_ready(RwLockUpgradableReadGuard::upgrade(guard), &mut context); | ||
| promotion += start.elapsed(); | ||
|
|
||
| black_box(*writer); | ||
| drop(writer); | ||
|
Comment on lines
+86
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty timed interval costs about 15 ns on my M5, roughly half the reported acquisition and promotion times. Could we print that baseline alongside the results? |
||
|
|
||
| let reader = poll_ready(lock.read(), &mut context); | ||
| let start = Instant::now(); | ||
| let guard = poll_ready(lock.upgradable_read(), &mut context); | ||
| acquisition_with_reader += start.elapsed(); | ||
| black_box(*guard); | ||
| drop(guard); | ||
| drop(reader); | ||
|
|
||
| for (index, &reader_count) in READER_COUNTS.iter().enumerate() { | ||
| let guard = poll_ready(lock.upgradable_read(), &mut context); | ||
| for _ in 0..reader_count { | ||
| readers.push(poll_ready(lock.read(), &mut context)); | ||
| } | ||
| let mut upgrade = pin!(RwLockUpgradableReadGuard::upgrade(guard)); | ||
| let start = Instant::now(); | ||
| poll_pending(upgrade.as_mut(), &mut context); | ||
| readers.clear(); | ||
| let writer = poll_pinned_ready(upgrade.as_mut(), &mut context); | ||
| promotion_with_readers[index] += start.elapsed(); | ||
| black_box(*writer); | ||
| drop(writer); | ||
| } | ||
|
|
||
| let guard = poll_ready(lock.upgradable_read(), &mut context); | ||
| let mut queued_writer = pin!(lock.write()); | ||
| poll_pending(queued_writer.as_mut(), &mut context); | ||
| let start = Instant::now(); | ||
| let writer = poll_ready(RwLockUpgradableReadGuard::upgrade(guard), &mut context); | ||
| promotion_with_waiting_writer += start.elapsed(); | ||
| black_box(*writer); | ||
| drop(writer); | ||
| drop(poll_pinned_ready(queued_writer.as_mut(), &mut context)); | ||
|
|
||
| let first_guard = poll_ready(lock.upgradable_read(), &mut context); | ||
| let mut queued_writer = pin!(lock.write()); | ||
| poll_pending(queued_writer.as_mut(), &mut context); | ||
| let mut queued_upgradable = pin!(lock.upgradable_read()); | ||
| poll_pending(queued_upgradable.as_mut(), &mut context); | ||
| let start = Instant::now(); | ||
| drop(first_guard); | ||
| drop(poll_pinned_ready(queued_writer.as_mut(), &mut context)); | ||
| let guard = poll_pinned_ready(queued_upgradable.as_mut(), &mut context); | ||
| acquisition_after_waiting_writer += start.elapsed(); | ||
| black_box(*guard); | ||
| drop(guard); | ||
| } | ||
|
|
||
| results.empty_interval.push(empty_interval); | ||
| results.acquisition.push(acquisition); | ||
| results.promotion.push(promotion); | ||
| results | ||
| .acquisition_with_reader | ||
| .push(acquisition_with_reader); | ||
| for (samples, elapsed) in results | ||
| .promotion_with_readers | ||
| .iter_mut() | ||
| .zip(promotion_with_readers) | ||
| { | ||
| samples.push(elapsed); | ||
| } | ||
| results | ||
| .promotion_with_waiting_writer | ||
| .push(promotion_with_waiting_writer); | ||
| results | ||
| .acquisition_after_waiting_writer | ||
| .push(acquisition_after_waiting_writer); | ||
| } | ||
|
|
||
| results | ||
| } | ||
|
|
||
| fn report(name: &str, rounds: usize, mut samples: Vec<Duration>) { | ||
| samples.sort_unstable(); | ||
| let middle = samples.len() / 2; | ||
| let median = if samples.len() % 2 == 0 { | ||
| let lower = samples[middle - 1]; | ||
| lower + (samples[middle] - lower) / 2 | ||
| } else { | ||
| samples[middle] | ||
| }; | ||
| let minimum = samples.first().unwrap(); | ||
| let maximum = samples.last().unwrap(); | ||
| let ns_per_round = median.as_nanos() as f64 / rounds as f64; | ||
| let runs = samples | ||
| .iter() | ||
| .map(|sample| format!("{:.3}", sample.as_secs_f64() * 1_000.0)) | ||
| .collect::<Vec<_>>() | ||
| .join(", "); | ||
|
|
||
| println!( | ||
| "{name:<24} median {:>8.2} ns/round ({:.2}..{:.2} ms; runs (ms): [{runs}])", | ||
| ns_per_round, | ||
| minimum.as_secs_f64() * 1_000.0, | ||
| maximum.as_secs_f64() * 1_000.0, | ||
| ); | ||
| } | ||
|
|
||
| fn main() { | ||
| let options = Options::parse(); | ||
| if options.test { | ||
| let _ = measure(1, 1); | ||
| println!("ok upgradable-lock stage benchmark smoke checks"); | ||
| return; | ||
| } | ||
| assert!(options.rounds > 0, "rounds must be positive"); | ||
| assert!(options.repeat > 0, "repeat must be positive"); | ||
|
|
||
| let results = measure(options.rounds, options.repeat); | ||
| println!( | ||
| "UpgradableRwLock, {} rounds per run, {} runs", | ||
| options.rounds, options.repeat | ||
| ); | ||
| println!("The clock reads add measurable overhead; treat these numbers as diagnostic."); | ||
| println!( | ||
| "The empty interval uses the same clock reads and is reported without subtracting it." | ||
| ); | ||
| report( | ||
| "empty timing interval", | ||
| options.rounds, | ||
| results.empty_interval, | ||
| ); | ||
| report("acquisition", options.rounds, results.acquisition); | ||
| report("promotion", options.rounds, results.promotion); | ||
| report( | ||
| "acquisition + reader held", | ||
| options.rounds, | ||
| results.acquisition_with_reader, | ||
| ); | ||
| for (&reader_count, samples) in READER_COUNTS.iter().zip(results.promotion_with_readers) { | ||
| report( | ||
| &format!("promotion + {reader_count} readers"), | ||
| options.rounds, | ||
| samples, | ||
| ); | ||
| } | ||
| report( | ||
| "promotion + writer queued", | ||
| options.rounds, | ||
| results.promotion_with_waiting_writer, | ||
| ); | ||
| report( | ||
| "acquire behind queued writer", | ||
| options.rounds, | ||
| results.acquisition_after_waiting_writer, | ||
| ); | ||
| } | ||
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.
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.
cargo x bench -- --listworked before and now fails, because the default run includes this target and it rejects--list. I'd leave it out of the default run and smoke-test it separately in CI.