solana/perf/src/packet.rs

181 lines
5.5 KiB
Rust
Raw Normal View History

//! The `packet` module defines data structures and methods to pull data from the network.
pub use solana_sdk::packet::{Meta, Packet, PacketFlags, PACKET_DATA_SIZE};
use {
crate::{cuda_runtime::PinnedVec, recycler::Recycler},
bincode::config::Options,
serde::Serialize,
std::net::SocketAddr,
};
pub const NUM_PACKETS: usize = 1024 * 8;
2021-03-03 10:23:05 -08:00
pub const PACKETS_PER_BATCH: usize = 128;
pub const NUM_RCVMMSGS: usize = 128;
#[derive(Debug, Default, Clone)]
2021-12-11 06:44:15 -08:00
pub struct PacketBatch {
pub packets: PinnedVec<Packet>,
}
2021-12-11 06:44:15 -08:00
pub type PacketBatchRecycler = Recycler<PinnedVec<Packet>>;
2021-12-11 06:44:15 -08:00
impl PacketBatch {
pub fn new(packets: Vec<Packet>) -> Self {
let packets = PinnedVec::from_vec(packets);
Self { packets }
}
pub fn with_capacity(capacity: usize) -> Self {
let packets = PinnedVec::with_capacity(capacity);
2021-12-11 06:44:15 -08:00
PacketBatch { packets }
}
pub fn new_unpinned_with_recycler(
2021-12-11 06:44:15 -08:00
recycler: PacketBatchRecycler,
size: usize,
name: &'static str,
) -> Self {
let mut packets = recycler.allocate(name);
packets.reserve(size);
2021-12-11 06:44:15 -08:00
PacketBatch { packets }
}
2021-12-11 06:44:15 -08:00
pub fn new_with_recycler(
recycler: PacketBatchRecycler,
size: usize,
name: &'static str,
) -> Self {
let mut packets = recycler.allocate(name);
packets.reserve_and_pin(size);
2021-12-11 06:44:15 -08:00
PacketBatch { packets }
}
pub fn new_with_recycler_data(
2021-12-11 06:44:15 -08:00
recycler: &PacketBatchRecycler,
name: &'static str,
mut packets: Vec<Packet>,
) -> Self {
2021-12-11 06:44:15 -08:00
let mut batch = Self::new_with_recycler(recycler.clone(), packets.len(), name);
batch.packets.append(&mut packets);
batch
}
pub fn new_unpinned_with_recycler_data_and_dests<T: Serialize>(
recycler: PacketBatchRecycler,
name: &'static str,
dests_and_data: &[(SocketAddr, T)],
) -> Self {
let mut batch =
PacketBatch::new_unpinned_with_recycler(recycler, dests_and_data.len(), name);
batch
.packets
.resize(dests_and_data.len(), Packet::default());
for ((addr, data), packet) in dests_and_data.iter().zip(batch.packets.iter_mut()) {
if !addr.ip().is_unspecified() && addr.port() != 0 {
if let Err(e) = Packet::populate_packet(packet, Some(addr), &data) {
// TODO: This should never happen. Instead the caller should
// break the payload into smaller messages, and here any errors
// should be propagated.
error!("Couldn't write to packet {:?}. Data skipped.", e);
}
} else {
trace!("Dropping packet, as destination is unknown");
}
}
batch
}
pub fn new_unpinned_with_recycler_data(
2021-12-11 06:44:15 -08:00
recycler: &PacketBatchRecycler,
name: &'static str,
mut packets: Vec<Packet>,
) -> Self {
2021-12-11 06:44:15 -08:00
let mut batch = Self::new_unpinned_with_recycler(recycler.clone(), packets.len(), name);
batch.packets.append(&mut packets);
batch
}
pub fn set_addr(&mut self, addr: &SocketAddr) {
2021-12-11 06:44:15 -08:00
for p in self.packets.iter_mut() {
p.meta.set_addr(addr);
}
}
pub fn is_empty(&self) -> bool {
self.packets.is_empty()
}
}
pub fn to_packet_batches<T: Serialize>(items: &[T], chunk_size: usize) -> Vec<PacketBatch> {
items
.chunks(chunk_size)
.map(|batch_items| {
let mut batch = PacketBatch::with_capacity(batch_items.len());
batch.packets.resize(batch_items.len(), Packet::default());
for (item, packet) in batch_items.iter().zip(batch.packets.iter_mut()) {
Packet::populate_packet(packet, None, item).expect("serialize request");
}
batch
})
.collect()
}
#[cfg(test)]
pub fn to_packet_batches_for_tests<T: Serialize>(items: &[T]) -> Vec<PacketBatch> {
to_packet_batches(items, NUM_PACKETS)
}
pub fn limited_deserialize<T>(data: &[u8]) -> bincode::Result<T>
where
T: serde::de::DeserializeOwned,
{
bincode::options()
.with_limit(PACKET_DATA_SIZE as u64)
.with_fixint_encoding()
.allow_trailing_bytes()
.deserialize_from(data)
}
#[cfg(test)]
mod tests {
use {
super::*,
solana_sdk::{
hash::Hash,
signature::{Keypair, Signer},
system_transaction,
},
};
#[test]
2021-12-11 06:44:15 -08:00
fn test_to_packet_batches() {
let keypair = Keypair::new();
let hash = Hash::new(&[1; 32]);
let tx = system_transaction::transfer(&keypair, &keypair.pubkey(), 1, hash);
2021-12-11 06:44:15 -08:00
let rv = to_packet_batches_for_tests(&[tx.clone(); 1]);
assert_eq!(rv.len(), 1);
assert_eq!(rv[0].packets.len(), 1);
#[allow(clippy::useless_vec)]
2021-12-11 06:44:15 -08:00
let rv = to_packet_batches_for_tests(&vec![tx.clone(); NUM_PACKETS]);
assert_eq!(rv.len(), 1);
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
#[allow(clippy::useless_vec)]
2021-12-11 06:44:15 -08:00
let rv = to_packet_batches_for_tests(&vec![tx; NUM_PACKETS + 1]);
assert_eq!(rv.len(), 2);
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
assert_eq!(rv[1].packets.len(), 1);
}
2019-11-20 10:22:26 -08:00
#[test]
fn test_to_packets_pinning() {
2021-12-11 06:44:15 -08:00
let recycler = PacketBatchRecycler::default();
2019-11-20 10:22:26 -08:00
for i in 0..2 {
2021-12-11 06:44:15 -08:00
let _first_packets =
PacketBatch::new_with_recycler(recycler.clone(), i + 1, "first one");
2019-11-20 10:22:26 -08:00
}
}
}