-
-
Notifications
You must be signed in to change notification settings - Fork 441
bench(udp): run GSO, GRO and recvmmsg permutations #2010
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
5 commits
Select commit
Hold shift + click to select a range
4fdcc0b
ci: test-run benchmarks
mxinden 9aee2b1
refactor(udp/bench): add fn new_socket
mxinden acaed2c
refactor(udp/bench): switch to async
mxinden 53b10b1
fix(udp/bench): enforce max 64k UDP datagram limit
mxinden 8d473c5
feat(udp/bench): support recvmmsg
mxinden 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,128 @@ | ||
use std::{ | ||
cmp::min, | ||
io::{ErrorKind, IoSliceMut}, | ||
net::{Ipv4Addr, Ipv6Addr, UdpSocket}, | ||
}; | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use quinn_udp::{RecvMeta, Transmit, UdpSocketState}; | ||
use std::cmp::min; | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
use std::{io::IoSliceMut, net::UdpSocket, slice}; | ||
use tokio::{io::Interest, runtime::Runtime}; | ||
|
||
use quinn_udp::{RecvMeta, Transmit, UdpSocketState, BATCH_SIZE}; | ||
|
||
pub fn criterion_benchmark(c: &mut Criterion) { | ||
const TOTAL_BYTES: usize = 10 * 1024 * 1024; | ||
// Maximum GSO buffer size is 64k. | ||
const MAX_BUFFER_SIZE: usize = u16::MAX as usize; | ||
const SEGMENT_SIZE: usize = 1280; | ||
|
||
let send = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)) | ||
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0))) | ||
.unwrap(); | ||
let recv = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)) | ||
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0))) | ||
.unwrap(); | ||
let max_segments = min( | ||
UdpSocketState::new((&send).into()) | ||
.unwrap() | ||
.max_gso_segments(), | ||
MAX_BUFFER_SIZE / SEGMENT_SIZE, | ||
); | ||
let dst_addr = recv.local_addr().unwrap(); | ||
let send_state = UdpSocketState::new((&send).into()).unwrap(); | ||
let recv_state = UdpSocketState::new((&recv).into()).unwrap(); | ||
// Reverse non-blocking flag set by `UdpSocketState` to make the test non-racy | ||
recv.set_nonblocking(false).unwrap(); | ||
|
||
let mut receive_buffer = vec![0; MAX_BUFFER_SIZE]; | ||
let mut meta = RecvMeta::default(); | ||
|
||
for gso_enabled in [false, true] { | ||
let mut group = c.benchmark_group(format!("gso_{}", gso_enabled)); | ||
group.throughput(criterion::Throughput::Bytes(TOTAL_BYTES as u64)); | ||
let rt = Runtime::new().unwrap(); | ||
let _guard = rt.enter(); | ||
|
||
let (send_state, send_socket) = new_socket(); | ||
let (recv_state, recv_socket) = new_socket(); | ||
let dst_addr = recv_socket.local_addr().unwrap(); | ||
|
||
let segments = if gso_enabled { max_segments } else { 1 }; | ||
let msg = vec![0xAB; SEGMENT_SIZE * segments]; | ||
let mut permutations = vec![]; | ||
djc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for gso_enabled in [ | ||
false, | ||
#[cfg(any(target_os = "linux", target_os = "windows"))] | ||
true, | ||
djc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] { | ||
for gro_enabled in [false, true] { | ||
#[cfg(target_os = "windows")] | ||
if gso_enabled && !gro_enabled { | ||
// Windows requires receive buffer to fit entire datagram on GRO | ||
// enabled socket. | ||
// | ||
// OS error: "A message sent on a datagram socket was larger | ||
// than the internal message buffer or some other network limit, | ||
// or the buffer used to receive a datagram into was smaller | ||
// than the datagram itself." | ||
continue; | ||
} | ||
|
||
for recvmmsg_enabled in [false, true] { | ||
permutations.push((gso_enabled, gro_enabled, recvmmsg_enabled)); | ||
} | ||
} | ||
} | ||
|
||
for (gso_enabled, gro_enabled, recvmmsg_enabled) in permutations { | ||
let mut group = c.benchmark_group(format!( | ||
"gso_{}_gro_{}_recvmmsg_{}", | ||
gso_enabled, gro_enabled, recvmmsg_enabled | ||
)); | ||
group.throughput(criterion::Throughput::Bytes(TOTAL_BYTES as u64)); | ||
|
||
let gso_segments = if gso_enabled { | ||
send_state.max_gso_segments() | ||
} else { | ||
1 | ||
}; | ||
let msg = vec![0xAB; min(MAX_DATAGRAM_SIZE, SEGMENT_SIZE * gso_segments)]; | ||
djc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let transmit = Transmit { | ||
destination: dst_addr, | ||
ecn: None, | ||
contents: &msg, | ||
segment_size: gso_enabled.then_some(SEGMENT_SIZE), | ||
src_ip: None, | ||
}; | ||
let gro_segments = if gro_enabled { | ||
recv_state.gro_segments() | ||
} else { | ||
1 | ||
}; | ||
let batch_size = if recvmmsg_enabled { BATCH_SIZE } else { 1 }; | ||
|
||
group.bench_function("throughput", |b| { | ||
b.iter(|| { | ||
b.to_async(&rt).iter(|| async { | ||
let mut receive_buffers = vec![vec![0; SEGMENT_SIZE * gro_segments]; batch_size]; | ||
let mut receive_slices = receive_buffers | ||
.iter_mut() | ||
.map(|buf| IoSliceMut::new(buf)) | ||
.collect::<Vec<_>>(); | ||
let mut meta = vec![RecvMeta::default(); batch_size]; | ||
|
||
let mut sent: usize = 0; | ||
let mut received: usize = 0; | ||
while sent < TOTAL_BYTES { | ||
send_state.send((&send).into(), &transmit).unwrap(); | ||
send_socket.writable().await.unwrap(); | ||
send_socket | ||
.try_io(Interest::WRITABLE, || { | ||
send_state.send((&send_socket).into(), &transmit) | ||
}) | ||
.unwrap(); | ||
sent += transmit.contents.len(); | ||
|
||
let mut received_segments = 0; | ||
while received_segments < segments { | ||
let n = recv_state | ||
.recv( | ||
(&recv).into(), | ||
&mut [IoSliceMut::new(&mut receive_buffer)], | ||
slice::from_mut(&mut meta), | ||
) | ||
.unwrap(); | ||
assert_eq!(n, 1); | ||
received_segments += meta.len / meta.stride; | ||
while received < sent { | ||
recv_socket.readable().await.unwrap(); | ||
let n = match recv_socket.try_io(Interest::READABLE, || { | ||
recv_state.recv((&recv_socket).into(), &mut receive_slices, &mut meta) | ||
}) { | ||
Ok(n) => n, | ||
// recv.readable() can lead to false positives. Try again. | ||
Err(e) if e.kind() == ErrorKind::WouldBlock => continue, | ||
e => e.unwrap(), | ||
Ralith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
received += meta.iter().map(|m| m.len).take(n).sum::<usize>(); | ||
} | ||
assert_eq!(received_segments, segments); | ||
} | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
fn new_socket() -> (UdpSocketState, tokio::net::UdpSocket) { | ||
let socket = UdpSocket::bind((Ipv6Addr::LOCALHOST, 0)) | ||
.or_else(|_| UdpSocket::bind((Ipv4Addr::LOCALHOST, 0))) | ||
.unwrap(); | ||
|
||
( | ||
UdpSocketState::new((&socket).into()).unwrap(), | ||
tokio::net::UdpSocket::from_std(socket).unwrap(), | ||
) | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); | ||
|
||
const MAX_IP_UDP_HEADER_SIZE: usize = 48; | ||
const MAX_DATAGRAM_SIZE: usize = u16::MAX as usize - MAX_IP_UDP_HEADER_SIZE; |
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.
Uh oh!
There was an error while loading. Please reload this page.