-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Reduce contention in broadcast channel #6284
Closed
Closed
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4f98a68
benches: add sync_broadcast benchmark
vnetserg cd43fc9
sync: reduce contention in broadcast channel
vnetserg a00a998
Replace let-else with if-let for 1.63 compat
vnetserg be0d220
Fix build errors
vnetserg 2822d9c
Merge branch 'master' into broadcast_atomic_list
Darksonn 3a2b35b
Move AtomicLinkedList into separate file
vnetserg d30117c
Fix wasi build errors
vnetserg 1ca92d0
Fix fuzzing build
vnetserg 74af0fe
Make waiter.queued atomic, use read lock in notify_rx
vnetserg 7a3a75a
Fix loom build, clippy warnings
vnetserg 685e5d5
Fix build
vnetserg 9ced45b
Dont use downgrade method
vnetserg 96f713c
Fix a bug with memory orders
vnetserg 9d25ae2
Fix comments
vnetserg 6161c55
Implement RwLock wrapper with downgrade method
vnetserg bf9f690
Add RwLock to loom wrappers
vnetserg 18d4d86
Fix loom imports
vnetserg 90b1c0e
Fix another bug in memory orders
vnetserg 4b45a20
Rename Atomic -> ConcurrentPush, fix tail locking in recv_ref
vnetserg 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 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 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,82 @@ | ||
use rand::{Rng, RngCore, SeedableRng}; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::sync::Arc; | ||
use tokio::sync::{broadcast, Notify}; | ||
|
||
use criterion::measurement::WallTime; | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkGroup, Criterion}; | ||
|
||
fn rt() -> tokio::runtime::Runtime { | ||
tokio::runtime::Builder::new_multi_thread() | ||
.worker_threads(6) | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
fn do_work(rng: &mut impl RngCore) -> u32 { | ||
use std::fmt::Write; | ||
let mut message = String::new(); | ||
for i in 1..=10 { | ||
let _ = write!(&mut message, " {i}={}", rng.gen::<f64>()); | ||
} | ||
message | ||
.as_bytes() | ||
.iter() | ||
.map(|&c| c as u32) | ||
.fold(0, u32::wrapping_add) | ||
} | ||
|
||
fn contention_impl<const N_TASKS: usize>(g: &mut BenchmarkGroup<WallTime>) { | ||
let rt = rt(); | ||
|
||
let (tx, _rx) = broadcast::channel::<usize>(1000); | ||
let wg = Arc::new((AtomicUsize::new(0), Notify::new())); | ||
|
||
for n in 0..N_TASKS { | ||
let wg = wg.clone(); | ||
let mut rx = tx.subscribe(); | ||
let mut rng = rand::rngs::StdRng::seed_from_u64(n as u64); | ||
rt.spawn(async move { | ||
while let Ok(_) = rx.recv().await { | ||
let r = do_work(&mut rng); | ||
let _ = black_box(r); | ||
if wg.0.fetch_sub(1, Ordering::Relaxed) == 1 { | ||
wg.1.notify_one(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
const N_ITERS: usize = 100; | ||
|
||
g.bench_function(N_TASKS.to_string(), |b| { | ||
b.iter(|| { | ||
rt.block_on({ | ||
let wg = wg.clone(); | ||
let tx = tx.clone(); | ||
async move { | ||
for i in 0..N_ITERS { | ||
assert_eq!(wg.0.fetch_add(N_TASKS, Ordering::Relaxed), 0); | ||
tx.send(i).unwrap(); | ||
while wg.0.load(Ordering::Relaxed) > 0 { | ||
wg.1.notified().await; | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}); | ||
} | ||
|
||
fn bench_contention(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("contention"); | ||
contention_impl::<10>(&mut group); | ||
contention_impl::<100>(&mut group); | ||
contention_impl::<500>(&mut group); | ||
contention_impl::<1000>(&mut group); | ||
group.finish(); | ||
} | ||
|
||
criterion_group!(contention, bench_contention); | ||
|
||
criterion_main!(contention); |
This file contains 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 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.
RwLock
implementations tend to be heavier thanMutex
. In this case, it looks like all reads are of numbers. Another option is to make these cellsAtomicUsize
(orAtomicU64
) and require writers to these cells to hold thetail
mutex. Reads can do the atomic read directly.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.
Are you saying that we could make
Tail
fields atomic? That would spare us the need to take a lock in some situations, but the main contention source is adding waiters to the list, which would still have to be done with a lock.